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

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

Issue 2538483002: Add management related code to SafeBrowsingNavigationObserverManager (Closed)
Patch Set: address nits Created 4 years 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
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
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
75 void CleanUpNavigationEvents() {
76 navigation_observer_manager_->CleanUpNavigationEvents();
77 }
78
79 void CleanUpIpAddresses() {
80 navigation_observer_manager_->CleanUpIpAddresses();
81 }
82
83 void CleanUpUserGestures() {
84 navigation_observer_manager_->CleanUpUserGestures();
85 }
86
59 protected: 87 protected:
60 SafeBrowsingNavigationObserverManager* navigation_observer_manager_; 88 SafeBrowsingNavigationObserverManager* navigation_observer_manager_;
61 SafeBrowsingNavigationObserver* navigation_observer_; 89 SafeBrowsingNavigationObserver* navigation_observer_;
62 90
63 private: 91 private:
64 DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest); 92 DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest);
65 }; 93 };
66 94
67 TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { 95 TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) {
68 // Navigation in current tab. 96 // Navigation in current tab.
69 content::NavigationController* controller = 97 content::NavigationController* controller =
70 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); 98 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
71 browser()->OpenURL( 99 browser()->OpenURL(
72 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), 100 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(),
73 WindowOpenDisposition::CURRENT_TAB, 101 WindowOpenDisposition::CURRENT_TAB,
74 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); 102 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
75 CommitPendingLoad(controller); 103 CommitPendingLoad(controller);
76 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); 104 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents());
77 auto nav_map = navigation_map(); 105 auto nav_map = navigation_map();
78 ASSERT_EQ(std::size_t(1), nav_map->size()); 106 ASSERT_EQ(std::size_t(1), nav_map->size());
79 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); 107 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size());
80 VerifyNavigationEvent(GURL("http://foo/0"), // source_url 108 VerifyNavigationEvent(GURL(), // source_url
81 GURL("http://foo/0"), // source_main_frame_url 109 GURL(), // source_main_frame_url
82 GURL("http://foo/1"), // original_request_url 110 GURL("http://foo/1"), // original_request_url
83 GURL("http://foo/1"), // destination_url 111 GURL("http://foo/1"), // destination_url
84 tab_id, // source_tab_id 112 tab_id, // source_tab_id
85 tab_id, // target_tab_id 113 tab_id, // target_tab_id
86 true, // is_user_initiated 114 true, // is_user_initiated
87 true, // has_committed 115 true, // has_committed
88 false, // has_server_redirect 116 false, // has_server_redirect
89 nav_map->at(GURL("http://foo/1")).at(0)); 117 nav_map->at(GURL("http://foo/1")).at(0));
90 } 118 }
91 119
(...skipping 15 matching lines...) Expand all
107 GURL("http://foo/3"), // original_request_url 135 GURL("http://foo/3"), // original_request_url
108 GURL("http://redirect/1"), // destination_url 136 GURL("http://redirect/1"), // destination_url
109 tab_id, // source_tab_id 137 tab_id, // source_tab_id
110 tab_id, // target_tab_id 138 tab_id, // target_tab_id
111 false, // is_user_initiated 139 false, // is_user_initiated
112 true, // has_committed 140 true, // has_committed
113 true, // has_server_redirect 141 true, // has_server_redirect
114 nav_map->at(GURL("http://redirect/1")).at(0)); 142 nav_map->at(GURL("http://redirect/1")).at(0));
115 } 143 }
116 144
145 TEST_F(SBNavigationObserverTest, TestCleanUpStaleNavigationEvents) {
146 // Sets up navigation_map() such that it includes fresh, stale and invalid
147 // navigation events.
148 base::Time now = base::Time::Now(); // Fresh
149 base::Time one_hour_ago =
150 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale
151 base::Time one_minute_ago =
152 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Fresh
153 base::Time in_an_hour =
154 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid
155 GURL url_0("http://foo/0");
156 GURL url_1("http://foo/1");
157 navigation_map()->insert(
158 std::make_pair(url_0, std::vector<NavigationEvent>()));
159 navigation_map()->at(url_0).push_back(
160 CreateNavigationEvent(url_0, one_hour_ago));
161 navigation_map()->at(url_0).push_back(CreateNavigationEvent(url_0, now));
162 navigation_map()->at(url_0).push_back(
163 CreateNavigationEvent(url_0, one_minute_ago));
164 navigation_map()->at(url_0).push_back(
165 CreateNavigationEvent(url_0, in_an_hour));
166 navigation_map()->insert(
167 std::make_pair(url_1, std::vector<NavigationEvent>()));
168 navigation_map()->at(url_1).push_back(
169 CreateNavigationEvent(url_0, one_hour_ago));
170 navigation_map()->at(url_1).push_back(
171 CreateNavigationEvent(url_0, one_hour_ago));
172 ASSERT_EQ(std::size_t(2), navigation_map()->size());
173 ASSERT_EQ(std::size_t(4), navigation_map()->at(url_0).size());
174 ASSERT_EQ(std::size_t(2), navigation_map()->at(url_1).size());
175
176 // Cleans up navigation events.
177 CleanUpNavigationEvents();
178
179 // Verifies all stale and invalid navigation events are removed.
180 ASSERT_EQ(std::size_t(1), navigation_map()->size());
181 EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1));
182 EXPECT_EQ(std::size_t(2), navigation_map()->at(url_0).size());
183 }
184
185 TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) {
186 // Sets up user_gesture_map() such that it includes fresh, stale and invalid
187 // user gestures.
188 base::Time now = base::Time::Now(); // Fresh
189 base::Time one_minute_ago =
190 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale
191 base::Time in_an_hour =
192 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid
193 AddTab(browser(), GURL("http://foo/1"));
194 AddTab(browser(), GURL("http://foo/2"));
195 content::WebContents* content0 =
196 browser()->tab_strip_model()->GetWebContentsAt(0);
197 content::WebContents* content1 =
198 browser()->tab_strip_model()->GetWebContentsAt(1);
199 content::WebContents* content2 =
200 browser()->tab_strip_model()->GetWebContentsAt(2);
201 user_gesture_map()->insert(std::make_pair(content0, now));
202 user_gesture_map()->insert(std::make_pair(content1, one_minute_ago));
203 user_gesture_map()->insert(std::make_pair(content2, in_an_hour));
204 ASSERT_EQ(std::size_t(3), user_gesture_map()->size());
205
206 // Cleans up user_gesture_map()
207 CleanUpUserGestures();
208
209 // Verifies all stale and invalid user gestures are removed.
210 ASSERT_EQ(std::size_t(1), user_gesture_map()->size());
211 EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0));
212 EXPECT_EQ(now, user_gesture_map()->at(content0));
213 }
214
215 TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) {
216 // Sets up host_to_ip_map() such that it includes fresh, stale and invalid
217 // user gestures.
218 base::Time now = base::Time::Now(); // Fresh
219 base::Time one_hour_ago =
220 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale
221 base::Time in_an_hour =
222 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid
223 std::string host_0 = GURL("http://foo/0").host();
224 std::string host_1 = GURL("http://bar/1").host();
225 host_to_ip_map()->insert(
226 std::make_pair(host_0, std::vector<ResolvedIPAddress>()));
227 host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1"));
228 host_to_ip_map()->at(host_0).push_back(
229 ResolvedIPAddress(one_hour_ago, "2.2.2.2"));
230 host_to_ip_map()->insert(
231 std::make_pair(host_1, std::vector<ResolvedIPAddress>()));
232 host_to_ip_map()->at(host_1).push_back(
233 ResolvedIPAddress(in_an_hour, "3.3.3.3"));
234 ASSERT_EQ(std::size_t(2), host_to_ip_map()->size());
235
236 // Cleans up host_to_ip_map()
237 CleanUpIpAddresses();
238
239 // Verifies all stale and invalid IP addresses are removed.
240 ASSERT_EQ(std::size_t(1), host_to_ip_map()->size());
241 EXPECT_EQ(host_to_ip_map()->end(), host_to_ip_map()->find(host_1));
242 ASSERT_EQ(std::size_t(1), host_to_ip_map()->at(host_0).size());
243 EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp);
244 }
245
117 } // namespace safe_browsing 246 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager.cc ('k') | chrome/common/safe_browsing/csd.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698