| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <sys/xattr.h> | 5 #include <sys/xattr.h> |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/mac/mac_util.h" | 13 #include "base/mac/mac_util.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "content/browser/download/quarantine.h" | 16 #include "content/public/common/quarantine.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/gtest_mac.h" | 18 #include "testing/gtest_mac.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 class QuarantineMacTest : public testing::Test { | 24 class QuarantineMacTest : public testing::Test { |
| 25 public: | 25 public: |
| 26 QuarantineMacTest() | 26 QuarantineMacTest() |
| 27 : source_url_("http://www.source.com"), | 27 : source_url_("http://www.source.example.com"), |
| 28 referrer_url_("http://www.referrer.com") {} | 28 referrer_url_("http://www.referrer.example.com") {} |
| 29 | 29 |
| 30 protected: | 30 protected: |
| 31 void SetUp() override { | 31 void SetUp() override { |
| 32 if (base::mac::IsAtMostOS10_9()) | 32 if (base::mac::IsAtMostOS10_9()) { |
| 33 LOG(WARNING) << "Test suite requires Mac OS X 10.10 or later"; |
| 33 return; | 34 return; |
| 35 } |
| 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 35 ASSERT_TRUE( | 37 ASSERT_TRUE( |
| 36 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_)); | 38 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_)); |
| 37 file_url_.reset([[NSURL alloc] | 39 file_url_.reset([[NSURL alloc] |
| 38 initFileURLWithPath:base::SysUTF8ToNSString(test_file_.value())]); | 40 initFileURLWithPath:base::SysUTF8ToNSString(test_file_.value())]); |
| 39 | 41 |
| 40 base::scoped_nsobject<NSMutableDictionary> properties( | 42 base::scoped_nsobject<NSMutableDictionary> properties( |
| 41 [[NSMutableDictionary alloc] init]); | 43 [[NSMutableDictionary alloc] init]); |
| 42 [properties | 44 [properties |
| 43 setValue:@"com.google.Chrome" | 45 setValue:@"com.google.Chrome" |
| 44 forKey:static_cast<NSString*>(kLSQuarantineAgentBundleIdentifierKey)]; | 46 forKey:static_cast<NSString*>(kLSQuarantineAgentBundleIdentifierKey)]; |
| 45 [properties setValue:@"Google Chrome.app" | 47 [properties setValue:@"Google Chrome.app" |
| 46 forKey:static_cast<NSString*>(kLSQuarantineAgentNameKey)]; | 48 forKey:static_cast<NSString*>(kLSQuarantineAgentNameKey)]; |
| 47 [properties setValue:@(1) forKey:@"kLSQuarantineIsOwnedByCurrentUserKey"]; | 49 [properties setValue:@(1) forKey:@"kLSQuarantineIsOwnedByCurrentUserKey"]; |
| 48 // NSURLQuarantinePropertiesKey is only available on macOS 10.10+. | 50 // NSURLQuarantinePropertiesKey is only available on macOS 10.10+. |
| 49 #pragma clang diagnostic push | 51 #pragma clang diagnostic push |
| 50 #pragma clang diagnostic ignored "-Wunguarded-availability" | 52 #pragma clang diagnostic ignored "-Wunguarded-availability" |
| 51 bool success = [file_url_ setResourceValue:properties | 53 bool success = [file_url_ setResourceValue:properties |
| 52 forKey:NSURLQuarantinePropertiesKey | 54 forKey:NSURLQuarantinePropertiesKey |
| 53 error:nullptr]; | 55 error:nullptr]; |
| 54 #pragma clang diagnostic pop | 56 #pragma clang diagnostic pop |
| 55 ASSERT_TRUE(success); | 57 ASSERT_TRUE(success); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void VerifyAttributesAreSetCorrectly() { | |
| 59 base::scoped_nsobject<NSURL> file_url([[NSURL alloc] | |
| 60 initFileURLWithPath:base::SysUTF8ToNSString(test_file_.value())]); | |
| 61 ASSERT_TRUE(file_url); | |
| 62 | |
| 63 NSError* error = nil; | |
| 64 NSDictionary* properties = nil; | |
| 65 // NSURLQuarantinePropertiesKey is only available on macOS 10.10+. | |
| 66 #pragma clang diagnostic push | |
| 67 #pragma clang diagnostic ignored "-Wunguarded-availability" | |
| 68 BOOL success = [file_url getResourceValue:&properties | |
| 69 forKey:NSURLQuarantinePropertiesKey | |
| 70 error:&error]; | |
| 71 #pragma clang diagnostic pop | |
| 72 ASSERT_TRUE(success); | |
| 73 ASSERT_TRUE(properties); | |
| 74 ASSERT_NSEQ( | |
| 75 [[properties valueForKey:static_cast<NSString*>( | |
| 76 kLSQuarantineOriginURLKey)] description], | |
| 77 base::SysUTF8ToNSString(referrer_url_.spec())); | |
| 78 ASSERT_NSEQ([[properties | |
| 79 valueForKey:static_cast<NSString*>(kLSQuarantineDataURLKey)] | |
| 80 description], | |
| 81 base::SysUTF8ToNSString(source_url_.spec())); | |
| 82 } | |
| 83 | |
| 84 base::ScopedTempDir temp_dir_; | 60 base::ScopedTempDir temp_dir_; |
| 85 base::FilePath test_file_; | 61 base::FilePath test_file_; |
| 86 GURL source_url_; | 62 GURL source_url_; |
| 87 GURL referrer_url_; | 63 GURL referrer_url_; |
| 88 base::scoped_nsobject<NSURL> file_url_; | 64 base::scoped_nsobject<NSURL> file_url_; |
| 89 }; | 65 }; |
| 90 | 66 |
| 91 TEST_F(QuarantineMacTest, CheckMetadataSetCorrectly) { | 67 TEST_F(QuarantineMacTest, CheckMetadataSetCorrectly) { |
| 92 if (base::mac::IsAtMostOS10_9()) | 68 if (base::mac::IsAtMostOS10_9()) |
| 93 return; | 69 return; |
| 94 QuarantineFile(test_file_, source_url_, referrer_url_, ""); | 70 EXPECT_EQ(QuarantineFileResult::OK, |
| 95 VerifyAttributesAreSetCorrectly(); | 71 QuarantineFile(test_file_, source_url_, referrer_url_, "")); |
| 72 EXPECT_TRUE(IsFileQuarantined(test_file_, source_url_, referrer_url_)); |
| 96 } | 73 } |
| 97 | 74 |
| 98 TEST_F(QuarantineMacTest, SetMetadataMultipleTimes) { | 75 TEST_F(QuarantineMacTest, SetMetadataMultipleTimes) { |
| 99 if (base::mac::IsAtMostOS10_9()) | 76 if (base::mac::IsAtMostOS10_9()) |
| 100 return; | 77 return; |
| 101 GURL dummy_url("http://www.dummy.com"); | 78 GURL dummy_url("http://www.dummy.example.com"); |
| 102 QuarantineFile(test_file_, source_url_, referrer_url_, ""); | 79 EXPECT_EQ(QuarantineFileResult::OK, |
| 103 QuarantineFile(test_file_, dummy_url, dummy_url, ""); | 80 QuarantineFile(test_file_, source_url_, referrer_url_, "")); |
| 104 VerifyAttributesAreSetCorrectly(); | 81 EXPECT_EQ(QuarantineFileResult::OK, |
| 82 QuarantineFile(test_file_, dummy_url, dummy_url, "")); |
| 83 EXPECT_TRUE(IsFileQuarantined(test_file_, source_url_, referrer_url_)); |
| 84 } |
| 85 |
| 86 TEST_F(QuarantineMacTest, IsFileQuarantined_NoFile) { |
| 87 if (base::mac::IsAtMostOS10_9()) |
| 88 return; |
| 89 base::FilePath does_not_exist = temp_dir_.GetPath().AppendASCII("a.jar"); |
| 90 EXPECT_FALSE(IsFileQuarantined(does_not_exist, GURL(), GURL())); |
| 91 } |
| 92 |
| 93 TEST_F(QuarantineMacTest, IsFileQuarantined_NoAnnotationsOnFile) { |
| 94 if (base::mac::IsAtMostOS10_9()) |
| 95 return; |
| 96 EXPECT_FALSE(IsFileQuarantined(test_file_, GURL(), GURL())); |
| 97 } |
| 98 |
| 99 TEST_F(QuarantineMacTest, IsFileQuarantined_SourceUrlOnly) { |
| 100 if (base::mac::IsAtMostOS10_9()) |
| 101 return; |
| 102 ASSERT_EQ(QuarantineFileResult::OK, |
| 103 QuarantineFile(test_file_, source_url_, GURL(), std::string())); |
| 104 EXPECT_TRUE(IsFileQuarantined(test_file_, source_url_, GURL())); |
| 105 EXPECT_TRUE(IsFileQuarantined(test_file_, GURL(), GURL())); |
| 106 EXPECT_TRUE(IsFileQuarantined(test_file_, GURL(), referrer_url_)); |
| 107 EXPECT_FALSE(IsFileQuarantined(test_file_, referrer_url_, GURL())); |
| 108 } |
| 109 |
| 110 TEST_F(QuarantineMacTest, IsFileQuarantined_FullMetadata) { |
| 111 if (base::mac::IsAtMostOS10_9()) |
| 112 return; |
| 113 ASSERT_EQ( |
| 114 QuarantineFileResult::OK, |
| 115 QuarantineFile(test_file_, source_url_, referrer_url_, std::string())); |
| 116 EXPECT_TRUE(IsFileQuarantined(test_file_, GURL(), GURL())); |
| 117 EXPECT_TRUE(IsFileQuarantined(test_file_, source_url_, GURL())); |
| 118 EXPECT_TRUE(IsFileQuarantined(test_file_, source_url_, referrer_url_)); |
| 119 EXPECT_TRUE(IsFileQuarantined(test_file_, GURL(), referrer_url_)); |
| 120 EXPECT_FALSE(IsFileQuarantined(test_file_, source_url_, source_url_)); |
| 121 EXPECT_FALSE(IsFileQuarantined(test_file_, referrer_url_, referrer_url_)); |
| 105 } | 122 } |
| 106 | 123 |
| 107 } // namespace | 124 } // namespace |
| 108 } // namespace content | 125 } // namespace content |
| OLD | NEW |