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