felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 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 | |
dcheng | 7bacc0e | 2016-04-11 20:10:54 | [diff] [blame] | 5 | #include "chrome/browser/safe_browsing/ui_manager.h" |
| 6 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 7 | #include "base/run_loop.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 8 | #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 9 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 10 | #include "chrome/browser/safe_browsing/ui_manager.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 11 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 | #include "chrome/test/base/testing_profile.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 13 | #include "components/safe_browsing_db/safe_browsing_prefs.h" |
vakh | fa183fa | 2016-03-29 17:33:37 | [diff] [blame] | 14 | #include "components/safe_browsing_db/util.h" |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 15 | #include "content/public/browser/navigation_entry.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 16 | #include "content/public/browser/render_process_host.h" |
| 17 | #include "content/public/browser/render_view_host.h" |
| 18 | #include "content/public/browser/web_contents.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 19 | #include "content/public/browser/web_contents_delegate.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 20 | #include "content/public/test/test_browser_thread_bundle.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 21 | #include "content/public/test/web_contents_tester.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | #include "url/gurl.h" |
| 24 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 25 | using content::BrowserThread; |
| 26 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 27 | static const char* kGoodURL = "https://www.good.com"; |
| 28 | static const char* kBadURL = "https://www.malware.com"; |
| 29 | static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 30 | static const char* kAnotherBadURL = "https://www.badware.com"; |
| 31 | static const char* kLandingURL = "https://www.landing.com"; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 32 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 33 | namespace safe_browsing { |
| 34 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 35 | class SafeBrowsingCallbackWaiter { |
| 36 | public: |
| 37 | SafeBrowsingCallbackWaiter() {} |
| 38 | |
| 39 | bool callback_called() const { return callback_called_; } |
| 40 | bool proceed() const { return proceed_; } |
| 41 | |
| 42 | void OnBlockingPageDone(bool proceed) { |
| 43 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 44 | callback_called_ = true; |
| 45 | proceed_ = proceed; |
| 46 | loop_.Quit(); |
| 47 | } |
| 48 | |
| 49 | void OnBlockingPageDoneOnIO(bool proceed) { |
| 50 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 51 | BrowserThread::PostTask( |
| 52 | BrowserThread::UI, FROM_HERE, |
| 53 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 54 | base::Unretained(this), proceed)); |
| 55 | } |
| 56 | |
| 57 | void WaitForCallback() { |
| 58 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 59 | loop_.Run(); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | bool callback_called_ = false; |
| 64 | bool proceed_ = false; |
| 65 | base::RunLoop loop_; |
| 66 | }; |
| 67 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 68 | class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 69 | public: |
| 70 | SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {} |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 71 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 72 | ~SafeBrowsingUIManagerTest() override{}; |
| 73 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 74 | void SetUp() override { |
| 75 | SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 76 | ChromeRenderViewHostTestHarness::SetUp(); |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 77 | SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents()); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 78 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 79 | |
| 80 | void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } |
| 81 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 82 | bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource) { |
| 83 | return ui_manager_->IsWhitelisted(resource); |
| 84 | } |
| 85 | |
| 86 | void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 87 | ui_manager_->AddToWhitelistUrlSet( |
| 88 | SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting( |
| 89 | resource), |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame^] | 90 | web_contents(), false, resource.threat_type); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 91 | } |
| 92 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 93 | SafeBrowsingUIManager::UnsafeResource MakeUnsafeResource( |
| 94 | const char* url, |
| 95 | bool is_subresource) { |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 96 | SafeBrowsingUIManager::UnsafeResource resource; |
| 97 | resource.url = GURL(url); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 98 | resource.is_subresource = is_subresource; |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 99 | resource.web_contents_getter = |
| 100 | SafeBrowsingUIManager::UnsafeResource::GetWebContentsGetter( |
| 101 | web_contents()->GetRenderProcessHost()->GetID(), |
| 102 | web_contents()->GetMainFrame()->GetRoutingID()); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 103 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 104 | return resource; |
| 105 | } |
| 106 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 107 | SafeBrowsingUIManager::UnsafeResource MakeUnsafeResourceAndStartNavigation( |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 108 | const char* url) { |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 109 | SafeBrowsingUIManager::UnsafeResource resource = |
| 110 | MakeUnsafeResource(url, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 111 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 112 | // The WC doesn't have a URL without a navigation. A main-frame malware |
| 113 | // unsafe resource must be a pending navigation. |
| 114 | content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 115 | return resource; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 116 | } |
| 117 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 118 | void SimulateBlockingPageDone( |
| 119 | const std::vector<SafeBrowsingUIManager::UnsafeResource>& resources, |
| 120 | bool proceed) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 121 | GURL main_frame_url; |
| 122 | content::NavigationEntry* entry = |
| 123 | web_contents()->GetController().GetVisibleEntry(); |
| 124 | if (entry) |
| 125 | main_frame_url = entry->GetURL(); |
| 126 | |
| 127 | ui_manager_->OnBlockingPageDone(resources, proceed, web_contents(), |
| 128 | main_frame_url); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 129 | } |
| 130 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 131 | protected: |
| 132 | SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); } |
| 133 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 134 | private: |
| 135 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 139 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 140 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 141 | AddToWhitelist(resource); |
| 142 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 143 | } |
| 144 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 145 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) { |
| 146 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 147 | MakeUnsafeResourceAndStartNavigation(kGoodURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 148 | EXPECT_FALSE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 149 | } |
| 150 | |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame^] | 151 | TEST_F(SafeBrowsingUIManagerTest, WhitelistRemembersThreatType) { |
| 152 | SafeBrowsingUIManager::UnsafeResource resource = |
| 153 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
| 154 | AddToWhitelist(resource); |
| 155 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 156 | SBThreatType threat_type; |
| 157 | content::NavigationEntry* entry = |
| 158 | web_contents()->GetController().GetVisibleEntry(); |
| 159 | ASSERT_TRUE(entry); |
| 160 | EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents( |
| 161 | resource.url, resource.is_subresource, entry, |
| 162 | resource.web_contents_getter.Run(), true, &threat_type)); |
| 163 | EXPECT_EQ(resource.threat_type, threat_type); |
| 164 | } |
| 165 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 166 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) { |
| 167 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 168 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 169 | AddToWhitelist(resource); |
| 170 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 171 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 172 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 173 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 174 | SafeBrowsingUIManager::UnsafeResource resource_path = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 175 | MakeUnsafeResourceAndStartNavigation(kBadURLWithPath); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 176 | EXPECT_TRUE(IsWhitelisted(resource_path)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 177 | } |
| 178 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 179 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) { |
| 180 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 181 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 182 | AddToWhitelist(resource); |
| 183 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 184 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 185 | SafeBrowsingUIManager::UnsafeResource resource_phishing = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 186 | MakeUnsafeResource(kBadURL, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 187 | resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 188 | EXPECT_TRUE(IsWhitelisted(resource_phishing)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 189 | } |
| 190 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 191 | TEST_F(SafeBrowsingUIManagerTest, WhitelistWithUnrelatedPendingLoad) { |
| 192 | // Commit load of landing page. |
| 193 | NavigateAndCommit(GURL(kLandingURL)); |
| 194 | { |
| 195 | // Simulate subresource malware hit on the landing page. |
| 196 | SafeBrowsingUIManager::UnsafeResource resource = |
| 197 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 198 | |
| 199 | // Start pending load to unrelated site. |
| 200 | content::WebContentsTester::For(web_contents()) |
| 201 | ->StartNavigation(GURL(kGoodURL)); |
| 202 | |
| 203 | // Whitelist the resource on the landing page. |
| 204 | AddToWhitelist(resource); |
| 205 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 206 | } |
| 207 | |
| 208 | // Commit the pending load of unrelated site. |
| 209 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 210 | { |
| 211 | // The unrelated site is not on the whitelist, even if the same subresource |
| 212 | // was on it. |
| 213 | SafeBrowsingUIManager::UnsafeResource resource = |
| 214 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 215 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 216 | } |
| 217 | |
| 218 | // Navigate back to the original landing url. |
| 219 | NavigateAndCommit(GURL(kLandingURL)); |
| 220 | { |
| 221 | SafeBrowsingUIManager::UnsafeResource resource = |
| 222 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 223 | // Original resource url is whitelisted. |
| 224 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 225 | } |
| 226 | { |
| 227 | // A different malware subresource on the same page is also whitelisted. |
| 228 | // (The whitelist is by the page url, not the resource url.) |
| 229 | SafeBrowsingUIManager::UnsafeResource resource2 = |
| 230 | MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */); |
| 231 | EXPECT_TRUE(IsWhitelisted(resource2)); |
| 232 | } |
| 233 | } |
| 234 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 235 | TEST_F(SafeBrowsingUIManagerTest, UICallbackProceed) { |
| 236 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 237 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 238 | SafeBrowsingCallbackWaiter waiter; |
| 239 | resource.callback = |
| 240 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 241 | base::Unretained(&waiter)); |
| 242 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 243 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 244 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 245 | resources.push_back(resource); |
| 246 | SimulateBlockingPageDone(resources, true); |
| 247 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 248 | waiter.WaitForCallback(); |
| 249 | EXPECT_TRUE(waiter.callback_called()); |
| 250 | EXPECT_TRUE(waiter.proceed()); |
| 251 | } |
| 252 | |
| 253 | TEST_F(SafeBrowsingUIManagerTest, UICallbackDontProceed) { |
| 254 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 255 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 256 | SafeBrowsingCallbackWaiter waiter; |
| 257 | resource.callback = |
| 258 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 259 | base::Unretained(&waiter)); |
| 260 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 261 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 262 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 263 | resources.push_back(resource); |
| 264 | SimulateBlockingPageDone(resources, false); |
| 265 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 266 | waiter.WaitForCallback(); |
| 267 | EXPECT_TRUE(waiter.callback_called()); |
| 268 | EXPECT_FALSE(waiter.proceed()); |
| 269 | } |
| 270 | |
| 271 | TEST_F(SafeBrowsingUIManagerTest, IOCallbackProceed) { |
| 272 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 273 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 274 | SafeBrowsingCallbackWaiter waiter; |
| 275 | resource.callback = |
| 276 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 277 | base::Unretained(&waiter)); |
| 278 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 279 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 280 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 281 | resources.push_back(resource); |
| 282 | SimulateBlockingPageDone(resources, true); |
| 283 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 284 | waiter.WaitForCallback(); |
| 285 | EXPECT_TRUE(waiter.callback_called()); |
| 286 | EXPECT_TRUE(waiter.proceed()); |
| 287 | } |
| 288 | |
| 289 | TEST_F(SafeBrowsingUIManagerTest, IOCallbackDontProceed) { |
| 290 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 291 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 292 | SafeBrowsingCallbackWaiter waiter; |
| 293 | resource.callback = |
| 294 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 295 | base::Unretained(&waiter)); |
| 296 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 297 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 298 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 299 | resources.push_back(resource); |
| 300 | SimulateBlockingPageDone(resources, false); |
| 301 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 302 | waiter.WaitForCallback(); |
| 303 | EXPECT_TRUE(waiter.callback_called()); |
| 304 | EXPECT_FALSE(waiter.proceed()); |
| 305 | } |
| 306 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 307 | namespace { |
| 308 | |
| 309 | // A WebContentsDelegate that records whether |
| 310 | // VisibleSecurityStateChanged() was called. |
| 311 | class SecurityStateWebContentsDelegate : public content::WebContentsDelegate { |
| 312 | public: |
| 313 | SecurityStateWebContentsDelegate() {} |
| 314 | ~SecurityStateWebContentsDelegate() override {} |
| 315 | |
| 316 | bool visible_security_state_changed() const { |
| 317 | return visible_security_state_changed_; |
| 318 | } |
| 319 | |
| 320 | void ClearVisibleSecurityStateChanged() { |
| 321 | visible_security_state_changed_ = false; |
| 322 | } |
| 323 | |
| 324 | // WebContentsDelegate: |
| 325 | void VisibleSecurityStateChanged(content::WebContents* source) override { |
| 326 | visible_security_state_changed_ = true; |
| 327 | } |
| 328 | |
| 329 | private: |
| 330 | bool visible_security_state_changed_ = false; |
| 331 | DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate); |
| 332 | }; |
| 333 | |
| 334 | // A test blocking page that does not create windows. |
| 335 | class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { |
| 336 | public: |
| 337 | TestSafeBrowsingBlockingPage(SafeBrowsingUIManager* manager, |
| 338 | content::WebContents* web_contents, |
| 339 | const GURL& main_frame_url, |
| 340 | const UnsafeResourceList& unsafe_resources) |
| 341 | : SafeBrowsingBlockingPage(manager, |
| 342 | web_contents, |
| 343 | main_frame_url, |
| 344 | unsafe_resources) { |
| 345 | // Don't delay details at all for the unittest. |
| 346 | threat_details_proceed_delay_ms_ = 0; |
| 347 | DontCreateViewForTesting(); |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | // A factory that creates TestSafeBrowsingBlockingPages. |
| 352 | class TestSafeBrowsingBlockingPageFactory |
| 353 | : public SafeBrowsingBlockingPageFactory { |
| 354 | public: |
| 355 | TestSafeBrowsingBlockingPageFactory() {} |
| 356 | ~TestSafeBrowsingBlockingPageFactory() override {} |
| 357 | |
| 358 | SafeBrowsingBlockingPage* CreateSafeBrowsingPage( |
| 359 | SafeBrowsingUIManager* delegate, |
| 360 | content::WebContents* web_contents, |
| 361 | const GURL& main_frame_url, |
| 362 | const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) |
| 363 | override { |
| 364 | return new TestSafeBrowsingBlockingPage(delegate, web_contents, |
| 365 | main_frame_url, unsafe_resources); |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | } // namespace |
| 370 | |
| 371 | // Tests that the WebContentsDelegate is notified of a visible security |
| 372 | // state change when a blocking page is shown for a subresource. |
| 373 | TEST_F(SafeBrowsingUIManagerTest, |
| 374 | VisibleSecurityStateChangedForUnsafeSubresource) { |
| 375 | TestSafeBrowsingBlockingPageFactory factory; |
| 376 | SafeBrowsingBlockingPage::RegisterFactory(&factory); |
| 377 | SecurityStateWebContentsDelegate delegate; |
| 378 | web_contents()->SetDelegate(&delegate); |
| 379 | |
| 380 | // Simulate a blocking page showing for an unsafe subresource. |
| 381 | SafeBrowsingUIManager::UnsafeResource resource = |
| 382 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 383 | // Needed for showing the blocking page. |
| 384 | resource.threat_source = safe_browsing::ThreatSource::REMOTE; |
| 385 | NavigateAndCommit(GURL("http://example.test")); |
| 386 | |
| 387 | delegate.ClearVisibleSecurityStateChanged(); |
| 388 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 389 | ui_manager()->DisplayBlockingPage(resource); |
| 390 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 391 | |
| 392 | // Simulate proceeding through the blocking page. |
| 393 | SafeBrowsingCallbackWaiter waiter; |
| 394 | resource.callback = |
| 395 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 396 | base::Unretained(&waiter)); |
| 397 | resource.callback_thread = |
| 398 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 399 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 400 | resources.push_back(resource); |
| 401 | |
| 402 | delegate.ClearVisibleSecurityStateChanged(); |
| 403 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 404 | SimulateBlockingPageDone(resources, true); |
| 405 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 406 | |
| 407 | waiter.WaitForCallback(); |
| 408 | EXPECT_TRUE(waiter.callback_called()); |
| 409 | EXPECT_TRUE(waiter.proceed()); |
| 410 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 411 | } |
| 412 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 413 | } // namespace safe_browsing |