Chromium Code Reviews| 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/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 protected: | |
| 35 virtual void SetUp() OVERRIDE { | |
| 36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 37 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | |
| 38 &test_file_)); | |
| 39 int result = setxattr(test_file_.value().c_str(), | |
| 40 "user.test", "test", 4, 0); | |
| 41 is_xattr_supported_ = (!result) || (errno != ENOTSUP); | |
| 42 } | |
| 43 | |
| 44 void CheckExtendedAttributeValue(const string attr_name, | |
| 45 const string expected_value) const { | |
| 46 ssize_t len = getxattr(test_file_.value().c_str(), attr_name.c_str(), | |
| 47 NULL, 0); | |
| 48 if (len <= static_cast<ssize_t>(0)) { | |
| 49 FAIL() << "Attribute '" << attr_name << "' does not exist"; | |
| 50 } | |
| 51 char* buffer = new char[len]; | |
| 52 len = getxattr(test_file_.value().c_str(), attr_name.c_str(), buffer, len); | |
| 53 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len)); | |
| 54 string real_value(buffer, len); | |
| 55 delete[] buffer; | |
| 56 EXPECT_EQ(expected_value, real_value); | |
| 57 } | |
| 58 | |
| 59 void GetExtendedAttributeNames(vector<string>* attr_names) const { | |
| 60 ssize_t len = listxattr(test_file_.value().c_str(), NULL, 0); | |
| 61 if (len <= static_cast<ssize_t>(0)) return; | |
| 62 char* buffer = new char[len]; | |
| 63 len = listxattr(test_file_.value().c_str(), buffer, len); | |
| 64 attr_names->clear(); | |
| 65 base::SplitString(string(buffer, len), '\0', attr_names); | |
| 66 delete[] buffer; | |
| 67 } | |
| 68 | |
| 69 void VerifyAttributesAreSetCorrectly() const { | |
| 70 vector<string> attr_names; | |
| 71 GetExtendedAttributeNames(&attr_names); | |
| 72 | |
| 73 // Check if the attributes are set on the file | |
| 74 vector<string>::const_iterator pos = find(attr_names.begin(), | |
| 75 attr_names.end(), file_metadata::kSourceURLAttrName); | |
| 76 EXPECT_NE(pos, attr_names.end()); | |
| 77 pos = find(attr_names.begin(), attr_names.end(), | |
| 78 file_metadata::kReferrerURLAttrName); | |
| 79 EXPECT_NE(pos, attr_names.end()); | |
| 80 | |
| 81 // Check if the attribute values are set correctly | |
| 82 CheckExtendedAttributeValue(file_metadata::kSourceURLAttrName, | |
| 83 source_url_.spec()); | |
| 84 CheckExtendedAttributeValue(file_metadata::kReferrerURLAttrName, | |
| 85 referrer_url_.spec()); | |
| 86 } | |
| 87 | |
| 88 ScopedTempDir temp_dir_; | |
|
benjhayden
2012/07/20 17:20:04
Please keep member variables private and expose th
| |
| 89 FilePath test_file_; | |
| 90 GURL source_url_; | |
| 91 GURL referrer_url_; | |
| 92 bool is_xattr_supported_; | |
| 93 }; | |
| 94 | |
| 95 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) { | |
| 96 if (!is_xattr_supported_) { | |
| 97 LOG(INFO) << "Test is skipped because extended attributes are not " | |
|
benjhayden
2012/07/20 17:20:04
Please put this message just once in SetUp instead
| |
| 98 << "supported on this OS/file system."; | |
| 99 return; | |
| 100 } | |
| 101 file_metadata::AddOriginMetadataToFile(test_file_, | |
| 102 source_url_, referrer_url_); | |
| 103 VerifyAttributesAreSetCorrectly(); | |
| 104 } | |
| 105 | |
| 106 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) { | |
| 107 if (!is_xattr_supported_) { | |
| 108 LOG(INFO) << "Test is skipped because extended attributes are not " | |
| 109 << "supported on this OS/file system."; | |
| 110 return; | |
| 111 } | |
| 112 file_metadata::AddOriginMetadataToFile(test_file_, | |
| 113 GURL("http://www.dummy.com"), GURL("http://www.dummy.com")); | |
| 114 file_metadata::AddOriginMetadataToFile(test_file_, | |
| 115 source_url_, referrer_url_); | |
| 116 VerifyAttributesAreSetCorrectly(); | |
| 117 } | |
| 118 | |
| 119 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) { | |
| 120 if (!is_xattr_supported_) { | |
| 121 LOG(INFO) << "Test is skipped because extended attributes are not " | |
| 122 << "supported on this OS/file system."; | |
| 123 return; | |
| 124 } | |
| 125 GURL invalid_url; | |
| 126 vector<string> attr_names; | |
| 127 file_metadata::AddOriginMetadataToFile(test_file_, | |
| 128 invalid_url, referrer_url_); | |
| 129 GetExtendedAttributeNames(&attr_names); | |
| 130 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
| 131 file_metadata::kSourceURLAttrName)); | |
| 132 CheckExtendedAttributeValue(file_metadata::kReferrerURLAttrName, | |
| 133 referrer_url_.spec()); | |
| 134 } | |
| 135 | |
| 136 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) { | |
| 137 if (!is_xattr_supported_) { | |
| 138 LOG(INFO) << "Test is skipped because extended attributes are not " | |
| 139 << "supported on this OS/file system."; | |
| 140 return; | |
| 141 } | |
| 142 GURL invalid_url; | |
| 143 vector<string> attr_names; | |
| 144 file_metadata::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 file_metadata::kReferrerURLAttrName)); | |
| 148 CheckExtendedAttributeValue(file_metadata::kSourceURLAttrName, | |
| 149 source_url_.spec()); | |
| 150 } | |
| 151 | |
| 152 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) { | |
| 153 if (!is_xattr_supported_) { | |
| 154 LOG(INFO) << "Test is skipped because extended attributes are not " | |
| 155 << "supported on this OS/file system."; | |
| 156 return; | |
| 157 } | |
| 158 GURL invalid_url; | |
| 159 vector<string> attr_names; | |
| 160 file_metadata::AddOriginMetadataToFile(test_file_, invalid_url, invalid_url); | |
| 161 GetExtendedAttributeNames(&attr_names); | |
| 162 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
| 163 file_metadata::kSourceURLAttrName)); | |
| 164 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | |
| 165 file_metadata::kReferrerURLAttrName)); | |
| 166 } | |
| 167 | |
| 168 } // namespace | |
| OLD | NEW |