Chromium Code Reviews| Index: chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc |
| diff --git a/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc b/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc |
| index 3c4fde5b4fc065197056484e5306ca12055c8b50..f23d518aad5525cdb7b0e62fe0eff241c0e63a74 100644 |
| --- a/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc |
| +++ b/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc |
| @@ -56,6 +56,22 @@ class SBNavigationObserverTest : public BrowserWithTestWindowTest { |
| return navigation_observer_manager_->navigation_map(); |
| } |
| + SafeBrowsingNavigationObserverManager::UserGestureMap* user_gesture_map() { |
| + return &navigation_observer_manager_->user_gesture_map_; |
| + } |
| + |
| + SafeBrowsingNavigationObserverManager::HostToIpMap* host_to_ip_map() { |
| + return &navigation_observer_manager_->host_to_ip_map_; |
| + } |
| + |
| + NavigationEvent CreateNavigationEvent(const GURL& destination_url, |
| + const base::Time& timestamp) { |
| + NavigationEvent nav_event; |
| + nav_event.destination_url = destination_url; |
| + nav_event.last_updated = timestamp; |
| + return nav_event; |
| + } |
| + |
| protected: |
| SafeBrowsingNavigationObserverManager* navigation_observer_manager_; |
| SafeBrowsingNavigationObserver* navigation_observer_; |
| @@ -77,8 +93,8 @@ TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { |
| auto nav_map = navigation_map(); |
| ASSERT_EQ(std::size_t(1), nav_map->size()); |
| ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); |
| - VerifyNavigationEvent(GURL("http://foo/0"), // source_url |
| - GURL("http://foo/0"), // source_main_frame_url |
| + VerifyNavigationEvent(GURL(), // source_url |
| + GURL(), // source_main_frame_url |
| GURL("http://foo/1"), // original_request_url |
| GURL("http://foo/1"), // destination_url |
| tab_id, // source_tab_id |
| @@ -114,4 +130,88 @@ TEST_F(SBNavigationObserverTest, ServerRedirect) { |
| nav_map->at(GURL("http://redirect/1")).at(0)); |
| } |
| +TEST_F(SBNavigationObserverTest, TestCleanUpStaleNavigationEvents) { |
| + // Set up navigation events |
| + base::Time now = base::Time::Now(); // Fresh |
| + base::Time one_hour_ago = |
| + base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| + base::Time one_minute_ago = |
| + base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Fresh |
| + base::Time in_an_hour = |
| + base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid |
| + GURL url_0("http://foo/0"); |
| + GURL url_1("http://foo/1"); |
| + 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.
|
| + std::make_pair(url_0, std::vector<NavigationEvent>())); |
| + navigation_map()->at(url_0).push_back( |
| + CreateNavigationEvent(url_0, one_hour_ago)); |
| + navigation_map()->at(url_0).push_back(CreateNavigationEvent(url_0, now)); |
| + navigation_map()->at(url_0).push_back( |
| + CreateNavigationEvent(url_0, one_minute_ago)); |
| + navigation_map()->at(url_0).push_back( |
| + CreateNavigationEvent(url_0, in_an_hour)); |
| + navigation_map()->insert( |
| + std::make_pair(url_1, std::vector<NavigationEvent>())); |
| + navigation_map()->at(url_1).push_back( |
| + CreateNavigationEvent(url_0, one_hour_ago)); |
| + navigation_map()->at(url_1).push_back( |
| + CreateNavigationEvent(url_0, one_hour_ago)); |
| + ASSERT_EQ(std::size_t(2), navigation_map()->size()); |
| + ASSERT_EQ(std::size_t(4), navigation_map()->at(url_0).size()); |
| + ASSERT_EQ(std::size_t(2), navigation_map()->at(url_1).size()); |
| + navigation_observer_manager_->CleanUpNavigationEvents(); |
| + ASSERT_EQ(std::size_t(1), navigation_map()->size()); |
| + EXPECT_EQ(navigation_map()->end(), navigation_map()->find(url_1)); |
| + EXPECT_EQ(std::size_t(2), navigation_map()->at(url_0).size()); |
| +} |
| + |
| +TEST_F(SBNavigationObserverTest, TestCleanUpStaleUserGestures) { |
| + base::Time now = base::Time::Now(); // Fresh |
| + base::Time one_hour_ago = |
| + base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| + base::Time one_minute_ago = |
| + base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale |
| + AddTab(browser(), GURL("http://foo/1")); |
| + AddTab(browser(), GURL("http://foo/2")); |
| + content::WebContents* content0 = |
| + browser()->tab_strip_model()->GetWebContentsAt(0); |
| + content::WebContents* content1 = |
| + browser()->tab_strip_model()->GetWebContentsAt(1); |
| + content::WebContents* content2 = |
| + browser()->tab_strip_model()->GetWebContentsAt(2); |
| + user_gesture_map()->insert(std::make_pair(content0, now)); |
| + user_gesture_map()->insert(std::make_pair(content1, one_minute_ago)); |
| + user_gesture_map()->insert(std::make_pair(content2, one_hour_ago)); |
| + ASSERT_EQ(std::size_t(3), user_gesture_map()->size()); |
| + navigation_observer_manager_->CleanUpUserGestures(); |
| + ASSERT_EQ(std::size_t(1), user_gesture_map()->size()); |
| + EXPECT_NE(user_gesture_map()->end(), user_gesture_map()->find(content0)); |
| + EXPECT_EQ(now, user_gesture_map()->at(content0)); |
| +} |
| + |
| +TEST_F(SBNavigationObserverTest, TestCleanUpStaleIPAddresses) { |
| + base::Time now = base::Time::Now(); // Fresh |
| + base::Time one_hour_ago = |
| + base::Time::FromDoubleT(now.ToDoubleT() - 60.0 * 60.0); // Stale |
| + base::Time in_an_hour = |
| + base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid |
| + std::string host_0 = GURL("http://foo/0").host(); |
| + std::string host_1 = GURL("http://bar/1").host(); |
| + host_to_ip_map()->insert( |
| + std::make_pair(host_0, std::vector<ResolvedIPAddress>())); |
| + host_to_ip_map()->at(host_0).push_back(ResolvedIPAddress(now, "1.1.1.1")); |
| + host_to_ip_map()->at(host_0).push_back( |
| + ResolvedIPAddress(one_hour_ago, "2.2.2.2")); |
| + host_to_ip_map()->insert( |
| + std::make_pair(host_1, std::vector<ResolvedIPAddress>())); |
| + host_to_ip_map()->at(host_1).push_back( |
| + ResolvedIPAddress(in_an_hour, "3.3.3.3")); |
| + ASSERT_EQ(std::size_t(2), host_to_ip_map()->size()); |
| + navigation_observer_manager_->CleanUpIpAddresses(); |
| + ASSERT_EQ(std::size_t(1), host_to_ip_map()->size()); |
| + EXPECT_EQ(host_to_ip_map()->end(), host_to_ip_map()->find(host_1)); |
| + ASSERT_EQ(std::size_t(1), host_to_ip_map()->at(host_0).size()); |
| + EXPECT_EQ(now, host_to_ip_map()->at(host_0).front().timestamp); |
| +} |
| + |
| } // namespace safe_browsing |