| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <sys/xattr.h> | |
| 6 | |
| 7 #import <Foundation/Foundation.h> | |
| 8 | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/files/file_util.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/mac/mac_util.h" | |
| 14 #include "base/mac/scoped_nsobject.h" | |
| 15 #include "base/strings/sys_string_conversions.h" | |
| 16 #include "content/browser/download/quarantine.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include "testing/gtest_mac.h" | |
| 19 #include "url/gurl.h" | |
| 20 | |
| 21 namespace content { | |
| 22 namespace { | |
| 23 | |
| 24 class QuarantineMacTest : public testing::Test { | |
| 25 public: | |
| 26 QuarantineMacTest() | |
| 27 : source_url_("http://www.source.com"), | |
| 28 referrer_url_("http://www.referrer.com") {} | |
| 29 | |
| 30 protected: | |
| 31 void SetUp() override { | |
| 32 if (base::mac::IsAtMostOS10_9()) | |
| 33 return; | |
| 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 35 ASSERT_TRUE( | |
| 36 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_)); | |
| 37 file_url_.reset([[NSURL alloc] | |
| 38 initFileURLWithPath:base::SysUTF8ToNSString(test_file_.value())]); | |
| 39 | |
| 40 base::scoped_nsobject<NSMutableDictionary> properties( | |
| 41 [[NSMutableDictionary alloc] init]); | |
| 42 [properties | |
| 43 setValue:@"com.google.Chrome" | |
| 44 forKey:static_cast<NSString*>(kLSQuarantineAgentBundleIdentifierKey)]; | |
| 45 [properties setValue:@"Google Chrome.app" | |
| 46 forKey:static_cast<NSString*>(kLSQuarantineAgentNameKey)]; | |
| 47 [properties setValue:@(1) forKey:@"kLSQuarantineIsOwnedByCurrentUserKey"]; | |
| 48 // NSURLQuarantinePropertiesKey is only available on macOS 10.10+. | |
| 49 #pragma clang diagnostic push | |
| 50 #pragma clang diagnostic ignored "-Wunguarded-availability" | |
| 51 bool success = [file_url_ setResourceValue:properties | |
| 52 forKey:NSURLQuarantinePropertiesKey | |
| 53 error:nullptr]; | |
| 54 #pragma clang diagnostic pop | |
| 55 ASSERT_TRUE(success); | |
| 56 } | |
| 57 | |
| 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_; | |
| 85 base::FilePath test_file_; | |
| 86 GURL source_url_; | |
| 87 GURL referrer_url_; | |
| 88 base::scoped_nsobject<NSURL> file_url_; | |
| 89 }; | |
| 90 | |
| 91 TEST_F(QuarantineMacTest, CheckMetadataSetCorrectly) { | |
| 92 if (base::mac::IsAtMostOS10_9()) | |
| 93 return; | |
| 94 QuarantineFile(test_file_, source_url_, referrer_url_, ""); | |
| 95 VerifyAttributesAreSetCorrectly(); | |
| 96 } | |
| 97 | |
| 98 TEST_F(QuarantineMacTest, SetMetadataMultipleTimes) { | |
| 99 if (base::mac::IsAtMostOS10_9()) | |
| 100 return; | |
| 101 GURL dummy_url("http://www.dummy.com"); | |
| 102 QuarantineFile(test_file_, source_url_, referrer_url_, ""); | |
| 103 QuarantineFile(test_file_, dummy_url, dummy_url, ""); | |
| 104 VerifyAttributesAreSetCorrectly(); | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 } // namespace content | |
| OLD | NEW |