| 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 "base/test/histogram_tester.h" |
| 5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 6 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" |
| 6 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager
.h" | 7 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager
.h" |
| 7 #include "chrome/browser/sessions/session_tab_helper.h" | 8 #include "chrome/browser/sessions/session_tab_helper.h" |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 9 #include "chrome/test/base/browser_with_test_window_test.h" | 10 #include "chrome/test/base/browser_with_test_window_test.h" |
| 10 #include "content/public/test/test_renderer_host.h" | 11 #include "content/public/test/test_renderer_host.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/base/window_open_disposition.h" | 14 #include "ui/base/window_open_disposition.h" |
| 13 | 15 |
| 16 namespace { |
| 17 |
| 18 const char kNavigationEventCleanUpHistogramName[] = |
| 19 "SafeBrowsing.NavigationObserver.NavigationEventCleanUpCount"; |
| 20 const char kIPAddressCleanUpHistogramName[] = |
| 21 "SafeBrowsing.NavigationObserver.IPAddressCleanUpCount"; |
| 22 } |
| 23 |
| 14 namespace safe_browsing { | 24 namespace safe_browsing { |
| 15 | 25 |
| 16 class SBNavigationObserverTest : public BrowserWithTestWindowTest { | 26 class SBNavigationObserverTest : public BrowserWithTestWindowTest { |
| 17 public: | 27 public: |
| 18 SBNavigationObserverTest() {} | 28 SBNavigationObserverTest() {} |
| 19 void SetUp() override { | 29 void SetUp() override { |
| 20 BrowserWithTestWindowTest::SetUp(); | 30 BrowserWithTestWindowTest::SetUp(); |
| 21 AddTab(browser(), GURL("http://foo/0")); | 31 AddTab(browser(), GURL("http://foo/0")); |
| 22 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); | 32 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); |
| 23 navigation_observer_ = new SafeBrowsingNavigationObserver( | 33 navigation_observer_ = new SafeBrowsingNavigationObserver( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 36 int expected_target_tab, | 46 int expected_target_tab, |
| 37 bool expected_is_user_initiated, | 47 bool expected_is_user_initiated, |
| 38 bool expected_has_committed, | 48 bool expected_has_committed, |
| 39 bool expected_has_server_redirect, | 49 bool expected_has_server_redirect, |
| 40 const NavigationEvent& actual_nav_event) { | 50 const NavigationEvent& actual_nav_event) { |
| 41 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | 51 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); |
| 42 EXPECT_EQ(expected_source_main_frame_url, | 52 EXPECT_EQ(expected_source_main_frame_url, |
| 43 actual_nav_event.source_main_frame_url); | 53 actual_nav_event.source_main_frame_url); |
| 44 EXPECT_EQ(expected_original_request_url, | 54 EXPECT_EQ(expected_original_request_url, |
| 45 actual_nav_event.original_request_url); | 55 actual_nav_event.original_request_url); |
| 46 EXPECT_EQ(expected_destination_url, actual_nav_event.destination_url); | 56 EXPECT_EQ(expected_destination_url, actual_nav_event.GetDestinationUrl()); |
| 47 EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); | 57 EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); |
| 48 EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); | 58 EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); |
| 49 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | 59 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); |
| 50 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | 60 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); |
| 51 EXPECT_EQ(expected_has_server_redirect, | 61 EXPECT_EQ(expected_has_server_redirect, |
| 52 actual_nav_event.has_server_redirect); | 62 !actual_nav_event.server_redirect_urls.empty()); |
| 53 } | 63 } |
| 54 | 64 |
| 55 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { | 65 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { |
| 56 return navigation_observer_manager_->navigation_map(); | 66 return navigation_observer_manager_->navigation_map(); |
| 57 } | 67 } |
| 58 | 68 |
| 59 SafeBrowsingNavigationObserverManager::UserGestureMap* user_gesture_map() { | 69 SafeBrowsingNavigationObserverManager::UserGestureMap* user_gesture_map() { |
| 60 return &navigation_observer_manager_->user_gesture_map_; | 70 return &navigation_observer_manager_->user_gesture_map_; |
| 61 } | 71 } |
| 62 | 72 |
| 63 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { | 73 SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { |
| 64 return &navigation_observer_manager_->host_to_ip_map_; | 74 return &navigation_observer_manager_->host_to_ip_map_; |
| 65 } | 75 } |
| 66 | 76 |
| 77 void RecordHostToIpMapping(const std::string& host, const std::string& ip) { |
| 78 navigation_observer_manager_->RecordHostToIpMapping(host, ip); |
| 79 } |
| 80 |
| 67 NavigationEvent CreateNavigationEvent(const GURL& destination_url, | 81 NavigationEvent CreateNavigationEvent(const GURL& destination_url, |
| 68 const base::Time& timestamp) { | 82 const base::Time& timestamp) { |
| 69 NavigationEvent nav_event; | 83 NavigationEvent nav_event; |
| 70 nav_event.destination_url = destination_url; | 84 nav_event.original_request_url = destination_url; |
| 71 nav_event.last_updated = timestamp; | 85 nav_event.last_updated = timestamp; |
| 72 return nav_event; | 86 return nav_event; |
| 73 } | 87 } |
| 74 | 88 |
| 75 void CleanUpNavigationEvents() { | 89 void CleanUpNavigationEvents() { |
| 76 navigation_observer_manager_->CleanUpNavigationEvents(); | 90 navigation_observer_manager_->CleanUpNavigationEvents(); |
| 77 } | 91 } |
| 78 | 92 |
| 79 void CleanUpIpAddresses() { | 93 void CleanUpIpAddresses() { |
| 80 navigation_observer_manager_->CleanUpIpAddresses(); | 94 navigation_observer_manager_->CleanUpIpAddresses(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 96 // Navigation in current tab. | 110 // Navigation in current tab. |
| 97 content::NavigationController* controller = | 111 content::NavigationController* controller = |
| 98 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 112 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
| 99 browser()->OpenURL( | 113 browser()->OpenURL( |
| 100 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), | 114 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), |
| 101 WindowOpenDisposition::CURRENT_TAB, | 115 WindowOpenDisposition::CURRENT_TAB, |
| 102 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 116 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); |
| 103 CommitPendingLoad(controller); | 117 CommitPendingLoad(controller); |
| 104 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); | 118 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); |
| 105 auto nav_map = navigation_map(); | 119 auto nav_map = navigation_map(); |
| 106 ASSERT_EQ(std::size_t(1), nav_map->size()); | 120 ASSERT_EQ(1U, nav_map->size()); |
| 107 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); | 121 ASSERT_EQ(1U, nav_map->at(GURL("http://foo/1")).size()); |
| 108 VerifyNavigationEvent(GURL(), // source_url | 122 VerifyNavigationEvent(GURL(), // source_url |
| 109 GURL(), // source_main_frame_url | 123 GURL(), // source_main_frame_url |
| 110 GURL("http://foo/1"), // original_request_url | 124 GURL("http://foo/1"), // original_request_url |
| 111 GURL("http://foo/1"), // destination_url | 125 GURL("http://foo/1"), // destination_url |
| 112 tab_id, // source_tab_id | 126 tab_id, // source_tab_id |
| 113 tab_id, // target_tab_id | 127 tab_id, // target_tab_id |
| 114 true, // is_user_initiated | 128 true, // is_user_initiated |
| 115 true, // has_committed | 129 true, // has_committed |
| 116 false, // has_server_redirect | 130 false, // has_server_redirect |
| 117 nav_map->at(GURL("http://foo/1")).at(0)); | 131 nav_map->at(GURL("http://foo/1")).at(0)); |
| 118 } | 132 } |
| 119 | 133 |
| 120 TEST_F(SBNavigationObserverTest, ServerRedirect) { | 134 TEST_F(SBNavigationObserverTest, ServerRedirect) { |
| 121 content::RenderFrameHostTester* rfh_tester = | 135 content::RenderFrameHostTester* rfh_tester = |
| 122 content::RenderFrameHostTester::For( | 136 content::RenderFrameHostTester::For( |
| 123 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); | 137 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
| 124 rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); | 138 rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); |
| 125 GURL redirect("http://redirect/1"); | 139 GURL redirect("http://redirect/1"); |
| 126 rfh_tester->SimulateRedirect(redirect); | 140 rfh_tester->SimulateRedirect(redirect); |
| 127 rfh_tester->SimulateNavigationCommit(redirect); | 141 rfh_tester->SimulateNavigationCommit(redirect); |
| 128 int tab_id = SessionTabHelper::IdForTab( | 142 int tab_id = SessionTabHelper::IdForTab( |
| 129 browser()->tab_strip_model()->GetWebContentsAt(0)); | 143 browser()->tab_strip_model()->GetWebContentsAt(0)); |
| 130 auto nav_map = navigation_map(); | 144 auto nav_map = navigation_map(); |
| 131 ASSERT_EQ(std::size_t(1), nav_map->size()); | 145 ASSERT_EQ(1U, nav_map->size()); |
| 132 ASSERT_EQ(std::size_t(1), nav_map->at(redirect).size()); | 146 ASSERT_EQ(1U, nav_map->at(redirect).size()); |
| 133 VerifyNavigationEvent(GURL("http://foo/0"), // source_url | 147 VerifyNavigationEvent(GURL("http://foo/0"), // source_url |
| 134 GURL("http://foo/0"), // source_main_frame_url | 148 GURL("http://foo/0"), // source_main_frame_url |
| 135 GURL("http://foo/3"), // original_request_url | 149 GURL("http://foo/3"), // original_request_url |
| 136 GURL("http://redirect/1"), // destination_url | 150 GURL("http://redirect/1"), // destination_url |
| 137 tab_id, // source_tab_id | 151 tab_id, // source_tab_id |
| 138 tab_id, // target_tab_id | 152 tab_id, // target_tab_id |
| 139 false, // is_user_initiated | 153 false, // is_user_initiated |
| 140 true, // has_committed | 154 true, // has_committed |
| 141 true, // has_server_redirect | 155 true, // has_server_redirect |
| 142 nav_map->at(GURL("http://redirect/1")).at(0)); | 156 nav_map->at(GURL("http://redirect/1")).at(0)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 162 navigation_map()->at(url_0).push_back( | 176 navigation_map()->at(url_0).push_back( |
| 163 CreateNavigationEvent(url_0, one_minute_ago)); | 177 CreateNavigationEvent(url_0, one_minute_ago)); |
| 164 navigation_map()->at(url_0).push_back( | 178 navigation_map()->at(url_0).push_back( |
| 165 CreateNavigationEvent(url_0, in_an_hour)); | 179 CreateNavigationEvent(url_0, in_an_hour)); |
| 166 navigation_map()->insert( | 180 navigation_map()->insert( |
| 167 std::make_pair(url_1, std::vector<NavigationEvent>())); | 181 std::make_pair(url_1, std::vector<NavigationEvent>())); |
| 168 navigation_map()->at(url_1).push_back( | 182 navigation_map()->at(url_1).push_back( |
| 169 CreateNavigationEvent(url_0, one_hour_ago)); | 183 CreateNavigationEvent(url_0, one_hour_ago)); |
| 170 navigation_map()->at(url_1).push_back( | 184 navigation_map()->at(url_1).push_back( |
| 171 CreateNavigationEvent(url_0, one_hour_ago)); | 185 CreateNavigationEvent(url_0, one_hour_ago)); |
| 172 ASSERT_EQ(std::size_t(2), navigation_map()->size()); | 186 ASSERT_EQ(2U, navigation_map()->size()); |
| 173 ASSERT_EQ(std::size_t(4), navigation_map()->at(url_0).size()); | 187 ASSERT_EQ(4U, navigation_map()->at(url_0).size()); |
| 174 ASSERT_EQ(std::size_t(2), navigation_map()->at(url_1).size()); | 188 ASSERT_EQ(2U, navigation_map()->at(url_1).size()); |
| 189 |
| 190 base::HistogramTester histograms; |
| 191 histograms.ExpectTotalCount(kNavigationEventCleanUpHistogramName, 0); |
| 175 | 192 |
| 176 // Cleans up navigation events. | 193 // Cleans up navigation events. |
| 177 CleanUpNavigationEvents(); | 194 CleanUpNavigationEvents(); |
| 178 | 195 |
| 179 // Verifies all stale and invalid navigation events are removed. | 196 // Verifies all stale and invalid navigation events are removed. |
| 180 ASSERT_EQ(std::size_t(1), navigation_map()->size()); | 197 ASSERT_EQ(1U, navigation_map()->size()); |
| 181 EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1)); | 198 EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1)); |
| 182 EXPECT_EQ(std::size_t(2), navigation_map()->at(url_0).size()); | 199 EXPECT_EQ(2U, navigation_map()->at(url_0).size()); |
| 200 EXPECT_THAT(histograms.GetAllSamples(kNavigationEventCleanUpHistogramName), |
| 201 testing::ElementsAre(base::Bucket(4, 1))); |
| 183 } | 202 } |
| 184 | 203 |
| 185 TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) { | 204 TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) { |
| 186 // Sets up user_gesture_map() such that it includes fresh, stale and invalid | 205 // Sets up user_gesture_map() such that it includes fresh, stale and invalid |
| 187 // user gestures. | 206 // user gestures. |
| 188 base::Time now = base::Time::Now(); // Fresh | 207 base::Time now = base::Time::Now(); // Fresh |
| 189 base::Time one_minute_ago = | 208 base::Time one_minute_ago = |
| 190 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale | 209 base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale |
| 191 base::Time in_an_hour = | 210 base::Time in_an_hour = |
| 192 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid | 211 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid |
| 193 AddTab(browser(), GURL("http://foo/1")); | 212 AddTab(browser(), GURL("http://foo/1")); |
| 194 AddTab(browser(), GURL("http://foo/2")); | 213 AddTab(browser(), GURL("http://foo/2")); |
| 195 content::WebContents* content0 = | 214 content::WebContents* content0 = |
| 196 browser()->tab_strip_model()->GetWebContentsAt(0); | 215 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 197 content::WebContents* content1 = | 216 content::WebContents* content1 = |
| 198 browser()->tab_strip_model()->GetWebContentsAt(1); | 217 browser()->tab_strip_model()->GetWebContentsAt(1); |
| 199 content::WebContents* content2 = | 218 content::WebContents* content2 = |
| 200 browser()->tab_strip_model()->GetWebContentsAt(2); | 219 browser()->tab_strip_model()->GetWebContentsAt(2); |
| 201 user_gesture_map()->insert(std::make_pair(content0, now)); | 220 user_gesture_map()->insert(std::make_pair(content0, now)); |
| 202 user_gesture_map()->insert(std::make_pair(content1, one_minute_ago)); | 221 user_gesture_map()->insert(std::make_pair(content1, one_minute_ago)); |
| 203 user_gesture_map()->insert(std::make_pair(content2, in_an_hour)); | 222 user_gesture_map()->insert(std::make_pair(content2, in_an_hour)); |
| 204 ASSERT_EQ(std::size_t(3), user_gesture_map()->size()); | 223 ASSERT_EQ(3U, user_gesture_map()->size()); |
| 205 | 224 |
| 206 // Cleans up user_gesture_map() | 225 // Cleans up user_gesture_map() |
| 207 CleanUpUserGestures(); | 226 CleanUpUserGestures(); |
| 208 | 227 |
| 209 // Verifies all stale and invalid user gestures are removed. | 228 // Verifies all stale and invalid user gestures are removed. |
| 210 ASSERT_EQ(std::size_t(1), user_gesture_map()->size()); | 229 ASSERT_EQ(1U, user_gesture_map()->size()); |
| 211 EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0)); | 230 EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0)); |
| 212 EXPECT_EQ(now, user_gesture_map()->at(content0)); | 231 EXPECT_EQ(now, user_gesture_map()->at(content0)); |
| 213 } | 232 } |
| 214 | 233 |
| 215 TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) { | 234 TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) { |
| 216 // Sets up host_to_ip_map() such that it includes fresh, stale and invalid | 235 // Sets up host_to_ip_map() such that it includes fresh, stale and invalid |
| 217 // user gestures. | 236 // user gestures. |
| 218 base::Time now = base::Time::Now(); // Fresh | 237 base::Time now = base::Time::Now(); // Fresh |
| 219 base::Time one_hour_ago = | 238 base::Time one_hour_ago = |
| 220 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale | 239 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| 221 base::Time in_an_hour = | 240 base::Time in_an_hour = |
| 222 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid | 241 base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid |
| 223 std::string host_0 = GURL("http://foo/0").host(); | 242 std::string host_0 = GURL("http://foo/0").host(); |
| 224 std::string host_1 = GURL("http://bar/1").host(); | 243 std::string host_1 = GURL("http://bar/1").host(); |
| 225 host_to_ip_map()->insert( | 244 host_to_ip_map()->insert( |
| 226 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); | 245 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); |
| 227 host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1")); | 246 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( | 247 host_to_ip_map()->at(host_0).push_back( |
| 229 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); | 248 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); |
| 230 host_to_ip_map()->insert( | 249 host_to_ip_map()->insert( |
| 231 std::make_pair(host_1, std::vector<ResolvedIPAddress>())); | 250 std::make_pair(host_1, std::vector<ResolvedIPAddress>())); |
| 232 host_to_ip_map()->at(host_1).push_back( | 251 host_to_ip_map()->at(host_1).push_back( |
| 233 ResolvedIPAddress(in_an_hour, "3.3.3.3")); | 252 ResolvedIPAddress(in_an_hour, "3.3.3.3")); |
| 234 ASSERT_EQ(std::size_t(2), host_to_ip_map()->size()); | 253 ASSERT_EQ(2U, host_to_ip_map()->size()); |
| 254 |
| 255 base::HistogramTester histograms; |
| 256 histograms.ExpectTotalCount(kIPAddressCleanUpHistogramName, 0); |
| 235 | 257 |
| 236 // Cleans up host_to_ip_map() | 258 // Cleans up host_to_ip_map() |
| 237 CleanUpIpAddresses(); | 259 CleanUpIpAddresses(); |
| 238 | 260 |
| 239 // Verifies all stale and invalid IP addresses are removed. | 261 // Verifies all stale and invalid IP addresses are removed. |
| 240 ASSERT_EQ(std::size_t(1), host_to_ip_map()->size()); | 262 ASSERT_EQ(1U, host_to_ip_map()->size()); |
| 241 EXPECT_EQ(host_to_ip_map()->end(), host_to_ip_map()->find(host_1)); | 263 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()); | 264 ASSERT_EQ(1U, host_to_ip_map()->at(host_0).size()); |
| 243 EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp); | 265 EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp); |
| 266 EXPECT_THAT(histograms.GetAllSamples(kIPAddressCleanUpHistogramName), |
| 267 testing::ElementsAre(base::Bucket(2, 1))); |
| 268 } |
| 269 |
| 270 TEST_F(SBNavigationObserverTest, TestRecordHostToIpMapping) { |
| 271 // Setup host_to_ip_map(). |
| 272 base::Time now = base::Time::Now(); // Fresh |
| 273 base::Time one_hour_ago = |
| 274 base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| 275 std::string host_0 = GURL("http://foo/0").host(); |
| 276 host_to_ip_map()->insert( |
| 277 std::make_pair(host_0, std::vector<ResolvedIPAddress>())); |
| 278 host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1")); |
| 279 host_to_ip_map()->at(host_0).push_back( |
| 280 ResolvedIPAddress(one_hour_ago, "2.2.2.2")); |
| 281 |
| 282 // Record a host-IP pair, where host is already in the map, and IP has |
| 283 // never been seen before. |
| 284 RecordHostToIpMapping(host_0, "3.3.3.3"); |
| 285 ASSERT_EQ(1U, host_to_ip_map()->size()); |
| 286 EXPECT_EQ(3U, host_to_ip_map()->at(host_0).size()); |
| 287 EXPECT_EQ("3.3.3.3", host_to_ip_map()->at(host_0).at(2).ip); |
| 288 |
| 289 // Record a host-IP pair which is already in the map. It should simply update |
| 290 // its timestamp. |
| 291 ASSERT_EQ(now, host_to_ip_map()->at(host_0).at(0).timestamp); |
| 292 RecordHostToIpMapping(host_0, "1.1.1.1"); |
| 293 ASSERT_EQ(1U, host_to_ip_map()->size()); |
| 294 EXPECT_EQ(3U, host_to_ip_map()->at(host_0).size()); |
| 295 EXPECT_LT(now, host_to_ip_map()->at(host_0).at(2).timestamp); |
| 296 |
| 297 // Record a host-ip pair, neither of which has been seen before. |
| 298 std::string host_1 = GURL("http://bar/1").host(); |
| 299 RecordHostToIpMapping(host_1, "9.9.9.9"); |
| 300 ASSERT_EQ(2U, host_to_ip_map()->size()); |
| 301 EXPECT_EQ(3U, host_to_ip_map()->at(host_0).size()); |
| 302 EXPECT_EQ(1U, host_to_ip_map()->at(host_1).size()); |
| 303 EXPECT_EQ("9.9.9.9", host_to_ip_map()->at(host_1).at(0).ip); |
| 244 } | 304 } |
| 245 | 305 |
| 246 } // namespace safe_browsing | 306 } // namespace safe_browsing |
| OLD | NEW |