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