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.
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"
25 using std::istringstream;
29 class FileMetadataLinuxTest : public testing::Test {
31 FileMetadataLinuxTest()
32 : source_url_("http://www.source.com"),
33 referrer_url_("http://www.referrer.com"),
34 is_xattr_supported_(false) {}
36 const base::FilePath& test_file() const {
40 const GURL& source_url() const {
44 const GURL& referrer_url() const {
48 bool is_xattr_supported() const {
49 return is_xattr_supported_;
53 virtual void SetUp() OVERRIDE {
54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
55 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(),
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.";
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(),
70 if (len <= static_cast<ssize_t>(0)) {
71 FAIL() << "Attribute '" << attr_name << "' does not exist";
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);
78 EXPECT_EQ(expected_value, real_value);
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);
87 base::SplitString(string(buffer, len), '\0', attr_names);
91 void VerifyAttributesAreSetCorrectly() const {
92 vector<string> attr_names;
93 GetExtendedAttributeNames(&attr_names);
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());
102 // Check if the attribute values are set correctly
103 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
104 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
108 base::ScopedTempDir temp_dir_;
109 base::FilePath test_file_;
112 bool is_xattr_supported_;
115 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) {
116 if (!is_xattr_supported()) return;
117 AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
118 VerifyAttributesAreSetCorrectly();
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();
129 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) {
130 if (!is_xattr_supported()) return;
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());
140 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) {
141 if (!is_xattr_supported()) return;
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());
151 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) {
152 if (!is_xattr_supported()) return;
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));
164 } // namespace content