summaryrefslogtreecommitdiffstats
path: root/net/base/host_cache_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'net/base/host_cache_unittest.cc')
-rw-r--r--net/base/host_cache_unittest.cc175
1 files changed, 108 insertions, 67 deletions
diff --git a/net/base/host_cache_unittest.cc b/net/base/host_cache_unittest.cc
index 2259185..55742ba 100644
--- a/net/base/host_cache_unittest.cc
+++ b/net/base/host_cache_unittest.cc
@@ -12,10 +12,16 @@
namespace net {
namespace {
-static const int kMaxCacheEntries = 10;
-static const int kCacheDurationMs = 10000; // 10 seconds.
+const int kMaxCacheEntries = 10;
+const int kCacheDurationMs = 10000; // 10 seconds.
+
+// Builds a key for |hostname|, defaulting the address family to unspecified.
+HostCache::Key Key(const std::string& hostname) {
+ return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED);
}
+} // namespace
+
TEST(HostCacheTest, Basic) {
HostCache cache(kMaxCacheEntries, kCacheDurationMs);
@@ -28,9 +34,9 @@ TEST(HostCacheTest, Basic) {
EXPECT_EQ(0U, cache.size());
// Add an entry for "foobar.com" at t=0.
- EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL);
- cache.Set("foobar.com", OK, AddressList(), now);
- entry1 = cache.Lookup("foobar.com", base::TimeTicks());
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
+ cache.Set(Key("foobar.com"), OK, AddressList(), now);
+ entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks());
EXPECT_FALSE(entry1 == NULL);
EXPECT_EQ(1U, cache.size());
@@ -38,9 +44,9 @@ TEST(HostCacheTest, Basic) {
now += base::TimeDelta::FromSeconds(5);
// Add an entry for "foobar2.com" at t=5.
- EXPECT_TRUE(cache.Lookup("foobar2.com", base::TimeTicks()) == NULL);
- cache.Set("foobar2.com", OK, AddressList(), now);
- entry2 = cache.Lookup("foobar2.com", base::TimeTicks());
+ EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL);
+ cache.Set(Key("foobar2.com"), OK, AddressList(), now);
+ entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks());
EXPECT_FALSE(NULL == entry1);
EXPECT_EQ(2U, cache.size());
@@ -48,30 +54,30 @@ TEST(HostCacheTest, Basic) {
now += base::TimeDelta::FromSeconds(4);
// Verify that the entries we added are still retrievable, and usable.
- EXPECT_EQ(entry1, cache.Lookup("foobar.com", now));
- EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now));
+ EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
+ EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
// Advance to t=10; entry1 is now expired.
now += base::TimeDelta::FromSeconds(1);
- EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL);
- EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now));
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
+ EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
// Update entry1, so it is no longer expired.
- cache.Set("foobar.com", OK, AddressList(), now);
+ cache.Set(Key("foobar.com"), OK, AddressList(), now);
// Re-uses existing entry storage.
- EXPECT_EQ(entry1, cache.Lookup("foobar.com", now));
+ EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
EXPECT_EQ(2U, cache.size());
// Both entries should still be retrievable and usable.
- EXPECT_EQ(entry1, cache.Lookup("foobar.com", now));
- EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now));
+ EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
+ EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
// Advance to t=20; both entries are now expired.
now += base::TimeDelta::FromSeconds(10);
- EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL);
- EXPECT_TRUE(cache.Lookup("foobar2.com", now) == NULL);
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
+ EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL);
}
// Try caching entries for a failed resolve attempt.
@@ -81,24 +87,24 @@ TEST(HostCacheTest, NegativeEntry) {
// Set t=0.
base::TimeTicks now;
- EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL);
- cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now);
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
+ cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now);
EXPECT_EQ(1U, cache.size());
// We disallow use of negative entries.
- EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL);
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
// Now overwrite with a valid entry, and then overwrite with negative entry
// again -- the valid entry should be kicked out.
- cache.Set("foobar.com", OK, AddressList(), now);
- EXPECT_FALSE(cache.Lookup("foobar.com", now) == NULL);
- cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now);
- EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL);
+ cache.Set(Key("foobar.com"), OK, AddressList(), now);
+ EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now) == NULL);
+ cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now);
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
}
TEST(HostCacheTest, Compact) {
// Initial entries limit is big enough to accomadate everything we add.
- net::HostCache cache(kMaxCacheEntries, kCacheDurationMs);
+ HostCache cache(kMaxCacheEntries, kCacheDurationMs);
EXPECT_EQ(0U, cache.size());
@@ -108,7 +114,7 @@ TEST(HostCacheTest, Compact) {
// Add five valid entries at t=10.
for (int i = 0; i < 5; ++i) {
std::string hostname = StringPrintf("valid%d", i);
- cache.Set(hostname, OK, AddressList(), now);
+ cache.Set(Key(hostname), OK, AddressList(), now);
}
EXPECT_EQ(5U, cache.size());
@@ -116,27 +122,27 @@ TEST(HostCacheTest, Compact) {
for (int i = 0; i < 3; ++i) {
std::string hostname = StringPrintf("expired%d", i);
base::TimeTicks t = now - base::TimeDelta::FromSeconds(10);
- cache.Set(hostname, OK, AddressList(), t);
+ cache.Set(Key(hostname), OK, AddressList(), t);
}
EXPECT_EQ(8U, cache.size());
// Add 2 negative entries at t=10
for (int i = 0; i < 2; ++i) {
std::string hostname = StringPrintf("negative%d", i);
- cache.Set(hostname, ERR_NAME_NOT_RESOLVED, AddressList(), now);
+ cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now);
}
EXPECT_EQ(10U, cache.size());
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "expired0"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "expired1"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "expired2"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "negative0"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "negative1"));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired0")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired1")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired2")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("negative0")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("negative1")));
// Shrink the max constraints bound and compact. We expect the "negative"
// and "expired" entries to have been dropped.
@@ -144,16 +150,16 @@ TEST(HostCacheTest, Compact) {
cache.Compact(now, NULL);
EXPECT_EQ(5U, cache.entries_.size());
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
- EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
- EXPECT_FALSE(ContainsKey(cache.entries_, "expired0"));
- EXPECT_FALSE(ContainsKey(cache.entries_, "expired1"));
- EXPECT_FALSE(ContainsKey(cache.entries_, "expired2"));
- EXPECT_FALSE(ContainsKey(cache.entries_, "negative0"));
- EXPECT_FALSE(ContainsKey(cache.entries_, "negative1"));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3")));
+ EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4")));
+ EXPECT_FALSE(ContainsKey(cache.entries_, Key("expired0")));
+ EXPECT_FALSE(ContainsKey(cache.entries_, Key("expired1")));
+ EXPECT_FALSE(ContainsKey(cache.entries_, Key("expired2")));
+ EXPECT_FALSE(ContainsKey(cache.entries_, Key("negative0")));
+ EXPECT_FALSE(ContainsKey(cache.entries_, Key("negative1")));
// Shrink further -- this time the compact will start dropping valid entries
// to make space.
@@ -164,39 +170,74 @@ TEST(HostCacheTest, Compact) {
// Add entries while the cache is at capacity, causing evictions.
TEST(HostCacheTest, SetWithCompact) {
- net::HostCache cache(3, kCacheDurationMs);
+ HostCache cache(3, kCacheDurationMs);
// t=10
base::TimeTicks now =
base::TimeTicks() + base::TimeDelta::FromMilliseconds(kCacheDurationMs);
- cache.Set("host1", OK, AddressList(), now);
- cache.Set("host2", OK, AddressList(), now);
- cache.Set("expired", OK, AddressList(),
+ cache.Set(Key("host1"), OK, AddressList(), now);
+ cache.Set(Key("host2"), OK, AddressList(), now);
+ cache.Set(Key("expired"), OK, AddressList(),
now - base::TimeDelta::FromMilliseconds(kCacheDurationMs));
EXPECT_EQ(3U, cache.size());
// Should all be retrievable except "expired".
- EXPECT_FALSE(NULL == cache.Lookup("host1", now));
- EXPECT_FALSE(NULL == cache.Lookup("host2", now));
- EXPECT_TRUE(NULL == cache.Lookup("expired", now));
+ EXPECT_FALSE(NULL == cache.Lookup(Key("host1"), now));
+ EXPECT_FALSE(NULL == cache.Lookup(Key("host2"), now));
+ EXPECT_TRUE(NULL == cache.Lookup(Key("expired"), now));
// Adding the fourth entry will cause "expired" to be evicted.
- cache.Set("host3", OK, AddressList(), now);
+ cache.Set(Key("host3"), OK, AddressList(), now);
EXPECT_EQ(3U, cache.size());
- EXPECT_TRUE(cache.Lookup("expired", now) == NULL);
- EXPECT_FALSE(cache.Lookup("host1", now) == NULL);
- EXPECT_FALSE(cache.Lookup("host2", now) == NULL);
- EXPECT_FALSE(cache.Lookup("host3", now) == NULL);
+ EXPECT_TRUE(cache.Lookup(Key("expired"), now) == NULL);
+ EXPECT_FALSE(cache.Lookup(Key("host1"), now) == NULL);
+ EXPECT_FALSE(cache.Lookup(Key("host2"), now) == NULL);
+ EXPECT_FALSE(cache.Lookup(Key("host3"), now) == NULL);
// Add two more entries. Something should be evicted, however "host5"
// should definitely be in there (since it was last inserted).
- cache.Set("host4", OK, AddressList(), now);
+ cache.Set(Key("host4"), OK, AddressList(), now);
EXPECT_EQ(3U, cache.size());
- cache.Set("host5", OK, AddressList(), now);
+ cache.Set(Key("host5"), OK, AddressList(), now);
EXPECT_EQ(3U, cache.size());
- EXPECT_FALSE(cache.Lookup("host5", now) == NULL);
+ EXPECT_FALSE(cache.Lookup(Key("host5"), now) == NULL);
+}
+
+// Tests that the same hostname can be duplicated in the cache, so long as
+// the address family differs.
+TEST(HostCacheTest, AddressFamilyIsPartOfKey) {
+ HostCache cache(kMaxCacheEntries, kCacheDurationMs);
+
+ // t=0.
+ base::TimeTicks now;
+
+ HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED);
+ HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4_ONLY);
+
+ const HostCache::Entry* entry1 = NULL; // Entry for key1
+ const HostCache::Entry* entry2 = NULL; // Entry for key2
+
+ EXPECT_EQ(0U, cache.size());
+
+ // Add an entry for ("foobar.com", UNSPECIFIED) at t=0.
+ EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL);
+ cache.Set(key1, OK, AddressList(), now);
+ entry1 = cache.Lookup(key1, base::TimeTicks());
+ EXPECT_FALSE(entry1 == NULL);
+ EXPECT_EQ(1U, cache.size());
+
+ // Add an entry for ("foobar.com", IPV4_ONLY) at t=0.
+ EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL);
+ cache.Set(key2, OK, AddressList(), now);
+ entry2 = cache.Lookup(key2, base::TimeTicks());
+ EXPECT_FALSE(entry2 == NULL);
+ EXPECT_EQ(2U, cache.size());
+
+ // Even though the hostnames were the same, we should have two unique
+ // entries (because the address families differ).
+ EXPECT_NE(entry1, entry2);
}
TEST(HostCacheTest, NoCache) {
@@ -208,9 +249,9 @@ TEST(HostCacheTest, NoCache) {
base::TimeTicks now;
// Lookup and Set should have no effect.
- EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL);
- cache.Set("foobar.com", OK, AddressList(), now);
- EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL);
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
+ cache.Set(Key("foobar.com"), OK, AddressList(), now);
+ EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
EXPECT_EQ(0U, cache.size());
}