| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/strings/stringprintf.h" | |
| 6 #include "chrome/browser/chrome_notification_types.h" | |
| 7 #include "chrome/browser/download/download_prefs.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager
.h" | |
| 11 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chrome/test/base/ui_test_utils.h" | |
| 17 #include "components/prefs/pref_service.h" | |
| 18 #include "content/public/browser/browser_context.h" | |
| 19 #include "content/public/browser/download_item.h" | |
| 20 #include "content/public/browser/download_manager.h" | |
| 21 #include "content/public/browser/notification_service.h" | |
| 22 #include "content/public/test/browser_test_utils.h" | |
| 23 #include "content/public/test/test_utils.h" | |
| 24 #include "net/dns/mock_host_resolver.h" | |
| 25 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 26 #include "url/gurl.h" | |
| 27 #include "url/url_canon.h" | |
| 28 | |
| 29 namespace safe_browsing { | |
| 30 | |
| 31 const char kSingleFrameTestURL[] = | |
| 32 "/safe_browsing/download_protection/navigation_observer/" | |
| 33 "navigation_observer_tests.html"; | |
| 34 const char kMultiFrameTestURL[] = | |
| 35 "/safe_browsing/download_protection/navigation_observer/" | |
| 36 "navigation_observer_multi_frame_tests.html"; | |
| 37 const char kRedirectURL[] = | |
| 38 "/safe_browsing/download_protection/navigation_observer/redirect.html"; | |
| 39 const char kDownloadDataURL[] = | |
| 40 "data:application/octet-stream;base64,a2poYWxrc2hkbGtoYXNka2xoYXNsa2RoYWxra" | |
| 41 "GtoYWxza2hka2xzamFoZGxramhhc2xka2hhc2xrZGgKYXNrZGpoa2FzZGpoYWtzaGRrYXNoZGt" | |
| 42 "oYXNrZGhhc2tkaGthc2hka2Foc2RraGFrc2hka2FzaGRraGFzCmFza2pkaGFrc2hkbSxjbmtza" | |
| 43 "mFoZGtoYXNrZGhhc2tka2hrYXNkCjg3MzQ2ODEyNzQ2OGtqc2hka2FoZHNrZGhraApha3NqZGt" | |
| 44 "hc2Roa3NkaGthc2hka2FzaGtkaAohISomXkAqJl4qYWhpZGFzeWRpeWlhc1xcb1wKa2Fqc2Roa" | |
| 45 "2FzaGRrYXNoZGsKYWtzamRoc2tkaAplbmQK"; | |
| 46 const char kIframeDirectDownloadURL[] = | |
| 47 "/safe_browsing/download_protection/navigation_observer/iframe.html"; | |
| 48 const char kIframeRetargetingURL[] = | |
| 49 "/safe_browsing/download_protection/navigation_observer/" | |
| 50 "iframe_retargeting.html"; | |
| 51 const char kDownloadItemURL[] = "/safe_browsing/download_protection/signed.exe"; | |
| 52 | |
| 53 // Test class to help create SafeBrowsingNavigationObservers for each | |
| 54 // WebContents before they are actually installed through AttachTabHelper. | |
| 55 class TestNavigationObserverManager | |
| 56 : public SafeBrowsingNavigationObserverManager { | |
| 57 public: | |
| 58 TestNavigationObserverManager() : SafeBrowsingNavigationObserverManager() { | |
| 59 registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED, | |
| 60 content::NotificationService::AllSources()); | |
| 61 } | |
| 62 | |
| 63 void Observe(int type, | |
| 64 const content::NotificationSource& source, | |
| 65 const content::NotificationDetails& details) override { | |
| 66 if (type == chrome::NOTIFICATION_TAB_ADDED) { | |
| 67 content::WebContents* dest_content = | |
| 68 content::Details<content::WebContents>(details).ptr(); | |
| 69 DCHECK(dest_content); | |
| 70 observer_list_.push_back( | |
| 71 new SafeBrowsingNavigationObserver(dest_content, this)); | |
| 72 DCHECK(observer_list_.back()); | |
| 73 } else if (type == chrome::NOTIFICATION_RETARGETING) { | |
| 74 RecordRetargeting(details); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 protected: | |
| 79 ~TestNavigationObserverManager() override { observer_list_.clear(); } | |
| 80 | |
| 81 private: | |
| 82 std::vector<SafeBrowsingNavigationObserver*> observer_list_; | |
| 83 }; | |
| 84 | |
| 85 class SBNavigationObserverBrowserTest : public InProcessBrowserTest { | |
| 86 public: | |
| 87 SBNavigationObserverBrowserTest() {} | |
| 88 | |
| 89 void SetUpOnMainThread() override { | |
| 90 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 91 host_resolver()->AddRule("*", "127.0.0.1"); | |
| 92 // Navigate to test page. | |
| 93 ui_test_utils::NavigateToURL( | |
| 94 browser(), embedded_test_server()->GetURL(kSingleFrameTestURL)); | |
| 95 observer_manager_ = new TestNavigationObserverManager(); | |
| 96 observer_ = new SafeBrowsingNavigationObserver( | |
| 97 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 98 observer_manager_); | |
| 99 ASSERT_TRUE(observer_); | |
| 100 ASSERT_TRUE(InitialSetup()); | |
| 101 } | |
| 102 | |
| 103 bool InitialSetup() { | |
| 104 if (!browser()) | |
| 105 return false; | |
| 106 | |
| 107 if (!downloads_directory_.CreateUniqueTempDir()) | |
| 108 return false; | |
| 109 | |
| 110 // Set up default download path. | |
| 111 browser()->profile()->GetPrefs()->SetFilePath( | |
| 112 prefs::kDownloadDefaultDirectory, downloads_directory_.GetPath()); | |
| 113 browser()->profile()->GetPrefs()->SetFilePath( | |
| 114 prefs::kSaveFileDefaultDirectory, downloads_directory_.GetPath()); | |
| 115 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPromptForDownload, | |
| 116 false); | |
| 117 content::DownloadManager* manager = | |
| 118 content::BrowserContext::GetDownloadManager(browser()->profile()); | |
| 119 DownloadPrefs::FromDownloadManager(manager)->ResetAutoOpen(); | |
| 120 manager->RemoveAllDownloads(); | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 void TearDownOnMainThread() override { delete observer_; } | |
| 126 | |
| 127 // Most test cases will trigger downloads, though we don't really care if | |
| 128 // download completed or not. So we cancel downloads as soon as we record | |
| 129 // all the navigation events we need. | |
| 130 void CancelDownloads() { | |
| 131 std::vector<content::DownloadItem*> download_items; | |
| 132 content::DownloadManager* manager = | |
| 133 content::BrowserContext::GetDownloadManager(browser()->profile()); | |
| 134 manager->GetAllDownloads(&download_items); | |
| 135 for (auto item : download_items) | |
| 136 item->Cancel(true); | |
| 137 } | |
| 138 | |
| 139 // This function needs javascript support, only works on | |
| 140 // navigation_observer_tests.html and | |
| 141 // navigation_observer_multi_frame_tests.html. | |
| 142 void ClickTestLink(const char* test_name, int navigation_count) { | |
| 143 content::WebContents* main_web_contents = | |
| 144 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 145 std::string script = base::StringPrintf("clickLink('%s')", test_name); | |
| 146 EXPECT_TRUE(content::ExecuteScript(main_web_contents, script)); | |
| 147 // Wait until all the navigations complete. | |
| 148 content::WaitForLoadStopWithoutSuccessCheck(main_web_contents); | |
| 149 for (int i = 0; i < navigation_count-1; i++) { | |
| 150 content::WebContents* active_contents = | |
| 151 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 152 content::WaitForLoadStopWithoutSuccessCheck(active_contents); | |
| 153 } | |
| 154 CancelDownloads(); | |
| 155 } | |
| 156 | |
| 157 void VerifyNavigationEvent(const GURL& expected_source_url, | |
| 158 const GURL& expected_source_main_frame_url, | |
| 159 const GURL& expected_original_request_url, | |
| 160 const GURL& expected_destination_url, | |
| 161 bool expected_is_user_initiated, | |
| 162 bool expected_has_committed, | |
| 163 bool expected_has_server_redirect, | |
| 164 const NavigationEvent& actual_nav_event) { | |
| 165 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | |
| 166 EXPECT_EQ(expected_source_main_frame_url, | |
| 167 actual_nav_event.source_main_frame_url); | |
| 168 EXPECT_EQ(expected_original_request_url, | |
| 169 actual_nav_event.original_request_url); | |
| 170 EXPECT_EQ(expected_destination_url, actual_nav_event.destination_url); | |
| 171 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | |
| 172 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | |
| 173 EXPECT_EQ(expected_has_server_redirect, | |
| 174 actual_nav_event.has_server_redirect); | |
| 175 } | |
| 176 | |
| 177 void VerifyHostToIpMap() { | |
| 178 // Since all testing pages have the same host, there is only one entry in | |
| 179 // host_to_ip_map_. | |
| 180 SafeBrowsingNavigationObserverManager::HostToIpMap* actual_host_ip_map = | |
| 181 host_to_ip_map(); | |
| 182 ASSERT_EQ(std::size_t(1), actual_host_ip_map->size()); | |
| 183 auto ip_list = | |
| 184 actual_host_ip_map->at(embedded_test_server()->base_url().host()); | |
| 185 ASSERT_EQ(std::size_t(1), ip_list.size()); | |
| 186 EXPECT_EQ(embedded_test_server()->host_port_pair().host(), | |
| 187 ip_list.back().ip); | |
| 188 } | |
| 189 | |
| 190 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { | |
| 191 return observer_manager_->navigation_map(); | |
| 192 } | |
| 193 | |
| 194 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { | |
| 195 return observer_manager_->host_to_ip_map(); | |
| 196 } | |
| 197 | |
| 198 protected: | |
| 199 SafeBrowsingNavigationObserverManager* observer_manager_; | |
| 200 SafeBrowsingNavigationObserver* observer_; | |
| 201 | |
| 202 private: | |
| 203 base::ScopedTempDir downloads_directory_; | |
| 204 }; | |
| 205 | |
| 206 // Click on a link and start download on the same page. | |
| 207 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, DirectDownload) { | |
| 208 ClickTestLink("direct_download", 1); | |
| 209 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 210 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 211 auto nav_map = navigation_map(); | |
| 212 ASSERT_TRUE(nav_map); | |
| 213 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 214 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 215 // Since this test uses javascript to mimic clicking on a link (no actual user | |
| 216 // gesture), and DidGetUserInteraction() does not respond to ExecuteScript(), | |
| 217 // therefore is_user_initiated is false. | |
| 218 VerifyNavigationEvent(initial_url, // source_url | |
| 219 initial_url, // source_main_frame_url | |
| 220 download_url, // original_request_url | |
| 221 download_url, // destination_url | |
| 222 false, // is_user_initiated, | |
| 223 false, // has_committed | |
| 224 false, // has_server_redirect | |
| 225 nav_map->at(download_url).at(0)); | |
| 226 VerifyHostToIpMap(); | |
| 227 } | |
| 228 | |
| 229 // Click on a link with rel="noreferrer" attribute, and start download on the | |
| 230 // same page. | |
| 231 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 232 DirectDownloadNoReferrer) { | |
| 233 ClickTestLink("direct_download_noreferrer", 1); | |
| 234 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 235 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 236 auto nav_map = navigation_map(); | |
| 237 ASSERT_TRUE(nav_map); | |
| 238 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 239 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 240 VerifyNavigationEvent(initial_url, // source_url | |
| 241 initial_url, // source_main_frame_url | |
| 242 download_url, // original_request_url | |
| 243 download_url, // destination_url | |
| 244 false, // is_user_initiated, | |
| 245 false, // has_committed | |
| 246 false, // has_server_redirect | |
| 247 nav_map->at(download_url).at(0)); | |
| 248 VerifyHostToIpMap(); | |
| 249 } | |
| 250 | |
| 251 // Click on a link with rel="noreferrer" attribute, and start download in a | |
| 252 // new tab using target=_blank. | |
| 253 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 254 DirectDownloadNoReferrerTargetBlank) { | |
| 255 ClickTestLink("direct_download_noreferrer_target_blank", 2); | |
| 256 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 257 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 258 auto nav_map = navigation_map(); | |
| 259 ASSERT_TRUE(nav_map); | |
| 260 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 261 ASSERT_EQ(std::size_t(2), nav_map->at(download_url).size()); | |
| 262 // The first NavigationEvent was obtained from NOIFICATION_RETARGETING. | |
| 263 // TODO(jialiul): After https://crbug.com/651895 is fixed, we'll no longer | |
| 264 // listen to NOTIFICATION_RETARGETING, hence only one NavigationEvent will | |
| 265 // be observed with the true initator URL. This applies to other new tab | |
| 266 // download, and target blank download test cases too. | |
| 267 VerifyNavigationEvent(initial_url, // source_url | |
| 268 initial_url, // source_main_frame_url | |
| 269 download_url, // original_request_url | |
| 270 download_url, // destination_url | |
| 271 false, // is_user_initiated, | |
| 272 false, // has_committed | |
| 273 false, // has_server_redirect | |
| 274 nav_map->at(download_url).at(0)); | |
| 275 // The second one is the actual navigation which triggers download. | |
| 276 VerifyNavigationEvent(GURL(), // source_url | |
| 277 GURL(), // source_main_frame_url | |
| 278 download_url, // original_request_url | |
| 279 download_url, // destination_url | |
| 280 false, // is_user_initiated, | |
| 281 false, // has_committed | |
| 282 false, // has_server_redirect | |
| 283 nav_map->at(download_url).at(1)); | |
| 284 VerifyHostToIpMap(); | |
| 285 } | |
| 286 | |
| 287 // Click on a link which navigates to a page then redirects to a download using | |
| 288 // META HTTP-EQUIV="refresh". All transitions happen in the same tab. | |
| 289 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 290 SingleMetaRefreshRedirect) { | |
| 291 ClickTestLink("single_meta_refresh_redirect", 2); | |
| 292 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 293 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 294 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 295 auto nav_map = navigation_map(); | |
| 296 ASSERT_TRUE(nav_map); | |
| 297 // Since unlike server redirects client redirects commit and then generate a | |
| 298 // second navigation, our observer records two NavigationEvents for this test. | |
| 299 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 300 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size()); | |
| 301 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 302 VerifyNavigationEvent(initial_url, // source_url | |
| 303 initial_url, // source_main_frame_url | |
| 304 redirect_url, // original_request_url | |
| 305 redirect_url, // destination_url | |
| 306 false, // is_user_initiated, | |
| 307 true, // has_committed | |
| 308 false, // has_server_redirect | |
| 309 nav_map->at(redirect_url).at(0)); | |
| 310 VerifyNavigationEvent(redirect_url, // source_url | |
| 311 redirect_url, // source_main_frame_url | |
| 312 download_url, // original_request_url | |
| 313 download_url, // destination_url | |
| 314 false, // is_user_initiated, | |
| 315 false, // has_committed | |
| 316 false, // has_server_redirect | |
| 317 nav_map->at(download_url).at(0)); | |
| 318 VerifyHostToIpMap(); | |
| 319 } | |
| 320 | |
| 321 // Click on a link which navigates to a page then redirects to a download using | |
| 322 // META HTTP-EQUIV="refresh". First navigation happens in target blank. | |
| 323 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 324 SingleMetaRefreshRedirectTargetBlank) { | |
| 325 ClickTestLink("single_meta_refresh_redirect_target_blank", 3); | |
| 326 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 327 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 328 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 329 auto nav_map = navigation_map(); | |
| 330 ASSERT_TRUE(nav_map); | |
| 331 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 332 ASSERT_EQ(std::size_t(2), nav_map->at(redirect_url).size()); | |
| 333 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 334 // TODO(jialiul): After https://crbug.com/651895 is fixed, we'll no longer | |
| 335 // listen to NOTIFICATION_RETARGETING, hence only two NavigationEvents will | |
| 336 // be observed with the true initator URL. | |
| 337 VerifyNavigationEvent(initial_url, // source_url | |
| 338 initial_url, // source_main_frame_url | |
| 339 redirect_url, // original_request_url | |
| 340 redirect_url, // destination_url | |
| 341 false, // is_user_initiated, | |
| 342 false, // has_committed | |
| 343 false, // has_server_redirect | |
| 344 nav_map->at(redirect_url).at(0)); | |
| 345 VerifyNavigationEvent(GURL(), // source_url | |
| 346 GURL(), // source_main_frame_url | |
| 347 redirect_url, // original_request_url | |
| 348 redirect_url, // destination_url | |
| 349 false, // is_user_initiated, | |
| 350 true, // has_committed | |
| 351 false, // has_server_redirect | |
| 352 nav_map->at(redirect_url).at(1)); | |
| 353 VerifyNavigationEvent(redirect_url, // source_url | |
| 354 redirect_url, // source_main_frame_url | |
| 355 download_url, // original_request_url | |
| 356 download_url, // destination_url | |
| 357 false, // is_user_initiated, | |
| 358 false, // has_committed | |
| 359 false, // has_server_redirect | |
| 360 nav_map->at(download_url).at(0)); | |
| 361 VerifyHostToIpMap(); | |
| 362 } | |
| 363 | |
| 364 // Click on a link which redirects twice before reaching download using | |
| 365 // META HTTP-EQUIV="refresh". All transitions happen in the same tab. | |
| 366 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 367 MultiMetaRefreshRedirects) { | |
| 368 ClickTestLink("multiple_meta_refresh_redirects", 3); | |
| 369 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 370 GURL first_redirect_url = embedded_test_server()->GetURL( | |
| 371 "/safe_browsing/download_protection/navigation_observer/" | |
| 372 "double_redirect.html"); | |
| 373 GURL second_redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 374 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 375 auto nav_map = navigation_map(); | |
| 376 ASSERT_TRUE(nav_map); | |
| 377 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 378 ASSERT_EQ(std::size_t(1), nav_map->at(first_redirect_url).size()); | |
| 379 ASSERT_EQ(std::size_t(1), nav_map->at(second_redirect_url).size()); | |
| 380 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 381 VerifyNavigationEvent(initial_url, // source_url | |
| 382 initial_url, // source_main_frame_url | |
| 383 first_redirect_url, // original_request_url | |
| 384 first_redirect_url, // destination_url | |
| 385 false, // is_user_initiated, | |
| 386 true, // has_committed | |
| 387 false, // has_server_redirect | |
| 388 nav_map->at(first_redirect_url).at(0)); | |
| 389 VerifyNavigationEvent(first_redirect_url, // source_url | |
| 390 first_redirect_url, // source_main_frame_url | |
| 391 second_redirect_url, // original_request_url | |
| 392 second_redirect_url, // destination_url | |
| 393 false, // is_user_initiated, | |
| 394 true, // has_committed | |
| 395 false, // has_server_redirect | |
| 396 nav_map->at(second_redirect_url).at(0)); | |
| 397 VerifyNavigationEvent(second_redirect_url, // source_url | |
| 398 second_redirect_url, // source_main_frame_url | |
| 399 download_url, // original_request_url | |
| 400 download_url, // destination_url | |
| 401 false, // is_user_initiated, | |
| 402 false, // has_committed | |
| 403 false, // has_server_redirect | |
| 404 nav_map->at(download_url).at(0)); | |
| 405 VerifyHostToIpMap(); | |
| 406 } | |
| 407 | |
| 408 // Click on a link which redirects to download using window.location.href. | |
| 409 // All transitions happen in the same tab. | |
| 410 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 411 WindowLocationHrefRedirect) { | |
| 412 ClickTestLink("window_location_href_redirect", 2); | |
| 413 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 414 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 415 auto nav_map = navigation_map(); | |
| 416 ASSERT_TRUE(nav_map); | |
| 417 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 418 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 419 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 420 VerifyNavigationEvent(initial_url, // source_url | |
| 421 initial_url, // source_main_frame_url | |
| 422 initial_url, // original_request_url | |
| 423 initial_url, // destination_url | |
| 424 false, // is_user_initiated, | |
| 425 true, // has_committed | |
| 426 false, // has_server_redirect | |
| 427 nav_map->at(initial_url).at(0)); | |
| 428 VerifyNavigationEvent(initial_url, // source_url | |
| 429 initial_url, // source_main_frame_url | |
| 430 download_url, // original_request_url | |
| 431 download_url, // destination_url | |
| 432 false, // is_user_initiated, | |
| 433 false, // has_committed | |
| 434 false, // has_server_redirect | |
| 435 nav_map->at(download_url).at(0)); | |
| 436 VerifyHostToIpMap(); | |
| 437 } | |
| 438 | |
| 439 // Click on a link which redirects twice until it reaches download using a | |
| 440 // mixture of meta refresh and window.location.href. All transitions happen in | |
| 441 // the same tab. | |
| 442 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, MixRedirects) { | |
| 443 ClickTestLink("mix_redirects", 3); | |
| 444 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 445 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 446 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 447 auto nav_map = navigation_map(); | |
| 448 ASSERT_TRUE(nav_map); | |
| 449 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 450 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 451 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size()); | |
| 452 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 453 VerifyNavigationEvent(initial_url, // source_url | |
| 454 initial_url, // source_main_frame_url | |
| 455 initial_url, // original_request_url | |
| 456 initial_url, // destination_url | |
| 457 false, // is_user_initiated, | |
| 458 true, // has_committed | |
| 459 false, // has_server_redirect | |
| 460 nav_map->at(initial_url).at(0)); | |
| 461 VerifyNavigationEvent(initial_url, // source_url | |
| 462 initial_url, // source_main_frame_url | |
| 463 redirect_url, // original_request_url | |
| 464 redirect_url, // destination_url | |
| 465 false, // is_user_initiated, | |
| 466 true, // has_committed | |
| 467 false, // has_server_redirect | |
| 468 nav_map->at(redirect_url).at(0)); | |
| 469 VerifyNavigationEvent(redirect_url, // source_url | |
| 470 redirect_url, // source_main_frame_url | |
| 471 download_url, // original_request_url | |
| 472 download_url, // destination_url | |
| 473 false, // is_user_initiated, | |
| 474 false, // has_committed | |
| 475 false, // has_server_redirect | |
| 476 nav_map->at(download_url).at(0)); | |
| 477 VerifyHostToIpMap(); | |
| 478 } | |
| 479 | |
| 480 // Use javascript to open download in a new tab. | |
| 481 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, NewTabDownload) { | |
| 482 ClickTestLink("new_tab_download", 4); | |
| 483 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 484 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 485 GURL blank_url = GURL(url::kAboutBlankURL); | |
| 486 auto nav_map = navigation_map(); | |
| 487 ASSERT_TRUE(nav_map); | |
| 488 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 489 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 490 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size()); | |
| 491 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 492 VerifyNavigationEvent(initial_url, // source_url | |
| 493 initial_url, // source_main_frame_url | |
| 494 initial_url, // original_request_url | |
| 495 initial_url, // destination_url | |
| 496 false, // is_user_initiated, | |
| 497 true, // has_committed | |
| 498 false, // has_server_redirect | |
| 499 nav_map->at(initial_url).at(0)); | |
| 500 VerifyNavigationEvent(initial_url, // source_url | |
| 501 initial_url, // source_main_frame_url | |
| 502 blank_url, // original_request_url | |
| 503 blank_url, // destination_url | |
| 504 false, // is_user_initiated, | |
| 505 false, // has_committed | |
| 506 false, // has_server_redirect | |
| 507 nav_map->at(blank_url).at(0)); | |
| 508 // Source and target are at different tabs. | |
| 509 EXPECT_NE(nav_map->at(blank_url).at(0).source_tab_id, | |
| 510 nav_map->at(blank_url).at(0).target_tab_id); | |
| 511 VerifyNavigationEvent(GURL(), // source_url | |
| 512 GURL(), // source_main_frame_url | |
| 513 blank_url, // original_request_url | |
| 514 blank_url, // destination_url | |
| 515 false, // is_user_initiated, | |
| 516 false, // has_committed | |
| 517 false, // has_server_redirect | |
| 518 nav_map->at(blank_url).at(1)); | |
| 519 EXPECT_EQ(nav_map->at(blank_url).at(1).source_tab_id, | |
| 520 nav_map->at(blank_url).at(1).target_tab_id); | |
| 521 VerifyNavigationEvent(blank_url, // source_url | |
| 522 blank_url, // source_main_frame_url | |
| 523 download_url, // original_request_url | |
| 524 download_url, // destination_url | |
| 525 false, // is_user_initiated, | |
| 526 false, // has_committed | |
| 527 false, // has_server_redirect | |
| 528 nav_map->at(download_url).at(0)); | |
| 529 EXPECT_EQ(nav_map->at(download_url).at(0).source_tab_id, | |
| 530 nav_map->at(download_url).at(0).target_tab_id); | |
| 531 VerifyHostToIpMap(); | |
| 532 } | |
| 533 | |
| 534 // Use javascript to open download in a new tab and download has a data url. | |
| 535 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 536 NewTabDownloadWithDataURL) { | |
| 537 ClickTestLink("new_tab_download_with_data_url", 4); | |
| 538 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 539 GURL download_url = GURL(kDownloadDataURL); | |
| 540 GURL blank_url = GURL("about:blank"); | |
| 541 auto nav_map = navigation_map(); | |
| 542 ASSERT_TRUE(nav_map); | |
| 543 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 544 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 545 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size()); | |
| 546 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 547 VerifyNavigationEvent(initial_url, // source_url | |
| 548 initial_url, // source_main_frame_url | |
| 549 initial_url, // original_request_url | |
| 550 initial_url, // destination_url | |
| 551 false, // is_user_initiated, | |
| 552 true, // has_committed | |
| 553 false, // has_server_redirect | |
| 554 nav_map->at(initial_url).at(0)); | |
| 555 VerifyNavigationEvent(initial_url, // source_url | |
| 556 initial_url, // source_main_frame_url | |
| 557 blank_url, // original_request_url | |
| 558 blank_url, // destination_url | |
| 559 false, // is_user_initiated, | |
| 560 false, // has_committed | |
| 561 false, // has_server_redirect | |
| 562 nav_map->at(blank_url).at(0)); | |
| 563 // Source and target are at different tabs. | |
| 564 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id == | |
| 565 nav_map->at(blank_url).at(0).target_tab_id); | |
| 566 VerifyNavigationEvent(GURL(), // source_url | |
| 567 GURL(), // source_main_frame_url | |
| 568 blank_url, // original_request_url | |
| 569 blank_url, // destination_url | |
| 570 false, // is_user_initiated, | |
| 571 false, // has_committed | |
| 572 false, // has_server_redirect | |
| 573 nav_map->at(blank_url).at(1)); | |
| 574 EXPECT_EQ(nav_map->at(blank_url).at(1).source_tab_id, | |
| 575 nav_map->at(blank_url).at(1).target_tab_id); | |
| 576 VerifyNavigationEvent(blank_url, // source_url | |
| 577 blank_url, // source_main_frame_url | |
| 578 download_url, // original_request_url | |
| 579 download_url, // destination_url | |
| 580 false, // is_user_initiated, | |
| 581 false, // has_committed | |
| 582 false, // has_server_redirect | |
| 583 nav_map->at(download_url).at(0)); | |
| 584 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id == | |
| 585 nav_map->at(download_url).at(0).target_tab_id); | |
| 586 // Since data url does does not have IP, host_to_ip_map_ should be empty. | |
| 587 EXPECT_EQ(std::size_t(0), host_to_ip_map()->size()); | |
| 588 } | |
| 589 | |
| 590 // TODO(jialiul): Need to figure out why this test is failing on Windows and | |
| 591 // flaky on other platforms. | |
| 592 #define MAYBE_DownloadViaHTML5FileApi DISABLED_DownloadViaHTML5FileApi | |
| 593 // Download via html5 file API. | |
| 594 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 595 MAYBE_DownloadViaHTML5FileApi) { | |
| 596 ClickTestLink("html5_file_api", 2); | |
| 597 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 598 std::string download_url_str = | |
| 599 base::StringPrintf("filesystem:%stemporary/test.exe", | |
| 600 embedded_test_server()->base_url().spec().c_str()); | |
| 601 GURL download_url = GURL(download_url_str); | |
| 602 auto nav_map = navigation_map(); | |
| 603 ASSERT_TRUE(nav_map); | |
| 604 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 605 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 606 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 607 VerifyNavigationEvent(initial_url, // source_url | |
| 608 initial_url, // source_main_frame_url | |
| 609 initial_url, // original_request_url | |
| 610 initial_url, // destination_url | |
| 611 false, // is_user_initiated, | |
| 612 true, // has_committed | |
| 613 false, // has_server_redirect | |
| 614 nav_map->at(initial_url).at(0)); | |
| 615 VerifyNavigationEvent(initial_url, // source_url | |
| 616 initial_url, // source_main_frame_url | |
| 617 download_url, // original_request_url | |
| 618 download_url, // destination_url | |
| 619 false, // is_user_initiated, | |
| 620 false, // has_committed | |
| 621 false, // has_server_redirect | |
| 622 nav_map->at(download_url).at(0)); | |
| 623 VerifyHostToIpMap(); | |
| 624 } | |
| 625 | |
| 626 // Click a link in a subframe and start download. | |
| 627 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 628 SubFrameDirectDownload) { | |
| 629 ui_test_utils::NavigateToURL( | |
| 630 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL)); | |
| 631 EXPECT_TRUE(content::WaitForLoadStop( | |
| 632 browser()->tab_strip_model()->GetActiveWebContents())); | |
| 633 std::string test_name = | |
| 634 base::StringPrintf("%s', '%s", "iframe1", "iframe_direct_download"); | |
| 635 ClickTestLink(test_name.c_str(), 1); | |
| 636 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 637 GURL multi_frame_test_url = | |
| 638 embedded_test_server()->GetURL(kMultiFrameTestURL); | |
| 639 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL); | |
| 640 GURL iframe_retargeting_url = | |
| 641 embedded_test_server()->GetURL(kIframeRetargetingURL); | |
| 642 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 643 auto nav_map = navigation_map(); | |
| 644 ASSERT_TRUE(nav_map); | |
| 645 ASSERT_EQ(std::size_t(4), nav_map->size()); | |
| 646 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size()); | |
| 647 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size()); | |
| 648 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_retargeting_url).size()); | |
| 649 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 650 VerifyNavigationEvent(initial_url, // source_url | |
| 651 initial_url, // source_main_frame_url | |
| 652 multi_frame_test_url, // original_request_url | |
| 653 multi_frame_test_url, // destination_url | |
| 654 true, // is_user_initiated, | |
| 655 true, // has_committed | |
| 656 false, // has_server_redirect | |
| 657 nav_map->at(multi_frame_test_url).at(0)); | |
| 658 VerifyNavigationEvent(GURL(), // source_url | |
| 659 multi_frame_test_url, // source_main_frame_url | |
| 660 iframe_url, // original_request_url | |
| 661 iframe_url, // destination_url | |
| 662 false, // is_user_initiated, | |
| 663 true, // has_committed | |
| 664 false, // has_server_redirect | |
| 665 nav_map->at(iframe_url).at(0)); | |
| 666 VerifyNavigationEvent(GURL(), // source_url | |
| 667 multi_frame_test_url, // source_main_frame_url | |
| 668 iframe_retargeting_url, // original_request_url | |
| 669 iframe_retargeting_url, // destination_url | |
| 670 false, // is_user_initiated, | |
| 671 true, // has_committed | |
| 672 false, // has_server_redirect | |
| 673 nav_map->at(iframe_retargeting_url).at(0)); | |
| 674 VerifyNavigationEvent(iframe_url, // source_url | |
| 675 multi_frame_test_url, // source_main_frame_url | |
| 676 download_url, // original_request_url | |
| 677 download_url, // destination_url | |
| 678 false, // is_user_initiated, | |
| 679 false, // has_committed | |
| 680 false, // has_server_redirect | |
| 681 nav_map->at(download_url).at(0)); | |
| 682 VerifyHostToIpMap(); | |
| 683 } | |
| 684 | |
| 685 // Click a link in a subframe and open download in a new tab. | |
| 686 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 687 SubFrameNewTabDownload) { | |
| 688 ui_test_utils::NavigateToURL( | |
| 689 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL)); | |
| 690 EXPECT_TRUE(content::WaitForLoadStop( | |
| 691 browser()->tab_strip_model()->GetActiveWebContents())); | |
| 692 std::string test_name = | |
| 693 base::StringPrintf("%s', '%s", "iframe2", "iframe_new_tab_download"); | |
| 694 ClickTestLink(test_name.c_str(), 4); | |
| 695 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 696 GURL multi_frame_test_url = | |
| 697 embedded_test_server()->GetURL(kMultiFrameTestURL); | |
| 698 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL); | |
| 699 GURL iframe_retargeting_url = | |
| 700 embedded_test_server()->GetURL(kIframeRetargetingURL); | |
| 701 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 702 GURL blank_url = GURL("about:blank"); | |
| 703 auto nav_map = navigation_map(); | |
| 704 ASSERT_TRUE(nav_map); | |
| 705 ASSERT_EQ(std::size_t(5), nav_map->size()); | |
| 706 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size()); | |
| 707 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size()); | |
| 708 ASSERT_EQ(std::size_t(2), nav_map->at(iframe_retargeting_url).size()); | |
| 709 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size()); | |
| 710 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 711 VerifyNavigationEvent(initial_url, // source_url | |
| 712 initial_url, // source_main_frame_url | |
| 713 multi_frame_test_url, // original_request_url | |
| 714 multi_frame_test_url, // destination_url | |
| 715 true, // is_user_initiated, | |
| 716 true, // has_committed | |
| 717 false, // has_server_redirect | |
| 718 nav_map->at(multi_frame_test_url).at(0)); | |
| 719 VerifyNavigationEvent(GURL(), // source_url | |
| 720 multi_frame_test_url, // source_main_frame_url | |
| 721 iframe_url, // original_request_url | |
| 722 iframe_url, // destination_url | |
| 723 false, // is_user_initiated, | |
| 724 true, // has_committed | |
| 725 false, // has_server_redirect | |
| 726 nav_map->at(iframe_url).at(0)); | |
| 727 VerifyNavigationEvent(GURL(), // source_url | |
| 728 multi_frame_test_url, // source_main_frame_url | |
| 729 iframe_retargeting_url, // original_request_url | |
| 730 iframe_retargeting_url, // destination_url | |
| 731 false, // is_user_initiated, | |
| 732 true, // has_committed | |
| 733 false, // has_server_redirect | |
| 734 nav_map->at(iframe_retargeting_url).at(0)); | |
| 735 VerifyNavigationEvent(iframe_retargeting_url, // source_url | |
| 736 multi_frame_test_url, // source_main_frame_url | |
| 737 iframe_retargeting_url, // original_request_url | |
| 738 iframe_retargeting_url, // destination_url | |
| 739 false, // is_user_initiated, | |
| 740 true, // has_committed | |
| 741 false, // has_server_redirect | |
| 742 nav_map->at(iframe_retargeting_url).at(1)); | |
| 743 VerifyNavigationEvent(iframe_retargeting_url, // source_url | |
| 744 multi_frame_test_url, // source_main_frame_url | |
| 745 blank_url, // original_request_url | |
| 746 blank_url, // destination_url | |
| 747 false, // is_user_initiated, | |
| 748 false, // has_committed | |
| 749 false, // has_server_redirect | |
| 750 nav_map->at(blank_url).at(0)); | |
| 751 VerifyNavigationEvent(GURL(), // source_url | |
| 752 GURL(), // source_main_frame_url | |
| 753 blank_url, // original_request_url | |
| 754 blank_url, // destination_url | |
| 755 false, // is_user_initiated, | |
| 756 false, // has_committed | |
| 757 false, // has_server_redirect | |
| 758 nav_map->at(blank_url).at(1)); | |
| 759 VerifyNavigationEvent(blank_url, // source_url | |
| 760 blank_url, // source_main_frame_url | |
| 761 download_url, // original_request_url | |
| 762 download_url, // destination_url | |
| 763 false, // is_user_initiated, | |
| 764 false, // has_committed | |
| 765 false, // has_server_redirect | |
| 766 nav_map->at(download_url).at(0)); | |
| 767 VerifyHostToIpMap(); | |
| 768 } | |
| 769 | |
| 770 // Server-side redirect. | |
| 771 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, ServerRedirect) { | |
| 772 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 773 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 774 GURL request_url = | |
| 775 embedded_test_server()->GetURL("/server-redirect?" + download_url.spec()); | |
| 776 ui_test_utils::NavigateToURL(browser(), request_url); | |
| 777 CancelDownloads(); | |
| 778 auto nav_map = navigation_map(); | |
| 779 ASSERT_TRUE(nav_map); | |
| 780 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 781 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 782 VerifyNavigationEvent(initial_url, // source_url | |
| 783 initial_url, // source_main_frame_url | |
| 784 request_url, // original_request_url | |
| 785 download_url, // destination_url | |
| 786 true, // is_user_initiated, | |
| 787 false, // has_committed | |
| 788 true, // has_server_redirect | |
| 789 nav_map->at(download_url).at(0)); | |
| 790 } | |
| 791 | |
| 792 // host_to_ip_map_ size should increase by one after a new navigation. | |
| 793 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, AddIPMapping) { | |
| 794 auto ip_map = host_to_ip_map(); | |
| 795 std::string test_server_host(embedded_test_server()->base_url().host()); | |
| 796 ip_map->insert( | |
| 797 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>())); | |
| 798 ASSERT_EQ(std::size_t(0), ip_map->at(test_server_host).size()); | |
| 799 ClickTestLink("direct_download", 1); | |
| 800 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size()); | |
| 801 EXPECT_EQ(embedded_test_server()->host_port_pair().host(), | |
| 802 ip_map->at(test_server_host).back().ip); | |
| 803 } | |
| 804 | |
| 805 // If we have already seen an IP associated with a host, update its timestamp. | |
| 806 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, IPListDedup) { | |
| 807 auto ip_map = host_to_ip_map(); | |
| 808 std::string test_server_host(embedded_test_server()->base_url().host()); | |
| 809 ip_map->insert( | |
| 810 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>())); | |
| 811 base::Time yesterday(base::Time::Now() - base::TimeDelta::FromDays(1)); | |
| 812 ip_map->at(test_server_host) | |
| 813 .push_back(ResolvedIPAddress( | |
| 814 yesterday, embedded_test_server()->host_port_pair().host())); | |
| 815 ASSERT_EQ(std::size_t(1), ip_map->at(test_server_host).size()); | |
| 816 ClickTestLink("direct_download", 1); | |
| 817 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size()); | |
| 818 EXPECT_EQ(embedded_test_server()->host_port_pair().host(), | |
| 819 ip_map->at(test_server_host).back().ip); | |
| 820 EXPECT_NE(yesterday, ip_map->at(test_server_host).front().timestamp); | |
| 821 } | |
| 822 | |
| 823 } // namespace safe_browsing | |
| OLD | NEW |