OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 TEST(HostCacheTest, Basic) { | 27 TEST(HostCacheTest, Basic) { |
28 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 28 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
29 | 29 |
30 HostCache cache(kMaxCacheEntries); | 30 HostCache cache(kMaxCacheEntries); |
31 | 31 |
32 // Start at t=0. | 32 // Start at t=0. |
33 base::TimeTicks now; | 33 base::TimeTicks now; |
34 | 34 |
35 HostCache::Key key1 = Key("foobar.com"); | 35 HostCache::Key key1 = Key("foobar.com"); |
36 HostCache::Key key2 = Key("foobar2.com"); | 36 HostCache::Key key2 = Key("foobar2.com"); |
| 37 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
37 | 38 |
38 EXPECT_EQ(0U, cache.size()); | 39 EXPECT_EQ(0U, cache.size()); |
39 | 40 |
40 // Add an entry for "foobar.com" at t=0. | 41 // Add an entry for "foobar.com" at t=0. |
41 EXPECT_FALSE(cache.Lookup(key1, now)); | 42 EXPECT_FALSE(cache.Lookup(key1, now)); |
42 cache.Set(key1, OK, AddressList(), now, kTTL); | 43 cache.Set(key1, entry, now, kTTL); |
43 EXPECT_TRUE(cache.Lookup(key1, now)); | 44 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 45 EXPECT_TRUE(cache.Lookup(key1, now)->error == entry.error); |
44 | 46 |
45 EXPECT_EQ(1U, cache.size()); | 47 EXPECT_EQ(1U, cache.size()); |
46 | 48 |
47 // Advance to t=5. | 49 // Advance to t=5. |
48 now += base::TimeDelta::FromSeconds(5); | 50 now += base::TimeDelta::FromSeconds(5); |
49 | 51 |
50 // Add an entry for "foobar2.com" at t=5. | 52 // Add an entry for "foobar2.com" at t=5. |
51 EXPECT_FALSE(cache.Lookup(key2, now)); | 53 EXPECT_FALSE(cache.Lookup(key2, now)); |
52 cache.Set(key2, OK, AddressList(), now, kTTL); | 54 cache.Set(key2, entry, now, kTTL); |
53 EXPECT_TRUE(cache.Lookup(key2, now)); | 55 EXPECT_TRUE(cache.Lookup(key2, now)); |
54 EXPECT_EQ(2U, cache.size()); | 56 EXPECT_EQ(2U, cache.size()); |
55 | 57 |
56 // Advance to t=9 | 58 // Advance to t=9 |
57 now += base::TimeDelta::FromSeconds(4); | 59 now += base::TimeDelta::FromSeconds(4); |
58 | 60 |
59 // Verify that the entries we added are still retrievable, and usable. | 61 // Verify that the entries we added are still retrievable, and usable. |
60 EXPECT_TRUE(cache.Lookup(key1, now)); | 62 EXPECT_TRUE(cache.Lookup(key1, now)); |
61 EXPECT_TRUE(cache.Lookup(key2, now)); | 63 EXPECT_TRUE(cache.Lookup(key2, now)); |
62 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); | 64 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); |
63 | 65 |
64 // Advance to t=10; key is now expired. | 66 // Advance to t=10; key is now expired. |
65 now += base::TimeDelta::FromSeconds(1); | 67 now += base::TimeDelta::FromSeconds(1); |
66 | 68 |
67 EXPECT_FALSE(cache.Lookup(key1, now)); | 69 EXPECT_FALSE(cache.Lookup(key1, now)); |
68 EXPECT_TRUE(cache.Lookup(key2, now)); | 70 EXPECT_TRUE(cache.Lookup(key2, now)); |
69 | 71 |
70 // Update key1, so it is no longer expired. | 72 // Update key1, so it is no longer expired. |
71 cache.Set(key1, OK, AddressList(), now, kTTL); | 73 cache.Set(key1, entry, now, kTTL); |
72 EXPECT_TRUE(cache.Lookup(key1, now)); | 74 EXPECT_TRUE(cache.Lookup(key1, now)); |
73 EXPECT_EQ(2U, cache.size()); | 75 EXPECT_EQ(2U, cache.size()); |
74 | 76 |
75 // Both entries should still be retrievable and usable. | 77 // Both entries should still be retrievable and usable. |
76 EXPECT_TRUE(cache.Lookup(key1, now)); | 78 EXPECT_TRUE(cache.Lookup(key1, now)); |
77 EXPECT_TRUE(cache.Lookup(key2, now)); | 79 EXPECT_TRUE(cache.Lookup(key2, now)); |
78 | 80 |
79 // Advance to t=20; both entries are now expired. | 81 // Advance to t=20; both entries are now expired. |
80 now += base::TimeDelta::FromSeconds(10); | 82 now += base::TimeDelta::FromSeconds(10); |
81 | 83 |
82 EXPECT_FALSE(cache.Lookup(key1, now)); | 84 EXPECT_FALSE(cache.Lookup(key1, now)); |
83 EXPECT_FALSE(cache.Lookup(key2, now)); | 85 EXPECT_FALSE(cache.Lookup(key2, now)); |
84 } | 86 } |
85 | 87 |
86 // Try caching entries for a failed resolve attempt -- since we set the TTL of | 88 // Try caching entries for a failed resolve attempt -- since we set the TTL of |
87 // such entries to 0 it won't store, but it will kick out the previous result. | 89 // such entries to 0 it won't store, but it will kick out the previous result. |
88 TEST(HostCacheTest, NoCacheNegative) { | 90 TEST(HostCacheTest, NoCacheZeroTTL) { |
89 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); | 91 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
90 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); | 92 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); |
91 | 93 |
92 HostCache cache(kMaxCacheEntries); | 94 HostCache cache(kMaxCacheEntries); |
93 | 95 |
94 // Set t=0. | 96 // Set t=0. |
95 base::TimeTicks now; | 97 base::TimeTicks now; |
96 | 98 |
97 HostCache::Key key1 = Key("foobar.com"); | 99 HostCache::Key key1 = Key("foobar.com"); |
98 HostCache::Key key2 = Key("foobar2.com"); | 100 HostCache::Key key2 = Key("foobar2.com"); |
| 101 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
99 | 102 |
100 EXPECT_FALSE(cache.Lookup(key1, now)); | 103 EXPECT_FALSE(cache.Lookup(key1, now)); |
101 cache.Set(key1, ERR_NAME_NOT_RESOLVED, AddressList(), now, kFailureEntryTTL); | 104 cache.Set(key1, entry, now, kFailureEntryTTL); |
102 EXPECT_EQ(1U, cache.size()); | 105 EXPECT_EQ(1U, cache.size()); |
103 | 106 |
104 // We disallow use of negative entries. | 107 // We disallow use of negative entries. |
105 EXPECT_FALSE(cache.Lookup(key1, now)); | 108 EXPECT_FALSE(cache.Lookup(key1, now)); |
106 | 109 |
107 // Now overwrite with a valid entry, and then overwrite with negative entry | 110 // Now overwrite with a valid entry, and then overwrite with negative entry |
108 // again -- the valid entry should be kicked out. | 111 // again -- the valid entry should be kicked out. |
109 cache.Set(key1, OK, AddressList(), now, kSuccessEntryTTL); | 112 cache.Set(key1, entry, now, kSuccessEntryTTL); |
110 EXPECT_TRUE(cache.Lookup(key1, now)); | 113 EXPECT_TRUE(cache.Lookup(key1, now)); |
111 cache.Set(key1, ERR_NAME_NOT_RESOLVED, AddressList(), now, kFailureEntryTTL); | 114 cache.Set(key1, entry, now, kFailureEntryTTL); |
112 EXPECT_FALSE(cache.Lookup(key1, now)); | 115 EXPECT_FALSE(cache.Lookup(key1, now)); |
113 } | 116 } |
114 | 117 |
115 // Try caching entries for a failed resolves for 10 seconds. | 118 // Try caching entries for a failed resolves for 10 seconds. |
116 TEST(HostCacheTest, CacheNegativeEntry) { | 119 TEST(HostCacheTest, CacheNegativeEntry) { |
117 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(10); | 120 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(10); |
118 | 121 |
119 HostCache cache(kMaxCacheEntries); | 122 HostCache cache(kMaxCacheEntries); |
120 | 123 |
121 // Start at t=0. | 124 // Start at t=0. |
122 base::TimeTicks now; | 125 base::TimeTicks now; |
123 | 126 |
124 HostCache::Key key1 = Key("foobar.com"); | 127 HostCache::Key key1 = Key("foobar.com"); |
125 HostCache::Key key2 = Key("foobar2.com"); | 128 HostCache::Key key2 = Key("foobar2.com"); |
126 | 129 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
127 | 130 |
128 EXPECT_EQ(0U, cache.size()); | 131 EXPECT_EQ(0U, cache.size()); |
129 | 132 |
130 // Add an entry for "foobar.com" at t=0. | 133 // Add an entry for "foobar.com" at t=0. |
131 EXPECT_FALSE(cache.Lookup(key1, now)); | 134 EXPECT_FALSE(cache.Lookup(key1, now)); |
132 cache.Set(key1, ERR_NAME_NOT_RESOLVED, AddressList(), now, kFailureEntryTTL); | 135 cache.Set(key1, entry, now, kFailureEntryTTL); |
133 EXPECT_TRUE(cache.Lookup(key1, now)); | 136 EXPECT_TRUE(cache.Lookup(key1, now)); |
134 EXPECT_EQ(1U, cache.size()); | 137 EXPECT_EQ(1U, cache.size()); |
135 | 138 |
136 // Advance to t=5. | 139 // Advance to t=5. |
137 now += base::TimeDelta::FromSeconds(5); | 140 now += base::TimeDelta::FromSeconds(5); |
138 | 141 |
139 // Add an entry for "foobar2.com" at t=5. | 142 // Add an entry for "foobar2.com" at t=5. |
140 EXPECT_FALSE(cache.Lookup(key2, now)); | 143 EXPECT_FALSE(cache.Lookup(key2, now)); |
141 cache.Set(key2, ERR_NAME_NOT_RESOLVED, AddressList(), now, kFailureEntryTTL); | 144 cache.Set(key2, entry, now, kFailureEntryTTL); |
142 EXPECT_TRUE(cache.Lookup(key2, now)); | 145 EXPECT_TRUE(cache.Lookup(key2, now)); |
143 EXPECT_EQ(2U, cache.size()); | 146 EXPECT_EQ(2U, cache.size()); |
144 | 147 |
145 // Advance to t=9 | 148 // Advance to t=9 |
146 now += base::TimeDelta::FromSeconds(4); | 149 now += base::TimeDelta::FromSeconds(4); |
147 | 150 |
148 // Verify that the entries we added are still retrievable, and usable. | 151 // Verify that the entries we added are still retrievable, and usable. |
149 EXPECT_TRUE(cache.Lookup(key1, now)); | 152 EXPECT_TRUE(cache.Lookup(key1, now)); |
150 EXPECT_TRUE(cache.Lookup(key2, now)); | 153 EXPECT_TRUE(cache.Lookup(key2, now)); |
151 | 154 |
152 // Advance to t=10; key1 is now expired. | 155 // Advance to t=10; key1 is now expired. |
153 now += base::TimeDelta::FromSeconds(1); | 156 now += base::TimeDelta::FromSeconds(1); |
154 | 157 |
155 EXPECT_FALSE(cache.Lookup(key1, now)); | 158 EXPECT_FALSE(cache.Lookup(key1, now)); |
156 EXPECT_TRUE(cache.Lookup(key2, now)); | 159 EXPECT_TRUE(cache.Lookup(key2, now)); |
157 | 160 |
158 // Update key1, so it is no longer expired. | 161 // Update key1, so it is no longer expired. |
159 cache.Set(key1, ERR_NAME_NOT_RESOLVED, AddressList(), now, kFailureEntryTTL); | 162 cache.Set(key1, entry, now, kFailureEntryTTL); |
160 // Re-uses existing entry storage. | 163 // Re-uses existing entry storage. |
161 EXPECT_TRUE(cache.Lookup(key1, now)); | 164 EXPECT_TRUE(cache.Lookup(key1, now)); |
162 EXPECT_EQ(2U, cache.size()); | 165 EXPECT_EQ(2U, cache.size()); |
163 | 166 |
164 // Both entries should still be retrievable and usable. | 167 // Both entries should still be retrievable and usable. |
165 EXPECT_TRUE(cache.Lookup(key1, now)); | 168 EXPECT_TRUE(cache.Lookup(key1, now)); |
166 EXPECT_TRUE(cache.Lookup(key2, now)); | 169 EXPECT_TRUE(cache.Lookup(key2, now)); |
167 | 170 |
168 // Advance to t=20; both entries are now expired. | 171 // Advance to t=20; both entries are now expired. |
169 now += base::TimeDelta::FromSeconds(10); | 172 now += base::TimeDelta::FromSeconds(10); |
170 | 173 |
171 EXPECT_FALSE(cache.Lookup(key1, now)); | 174 EXPECT_FALSE(cache.Lookup(key1, now)); |
172 EXPECT_FALSE(cache.Lookup(key2, now)); | 175 EXPECT_FALSE(cache.Lookup(key2, now)); |
173 } | 176 } |
174 | 177 |
175 // Tests that the same hostname can be duplicated in the cache, so long as | 178 // Tests that the same hostname can be duplicated in the cache, so long as |
176 // the address family differs. | 179 // the address family differs. |
177 TEST(HostCacheTest, AddressFamilyIsPartOfKey) { | 180 TEST(HostCacheTest, AddressFamilyIsPartOfKey) { |
178 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); | 181 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
179 | 182 |
180 HostCache cache(kMaxCacheEntries); | 183 HostCache cache(kMaxCacheEntries); |
181 | 184 |
182 // t=0. | 185 // t=0. |
183 base::TimeTicks now; | 186 base::TimeTicks now; |
184 | 187 |
185 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0); | 188 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0); |
186 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0); | 189 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0); |
| 190 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
187 | 191 |
188 EXPECT_EQ(0U, cache.size()); | 192 EXPECT_EQ(0U, cache.size()); |
189 | 193 |
190 // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. | 194 // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. |
191 EXPECT_FALSE(cache.Lookup(key1, now)); | 195 EXPECT_FALSE(cache.Lookup(key1, now)); |
192 cache.Set(key1, OK, AddressList(), now, kSuccessEntryTTL); | 196 cache.Set(key1, entry, now, kSuccessEntryTTL); |
193 EXPECT_TRUE(cache.Lookup(key1, now)); | 197 EXPECT_TRUE(cache.Lookup(key1, now)); |
194 EXPECT_EQ(1U, cache.size()); | 198 EXPECT_EQ(1U, cache.size()); |
195 | 199 |
196 // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. | 200 // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. |
197 EXPECT_FALSE(cache.Lookup(key2, now)); | 201 EXPECT_FALSE(cache.Lookup(key2, now)); |
198 cache.Set(key2, OK, AddressList(), now, kSuccessEntryTTL); | 202 cache.Set(key2, entry, now, kSuccessEntryTTL); |
199 EXPECT_TRUE(cache.Lookup(key2, now)); | 203 EXPECT_TRUE(cache.Lookup(key2, now)); |
200 EXPECT_EQ(2U, cache.size()); | 204 EXPECT_EQ(2U, cache.size()); |
201 | 205 |
202 // Even though the hostnames were the same, we should have two unique | 206 // Even though the hostnames were the same, we should have two unique |
203 // entries (because the address families differ). | 207 // entries (because the address families differ). |
204 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); | 208 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); |
205 } | 209 } |
206 | 210 |
207 // Tests that the same hostname can be duplicated in the cache, so long as | 211 // Tests that the same hostname can be duplicated in the cache, so long as |
208 // the HostResolverFlags differ. | 212 // the HostResolverFlags differ. |
209 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { | 213 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { |
210 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 214 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
211 | 215 |
212 HostCache cache(kMaxCacheEntries); | 216 HostCache cache(kMaxCacheEntries); |
213 | 217 |
214 // t=0. | 218 // t=0. |
215 base::TimeTicks now; | 219 base::TimeTicks now; |
216 | 220 |
217 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0); | 221 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0); |
218 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, | 222 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, |
219 HOST_RESOLVER_CANONNAME); | 223 HOST_RESOLVER_CANONNAME); |
220 HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4, | 224 HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4, |
221 HOST_RESOLVER_LOOPBACK_ONLY); | 225 HOST_RESOLVER_LOOPBACK_ONLY); |
| 226 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
222 | 227 |
223 EXPECT_EQ(0U, cache.size()); | 228 EXPECT_EQ(0U, cache.size()); |
224 | 229 |
225 // Add an entry for ("foobar.com", IPV4, NONE) at t=0. | 230 // Add an entry for ("foobar.com", IPV4, NONE) at t=0. |
226 EXPECT_FALSE(cache.Lookup(key1, now)); | 231 EXPECT_FALSE(cache.Lookup(key1, now)); |
227 cache.Set(key1, OK, AddressList(), now, kTTL); | 232 cache.Set(key1, entry, now, kTTL); |
228 EXPECT_TRUE(cache.Lookup(key1, now)); | 233 EXPECT_TRUE(cache.Lookup(key1, now)); |
229 EXPECT_EQ(1U, cache.size()); | 234 EXPECT_EQ(1U, cache.size()); |
230 | 235 |
231 // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0. | 236 // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0. |
232 EXPECT_FALSE(cache.Lookup(key2, now)); | 237 EXPECT_FALSE(cache.Lookup(key2, now)); |
233 cache.Set(key2, OK, AddressList(), now, kTTL); | 238 cache.Set(key2, entry, now, kTTL); |
234 EXPECT_TRUE(cache.Lookup(key2, now)); | 239 EXPECT_TRUE(cache.Lookup(key2, now)); |
235 EXPECT_EQ(2U, cache.size()); | 240 EXPECT_EQ(2U, cache.size()); |
236 | 241 |
237 // Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0. | 242 // Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0. |
238 EXPECT_FALSE(cache.Lookup(key3, now)); | 243 EXPECT_FALSE(cache.Lookup(key3, now)); |
239 cache.Set(key3, OK, AddressList(), now, kTTL); | 244 cache.Set(key3, entry, now, kTTL); |
240 EXPECT_TRUE(cache.Lookup(key3, now)); | 245 EXPECT_TRUE(cache.Lookup(key3, now)); |
241 EXPECT_EQ(3U, cache.size()); | 246 EXPECT_EQ(3U, cache.size()); |
242 | 247 |
243 // Even though the hostnames were the same, we should have two unique | 248 // Even though the hostnames were the same, we should have two unique |
244 // entries (because the HostResolverFlags differ). | 249 // entries (because the HostResolverFlags differ). |
245 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); | 250 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); |
246 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now)); | 251 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now)); |
247 EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now)); | 252 EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now)); |
248 } | 253 } |
249 | 254 |
250 TEST(HostCacheTest, NoCache) { | 255 TEST(HostCacheTest, NoCache) { |
251 // Disable caching. | 256 // Disable caching. |
252 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 257 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
253 | 258 |
254 HostCache cache(0); | 259 HostCache cache(0); |
255 EXPECT_TRUE(cache.caching_is_disabled()); | 260 EXPECT_TRUE(cache.caching_is_disabled()); |
256 | 261 |
257 // Set t=0. | 262 // Set t=0. |
258 base::TimeTicks now; | 263 base::TimeTicks now; |
259 | 264 |
| 265 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 266 |
260 // Lookup and Set should have no effect. | 267 // Lookup and Set should have no effect. |
261 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); | 268 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); |
262 cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL); | 269 cache.Set(Key("foobar.com"), entry, now, kTTL); |
263 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); | 270 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); |
264 | 271 |
265 EXPECT_EQ(0U, cache.size()); | 272 EXPECT_EQ(0U, cache.size()); |
266 } | 273 } |
267 | 274 |
268 TEST(HostCacheTest, Clear) { | 275 TEST(HostCacheTest, Clear) { |
269 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 276 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
270 | 277 |
271 HostCache cache(kMaxCacheEntries); | 278 HostCache cache(kMaxCacheEntries); |
272 | 279 |
273 // Set t=0. | 280 // Set t=0. |
274 base::TimeTicks now; | 281 base::TimeTicks now; |
275 | 282 |
| 283 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 284 |
276 EXPECT_EQ(0u, cache.size()); | 285 EXPECT_EQ(0u, cache.size()); |
277 | 286 |
278 // Add three entries. | 287 // Add three entries. |
279 cache.Set(Key("foobar1.com"), OK, AddressList(), now, kTTL); | 288 cache.Set(Key("foobar1.com"), entry, now, kTTL); |
280 cache.Set(Key("foobar2.com"), OK, AddressList(), now, kTTL); | 289 cache.Set(Key("foobar2.com"), entry, now, kTTL); |
281 cache.Set(Key("foobar3.com"), OK, AddressList(), now, kTTL); | 290 cache.Set(Key("foobar3.com"), entry, now, kTTL); |
282 | 291 |
283 EXPECT_EQ(3u, cache.size()); | 292 EXPECT_EQ(3u, cache.size()); |
284 | 293 |
285 cache.clear(); | 294 cache.clear(); |
286 | 295 |
287 EXPECT_EQ(0u, cache.size()); | 296 EXPECT_EQ(0u, cache.size()); |
288 } | 297 } |
289 | 298 |
290 // Tests the less than and equal operators for HostCache::Key work. | 299 // Tests the less than and equal operators for HostCache::Key work. |
291 TEST(HostCacheTest, KeyComparators) { | 300 TEST(HostCacheTest, KeyComparators) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 EXPECT_FALSE(key1 < key2); | 379 EXPECT_FALSE(key1 < key2); |
371 EXPECT_TRUE(key2 < key1); | 380 EXPECT_TRUE(key2 < key1); |
372 break; | 381 break; |
373 default: | 382 default: |
374 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 383 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
375 } | 384 } |
376 } | 385 } |
377 } | 386 } |
378 | 387 |
379 } // namespace net | 388 } // namespace net |
OLD | NEW |