| OLD | NEW | 
|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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-inl.h" | 8 #include "base/stl_util-inl.h" | 
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" | 
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" | 
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" | 
| 12 | 12 | 
| 13 namespace net { | 13 namespace net { | 
| 14 | 14 | 
| 15 namespace { | 15 namespace { | 
| 16 const int kMaxCacheEntries = 10; | 16 const int kMaxCacheEntries = 10; | 
| 17 | 17 | 
| 18 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); | 18 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); | 
| 19 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); | 19 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); | 
| 20 | 20 | 
| 21 // Builds a key for |hostname|, defaulting the address family to unspecified. | 21 // Builds a key for |hostname|, defaulting the address family to unspecified. | 
| 22 HostCache::Key Key(const std::string& hostname) { | 22 HostCache::Key Key(const std::string& hostname) { | 
| 23   return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED); | 23   return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0); | 
| 24 } | 24 } | 
| 25 | 25 | 
| 26 }  // namespace | 26 }  // namespace | 
| 27 | 27 | 
| 28 TEST(HostCacheTest, Basic) { | 28 TEST(HostCacheTest, Basic) { | 
| 29   HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); | 29   HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); | 
| 30 | 30 | 
| 31   // Start at t=0. | 31   // Start at t=0. | 
| 32   base::TimeTicks now; | 32   base::TimeTicks now; | 
| 33 | 33 | 
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 269 } | 269 } | 
| 270 | 270 | 
| 271 // Tests that the same hostname can be duplicated in the cache, so long as | 271 // Tests that the same hostname can be duplicated in the cache, so long as | 
| 272 // the address family differs. | 272 // the address family differs. | 
| 273 TEST(HostCacheTest, AddressFamilyIsPartOfKey) { | 273 TEST(HostCacheTest, AddressFamilyIsPartOfKey) { | 
| 274   HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); | 274   HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); | 
| 275 | 275 | 
| 276   // t=0. | 276   // t=0. | 
| 277   base::TimeTicks now; | 277   base::TimeTicks now; | 
| 278 | 278 | 
| 279   HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED); | 279   HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0); | 
| 280   HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4); | 280   HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0); | 
| 281 | 281 | 
| 282   const HostCache::Entry* entry1 = NULL;  // Entry for key1 | 282   const HostCache::Entry* entry1 = NULL;  // Entry for key1 | 
| 283   const HostCache::Entry* entry2 = NULL;  // Entry for key2 | 283   const HostCache::Entry* entry2 = NULL;  // Entry for key2 | 
| 284 | 284 | 
| 285   EXPECT_EQ(0U, cache.size()); | 285   EXPECT_EQ(0U, cache.size()); | 
| 286 | 286 | 
| 287   // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. | 287   // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. | 
| 288   EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); | 288   EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); | 
| 289   cache.Set(key1, OK, AddressList(), now); | 289   cache.Set(key1, OK, AddressList(), now); | 
| 290   entry1 = cache.Lookup(key1, base::TimeTicks()); | 290   entry1 = cache.Lookup(key1, base::TimeTicks()); | 
| 291   EXPECT_FALSE(entry1 == NULL); | 291   EXPECT_FALSE(entry1 == NULL); | 
| 292   EXPECT_EQ(1U, cache.size()); | 292   EXPECT_EQ(1U, cache.size()); | 
| 293 | 293 | 
| 294   // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. | 294   // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. | 
| 295   EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); | 295   EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); | 
| 296   cache.Set(key2, OK, AddressList(), now); | 296   cache.Set(key2, OK, AddressList(), now); | 
| 297   entry2 = cache.Lookup(key2, base::TimeTicks()); | 297   entry2 = cache.Lookup(key2, base::TimeTicks()); | 
| 298   EXPECT_FALSE(entry2 == NULL); | 298   EXPECT_FALSE(entry2 == NULL); | 
| 299   EXPECT_EQ(2U, cache.size()); | 299   EXPECT_EQ(2U, cache.size()); | 
| 300 | 300 | 
| 301   // Even though the hostnames were the same, we should have two unique | 301   // Even though the hostnames were the same, we should have two unique | 
| 302   // entries (because the address families differ). | 302   // entries (because the address families differ). | 
| 303   EXPECT_NE(entry1, entry2); | 303   EXPECT_NE(entry1, entry2); | 
| 304 } | 304 } | 
| 305 | 305 | 
|  | 306 // Tests that the same hostname can be duplicated in the cache, so long as | 
|  | 307 // the HostResolverFlags differ. | 
|  | 308 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { | 
|  | 309   HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); | 
|  | 310 | 
|  | 311   // t=0. | 
|  | 312   base::TimeTicks now; | 
|  | 313 | 
|  | 314   HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0); | 
|  | 315   HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, | 
|  | 316                       HOST_RESOLVER_FLAGS_CANONNAME); | 
|  | 317 | 
|  | 318   const HostCache::Entry* entry1 = NULL;  // Entry for key1 | 
|  | 319   const HostCache::Entry* entry2 = NULL;  // Entry for key2 | 
|  | 320 | 
|  | 321   EXPECT_EQ(0U, cache.size()); | 
|  | 322 | 
|  | 323   // Add an entry for ("foobar.com", IPV4, NONE) at t=0. | 
|  | 324   EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); | 
|  | 325   cache.Set(key1, OK, AddressList(), now); | 
|  | 326   entry1 = cache.Lookup(key1, base::TimeTicks()); | 
|  | 327   EXPECT_FALSE(entry1 == NULL); | 
|  | 328   EXPECT_EQ(1U, cache.size()); | 
|  | 329 | 
|  | 330   // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0. | 
|  | 331   EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); | 
|  | 332   cache.Set(key2, OK, AddressList(), now); | 
|  | 333   entry2 = cache.Lookup(key2, base::TimeTicks()); | 
|  | 334   EXPECT_FALSE(entry2 == NULL); | 
|  | 335   EXPECT_EQ(2U, cache.size()); | 
|  | 336 | 
|  | 337   // Even though the hostnames were the same, we should have two unique | 
|  | 338   // entries (because the HostResolverFlags differ). | 
|  | 339   EXPECT_NE(entry1, entry2); | 
|  | 340 } | 
|  | 341 | 
| 306 TEST(HostCacheTest, NoCache) { | 342 TEST(HostCacheTest, NoCache) { | 
| 307   // Disable caching. | 343   // Disable caching. | 
| 308   HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); | 344   HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); | 
| 309   EXPECT_TRUE(cache.caching_is_disabled()); | 345   EXPECT_TRUE(cache.caching_is_disabled()); | 
| 310 | 346 | 
| 311   // Set t=0. | 347   // Set t=0. | 
| 312   base::TimeTicks now; | 348   base::TimeTicks now; | 
| 313 | 349 | 
| 314   // Lookup and Set should have no effect. | 350   // Lookup and Set should have no effect. | 
| 315   EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); | 351   EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); | 
| (...skipping 30 matching lines...) Expand all  Loading... | 
| 346     HostCache::Key key1; | 382     HostCache::Key key1; | 
| 347     HostCache::Key key2; | 383     HostCache::Key key2; | 
| 348 | 384 | 
| 349     // Expectation. | 385     // Expectation. | 
| 350     //   -1 means key1 is less than key2 | 386     //   -1 means key1 is less than key2 | 
| 351     //    0 means key1 equals key2 | 387     //    0 means key1 equals key2 | 
| 352     //    1 means key1 is greater than key2 | 388     //    1 means key1 is greater than key2 | 
| 353     int expected_comparison; | 389     int expected_comparison; | 
| 354   } tests[] = { | 390   } tests[] = { | 
| 355     { | 391     { | 
| 356       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), | 392       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 357       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), | 393       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 358       0 | 394       0 | 
| 359     }, | 395     }, | 
| 360     { | 396     { | 
| 361       HostCache::Key("host1", ADDRESS_FAMILY_IPV4), | 397       HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 
| 362       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), | 398       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 363       1 | 399       1 | 
| 364     }, | 400     }, | 
| 365     { | 401     { | 
| 366       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), | 402       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 367       HostCache::Key("host1", ADDRESS_FAMILY_IPV4), | 403       HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 
| 368       -1 | 404       -1 | 
| 369     }, | 405     }, | 
| 370     { | 406     { | 
| 371       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), | 407       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 372       HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), | 408       HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 373       -1 | 409       -1 | 
| 374     }, | 410     }, | 
| 375     { | 411     { | 
| 376       HostCache::Key("host1", ADDRESS_FAMILY_IPV4), | 412       HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 
| 377       HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), | 413       HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 378       1 | 414       1 | 
| 379     }, | 415     }, | 
| 380     { | 416     { | 
| 381       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), | 417       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
| 382       HostCache::Key("host2", ADDRESS_FAMILY_IPV4), | 418       HostCache::Key("host2", ADDRESS_FAMILY_IPV4, 0), | 
|  | 419       -1 | 
|  | 420     }, | 
|  | 421         { | 
|  | 422       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
|  | 423       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | 
|  | 424                      HOST_RESOLVER_FLAGS_CANONNAME), | 
|  | 425       -1 | 
|  | 426     }, | 
|  | 427     { | 
|  | 428       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | 
|  | 429                      HOST_RESOLVER_FLAGS_CANONNAME), | 
|  | 430       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 
|  | 431       1 | 
|  | 432     }, | 
|  | 433     { | 
|  | 434       HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | 
|  | 435                      HOST_RESOLVER_FLAGS_CANONNAME), | 
|  | 436       HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, | 
|  | 437                      HOST_RESOLVER_FLAGS_CANONNAME), | 
| 383       -1 | 438       -1 | 
| 384     }, | 439     }, | 
| 385   }; | 440   }; | 
| 386 | 441 | 
| 387   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 442   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 
| 388     SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]", i)); | 443     SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]", i)); | 
| 389 | 444 | 
| 390     const HostCache::Key& key1 = tests[i].key1; | 445     const HostCache::Key& key1 = tests[i].key1; | 
| 391     const HostCache::Key& key2 = tests[i].key2; | 446     const HostCache::Key& key2 = tests[i].key2; | 
| 392 | 447 | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 406         EXPECT_TRUE(key2 < key1); | 461         EXPECT_TRUE(key2 < key1); | 
| 407         EXPECT_FALSE(key2 == key1); | 462         EXPECT_FALSE(key2 == key1); | 
| 408         break; | 463         break; | 
| 409       default: | 464       default: | 
| 410         FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 465         FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 
| 411     } | 466     } | 
| 412   } | 467   } | 
| 413 } | 468 } | 
| 414 | 469 | 
| 415 }  // namespace net | 470 }  // namespace net | 
| OLD | NEW | 
|---|