| OLD | NEW |
| 1 // Copyright (c) 2010 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/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, 0); | 23 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED); |
| 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, 0); | 279 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED); |
| 280 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0); | 280 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4); |
| 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 | |
| 342 TEST(HostCacheTest, NoCache) { | 306 TEST(HostCacheTest, NoCache) { |
| 343 // Disable caching. | 307 // Disable caching. |
| 344 HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); | 308 HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); |
| 345 EXPECT_TRUE(cache.caching_is_disabled()); | 309 EXPECT_TRUE(cache.caching_is_disabled()); |
| 346 | 310 |
| 347 // Set t=0. | 311 // Set t=0. |
| 348 base::TimeTicks now; | 312 base::TimeTicks now; |
| 349 | 313 |
| 350 // Lookup and Set should have no effect. | 314 // Lookup and Set should have no effect. |
| 351 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); | 315 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 382 HostCache::Key key1; | 346 HostCache::Key key1; |
| 383 HostCache::Key key2; | 347 HostCache::Key key2; |
| 384 | 348 |
| 385 // Expectation. | 349 // Expectation. |
| 386 // -1 means key1 is less than key2 | 350 // -1 means key1 is less than key2 |
| 387 // 0 means key1 equals key2 | 351 // 0 means key1 equals key2 |
| 388 // 1 means key1 is greater than key2 | 352 // 1 means key1 is greater than key2 |
| 389 int expected_comparison; | 353 int expected_comparison; |
| 390 } tests[] = { | 354 } tests[] = { |
| 391 { | 355 { |
| 392 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 356 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 393 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 357 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 394 0 | 358 0 |
| 395 }, | 359 }, |
| 396 { | 360 { |
| 397 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 361 HostCache::Key("host1", ADDRESS_FAMILY_IPV4), |
| 398 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 362 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 399 1 | 363 1 |
| 400 }, | 364 }, |
| 401 { | 365 { |
| 402 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 366 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 403 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 367 HostCache::Key("host1", ADDRESS_FAMILY_IPV4), |
| 404 -1 | 368 -1 |
| 405 }, | 369 }, |
| 406 { | 370 { |
| 407 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 371 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 408 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), | 372 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), |
| 409 -1 | 373 -1 |
| 410 }, | 374 }, |
| 411 { | 375 { |
| 412 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 376 HostCache::Key("host1", ADDRESS_FAMILY_IPV4), |
| 413 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), | 377 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), |
| 414 1 | 378 1 |
| 415 }, | 379 }, |
| 416 { | 380 { |
| 417 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 381 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 418 HostCache::Key("host2", ADDRESS_FAMILY_IPV4, 0), | 382 HostCache::Key("host2", ADDRESS_FAMILY_IPV4), |
| 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), | |
| 438 -1 | 383 -1 |
| 439 }, | 384 }, |
| 440 }; | 385 }; |
| 441 | 386 |
| 442 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 387 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 443 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]", i)); | 388 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]", i)); |
| 444 | 389 |
| 445 const HostCache::Key& key1 = tests[i].key1; | 390 const HostCache::Key& key1 = tests[i].key1; |
| 446 const HostCache::Key& key2 = tests[i].key2; | 391 const HostCache::Key& key2 = tests[i].key2; |
| 447 | 392 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 461 EXPECT_TRUE(key2 < key1); | 406 EXPECT_TRUE(key2 < key1); |
| 462 EXPECT_FALSE(key2 == key1); | 407 EXPECT_FALSE(key2 == key1); |
| 463 break; | 408 break; |
| 464 default: | 409 default: |
| 465 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 410 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
| 466 } | 411 } |
| 467 } | 412 } |
| 468 } | 413 } |
| 469 | 414 |
| 470 } // namespace net | 415 } // namespace net |
| OLD | NEW |