| 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 25 matching lines...) Expand all Loading... |
| 36 int expected_target_tab, | 36 int expected_target_tab, |
| 37 bool expected_is_user_initiated, | 37 bool expected_is_user_initiated, |
| 38 bool expected_has_committed, | 38 bool expected_has_committed, |
| 39 bool expected_has_server_redirect, | 39 bool expected_has_server_redirect, |
| 40 const NavigationEvent& actual_nav_event) { | 40 const NavigationEvent& actual_nav_event) { |
| 41 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | 41 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); |
| 42 EXPECT_EQ(expected_source_main_frame_url, | 42 EXPECT_EQ(expected_source_main_frame_url, |
| 43 actual_nav_event.source_main_frame_url); | 43 actual_nav_event.source_main_frame_url); |
| 44 EXPECT_EQ(expected_original_request_url, | 44 EXPECT_EQ(expected_original_request_url, |
| 45 actual_nav_event.original_request_url); | 45 actual_nav_event.original_request_url); |
| 46 EXPECT_EQ(expected_destination_url, actual_nav_event.destination_url); | 46 EXPECT_EQ(expected_destination_url, actual_nav_event.GetDestinationUrl()); |
| 47 EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); | 47 EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); |
| 48 EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); | 48 EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); |
| 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() { | 59 SafeBrowsingNavigationObserverManager::UserGestureMap* user_gesture_map() { |
| 60 return &navigation_observer_manager_->user_gesture_map_; | 60 return &navigation_observer_manager_->user_gesture_map_; |
| 61 } | 61 } |
| 62 | 62 |
| 63 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { | 63 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { |
| 64 return &navigation_observer_manager_->host_to_ip_map_; | 64 return &navigation_observer_manager_->host_to_ip_map_; |
| 65 } | 65 } |
| 66 | 66 |
| 67 void RecordHostToIpMapping(const std::string& host, |
| 68 const std::string& ip) { |
| 69 navigation_observer_manager_->RecordHostToIpMapping(host, ip); |
| 70 } |
| 71 |
| 67 NavigationEvent CreateNavigationEvent(const GURL& destination_url, | 72 NavigationEvent CreateNavigationEvent(const GURL& destination_url, |
| 68 const base::Time& timestamp) { | 73 const base::Time& timestamp) { |
| 69 NavigationEvent nav_event; | 74 NavigationEvent nav_event; |
| 70 nav_event.destination_url = destination_url; | 75 nav_event.original_request_url = destination_url; |
| 71 nav_event.last_updated = timestamp; | 76 nav_event.last_updated = timestamp; |
| 72 return nav_event; | 77 return nav_event; |
| 73 } | 78 } |
| 74 | 79 |
| 75 void CleanUpNavigationEvents() { | 80 void CleanUpNavigationEvents() { |
| 76 navigation_observer_manager_->CleanUpNavigationEvents(); | 81 navigation_observer_manager_->CleanUpNavigationEvents(); |
| 77 } | 82 } |
| 78 | 83 |
| 79 void CleanUpIpAddresses() { | 84 void CleanUpIpAddresses() { |
| 80 navigation_observer_manager_->CleanUpIpAddresses(); | 85 navigation_observer_manager_->CleanUpIpAddresses(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 96 // Navigation in current tab. | 101 // Navigation in current tab. |
| 97 content::NavigationController* controller = | 102 content::NavigationController* controller = |
| 98 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 103 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
| 99 browser()->OpenURL( | 104 browser()->OpenURL( |
| 100 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), | 105 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), |
| 101 WindowOpenDisposition::CURRENT_TAB, | 106 WindowOpenDisposition::CURRENT_TAB, |
| 102 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 107 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); |
| 103 CommitPendingLoad(controller); | 108 CommitPendingLoad(controller); |
| 104 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); | 109 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); |
| 105 auto nav_map = navigation_map(); | 110 auto nav_map = navigation_map(); |
| 106 ASSERT_EQ(std::size_t(1), nav_map->size()); | 111 ASSERT_EQ(1U, nav_map->size()); |
| 107 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); | 112 ASSERT_EQ(1U, nav_map->at(GURL("http://foo/1")).size()); |
| 108 VerifyNavigationEvent(GURL(), // source_url | 113 VerifyNavigationEvent(GURL(), // source_url |
| 109 GURL(), // source_main_frame_url | 114 GURL(), // source_main_frame_url |
| 110 GURL("http://foo/1"), // original_request_url | 115 GURL("http://foo/1"), // original_request_url |
| 111 GURL("http://foo/1"), // destination_url | 116 GURL("http://foo/1"), // destination_url |
| 112 tab_id, // source_tab_id | 117 tab_id, // source_tab_id |
| 113 tab_id, // target_tab_id | 118 tab_id, // target_tab_id |
| 114 true, // is_user_initiated | 119 true, // is_user_initiated |
| 115 true, // has_committed | 120 true, // has_committed |
| 116 false, // has_server_redirect | 121 false, // has_server_redirect |
| 117 nav_map->at(GURL("http://foo/1")).at(0)); | 122 nav_map->at(GURL("http://foo/1")).at(0)); |
| 118 } | 123 } |
| 119 | 124 |
| 120 TEST_F(SBNavigationObserverTest, ServerRedirect) { | 125 TEST_F(SBNavigationObserverTest, ServerRedirect) { |
| 121 content::RenderFrameHostTester* rfh_tester = | 126 content::RenderFrameHostTester* rfh_tester = |
| 122 content::RenderFrameHostTester::For( | 127 content::RenderFrameHostTester::For( |
| 123 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); | 128 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
| 124 rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); | 129 rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); |
| 125 GURL redirect("http://redirect/1"); | 130 GURL redirect("http://redirect/1"); |
| 126 rfh_tester->SimulateRedirect(redirect); | 131 rfh_tester->SimulateRedirect(redirect); |
| 127 rfh_tester->SimulateNavigationCommit(redirect); | 132 rfh_tester->SimulateNavigationCommit(redirect); |
| 128 int tab_id = SessionTabHelper::IdForTab( | 133 int tab_id = SessionTabHelper::IdForTab( |
| 129 browser()->tab_strip_model()->GetWebContentsAt(0)); | 134 browser()->tab_strip_model()->GetWebContentsAt(0)); |
| 130 auto nav_map = navigation_map(); | 135 auto nav_map = navigation_map(); |
| 131 ASSERT_EQ(std::size_t(1), nav_map->size()); | 136 ASSERT_EQ(1U, nav_map->size()); |
| 132 ASSERT_EQ(std::size_t(1), nav_map->at(redirect).size()); | 137 ASSERT_EQ(1U, nav_map->at(redirect).size()); |
| 133 VerifyNavigationEvent(GURL("http://foo/0"), // source_url | 138 VerifyNavigationEvent(GURL("http://foo/0"), // source_url |
| 134 GURL("http://foo/0"), // source_main_frame_url | 139 GURL("http://foo/0"), // source_main_frame_url |
| 135 GURL("http://foo/3"), // original_request_url | 140 GURL("http://foo/3"), // original_request_url |
| 136 GURL("http://redirect/1"), // destination_url | 141 GURL("http://redirect/1"), // destination_url |
| 137 tab_id, // source_tab_id | 142 tab_id, // source_tab_id |
| 138 tab_id, // target_tab_id | 143 tab_id, // target_tab_id |
| 139 false, // is_user_initiated | 144 false, // is_user_initiated |
| 140 true, // has_committed | 145 true, // has_committed |
| 141 true, // has_server_redirect | 146 true, // has_server_redirect |
| 142 nav_map->at(GURL("http://redirect/1")).at(0)); | 147 nav_map->at(GURL("http://redirect/1")).at(0)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 162 navigation_map()->at(url_0).push_back( | 167 navigation_map()->at(url_0).push_back( |
| 163 CreateNavigationEvent(url_0, one_minute_ago)); | 168 CreateNavigationEvent(url_0, one_minute_ago)); |
| 164 navigation_map()->at(url_0).push_back( | 169 navigation_map()->at(url_0).push_back( |
| 165 CreateNavigationEvent(url_0, in_an_hour)); | 170 CreateNavigationEvent(url_0, in_an_hour)); |
| 166 navigation_map()->insert( | 171 navigation_map()->insert( |
| 167 std::make_pair(url_1, std::vector<NavigationEvent>())); | 172 std::make_pair(url_1, std::vector<NavigationEvent>())); |
| 168 navigation_map()->at(url_1).push_back( | 173 navigation_map()->at(url_1).push_back( |
| 169 CreateNavigationEvent(url_0, one_hour_ago)); | 174 CreateNavigationEvent(url_0, one_hour_ago)); |
| 170 navigation_map()->at(url_1).push_back( | 175 navigation_map()->at(url_1).push_back( |
| 171 CreateNavigationEvent(url_0, one_hour_ago)); | 176 CreateNavigationEvent(url_0, one_hour_ago)); |
| 172 ASSERT_EQ(std::size_t(2), navigation_map()->size()); | 177 ASSERT_EQ(2U, navigation_map()->size()); |
| 173 ASSERT_EQ(std::size_t(4), navigation_map()->at(url_0).size()); | 178 ASSERT_EQ(4U, navigation_map()->at(url_0).size()); |
| 174 ASSERT_EQ(std::size_t(2), navigation_map()->at(url_1).size()); | 179 ASSERT_EQ(2U, navigation_map()->at(url_1).size()); |
| 175 | 180 |
| 176 // Cleans up navigation events. | 181 // Cleans up navigation events. |
| 177 CleanUpNavigationEvents(); | 182 CleanUpNavigationEvents(); |
| 178 | 183 |
| 179 // Verifies all stale and invalid navigation events are removed. | 184 // Verifies all stale and invalid navigation events are removed. |
| 180 ASSERT_EQ(std::size_t(1), navigation_map()->size()); | 185 ASSERT_EQ(1U, navigation_map()->size()); |
| 181 EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1)); | 186 EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1)); |
| 182 EXPECT_EQ(std::size_t(2), navigation_map()->at(url_0).size()); | 187 EXPECT_EQ(2U, navigation_map()->at(url_0).size()); |
| 183 } | 188 } |
| 184 | 189 |
| 185 TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) { | 190 TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) { |
| 186 // Sets up user_gesture_map() such that it includes fresh, stale and invalid | 191 // Sets up user_gesture_map() such that it includes fresh, stale and invalid |
| 187 // user gestures. | 192 // user gestures. |
| 188 base::Time now = base::Time::Now(); // Fresh | 193 base::Time now = base::Time::Now(); // Fresh |
| 189 base::Time one_minute_ago = | 194 base::Time one_minute_ago = |
| 190 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale | 195 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale |
| 191 base::Time in_an_hour = | 196 base::Time in_an_hour = |
| 192 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid | 197 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid |
| 193 AddTab(browser(), GURL("http://foo/1")); | 198 AddTab(browser(), GURL("http://foo/1")); |
| 194 AddTab(browser(), GURL("http://foo/2")); | 199 AddTab(browser(), GURL("http://foo/2")); |
| 195 content::WebContents* content0 = | 200 content::WebContents* content0 = |
| 196 browser()->tab_strip_model()->GetWebContentsAt(0); | 201 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 197 content::WebContents* content1 = | 202 content::WebContents* content1 = |
| 198 browser()->tab_strip_model()->GetWebContentsAt(1); | 203 browser()->tab_strip_model()->GetWebContentsAt(1); |
| 199 content::WebContents* content2 = | 204 content::WebContents* content2 = |
| 200 browser()->tab_strip_model()->GetWebContentsAt(2); | 205 browser()->tab_strip_model()->GetWebContentsAt(2); |
| 201 user_gesture_map()->insert(std::make_pair(content0, now)); | 206 user_gesture_map()->insert(std::make_pair(content0, now)); |
| 202 user_gesture_map()->insert(std::make_pair(content1, one_minute_ago)); | 207 user_gesture_map()->insert(std::make_pair(content1, one_minute_ago)); |
| 203 user_gesture_map()->insert(std::make_pair(content2, in_an_hour)); | 208 user_gesture_map()->insert(std::make_pair(content2, in_an_hour)); |
| 204 ASSERT_EQ(std::size_t(3), user_gesture_map()->size()); | 209 ASSERT_EQ(3U, user_gesture_map()->size()); |
| 205 | 210 |
| 206 // Cleans up user_gesture_map() | 211 // Cleans up user_gesture_map() |
| 207 CleanUpUserGestures(); | 212 CleanUpUserGestures(); |
| 208 | 213 |
| 209 // Verifies all stale and invalid user gestures are removed. | 214 // Verifies all stale and invalid user gestures are removed. |
| 210 ASSERT_EQ(std::size_t(1), user_gesture_map()->size()); | 215 ASSERT_EQ(1U, user_gesture_map()->size()); |
| 211 EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0)); | 216 EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0)); |
| 212 EXPECT_EQ(now, user_gesture_map()->at(content0)); | 217 EXPECT_EQ(now, user_gesture_map()->at(content0)); |
| 213 } | 218 } |
| 214 | 219 |
| 215 TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) { | 220 TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) { |
| 216 // Sets up host_to_ip_map() such that it includes fresh, stale and invalid | 221 // Sets up host_to_ip_map() such that it includes fresh, stale and invalid |
| 217 // user gestures. | 222 // user gestures. |
| 218 base::Time now = base::Time::Now(); // Fresh | 223 base::Time now = base::Time::Now(); // Fresh |
| 219 base::Time one_hour_ago = | 224 base::Time one_hour_ago = |
| 220 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale | 225 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| 221 base::Time in_an_hour = | 226 base::Time in_an_hour = |
| 222 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid | 227 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid |
| 223 std::string host_0 = GURL("http://foo/0").host(); | 228 std::string host_0 = GURL("http://foo/0").host(); |
| 224 std::string host_1 = GURL("http://bar/1").host(); | 229 std::string host_1 = GURL("http://bar/1").host(); |
| 225 host_to_ip_map()->insert( | 230 host_to_ip_map()->insert( |
| 226 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); | 231 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); |
| 227 host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1")); | 232 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( | 233 host_to_ip_map()->at(host_0).push_back( |
| 229 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); | 234 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); |
| 230 host_to_ip_map()->insert( | 235 host_to_ip_map()->insert( |
| 231 std::make_pair(host_1, std::vector<ResolvedIPAddress>())); | 236 std::make_pair(host_1, std::vector<ResolvedIPAddress>())); |
| 232 host_to_ip_map()->at(host_1).push_back( | 237 host_to_ip_map()->at(host_1).push_back( |
| 233 ResolvedIPAddress(in_an_hour, "3.3.3.3")); | 238 ResolvedIPAddress(in_an_hour, "3.3.3.3")); |
| 234 ASSERT_EQ(std::size_t(2), host_to_ip_map()->size()); | 239 ASSERT_EQ(2U, host_to_ip_map()->size()); |
| 235 | 240 |
| 236 // Cleans up host_to_ip_map() | 241 // Cleans up host_to_ip_map() |
| 237 CleanUpIpAddresses(); | 242 CleanUpIpAddresses(); |
| 238 | 243 |
| 239 // Verifies all stale and invalid IP addresses are removed. | 244 // Verifies all stale and invalid IP addresses are removed. |
| 240 ASSERT_EQ(std::size_t(1), host_to_ip_map()->size()); | 245 ASSERT_EQ(1U, host_to_ip_map()->size()); |
| 241 EXPECT_EQ(host_to_ip_map()->end(), host_to_ip_map()->find(host_1)); | 246 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()); | 247 ASSERT_EQ(1U, host_to_ip_map()->at(host_0).size()); |
| 243 EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp); | 248 EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp); |
| 244 } | 249 } |
| 245 | 250 |
| 251 TEST_F(SBNavigationObserverTest, TestRecordHostToIpMapping) { |
| 252 // Setup host_to_ip_map(). |
| 253 base::Time now = base::Time::Now(); // Fresh |
| 254 base::Time one_hour_ago = |
| 255 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| 256 std::string host_0 = GURL("http://foo/0").host(); |
| 257 host_to_ip_map()->insert( |
| 258 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); |
| 259 host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1")); |
| 260 host_to_ip_map()->at(host_0).push_back( |
| 261 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); |
| 262 |
| 263 // Record a host-IP pair, where host is already in the map, and IP has |
| 264 // never been seen before. |
| 265 RecordHostToIpMapping(host_0, "3.3.3.3"); |
| 266 ASSERT_EQ(1U, host_to_ip_map()->size()); |
| 267 EXPECT_EQ(3U, host_to_ip_map()->at(host_0).size()); |
| 268 EXPECT_EQ("3.3.3.3", host_to_ip_map()->at(host_0).at(2).ip); |
| 269 |
| 270 // Record a host-IP pair which is already in the map. It should simply update |
| 271 // its timestamp. |
| 272 ASSERT_EQ(now, host_to_ip_map()->at(host_0).at(0).timestamp); |
| 273 RecordHostToIpMapping(host_0, "1.1.1.1"); |
| 274 ASSERT_EQ(1U, host_to_ip_map()->size()); |
| 275 EXPECT_EQ(3U, host_to_ip_map()->at(host_0).size()); |
| 276 EXPECT_LT(now, host_to_ip_map()->at(host_0).at(2).timestamp); |
| 277 |
| 278 // Record a host-ip pair, neither of which has been seen before. |
| 279 std::string host_1 = GURL("http://bar/1").host(); |
| 280 RecordHostToIpMapping(host_1, "9.9.9.9"); |
| 281 ASSERT_EQ(2U, host_to_ip_map()->size()); |
| 282 EXPECT_EQ(3U, host_to_ip_map()->at(host_0).size()); |
| 283 EXPECT_EQ(1U, host_to_ip_map()->at(host_1).size()); |
| 284 EXPECT_EQ("9.9.9.9", host_to_ip_map()->at(host_1).at(0).ip); |
| 285 } |
| 286 |
| 246 } // namespace safe_browsing | 287 } // namespace safe_browsing |
| OLD | NEW |