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