| 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 return new TestSafeBrowsingService(); |
| 54 } |
| 55 }; |
| 56 |
| 57 class CompletionCallback { |
| 58 public: |
| 59 CompletionCallback() {} |
| 60 |
| 61 UnverifiedDownloadCheckCompletionCallback GetCallback() { |
| 62 completed_ = false; |
| 63 completion_closure_.Reset(); |
| 64 return base::Bind(&CompletionCallback::OnUnverifiedDownloadPolicyAvailable, |
| 65 base::Unretained(this)); |
| 66 } |
| 67 |
| 68 UnverifiedDownloadPolicy WaitForResult() { |
| 69 if (!completed_) { |
| 70 base::RunLoop run_loop; |
| 71 completion_closure_ = run_loop.QuitClosure(); |
| 72 run_loop.Run(); |
| 73 } |
| 74 return result_; |
| 75 } |
| 76 |
| 77 private: |
| 78 void OnUnverifiedDownloadPolicyAvailable(UnverifiedDownloadPolicy policy) { |
| 79 result_ = policy; |
| 80 completed_ = true; |
| 81 if (!completion_closure_.is_null()) |
| 82 base::ResetAndReturn(&completion_closure_).Run(); |
| 83 } |
| 84 |
| 85 bool completed_ = false; |
| 86 base::Closure completion_closure_; |
| 87 UnverifiedDownloadPolicy result_ = UnverifiedDownloadPolicy::DISALLOWED; |
| 88 }; |
| 89 |
| 90 class UnverifiedDownloadPolicyTest : public ::testing::Test { |
| 91 public: |
| 92 void SetUp() override { |
| 93 SafeBrowsingService::RegisterFactory(&test_safe_browsing_service_factory_); |
| 94 testing_safe_browsing_service_ = |
| 95 SafeBrowsingService::CreateSafeBrowsingService(); |
| 96 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); |
| 97 browser_process->SetSafeBrowsingService( |
| 98 testing_safe_browsing_service_.get()); |
| 99 browser_process->safe_browsing_service()->Initialize(); |
| 100 base::RunLoop().RunUntilIdle(); |
| 101 |
| 102 testing_profile_manager_.reset( |
| 103 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 104 ASSERT_TRUE(testing_profile_manager_->SetUp()); |
| 105 |
| 106 testing_profile_ = testing_profile_manager_->CreateTestingProfile("foo"); |
| 107 testing_profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, true); |
| 108 } |
| 109 |
| 110 void TearDown() override { |
| 111 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); |
| 112 browser_process->safe_browsing_service()->ShutDown(); |
| 113 browser_process->SetSafeBrowsingService(nullptr); |
| 114 testing_safe_browsing_service_ = nullptr; |
| 115 SafeBrowsingService::RegisterFactory(nullptr); |
| 116 base::RunLoop().RunUntilIdle(); |
| 117 } |
| 118 |
| 119 protected: |
| 120 content::TestBrowserThreadBundle thread_bundle_; |
| 121 TestSafeBrowsingServiceFactory test_safe_browsing_service_factory_; |
| 122 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 123 scoped_refptr<SafeBrowsingService> testing_safe_browsing_service_; |
| 124 TestingProfile* testing_profile_ = nullptr; |
| 125 }; |
| 126 |
| 127 } // namespace |
| 128 |
| 129 // Verify that SafeBrowsing whitelists can override field trials. |
| 130 TEST_F(UnverifiedDownloadPolicyTest, Whitelist) { |
| 131 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 132 switches::kDisallowUncheckedDangerousDownloads); |
| 133 |
| 134 base::FilePath test_file_path(FILE_PATH_LITERAL("foo.exe")); |
| 135 |
| 136 CompletionCallback completion_callback; |
| 137 CheckUnverifiedDownloadPolicy(GURL(), test_file_path, |
| 138 completion_callback.GetCallback()); |
| 139 EXPECT_EQ(UnverifiedDownloadPolicy::DISALLOWED, |
| 140 completion_callback.WaitForResult()); |
| 141 |
| 142 // Not http/s and hence isn't covered by the whitelist. |
| 143 CheckUnverifiedDownloadPolicy(GURL("ftp://supported.example.com/foo/bar"), |
| 144 test_file_path, |
| 145 completion_callback.GetCallback()); |
| 146 EXPECT_EQ(UnverifiedDownloadPolicy::DISALLOWED, |
| 147 completion_callback.WaitForResult()); |
| 148 |
| 149 CheckUnverifiedDownloadPolicy(GURL("http://supported.example.com/foo/bar"), |
| 150 test_file_path, |
| 151 completion_callback.GetCallback()); |
| 152 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, |
| 153 completion_callback.WaitForResult()); |
| 154 |
| 155 CheckUnverifiedDownloadPolicy(GURL("http://unsupported.example.com/foo/bar"), |
| 156 test_file_path, |
| 157 completion_callback.GetCallback()); |
| 158 EXPECT_EQ(UnverifiedDownloadPolicy::DISALLOWED, |
| 159 completion_callback.WaitForResult()); |
| 160 |
| 161 CheckUnverifiedDownloadPolicy(GURL("https://supported.example.com/foo/bar"), |
| 162 test_file_path, |
| 163 completion_callback.GetCallback()); |
| 164 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, |
| 165 completion_callback.WaitForResult()); |
| 166 } |
| 167 |
| 168 // Verify behavior when the SafeBrowsing service is disabled. |
| 169 TEST_F(UnverifiedDownloadPolicyTest, ServiceDisabled) { |
| 170 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 171 switches::kDisallowUncheckedDangerousDownloads); |
| 172 |
| 173 testing_profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 174 base::RunLoop().RunUntilIdle(); |
| 175 |
| 176 base::FilePath test_file_path(FILE_PATH_LITERAL("foo.exe")); |
| 177 |
| 178 CompletionCallback completion_callback; |
| 179 CheckUnverifiedDownloadPolicy(GURL("http://supported.example.com/foo/bar"), |
| 180 test_file_path, |
| 181 completion_callback.GetCallback()); |
| 182 EXPECT_EQ(UnverifiedDownloadPolicy::ALLOWED, |
| 183 completion_callback.WaitForResult()); |
| 184 } |
| 185 |
| 186 } // namespace safe_browsing |
| OLD | NEW |