OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" |
6 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" | 6 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" |
7 #include "chrome/browser/sessions/session_tab_helper.h" | 7 #include "chrome/browser/sessions/session_tab_helper.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/test/base/browser_with_test_window_test.h" | 9 #include "chrome/test/base/browser_with_test_window_test.h" |
10 #include "content/public/test/test_renderer_host.h" | 10 #include "content/public/test/test_renderer_host.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | 49 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); |
50 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | 50 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); |
51 EXPECT_EQ(expected_has_server_redirect, | 51 EXPECT_EQ(expected_has_server_redirect, |
52 actual_nav_event.has_server_redirect); | 52 actual_nav_event.has_server_redirect); |
53 } | 53 } |
54 | 54 |
55 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { | 55 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { |
56 return navigation_observer_manager_->navigation_map(); | 56 return navigation_observer_manager_->navigation_map(); |
57 } | 57 } |
58 | 58 |
59 SafeBrowsingNavigationObserverManager::UserGestureMap* user_gesture_map() { | |
60 return &navigation_observer_manager_->user_gesture_map_; | |
61 } | |
62 | |
63 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { | |
64 return &navigation_observer_manager_->host_to_ip_map_; | |
65 } | |
66 | |
67 NavigationEvent CreateNavigationEvent(const GURL& destination_url, | |
68 const base::Time& timestamp) { | |
69 NavigationEvent nav_event; | |
70 nav_event.destination_url = destination_url; | |
71 nav_event.last_updated = timestamp; | |
72 return nav_event; | |
73 } | |
74 | |
59 protected: | 75 protected: |
60 SafeBrowsingNavigationObserverManager* navigation_observer_manager_; | 76 SafeBrowsingNavigationObserverManager* navigation_observer_manager_; |
61 SafeBrowsingNavigationObserver* navigation_observer_; | 77 SafeBrowsingNavigationObserver* navigation_observer_; |
62 | 78 |
63 private: | 79 private: |
64 DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest); | 80 DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest); |
65 }; | 81 }; |
66 | 82 |
67 TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { | 83 TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { |
68 // Navigation in current tab. | 84 // Navigation in current tab. |
69 content::NavigationController* controller = | 85 content::NavigationController* controller = |
70 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 86 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
71 browser()->OpenURL( | 87 browser()->OpenURL( |
72 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), | 88 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), |
73 WindowOpenDisposition::CURRENT_TAB, | 89 WindowOpenDisposition::CURRENT_TAB, |
74 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 90 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); |
75 CommitPendingLoad(controller); | 91 CommitPendingLoad(controller); |
76 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); | 92 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); |
77 auto nav_map = navigation_map(); | 93 auto nav_map = navigation_map(); |
78 ASSERT_EQ(std::size_t(1), nav_map->size()); | 94 ASSERT_EQ(std::size_t(1), nav_map->size()); |
79 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); | 95 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); |
80 VerifyNavigationEvent(GURL("http://foo/0"), // source_url | 96 VerifyNavigationEvent(GURL(), // source_url |
81 GURL("http://foo/0"), // source_main_frame_url | 97 GURL(), // source_main_frame_url |
82 GURL("http://foo/1"), // original_request_url | 98 GURL("http://foo/1"), // original_request_url |
83 GURL("http://foo/1"), // destination_url | 99 GURL("http://foo/1"), // destination_url |
84 tab_id, // source_tab_id | 100 tab_id, // source_tab_id |
85 tab_id, // target_tab_id | 101 tab_id, // target_tab_id |
86 true, // is_user_initiated | 102 true, // is_user_initiated |
87 true, // has_committed | 103 true, // has_committed |
88 false, // has_server_redirect | 104 false, // has_server_redirect |
89 nav_map->at(GURL("http://foo/1")).at(0)); | 105 nav_map->at(GURL("http://foo/1")).at(0)); |
90 } | 106 } |
91 | 107 |
(...skipping 15 matching lines...) Expand all Loading... | |
107 GURL("http://foo/3"), // original_request_url | 123 GURL("http://foo/3"), // original_request_url |
108 GURL("http://redirect/1"), // destination_url | 124 GURL("http://redirect/1"), // destination_url |
109 tab_id, // source_tab_id | 125 tab_id, // source_tab_id |
110 tab_id, // target_tab_id | 126 tab_id, // target_tab_id |
111 false, // is_user_initiated | 127 false, // is_user_initiated |
112 true, // has_committed | 128 true, // has_committed |
113 true, // has_server_redirect | 129 true, // has_server_redirect |
114 nav_map->at(GURL("http://redirect/1")).at(0)); | 130 nav_map->at(GURL("http://redirect/1")).at(0)); |
115 } | 131 } |
116 | 132 |
133 TEST_F(SBNavigationObserverTest, TestCleanUpStaleNavigationEvents) { | |
134 // Set up navigation events | |
135 base::Time now = base::Time::Now(); // Fresh | |
136 base::Time one_hour_ago = | |
137 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale | |
138 base::Time one_minute_ago = | |
139 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Fresh | |
140 base::Time in_an_hour = | |
141 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid | |
142 GURL url_0("http://foo/0"); | |
143 GURL url_1("http://foo/1"); | |
144 navigation_map()->insert( | |
Charlie Reis
2016/12/09 22:00:26
nit: Some blank lines and comments might make thes
Jialiu Lin
2016/12/12 23:43:38
Done.
| |
145 std::make_pair(url_0, std::vector<NavigationEvent>())); | |
146 navigation_map()->at(url_0).push_back( | |
147 CreateNavigationEvent(url_0, one_hour_ago)); | |
148 navigation_map()->at(url_0).push_back(CreateNavigationEvent(url_0, now)); | |
149 navigation_map()->at(url_0).push_back( | |
150 CreateNavigationEvent(url_0, one_minute_ago)); | |
151 navigation_map()->at(url_0).push_back( | |
152 CreateNavigationEvent(url_0, in_an_hour)); | |
153 navigation_map()->insert( | |
154 std::make_pair(url_1, std::vector<NavigationEvent>())); | |
155 navigation_map()->at(url_1).push_back( | |
156 CreateNavigationEvent(url_0, one_hour_ago)); | |
157 navigation_map()->at(url_1).push_back( | |
158 CreateNavigationEvent(url_0, one_hour_ago)); | |
159 ASSERT_EQ(std::size_t(2), navigation_map()->size()); | |
160 ASSERT_EQ(std::size_t(4), navigation_map()->at(url_0).size()); | |
161 ASSERT_EQ(std::size_t(2), navigation_map()->at(url_1).size()); | |
162 navigation_observer_manager_->CleanUpNavigationEvents(); | |
163 ASSERT_EQ(std::size_t(1), navigation_map()->size()); | |
164 EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1)); | |
165 EXPECT_EQ(std::size_t(2), navigation_map()->at(url_0).size()); | |
166 } | |
167 | |
168 TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) { | |
169 base::Time now = base::Time::Now(); // Fresh | |
170 base::Time one_hour_ago = | |
171 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale | |
172 base::Time one_minute_ago = | |
173 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale | |
174 AddTab(browser(), GURL("http://foo/1")); | |
175 AddTab(browser(), GURL("http://foo/2")); | |
176 content::WebContents* content0 = | |
177 browser()->tab_strip_model()->GetWebContentsAt(0); | |
178 content::WebContents* content1 = | |
179 browser()->tab_strip_model()->GetWebContentsAt(1); | |
180 content::WebContents* content2 = | |
181 browser()->tab_strip_model()->GetWebContentsAt(2); | |
182 user_gesture_map()->insert(std::make_pair(content0, now)); | |
183 user_gesture_map()->insert(std::make_pair(content1, one_minute_ago)); | |
184 user_gesture_map()->insert(std::make_pair(content2, one_hour_ago)); | |
185 ASSERT_EQ(std::size_t(3), user_gesture_map()->size()); | |
186 navigation_observer_manager_->CleanUpUserGestures(); | |
187 ASSERT_EQ(std::size_t(1), user_gesture_map()->size()); | |
188 EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0)); | |
189 EXPECT_EQ(now, user_gesture_map()->at(content0)); | |
190 } | |
191 | |
192 TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) { | |
193 base::Time now = base::Time::Now(); // Fresh | |
194 base::Time one_hour_ago = | |
195 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale | |
196 base::Time in_an_hour = | |
197 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid | |
198 std::string host_0 = GURL("http://foo/0").host(); | |
199 std::string host_1 = GURL("http://bar/1").host(); | |
200 host_to_ip_map()->insert( | |
201 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); | |
202 host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1")); | |
203 host_to_ip_map()->at(host_0).push_back( | |
204 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); | |
205 host_to_ip_map()->insert( | |
206 std::make_pair(host_1, std::vector<ResolvedIPAddress>())); | |
207 host_to_ip_map()->at(host_1).push_back( | |
208 ResolvedIPAddress(in_an_hour, "3.3.3.3")); | |
209 ASSERT_EQ(std::size_t(2), host_to_ip_map()->size()); | |
210 navigation_observer_manager_->CleanUpIpAddresses(); | |
211 ASSERT_EQ(std::size_t(1), host_to_ip_map()->size()); | |
212 EXPECT_EQ(host_to_ip_map()->end(), host_to_ip_map()->find(host_1)); | |
213 ASSERT_EQ(std::size_t(1), host_to_ip_map()->at(host_0).size()); | |
214 EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp); | |
215 } | |
216 | |
117 } // namespace safe_browsing | 217 } // namespace safe_browsing |
OLD | NEW |