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 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | 5 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
6 | 6 |
7 #include "chrome/browser/browser_thread.h" | 7 #include "chrome/browser/browser_thread.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/safe_browsing/malware_details.h" |
8 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 11 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
9 #include "chrome/browser/tab_contents/navigation_entry.h" | 12 #include "chrome/browser/tab_contents/navigation_entry.h" |
10 #include "chrome/browser/tab_contents/test_tab_contents.h" | 13 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 14 #include "chrome/common/pref_names.h" |
11 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
12 #include "chrome/common/render_messages_params.h" | 16 #include "chrome/common/render_messages_params.h" |
13 | 17 |
14 static const char* kGoogleURL = "http://www.google.com/"; | 18 static const char* kGoogleURL = "http://www.google.com/"; |
15 static const char* kGoodURL = "http://www.goodguys.com/"; | 19 static const char* kGoodURL = "http://www.goodguys.com/"; |
16 static const char* kBadURL = "http://www.badguys.com/"; | 20 static const char* kBadURL = "http://www.badguys.com/"; |
17 static const char* kBadURL2 = "http://www.badguys2.com/"; | 21 static const char* kBadURL2 = "http://www.badguys2.com/"; |
18 static const char* kBadURL3 = "http://www.badguys3.com/"; | 22 static const char* kBadURL3 = "http://www.badguys3.com/"; |
19 | 23 |
20 // A SafeBrowingBlockingPage class that does not create windows. | 24 // A SafeBrowingBlockingPage class that does not create windows. |
21 class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { | 25 class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { |
22 public: | 26 public: |
23 TestSafeBrowsingBlockingPage(SafeBrowsingService* service, | 27 TestSafeBrowsingBlockingPage(SafeBrowsingService* service, |
24 TabContents* tab_contents, | 28 TabContents* tab_contents, |
25 const UnsafeResourceList& unsafe_resources) | 29 const UnsafeResourceList& unsafe_resources) |
26 : SafeBrowsingBlockingPage(service, tab_contents, unsafe_resources) { | 30 : SafeBrowsingBlockingPage(service, tab_contents, unsafe_resources) { |
27 } | 31 } |
28 | 32 |
29 // Overriden from InterstitialPage. Don't create a view. | 33 // Overriden from InterstitialPage. Don't create a view. |
30 virtual TabContentsView* CreateTabContentsView() { | 34 virtual TabContentsView* CreateTabContentsView() { |
31 return NULL; | 35 return NULL; |
32 } | 36 } |
33 }; | 37 }; |
34 | 38 |
| 39 class TestSafeBrowsingService: public SafeBrowsingService { |
| 40 public: |
| 41 virtual ~TestSafeBrowsingService() {} |
| 42 virtual void ReportMalwareDetails(scoped_refptr<MalwareDetails> details) { |
| 43 details_.push_back(details); |
| 44 } |
| 45 |
| 46 std::list<scoped_refptr<MalwareDetails> >* GetDetails() { |
| 47 return &details_; |
| 48 } |
| 49 |
| 50 std::list<scoped_refptr<MalwareDetails> > details_; |
| 51 }; |
| 52 |
35 class TestSafeBrowsingBlockingPageFactory | 53 class TestSafeBrowsingBlockingPageFactory |
36 : public SafeBrowsingBlockingPageFactory { | 54 : public SafeBrowsingBlockingPageFactory { |
37 public: | 55 public: |
38 TestSafeBrowsingBlockingPageFactory() { } | 56 TestSafeBrowsingBlockingPageFactory() { } |
39 ~TestSafeBrowsingBlockingPageFactory() { } | 57 ~TestSafeBrowsingBlockingPageFactory() { } |
40 | 58 |
41 virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage( | 59 virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage( |
42 SafeBrowsingService* service, | 60 SafeBrowsingService* service, |
43 TabContents* tab_contents, | 61 TabContents* tab_contents, |
44 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) { | 62 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) { |
45 return new TestSafeBrowsingBlockingPage(service, tab_contents, | 63 return new TestSafeBrowsingBlockingPage(service, tab_contents, |
46 unsafe_resources); | 64 unsafe_resources); |
47 } | 65 } |
48 }; | 66 }; |
49 | 67 |
50 class SafeBrowsingBlockingPageTest : public RenderViewHostTestHarness, | 68 class SafeBrowsingBlockingPageTest : public RenderViewHostTestHarness, |
51 public SafeBrowsingService::Client { | 69 public SafeBrowsingService::Client { |
52 public: | 70 public: |
53 // The decision the user made. | 71 // The decision the user made. |
54 enum UserResponse { | 72 enum UserResponse { |
55 PENDING, | 73 PENDING, |
56 OK, | 74 OK, |
57 CANCEL | 75 CANCEL |
58 }; | 76 }; |
59 | 77 |
60 SafeBrowsingBlockingPageTest() | 78 SafeBrowsingBlockingPageTest() |
61 : ui_thread_(BrowserThread::UI, MessageLoop::current()), | 79 : ui_thread_(BrowserThread::UI, MessageLoop::current()), |
62 io_thread_(BrowserThread::IO, MessageLoop::current()) { | 80 io_thread_(BrowserThread::IO, MessageLoop::current()) { |
63 ResetUserResponse(); | 81 ResetUserResponse(); |
64 service_ = SafeBrowsingService::CreateSafeBrowsingService(); | 82 service_ = new TestSafeBrowsingService(); |
65 } | 83 } |
66 | 84 |
67 virtual void SetUp() { | 85 virtual void SetUp() { |
68 RenderViewHostTestHarness::SetUp(); | 86 RenderViewHostTestHarness::SetUp(); |
69 SafeBrowsingBlockingPage::RegisterFactory(&factory_); | 87 SafeBrowsingBlockingPage::RegisterFactory(&factory_); |
70 ResetUserResponse(); | 88 ResetUserResponse(); |
71 } | 89 } |
72 | 90 |
73 // SafeBrowsingService::Client implementation. | 91 // SafeBrowsingService::Client implementation. |
74 virtual void OnUrlCheckResult(const GURL& url, | 92 virtual void OnUrlCheckResult(const GURL& url, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 MessageLoop::current()->RunAllPending(); | 139 MessageLoop::current()->RunAllPending(); |
122 } | 140 } |
123 | 141 |
124 static void DontProceedThroughInterstitial( | 142 static void DontProceedThroughInterstitial( |
125 SafeBrowsingBlockingPage* sb_interstitial) { | 143 SafeBrowsingBlockingPage* sb_interstitial) { |
126 sb_interstitial->DontProceed(); | 144 sb_interstitial->DontProceed(); |
127 // DontProceed() posts a task to update the SafeBrowsingService::Client. | 145 // DontProceed() posts a task to update the SafeBrowsingService::Client. |
128 MessageLoop::current()->RunAllPending(); | 146 MessageLoop::current()->RunAllPending(); |
129 } | 147 } |
130 | 148 |
| 149 scoped_refptr<TestSafeBrowsingService> service_; |
| 150 |
131 private: | 151 private: |
132 void InitResource(SafeBrowsingService::UnsafeResource* resource, | 152 void InitResource(SafeBrowsingService::UnsafeResource* resource, |
133 ResourceType::Type resource_type, | 153 ResourceType::Type resource_type, |
134 const GURL& url) { | 154 const GURL& url) { |
135 resource->client = this; | 155 resource->client = this; |
136 resource->url = url; | 156 resource->url = url; |
137 resource->resource_type = resource_type; | 157 resource->resource_type = resource_type; |
138 resource->threat_type = SafeBrowsingService::URL_MALWARE; | 158 resource->threat_type = SafeBrowsingService::URL_MALWARE; |
139 resource->render_process_host_id = contents_->GetRenderProcessHost()->id(); | 159 resource->render_process_host_id = contents_->GetRenderProcessHost()->id(); |
140 resource->render_view_id = contents_->render_view_host()->routing_id(); | 160 resource->render_view_id = contents_->render_view_host()->routing_id(); |
141 } | 161 } |
142 | 162 |
143 UserResponse user_response_; | 163 UserResponse user_response_; |
144 scoped_refptr<SafeBrowsingService> service_; | |
145 TestSafeBrowsingBlockingPageFactory factory_; | 164 TestSafeBrowsingBlockingPageFactory factory_; |
146 BrowserThread ui_thread_; | 165 BrowserThread ui_thread_; |
147 BrowserThread io_thread_; | 166 BrowserThread io_thread_; |
148 }; | 167 }; |
149 | 168 |
150 // Tests showing a blocking page for a malware page and not proceeding. | 169 // Tests showing a blocking page for a malware page and not proceeding. |
151 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) { | 170 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) { |
| 171 // Enable malware details. |
| 172 contents()->profile()->GetPrefs()->SetBoolean( |
| 173 prefs::kSafeBrowsingReportingEnabled, true); |
| 174 |
152 // Start a load. | 175 // Start a load. |
153 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); | 176 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); |
154 | 177 |
| 178 |
155 // Simulate the load causing a safe browsing interstitial to be shown. | 179 // Simulate the load causing a safe browsing interstitial to be shown. |
156 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); | 180 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); |
157 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 181 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
158 ASSERT_TRUE(sb_interstitial); | 182 ASSERT_TRUE(sb_interstitial); |
159 | 183 |
160 MessageLoop::current()->RunAllPending(); | 184 MessageLoop::current()->RunAllPending(); |
161 | 185 |
162 // Simulate the user clicking "don't proceed". | 186 // Simulate the user clicking "don't proceed". |
163 DontProceedThroughInterstitial(sb_interstitial); | 187 DontProceedThroughInterstitial(sb_interstitial); |
164 | 188 |
165 // The interstitial should be gone. | 189 // The interstitial should be gone. |
166 EXPECT_EQ(CANCEL, user_response()); | 190 EXPECT_EQ(CANCEL, user_response()); |
167 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 191 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
168 | 192 |
169 // We did not proceed, the pending entry should be gone. | 193 // We did not proceed, the pending entry should be gone. |
170 EXPECT_FALSE(controller().pending_entry()); | 194 EXPECT_FALSE(controller().pending_entry()); |
| 195 |
| 196 // A report should have been sent. |
| 197 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 198 service_->GetDetails()->clear(); |
171 } | 199 } |
172 | 200 |
173 // Tests showing a blocking page for a malware page and then proceeding. | 201 // Tests showing a blocking page for a malware page and then proceeding. |
174 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageProceed) { | 202 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageProceed) { |
| 203 // Enable malware reports. |
| 204 contents()->profile()->GetPrefs()->SetBoolean( |
| 205 prefs::kSafeBrowsingReportingEnabled, true); |
| 206 |
175 // Start a load. | 207 // Start a load. |
176 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); | 208 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); |
177 | 209 |
178 // Simulate the load causing a safe browsing interstitial to be shown. | 210 // Simulate the load causing a safe browsing interstitial to be shown. |
179 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); | 211 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); |
180 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 212 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
181 ASSERT_TRUE(sb_interstitial); | 213 ASSERT_TRUE(sb_interstitial); |
182 | 214 |
183 // Simulate the user clicking "proceed". | 215 // Simulate the user clicking "proceed". |
184 ProceedThroughInterstitial(sb_interstitial); | 216 ProceedThroughInterstitial(sb_interstitial); |
185 | 217 |
186 // The interstitial is shown until the navigation commits. | 218 // The interstitial is shown until the navigation commits. |
187 ASSERT_TRUE(InterstitialPage::GetInterstitialPage(contents())); | 219 ASSERT_TRUE(InterstitialPage::GetInterstitialPage(contents())); |
188 // Commit the navigation. | 220 // Commit the navigation. |
189 Navigate(kBadURL, 1); | 221 Navigate(kBadURL, 1); |
190 // The interstitial should be gone now. | 222 // The interstitial should be gone now. |
191 ASSERT_FALSE(InterstitialPage::GetInterstitialPage(contents())); | 223 ASSERT_FALSE(InterstitialPage::GetInterstitialPage(contents())); |
| 224 |
| 225 // A report should have been sent. |
| 226 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 227 service_->GetDetails()->clear(); |
192 } | 228 } |
193 | 229 |
194 // Tests showing a blocking page for a page that contains malware subresources | 230 // Tests showing a blocking page for a page that contains malware subresources |
195 // and not proceeding. | 231 // and not proceeding. |
196 TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceDontProceed) { | 232 TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceDontProceed) { |
| 233 // Enable malware reports. |
| 234 contents()->profile()->GetPrefs()->SetBoolean( |
| 235 prefs::kSafeBrowsingReportingEnabled, true); |
| 236 |
197 // Navigate somewhere. | 237 // Navigate somewhere. |
198 Navigate(kGoogleURL, 1); | 238 Navigate(kGoogleURL, 1); |
199 | 239 |
200 // Navigate somewhere else. | 240 // Navigate somewhere else. |
201 Navigate(kGoodURL, 2); | 241 Navigate(kGoodURL, 2); |
202 | 242 |
203 // Simulate that page loading a bad-resource triggering an interstitial. | 243 // Simulate that page loading a bad-resource triggering an interstitial. |
204 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); | 244 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); |
205 | 245 |
206 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 246 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
207 ASSERT_TRUE(sb_interstitial); | 247 ASSERT_TRUE(sb_interstitial); |
208 | 248 |
209 // Simulate the user clicking "don't proceed". | 249 // Simulate the user clicking "don't proceed". |
210 DontProceedThroughInterstitial(sb_interstitial); | 250 DontProceedThroughInterstitial(sb_interstitial); |
211 EXPECT_EQ(CANCEL, user_response()); | 251 EXPECT_EQ(CANCEL, user_response()); |
212 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 252 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
213 | 253 |
214 // We did not proceed, we should be back to the first page, the 2nd one should | 254 // We did not proceed, we should be back to the first page, the 2nd one should |
215 // have been removed from the navigation controller. | 255 // have been removed from the navigation controller. |
216 ASSERT_EQ(1, controller().entry_count()); | 256 ASSERT_EQ(1, controller().entry_count()); |
217 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec()); | 257 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec()); |
| 258 |
| 259 // A report should have been sent. |
| 260 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 261 service_->GetDetails()->clear(); |
218 } | 262 } |
219 | 263 |
220 // Tests showing a blocking page for a page that contains malware subresources | 264 // Tests showing a blocking page for a page that contains malware subresources |
221 // and proceeding. | 265 // and proceeding. |
222 TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceProceed) { | 266 TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceProceed) { |
| 267 // Enable malware reports. |
| 268 contents()->profile()->GetPrefs()->SetBoolean( |
| 269 prefs::kSafeBrowsingReportingEnabled, true); |
| 270 |
223 // Navigate somewhere. | 271 // Navigate somewhere. |
224 Navigate(kGoodURL, 1); | 272 Navigate(kGoodURL, 1); |
225 | 273 |
226 // Simulate that page loading a bad-resource triggering an interstitial. | 274 // Simulate that page loading a bad-resource triggering an interstitial. |
227 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); | 275 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); |
228 | 276 |
229 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 277 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
230 ASSERT_TRUE(sb_interstitial); | 278 ASSERT_TRUE(sb_interstitial); |
231 | 279 |
232 // Simulate the user clicking "proceed". | 280 // Simulate the user clicking "proceed". |
233 ProceedThroughInterstitial(sb_interstitial); | 281 ProceedThroughInterstitial(sb_interstitial); |
234 EXPECT_EQ(OK, user_response()); | 282 EXPECT_EQ(OK, user_response()); |
235 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 283 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
236 | 284 |
237 // We did proceed, we should be back to showing the page. | 285 // We did proceed, we should be back to showing the page. |
238 ASSERT_EQ(1, controller().entry_count()); | 286 ASSERT_EQ(1, controller().entry_count()); |
239 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->url().spec()); | 287 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->url().spec()); |
| 288 |
| 289 // A report should have been sent. |
| 290 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 291 service_->GetDetails()->clear(); |
240 } | 292 } |
241 | 293 |
242 // Tests showing a blocking page for a page that contains multiple malware | 294 // Tests showing a blocking page for a page that contains multiple malware |
243 // subresources and not proceeding. This just tests that the extra malware | 295 // subresources and not proceeding. This just tests that the extra malware |
244 // subresources (which trigger queued interstitial pages) do not break anything. | 296 // subresources (which trigger queued interstitial pages) do not break anything. |
245 TEST_F(SafeBrowsingBlockingPageTest, | 297 TEST_F(SafeBrowsingBlockingPageTest, |
246 PageWithMultipleMalwareResourceDontProceed) { | 298 PageWithMultipleMalwareResourceDontProceed) { |
| 299 // Enable malware reports. |
| 300 contents()->profile()->GetPrefs()->SetBoolean( |
| 301 prefs::kSafeBrowsingReportingEnabled, true); |
| 302 |
247 // Navigate somewhere. | 303 // Navigate somewhere. |
248 Navigate(kGoogleURL, 1); | 304 Navigate(kGoogleURL, 1); |
249 | 305 |
250 // Navigate somewhere else. | 306 // Navigate somewhere else. |
251 Navigate(kGoodURL, 2); | 307 Navigate(kGoodURL, 2); |
252 | 308 |
253 // Simulate that page loading a bad-resource triggering an interstitial. | 309 // Simulate that page loading a bad-resource triggering an interstitial. |
254 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); | 310 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); |
255 | 311 |
256 // More bad resources loading causing more interstitials. The new | 312 // More bad resources loading causing more interstitials. The new |
257 // interstitials should be queued. | 313 // interstitials should be queued. |
258 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL2); | 314 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL2); |
259 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL3); | 315 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL3); |
260 | 316 |
261 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 317 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
262 ASSERT_TRUE(sb_interstitial); | 318 ASSERT_TRUE(sb_interstitial); |
263 | 319 |
264 // Simulate the user clicking "don't proceed". | 320 // Simulate the user clicking "don't proceed". |
265 DontProceedThroughInterstitial(sb_interstitial); | 321 DontProceedThroughInterstitial(sb_interstitial); |
266 EXPECT_EQ(CANCEL, user_response()); | 322 EXPECT_EQ(CANCEL, user_response()); |
267 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 323 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
268 | 324 |
269 // We did not proceed, we should be back to the first page, the 2nd one should | 325 // We did not proceed, we should be back to the first page, the 2nd one should |
270 // have been removed from the navigation controller. | 326 // have been removed from the navigation controller. |
271 ASSERT_EQ(1, controller().entry_count()); | 327 ASSERT_EQ(1, controller().entry_count()); |
272 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec()); | 328 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec()); |
| 329 |
| 330 // A report should have been sent. |
| 331 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 332 service_->GetDetails()->clear(); |
273 } | 333 } |
274 | 334 |
275 // Tests showing a blocking page for a page that contains multiple malware | 335 // Tests showing a blocking page for a page that contains multiple malware |
276 // subresources and proceeding through the first interstitial, but not the next. | 336 // subresources and proceeding through the first interstitial, but not the next. |
277 TEST_F(SafeBrowsingBlockingPageTest, | 337 TEST_F(SafeBrowsingBlockingPageTest, |
278 PageWithMultipleMalwareResourceProceedThenDontProceed) { | 338 PageWithMultipleMalwareResourceProceedThenDontProceed) { |
| 339 // Enable malware reports. |
| 340 contents()->profile()->GetPrefs()->SetBoolean( |
| 341 prefs::kSafeBrowsingReportingEnabled, true); |
| 342 |
279 // Navigate somewhere. | 343 // Navigate somewhere. |
280 Navigate(kGoogleURL, 1); | 344 Navigate(kGoogleURL, 1); |
281 | 345 |
282 // Navigate somewhere else. | 346 // Navigate somewhere else. |
283 Navigate(kGoodURL, 2); | 347 Navigate(kGoodURL, 2); |
284 | 348 |
285 // Simulate that page loading a bad-resource triggering an interstitial. | 349 // Simulate that page loading a bad-resource triggering an interstitial. |
286 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); | 350 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); |
287 | 351 |
288 // More bad resources loading causing more interstitials. The new | 352 // More bad resources loading causing more interstitials. The new |
289 // interstitials should be queued. | 353 // interstitials should be queued. |
290 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL2); | 354 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL2); |
291 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL3); | 355 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL3); |
292 | 356 |
293 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 357 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
294 ASSERT_TRUE(sb_interstitial); | 358 ASSERT_TRUE(sb_interstitial); |
295 | 359 |
296 // Proceed through the 1st interstitial. | 360 // Proceed through the 1st interstitial. |
297 ProceedThroughInterstitial(sb_interstitial); | 361 ProceedThroughInterstitial(sb_interstitial); |
298 EXPECT_EQ(OK, user_response()); | 362 EXPECT_EQ(OK, user_response()); |
299 | 363 |
| 364 // A report should have been sent. |
| 365 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 366 service_->GetDetails()->clear(); |
| 367 |
300 ResetUserResponse(); | 368 ResetUserResponse(); |
301 | 369 |
302 // We should land to a 2nd interstitial (aggregating all the malware resources | 370 // We should land to a 2nd interstitial (aggregating all the malware resources |
303 // loaded while the 1st interstitial was showing). | 371 // loaded while the 1st interstitial was showing). |
304 sb_interstitial = GetSafeBrowsingBlockingPage(); | 372 sb_interstitial = GetSafeBrowsingBlockingPage(); |
305 ASSERT_TRUE(sb_interstitial); | 373 ASSERT_TRUE(sb_interstitial); |
306 | 374 |
307 // Don't proceed through the 2nd interstitial. | 375 // Don't proceed through the 2nd interstitial. |
308 DontProceedThroughInterstitial(sb_interstitial); | 376 DontProceedThroughInterstitial(sb_interstitial); |
309 EXPECT_EQ(CANCEL, user_response()); | 377 EXPECT_EQ(CANCEL, user_response()); |
310 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 378 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
311 | 379 |
312 // We did not proceed, we should be back to the first page, the 2nd one should | 380 // We did not proceed, we should be back to the first page, the 2nd one should |
313 // have been removed from the navigation controller. | 381 // have been removed from the navigation controller. |
314 ASSERT_EQ(1, controller().entry_count()); | 382 ASSERT_EQ(1, controller().entry_count()); |
315 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec()); | 383 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->url().spec()); |
| 384 |
| 385 // No report should have been sent -- we don't create a report the |
| 386 // second time. |
| 387 EXPECT_EQ(0u, service_->GetDetails()->size()); |
| 388 service_->GetDetails()->clear(); |
316 } | 389 } |
317 | 390 |
318 // Tests showing a blocking page for a page that contains multiple malware | 391 // Tests showing a blocking page for a page that contains multiple malware |
319 // subresources and proceeding through the multiple interstitials. | 392 // subresources and proceeding through the multiple interstitials. |
320 TEST_F(SafeBrowsingBlockingPageTest, PageWithMultipleMalwareResourceProceed) { | 393 TEST_F(SafeBrowsingBlockingPageTest, PageWithMultipleMalwareResourceProceed) { |
| 394 // Enable malware reports. |
| 395 contents()->profile()->GetPrefs()->SetBoolean( |
| 396 prefs::kSafeBrowsingReportingEnabled, true); |
| 397 |
321 // Navigate somewhere else. | 398 // Navigate somewhere else. |
322 Navigate(kGoodURL, 1); | 399 Navigate(kGoodURL, 1); |
323 | 400 |
324 // Simulate that page loading a bad-resource triggering an interstitial. | 401 // Simulate that page loading a bad-resource triggering an interstitial. |
325 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); | 402 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL); |
326 | 403 |
327 // More bad resources loading causing more interstitials. The new | 404 // More bad resources loading causing more interstitials. The new |
328 // interstitials should be queued. | 405 // interstitials should be queued. |
329 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL2); | 406 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL2); |
330 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL3); | 407 ShowInterstitial(ResourceType::SUB_RESOURCE, kBadURL3); |
331 | 408 |
332 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 409 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
333 ASSERT_TRUE(sb_interstitial); | 410 ASSERT_TRUE(sb_interstitial); |
334 | 411 |
335 // Proceed through the 1st interstitial. | 412 // Proceed through the 1st interstitial. |
336 ProceedThroughInterstitial(sb_interstitial); | 413 ProceedThroughInterstitial(sb_interstitial); |
337 EXPECT_EQ(OK, user_response()); | 414 EXPECT_EQ(OK, user_response()); |
338 | 415 |
| 416 // A report should have been sent. |
| 417 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 418 service_->GetDetails()->clear(); |
| 419 |
339 ResetUserResponse(); | 420 ResetUserResponse(); |
340 | 421 |
341 // We should land to a 2nd interstitial (aggregating all the malware resources | 422 // We should land to a 2nd interstitial (aggregating all the malware resources |
342 // loaded while the 1st interstitial was showing). | 423 // loaded while the 1st interstitial was showing). |
343 sb_interstitial = GetSafeBrowsingBlockingPage(); | 424 sb_interstitial = GetSafeBrowsingBlockingPage(); |
344 ASSERT_TRUE(sb_interstitial); | 425 ASSERT_TRUE(sb_interstitial); |
345 | 426 |
346 // Proceed through the 2nd interstitial. | 427 // Proceed through the 2nd interstitial. |
347 ProceedThroughInterstitial(sb_interstitial); | 428 ProceedThroughInterstitial(sb_interstitial); |
348 EXPECT_EQ(OK, user_response()); | 429 EXPECT_EQ(OK, user_response()); |
349 | 430 |
350 // We did proceed, we should be back to the initial page. | 431 // We did proceed, we should be back to the initial page. |
351 ASSERT_EQ(1, controller().entry_count()); | 432 ASSERT_EQ(1, controller().entry_count()); |
352 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->url().spec()); | 433 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->url().spec()); |
| 434 |
| 435 // No report should have been sent -- we don't create a report the |
| 436 // second time. |
| 437 EXPECT_EQ(0u, service_->GetDetails()->size()); |
| 438 service_->GetDetails()->clear(); |
353 } | 439 } |
354 | 440 |
355 // Tests showing a blocking page then navigating back and forth to make sure the | 441 // Tests showing a blocking page then navigating back and forth to make sure the |
356 // controller entries are OK. http://crbug.com/17627 | 442 // controller entries are OK. http://crbug.com/17627 |
357 TEST_F(SafeBrowsingBlockingPageTest, NavigatingBackAndForth) { | 443 TEST_F(SafeBrowsingBlockingPageTest, NavigatingBackAndForth) { |
| 444 // Enable malware reports. |
| 445 contents()->profile()->GetPrefs()->SetBoolean( |
| 446 prefs::kSafeBrowsingReportingEnabled, true); |
| 447 |
358 // Navigate somewhere. | 448 // Navigate somewhere. |
359 Navigate(kGoodURL, 1); | 449 Navigate(kGoodURL, 1); |
360 | 450 |
361 // Now navigate to a bad page triggerring an interstitial. | 451 // Now navigate to a bad page triggerring an interstitial. |
362 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); | 452 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); |
363 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); | 453 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); |
364 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 454 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
365 ASSERT_TRUE(sb_interstitial); | 455 ASSERT_TRUE(sb_interstitial); |
366 | 456 |
367 // Proceed, then navigate back. | 457 // Proceed, then navigate back. |
(...skipping 13 matching lines...) Expand all Loading... |
381 sb_interstitial = GetSafeBrowsingBlockingPage(); | 471 sb_interstitial = GetSafeBrowsingBlockingPage(); |
382 ASSERT_TRUE(sb_interstitial); | 472 ASSERT_TRUE(sb_interstitial); |
383 | 473 |
384 // Let's proceed and make sure everything is OK (bug 17627). | 474 // Let's proceed and make sure everything is OK (bug 17627). |
385 ProceedThroughInterstitial(sb_interstitial); | 475 ProceedThroughInterstitial(sb_interstitial); |
386 Navigate(kBadURL, 2); // Commit the navigation. | 476 Navigate(kBadURL, 2); // Commit the navigation. |
387 sb_interstitial = GetSafeBrowsingBlockingPage(); | 477 sb_interstitial = GetSafeBrowsingBlockingPage(); |
388 ASSERT_FALSE(sb_interstitial); | 478 ASSERT_FALSE(sb_interstitial); |
389 ASSERT_EQ(2, controller().entry_count()); | 479 ASSERT_EQ(2, controller().entry_count()); |
390 EXPECT_EQ(kBadURL, controller().GetActiveEntry()->url().spec()); | 480 EXPECT_EQ(kBadURL, controller().GetActiveEntry()->url().spec()); |
| 481 |
| 482 // Two reports should have been sent. |
| 483 EXPECT_EQ(2u, service_->GetDetails()->size()); |
| 484 service_->GetDetails()->clear(); |
391 } | 485 } |
392 | 486 |
393 // Tests that calling "don't proceed" after "proceed" has been called doesn't | 487 // Tests that calling "don't proceed" after "proceed" has been called doesn't |
394 // cause problems. http://crbug.com/30079 | 488 // cause problems. http://crbug.com/30079 |
395 TEST_F(SafeBrowsingBlockingPageTest, ProceedThenDontProceed) { | 489 TEST_F(SafeBrowsingBlockingPageTest, ProceedThenDontProceed) { |
| 490 // Enable malware reports. |
| 491 contents()->profile()->GetPrefs()->SetBoolean( |
| 492 prefs::kSafeBrowsingReportingEnabled, true); |
| 493 |
396 // Start a load. | 494 // Start a load. |
397 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); | 495 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); |
398 | 496 |
399 // Simulate the load causing a safe browsing interstitial to be shown. | 497 // Simulate the load causing a safe browsing interstitial to be shown. |
400 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); | 498 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); |
401 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); | 499 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
402 ASSERT_TRUE(sb_interstitial); | 500 ASSERT_TRUE(sb_interstitial); |
403 | 501 |
404 MessageLoop::current()->RunAllPending(); | 502 MessageLoop::current()->RunAllPending(); |
405 | 503 |
406 // Simulate the user clicking "proceed" then "don't proceed" (before the | 504 // Simulate the user clicking "proceed" then "don't proceed" (before the |
407 // interstitial is shown). | 505 // interstitial is shown). |
408 sb_interstitial->Proceed(); | 506 sb_interstitial->Proceed(); |
409 sb_interstitial->DontProceed(); | 507 sb_interstitial->DontProceed(); |
410 // Proceed() and DontProceed() post a task to update the | 508 // Proceed() and DontProceed() post a task to update the |
411 // SafeBrowsingService::Client. | 509 // SafeBrowsingService::Client. |
412 MessageLoop::current()->RunAllPending(); | 510 MessageLoop::current()->RunAllPending(); |
413 | 511 |
414 // The interstitial should be gone. | 512 // The interstitial should be gone. |
415 EXPECT_EQ(OK, user_response()); | 513 EXPECT_EQ(OK, user_response()); |
416 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); | 514 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
| 515 |
| 516 // Only one report should have been sent. |
| 517 EXPECT_EQ(1u, service_->GetDetails()->size()); |
| 518 service_->GetDetails()->clear(); |
417 } | 519 } |
| 520 |
| 521 // Tests showing a blocking page for a malware page with reports disabled. |
| 522 TEST_F(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) { |
| 523 // Disable malware reports. |
| 524 contents()->profile()->GetPrefs()->SetBoolean( |
| 525 prefs::kSafeBrowsingReportingEnabled, false); |
| 526 |
| 527 // Start a load. |
| 528 controller().LoadURL(GURL(kBadURL), GURL(), PageTransition::TYPED); |
| 529 |
| 530 // Simulate the load causing a safe browsing interstitial to be shown. |
| 531 ShowInterstitial(ResourceType::MAIN_FRAME, kBadURL); |
| 532 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage(); |
| 533 ASSERT_TRUE(sb_interstitial); |
| 534 |
| 535 MessageLoop::current()->RunAllPending(); |
| 536 |
| 537 // Simulate the user clicking "don't proceed". |
| 538 DontProceedThroughInterstitial(sb_interstitial); |
| 539 |
| 540 // The interstitial should be gone. |
| 541 EXPECT_EQ(CANCEL, user_response()); |
| 542 EXPECT_FALSE(GetSafeBrowsingBlockingPage()); |
| 543 |
| 544 // We did not proceed, the pending entry should be gone. |
| 545 EXPECT_FALSE(controller().pending_entry()); |
| 546 |
| 547 // No report should have been sent. |
| 548 EXPECT_EQ(0u, service_->GetDetails()->size()); |
| 549 service_->GetDetails()->clear(); |
| 550 } |
OLD | NEW |