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

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: address creis@ comments Created 4 years, 2 months 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.
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.
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,
Nathan Parker 2016/10/19 22:56:14 These repeated GURLs and bools are pretty easy to
Jialiu Lin 2016/10/21 02:38:39 Adding comments to the first call of this function
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_actual_target_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_actual_target_url, actual_nav_event.actual_target_url);
174 }
175
176 void VerifyHostToIpMap() {
177 // Since all testing pages have the same host, there is onlly one entry in
Nathan Parker 2016/10/19 22:56:14 nit:only
Jialiu Lin 2016/10/21 02:38:39 Done.
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 ASSERT_EQ(std::size_t(1),
183 actual_host_ip_map->at(embedded_test_server()->base_url().host())
Nathan Parker 2016/10/19 22:56:14 nit: Easier to read w/ a temporary auto& ip_list =
Jialiu Lin 2016/10/21 02:38:39 Done.
184 .size());
185 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
186 actual_host_ip_map->at(embedded_test_server()->base_url().host())
187 .back()
188 .ip);
189 }
190
191 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() {
192 return observer_manager_->navigation_map();
193 }
194
195 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() {
196 return observer_manager_->host_to_ip_map();
197 }
198
199 protected:
200 SafeBrowsingNavigationObserverManager* observer_manager_;
201 SafeBrowsingNavigationObserver* observer_;
202
203 private:
204 base::ScopedTempDir downloads_directory_;
205 };
206
207 // Click on a link and start download on the same page.
208 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, DirectDownload) {
209 ClickTestLink("direct_download", 1);
210 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
211 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
212 auto nav_map = navigation_map();
213 ASSERT_TRUE(nav_map);
214 ASSERT_EQ(std::size_t(1), nav_map->size());
215 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
216 // Since this test uses java script to mimic clicking on a link, there is no
217 // actual user gesture, therefore is_user_initiated is false.
218 VerifyNavigationEvent(initial_url, // source_url
219 download_url, // original_request_url
220 initial_url, // source_main_frame_url
221 false, // is_user_initiated,
222 false, // has_committed
223 false, // has_server_redirect
224 download_url, // actual_target_url
225 nav_map->at(download_url).at(0));
226 VerifyHostToIpMap();
227 }
228
229 // Click on a link with rel="noreferrer" attribute, and start download on the
230 // same page.
231 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
232 DirectDownloadNoReferrer) {
233 ClickTestLink("direct_download_noreferrer", 1);
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 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
241 false, download_url, nav_map->at(download_url).at(0));
242 VerifyHostToIpMap();
243 }
244
245 // Click on a link with rel="noreferrer" attribute, and start download in a
246 // new tab using target=_blank.
247 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
248 DirectDownloadNoReferrerTargetBlank) {
249 ClickTestLink("direct_download_noreferrer_target_blank", 1);
250 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
251 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
252 auto nav_map = navigation_map();
253 ASSERT_TRUE(nav_map);
254 ASSERT_EQ(std::size_t(1), nav_map->size());
255 ASSERT_EQ(std::size_t(2), nav_map->at(download_url).size());
256 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
257 false, download_url, nav_map->at(download_url).at(0));
258 VerifyNavigationEvent(download_url, download_url, download_url, false, false,
259 false, download_url, nav_map->at(download_url).at(1));
260 VerifyHostToIpMap();
261 }
262
263 // Click on a link which navigates to a page then redirect to download using
264 // META HTTP-EQUIV="refresh". All transitions happen in the same tab.
265 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
266 SingleMetaRefreshRedirect) {
267 ClickTestLink("single_meta_refresh_redirect", 2);
268 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
269 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
270 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
271 auto nav_map = navigation_map();
272 ASSERT_TRUE(nav_map);
273 ASSERT_EQ(std::size_t(2), nav_map->size());
274 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size());
275 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
276 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, true,
277 false, redirect_url, nav_map->at(redirect_url).at(0));
278 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
279 false, download_url, nav_map->at(download_url).at(0));
280 VerifyHostToIpMap();
281 }
282
283 // Click on a link which navigates to a page then redirect to download using
284 // META HTTP-EQUIV="refresh". First navigation happens in target blank.
285 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
286 SingleMetaRefreshRedirectTargetBlank) {
287 ClickTestLink("single_meta_refresh_redirect_target_blank", 3);
288 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
289 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
290 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
291 auto nav_map = navigation_map();
292 ASSERT_TRUE(nav_map);
293 ASSERT_EQ(std::size_t(2), nav_map->size());
294 ASSERT_EQ(std::size_t(2), nav_map->at(redirect_url).size());
295 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
296 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, false,
297 false, redirect_url, nav_map->at(redirect_url).at(0));
298 VerifyNavigationEvent(redirect_url, redirect_url, redirect_url, false, true,
299 false, redirect_url, nav_map->at(redirect_url).at(1));
300 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
301 false, download_url, nav_map->at(download_url).at(0));
302 VerifyHostToIpMap();
303 }
304
305 // Click on a link which redirects twice before reaching download using
306 // META HTTP-EQUIV="refresh". All transitions happen in the same tab.
307 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
308 MultiMetaRefreshRedirects) {
309 ClickTestLink("multiple_meta_refresh_redirects", 3);
310 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
311 GURL first_redirect_url = embedded_test_server()->GetURL(
312 "/safe_browsing/download_protection/navigation_observer/"
313 "double_redirect.html");
314 GURL second_redirect_url = embedded_test_server()->GetURL(kRedirectURL);
315 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
316 auto nav_map = navigation_map();
317 ASSERT_TRUE(nav_map);
318 ASSERT_EQ(std::size_t(3), nav_map->size());
319 ASSERT_EQ(std::size_t(1), nav_map->at(first_redirect_url).size());
320 ASSERT_EQ(std::size_t(1), nav_map->at(second_redirect_url).size());
321 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
322 VerifyNavigationEvent(initial_url, first_redirect_url, initial_url, false,
323 true, false, first_redirect_url,
324 nav_map->at(first_redirect_url).at(0));
325 VerifyNavigationEvent(
326 first_redirect_url, second_redirect_url, first_redirect_url, false, true,
327 false, second_redirect_url, nav_map->at(second_redirect_url).at(0));
328 VerifyNavigationEvent(second_redirect_url, download_url, second_redirect_url,
329 false, false, false, download_url,
330 nav_map->at(download_url).at(0));
331 VerifyHostToIpMap();
332 }
333
334 // Click on a link which redirects to download using window.location.href.
335 // All transitions happen in the same tab.
336 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
337 WindowLocationHrefRedirect) {
338 ClickTestLink("window_location_href_redirect", 2);
339 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
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(1), nav_map->at(initial_url).size());
345 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
346 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
347 false, initial_url, nav_map->at(initial_url).at(0));
348 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
349 false, download_url, nav_map->at(download_url).at(0));
350 VerifyHostToIpMap();
351 }
352
353 // Click on a link which redirect twice until reaches download using a mixture
354 // of meta refresh and window.location.href. All transitions happen in the same
355 // tab.
356 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, MixRedirects) {
357 ClickTestLink("mix_redirects", 3);
358 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
359 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
360 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
361 auto nav_map = navigation_map();
362 ASSERT_TRUE(nav_map);
363 ASSERT_EQ(std::size_t(3), nav_map->size());
364 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
365 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size());
366 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
367 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
368 false, initial_url, nav_map->at(initial_url).at(0));
369 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, true,
370 false, redirect_url, nav_map->at(redirect_url).at(0));
371 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
372 false, download_url, nav_map->at(download_url).at(0));
373 VerifyHostToIpMap();
374 }
375
376 // Use javascript to open download in a new tab.
377 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, NewTabDownload) {
378 ClickTestLink("new_tab_download", 4);
379 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
380 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
381 GURL blank_url = GURL("about:blank");
382 auto nav_map = navigation_map();
383 ASSERT_TRUE(nav_map);
384 ASSERT_EQ(std::size_t(3), nav_map->size());
385 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
386 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
387 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
388 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
389 false, initial_url, nav_map->at(initial_url).at(0));
390 VerifyNavigationEvent(initial_url, blank_url, initial_url, false, false,
391 false, blank_url, nav_map->at(blank_url).at(0));
392 // Source and target are at different tabs.
393 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id ==
394 nav_map->at(blank_url).at(0).target_tab_id);
395 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
396 blank_url, nav_map->at(blank_url).at(1));
397 EXPECT_TRUE(nav_map->at(blank_url).at(1).source_tab_id ==
398 nav_map->at(blank_url).at(1).target_tab_id);
399 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
400 download_url, nav_map->at(download_url).at(0));
401 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id ==
402 nav_map->at(download_url).at(0).target_tab_id);
403 VerifyHostToIpMap();
404 }
405
406 // Use javascript to open download in a new tab and download has a data url.
407 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
408 NewTabDownloadWithDataURL) {
409 ClickTestLink("new_tab_download_with_data_url", 4);
410 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
411 GURL download_url = GURL(kDownloadDataURL);
412 GURL blank_url = GURL("about:blank");
413 auto nav_map = navigation_map();
414 ASSERT_TRUE(nav_map);
415 ASSERT_EQ(std::size_t(3), nav_map->size());
416 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
417 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
418 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
419 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
420 false, initial_url, nav_map->at(initial_url).at(0));
421 VerifyNavigationEvent(initial_url, blank_url, initial_url, false, false,
422 false, blank_url, nav_map->at(blank_url).at(0));
423 // Source and target are at different tabs.
424 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id ==
Nathan Parker 2016/10/19 22:56:14 nit: EXPECT_EQ and _NE rather than TRUE and FALSE.
Jialiu Lin 2016/10/21 02:38:39 Done.
425 nav_map->at(blank_url).at(0).target_tab_id);
426 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
427 blank_url, nav_map->at(blank_url).at(1));
428 EXPECT_TRUE(nav_map->at(blank_url).at(1).source_tab_id ==
429 nav_map->at(blank_url).at(1).target_tab_id);
430 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
431 download_url, nav_map->at(download_url).at(0));
432 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id ==
433 nav_map->at(download_url).at(0).target_tab_id);
434 // Since data url does does not have IP, host_to_ip_map_ should be empty.
435 EXPECT_EQ(std::size_t(0), host_to_ip_map()->size());
436 }
437
438 // TODO(jialiul): Need to figure out why this test is failing on Windows and
439 // flacky on other platforms.
440 #define MAYBE_DownloadViaHTML5FileApi DISABLED_DownloadViaHTML5FileApi
441 // Download via html5 file API.
442 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
443 MAYBE_DownloadViaHTML5FileApi) {
444 ClickTestLink("html5_file_api", 2);
445 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
446 std::string download_url_str =
447 base::StringPrintf("filesystem:%stemporary/test.exe",
448 embedded_test_server()->base_url().spec().c_str());
449 GURL download_url = GURL(download_url_str);
450 auto nav_map = navigation_map();
451 ASSERT_TRUE(nav_map);
452 ASSERT_EQ(std::size_t(2), nav_map->size());
453 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
454 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
455 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
456 false, initial_url, nav_map->at(initial_url).at(0));
457 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
458 false, download_url, nav_map->at(download_url).at(0));
459 VerifyHostToIpMap();
460 }
461
462 // Click a link in a subframe and start download.
463 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
464 SubFrameDirectDownload) {
465 ui_test_utils::NavigateToURL(
466 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL));
467 EXPECT_TRUE(content::WaitForLoadStop(
468 browser()->tab_strip_model()->GetActiveWebContents()));
469 std::string test_name =
470 base::StringPrintf("%s', '%s", "iframe1", "iframe_direct_download");
471 ClickTestLink(test_name.c_str(), 1);
472 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
473 GURL multi_frame_test_url =
474 embedded_test_server()->GetURL(kMultiFrameTestURL);
475 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL);
476 GURL iframe_retargeting_url =
477 embedded_test_server()->GetURL(kIframeRetargetingURL);
478 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
479 auto nav_map = navigation_map();
480 ASSERT_TRUE(nav_map);
481 ASSERT_EQ(std::size_t(4), nav_map->size());
482 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size());
483 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size());
484 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_retargeting_url).size());
485 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
486 VerifyNavigationEvent(initial_url, multi_frame_test_url, initial_url, true,
487 true, false, multi_frame_test_url,
488 nav_map->at(multi_frame_test_url).at(0));
489 VerifyNavigationEvent(iframe_url, iframe_url, multi_frame_test_url, false,
490 true, false, iframe_url, nav_map->at(iframe_url).at(0));
491 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
492 multi_frame_test_url, false, true, false,
493 iframe_retargeting_url,
494 nav_map->at(iframe_retargeting_url).at(0));
495 VerifyNavigationEvent(iframe_url, download_url, multi_frame_test_url, false,
496 false, false, download_url,
497 nav_map->at(download_url).at(0));
498 VerifyHostToIpMap();
499 }
500
501 // Click a link in a subframe and open download in a new tab.
502 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
503 SubFrameNewTabDownload) {
504 ui_test_utils::NavigateToURL(
505 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL));
506 EXPECT_TRUE(content::WaitForLoadStop(
507 browser()->tab_strip_model()->GetActiveWebContents()));
508 std::string test_name =
509 base::StringPrintf("%s', '%s", "iframe2", "iframe_new_tab_download");
510 ClickTestLink(test_name.c_str(), 4);
511 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
512 GURL multi_frame_test_url =
513 embedded_test_server()->GetURL(kMultiFrameTestURL);
514 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL);
515 GURL iframe_retargeting_url =
516 embedded_test_server()->GetURL(kIframeRetargetingURL);
517 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
518 GURL blank_url = GURL("about:blank");
519 auto nav_map = navigation_map();
520 ASSERT_TRUE(nav_map);
521 ASSERT_EQ(std::size_t(5), nav_map->size());
522 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size());
523 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size());
524 ASSERT_EQ(std::size_t(2), nav_map->at(iframe_retargeting_url).size());
525 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
526 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
527 VerifyNavigationEvent(initial_url, multi_frame_test_url, initial_url, true,
528 true, false, multi_frame_test_url,
529 nav_map->at(multi_frame_test_url).at(0));
530 VerifyNavigationEvent(iframe_url, iframe_url, multi_frame_test_url, false,
531 true, false, iframe_url, nav_map->at(iframe_url).at(0));
532 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
533 multi_frame_test_url, false, true, false,
534 iframe_retargeting_url,
535 nav_map->at(iframe_retargeting_url).at(0));
536 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
537 multi_frame_test_url, false, true, false,
538 iframe_retargeting_url,
539 nav_map->at(iframe_retargeting_url).at(1));
540 VerifyNavigationEvent(iframe_retargeting_url, blank_url, multi_frame_test_url,
541 false, false, false, blank_url,
542 nav_map->at(blank_url).at(0));
543 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
544 blank_url, nav_map->at(blank_url).at(1));
545 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
546 download_url, nav_map->at(download_url).at(0));
547 VerifyHostToIpMap();
548 }
549
550 // host_to_ip_map_ size should increase by one after a new navigation.
551 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, AddIPMapping) {
552 auto ip_map = host_to_ip_map();
553 std::string test_server_host(embedded_test_server()->base_url().host());
554 ip_map->insert(
555 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>()));
556 ASSERT_EQ(std::size_t(0), ip_map->at(test_server_host).size());
557 ClickTestLink("direct_download", 1);
558 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
559 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
560 ip_map->at(test_server_host).back().ip);
561 }
562 // If we have already seem an IP associated with a host, update its timestamp.
Nathan Parker 2016/10/19 22:56:14 Nit: space between tests s/seem/seen/
Jialiu Lin 2016/10/21 02:38:39 Done.
563 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, IPListDedup) {
564 auto ip_map = host_to_ip_map();
565 std::string test_server_host(embedded_test_server()->base_url().host());
566 ip_map->insert(
567 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>()));
568 base::Time yesterday(base::Time::Now() - base::TimeDelta::FromDays(1));
569 ip_map->at(test_server_host)
570 .push_back(ResolvedIPAddress(
571 yesterday, embedded_test_server()->host_port_pair().host()));
572 ASSERT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
573 ClickTestLink("direct_download", 1);
574 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
575 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
576 ip_map->at(test_server_host).back().ip);
577 EXPECT_NE(yesterday, ip_map->at(test_server_host).front().timestamp);
578 }
579
580 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698