| 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // the HostResolverFlags differ. | 212 // the HostResolverFlags differ. |
| 213 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { | 213 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { |
| 214 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 214 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| 215 | 215 |
| 216 HostCache cache(kMaxCacheEntries); | 216 HostCache cache(kMaxCacheEntries); |
| 217 | 217 |
| 218 // t=0. | 218 // t=0. |
| 219 base::TimeTicks now; | 219 base::TimeTicks now; |
| 220 | 220 |
| 221 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0); | 221 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0); |
| 222 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, | 222 HostCache::Key key2( |
| 223 HOST_RESOLVER_CANONNAME); | 223 "foobar.com", ADDRESS_FAMILY_IPV4, HOST_RESOLVER_CANONNAME); |
| 224 HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4, | 224 HostCache::Key key3( |
| 225 HOST_RESOLVER_LOOPBACK_ONLY); | 225 "foobar.com", ADDRESS_FAMILY_IPV4, HOST_RESOLVER_LOOPBACK_ONLY); |
| 226 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); | 226 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| 227 | 227 |
| 228 EXPECT_EQ(0U, cache.size()); | 228 EXPECT_EQ(0U, cache.size()); |
| 229 | 229 |
| 230 // Add an entry for ("foobar.com", IPV4, NONE) at t=0. | 230 // Add an entry for ("foobar.com", IPV4, NONE) at t=0. |
| 231 EXPECT_FALSE(cache.Lookup(key1, now)); | 231 EXPECT_FALSE(cache.Lookup(key1, now)); |
| 232 cache.Set(key1, entry, now, kTTL); | 232 cache.Set(key1, entry, now, kTTL); |
| 233 EXPECT_TRUE(cache.Lookup(key1, now)); | 233 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 234 EXPECT_EQ(1U, cache.size()); | 234 EXPECT_EQ(1U, cache.size()); |
| 235 | 235 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 258 | 258 |
| 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)); |
| 269 cache.Set(Key("foobar.com"), entry, now, kTTL); | 269 cache.Set(Key("foobar.com"), entry, now, kTTL); |
| 270 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); | 270 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); |
| 271 | 271 |
| 272 EXPECT_EQ(0U, cache.size()); | 272 EXPECT_EQ(0U, cache.size()); |
| 273 } | 273 } |
| 274 | 274 |
| 275 TEST(HostCacheTest, Clear) { | 275 TEST(HostCacheTest, Clear) { |
| 276 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 276 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| 277 | 277 |
| 278 HostCache cache(kMaxCacheEntries); | 278 HostCache cache(kMaxCacheEntries); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 302 // Inputs. | 302 // Inputs. |
| 303 HostCache::Key key1; | 303 HostCache::Key key1; |
| 304 HostCache::Key key2; | 304 HostCache::Key key2; |
| 305 | 305 |
| 306 // Expectation. | 306 // Expectation. |
| 307 // -1 means key1 is less than key2 | 307 // -1 means key1 is less than key2 |
| 308 // 0 means key1 equals key2 | 308 // 0 means key1 equals key2 |
| 309 // 1 means key1 is greater than key2 | 309 // 1 means key1 is greater than key2 |
| 310 int expected_comparison; | 310 int expected_comparison; |
| 311 } tests[] = { | 311 } tests[] = { |
| 312 { | 312 {HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), |
| 313 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 313 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), 0}, |
| 314 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 314 {HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), |
| 315 0 | 315 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), 1}, |
| 316 }, | 316 {HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), |
| 317 { | 317 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), -1}, |
| 318 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 318 {HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), |
| 319 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 319 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), -1}, |
| 320 1 | 320 {HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), |
| 321 }, | 321 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), 1}, |
| 322 { | 322 {HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), |
| 323 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 323 HostCache::Key("host2", ADDRESS_FAMILY_IPV4, 0), -1}, |
| 324 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 324 {HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), |
| 325 -1 | 325 HostCache::Key( |
| 326 }, | 326 "host1", ADDRESS_FAMILY_UNSPECIFIED, HOST_RESOLVER_CANONNAME), |
| 327 { | 327 -1}, |
| 328 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | 328 {HostCache::Key( |
| 329 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), | 329 "host1", ADDRESS_FAMILY_UNSPECIFIED, HOST_RESOLVER_CANONNAME), |
| 330 -1 | 330 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), 1}, |
| 331 }, | 331 {HostCache::Key( |
| 332 { | 332 "host1", ADDRESS_FAMILY_UNSPECIFIED, HOST_RESOLVER_CANONNAME), |
| 333 HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0), | 333 HostCache::Key( |
| 334 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0), | 334 "host2", ADDRESS_FAMILY_UNSPECIFIED, HOST_RESOLVER_CANONNAME), |
| 335 1 | 335 -1}, |
| 336 }, | 336 }; |
| 337 { | |
| 338 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | |
| 339 HostCache::Key("host2", ADDRESS_FAMILY_IPV4, 0), | |
| 340 -1 | |
| 341 }, | |
| 342 { | |
| 343 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | |
| 344 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | |
| 345 HOST_RESOLVER_CANONNAME), | |
| 346 -1 | |
| 347 }, | |
| 348 { | |
| 349 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | |
| 350 HOST_RESOLVER_CANONNAME), | |
| 351 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0), | |
| 352 1 | |
| 353 }, | |
| 354 { | |
| 355 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | |
| 356 HOST_RESOLVER_CANONNAME), | |
| 357 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, | |
| 358 HOST_RESOLVER_CANONNAME), | |
| 359 -1 | |
| 360 }, | |
| 361 }; | |
| 362 | 337 |
| 363 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 338 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 364 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]", i)); | 339 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]", i)); |
| 365 | 340 |
| 366 const HostCache::Key& key1 = tests[i].key1; | 341 const HostCache::Key& key1 = tests[i].key1; |
| 367 const HostCache::Key& key2 = tests[i].key2; | 342 const HostCache::Key& key2 = tests[i].key2; |
| 368 | 343 |
| 369 switch (tests[i].expected_comparison) { | 344 switch (tests[i].expected_comparison) { |
| 370 case -1: | 345 case -1: |
| 371 EXPECT_TRUE(key1 < key2); | 346 EXPECT_TRUE(key1 < key2); |
| 372 EXPECT_FALSE(key2 < key1); | 347 EXPECT_FALSE(key2 < key1); |
| 373 break; | 348 break; |
| 374 case 0: | 349 case 0: |
| 375 EXPECT_FALSE(key1 < key2); | 350 EXPECT_FALSE(key1 < key2); |
| 376 EXPECT_FALSE(key2 < key1); | 351 EXPECT_FALSE(key2 < key1); |
| 377 break; | 352 break; |
| 378 case 1: | 353 case 1: |
| 379 EXPECT_FALSE(key1 < key2); | 354 EXPECT_FALSE(key1 < key2); |
| 380 EXPECT_TRUE(key2 < key1); | 355 EXPECT_TRUE(key2 < key1); |
| 381 break; | 356 break; |
| 382 default: | 357 default: |
| 383 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 358 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
| 384 } | 359 } |
| 385 } | 360 } |
| 386 } | 361 } |
| 387 | 362 |
| 388 } // namespace net | 363 } // namespace net |
| OLD | NEW |