| 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 <stddef.h> | |
| 7 #include <sys/types.h> | |
| 8 #include <sys/xattr.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 #include <sstream> | |
| 12 #include <string> | |
| 13 | |
| 14 #include "base/files/file_path.h" | |
| 15 #include "base/files/file_util.h" | |
| 16 #include "base/files/scoped_temp_dir.h" | |
| 17 #include "base/logging.h" | |
| 18 #include "base/strings/string_split.h" | |
| 19 #include "content/browser/download/file_metadata_linux.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace content { | |
| 24 namespace { | |
| 25 | |
| 26 using std::istringstream; | |
| 27 using std::string; | |
| 28 using std::vector; | |
| 29 | |
| 30 class FileMetadataLinuxTest : public testing::Test { | |
| 31 public: | |
| 32 FileMetadataLinuxTest() | |
| 33 : source_url_("http://www.source.com"), | |
| 34 referrer_url_("http://www.referrer.com"), | |
| 35 is_xattr_supported_(false) {} | |
| 36 | |
| 37 const base::FilePath& test_file() const { | |
| 38 return test_file_; | |
| 39 } | |
| 40 | |
| 41 const GURL& source_url() const { | |
| 42 return source_url_; | |
| 43 } | |
| 44 | |
| 45 const GURL& referrer_url() const { | |
| 46 return referrer_url_; | |
| 47 } | |
| 48 | |
| 49 bool is_xattr_supported() const { | |
| 50 return is_xattr_supported_; | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 void SetUp() override { | |
| 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 56 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &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 DVLOG(0) << "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 = base::SplitString(string(buffer, len), std::string(1, '\0'), | |
| 87 base::TRIM_WHITESPACE, | |
| 88 base::SPLIT_WANT_ALL); | |
| 89 delete[] buffer; | |
| 90 } | |
| 91 | |
| 92 void VerifyAttributesAreSetCorrectly() const { | |
| 93 vector<string> attr_names; | |
| 94 GetExtendedAttributeNames(&attr_names); | |
| 95 | |
| 96 // Check if the attributes are set on the file | |
| 97 vector<string>::const_iterator pos = find(attr_names.begin(), | |
| 98 attr_names.end(), kSourceURLAttrName); | |
| 99 EXPECT_NE(pos, attr_names.end()); | |
| 100 pos = find(attr_names.begin(), attr_names.end(), kReferrerURLAttrName); | |
| 101 EXPECT_NE(pos, attr_names.end()); | |
| 102 | |
| 103 // Check if the attribute values are set correctly | |
| 104 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec()); | |
| 105 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec()); | |
| 106 } | |
| 107 | |
| 108 private: | |
| 109 base::ScopedTempDir temp_dir_; | |
| 110 base::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; | |
| 118 AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | |
| 119 VerifyAttributesAreSetCorrectly(); | |
| 120 } | |
| 121 | |
| 122 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) { | |
| 123 if (!is_xattr_supported()) return; | |
| 124 GURL dummy_url("http://www.dummy.com"); | |
| 125 AddOriginMetadataToFile(test_file(), dummy_url, dummy_url); | |
| 126 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 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 kSourceURLAttrName)); | |
| 138 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) { | |
| 142 if (!is_xattr_supported()) return; | |
| 143 GURL invalid_url; | |
| 144 vector<string> attr_names; | |
| 145 AddOriginMetadataToFile(test_file(), source_url(), invalid_url); | |
| 146 GetExtendedAttributeNames(&attr_names); | |
| 147 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
| 148 kReferrerURLAttrName)); | |
| 149 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec()); | |
| 150 } | |
| 151 | |
| 152 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) { | |
| 153 if (!is_xattr_supported()) return; | |
| 154 GURL invalid_url; | |
| 155 vector<string> attr_names; | |
| 156 AddOriginMetadataToFile(test_file(), invalid_url, invalid_url); | |
| 157 GetExtendedAttributeNames(&attr_names); | |
| 158 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
| 159 kSourceURLAttrName)); | |
| 160 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
| 161 kReferrerURLAttrName)); | |
| 162 } | |
| 163 | |
| 164 } // namespace | |
| 165 } // namespace content | |
| OLD | NEW |