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

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: make all bots happy 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_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 event we need.
Charlie Reis 2016/10/25 04:52:27 nit: events
Jialiu Lin 2016/10/27 02:15:20 Done.
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 navigation completes.
Charlie Reis 2016/10/25 04:52:26 nit: navigations complete.
Jialiu Lin 2016/10/27 02:15:20 Done.
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_original_request_url,
158 const GURL& expected_source_main_frame_url,
159 bool expected_is_user_initiated,
160 bool expected_has_committed,
161 bool expected_has_server_redirect,
162 const GURL& expected_destination_url,
163 const NavigationEvent& actual_nav_event) {
164 EXPECT_EQ(expected_source_url, actual_nav_event.source_url);
165 EXPECT_EQ(expected_original_request_url,
166 actual_nav_event.original_request_url);
167 EXPECT_EQ(expected_source_main_frame_url,
168 actual_nav_event.source_main_frame_url);
169 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated);
170 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed);
171 EXPECT_EQ(expected_has_server_redirect,
172 actual_nav_event.has_server_redirect);
173 EXPECT_EQ(expected_destination_url, actual_nav_event.destination_url);
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 java script to mimic clicking on a link, there is no
Charlie Reis 2016/10/25 04:52:27 nit: Javascript Still, this sounds incorrect to m
Jialiu Lin 2016/10/27 02:15:20 Not sure why, but when I tested manually, WebConte
Charlie Reis 2016/10/28 23:36:08 Hmm, I wonder if we have two different ways of tra
Jialiu Lin 2016/10/29 00:45:34 It is probably because DidGetUserInteraction() onl
215 // actual user gesture, therefore is_user_initiated is false.
216 VerifyNavigationEvent(initial_url, // source_url
217 download_url, // original_request_url
218 initial_url, // source_main_frame_url
Charlie Reis 2016/10/25 04:52:26 Seems like this would be easier to read if the sou
Jialiu Lin 2016/10/27 02:15:20 Done.
Charlie Reis 2016/10/28 23:36:08 Thanks! I'm finding the tests much easier to read
219 false, // is_user_initiated,
220 false, // has_committed
221 false, // has_server_redirect
222 download_url, // destination_url
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 download_url, // original_request_url
240 initial_url, // source_main_frame_url
241 false, // is_user_initiated,
242 false, // has_committed
243 false, // has_server_redirect
244 download_url, // destination_url
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 VerifyNavigationEvent(initial_url, // source_url
261 download_url, // original_request_url
262 initial_url, // source_main_frame_url
263 false, // is_user_initiated,
264 false, // has_committed
265 false, // has_server_redirect
266 download_url, // destination_url
267 nav_map->at(download_url).at(0));
268 VerifyNavigationEvent(download_url, download_url, download_url, false, false,
Charlie Reis 2016/10/25 04:52:27 I think it would be useful to format this like the
Jialiu Lin 2016/10/27 02:15:20 The first one comes from NOTIFICATION_RETARGETING,
Charlie Reis 2016/10/28 23:36:08 Ah, that explains it. Thanks!
269 false, download_url, nav_map->at(download_url).at(1));
270 VerifyHostToIpMap();
271 }
272
273 // Click on a link which navigates to a page then redirect to download using
Charlie Reis 2016/10/25 04:52:26 nit: redirects to a download
Jialiu Lin 2016/10/27 02:15:20 Done.
274 // META HTTP-EQUIV="refresh". All transitions happen in the same tab.
275 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
276 SingleMetaRefreshRedirect) {
277 ClickTestLink("single_meta_refresh_redirect", 2);
278 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
279 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
280 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
281 auto nav_map = navigation_map();
282 ASSERT_TRUE(nav_map);
283 ASSERT_EQ(std::size_t(2), nav_map->size());
284 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size());
285 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
286 VerifyNavigationEvent(initial_url, // source_url
287 redirect_url, // original_request_url
288 initial_url, // source_main_frame_url
289 false, // is_user_initiated,
290 true, // has_committed
291 false, // has_server_redirect
292 redirect_url, // destination_url
Charlie Reis 2016/10/25 04:52:27 This is a bit surprising when first reading the te
Jialiu Lin 2016/10/27 02:15:20 Sure. comment added. Server side redirection is t
Charlie Reis 2016/10/28 23:36:08 We've found that unit tests (while they're useful
Jialiu Lin 2016/10/29 00:45:34 Sure. redirect browser test added.
293 nav_map->at(redirect_url).at(0));
294 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
295 false, download_url, nav_map->at(download_url).at(0));
296 VerifyHostToIpMap();
297 }
298
299 // Click on a link which navigates to a page then redirect to download using
Charlie Reis 2016/10/25 04:52:27 nit: redirects to a download
Jialiu Lin 2016/10/27 02:15:20 Done.
300 // META HTTP-EQUIV="refresh". First navigation happens in target blank.
301 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
302 SingleMetaRefreshRedirectTargetBlank) {
303 ClickTestLink("single_meta_refresh_redirect_target_blank", 3);
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 ASSERT_EQ(std::size_t(2), nav_map->size());
310 ASSERT_EQ(std::size_t(2), nav_map->at(redirect_url).size());
311 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
312 VerifyNavigationEvent(initial_url, // source_url
313 redirect_url, // original_request_url
314 initial_url, // source_main_frame_url
315 false, // is_user_initiated,
316 false, // has_committed
317 false, // has_server_redirect
318 redirect_url, // destination_url
319 nav_map->at(redirect_url).at(0));
320 VerifyNavigationEvent(redirect_url, redirect_url, redirect_url, false, true,
321 false, redirect_url, nav_map->at(redirect_url).at(1));
Charlie Reis 2016/10/25 04:52:27 What's going on in this one? I'm not sure why we
Jialiu Lin 2016/10/27 02:15:20 Yes, source should be empty after changes in obser
322 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
323 false, download_url, nav_map->at(download_url).at(0));
324 VerifyHostToIpMap();
325 }
326
327 // Click on a link which redirects twice before reaching download using
328 // META HTTP-EQUIV="refresh". All transitions happen in the same tab.
329 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
330 MultiMetaRefreshRedirects) {
331 ClickTestLink("multiple_meta_refresh_redirects", 3);
332 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
333 GURL first_redirect_url = embedded_test_server()->GetURL(
334 "/safe_browsing/download_protection/navigation_observer/"
335 "double_redirect.html");
336 GURL second_redirect_url = embedded_test_server()->GetURL(kRedirectURL);
337 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
338 auto nav_map = navigation_map();
339 ASSERT_TRUE(nav_map);
340 ASSERT_EQ(std::size_t(3), nav_map->size());
341 ASSERT_EQ(std::size_t(1), nav_map->at(first_redirect_url).size());
342 ASSERT_EQ(std::size_t(1), nav_map->at(second_redirect_url).size());
343 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
344 VerifyNavigationEvent(initial_url, // source_url
345 first_redirect_url, // original_request_url
346 initial_url, // source_main_frame_url
347 false, // is_user_initiated,
348 true, // has_committed
349 false, // has_server_redirect
350 first_redirect_url, // destination_url
351 nav_map->at(first_redirect_url).at(0));
352 VerifyNavigationEvent(
353 first_redirect_url, second_redirect_url, first_redirect_url, false, true,
354 false, second_redirect_url, nav_map->at(second_redirect_url).at(0));
355 VerifyNavigationEvent(second_redirect_url, download_url, second_redirect_url,
356 false, false, false, download_url,
357 nav_map->at(download_url).at(0));
358 VerifyHostToIpMap();
359 }
360
361 // Click on a link which redirects to download using window.location.href.
362 // All transitions happen in the same tab.
363 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
364 WindowLocationHrefRedirect) {
365 ClickTestLink("window_location_href_redirect", 2);
366 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
367 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
368 auto nav_map = navigation_map();
369 ASSERT_TRUE(nav_map);
370 ASSERT_EQ(std::size_t(2), nav_map->size());
371 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
372 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
373 VerifyNavigationEvent(initial_url, // source_url
374 initial_url, // original_request_url
375 initial_url, // source_main_frame_url
376 false, // is_user_initiated,
377 true, // has_committed
378 false, // has_server_redirect
379 initial_url, // destination_url
380 nav_map->at(initial_url).at(0));
381 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
382 false, download_url, nav_map->at(download_url).at(0));
383 VerifyHostToIpMap();
384 }
385
386 // Click on a link which redirect twice until reaches download using a mixture
Charlie Reis 2016/10/25 04:52:26 nit: redirects twice until it
Jialiu Lin 2016/10/27 02:15:20 Done.
387 // of meta refresh and window.location.href. All transitions happen in the same
388 // tab.
389 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, MixRedirects) {
390 ClickTestLink("mix_redirects", 3);
391 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
392 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
393 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
394 auto nav_map = navigation_map();
395 ASSERT_TRUE(nav_map);
396 ASSERT_EQ(std::size_t(3), nav_map->size());
397 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
398 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size());
399 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
400 VerifyNavigationEvent(initial_url, // source_url
401 initial_url, // original_request_url
402 initial_url, // source_main_frame_url
403 false, // is_user_initiated,
404 true, // has_committed
405 false, // has_server_redirect
406 initial_url, // destination_url
407 nav_map->at(initial_url).at(0));
408 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, true,
409 false, redirect_url, nav_map->at(redirect_url).at(0));
410 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
411 false, download_url, nav_map->at(download_url).at(0));
412 VerifyHostToIpMap();
413 }
414
415 // Use javascript to open download in a new tab.
416 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, NewTabDownload) {
417 ClickTestLink("new_tab_download", 4);
418 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
419 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
420 GURL blank_url = GURL("about:blank");
Charlie Reis 2016/10/25 04:52:27 nit: url::kAboutBlankURL
Jialiu Lin 2016/10/27 02:15:20 Done.
421 auto nav_map = navigation_map();
422 ASSERT_TRUE(nav_map);
423 ASSERT_EQ(std::size_t(3), nav_map->size());
424 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
425 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
426 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
427 VerifyNavigationEvent(initial_url, // source_url
428 initial_url, // original_request_url
429 initial_url, // source_main_frame_url
430 false, // is_user_initiated,
431 true, // has_committed
432 false, // has_server_redirect
433 initial_url, // destination_url
434 nav_map->at(initial_url).at(0));
435 VerifyNavigationEvent(initial_url, blank_url, initial_url, false, false,
436 false, blank_url, nav_map->at(blank_url).at(0));
437 // Source and target are at different tabs.
438 EXPECT_NE(nav_map->at(blank_url).at(0).source_tab_id,
439 nav_map->at(blank_url).at(0).target_tab_id);
440 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
441 blank_url, nav_map->at(blank_url).at(1));
442 EXPECT_EQ(nav_map->at(blank_url).at(1).source_tab_id,
443 nav_map->at(blank_url).at(1).target_tab_id);
444 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
445 download_url, nav_map->at(download_url).at(0));
Charlie Reis 2016/10/25 04:52:27 Can you remind me how we're going to link this Nav
Jialiu Lin 2016/10/27 02:15:20 Since all the NavigationEvents are keyed on their
Charlie Reis 2016/10/28 23:36:08 Thanks. As discussed, the "most recent" approach
Jialiu Lin 2016/10/29 00:45:34 Yes, sure. That's my plan too.
446 EXPECT_EQ(nav_map->at(download_url).at(0).source_tab_id,
447 nav_map->at(download_url).at(0).target_tab_id);
448 VerifyHostToIpMap();
449 }
450
451 // Use javascript to open download in a new tab and download has a data url.
452 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
453 NewTabDownloadWithDataURL) {
454 ClickTestLink("new_tab_download_with_data_url", 4);
455 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
456 GURL download_url = GURL(kDownloadDataURL);
457 GURL blank_url = GURL("about:blank");
458 auto nav_map = navigation_map();
459 ASSERT_TRUE(nav_map);
460 ASSERT_EQ(std::size_t(3), nav_map->size());
461 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
462 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
463 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
464 VerifyNavigationEvent(initial_url, // source_url
465 initial_url, // original_request_url
466 initial_url, // source_main_frame_url
467 false, // is_user_initiated,
468 true, // has_committed
469 false, // has_server_redirect
470 initial_url, // destination_url
471 nav_map->at(initial_url).at(0));
472 VerifyNavigationEvent(initial_url, blank_url, initial_url, false, false,
473 false, blank_url, nav_map->at(blank_url).at(0));
474 // Source and target are at different tabs.
475 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id ==
476 nav_map->at(blank_url).at(0).target_tab_id);
477 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
478 blank_url, nav_map->at(blank_url).at(1));
479 EXPECT_TRUE(nav_map->at(blank_url).at(1).source_tab_id ==
480 nav_map->at(blank_url).at(1).target_tab_id);
481 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
482 download_url, nav_map->at(download_url).at(0));
483 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id ==
484 nav_map->at(download_url).at(0).target_tab_id);
485 // Since data url does does not have IP, host_to_ip_map_ should be empty.
486 EXPECT_EQ(std::size_t(0), host_to_ip_map()->size());
487 }
488
489 // TODO(jialiul): Need to figure out why this test is failing on Windows and
490 // flacky on other platforms.
Charlie Reis 2016/10/25 04:52:27 nit: flaky
Jialiu Lin 2016/10/27 02:15:20 Done.
491 #define MAYBE_DownloadViaHTML5FileApi DISABLED_DownloadViaHTML5FileApi
492 // Download via html5 file API.
493 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
494 MAYBE_DownloadViaHTML5FileApi) {
495 ClickTestLink("html5_file_api", 2);
496 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
497 std::string download_url_str =
498 base::StringPrintf("filesystem:%stemporary/test.exe",
499 embedded_test_server()->base_url().spec().c_str());
500 GURL download_url = GURL(download_url_str);
501 auto nav_map = navigation_map();
502 ASSERT_TRUE(nav_map);
503 ASSERT_EQ(std::size_t(2), nav_map->size());
504 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
505 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
506 VerifyNavigationEvent(initial_url, // source_url
507 initial_url, // original_request_url
508 initial_url, // source_main_frame_url
509 false, // is_user_initiated,
510 true, // has_committed
511 false, // has_server_redirect
512 initial_url, // destination_url
513 nav_map->at(initial_url).at(0));
514 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
515 false, download_url, nav_map->at(download_url).at(0));
516 VerifyHostToIpMap();
517 }
518
519 // Click a link in a subframe and start download.
520 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
521 SubFrameDirectDownload) {
522 ui_test_utils::NavigateToURL(
523 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL));
524 EXPECT_TRUE(content::WaitForLoadStop(
525 browser()->tab_strip_model()->GetActiveWebContents()));
526 std::string test_name =
527 base::StringPrintf("%s', '%s", "iframe1", "iframe_direct_download");
528 ClickTestLink(test_name.c_str(), 1);
529 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
530 GURL multi_frame_test_url =
531 embedded_test_server()->GetURL(kMultiFrameTestURL);
532 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL);
533 GURL iframe_retargeting_url =
534 embedded_test_server()->GetURL(kIframeRetargetingURL);
535 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
536 auto nav_map = navigation_map();
537 ASSERT_TRUE(nav_map);
538 ASSERT_EQ(std::size_t(4), nav_map->size());
539 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size());
540 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size());
541 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_retargeting_url).size());
542 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
543 VerifyNavigationEvent(initial_url, // source_url
544 multi_frame_test_url, // original_request_url
545 initial_url, // source_main_frame_url
546 true, // is_user_initiated,
547 true, // has_committed
548 false, // has_server_redirect
549 multi_frame_test_url, // destination_url
550 nav_map->at(multi_frame_test_url).at(0));
551 VerifyNavigationEvent(iframe_url, iframe_url, multi_frame_test_url, false,
552 true, false, iframe_url, nav_map->at(iframe_url).at(0));
553 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
554 multi_frame_test_url, false, true, false,
555 iframe_retargeting_url,
556 nav_map->at(iframe_retargeting_url).at(0));
557 VerifyNavigationEvent(iframe_url, download_url, multi_frame_test_url, false,
558 false, false, download_url,
559 nav_map->at(download_url).at(0));
560 VerifyHostToIpMap();
561 }
562
563 // Click a link in a subframe and open download in a new tab.
564 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
565 SubFrameNewTabDownload) {
566 ui_test_utils::NavigateToURL(
567 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL));
568 EXPECT_TRUE(content::WaitForLoadStop(
569 browser()->tab_strip_model()->GetActiveWebContents()));
570 std::string test_name =
571 base::StringPrintf("%s', '%s", "iframe2", "iframe_new_tab_download");
572 ClickTestLink(test_name.c_str(), 4);
573 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
574 GURL multi_frame_test_url =
575 embedded_test_server()->GetURL(kMultiFrameTestURL);
576 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL);
577 GURL iframe_retargeting_url =
578 embedded_test_server()->GetURL(kIframeRetargetingURL);
579 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
580 GURL blank_url = GURL("about:blank");
581 auto nav_map = navigation_map();
582 ASSERT_TRUE(nav_map);
583 ASSERT_EQ(std::size_t(5), nav_map->size());
584 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size());
585 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size());
586 ASSERT_EQ(std::size_t(2), nav_map->at(iframe_retargeting_url).size());
587 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
588 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
589 VerifyNavigationEvent(initial_url, // source_url
590 multi_frame_test_url, // original_request_url
591 initial_url, // source_main_frame_url
592 true, // is_user_initiated,
593 true, // has_committed
594 false, // has_server_redirect
595 multi_frame_test_url, // destination_url
596 nav_map->at(multi_frame_test_url).at(0));
597 VerifyNavigationEvent(iframe_url, iframe_url, multi_frame_test_url, false,
598 true, false, iframe_url, nav_map->at(iframe_url).at(0));
599 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
600 multi_frame_test_url, false, true, false,
601 iframe_retargeting_url,
602 nav_map->at(iframe_retargeting_url).at(0));
603 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
604 multi_frame_test_url, false, true, false,
605 iframe_retargeting_url,
606 nav_map->at(iframe_retargeting_url).at(1));
607 VerifyNavigationEvent(iframe_retargeting_url, blank_url, multi_frame_test_url,
608 false, false, false, blank_url,
609 nav_map->at(blank_url).at(0));
610 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
611 blank_url, nav_map->at(blank_url).at(1));
612 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
613 download_url, nav_map->at(download_url).at(0));
614 VerifyHostToIpMap();
615 }
616
617 // host_to_ip_map_ size should increase by one after a new navigation.
618 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, AddIPMapping) {
619 auto ip_map = host_to_ip_map();
620 std::string test_server_host(embedded_test_server()->base_url().host());
621 ip_map->insert(
622 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>()));
623 ASSERT_EQ(std::size_t(0), ip_map->at(test_server_host).size());
624 ClickTestLink("direct_download", 1);
625 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
626 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
627 ip_map->at(test_server_host).back().ip);
628 }
629
630 // If we have already seen an IP associated with a host, update its timestamp.
631 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, IPListDedup) {
632 auto ip_map = host_to_ip_map();
633 std::string test_server_host(embedded_test_server()->base_url().host());
634 ip_map->insert(
635 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>()));
636 base::Time yesterday(base::Time::Now() - base::TimeDelta::FromDays(1));
637 ip_map->at(test_server_host)
638 .push_back(ResolvedIPAddress(
639 yesterday, embedded_test_server()->host_port_pair().host()));
640 ASSERT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
641 ClickTestLink("direct_download", 1);
642 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
643 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
644 ip_map->at(test_server_host).back().ip);
645 EXPECT_NE(yesterday, ip_map->at(test_server_host).front().timestamp);
646 }
647
648 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698