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