| 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/dns/host_cache.h" | 5 #include "net/dns/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/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 38 | 38 |
| 39 EXPECT_EQ(0U, cache.size()); | 39 EXPECT_EQ(0U, cache.size()); |
| 40 | 40 |
| 41 // Add an entry for "foobar.com" at t=0. | 41 // Add an entry for "foobar.com" at t=0. |
| 42 EXPECT_FALSE(cache.Lookup(key1, now)); | 42 EXPECT_FALSE(cache.Lookup(key1, now)); |
| 43 cache.Set(key1, entry, now, kTTL); | 43 cache.Set(key1, entry, now, kTTL); |
| 44 EXPECT_TRUE(cache.Lookup(key1, now)); | 44 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 45 EXPECT_TRUE(cache.Lookup(key1, now)->error == entry.error); | 45 EXPECT_TRUE(cache.Lookup(key1, now)->error() == entry.error()); |
| 46 | 46 |
| 47 EXPECT_EQ(1U, cache.size()); | 47 EXPECT_EQ(1U, cache.size()); |
| 48 | 48 |
| 49 // Advance to t=5. | 49 // Advance to t=5. |
| 50 now += base::TimeDelta::FromSeconds(5); | 50 now += base::TimeDelta::FromSeconds(5); |
| 51 | 51 |
| 52 // Add an entry for "foobar2.com" at t=5. | 52 // Add an entry for "foobar2.com" at t=5. |
| 53 EXPECT_FALSE(cache.Lookup(key2, now)); | 53 EXPECT_FALSE(cache.Lookup(key2, now)); |
| 54 cache.Set(key2, entry, now, kTTL); | 54 cache.Set(key2, entry, now, kTTL); |
| 55 EXPECT_TRUE(cache.Lookup(key2, now)); | 55 EXPECT_TRUE(cache.Lookup(key2, now)); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 EXPECT_EQ(3U, cache.size()); | 246 EXPECT_EQ(3U, cache.size()); |
| 247 | 247 |
| 248 // 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 |
| 249 // entries (because the HostResolverFlags differ). | 249 // entries (because the HostResolverFlags differ). |
| 250 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); | 250 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now)); |
| 251 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now)); | 251 EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now)); |
| 252 EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now)); | 252 EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now)); |
| 253 } | 253 } |
| 254 | 254 |
| 255 TEST(HostCacheTest, NoCache) { | 255 TEST(HostCacheTest, NoCache) { |
| 256 // Disable caching. | |
| 257 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 256 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| 258 | 257 |
| 258 // Disable caching. |
| 259 HostCache cache(0); | 259 HostCache cache(0); |
| 260 EXPECT_TRUE(cache.caching_is_disabled()); | 260 EXPECT_TRUE(cache.caching_is_disabled()); |
| 261 | 261 |
| 262 // Set t=0. | 262 // Set t=0. |
| 263 base::TimeTicks now; | 263 base::TimeTicks now; |
| 264 | 264 |
| 265 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); | 265 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 266 | 266 |
| 267 // Lookup and Set should have no effect. | 267 // Lookup and Set should have no effect. |
| 268 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); | 268 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 289 cache.Set(Key("foobar2.com"), entry, now, kTTL); | 289 cache.Set(Key("foobar2.com"), entry, now, kTTL); |
| 290 cache.Set(Key("foobar3.com"), entry, now, kTTL); | 290 cache.Set(Key("foobar3.com"), entry, now, kTTL); |
| 291 | 291 |
| 292 EXPECT_EQ(3u, cache.size()); | 292 EXPECT_EQ(3u, cache.size()); |
| 293 | 293 |
| 294 cache.clear(); | 294 cache.clear(); |
| 295 | 295 |
| 296 EXPECT_EQ(0u, cache.size()); | 296 EXPECT_EQ(0u, cache.size()); |
| 297 } | 297 } |
| 298 | 298 |
| 299 // Try to add too many entries to cache; it should evict the one with the oldest |
| 300 // expiration time. |
| 301 TEST(HostCacheTest, Evict) { |
| 302 HostCache cache(2); |
| 303 |
| 304 base::TimeTicks now; |
| 305 |
| 306 HostCache::Key key1 = Key("foobar.com"); |
| 307 HostCache::Key key2 = Key("foobar2.com"); |
| 308 HostCache::Key key3 = Key("foobar3.com"); |
| 309 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 310 |
| 311 EXPECT_EQ(0u, cache.size()); |
| 312 EXPECT_FALSE(cache.Lookup(key1, now)); |
| 313 EXPECT_FALSE(cache.Lookup(key2, now)); |
| 314 EXPECT_FALSE(cache.Lookup(key3, now)); |
| 315 |
| 316 // |key1| expires in 10 seconds, but |key2| in just 5. |
| 317 cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10)); |
| 318 cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5)); |
| 319 EXPECT_EQ(2u, cache.size()); |
| 320 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 321 EXPECT_TRUE(cache.Lookup(key2, now)); |
| 322 EXPECT_FALSE(cache.Lookup(key3, now)); |
| 323 |
| 324 // |key2| should be chosen for eviction, since it expires sooner. |
| 325 cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10)); |
| 326 EXPECT_EQ(2u, cache.size()); |
| 327 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 328 EXPECT_FALSE(cache.Lookup(key2, now)); |
| 329 EXPECT_TRUE(cache.Lookup(key3, now)); |
| 330 } |
| 331 |
| 332 // Try to retrieve stale entries from the cache. They should be returned by |
| 333 // |LookupStale()| but not |Lookup()|, with correct |EntryStaleness| data. |
| 334 TEST(HostCacheTest, Stale) { |
| 335 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| 336 |
| 337 HostCache cache(kMaxCacheEntries); |
| 338 |
| 339 // Start at t=0. |
| 340 base::TimeTicks now; |
| 341 HostCache::EntryStaleness stale; |
| 342 |
| 343 HostCache::Key key = Key("foobar.com"); |
| 344 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 345 |
| 346 EXPECT_EQ(0U, cache.size()); |
| 347 |
| 348 // Add an entry for "foobar.com" at t=0. |
| 349 EXPECT_FALSE(cache.Lookup(key, now)); |
| 350 EXPECT_FALSE(cache.LookupStale(key, now, &stale)); |
| 351 cache.Set(key, entry, now, kTTL); |
| 352 EXPECT_TRUE(cache.Lookup(key, now)); |
| 353 EXPECT_TRUE(cache.LookupStale(key, now, &stale)); |
| 354 EXPECT_FALSE(stale.is_stale()); |
| 355 EXPECT_EQ(0, stale.stale_hits); |
| 356 |
| 357 EXPECT_EQ(1U, cache.size()); |
| 358 |
| 359 // Advance to t=5. |
| 360 now += base::TimeDelta::FromSeconds(5); |
| 361 |
| 362 EXPECT_TRUE(cache.Lookup(key, now)); |
| 363 EXPECT_TRUE(cache.LookupStale(key, now, &stale)); |
| 364 EXPECT_FALSE(stale.is_stale()); |
| 365 EXPECT_EQ(0, stale.stale_hits); |
| 366 |
| 367 // Advance to t=15. |
| 368 now += base::TimeDelta::FromSeconds(10); |
| 369 |
| 370 EXPECT_FALSE(cache.Lookup(key, now)); |
| 371 EXPECT_TRUE(cache.LookupStale(key, now, &stale)); |
| 372 EXPECT_TRUE(stale.is_stale()); |
| 373 EXPECT_EQ(base::TimeDelta::FromSeconds(5), stale.expired_by); |
| 374 EXPECT_EQ(0, stale.network_changes); |
| 375 EXPECT_EQ(1, stale.stale_hits); |
| 376 |
| 377 // Advance to t=20. |
| 378 now += base::TimeDelta::FromSeconds(5); |
| 379 |
| 380 EXPECT_FALSE(cache.Lookup(key, now)); |
| 381 EXPECT_TRUE(cache.LookupStale(key, now, &stale)); |
| 382 EXPECT_TRUE(stale.is_stale()); |
| 383 EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by); |
| 384 EXPECT_EQ(0, stale.network_changes); |
| 385 EXPECT_EQ(2, stale.stale_hits); |
| 386 |
| 387 // Simulate network change. |
| 388 cache.OnNetworkChange(); |
| 389 |
| 390 EXPECT_FALSE(cache.Lookup(key, now)); |
| 391 EXPECT_TRUE(cache.LookupStale(key, now, &stale)); |
| 392 EXPECT_TRUE(stale.is_stale()); |
| 393 EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by); |
| 394 EXPECT_EQ(1, stale.network_changes); |
| 395 EXPECT_EQ(3, stale.stale_hits); |
| 396 } |
| 397 |
| 299 // Tests the less than and equal operators for HostCache::Key work. | 398 // Tests the less than and equal operators for HostCache::Key work. |
| 300 TEST(HostCacheTest, KeyComparators) { | 399 TEST(HostCacheTest, KeyComparators) { |
| 301 struct { | 400 struct { |
| 302 // Inputs. | 401 // Inputs. |
| 303 HostCache::Key key1; | 402 HostCache::Key key1; |
| 304 HostCache::Key key2; | 403 HostCache::Key key2; |
| 305 | 404 |
| 306 // Expectation. | 405 // Expectation. |
| 307 // -1 means key1 is less than key2 | 406 // -1 means key1 is less than key2 |
| 308 // 0 means key1 equals key2 | 407 // 0 means key1 equals key2 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 EXPECT_FALSE(key1 < key2); | 478 EXPECT_FALSE(key1 < key2); |
| 380 EXPECT_TRUE(key2 < key1); | 479 EXPECT_TRUE(key2 < key1); |
| 381 break; | 480 break; |
| 382 default: | 481 default: |
| 383 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 482 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
| 384 } | 483 } |
| 385 } | 484 } |
| 386 } | 485 } |
| 387 | 486 |
| 388 } // namespace net | 487 } // namespace net |
| OLD | NEW |