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 { |
| 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(), file_metadata::kSourceURLAttrName); |
| 96 EXPECT_NE(pos, attr_names.end()); |
| 97 pos = find(attr_names.begin(), attr_names.end(), |
| 98 file_metadata::kReferrerURLAttrName); |
| 99 EXPECT_NE(pos, attr_names.end()); |
| 100 |
| 101 // Check if the attribute values are set correctly |
| 102 CheckExtendedAttributeValue(file_metadata::kSourceURLAttrName, |
| 103 source_url().spec()); |
| 104 CheckExtendedAttributeValue(file_metadata::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; |
| 118 file_metadata::AddOriginMetadataToFile(test_file(), |
| 119 source_url(), referrer_url()); |
| 120 VerifyAttributesAreSetCorrectly(); |
| 121 } |
| 122 |
| 123 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) { |
| 124 if (!is_xattr_supported()) return; |
| 125 file_metadata::AddOriginMetadataToFile(test_file(), |
| 126 GURL("http://www.dummy.com"), GURL("http://www.dummy.com")); |
| 127 file_metadata::AddOriginMetadataToFile(test_file(), |
| 128 source_url(), referrer_url()); |
| 129 VerifyAttributesAreSetCorrectly(); |
| 130 } |
| 131 |
| 132 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) { |
| 133 if (!is_xattr_supported()) return; |
| 134 GURL invalid_url; |
| 135 vector<string> attr_names; |
| 136 file_metadata::AddOriginMetadataToFile(test_file(), |
| 137 invalid_url, referrer_url()); |
| 138 GetExtendedAttributeNames(&attr_names); |
| 139 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 140 file_metadata::kSourceURLAttrName)); |
| 141 CheckExtendedAttributeValue(file_metadata::kReferrerURLAttrName, |
| 142 referrer_url().spec()); |
| 143 } |
| 144 |
| 145 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) { |
| 146 if (!is_xattr_supported()) return; |
| 147 GURL invalid_url; |
| 148 vector<string> attr_names; |
| 149 file_metadata::AddOriginMetadataToFile(test_file(), |
| 150 source_url(), invalid_url); |
| 151 GetExtendedAttributeNames(&attr_names); |
| 152 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 153 file_metadata::kReferrerURLAttrName)); |
| 154 CheckExtendedAttributeValue(file_metadata::kSourceURLAttrName, |
| 155 source_url().spec()); |
| 156 } |
| 157 |
| 158 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) { |
| 159 if (!is_xattr_supported()) return; |
| 160 GURL invalid_url; |
| 161 vector<string> attr_names; |
| 162 file_metadata::AddOriginMetadataToFile(test_file(), invalid_url, invalid_url); |
| 163 GetExtendedAttributeNames(&attr_names); |
| 164 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 165 file_metadata::kSourceURLAttrName)); |
| 166 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 167 file_metadata::kReferrerURLAttrName)); |
| 168 } |
| 169 |
| 170 } // namespace |
OLD | NEW |