| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This test creates a fake safebrowsing service, where we can inject | 5 // This test creates a fake safebrowsing service, where we can inject |
| 6 // malware and phishing urls. It then uses a real browser to go to | 6 // malware and phishing urls. It then uses a real browser to go to |
| 7 // these urls, and sends "goback" or "proceed" commands and verifies | 7 // these urls, and sends "goback" or "proceed" commands and verifies |
| 8 // they work. | 8 // they work. |
| 9 | 9 |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/renderer_host/render_process_host.h" | 14 #include "chrome/browser/renderer_host/render_process_host.h" |
| 13 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 15 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 16 #include "chrome/browser/safe_browsing/malware_details.h" |
| 14 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 17 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 15 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 18 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
| 16 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
| 17 #include "chrome/browser/tab_contents/tab_contents_view.h" | 20 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 18 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 20 #include "chrome/test/in_process_browser_test.h" | 24 #include "chrome/test/in_process_browser_test.h" |
| 21 #include "chrome/test/ui_test_utils.h" | 25 #include "chrome/test/ui_test_utils.h" |
| 22 | 26 |
| 23 // A SafeBrowingService class that allows us to inject the malicious URLs. | 27 // A SafeBrowingService class that allows us to inject the malicious URLs. |
| 24 class FakeSafeBrowsingService : public SafeBrowsingService { | 28 class FakeSafeBrowsingService : public SafeBrowsingService { |
| 25 public: | 29 public: |
| 26 FakeSafeBrowsingService() {} | 30 FakeSafeBrowsingService() {} |
| 27 | 31 |
| 28 virtual ~FakeSafeBrowsingService() {} | 32 virtual ~FakeSafeBrowsingService() {} |
| (...skipping 16 matching lines...) Expand all Loading... |
| 45 } | 49 } |
| 46 | 50 |
| 47 void OnCheckDone(std::string url, Client* client) { | 51 void OnCheckDone(std::string url, Client* client) { |
| 48 client->OnUrlCheckResult(GURL(url), badurls[url]); | 52 client->OnUrlCheckResult(GURL(url), badurls[url]); |
| 49 } | 53 } |
| 50 | 54 |
| 51 void AddURLResult(const GURL& url, UrlCheckResult checkresult) { | 55 void AddURLResult(const GURL& url, UrlCheckResult checkresult) { |
| 52 badurls[url.spec()] = checkresult; | 56 badurls[url.spec()] = checkresult; |
| 53 } | 57 } |
| 54 | 58 |
| 59 virtual void ReportMalwareDetails(scoped_refptr<MalwareDetails> details) { |
| 60 details_.push_back(details); |
| 61 // Notify the UI thread, that we got a report. |
| 62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 63 NewRunnableMethod(this, |
| 64 &FakeSafeBrowsingService::OnMalwareDetailsDone)); |
| 65 } |
| 66 |
| 67 void OnMalwareDetailsDone() { |
| 68 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 69 MessageLoopForUI::current()->Quit(); |
| 70 } |
| 71 |
| 72 std::list<scoped_refptr<MalwareDetails> >* GetDetails() { |
| 73 return &details_; |
| 74 } |
| 75 |
| 76 std::list<scoped_refptr<MalwareDetails> > details_; |
| 77 |
| 78 |
| 55 private: | 79 private: |
| 56 base::hash_map<std::string, UrlCheckResult> badurls; | 80 base::hash_map<std::string, UrlCheckResult> badurls; |
| 57 }; | 81 }; |
| 58 | 82 |
| 59 // Factory that creates FakeSafeBrowsingService instances. | 83 // Factory that creates FakeSafeBrowsingService instances. |
| 60 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | 84 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { |
| 61 public: | 85 public: |
| 62 TestSafeBrowsingServiceFactory() { } | 86 TestSafeBrowsingServiceFactory() { } |
| 63 virtual ~TestSafeBrowsingServiceFactory() { } | 87 virtual ~TestSafeBrowsingServiceFactory() { } |
| 64 | 88 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 95 virtual void OnBlockingPageComplete(bool proceed) { | 119 virtual void OnBlockingPageComplete(bool proceed) { |
| 96 } | 120 } |
| 97 | 121 |
| 98 void AddURLResult(const GURL& url, | 122 void AddURLResult(const GURL& url, |
| 99 SafeBrowsingService::UrlCheckResult checkresult) { | 123 SafeBrowsingService::UrlCheckResult checkresult) { |
| 100 FakeSafeBrowsingService* service = | 124 FakeSafeBrowsingService* service = |
| 101 static_cast<FakeSafeBrowsingService*>( | 125 static_cast<FakeSafeBrowsingService*>( |
| 102 g_browser_process->resource_dispatcher_host()-> | 126 g_browser_process->resource_dispatcher_host()-> |
| 103 safe_browsing_service()); | 127 safe_browsing_service()); |
| 104 | 128 |
| 105 ASSERT_TRUE(service != NULL); | 129 ASSERT_TRUE(service); |
| 106 service->AddURLResult(url, checkresult); | 130 service->AddURLResult(url, checkresult); |
| 107 } | 131 } |
| 108 | 132 |
| 109 void SendCommand(const std::string& command) { | 133 void SendCommand(const std::string& command) { |
| 110 TabContents* contents = browser()->GetSelectedTabContents(); | 134 TabContents* contents = browser()->GetSelectedTabContents(); |
| 111 SafeBrowsingBlockingPage* interstitial_page = | 135 SafeBrowsingBlockingPage* interstitial_page = |
| 112 static_cast<SafeBrowsingBlockingPage*>( | 136 static_cast<SafeBrowsingBlockingPage*>( |
| 113 contents->interstitial_page()); | 137 contents->interstitial_page()); |
| 114 ASSERT_TRUE(interstitial_page); | 138 ASSERT_TRUE(interstitial_page); |
| 115 interstitial_page->CommandReceived(command); | 139 interstitial_page->CommandReceived(command); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 134 InterstitialPage* interstitial_page = contents->interstitial_page(); | 158 InterstitialPage* interstitial_page = contents->interstitial_page(); |
| 135 ASSERT_FALSE(interstitial_page); | 159 ASSERT_FALSE(interstitial_page); |
| 136 } | 160 } |
| 137 | 161 |
| 138 void WaitForNavigation() { | 162 void WaitForNavigation() { |
| 139 NavigationController* controller = | 163 NavigationController* controller = |
| 140 &browser()->GetSelectedTabContents()->controller(); | 164 &browser()->GetSelectedTabContents()->controller(); |
| 141 ui_test_utils::WaitForNavigation(controller); | 165 ui_test_utils::WaitForNavigation(controller); |
| 142 } | 166 } |
| 143 | 167 |
| 168 void AssertReportSent() { |
| 169 // When a report is scheduled in the IO thread we should get notified. |
| 170 ui_test_utils::RunMessageLoop(); |
| 171 |
| 172 FakeSafeBrowsingService* service = |
| 173 static_cast<FakeSafeBrowsingService*>( |
| 174 g_browser_process->resource_dispatcher_host()-> |
| 175 safe_browsing_service()); |
| 176 ASSERT_EQ(1u, service->GetDetails()->size()); |
| 177 } |
| 178 |
| 144 private: | 179 private: |
| 145 TestSafeBrowsingServiceFactory factory; | 180 TestSafeBrowsingServiceFactory factory; |
| 146 | 181 |
| 147 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPageTest); | 182 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPageTest); |
| 148 }; | 183 }; |
| 149 | 184 |
| 150 namespace { | 185 namespace { |
| 151 | 186 |
| 152 const char kEmptyPage[] = "files/empty.html"; | 187 const char kEmptyPage[] = "files/empty.html"; |
| 153 const char kMalwarePage[] = "files/safe_browsing/malware.html"; | 188 const char kMalwarePage[] = "files/safe_browsing/malware.html"; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 AddURLResult(iframe_url, SafeBrowsingService::URL_MALWARE); | 286 AddURLResult(iframe_url, SafeBrowsingService::URL_MALWARE); |
| 252 | 287 |
| 253 ui_test_utils::NavigateToURL(browser(), url); | 288 ui_test_utils::NavigateToURL(browser(), url); |
| 254 | 289 |
| 255 SendCommand("\"proceed\""); // Simulate the user clicking "proceed" | 290 SendCommand("\"proceed\""); // Simulate the user clicking "proceed" |
| 256 AssertNoInterstitial(); // Assert the interstitial is gone | 291 AssertNoInterstitial(); // Assert the interstitial is gone |
| 257 | 292 |
| 258 EXPECT_EQ(url, browser()->GetSelectedTabContents()->GetURL()); | 293 EXPECT_EQ(url, browser()->GetSelectedTabContents()->GetURL()); |
| 259 } | 294 } |
| 260 | 295 |
| 296 IN_PROC_BROWSER_TEST_F(SafeBrowsingBlockingPageTest, |
| 297 MalwareIframeReportDetails) { |
| 298 // Enable reporting of malware details. |
| 299 browser()->GetProfile()->GetPrefs()->SetBoolean( |
| 300 prefs::kSafeBrowsingReportingEnabled, true); |
| 301 EXPECT_TRUE(browser()->GetProfile()->GetPrefs()->GetBoolean( |
| 302 prefs::kSafeBrowsingReportingEnabled)); |
| 303 |
| 304 GURL url = test_server()->GetURL(kMalwarePage); |
| 305 GURL iframe_url = test_server()->GetURL(kMalwareIframe); |
| 306 AddURLResult(iframe_url, SafeBrowsingService::URL_MALWARE); |
| 307 |
| 308 ui_test_utils::NavigateToURL(browser(), url); |
| 309 |
| 310 SendCommand("\"proceed\""); // Simulate the user clicking "back" |
| 311 AssertNoInterstitial(); // Assert the interstitial is gone |
| 312 |
| 313 EXPECT_EQ(url, browser()->GetSelectedTabContents()->GetURL()); |
| 314 AssertReportSent(); |
| 315 } |
| 316 |
| 261 } // namespace | 317 } // namespace |
| OLD | NEW |