| 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( | 49 ASSERT_TRUE( |
| 57 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_)); | 50 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_)); |
| 58 int result = setxattr(test_file_.value().c_str(), | 51 int result = |
| 59 "user.test", "test", 4, 0); | 52 setxattr(test_file_.value().c_str(), "user.test", "test", 4, 0); |
| 60 is_xattr_supported_ = (!result) || (errno != ENOTSUP); | 53 is_xattr_supported_ = (!result) || (errno != ENOTSUP); |
| 61 if (!is_xattr_supported_) { | 54 if (!is_xattr_supported_) { |
| 62 DVLOG(0) << "Test will be skipped because extended attributes are not " | 55 DVLOG(0) << "Test will be skipped because extended attributes are not " |
| 63 << "supported on this OS/file system."; | 56 << "supported on this OS/file system."; |
| 64 } | 57 } |
| 65 } | 58 } |
| 66 | 59 |
| 67 void CheckExtendedAttributeValue(const string attr_name, | 60 void CheckExtendedAttributeValue(const string attr_name, |
| 68 const string expected_value) const { | 61 const string expected_value) const { |
| 69 ssize_t len = getxattr(test_file().value().c_str(), attr_name.c_str(), | 62 ssize_t len = |
| 70 NULL, 0); | 63 getxattr(test_file().value().c_str(), attr_name.c_str(), NULL, 0); |
| 71 if (len <= static_cast<ssize_t>(0)) { | 64 if (len <= static_cast<ssize_t>(0)) { |
| 72 FAIL() << "Attribute '" << attr_name << "' does not exist"; | 65 FAIL() << "Attribute '" << attr_name << "' does not exist"; |
| 73 } | 66 } |
| 74 char* buffer = new char[len]; | 67 char* buffer = new char[len]; |
| 75 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len); | 68 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len); |
| 76 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len)); | 69 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len)); |
| 77 string real_value(buffer, len); | 70 string real_value(buffer, len); |
| 78 delete[] buffer; | 71 delete[] buffer; |
| 79 EXPECT_EQ(expected_value, real_value); | 72 EXPECT_EQ(expected_value, real_value); |
| 80 } | 73 } |
| 81 | 74 |
| 82 void GetExtendedAttributeNames(vector<string>* attr_names) const { | 75 void GetExtendedAttributeNames(vector<string>* attr_names) const { |
| 83 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0); | 76 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0); |
| 84 if (len <= static_cast<ssize_t>(0)) return; | 77 if (len <= static_cast<ssize_t>(0)) |
| 78 return; |
| 85 char* buffer = new char[len]; | 79 char* buffer = new char[len]; |
| 86 len = listxattr(test_file().value().c_str(), buffer, len); | 80 len = listxattr(test_file().value().c_str(), buffer, len); |
| 87 *attr_names = base::SplitString(string(buffer, len), std::string(1, '\0'), | 81 *attr_names = |
| 88 base::TRIM_WHITESPACE, | 82 base::SplitString(string(buffer, len), std::string(1, '\0'), |
| 89 base::SPLIT_WANT_ALL); | 83 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 90 delete[] buffer; | 84 delete[] buffer; |
| 91 } | 85 } |
| 92 | 86 |
| 93 void VerifyAttributesAreSetCorrectly() const { | 87 void VerifyAttributesAreSetCorrectly() const { |
| 94 vector<string> attr_names; | 88 vector<string> attr_names; |
| 95 GetExtendedAttributeNames(&attr_names); | 89 GetExtendedAttributeNames(&attr_names); |
| 96 | 90 |
| 97 // Check if the attributes are set on the file | 91 // Check if the attributes are set on the file |
| 98 vector<string>::const_iterator pos = find(attr_names.begin(), | 92 vector<string>::const_iterator pos = |
| 99 attr_names.end(), kSourceURLAttrName); | 93 find(attr_names.begin(), attr_names.end(), kSourceURLExtendedAttrName); |
| 100 EXPECT_NE(pos, attr_names.end()); | 94 EXPECT_NE(pos, attr_names.end()); |
| 101 pos = find(attr_names.begin(), attr_names.end(), kReferrerURLAttrName); | 95 pos = find(attr_names.begin(), attr_names.end(), |
| 96 kReferrerURLExtendedAttrName); |
| 102 EXPECT_NE(pos, attr_names.end()); | 97 EXPECT_NE(pos, attr_names.end()); |
| 103 | 98 |
| 104 // Check if the attribute values are set correctly | 99 // Check if the attribute values are set correctly |
| 105 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec()); | 100 CheckExtendedAttributeValue(kSourceURLExtendedAttrName, |
| 106 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec()); | 101 source_url().spec()); |
| 102 CheckExtendedAttributeValue(kReferrerURLExtendedAttrName, |
| 103 referrer_url().spec()); |
| 107 } | 104 } |
| 108 | 105 |
| 109 private: | 106 private: |
| 110 base::ScopedTempDir temp_dir_; | 107 base::ScopedTempDir temp_dir_; |
| 111 base::FilePath test_file_; | 108 base::FilePath test_file_; |
| 112 GURL source_url_; | 109 GURL source_url_; |
| 113 GURL referrer_url_; | 110 GURL referrer_url_; |
| 114 bool is_xattr_supported_; | 111 bool is_xattr_supported_; |
| 115 }; | 112 }; |
| 116 | 113 |
| 117 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) { | 114 TEST_F(QuarantineLinuxTest, CheckMetadataSetCorrectly) { |
| 118 if (!is_xattr_supported()) return; | 115 if (!is_xattr_supported()) |
| 119 AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | 116 return; |
| 117 EXPECT_EQ( |
| 118 QuarantineFileResult::OK, |
| 119 QuarantineFile(test_file(), source_url(), referrer_url(), std::string())); |
| 120 VerifyAttributesAreSetCorrectly(); | 120 VerifyAttributesAreSetCorrectly(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) { | 123 TEST_F(QuarantineLinuxTest, SetMetadataMultipleTimes) { |
| 124 if (!is_xattr_supported()) return; | 124 if (!is_xattr_supported()) |
| 125 return; |
| 125 GURL dummy_url("http://www.dummy.com"); | 126 GURL dummy_url("http://www.dummy.com"); |
| 126 AddOriginMetadataToFile(test_file(), dummy_url, dummy_url); | 127 EXPECT_EQ(QuarantineFileResult::OK, |
| 127 AddOriginMetadataToFile(test_file(), source_url(), referrer_url()); | 128 QuarantineFile(test_file(), dummy_url, dummy_url, std::string())); |
| 129 EXPECT_EQ( |
| 130 QuarantineFileResult::OK, |
| 131 QuarantineFile(test_file(), source_url(), referrer_url(), std::string())); |
| 128 VerifyAttributesAreSetCorrectly(); | 132 VerifyAttributesAreSetCorrectly(); |
| 129 } | 133 } |
| 130 | 134 |
| 131 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) { | 135 TEST_F(QuarantineLinuxTest, InvalidSourceURLTest) { |
| 132 if (!is_xattr_supported()) return; | 136 if (!is_xattr_supported()) |
| 137 return; |
| 133 GURL invalid_url; | 138 GURL invalid_url; |
| 134 vector<string> attr_names; | 139 vector<string> attr_names; |
| 135 AddOriginMetadataToFile(test_file(), invalid_url, referrer_url()); | 140 EXPECT_EQ( |
| 141 QuarantineFileResult::ANNOTATION_FAILED, |
| 142 QuarantineFile(test_file(), invalid_url, referrer_url(), std::string())); |
| 136 GetExtendedAttributeNames(&attr_names); | 143 GetExtendedAttributeNames(&attr_names); |
| 137 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 144 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 138 kSourceURLAttrName)); | 145 kSourceURLExtendedAttrName)); |
| 139 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec()); | 146 CheckExtendedAttributeValue(kReferrerURLExtendedAttrName, |
| 147 referrer_url().spec()); |
| 140 } | 148 } |
| 141 | 149 |
| 142 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) { | 150 TEST_F(QuarantineLinuxTest, InvalidReferrerURLTest) { |
| 143 if (!is_xattr_supported()) return; | 151 if (!is_xattr_supported()) |
| 152 return; |
| 144 GURL invalid_url; | 153 GURL invalid_url; |
| 145 vector<string> attr_names; | 154 vector<string> attr_names; |
| 146 AddOriginMetadataToFile(test_file(), source_url(), invalid_url); | 155 EXPECT_EQ( |
| 156 QuarantineFileResult::OK, |
| 157 QuarantineFile(test_file(), source_url(), invalid_url, std::string())); |
| 147 GetExtendedAttributeNames(&attr_names); | 158 GetExtendedAttributeNames(&attr_names); |
| 148 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 159 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 149 kReferrerURLAttrName)); | 160 kReferrerURLExtendedAttrName)); |
| 150 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec()); | 161 CheckExtendedAttributeValue(kSourceURLExtendedAttrName, source_url().spec()); |
| 151 } | 162 } |
| 152 | 163 |
| 153 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) { | 164 TEST_F(QuarantineLinuxTest, InvalidURLsTest) { |
| 154 if (!is_xattr_supported()) return; | 165 if (!is_xattr_supported()) |
| 166 return; |
| 155 GURL invalid_url; | 167 GURL invalid_url; |
| 156 vector<string> attr_names; | 168 vector<string> attr_names; |
| 157 AddOriginMetadataToFile(test_file(), invalid_url, invalid_url); | 169 EXPECT_EQ( |
| 170 QuarantineFileResult::ANNOTATION_FAILED, |
| 171 QuarantineFile(test_file(), invalid_url, invalid_url, std::string())); |
| 158 GetExtendedAttributeNames(&attr_names); | 172 GetExtendedAttributeNames(&attr_names); |
| 159 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 173 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 160 kSourceURLAttrName)); | 174 kSourceURLExtendedAttrName)); |
| 161 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), | 175 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(), |
| 162 kReferrerURLAttrName)); | 176 kReferrerURLExtendedAttrName)); |
| 163 } | 177 } |
| 164 | 178 |
| 165 } // namespace | 179 } // namespace |
| 166 } // namespace content | 180 } // namespace content |
| OLD | NEW |