| 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..f38dcf8e988bec0edeed1735427e2c37d8e1a58b 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,34 @@ 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;
|
| + }
|
| +
|
| + void CleanUpNavigationEvents() {
|
| + navigation_observer_manager_->CleanUpNavigationEvents();
|
| + }
|
| +
|
| + void CleanUpIpAddresses() {
|
| + navigation_observer_manager_->CleanUpIpAddresses();
|
| + }
|
| +
|
| + void CleanUpUserGestures() {
|
| + navigation_observer_manager_->CleanUpUserGestures();
|
| + }
|
| +
|
| protected:
|
| SafeBrowsingNavigationObserverManager* navigation_observer_manager_;
|
| SafeBrowsingNavigationObserver* navigation_observer_;
|
| @@ -77,8 +105,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 +142,105 @@ TEST_F(SBNavigationObserverTest, ServerRedirect) {
|
| nav_map->at(GURL("http://redirect/1")).at(0));
|
| }
|
|
|
| +TEST_F(SBNavigationObserverTest, TestCleanUpStaleNavigationEvents) {
|
| + // Sets up navigation_map() such that it includes fresh, stale and invalid
|
| + // 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(
|
| + 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());
|
| +
|
| + // Cleans up navigation events.
|
| + CleanUpNavigationEvents();
|
| +
|
| + // Verifies all stale and invalid navigation events are removed.
|
| + 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) {
|
| + // Sets up user_gesture_map() such that it includes fresh, stale and invalid
|
| + // user gestures.
|
| + base::Time now = base::Time::Now(); // Fresh
|
| + base::Time one_minute_ago =
|
| + base::Time::FromDoubleT(now.ToDoubleT() - 60.0); // Stale
|
| + base::Time in_an_hour =
|
| + base::Time::FromDoubleT(now.ToDoubleT() + 60.0 * 60.0); // Invalid
|
| + 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, in_an_hour));
|
| + ASSERT_EQ(std::size_t(3), user_gesture_map()->size());
|
| +
|
| + // Cleans up user_gesture_map()
|
| + CleanUpUserGestures();
|
| +
|
| + // Verifies all stale and invalid user gestures are removed.
|
| + 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) {
|
| + // Sets up host_to_ip_map() such that it includes fresh, stale and invalid
|
| + // user gestures.
|
| + 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());
|
| +
|
| + // Cleans up host_to_ip_map()
|
| + CleanUpIpAddresses();
|
| +
|
| + // Verifies all stale and invalid IP addresses are removed.
|
| + 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
|
|
|