1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/net/connect_interceptor.h" 6 7 #include "base/threading/platform_thread.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace chrome_browser_net { 11 12 // These tests are all focused ConnectInterceptor::TimedCache. 13 TEST(ConnectInterceptorTest, TimedCacheRecall) { 14 // Creat a cache that has a long expiration so that we can test basic recall. 15 ConnectInterceptor::TimedCache cache(base::TimeDelta::FromHours(1)); 16 17 GURL url("http://google.com/anypath"); 18 GURL ssl_url("https://ssl_google.com/anypath"); 19 EXPECT_FALSE(cache.WasRecentlySeen(url)); 20 EXPECT_FALSE(cache.WasRecentlySeen(ssl_url)); 21 22 cache.SetRecentlySeen(url); 23 24 EXPECT_TRUE(cache.WasRecentlySeen(url)); 25 EXPECT_FALSE(cache.WasRecentlySeen(ssl_url)); 26 27 cache.SetRecentlySeen(ssl_url); 28 29 EXPECT_TRUE(cache.WasRecentlySeen(url)); 30 EXPECT_TRUE(cache.WasRecentlySeen(ssl_url)); 31 32 // Check that port defaults correctly in canonicalization. 33 GURL url_with_port("http://google.com:80/anypath"); 34 GURL ssl_url_with_port("https://ssl_google.com:443/anypath"); 35 EXPECT_TRUE(cache.WasRecentlySeen(url_with_port)); 36 EXPECT_TRUE(cache.WasRecentlySeen(ssl_url_with_port)); 37 38 // Check for similar urls, to verify canonicalization isn't too generous. 39 GURL ssl_url_wrong_host("https://google.com/otherpath"); 40 GURL ssl_url_wrong_path("https://ssl_google.com/otherpath"); 41 GURL ssl_url_wrong_port("https://ssl_google.com:666/anypath"); 42 GURL url_wrong_scheme("ftp://google.com/anypath"); 43 GURL url_wrong_host("http://DOODLE.com/otherpath"); 44 GURL url_wrong_path("http://google.com/otherpath"); 45 GURL url_wrong_port("http://google.com:81/anypath"); 46 47 EXPECT_FALSE(cache.WasRecentlySeen(ssl_url_wrong_host)); 48 EXPECT_FALSE(cache.WasRecentlySeen(ssl_url_wrong_path)); 49 EXPECT_FALSE(cache.WasRecentlySeen(ssl_url_wrong_port)); 50 51 EXPECT_FALSE(cache.WasRecentlySeen(url_wrong_scheme)); 52 53 EXPECT_FALSE(cache.WasRecentlySeen(url_wrong_host)); 54 EXPECT_FALSE(cache.WasRecentlySeen(url_wrong_path)); 55 EXPECT_FALSE(cache.WasRecentlySeen(url_wrong_port)); 56 } 57 58 TEST(ConnectInterceptorTest, TimedCacheEviction) { 59 // Creat a cache that has a short expiration so that we can force evictions. 60 ConnectInterceptor::TimedCache cache(base::TimeDelta::FromMilliseconds(1)); 61 62 GURL url("http://google.com/anypath"); 63 EXPECT_FALSE(cache.WasRecentlySeen(url)); 64 65 cache.SetRecentlySeen(url); 66 67 // Sleep at least long enough to cause an eviction. 68 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); 69 70 EXPECT_FALSE(cache.WasRecentlySeen(url)); 71 } 72 73 } // namespace chrome_browser_net. 74