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/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 LOG(INFO) << "ReportMalwareDetails"; | |
lzheng
2010/12/03 01:16:57
Nit: You might want to get rid of these LOG(INFO)s
| |
62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
63 NewRunnableMethod(this, | |
64 &FakeSafeBrowsingService::OnMalwareDetailsDone)); | |
65 } | |
66 | |
67 // Notify the UI thread so it can access details_. | |
68 void OnMalwareDetailsDone() { | |
69 LOG(INFO) << "OnMalwareDetailsDone"; | |
70 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
71 MessageLoopForUI::current()->Quit(); | |
72 } | |
73 | |
74 std::list<scoped_refptr<MalwareDetails> >* GetDetails() { | |
75 return &details_; | |
76 } | |
77 | |
78 std::list<scoped_refptr<MalwareDetails> > details_; | |
79 | |
80 | |
55 private: | 81 private: |
56 base::hash_map<std::string, UrlCheckResult> badurls; | 82 base::hash_map<std::string, UrlCheckResult> badurls; |
57 }; | 83 }; |
58 | 84 |
59 // Factory that creates FakeSafeBrowsingService instances. | 85 // Factory that creates FakeSafeBrowsingService instances. |
60 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | 86 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { |
61 public: | 87 public: |
62 TestSafeBrowsingServiceFactory() { } | 88 TestSafeBrowsingServiceFactory() { } |
63 virtual ~TestSafeBrowsingServiceFactory() { } | 89 virtual ~TestSafeBrowsingServiceFactory() { } |
64 | 90 |
(...skipping 30 matching lines...) Expand all Loading... | |
95 virtual void OnBlockingPageComplete(bool proceed) { | 121 virtual void OnBlockingPageComplete(bool proceed) { |
96 } | 122 } |
97 | 123 |
98 void AddURLResult(const GURL& url, | 124 void AddURLResult(const GURL& url, |
99 SafeBrowsingService::UrlCheckResult checkresult) { | 125 SafeBrowsingService::UrlCheckResult checkresult) { |
100 FakeSafeBrowsingService* service = | 126 FakeSafeBrowsingService* service = |
101 static_cast<FakeSafeBrowsingService*>( | 127 static_cast<FakeSafeBrowsingService*>( |
102 g_browser_process->resource_dispatcher_host()-> | 128 g_browser_process->resource_dispatcher_host()-> |
103 safe_browsing_service()); | 129 safe_browsing_service()); |
104 | 130 |
105 ASSERT_TRUE(service != NULL); | 131 ASSERT_TRUE(service); |
106 service->AddURLResult(url, checkresult); | 132 service->AddURLResult(url, checkresult); |
107 } | 133 } |
108 | 134 |
109 void SendCommand(const std::string& command) { | 135 void SendCommand(const std::string& command) { |
110 TabContents* contents = browser()->GetSelectedTabContents(); | 136 TabContents* contents = browser()->GetSelectedTabContents(); |
111 SafeBrowsingBlockingPage* interstitial_page = | 137 SafeBrowsingBlockingPage* interstitial_page = |
112 static_cast<SafeBrowsingBlockingPage*>( | 138 static_cast<SafeBrowsingBlockingPage*>( |
113 contents->interstitial_page()); | 139 contents->interstitial_page()); |
114 ASSERT_TRUE(interstitial_page); | 140 ASSERT_TRUE(interstitial_page); |
115 interstitial_page->CommandReceived(command); | 141 interstitial_page->CommandReceived(command); |
(...skipping 18 matching lines...) Expand all Loading... | |
134 InterstitialPage* interstitial_page = contents->interstitial_page(); | 160 InterstitialPage* interstitial_page = contents->interstitial_page(); |
135 ASSERT_FALSE(interstitial_page); | 161 ASSERT_FALSE(interstitial_page); |
136 } | 162 } |
137 | 163 |
138 void WaitForNavigation() { | 164 void WaitForNavigation() { |
139 NavigationController* controller = | 165 NavigationController* controller = |
140 &browser()->GetSelectedTabContents()->controller(); | 166 &browser()->GetSelectedTabContents()->controller(); |
141 ui_test_utils::WaitForNavigation(controller); | 167 ui_test_utils::WaitForNavigation(controller); |
142 } | 168 } |
143 | 169 |
170 void AssertReportSent() { | |
171 // When a report is scheduled in the IO thread we should get notified. | |
172 LOG(INFO) << "Waiting for malware reports"; | |
173 ui_test_utils::RunMessageLoop(); | |
174 | |
175 FakeSafeBrowsingService* service = | |
176 static_cast<FakeSafeBrowsingService*>( | |
177 g_browser_process->resource_dispatcher_host()-> | |
178 safe_browsing_service()); | |
179 ASSERT_EQ(1u, service->GetDetails()->size()); | |
180 } | |
181 | |
144 private: | 182 private: |
145 TestSafeBrowsingServiceFactory factory; | 183 TestSafeBrowsingServiceFactory factory; |
146 | 184 |
147 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPageTest); | 185 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPageTest); |
148 }; | 186 }; |
149 | 187 |
150 namespace { | 188 namespace { |
151 | 189 |
152 const char kEmptyPage[] = "files/empty.html"; | 190 const char kEmptyPage[] = "files/empty.html"; |
153 const char kMalwarePage[] = "files/safe_browsing/malware.html"; | 191 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); | 289 AddURLResult(iframe_url, SafeBrowsingService::URL_MALWARE); |
252 | 290 |
253 ui_test_utils::NavigateToURL(browser(), url); | 291 ui_test_utils::NavigateToURL(browser(), url); |
254 | 292 |
255 SendCommand("\"proceed\""); // Simulate the user clicking "proceed" | 293 SendCommand("\"proceed\""); // Simulate the user clicking "proceed" |
256 AssertNoInterstitial(); // Assert the interstitial is gone | 294 AssertNoInterstitial(); // Assert the interstitial is gone |
257 | 295 |
258 EXPECT_EQ(url, browser()->GetSelectedTabContents()->GetURL()); | 296 EXPECT_EQ(url, browser()->GetSelectedTabContents()->GetURL()); |
259 } | 297 } |
260 | 298 |
299 IN_PROC_BROWSER_TEST_F(SafeBrowsingBlockingPageTest, | |
300 MalwareIframeReportDetails) { | |
301 // Enable reporting of malware details. | |
302 browser()->GetProfile()->GetPrefs()->SetBoolean( | |
303 prefs::kSafeBrowsingReportingEnabled, true); | |
304 EXPECT_TRUE(browser()->GetProfile()->GetPrefs()->GetBoolean( | |
305 prefs::kSafeBrowsingReportingEnabled)); | |
306 | |
307 GURL url = test_server()->GetURL(kMalwarePage); | |
308 GURL iframe_url = test_server()->GetURL(kMalwareIframe); | |
309 AddURLResult(iframe_url, SafeBrowsingService::URL_MALWARE); | |
310 | |
311 ui_test_utils::NavigateToURL(browser(), url); | |
312 | |
313 SendCommand("\"proceed\""); // Simulate the user clicking "back" | |
314 AssertNoInterstitial(); // Assert the interstitial is gone | |
315 | |
316 EXPECT_EQ(url, browser()->GetSelectedTabContents()->GetURL()); | |
317 AssertReportSent(); | |
318 } | |
319 | |
261 } // namespace | 320 } // namespace |
OLD | NEW |