- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / download / file_metadata_unittest_linux.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/xattr.h>
8
9 #include <algorithm>
10 #include <sstream>
11 #include <string>
12
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/logging.h"
17 #include "base/strings/string_split.h"
18 #include "content/browser/download/file_metadata_linux.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
21
22 namespace content {
23 namespace {
24
25 using std::istringstream;
26 using std::string;
27 using std::vector;
28
29 class FileMetadataLinuxTest : public testing::Test {
30  public:
31   FileMetadataLinuxTest()
32       : source_url_("http://www.source.com"),
33         referrer_url_("http://www.referrer.com"),
34         is_xattr_supported_(false) {}
35
36   const base::FilePath& test_file() const {
37     return test_file_;
38   }
39
40   const GURL& source_url() const {
41     return source_url_;
42   }
43
44   const GURL& referrer_url() const {
45     return referrer_url_;
46   }
47
48   bool is_xattr_supported() const {
49     return is_xattr_supported_;
50   }
51
52  protected:
53   virtual void SetUp() OVERRIDE {
54     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
55     ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(),
56                                                     &test_file_));
57     int result = setxattr(test_file_.value().c_str(),
58                           "user.test", "test", 4, 0);
59     is_xattr_supported_ = (!result) || (errno != ENOTSUP);
60     if (!is_xattr_supported_) {
61       LOG(INFO) << "Test will be skipped because extended attributes are not "
62                 << "supported on this OS/file system.";
63     }
64   }
65
66   void CheckExtendedAttributeValue(const string attr_name,
67                                    const string expected_value) const {
68     ssize_t len = getxattr(test_file().value().c_str(), attr_name.c_str(),
69                            NULL, 0);
70     if (len <= static_cast<ssize_t>(0)) {
71       FAIL() << "Attribute '" << attr_name << "' does not exist";
72     }
73     char* buffer = new char[len];
74     len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len);
75     EXPECT_EQ(expected_value.size(), static_cast<size_t>(len));
76     string real_value(buffer, len);
77     delete[] buffer;
78     EXPECT_EQ(expected_value, real_value);
79   }
80
81   void GetExtendedAttributeNames(vector<string>* attr_names) const {
82     ssize_t len = listxattr(test_file().value().c_str(), NULL, 0);
83     if (len <= static_cast<ssize_t>(0)) return;
84     char* buffer = new char[len];
85     len = listxattr(test_file().value().c_str(), buffer, len);
86     attr_names->clear();
87     base::SplitString(string(buffer, len), '\0', attr_names);
88     delete[] buffer;
89   }
90
91   void VerifyAttributesAreSetCorrectly() const {
92     vector<string> attr_names;
93     GetExtendedAttributeNames(&attr_names);
94
95     // Check if the attributes are set on the file
96     vector<string>::const_iterator pos = find(attr_names.begin(),
97         attr_names.end(), kSourceURLAttrName);
98     EXPECT_NE(pos, attr_names.end());
99     pos = find(attr_names.begin(), attr_names.end(), kReferrerURLAttrName);
100     EXPECT_NE(pos, attr_names.end());
101
102     // Check if the attribute values are set correctly
103     CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
104     CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
105   }
106
107  private:
108   base::ScopedTempDir temp_dir_;
109   base::FilePath test_file_;
110   GURL source_url_;
111   GURL referrer_url_;
112   bool is_xattr_supported_;
113 };
114
115 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) {
116   if (!is_xattr_supported()) return;
117   AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
118   VerifyAttributesAreSetCorrectly();
119 }
120
121 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) {
122   if (!is_xattr_supported()) return;
123   GURL dummy_url("http://www.dummy.com");
124   AddOriginMetadataToFile(test_file(), dummy_url, dummy_url);
125   AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
126   VerifyAttributesAreSetCorrectly();
127 }
128
129 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) {
130   if (!is_xattr_supported()) return;
131   GURL invalid_url;
132   vector<string> attr_names;
133   AddOriginMetadataToFile(test_file(), invalid_url, referrer_url());
134   GetExtendedAttributeNames(&attr_names);
135   EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
136       kSourceURLAttrName));
137   CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
138 }
139
140 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) {
141   if (!is_xattr_supported()) return;
142   GURL invalid_url;
143   vector<string> attr_names;
144   AddOriginMetadataToFile(test_file(), source_url(), invalid_url);
145   GetExtendedAttributeNames(&attr_names);
146   EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
147       kReferrerURLAttrName));
148   CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
149 }
150
151 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) {
152   if (!is_xattr_supported()) return;
153   GURL invalid_url;
154   vector<string> attr_names;
155   AddOriginMetadataToFile(test_file(), invalid_url, invalid_url);
156   GetExtendedAttributeNames(&attr_names);
157   EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
158       kSourceURLAttrName));
159   EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
160       kReferrerURLAttrName));
161 }
162
163 }  // namespace
164 }  // namespace content