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

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: nit 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,
157 const GURL& expected_target_url,
158 const GURL& expected_main_frame_url,
159 bool expected_is_user_initiated,
160 bool expected_has_committed,
161 bool expected_has_server_redirect,
162 const GURL& expected_redirect_url,
163 const NavigationEvent& actual_nav_event) {
164 EXPECT_EQ(expected_source_url, actual_nav_event.source_url);
165 EXPECT_EQ(expected_target_url, actual_nav_event.target_url);
166 EXPECT_EQ(expected_main_frame_url, actual_nav_event.main_frame_url);
167 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated);
168 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed);
169 EXPECT_EQ(expected_has_server_redirect,
170 actual_nav_event.has_server_redirect);
171 EXPECT_EQ(expected_redirect_url, actual_nav_event.server_redirect_url);
172 }
173
174 void VerifyHostToIpMap() {
175 // Since all testing pages have the same host, there is onlly one entry in
176 // host_to_ip_map_.
177 SafeBrowsingNavigationObserverManager::HostToIpMap* actual_host_ip_map =
178 host_to_ip_map();
179 ASSERT_EQ(std::size_t(1), actual_host_ip_map->size());
180 ASSERT_EQ(std::size_t(1),
181 actual_host_ip_map->at(embedded_test_server()->base_url().host())
182 .size());
183 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
184 actual_host_ip_map->at(embedded_test_server()->base_url().host())
185 .back()
186 .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
215 // actual user gesture, therefore is_user_initiated is false.
216 VerifyNavigationEvent(initial_url, // source_url
217 download_url, // target_url
218 initial_url, // source's main_frame_url
219 false, // is_user_initiated,
220 false, // has_committed
221 false, // has_server_redirect
222 GURL(), // redirect_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, download_url, initial_url, false, false,
239 false, GURL(), nav_map->at(download_url).at(0));
240 VerifyHostToIpMap();
241 }
242
243 // Click on a link with rel="noreferrer" attribute, and start download in a
244 // new tab using target=_blank.
245 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
246 DirectDownloadNoReferrerTargetBlank) {
247 ClickTestLink("direct_download_noreferrer_target_blank", 1);
248 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
249 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
250 auto nav_map = navigation_map();
251 ASSERT_TRUE(nav_map);
252 ASSERT_EQ(std::size_t(1), nav_map->size());
253 ASSERT_EQ(std::size_t(2), nav_map->at(download_url).size());
254 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
255 false, GURL(), nav_map->at(download_url).at(0));
256 VerifyNavigationEvent(download_url, download_url, download_url, false, false,
257 false, GURL(), nav_map->at(download_url).at(1));
258 VerifyHostToIpMap();
259 }
260
261 // Click on a link which navigates to a page then redirect to download using
262 // META HTTP-EQUIV="refresh". All transitions happen in the same tab.
263 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
264 SingleMetaRefreshRedirect) {
265 ClickTestLink("single_meta_refresh_redirect", 2);
266 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
267 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
268 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
269 auto nav_map = navigation_map();
270 ASSERT_TRUE(nav_map);
271 ASSERT_EQ(std::size_t(2), nav_map->size());
272 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size());
273 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
274 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, true,
275 false, GURL(), nav_map->at(redirect_url).at(0));
276 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
277 false, GURL(), nav_map->at(download_url).at(0));
278 VerifyHostToIpMap();
279 }
280
281 // Click on a link which navigates to a page then redirect to download using
282 // META HTTP-EQUIV="refresh". First navigation happens in target blank.
283 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
284 SingleMetaRefreshRedirectTargetBlank) {
285 ClickTestLink("single_meta_refresh_redirect_target_blank", 3);
286 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
287 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
288 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
289 auto nav_map = navigation_map();
290 ASSERT_TRUE(nav_map);
291 ASSERT_EQ(std::size_t(2), nav_map->size());
292 ASSERT_EQ(std::size_t(2), nav_map->at(redirect_url).size());
293 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
294 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, false,
295 false, GURL(), nav_map->at(redirect_url).at(0));
296 VerifyNavigationEvent(redirect_url, redirect_url, redirect_url, false, true,
297 false, GURL(), nav_map->at(redirect_url).at(1));
298 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
299 false, GURL(), nav_map->at(download_url).at(0));
300 VerifyHostToIpMap();
301 }
302
303 // Click on a link which redirects twice before reaching download using
304 // META HTTP-EQUIV="refresh". All transitions happen in the same tab.
305 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
306 MultiMetaRefreshRedirects) {
307 ClickTestLink("multiple_meta_refresh_redirects", 3);
308 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
309 GURL first_redirect_url = embedded_test_server()->GetURL(
310 "/safe_browsing/download_protection/navigation_observer/"
311 "double_redirect.html");
312 GURL second_redirect_url = embedded_test_server()->GetURL(kRedirectURL);
313 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
314 auto nav_map = navigation_map();
315 ASSERT_TRUE(nav_map);
316 ASSERT_EQ(std::size_t(3), nav_map->size());
317 ASSERT_EQ(std::size_t(1), nav_map->at(first_redirect_url).size());
318 ASSERT_EQ(std::size_t(1), nav_map->at(second_redirect_url).size());
319 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
320 VerifyNavigationEvent(initial_url, first_redirect_url, initial_url, false,
321 true, false, GURL(),
322 nav_map->at(first_redirect_url).at(0));
323 VerifyNavigationEvent(first_redirect_url, second_redirect_url,
324 first_redirect_url, false, true, false, GURL(),
325 nav_map->at(second_redirect_url).at(0));
326 VerifyNavigationEvent(second_redirect_url, download_url, second_redirect_url,
327 false, false, false, GURL(),
328 nav_map->at(download_url).at(0));
329 VerifyHostToIpMap();
330 }
331
332 // Click on a link which redirects to download using window.location.href.
333 // All transitions happen in the same tab.
334 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
335 WindowLocationHrefRedirect) {
336 ClickTestLink("window_location_href_redirect", 2);
337 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
338 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
339 auto nav_map = navigation_map();
340 ASSERT_TRUE(nav_map);
341 ASSERT_EQ(std::size_t(2), nav_map->size());
342 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
343 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
344 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
345 false, GURL(), nav_map->at(initial_url).at(0));
346 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
347 false, GURL(), nav_map->at(download_url).at(0));
348 VerifyHostToIpMap();
349 }
350
351 // Click on a link which redirect twice until reaches download using a mixture
352 // of meta refresh and window.location.href. All transitions happen in the same
353 // tab.
354 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, MixRedirects) {
355 ClickTestLink("mix_redirects", 3);
356 GURL redirect_url = embedded_test_server()->GetURL(kRedirectURL);
357 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
358 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
359 auto nav_map = navigation_map();
360 ASSERT_TRUE(nav_map);
361 ASSERT_EQ(std::size_t(3), nav_map->size());
362 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
363 ASSERT_EQ(std::size_t(1), nav_map->at(redirect_url).size());
364 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
365 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
366 false, GURL(), nav_map->at(initial_url).at(0));
367 VerifyNavigationEvent(initial_url, redirect_url, initial_url, false, true,
368 false, GURL(), nav_map->at(redirect_url).at(0));
369 VerifyNavigationEvent(redirect_url, download_url, redirect_url, false, false,
370 false, GURL(), nav_map->at(download_url).at(0));
371 VerifyHostToIpMap();
372 }
373
374 // Use javascript to open download in a new tab.
375 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, NewTabDownload) {
376 ClickTestLink("new_tab_download", 4);
377 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
378 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
379 GURL blank_url = GURL("about:blank");
380 auto nav_map = navigation_map();
381 ASSERT_TRUE(nav_map);
382 ASSERT_EQ(std::size_t(3), nav_map->size());
383 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
384 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
385 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
386 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
387 false, GURL(), nav_map->at(initial_url).at(0));
388 VerifyNavigationEvent(initial_url, blank_url, initial_url, false, false,
389 false, GURL(), nav_map->at(blank_url).at(0));
390 // Source and target are at different tabs.
391 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id ==
392 nav_map->at(blank_url).at(0).target_tab_id);
393 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
394 GURL(), nav_map->at(blank_url).at(1));
395 EXPECT_TRUE(nav_map->at(blank_url).at(1).source_tab_id ==
396 nav_map->at(blank_url).at(1).target_tab_id);
397 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
398 GURL(), nav_map->at(download_url).at(0));
399 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id ==
400 nav_map->at(download_url).at(0).target_tab_id);
401 VerifyHostToIpMap();
402 }
403
404 // Use javascript to open download in a new tab and download has a data url.
405 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
406 NewTabDownloadWithDataURL) {
407 ClickTestLink("new_tab_download_with_data_url", 4);
408 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
409 GURL download_url = GURL(kDownloadDataURL);
410 GURL blank_url = GURL("about:blank");
411 auto nav_map = navigation_map();
412 ASSERT_TRUE(nav_map);
413 ASSERT_EQ(std::size_t(3), nav_map->size());
414 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
415 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
416 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
417 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
418 false, GURL(), nav_map->at(initial_url).at(0));
419 VerifyNavigationEvent(initial_url, blank_url, initial_url, false, false,
420 false, GURL(), nav_map->at(blank_url).at(0));
421 // Source and target are at different tabs.
422 EXPECT_FALSE(nav_map->at(blank_url).at(0).source_tab_id ==
423 nav_map->at(blank_url).at(0).target_tab_id);
424 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
425 GURL(), nav_map->at(blank_url).at(1));
426 EXPECT_TRUE(nav_map->at(blank_url).at(1).source_tab_id ==
427 nav_map->at(blank_url).at(1).target_tab_id);
428 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
429 GURL(), nav_map->at(download_url).at(0));
430 EXPECT_TRUE(nav_map->at(download_url).at(0).source_tab_id ==
431 nav_map->at(download_url).at(0).target_tab_id);
432 // Since data url does does not have IP, host_to_ip_map_ should be empty.
433 EXPECT_EQ(std::size_t(0), host_to_ip_map()->size());
434 }
435
436 // TODO(jialiul): Need to figure out why this test is failing on Windows and
437 // flacky on other platforms.
438 #define MAYBE_DownloadViaHTML5FileApi DISABLED_DownloadViaHTML5FileApi
439 // Download via html5 file API.
440 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
441 MAYBE_DownloadViaHTML5FileApi) {
442 ClickTestLink("html5_file_api", 2);
443 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
444 std::string download_url_str =
445 base::StringPrintf("filesystem:%stemporary/test.exe",
446 embedded_test_server()->base_url().spec().c_str());
447 GURL download_url = GURL(download_url_str);
448 auto nav_map = navigation_map();
449 ASSERT_TRUE(nav_map);
450 ASSERT_EQ(std::size_t(2), nav_map->size());
451 ASSERT_EQ(std::size_t(1), nav_map->at(initial_url).size());
452 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
453 VerifyNavigationEvent(initial_url, initial_url, initial_url, false, true,
454 false, GURL(), nav_map->at(initial_url).at(0));
455 VerifyNavigationEvent(initial_url, download_url, initial_url, false, false,
456 false, GURL(), nav_map->at(download_url).at(0));
457 VerifyHostToIpMap();
458 }
459
460 // Click a link in a subframe and start download.
461 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
462 SubFrameDirectDownload) {
463 ui_test_utils::NavigateToURL(
464 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL));
465 EXPECT_TRUE(content::WaitForLoadStop(
466 browser()->tab_strip_model()->GetActiveWebContents()));
467 std::string test_name =
468 base::StringPrintf("%s', '%s", "iframe1", "iframe_direct_download");
469 ClickTestLink(test_name.c_str(), 1);
470 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
471 GURL multi_frame_test_url =
472 embedded_test_server()->GetURL(kMultiFrameTestURL);
473 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL);
474 GURL iframe_retargeting_url =
475 embedded_test_server()->GetURL(kIframeRetargetingURL);
476 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
477 auto nav_map = navigation_map();
478 ASSERT_TRUE(nav_map);
479 ASSERT_EQ(std::size_t(4), nav_map->size());
480 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size());
481 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size());
482 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_retargeting_url).size());
483 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
484 VerifyNavigationEvent(initial_url, multi_frame_test_url, initial_url, true,
485 true, false, GURL(),
486 nav_map->at(multi_frame_test_url).at(0));
487 VerifyNavigationEvent(iframe_url, iframe_url, multi_frame_test_url, false,
488 true, false, GURL(), nav_map->at(iframe_url).at(0));
489 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
490 multi_frame_test_url, false, true, false, GURL(),
491 nav_map->at(iframe_retargeting_url).at(0));
492 VerifyNavigationEvent(iframe_url, download_url, multi_frame_test_url, false,
493 false, false, GURL(), nav_map->at(download_url).at(0));
494 VerifyHostToIpMap();
495 }
496
497 // Click a link in a subframe and open download in a new tab.
498 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest,
499 SubFrameNewTabDownload) {
500 ui_test_utils::NavigateToURL(
501 browser(), embedded_test_server()->GetURL(kMultiFrameTestURL));
502 EXPECT_TRUE(content::WaitForLoadStop(
503 browser()->tab_strip_model()->GetActiveWebContents()));
504 std::string test_name =
505 base::StringPrintf("%s', '%s", "iframe2", "iframe_new_tab_download");
506 ClickTestLink(test_name.c_str(), 4);
507 GURL initial_url = embedded_test_server()->GetURL(kSingleFrameTestURL);
508 GURL multi_frame_test_url =
509 embedded_test_server()->GetURL(kMultiFrameTestURL);
510 GURL iframe_url = embedded_test_server()->GetURL(kIframeDirectDownloadURL);
511 GURL iframe_retargeting_url =
512 embedded_test_server()->GetURL(kIframeRetargetingURL);
513 GURL download_url = embedded_test_server()->GetURL(kDownloadItemURL);
514 GURL blank_url = GURL("about:blank");
515 auto nav_map = navigation_map();
516 ASSERT_TRUE(nav_map);
517 ASSERT_EQ(std::size_t(5), nav_map->size());
518 ASSERT_EQ(std::size_t(1), nav_map->at(multi_frame_test_url).size());
519 ASSERT_EQ(std::size_t(1), nav_map->at(iframe_url).size());
520 ASSERT_EQ(std::size_t(2), nav_map->at(iframe_retargeting_url).size());
521 ASSERT_EQ(std::size_t(2), nav_map->at(blank_url).size());
522 ASSERT_EQ(std::size_t(1), nav_map->at(download_url).size());
523 VerifyNavigationEvent(initial_url, multi_frame_test_url, initial_url, true,
524 true, false, GURL(),
525 nav_map->at(multi_frame_test_url).at(0));
526 VerifyNavigationEvent(iframe_url, iframe_url, multi_frame_test_url, false,
527 true, false, GURL(), nav_map->at(iframe_url).at(0));
528 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
529 multi_frame_test_url, false, true, false, GURL(),
530 nav_map->at(iframe_retargeting_url).at(0));
531 VerifyNavigationEvent(iframe_retargeting_url, iframe_retargeting_url,
532 multi_frame_test_url, false, true, false, GURL(),
533 nav_map->at(iframe_retargeting_url).at(1));
534 VerifyNavigationEvent(iframe_retargeting_url, blank_url, multi_frame_test_url,
535 false, false, false, GURL(),
536 nav_map->at(blank_url).at(0));
537 VerifyNavigationEvent(blank_url, blank_url, blank_url, false, false, false,
538 GURL(), nav_map->at(blank_url).at(1));
539 VerifyNavigationEvent(blank_url, download_url, blank_url, false, false, false,
540 GURL(), nav_map->at(download_url).at(0));
541 VerifyHostToIpMap();
542 }
543
544 // host_to_ip_map_ size should increase by one after a new navigation.
545 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, AddIPMapping) {
546 auto ip_map = host_to_ip_map();
547 std::string test_server_host(embedded_test_server()->base_url().host());
548 ip_map->insert(
549 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>()));
550 ASSERT_EQ(std::size_t(0), ip_map->at(test_server_host).size());
551 ClickTestLink("direct_download", 1);
552 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
553 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
554 ip_map->at(test_server_host).back().ip);
555 }
556 // If we have already seem an IP associated with a host, update its timestamp.
557 IN_PROC_BROWSER_TEST_F(SBNavigationObserverBrowserTest, IPListDedup) {
558 auto ip_map = host_to_ip_map();
559 std::string test_server_host(embedded_test_server()->base_url().host());
560 ip_map->insert(
561 std::make_pair(test_server_host, std::vector<ResolvedIPAddress>()));
562 base::Time yesterday(base::Time::Now() - base::TimeDelta::FromDays(1));
563 ip_map->at(test_server_host)
564 .push_back(ResolvedIPAddress(
565 yesterday, embedded_test_server()->host_port_pair().host()));
566 ASSERT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
567 ClickTestLink("direct_download", 1);
568 EXPECT_EQ(std::size_t(1), ip_map->at(test_server_host).size());
569 EXPECT_EQ(embedded_test_server()->host_port_pair().host(),
570 ip_map->at(test_server_host).back().ip);
571 EXPECT_NE(yesterday, ip_map->at(test_server_host).front().timestamp);
572 }
573
574 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698