Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1172)

Side by Side Diff: net/base/host_cache_unittest.cc

Issue 302010: Add a mechanism to disable IPv6.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Address darin's comments Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "net/base/host_cache.h" 5 #include "net/base/host_cache.h"
6 6
7 #include "base/stl_util-inl.h" 7 #include "base/stl_util-inl.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace net { 12 namespace net {
13 13
14 namespace { 14 namespace {
15 static const int kMaxCacheEntries = 10; 15 const int kMaxCacheEntries = 10;
16 static const int kCacheDurationMs = 10000; // 10 seconds. 16 const int kCacheDurationMs = 10000; // 10 seconds.
17
18 // Builds a key for |hostname|, defaulting the address family to unspecified.
19 HostCache::Key Key(const std::string& hostname) {
20 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED);
17 } 21 }
18 22
23 } // namespace
24
19 TEST(HostCacheTest, Basic) { 25 TEST(HostCacheTest, Basic) {
20 HostCache cache(kMaxCacheEntries, kCacheDurationMs); 26 HostCache cache(kMaxCacheEntries, kCacheDurationMs);
21 27
22 // Start at t=0. 28 // Start at t=0.
23 base::TimeTicks now; 29 base::TimeTicks now;
24 30
25 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com. 31 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com.
26 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com. 32 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com.
27 33
28 EXPECT_EQ(0U, cache.size()); 34 EXPECT_EQ(0U, cache.size());
29 35
30 // Add an entry for "foobar.com" at t=0. 36 // Add an entry for "foobar.com" at t=0.
31 EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL); 37 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
32 cache.Set("foobar.com", OK, AddressList(), now); 38 cache.Set(Key("foobar.com"), OK, AddressList(), now);
33 entry1 = cache.Lookup("foobar.com", base::TimeTicks()); 39 entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks());
34 EXPECT_FALSE(entry1 == NULL); 40 EXPECT_FALSE(entry1 == NULL);
35 EXPECT_EQ(1U, cache.size()); 41 EXPECT_EQ(1U, cache.size());
36 42
37 // Advance to t=5. 43 // Advance to t=5.
38 now += base::TimeDelta::FromSeconds(5); 44 now += base::TimeDelta::FromSeconds(5);
39 45
40 // Add an entry for "foobar2.com" at t=5. 46 // Add an entry for "foobar2.com" at t=5.
41 EXPECT_TRUE(cache.Lookup("foobar2.com", base::TimeTicks()) == NULL); 47 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL);
42 cache.Set("foobar2.com", OK, AddressList(), now); 48 cache.Set(Key("foobar2.com"), OK, AddressList(), now);
43 entry2 = cache.Lookup("foobar2.com", base::TimeTicks()); 49 entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks());
44 EXPECT_FALSE(NULL == entry1); 50 EXPECT_FALSE(NULL == entry1);
45 EXPECT_EQ(2U, cache.size()); 51 EXPECT_EQ(2U, cache.size());
46 52
47 // Advance to t=9 53 // Advance to t=9
48 now += base::TimeDelta::FromSeconds(4); 54 now += base::TimeDelta::FromSeconds(4);
49 55
50 // Verify that the entries we added are still retrievable, and usable. 56 // Verify that the entries we added are still retrievable, and usable.
51 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); 57 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
52 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); 58 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
53 59
54 // Advance to t=10; entry1 is now expired. 60 // Advance to t=10; entry1 is now expired.
55 now += base::TimeDelta::FromSeconds(1); 61 now += base::TimeDelta::FromSeconds(1);
56 62
57 EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL); 63 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
58 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); 64 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
59 65
60 // Update entry1, so it is no longer expired. 66 // Update entry1, so it is no longer expired.
61 cache.Set("foobar.com", OK, AddressList(), now); 67 cache.Set(Key("foobar.com"), OK, AddressList(), now);
62 // Re-uses existing entry storage. 68 // Re-uses existing entry storage.
63 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); 69 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
64 EXPECT_EQ(2U, cache.size()); 70 EXPECT_EQ(2U, cache.size());
65 71
66 // Both entries should still be retrievable and usable. 72 // Both entries should still be retrievable and usable.
67 EXPECT_EQ(entry1, cache.Lookup("foobar.com", now)); 73 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
68 EXPECT_EQ(entry2, cache.Lookup("foobar2.com", now)); 74 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
69 75
70 // Advance to t=20; both entries are now expired. 76 // Advance to t=20; both entries are now expired.
71 now += base::TimeDelta::FromSeconds(10); 77 now += base::TimeDelta::FromSeconds(10);
72 78
73 EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL); 79 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
74 EXPECT_TRUE(cache.Lookup("foobar2.com", now) == NULL); 80 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL);
75 } 81 }
76 82
77 // Try caching entries for a failed resolve attempt. 83 // Try caching entries for a failed resolve attempt.
78 TEST(HostCacheTest, NegativeEntry) { 84 TEST(HostCacheTest, NegativeEntry) {
79 HostCache cache(kMaxCacheEntries, kCacheDurationMs); 85 HostCache cache(kMaxCacheEntries, kCacheDurationMs);
80 86
81 // Set t=0. 87 // Set t=0.
82 base::TimeTicks now; 88 base::TimeTicks now;
83 89
84 EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL); 90 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
85 cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now); 91 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now);
86 EXPECT_EQ(1U, cache.size()); 92 EXPECT_EQ(1U, cache.size());
87 93
88 // We disallow use of negative entries. 94 // We disallow use of negative entries.
89 EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL); 95 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
90 96
91 // Now overwrite with a valid entry, and then overwrite with negative entry 97 // Now overwrite with a valid entry, and then overwrite with negative entry
92 // again -- the valid entry should be kicked out. 98 // again -- the valid entry should be kicked out.
93 cache.Set("foobar.com", OK, AddressList(), now); 99 cache.Set(Key("foobar.com"), OK, AddressList(), now);
94 EXPECT_FALSE(cache.Lookup("foobar.com", now) == NULL); 100 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now) == NULL);
95 cache.Set("foobar.com", ERR_NAME_NOT_RESOLVED, AddressList(), now); 101 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now);
96 EXPECT_TRUE(cache.Lookup("foobar.com", now) == NULL); 102 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
97 } 103 }
98 104
99 TEST(HostCacheTest, Compact) { 105 TEST(HostCacheTest, Compact) {
100 // Initial entries limit is big enough to accomadate everything we add. 106 // Initial entries limit is big enough to accomadate everything we add.
101 net::HostCache cache(kMaxCacheEntries, kCacheDurationMs); 107 HostCache cache(kMaxCacheEntries, kCacheDurationMs);
102 108
103 EXPECT_EQ(0U, cache.size()); 109 EXPECT_EQ(0U, cache.size());
104 110
105 // t=10 111 // t=10
106 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10); 112 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10);
107 113
108 // Add five valid entries at t=10. 114 // Add five valid entries at t=10.
109 for (int i = 0; i < 5; ++i) { 115 for (int i = 0; i < 5; ++i) {
110 std::string hostname = StringPrintf("valid%d", i); 116 std::string hostname = StringPrintf("valid%d", i);
111 cache.Set(hostname, OK, AddressList(), now); 117 cache.Set(Key(hostname), OK, AddressList(), now);
112 } 118 }
113 EXPECT_EQ(5U, cache.size()); 119 EXPECT_EQ(5U, cache.size());
114 120
115 // Add 3 expired entries at t=0. 121 // Add 3 expired entries at t=0.
116 for (int i = 0; i < 3; ++i) { 122 for (int i = 0; i < 3; ++i) {
117 std::string hostname = StringPrintf("expired%d", i); 123 std::string hostname = StringPrintf("expired%d", i);
118 base::TimeTicks t = now - base::TimeDelta::FromSeconds(10); 124 base::TimeTicks t = now - base::TimeDelta::FromSeconds(10);
119 cache.Set(hostname, OK, AddressList(), t); 125 cache.Set(Key(hostname), OK, AddressList(), t);
120 } 126 }
121 EXPECT_EQ(8U, cache.size()); 127 EXPECT_EQ(8U, cache.size());
122 128
123 // Add 2 negative entries at t=10 129 // Add 2 negative entries at t=10
124 for (int i = 0; i < 2; ++i) { 130 for (int i = 0; i < 2; ++i) {
125 std::string hostname = StringPrintf("negative%d", i); 131 std::string hostname = StringPrintf("negative%d", i);
126 cache.Set(hostname, ERR_NAME_NOT_RESOLVED, AddressList(), now); 132 cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now);
127 } 133 }
128 EXPECT_EQ(10U, cache.size()); 134 EXPECT_EQ(10U, cache.size());
129 135
130 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0")); 136 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0")));
131 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1")); 137 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1")));
132 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2")); 138 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2")));
133 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3")); 139 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3")));
134 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4")); 140 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4")));
135 EXPECT_TRUE(ContainsKey(cache.entries_, "expired0")); 141 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired0")));
136 EXPECT_TRUE(ContainsKey(cache.entries_, "expired1")); 142 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired1")));
137 EXPECT_TRUE(ContainsKey(cache.entries_, "expired2")); 143 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired2")));
138 EXPECT_TRUE(ContainsKey(cache.entries_, "negative0")); 144 EXPECT_TRUE(ContainsKey(cache.entries_, Key("negative0")));
139 EXPECT_TRUE(ContainsKey(cache.entries_, "negative1")); 145 EXPECT_TRUE(ContainsKey(cache.entries_, Key("negative1")));
140 146
141 // Shrink the max constraints bound and compact. We expect the "negative" 147 // Shrink the max constraints bound and compact. We expect the "negative"
142 // and "expired" entries to have been dropped. 148 // and "expired" entries to have been dropped.
143 cache.max_entries_ = 5; 149 cache.max_entries_ = 5;
144 cache.Compact(now, NULL); 150 cache.Compact(now, NULL);
145 EXPECT_EQ(5U, cache.entries_.size()); 151 EXPECT_EQ(5U, cache.entries_.size());
146 152
147 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0")); 153 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0")));
148 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1")); 154 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1")));
149 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2")); 155 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2")));
150 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3")); 156 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3")));
151 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4")); 157 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4")));
152 EXPECT_FALSE(ContainsKey(cache.entries_, "expired0")); 158 EXPECT_FALSE(ContainsKey(cache.entries_, Key("expired0")));
153 EXPECT_FALSE(ContainsKey(cache.entries_, "expired1")); 159 EXPECT_FALSE(ContainsKey(cache.entries_, Key("expired1")));
154 EXPECT_FALSE(ContainsKey(cache.entries_, "expired2")); 160 EXPECT_FALSE(ContainsKey(cache.entries_, Key("expired2")));
155 EXPECT_FALSE(ContainsKey(cache.entries_, "negative0")); 161 EXPECT_FALSE(ContainsKey(cache.entries_, Key("negative0")));
156 EXPECT_FALSE(ContainsKey(cache.entries_, "negative1")); 162 EXPECT_FALSE(ContainsKey(cache.entries_, Key("negative1")));
157 163
158 // Shrink further -- this time the compact will start dropping valid entries 164 // Shrink further -- this time the compact will start dropping valid entries
159 // to make space. 165 // to make space.
160 cache.max_entries_ = 3; 166 cache.max_entries_ = 3;
161 cache.Compact(now, NULL); 167 cache.Compact(now, NULL);
162 EXPECT_EQ(3U, cache.size()); 168 EXPECT_EQ(3U, cache.size());
163 } 169 }
164 170
165 // Add entries while the cache is at capacity, causing evictions. 171 // Add entries while the cache is at capacity, causing evictions.
166 TEST(HostCacheTest, SetWithCompact) { 172 TEST(HostCacheTest, SetWithCompact) {
167 net::HostCache cache(3, kCacheDurationMs); 173 HostCache cache(3, kCacheDurationMs);
168 174
169 // t=10 175 // t=10
170 base::TimeTicks now = 176 base::TimeTicks now =
171 base::TimeTicks() + base::TimeDelta::FromMilliseconds(kCacheDurationMs); 177 base::TimeTicks() + base::TimeDelta::FromMilliseconds(kCacheDurationMs);
172 178
173 cache.Set("host1", OK, AddressList(), now); 179 cache.Set(Key("host1"), OK, AddressList(), now);
174 cache.Set("host2", OK, AddressList(), now); 180 cache.Set(Key("host2"), OK, AddressList(), now);
175 cache.Set("expired", OK, AddressList(), 181 cache.Set(Key("expired"), OK, AddressList(),
176 now - base::TimeDelta::FromMilliseconds(kCacheDurationMs)); 182 now - base::TimeDelta::FromMilliseconds(kCacheDurationMs));
177 183
178 EXPECT_EQ(3U, cache.size()); 184 EXPECT_EQ(3U, cache.size());
179 185
180 // Should all be retrievable except "expired". 186 // Should all be retrievable except "expired".
181 EXPECT_FALSE(NULL == cache.Lookup("host1", now)); 187 EXPECT_FALSE(NULL == cache.Lookup(Key("host1"), now));
182 EXPECT_FALSE(NULL == cache.Lookup("host2", now)); 188 EXPECT_FALSE(NULL == cache.Lookup(Key("host2"), now));
183 EXPECT_TRUE(NULL == cache.Lookup("expired", now)); 189 EXPECT_TRUE(NULL == cache.Lookup(Key("expired"), now));
184 190
185 // Adding the fourth entry will cause "expired" to be evicted. 191 // Adding the fourth entry will cause "expired" to be evicted.
186 cache.Set("host3", OK, AddressList(), now); 192 cache.Set(Key("host3"), OK, AddressList(), now);
187 EXPECT_EQ(3U, cache.size()); 193 EXPECT_EQ(3U, cache.size());
188 EXPECT_TRUE(cache.Lookup("expired", now) == NULL); 194 EXPECT_TRUE(cache.Lookup(Key("expired"), now) == NULL);
189 EXPECT_FALSE(cache.Lookup("host1", now) == NULL); 195 EXPECT_FALSE(cache.Lookup(Key("host1"), now) == NULL);
190 EXPECT_FALSE(cache.Lookup("host2", now) == NULL); 196 EXPECT_FALSE(cache.Lookup(Key("host2"), now) == NULL);
191 EXPECT_FALSE(cache.Lookup("host3", now) == NULL); 197 EXPECT_FALSE(cache.Lookup(Key("host3"), now) == NULL);
192 198
193 // Add two more entries. Something should be evicted, however "host5" 199 // Add two more entries. Something should be evicted, however "host5"
194 // should definitely be in there (since it was last inserted). 200 // should definitely be in there (since it was last inserted).
195 cache.Set("host4", OK, AddressList(), now); 201 cache.Set(Key("host4"), OK, AddressList(), now);
196 EXPECT_EQ(3U, cache.size()); 202 EXPECT_EQ(3U, cache.size());
197 cache.Set("host5", OK, AddressList(), now); 203 cache.Set(Key("host5"), OK, AddressList(), now);
198 EXPECT_EQ(3U, cache.size()); 204 EXPECT_EQ(3U, cache.size());
199 EXPECT_FALSE(cache.Lookup("host5", now) == NULL); 205 EXPECT_FALSE(cache.Lookup(Key("host5"), now) == NULL);
206 }
207
208 // Tests that the same hostname can be duplicated in the cache, so long as
209 // the address family differs.
210 TEST(HostCacheTest, AddressFamilyIsPartOfKey) {
211 HostCache cache(kMaxCacheEntries, kCacheDurationMs);
212
213 // t=0.
214 base::TimeTicks now;
215
216 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED);
217 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4_ONLY);
218
219 const HostCache::Entry* entry1 = NULL; // Entry for key1
220 const HostCache::Entry* entry2 = NULL; // Entry for key2
221
222 EXPECT_EQ(0U, cache.size());
223
224 // Add an entry for ("foobar.com", UNSPECIFIED) at t=0.
225 EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL);
226 cache.Set(key1, OK, AddressList(), now);
227 entry1 = cache.Lookup(key1, base::TimeTicks());
228 EXPECT_FALSE(entry1 == NULL);
229 EXPECT_EQ(1U, cache.size());
230
231 // Add an entry for ("foobar.com", IPV4_ONLY) at t=0.
232 EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL);
233 cache.Set(key2, OK, AddressList(), now);
234 entry2 = cache.Lookup(key2, base::TimeTicks());
235 EXPECT_FALSE(entry2 == NULL);
236 EXPECT_EQ(2U, cache.size());
237
238 // Even though the hostnames were the same, we should have two unique
239 // entries (because the address families differ).
240 EXPECT_NE(entry1, entry2);
200 } 241 }
201 242
202 TEST(HostCacheTest, NoCache) { 243 TEST(HostCacheTest, NoCache) {
203 // Disable caching. 244 // Disable caching.
204 HostCache cache(0, kCacheDurationMs); 245 HostCache cache(0, kCacheDurationMs);
205 EXPECT_TRUE(cache.caching_is_disabled()); 246 EXPECT_TRUE(cache.caching_is_disabled());
206 247
207 // Set t=0. 248 // Set t=0.
208 base::TimeTicks now; 249 base::TimeTicks now;
209 250
210 // Lookup and Set should have no effect. 251 // Lookup and Set should have no effect.
211 EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL); 252 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
212 cache.Set("foobar.com", OK, AddressList(), now); 253 cache.Set(Key("foobar.com"), OK, AddressList(), now);
213 EXPECT_TRUE(cache.Lookup("foobar.com", base::TimeTicks()) == NULL); 254 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
214 255
215 EXPECT_EQ(0U, cache.size()); 256 EXPECT_EQ(0U, cache.size());
216 } 257 }
217 258
218 } // namespace net 259 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_cache.cc ('k') | net/base/host_resolver.h » ('j') | net/base/host_resolver_impl.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698