| 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 for (int i = 0; i < navigation_count; i++) { | |
| 149 content::WebContents* active_contents = | |
| 150 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 151 content::WaitForLoadStopWithoutSuccessCheck(active_contents); | |
| 152 } | |
| 153 CancelDownloads(); | |
| 154 } | |
| 155 | |
| 156 void VerifyNavigationEvent(const GURL& expected_source_url, | |
| 157 const GURL& expected_source_main_frame_url, | |
| 158 const GURL& expected_original_request_url, | |
| 159 const GURL& expected_destination_url, | |
| 160 bool expected_is_user_initiated, | |
| 161 bool expected_has_committed, | |
| 162 bool expected_has_server_redirect, | |
| 163 const NavigationEvent& actual_nav_event) { | |
| 164 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | |
| 165 EXPECT_EQ(expected_source_main_frame_url, | |
| 166 actual_nav_event.source_main_frame_url); | |
| 167 EXPECT_EQ(expected_original_request_url, | |
| 168 actual_nav_event.original_request_url); | |
| 169 EXPECT_EQ(expected_destination_url, actual_nav_event.destination_url); | |
| 170 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | |
| 171 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | |
| 172 EXPECT_EQ(expected_has_server_redirect, | |
| 173 actual_nav_event.has_server_redirect); | |
| 174 } | |
| 175 | |
| 176 void VerifyHostToIpMap() { | |
| 177 // Since all testing pages have the same host, there is only one entry in | |
| 178 // host_to_ip_map_. | |
| 179 SafeBrowsingNavigationObserverManager::HostToIpMap* actual_host_ip_map = | |
| 180 host_to_ip_map(); | |
| 181 ASSERT_EQ(std::size_t(1), actual_host_ip_map->size()); | |
| 182 auto ip_list = | |
| 183 actual_host_ip_map->at(embedded_test_server()->base_url().host()); | |
| 184 ASSERT_EQ(std::size_t(1), ip_list.size()); | |
| 185 EXPECT_EQ(embedded_test_server()->host_port_pair().host(), | |
| 186 ip_list.back().ip); | |
| 187 } | |
| 188 | |
| 189 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { | |
| 190 return observer_manager_->navigation_map(); | |
| 191 } | |
| 192 | |
| 193 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { | |
| 194 return observer_manager_->host_to_ip_map(); | |
| 195 } | |
| 196 | |
| 197 protected: | |
| 198 SafeBrowsingNavigationObserverManager* observer_manager_; | |
| 199 SafeBrowsingNavigationObserver* observer_; | |
| 200 | |
| 201 private: | |
| 202 base::ScopedTempDir downloads_directory_; | |
| 203 }; | |
| 204 | |
| 205 // Click on a link and start download on the same page. | |
| 206 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, DirectDownload) { | |
| 207 ClickTestLink("direct_download", 1); | |
| 208 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 209 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 210 auto nav_map = navigation_map(); | |
| 211 ASSERT_TRUE(nav_map); | |
| 212 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 213 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 214 // Since this test uses javascript to mimic clicking on a link (no actual user | |
| 215 // gesture), and DidGetUserInteraction() does not respond to ExecuteScript(), | |
| 216 // therefore is_user_initiated is false. | |
| 217 VerifyNavigationEvent(initial_url, // source_url | |
| 218 initial_url, // source_main_frame_url | |
| 219 download_url, // original_request_url | |
| 220 download_url, // destination_url | |
| 221 false, // is_user_initiated, | |
| 222 false, // has_committed | |
| 223 false, // has_server_redirect | |
| 224 nav_map->at(download_url).at(0)); | |
| 225 VerifyHostToIpMap(); | |
| 226 } | |
| 227 | |
| 228 // Click on a link with rel="noreferrer" attribute, and start download on the | |
| 229 // same page. | |
| 230 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 231 DirectDownloadNoReferrer) { | |
| 232 ClickTestLink("direct_download_noreferrer", 1); | |
| 233 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 234 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 235 auto nav_map = navigation_map(); | |
| 236 ASSERT_TRUE(nav_map); | |
| 237 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 238 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 239 VerifyNavigationEvent(initial_url, // source_url | |
| 240 initial_url, // source_main_frame_url | |
| 241 download_url, // original_request_url | |
| 242 download_url, // destination_url | |
| 243 false, // is_user_initiated, | |
| 244 false, // has_committed | |
| 245 false, // has_server_redirect | |
| 246 nav_map->at(download_url).at(0)); | |
| 247 VerifyHostToIpMap(); | |
| 248 } | |
| 249 | |
| 250 // Click on a link with rel="noreferrer" attribute, and start download in a | |
| 251 // new tab using target=_blank. | |
| 252 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 253 DirectDownloadNoReferrerTargetBlank) { | |
| 254 ClickTestLink("direct_download_noreferrer_target_blank", 1); | |
| 255 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 256 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 257 auto nav_map = navigation_map(); | |
| 258 ASSERT_TRUE(nav_map); | |
| 259 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 260 ASSERT_EQ(std::size_t(2), nav_map->at(download_url).size()); | |
| 261 // The first NavigationEvent was obtained from NOIFICATION_RETARGETING. | |
| 262 // TODO(jialiul): After https://crbug.com/651895 is fixed, we'll no longer | |
| 263 // listen to NOTIFICATION_RETARGETING, hence only one NavigationEvent will | |
| 264 // be observed with the true initator URL. This applies to other new tab | |
| 265 // download, and target blank download test cases too. | |
| 266 VerifyNavigationEvent(initial_url, // source_url | |
| 267 initial_url, // source_main_frame_url | |
| 268 download_url, // original_request_url | |
| 269 download_url, // destination_url | |
| 270 false, // is_user_initiated, | |
| 271 false, // has_committed | |
| 272 false, // has_server_redirect | |
| 273 nav_map->at(download_url).at(0)); | |
| 274 // The second one is the actual navigation which triggers download. | |
| 275 VerifyNavigationEvent(GURL(), // source_url | |
| 276 GURL(), // source_main_frame_url | |
| 277 download_url, // original_request_url | |
| 278 download_url, // destination_url | |
| 279 false, // is_user_initiated, | |
| 280 false, // has_committed | |
| 281 false, // has_server_redirect | |
| 282 nav_map->at(download_url).at(1)); | |
| 283 VerifyHostToIpMap(); | |
| 284 } | |
| 285 | |
| 286 // Click on a link which navigates to a page then redirects to a download using | |
| 287 // META HTTP-EQUIV="refresh". All transitions happen in the same tab. | |
| 288 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 289 SingleMetaRefreshRedirect) { | |
| 290 ClickTestLink("single_meta_refresh_redirect", 2); | |
| 291 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 292 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 293 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 294 auto nav_map = navigation_map(); | |
| 295 ASSERT_TRUE(nav_map); | |
| 296 // Since unlike server redirects client redirects commit and then generate a | |
| 297 // second navigation, our observer records two NavigationEvents for this test. | |
| 298 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 299 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size()); | |
| 300 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 301 VerifyNavigationEvent(initial_url, // source_url | |
| 302 initial_url, // source_main_frame_url | |
| 303 redirect_url, // original_request_url | |
| 304 redirect_url, // destination_url | |
| 305 false, // is_user_initiated, | |
| 306 true, // has_committed | |
| 307 false, // has_server_redirect | |
| 308 nav_map->at(redirect_url).at(0)); | |
| 309 VerifyNavigationEvent(redirect_url, // source_url | |
| 310 redirect_url, // source_main_frame_url | |
| 311 download_url, // original_request_url | |
| 312 download_url, // destination_url | |
| 313 false, // is_user_initiated, | |
| 314 false, // has_committed | |
| 315 false, // has_server_redirect | |
| 316 nav_map->at(download_url).at(0)); | |
| 317 VerifyHostToIpMap(); | |
| 318 } | |
| 319 | |
| 320 // Click on a link which navigates to a page then redirects to a download using | |
| 321 // META HTTP-EQUIV="refresh". First navigation happens in target blank. | |
| 322 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 323 SingleMetaRefreshRedirectTargetBlank) { | |
| 324 ClickTestLink("single_meta_refresh_redirect_target_blank", 3); | |
| 325 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 326 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 327 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 328 auto nav_map = navigation_map(); | |
| 329 ASSERT_TRUE(nav_map); | |
| 330 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 331 ASSERT_EQ(std::size_t(2), nav_map->at(redirect_url).size()); | |
| 332 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 333 // TODO(jialiul): After https://crbug.com/651895 is fixed, we'll no longer | |
| 334 // listen to NOTIFICATION_RETARGETING, hence only two NavigationEvents will | |
| 335 // be observed with the true initator URL. | |
| 336 VerifyNavigationEvent(initial_url, // source_url | |
| 337 initial_url, // source_main_frame_url | |
| 338 redirect_url, // original_request_url | |
| 339 redirect_url, // destination_url | |
| 340 false, // is_user_initiated, | |
| 341 false, // has_committed | |
| 342 false, // has_server_redirect | |
| 343 nav_map->at(redirect_url).at(0)); | |
| 344 VerifyNavigationEvent(GURL(), // source_url | |
| 345 GURL(), // source_main_frame_url | |
| 346 redirect_url, // original_request_url | |
| 347 redirect_url, // destination_url | |
| 348 false, // is_user_initiated, | |
| 349 true, // has_committed | |
| 350 false, // has_server_redirect | |
| 351 nav_map->at(redirect_url).at(1)); | |
| 352 VerifyNavigationEvent(redirect_url, // source_url | |
| 353 redirect_url, // source_main_frame_url | |
| 354 download_url, // original_request_url | |
| 355 download_url, // destination_url | |
| 356 false, // is_user_initiated, | |
| 357 false, // has_committed | |
| 358 false, // has_server_redirect | |
| 359 nav_map->at(download_url).at(0)); | |
| 360 VerifyHostToIpMap(); | |
| 361 } | |
| 362 | |
| 363 // Click on a link which redirects twice before reaching download using | |
| 364 // META HTTP-EQUIV="refresh". All transitions happen in the same tab. | |
| 365 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 366 MultiMetaRefreshRedirects) { | |
| 367 ClickTestLink("multiple_meta_refresh_redirects", 3); | |
| 368 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 369 GURL first_redirect_url = embedded_test_server()->GetURL( | |
| 370 "/safe_browsing/download_protection/navigation_observer/" | |
| 371 "double_redirect.html"); | |
| 372 GURL second_redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 373 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 374 auto nav_map = navigation_map(); | |
| 375 ASSERT_TRUE(nav_map); | |
| 376 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 377 ASSERT_EQ(std::size_t(1), nav_map->at(first_redirect_url).size()); | |
| 378 ASSERT_EQ(std::size_t(1), nav_map->at(second_redirect_url).size()); | |
| 379 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 380 VerifyNavigationEvent(initial_url, // source_url | |
| 381 initial_url, // source_main_frame_url | |
| 382 first_redirect_url, // original_request_url | |
| 383 first_redirect_url, // destination_url | |
| 384 false, // is_user_initiated, | |
| 385 true, // has_committed | |
| 386 false, // has_server_redirect | |
| 387 nav_map->at(first_redirect_url).at(0)); | |
| 388 VerifyNavigationEvent(first_redirect_url, // source_url | |
| 389 first_redirect_url, // source_main_frame_url | |
| 390 second_redirect_url, // original_request_url | |
| 391 second_redirect_url, // destination_url | |
| 392 false, // is_user_initiated, | |
| 393 true, // has_committed | |
| 394 false, // has_server_redirect | |
| 395 nav_map->at(second_redirect_url).at(0)); | |
| 396 VerifyNavigationEvent(second_redirect_url, // source_url | |
| 397 second_redirect_url, // source_main_frame_url | |
| 398 download_url, // original_request_url | |
| 399 download_url, // destination_url | |
| 400 false, // is_user_initiated, | |
| 401 false, // has_committed | |
| 402 false, // has_server_redirect | |
| 403 nav_map->at(download_url).at(0)); | |
| 404 VerifyHostToIpMap(); | |
| 405 } | |
| 406 | |
| 407 // Click on a link which redirects to download using window.location.href. | |
| 408 // All transitions happen in the same tab. | |
| 409 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 410 WindowLocationHrefRedirect) { | |
| 411 ClickTestLink("window_location_href_redirect", 2); | |
| 412 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 413 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 414 auto nav_map = navigation_map(); | |
| 415 ASSERT_TRUE(nav_map); | |
| 416 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 417 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 418 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 419 VerifyNavigationEvent(initial_url, // source_url | |
| 420 initial_url, // source_main_frame_url | |
| 421 initial_url, // original_request_url | |
| 422 initial_url, // destination_url | |
| 423 false, // is_user_initiated, | |
| 424 true, // has_committed | |
| 425 false, // has_server_redirect | |
| 426 nav_map->at(initial_url).at(0)); | |
| 427 VerifyNavigationEvent(initial_url, // source_url | |
| 428 initial_url, // source_main_frame_url | |
| 429 download_url, // original_request_url | |
| 430 download_url, // destination_url | |
| 431 false, // is_user_initiated, | |
| 432 false, // has_committed | |
| 433 false, // has_server_redirect | |
| 434 nav_map->at(download_url).at(0)); | |
| 435 VerifyHostToIpMap(); | |
| 436 } | |
| 437 | |
| 438 // Click on a link which redirects twice until it reaches download using a | |
| 439 // mixture of meta refresh and window.location.href. All transitions happen in | |
| 440 // the same tab. | |
| 441 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, MixRedirects) { | |
| 442 ClickTestLink("mix_redirects", 3); | |
| 443 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL); | |
| 444 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 445 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 446 auto nav_map = navigation_map(); | |
| 447 ASSERT_TRUE(nav_map); | |
| 448 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 449 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 450 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size()); | |
| 451 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 452 VerifyNavigationEvent(initial_url, // source_url | |
| 453 initial_url, // source_main_frame_url | |
| 454 initial_url, // original_request_url | |
| 455 initial_url, // destination_url | |
| 456 false, // is_user_initiated, | |
| 457 true, // has_committed | |
| 458 false, // has_server_redirect | |
| 459 nav_map->at(initial_url).at(0)); | |
| 460 VerifyNavigationEvent(initial_url, // source_url | |
| 461 initial_url, // source_main_frame_url | |
| 462 redirect_url, // original_request_url | |
| 463 redirect_url, // destination_url | |
| 464 false, // is_user_initiated, | |
| 465 true, // has_committed | |
| 466 false, // has_server_redirect | |
| 467 nav_map->at(redirect_url).at(0)); | |
| 468 VerifyNavigationEvent(redirect_url, // source_url | |
| 469 redirect_url, // source_main_frame_url | |
| 470 download_url, // original_request_url | |
| 471 download_url, // destination_url | |
| 472 false, // is_user_initiated, | |
| 473 false, // has_committed | |
| 474 false, // has_server_redirect | |
| 475 nav_map->at(download_url).at(0)); | |
| 476 VerifyHostToIpMap(); | |
| 477 } | |
| 478 | |
| 479 // Use javascript to open download in a new tab. | |
| 480 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, NewTabDownload) { | |
| 481 ClickTestLink("new_tab_download", 4); | |
| 482 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 483 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 484 GURL blank_url = GURL(url::kAboutBlankURL); | |
| 485 auto nav_map = navigation_map(); | |
| 486 ASSERT_TRUE(nav_map); | |
| 487 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 488 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 489 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size()); | |
| 490 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 491 VerifyNavigationEvent(initial_url, // source_url | |
| 492 initial_url, // source_main_frame_url | |
| 493 initial_url, // original_request_url | |
| 494 initial_url, // destination_url | |
| 495 false, // is_user_initiated, | |
| 496 true, // has_committed | |
| 497 false, // has_server_redirect | |
| 498 nav_map->at(initial_url).at(0)); | |
| 499 VerifyNavigationEvent(initial_url, // source_url | |
| 500 initial_url, // source_main_frame_url | |
| 501 blank_url, // original_request_url | |
| 502 blank_url, // destination_url | |
| 503 false, // is_user_initiated, | |
| 504 false, // has_committed | |
| 505 false, // has_server_redirect | |
| 506 nav_map->at(blank_url).at(0)); | |
| 507 // Source and target are at different tabs. | |
| 508 EXPECT_NE(nav_map->at(blank_url).at(0).source_tab_id, | |
| 509 nav_map->at(blank_url).at(0).target_tab_id); | |
| 510 VerifyNavigationEvent(GURL(), // source_url | |
| 511 GURL(), // source_main_frame_url | |
| 512 blank_url, // original_request_url | |
| 513 blank_url, // destination_url | |
| 514 false, // is_user_initiated, | |
| 515 false, // has_committed | |
| 516 false, // has_server_redirect | |
| 517 nav_map->at(blank_url).at(1)); | |
| 518 EXPECT_EQ(nav_map->at(blank_url).at(1).source_tab_id, | |
| 519 nav_map->at(blank_url).at(1).target_tab_id); | |
| 520 VerifyNavigationEvent(blank_url, // source_url | |
| 521 blank_url, // source_main_frame_url | |
| 522 download_url, // original_request_url | |
| 523 download_url, // destination_url | |
| 524 false, // is_user_initiated, | |
| 525 false, // has_committed | |
| 526 false, // has_server_redirect | |
| 527 nav_map->at(download_url).at(0)); | |
| 528 EXPECT_EQ(nav_map->at(download_url).at(0).source_tab_id, | |
| 529 nav_map->at(download_url).at(0).target_tab_id); | |
| 530 VerifyHostToIpMap(); | |
| 531 } | |
| 532 | |
| 533 // Use javascript to open download in a new tab and download has a data url. | |
| 534 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 535 NewTabDownloadWithDataURL) { | |
| 536 ClickTestLink("new_tab_download_with_data_url", 4); | |
| 537 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 538 GURL download_url = GURL(kDownloadDataURL); | |
| 539 GURL blank_url = GURL("about:blank"); | |
| 540 auto nav_map = navigation_map(); | |
| 541 ASSERT_TRUE(nav_map); | |
| 542 ASSERT_EQ(std::size_t(3), nav_map->size()); | |
| 543 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 544 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size()); | |
| 545 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 546 VerifyNavigationEvent(initial_url, // source_url | |
| 547 initial_url, // source_main_frame_url | |
| 548 initial_url, // original_request_url | |
| 549 initial_url, // destination_url | |
| 550 false, // is_user_initiated, | |
| 551 true, // has_committed | |
| 552 false, // has_server_redirect | |
| 553 nav_map->at(initial_url).at(0)); | |
| 554 VerifyNavigationEvent(initial_url, // source_url | |
| 555 initial_url, // source_main_frame_url | |
| 556 blank_url, // original_request_url | |
| 557 blank_url, // destination_url | |
| 558 false, // is_user_initiated, | |
| 559 false, // has_committed | |
| 560 false, // has_server_redirect | |
| 561 nav_map->at(blank_url).at(0)); | |
| 562 // Source and target are at different tabs. | |
| 563 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id == | |
| 564 nav_map->at(blank_url).at(0).target_tab_id); | |
| 565 VerifyNavigationEvent(GURL(), // source_url | |
| 566 GURL(), // source_main_frame_url | |
| 567 blank_url, // original_request_url | |
| 568 blank_url, // destination_url | |
| 569 false, // is_user_initiated, | |
| 570 false, // has_committed | |
| 571 false, // has_server_redirect | |
| 572 nav_map->at(blank_url).at(1)); | |
| 573 EXPECT_EQ(nav_map->at(blank_url).at(1).source_tab_id, | |
| 574 nav_map->at(blank_url).at(1).target_tab_id); | |
| 575 VerifyNavigationEvent(blank_url, // source_url | |
| 576 blank_url, // source_main_frame_url | |
| 577 download_url, // original_request_url | |
| 578 download_url, // destination_url | |
| 579 false, // is_user_initiated, | |
| 580 false, // has_committed | |
| 581 false, // has_server_redirect | |
| 582 nav_map->at(download_url).at(0)); | |
| 583 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id == | |
| 584 nav_map->at(download_url).at(0).target_tab_id); | |
| 585 // Since data url does does not have IP, host_to_ip_map_ should be empty. | |
| 586 EXPECT_EQ(std::size_t(0), host_to_ip_map()->size()); | |
| 587 } | |
| 588 | |
| 589 // TODO(jialiul): Need to figure out why this test is failing on Windows and | |
| 590 // flaky on other platforms. | |
| 591 #define MAYBE_DownloadViaHTML5FileApi DISABLED_DownloadViaHTML5FileApi | |
| 592 // Download via html5 file API. | |
| 593 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 594 MAYBE_DownloadViaHTML5FileApi) { | |
| 595 ClickTestLink("html5_file_api", 2); | |
| 596 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 597 std::string download_url_str = | |
| 598 base::StringPrintf("filesystem:%stemporary/test.exe", | |
| 599 embedded_test_server()->base_url().spec().c_str()); | |
| 600 GURL download_url = GURL(download_url_str); | |
| 601 auto nav_map = navigation_map(); | |
| 602 ASSERT_TRUE(nav_map); | |
| 603 ASSERT_EQ(std::size_t(2), nav_map->size()); | |
| 604 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size()); | |
| 605 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 606 VerifyNavigationEvent(initial_url, // source_url | |
| 607 initial_url, // source_main_frame_url | |
| 608 initial_url, // original_request_url | |
| 609 initial_url, // destination_url | |
| 610 false, // is_user_initiated, | |
| 611 true, // has_committed | |
| 612 false, // has_server_redirect | |
| 613 nav_map->at(initial_url).at(0)); | |
| 614 VerifyNavigationEvent(initial_url, // source_url | |
| 615 initial_url, // source_main_frame_url | |
| 616 download_url, // original_request_url | |
| 617 download_url, // destination_url | |
| 618 false, // is_user_initiated, | |
| 619 false, // has_committed | |
| 620 false, // has_server_redirect | |
| 621 nav_map->at(download_url).at(0)); | |
| 622 VerifyHostToIpMap(); | |
| 623 } | |
| 624 | |
| 625 // Click a link in a subframe and start download. | |
| 626 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 627 SubFrameDirectDownload) { | |
| 628 ui_test_utils::NavigateToURL( | |
| 629 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL)); | |
| 630 EXPECT_TRUE(content::WaitForLoadStop( | |
| 631 browser()->tab_strip_model()->GetActiveWebContents())); | |
| 632 std::string test_name = | |
| 633 base::StringPrintf("%s', '%s", "iframe1", "iframe_direct_download"); | |
| 634 ClickTestLink(test_name.c_str(), 1); | |
| 635 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 636 GURL multi_frame_test_url = | |
| 637 embedded_test_server()->GetURL(kMultiFrameTestURL); | |
| 638 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL); | |
| 639 GURL iframe_retargeting_url = | |
| 640 embedded_test_server()->GetURL(kIframeRetargetingURL); | |
| 641 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 642 auto nav_map = navigation_map(); | |
| 643 ASSERT_TRUE(nav_map); | |
| 644 ASSERT_EQ(std::size_t(4), nav_map->size()); | |
| 645 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size()); | |
| 646 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size()); | |
| 647 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_retargeting_url).size()); | |
| 648 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 649 VerifyNavigationEvent(initial_url, // source_url | |
| 650 initial_url, // source_main_frame_url | |
| 651 multi_frame_test_url, // original_request_url | |
| 652 multi_frame_test_url, // destination_url | |
| 653 true, // is_user_initiated, | |
| 654 true, // has_committed | |
| 655 false, // has_server_redirect | |
| 656 nav_map->at(multi_frame_test_url).at(0)); | |
| 657 VerifyNavigationEvent(GURL(), // source_url | |
| 658 multi_frame_test_url, // source_main_frame_url | |
| 659 iframe_url, // original_request_url | |
| 660 iframe_url, // destination_url | |
| 661 false, // is_user_initiated, | |
| 662 true, // has_committed | |
| 663 false, // has_server_redirect | |
| 664 nav_map->at(iframe_url).at(0)); | |
| 665 VerifyNavigationEvent(GURL(), // source_url | |
| 666 multi_frame_test_url, // source_main_frame_url | |
| 667 iframe_retargeting_url, // original_request_url | |
| 668 iframe_retargeting_url, // destination_url | |
| 669 false, // is_user_initiated, | |
| 670 true, // has_committed | |
| 671 false, // has_server_redirect | |
| 672 nav_map->at(iframe_retargeting_url).at(0)); | |
| 673 VerifyNavigationEvent(iframe_url, // source_url | |
| 674 multi_frame_test_url, // source_main_frame_url | |
| 675 download_url, // original_request_url | |
| 676 download_url, // destination_url | |
| 677 false, // is_user_initiated, | |
| 678 false, // has_committed | |
| 679 false, // has_server_redirect | |
| 680 nav_map->at(download_url).at(0)); | |
| 681 VerifyHostToIpMap(); | |
| 682 } | |
| 683 | |
| 684 // Click a link in a subframe and open download in a new tab. | |
| 685 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, | |
| 686 SubFrameNewTabDownload) { | |
| 687 ui_test_utils::NavigateToURL( | |
| 688 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL)); | |
| 689 EXPECT_TRUE(content::WaitForLoadStop( | |
| 690 browser()->tab_strip_model()->GetActiveWebContents())); | |
| 691 std::string test_name = | |
| 692 base::StringPrintf("%s', '%s", "iframe2", "iframe_new_tab_download"); | |
| 693 ClickTestLink(test_name.c_str(), 4); | |
| 694 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 695 GURL multi_frame_test_url = | |
| 696 embedded_test_server()->GetURL(kMultiFrameTestURL); | |
| 697 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL); | |
| 698 GURL iframe_retargeting_url = | |
| 699 embedded_test_server()->GetURL(kIframeRetargetingURL); | |
| 700 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 701 GURL blank_url = GURL("about:blank"); | |
| 702 auto nav_map = navigation_map(); | |
| 703 ASSERT_TRUE(nav_map); | |
| 704 ASSERT_EQ(std::size_t(5), nav_map->size()); | |
| 705 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size()); | |
| 706 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size()); | |
| 707 ASSERT_EQ(std::size_t(2), nav_map->at(iframe_retargeting_url).size()); | |
| 708 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size()); | |
| 709 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 710 VerifyNavigationEvent(initial_url, // source_url | |
| 711 initial_url, // source_main_frame_url | |
| 712 multi_frame_test_url, // original_request_url | |
| 713 multi_frame_test_url, // destination_url | |
| 714 true, // is_user_initiated, | |
| 715 true, // has_committed | |
| 716 false, // has_server_redirect | |
| 717 nav_map->at(multi_frame_test_url).at(0)); | |
| 718 VerifyNavigationEvent(GURL(), // source_url | |
| 719 multi_frame_test_url, // source_main_frame_url | |
| 720 iframe_url, // original_request_url | |
| 721 iframe_url, // destination_url | |
| 722 false, // is_user_initiated, | |
| 723 true, // has_committed | |
| 724 false, // has_server_redirect | |
| 725 nav_map->at(iframe_url).at(0)); | |
| 726 VerifyNavigationEvent(GURL(), // source_url | |
| 727 multi_frame_test_url, // source_main_frame_url | |
| 728 iframe_retargeting_url, // original_request_url | |
| 729 iframe_retargeting_url, // destination_url | |
| 730 false, // is_user_initiated, | |
| 731 true, // has_committed | |
| 732 false, // has_server_redirect | |
| 733 nav_map->at(iframe_retargeting_url).at(0)); | |
| 734 VerifyNavigationEvent(iframe_retargeting_url, // source_url | |
| 735 multi_frame_test_url, // source_main_frame_url | |
| 736 iframe_retargeting_url, // original_request_url | |
| 737 iframe_retargeting_url, // destination_url | |
| 738 false, // is_user_initiated, | |
| 739 true, // has_committed | |
| 740 false, // has_server_redirect | |
| 741 nav_map->at(iframe_retargeting_url).at(1)); | |
| 742 VerifyNavigationEvent(iframe_retargeting_url, // source_url | |
| 743 multi_frame_test_url, // source_main_frame_url | |
| 744 blank_url, // original_request_url | |
| 745 blank_url, // destination_url | |
| 746 false, // is_user_initiated, | |
| 747 false, // has_committed | |
| 748 false, // has_server_redirect | |
| 749 nav_map->at(blank_url).at(0)); | |
| 750 VerifyNavigationEvent(GURL(), // source_url | |
| 751 GURL(), // source_main_frame_url | |
| 752 blank_url, // original_request_url | |
| 753 blank_url, // destination_url | |
| 754 false, // is_user_initiated, | |
| 755 false, // has_committed | |
| 756 false, // has_server_redirect | |
| 757 nav_map->at(blank_url).at(1)); | |
| 758 VerifyNavigationEvent(blank_url, // source_url | |
| 759 blank_url, // source_main_frame_url | |
| 760 download_url, // original_request_url | |
| 761 download_url, // destination_url | |
| 762 false, // is_user_initiated, | |
| 763 false, // has_committed | |
| 764 false, // has_server_redirect | |
| 765 nav_map->at(download_url).at(0)); | |
| 766 VerifyHostToIpMap(); | |
| 767 } | |
| 768 | |
| 769 // Server-side redirect. | |
| 770 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, ServerRedirect) { | |
| 771 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL); | |
| 772 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL); | |
| 773 GURL request_url = | |
| 774 embedded_test_server()->GetURL("/server-redirect?" + download_url.spec()); | |
| 775 ui_test_utils::NavigateToURL(browser(), request_url); | |
| 776 CancelDownloads(); | |
| 777 auto nav_map = navigation_map(); | |
| 778 ASSERT_TRUE(nav_map); | |
| 779 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 780 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size()); | |
| 781 VerifyNavigationEvent(initial_url, // source_url | |
| 782 initial_url, // source_main_frame_url | |
| 783 request_url, // original_request_url | |
| 784 download_url, // destination_url | |
| 785 true, // is_user_initiated, | |
| 786 false, // has_committed | |
| 787 true, // has_server_redirect | |
| 788 nav_map->at(download_url).at(0)); | |
| 789 } | |
| 790 | |
| 791 // host_to_ip_map_ size should increase by one after a new navigation. | |
| 792 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, AddIPMapping) { | |
| 793 auto ip_map = host_to_ip_map(); | |
| 794 std::string test_server_host(embedded_test_server()->base_url().host()); | |
| 795 ip_map->insert( | |
| 796 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>())); | |
| 797 ASSERT_EQ(std::size_t(0), ip_map->at(test_server_host).size()); | |
| 798 ClickTestLink("direct_download", 1); | |
| 799 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size()); | |
| 800 EXPECT_EQ(embedded_test_server()->host_port_pair().host(), | |
| 801 ip_map->at(test_server_host).back().ip); | |
| 802 } | |
| 803 | |
| 804 // If we have already seen an IP associated with a host, update its timestamp. | |
| 805 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, IPListDedup) { | |
| 806 auto ip_map = host_to_ip_map(); | |
| 807 std::string test_server_host(embedded_test_server()->base_url().host()); | |
| 808 ip_map->insert( | |
| 809 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>())); | |
| 810 base::Time yesterday(base::Time::Now() - base::TimeDelta::FromDays(1)); | |
| 811 ip_map->at(test_server_host) | |
| 812 .push_back(ResolvedIPAddress( | |
| 813 yesterday, embedded_test_server()->host_port_pair().host())); | |
| 814 ASSERT_EQ(std::size_t(1), ip_map->at(test_server_host).size()); | |
| 815 ClickTestLink("direct_download", 1); | |
| 816 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size()); | |
| 817 EXPECT_EQ(embedded_test_server()->host_port_pair().host(), | |
| 818 ip_map->at(test_server_host).back().ip); | |
| 819 EXPECT_NE(yesterday, ip_map->at(test_server_host).front().timestamp); | |
| 820 } | |
| 821 | |
| 822 } // namespace safe_browsing | |
| OLD | NEW |