| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <stddef.h> | 6 #include <stddef.h> |
| 7 #include <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <sys/xattr.h> | 8 #include <sys/xattr.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "content/browser/download/file_metadata_linux.h" | 19 #include "content/browser/download/quarantine.h" |
| 20 #include "content/browser/download/quarantine_constants_linux.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 using std::istringstream; | 27 using std::istringstream; |
| 27 using std::string; | 28 using std::string; |
| 28 using std::vector; | 29 using std::vector; |
| 29 | 30 |
| 30 class FileMetadataLinuxTest : public testing::Test { | 31 class QuarantineLinuxTest : public testing::Test { |
| 31 public: | 32 public: |
| 32 FileMetadataLinuxTest() | 33 QuarantineLinuxTest() |
| 33 : source_url_("http://www.source.com"), | 34 : source_url_("http://www.source.com"), |
| 34 referrer_url_("http://www.referrer.com"), | 35 referrer_url_("http://www.referrer.com"), |
| 35 is_xattr_supported_(false) {} | 36 is_xattr_supported_(false) {} |
| 36 | 37 |
| 37 const base::FilePath& test_file() const { | 38 const base::FilePath& test_file() const { return test_file_; } |
| 38 return test_file_; | |
| 39 } | |
| 40 | 39 |
| 41 const GURL& source_url() const { | 40 const GURL& source_url() const { return source_url_; } |
| 42 return source_url_; | |
| 43 } | |
| 44 | 41 |
| 45 const GURL& referrer_url() const { | 42 const GURL& referrer_url() const { return referrer_url_; } |
| 46 return referrer_url_; | |
| 47 } | |
| 48 | 43 |
| 49 bool is_xattr_supported() const { | 44 bool is_xattr_supported() const { return is_xattr_supported_; } |
| 50 return is_xattr_supported_; | |
| 51 } | |
| 52 | 45 |
| 53 protected: | 46 protected: |
| 54 void SetUp() override { | 47 void SetUp() override { |
| 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 56 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &test_file_)); | 49 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &test_file_)); |
| 57 int result = setxattr(test_file_.value().c_str(), | 50 int result = |
| 58 "user.test", "test", 4, 0); | 51 setxattr(test_file_.value().c_str(), "user.test", "test", 4, 0); |
| 59 is_xattr_supported_ = (!result) || (errno != ENOTSUP); | 52 is_xattr_supported_ = (!result) || (errno != ENOTSUP); |
| 60 if (!is_xattr_supported_) { | 53 if (!is_xattr_supported_) { |
| 61 DVLOG(0) << "Test will be skipped because extended attributes are not " | 54 DVLOG(0) << "Test will be skipped because extended attributes are not " |
| 62 << "supported on this OS/file system."; | 55 << "supported on this OS/file system."; |
| 63 } | 56 } |
| 64 } | 57 } |
| 65 | 58 |
| 66 void CheckExtendedAttributeValue(const string attr_name, | 59 void CheckExtendedAttributeValue(const string attr_name, |
| 67 const string expected_value) const { | 60 const string expected_value) const { |
| 68 ssize_t len = getxattr(test_file().value().c_str(), attr_name.c_str(), | 61 ssize_t len = |
| 69 NULL, 0); | 62 getxattr(test_file().value().c_str(), attr_name.c_str(), NULL, 0); |
| 70 if (len <= static_cast<ssize_t>(0)) { | 63 if (len <= static_cast<ssize_t>(0)) { |
| 71 FAIL() << "Attribute '" << attr_name << "' does not exist"; | 64 FAIL() << "Attribute '" << attr_name << "' does not exist"; |
| 72 } | 65 } |
| 73 char* buffer = new char[len]; | 66 char* buffer = new char[len]; |
| 74 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len); | 67 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len); |
| 75 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len)); | 68 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len)); |
| 76 string real_value(buffer, len); | 69 string real_value(buffer, len); |
| 77 delete[] buffer; | 70 delete[] buffer; |
| 78 EXPECT_EQ(expected_value, real_value); | 71 EXPECT_EQ(expected_value, real_value); |
| 79 } | 72 } |
| 80 | 73 |
| 81 void GetExtendedAttributeNames(vector<string>* attr_names) const { | 74 void GetExtendedAttributeNames(vector<string>* attr_names) const { |
| 82 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0); | 75 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0); |
| 83 if (len <= static_cast<ssize_t>(0)) return; | 76 if (len <= static_cast<ssize_t>(0)) |
| 77 return; |
| 84 char* buffer = new char[len]; | 78 char* buffer = new char[len]; |
| 85 len = listxattr(test_file().value().c_str(), buffer, len); | 79 len = listxattr(test_file().value().c_str(), buffer, len); |
| 86 *attr_names = base::SplitString(string(buffer, len), std::string(1, '\0'), | 80 *attr_names = |
| 87 base::TRIM_WHITESPACE, | 81 base::SplitString(string(buffer, len), std::string(1, '\0'), |
| 88 base::SPLIT_WANT_ALL); | 82 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 89 delete[] buffer; | 83 delete[] buffer; |
| 90 } | 84 } |
| 91 | 85 |
| 92 void VerifyAttributesAreSetCorrectly() const { | 86 void VerifyAttributesAreSetCorrectly() const { |
| 93 vector<string> attr_names; | 87 vector<string> attr_names; |
| 94 GetExtendedAttributeNames(&attr_names); | 88 GetExtendedAttributeNames(&attr_names); |
| 95 | 89 |
| 96 // Check if the attributes are set on the file | 90 // Check if the attributes are set on the file |
| 97 vector<string>::const_iterator pos = find(attr_names.begin(), | 91 vector<string>::const_iterator pos = |
| 98 attr_names.end(), kSourceURLAttrName); | 92 find(attr_names.begin(), attr_names.end(), kSourceURLExtendedAttrName); |
| 99 EXPECT_NE(pos, attr_names.end()); | 93 EXPECT_NE(pos, attr_names.end()); |
| 100 pos = find(attr_names.begin(), attr_names.end(), kReferrerURLAttrName); | 94 pos = find(attr_names.begin(), attr_names.end(), |
| 95 kReferrerURLExtendedAttrName); |
| 101 EXPECT_NE(pos, attr_names.end()); | 96 EXPECT_NE(pos, attr_names.end()); |
| 102 | 97 |
| 103 // Check if the attribute values are set correctly | 98 // Check if the attribute values are set correctly |
| 104 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec()); | 99 CheckExtendedAttributeValue(kSourceURLExtendedAttrName, |
| 105 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec()); | 100 source_url().spec()); |
| 101 CheckExtendedAttributeValue(kReferrerURLExtendedAttrName, |
| 102 referrer_url().spec()); |
| 106 } | 103 } |
| 107 | 104 |
| 108 private: | 105 private: |
| 109 base::ScopedTempDir temp_dir_; | 106 base::ScopedTempDir temp_dir_; |
| 110 base::FilePath test_file_; | 107 base::FilePath test_file_; |
| 111 GURL source_url_; | 108 GURL source_url_; |
| 112 GURL referrer_url_; | 109 GURL referrer_url_; |
| 113 bool is_xattr_supported_; | 110 bool is_xattr_supported_; |
| 114 }; | 111 }; |
| 115 | 112 |
| 116 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) { | 113 TEST_F(QuarantineLinuxTest, CheckMetadataSetCorrectly) { |
| 117 if (!is_xattr_supported()) return; | 114 if (!is_xattr_supported()) |
| 118 AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | 115 return; |
| 116 EXPECT_EQ( |
| 117 QuarantineFileResult::OK, |
| 118 QuarantineFile(test_file(), source_url(), referrer_url(), std::string())); |
| 119 VerifyAttributesAreSetCorrectly(); | 119 VerifyAttributesAreSetCorrectly(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) { | 122 TEST_F(QuarantineLinuxTest, SetMetadataMultipleTimes) { |
| 123 if (!is_xattr_supported()) return; | 123 if (!is_xattr_supported()) |
| 124 return; |
| 124 GURL dummy_url("http://www.dummy.com"); | 125 GURL dummy_url("http://www.dummy.com"); |
| 125 AddOriginMetadataToFile(test_file(), dummy_url, dummy_url); | 126 EXPECT_EQ(QuarantineFileResult::OK, |
| 126 AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | 127 QuarantineFile(test_file(), dummy_url, dummy_url, std::string())); |
| 128 EXPECT_EQ( |
| 129 QuarantineFileResult::OK, |
| 130 QuarantineFile(test_file(), source_url(), referrer_url(), std::string())); |
| 127 VerifyAttributesAreSetCorrectly(); | 131 VerifyAttributesAreSetCorrectly(); |
| 128 } | 132 } |
| 129 | 133 |
| 130 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) { | 134 TEST_F(QuarantineLinuxTest, InvalidSourceURLTest) { |
| 131 if (!is_xattr_supported()) return; | 135 if (!is_xattr_supported()) |
| 136 return; |
| 132 GURL invalid_url; | 137 GURL invalid_url; |
| 133 vector<string> attr_names; | 138 vector<string> attr_names; |
| 134 AddOriginMetadataToFile(test_file(), invalid_url, referrer_url()); | 139 EXPECT_EQ( |
| 140 QuarantineFileResult::ANNOTATION_FAILED, |
| 141 QuarantineFile(test_file(), invalid_url, referrer_url(), std::string())); |
| 135 GetExtendedAttributeNames(&attr_names); | 142 GetExtendedAttributeNames(&attr_names); |
| 136 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 143 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 137 kSourceURLAttrName)); | 144 kSourceURLExtendedAttrName)); |
| 138 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec()); | 145 CheckExtendedAttributeValue(kReferrerURLExtendedAttrName, |
| 146 referrer_url().spec()); |
| 139 } | 147 } |
| 140 | 148 |
| 141 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) { | 149 TEST_F(QuarantineLinuxTest, InvalidReferrerURLTest) { |
| 142 if (!is_xattr_supported()) return; | 150 if (!is_xattr_supported()) |
| 151 return; |
| 143 GURL invalid_url; | 152 GURL invalid_url; |
| 144 vector<string> attr_names; | 153 vector<string> attr_names; |
| 145 AddOriginMetadataToFile(test_file(), source_url(), invalid_url); | 154 EXPECT_EQ( |
| 155 QuarantineFileResult::OK, |
| 156 QuarantineFile(test_file(), source_url(), invalid_url, std::string())); |
| 146 GetExtendedAttributeNames(&attr_names); | 157 GetExtendedAttributeNames(&attr_names); |
| 147 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 158 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 148 kReferrerURLAttrName)); | 159 kReferrerURLExtendedAttrName)); |
| 149 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec()); | 160 CheckExtendedAttributeValue(kSourceURLExtendedAttrName, source_url().spec()); |
| 150 } | 161 } |
| 151 | 162 |
| 152 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) { | 163 TEST_F(QuarantineLinuxTest, InvalidURLsTest) { |
| 153 if (!is_xattr_supported()) return; | 164 if (!is_xattr_supported()) |
| 165 return; |
| 154 GURL invalid_url; | 166 GURL invalid_url; |
| 155 vector<string> attr_names; | 167 vector<string> attr_names; |
| 156 AddOriginMetadataToFile(test_file(), invalid_url, invalid_url); | 168 EXPECT_EQ( |
| 169 QuarantineFileResult::ANNOTATION_FAILED, |
| 170 QuarantineFile(test_file(), invalid_url, invalid_url, std::string())); |
| 157 GetExtendedAttributeNames(&attr_names); | 171 GetExtendedAttributeNames(&attr_names); |
| 158 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 172 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 159 kSourceURLAttrName)); | 173 kSourceURLExtendedAttrName)); |
| 160 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 174 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 161 kReferrerURLAttrName)); | 175 kReferrerURLExtendedAttrName)); |
| 162 } | 176 } |
| 163 | 177 |
| 164 } // namespace | 178 } // namespace |
| 165 } // namespace content | 179 } // namespace content |
| OLD | NEW |