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