| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/safe_browsing/unverified_download_policy.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/callback_helpers.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/prefs/testing_pref_service.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 15 #include "chrome/browser/safe_browsing/test_database_manager.h" |
| 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/testing_browser_process.h" |
| 19 #include "chrome/test/base/testing_profile_manager.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace safe_browsing { |
| 25 |
| 26 namespace { |
| 27 |
| 28 class FakeDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 29 public: |
| 30 bool IsSupported() const override { return true; } |
| 31 |
| 32 bool MatchDownloadWhitelistUrl(const GURL& url) override { |
| 33 return url.SchemeIsHTTPOrHTTPS() && url.host() == "supported.example.com"; |
| 34 } |
| 35 |
| 36 protected: |
| 37 ~FakeDatabaseManager() override {} |
| 38 }; |
| 39 |
| 40 class TestSafeBrowsingService : public SafeBrowsingService { |
| 41 public: |
| 42 SafeBrowsingDatabaseManager* CreateDatabaseManager() override { |
| 43 return new FakeDatabaseManager(); |
| 44 } |
| 45 |
| 46 protected: |
| 47 ~TestSafeBrowsingService() override {} |
| 48 }; |
| 49 |
| 50 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { |
| 51 public: |
| 52 SafeBrowsingService* CreateSafeBrowsingService() override { |
| 53 SafeBrowsingService* service = new TestSafeBrowsingService(); |
| 54 return service; |
| 55 } |
| 56 }; |
| 57 |
| 58 class CompletionCallback { |
| 59 public: |
| 60 CompletionCallback() {} |
| 61 |
| 62 UnverifiedDownloadCheckCompletionCallback GetCallback() { |
| 63 completed_ = false; |
| 64 completion_closure_.Reset(); |
| 65 return base::Bind(&CompletionCallback::OnUnverifiedDownloadPolicyAvailable, |
| 66 base::Unretained(this)); |
| 67 } |
| 68 |
| 69 UnverifiedDownloadPolicy WaitForResult() { |
| 70 if (!completed_) { |
| 71 base::RunLoop run_loop; |
| 72 completion_closure_ = run_loop.QuitClosure(); |
| 73 run_loop.Run(); |
| 74 } |
| 75 return result_; |
| 76 } |
| 77 |
| 78 private: |
| 79 void OnUnverifiedDownloadPolicyAvailable(UnverifiedDownloadPolicy policy) { |
| 80 result_ = policy; |
| 81 completed_ = true; |
| 82 if (!completion_closure_.is_null()) |
| 83 base::ResetAndReturn(&completion_closure_).Run(); |
| 84 } |
| 85 |
| 86 bool completed_ = false; |
| 87 base::Closure completion_closure_; |
| 88 UnverifiedDownloadPolicy result_ = UnverifiedDownloadPolicy::DISALLOWED; |
| 89 }; |
| 90 |
| 91 class UnverifiedDownloadPolicyTest : public ::testing::Test { |
| 92 public: |
| 93 void SetUp() override { |
| 94 SafeBrowsingService::RegisterFactory(&test_safe_browsing_service_factory_); |
| 95 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); |
| 96 browser_process->SetSafeBrowsingService( |
| 97 SafeBrowsingService::CreateSafeBrowsingService()); |
| 98 browser_process->safe_browsing_service()->Initialize(); |
| 99 base::RunLoop().RunUntilIdle(); |
| 100 |
| 101 testing_profile_manager_.reset( |
| 102 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 103 ASSERT_TRUE(testing_profile_manager_->SetUp()); |
| 104 |
| 105 testing_profile_ = testing_profile_manager_->CreateTestingProfile("foo"); |
| 106 testing_profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, true); |
| 107 } |
| 108 |
| 109 void TearDown() override { SafeBrowsingService::RegisterFactory(nullptr); } |
| 110 |
| 111 protected: |
| 112 content::TestBrowserThreadBundle thread_bundle_; |
| 113 TestSafeBrowsingServiceFactory test_safe_browsing_service_factory_; |
| 114 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 115 TestingProfile* testing_profile_ = nullptr; |
| 116 }; |
| 117 |
| 118 } // namespace |
| 119 |
| 120 // Verify that SafeBrowsing whitelists can override field trials. |
| 121 TEST_F(UnverifiedDownloadPolicyTest, Whitelist) { |
| 122 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 123 switches::kDisallowUncheckedDangerousDownloads); |
| 124 |
| 125 base::FilePath test_file_path(FILE_PATH_LITERAL("foo.exe")); |
| 126 |
| 127 CompletionCallback completion_callback; |
| 128 CheckUnverifiedDownloadPolicy(GURL(), test_file_path, |
| 129 completion_callback.GetCallback()); |
| 130 EXPECT_EQ(UnverifiedDownloadPolicy::DISALLOWED, |
| 131 completion_callback.WaitForResult()); |
| 132 |
| 133 // Not http/s and hence isn't covered by the whitelist. |
| 134 CheckUnverifiedDownloadPolicy(GURL("ftp://supported.example.com/foo/bar"), |
| 135 test_file_path, |
| 136 completion_callback.GetCallback()); |
| 137 EXPECT_EQ(UnverifiedDownloadPolicy::DISALLOWED, |
| 138 completion_callback.WaitForResult()); |
| 139 |
| 140 CheckUnverifiedDownloadPolicy(GURL("http://supported.example.com/foo/bar"), |
| 141 test_file_path, |
| 142 completion_callback.GetCallback()); |
| 143 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, |
| 144 completion_callback.WaitForResult()); |
| 145 |
| 146 CheckUnverifiedDownloadPolicy(GURL("http://unsupported.example.com/foo/bar"), |
| 147 test_file_path, |
| 148 completion_callback.GetCallback()); |
| 149 EXPECT_EQ(UnverifiedDownloadPolicy::DISALLOWED, |
| 150 completion_callback.WaitForResult()); |
| 151 |
| 152 CheckUnverifiedDownloadPolicy(GURL("https://supported.example.com/foo/bar"), |
| 153 test_file_path, |
| 154 completion_callback.GetCallback()); |
| 155 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, |
| 156 completion_callback.WaitForResult()); |
| 157 } |
| 158 |
| 159 // Verify behavior when the SafeBrowsing service is disabled. |
| 160 TEST_F(UnverifiedDownloadPolicyTest, ServiceDisabled) { |
| 161 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 162 switches::kDisallowUncheckedDangerousDownloads); |
| 163 |
| 164 testing_profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 165 base::RunLoop().RunUntilIdle(); |
| 166 |
| 167 base::FilePath test_file_path(FILE_PATH_LITERAL("foo.exe")); |
| 168 |
| 169 CompletionCallback completion_callback; |
| 170 CheckUnverifiedDownloadPolicy(GURL("http://supported.example.com/foo/bar"), |
| 171 test_file_path, |
| 172 completion_callback.GetCallback()); |
| 173 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, |
| 174 completion_callback.WaitForResult()); |
| 175 } |
| 176 |
| 177 } // namespace safe_browsing |
| OLD | NEW |