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