OLD | NEW |
---|---|
(Empty) | |
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_path.h" | |
14 #include "base/file_util.h" | |
15 #include "base/logging.h" | |
16 #include "base/scoped_temp_dir.h" | |
17 #include "base/string_split.h" | |
18 #include "content/browser/download/file_metadata_linux.h" | |
19 #include "googleurl/src/gurl.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace { | |
darin (slow to review)
2012/07/26 18:47:00
you can also put the test in the content namespace
| |
23 | |
24 using std::istringstream; | |
25 using std::string; | |
26 using std::vector; | |
27 | |
28 class FileMetadataLinuxTest : public testing::Test { | |
29 public: | |
30 FileMetadataLinuxTest() | |
31 : source_url_("http://www.source.com"), | |
32 referrer_url_("http://www.referrer.com") {} | |
33 | |
34 const FilePath& test_file() const { | |
35 return test_file_; | |
36 } | |
37 | |
38 const GURL& source_url() const { | |
39 return source_url_; | |
40 } | |
41 | |
42 const GURL& referrer_url() const { | |
43 return referrer_url_; | |
44 } | |
45 | |
46 bool is_xattr_supported() const { | |
47 return is_xattr_supported_; | |
48 } | |
49 | |
50 protected: | |
51 virtual void SetUp() OVERRIDE { | |
52 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
53 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | |
54 &test_file_)); | |
55 int result = setxattr(test_file_.value().c_str(), | |
56 "user.test", "test", 4, 0); | |
57 is_xattr_supported_ = (!result) || (errno != ENOTSUP); | |
58 if (!is_xattr_supported_) { | |
59 LOG(INFO) << "Test will be skipped because extended attributes are not " | |
60 << "supported on this OS/file system."; | |
61 } | |
62 } | |
63 | |
64 void CheckExtendedAttributeValue(const string attr_name, | |
65 const string expected_value) const { | |
66 ssize_t len = getxattr(test_file().value().c_str(), attr_name.c_str(), | |
67 NULL, 0); | |
68 if (len <= static_cast<ssize_t>(0)) { | |
69 FAIL() << "Attribute '" << attr_name << "' does not exist"; | |
70 } | |
71 char* buffer = new char[len]; | |
72 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len); | |
73 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len)); | |
74 string real_value(buffer, len); | |
75 delete[] buffer; | |
76 EXPECT_EQ(expected_value, real_value); | |
77 } | |
78 | |
79 void GetExtendedAttributeNames(vector<string>* attr_names) const { | |
80 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0); | |
81 if (len <= static_cast<ssize_t>(0)) return; | |
82 char* buffer = new char[len]; | |
83 len = listxattr(test_file().value().c_str(), buffer, len); | |
84 attr_names->clear(); | |
85 base::SplitString(string(buffer, len), '\0', attr_names); | |
86 delete[] buffer; | |
87 } | |
88 | |
89 void VerifyAttributesAreSetCorrectly() const { | |
90 vector<string> attr_names; | |
91 GetExtendedAttributeNames(&attr_names); | |
92 | |
93 // Check if the attributes are set on the file | |
94 vector<string>::const_iterator pos = find(attr_names.begin(), | |
95 attr_names.end(), content::kSourceURLAttrName); | |
96 EXPECT_NE(pos, attr_names.end()); | |
97 pos = find(attr_names.begin(), attr_names.end(), | |
98 content::kReferrerURLAttrName); | |
99 EXPECT_NE(pos, attr_names.end()); | |
100 | |
101 // Check if the attribute values are set correctly | |
102 CheckExtendedAttributeValue(content::kSourceURLAttrName, | |
103 source_url().spec()); | |
104 CheckExtendedAttributeValue(content::kReferrerURLAttrName, | |
105 referrer_url().spec()); | |
106 } | |
107 | |
108 private: | |
109 ScopedTempDir temp_dir_; | |
110 FilePath test_file_; | |
111 GURL source_url_; | |
112 GURL referrer_url_; | |
113 bool is_xattr_supported_; | |
114 }; | |
115 | |
116 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) { | |
117 if (!is_xattr_supported()) return; | |
Paweł Hajdan Jr.
2012/08/29 16:17:14
nit: Each statement should be on its own line. Ple
| |
118 content::AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | |
119 VerifyAttributesAreSetCorrectly(); | |
120 } | |
121 | |
122 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) { | |
123 if (!is_xattr_supported()) return; | |
124 content::AddOriginMetadataToFile(test_file(), | |
125 GURL("http://www.dummy.com"), GURL("http://www.dummy.com")); | |
126 content::AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | |
127 VerifyAttributesAreSetCorrectly(); | |
128 } | |
129 | |
130 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) { | |
131 if (!is_xattr_supported()) return; | |
132 GURL invalid_url; | |
133 vector<string> attr_names; | |
134 content::AddOriginMetadataToFile(test_file(), invalid_url, referrer_url()); | |
135 GetExtendedAttributeNames(&attr_names); | |
136 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
137 content::kSourceURLAttrName)); | |
138 CheckExtendedAttributeValue(content::kReferrerURLAttrName, | |
139 referrer_url().spec()); | |
140 } | |
141 | |
142 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) { | |
143 if (!is_xattr_supported()) return; | |
144 GURL invalid_url; | |
145 vector<string> attr_names; | |
146 content::AddOriginMetadataToFile(test_file(), source_url(), invalid_url); | |
147 GetExtendedAttributeNames(&attr_names); | |
148 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
149 content::kReferrerURLAttrName)); | |
150 CheckExtendedAttributeValue(content::kSourceURLAttrName, source_url().spec()); | |
151 } | |
152 | |
153 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) { | |
154 if (!is_xattr_supported()) return; | |
155 GURL invalid_url; | |
156 vector<string> attr_names; | |
157 content::AddOriginMetadataToFile(test_file(), invalid_url, invalid_url); | |
158 GetExtendedAttributeNames(&attr_names); | |
159 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
160 content::kSourceURLAttrName)); | |
161 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
162 content::kReferrerURLAttrName)); | |
163 } | |
164 | |
165 } // namespace | |
OLD | NEW |