Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_navigation_observer_browsertest.cc

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

Powered by Google App Engine
This is Rietveld 408576698