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 <string> | 7 #include <string> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const int kMaxCacheEntries = 10; | 24 const int kMaxCacheEntries = 10; |
24 | 25 |
25 // Builds a key for |hostname|, defaulting the address family to unspecified. | 26 // Builds a key for |hostname|, defaulting the address family to unspecified. |
26 HostCache::Key Key(const std::string& hostname) { | 27 HostCache::Key Key(const std::string& hostname) { |
27 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0); | 28 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0); |
28 } | 29 } |
29 | 30 |
30 bool FoobarIndexIsOdd(const std::string& foobarx_com) { | 31 bool FoobarIndexIsOdd(const std::string& foobarx_com) { |
31 return (foobarx_com[6] - '0') % 2 == 1; | 32 return (foobarx_com[6] - '0') % 2 == 1; |
32 } | 33 } |
33 | 34 |
| 35 class MockPersistenceDelegate : public HostCache::PersistenceDelegate { |
| 36 public: |
| 37 void ScheduleWrite() override { ++num_changes_; } |
| 38 |
| 39 int num_changes() const { return num_changes_; } |
| 40 |
| 41 private: |
| 42 int num_changes_ = 0; |
| 43 }; |
| 44 |
34 } // namespace | 45 } // namespace |
35 | 46 |
36 TEST(HostCacheTest, Basic) { | 47 TEST(HostCacheTest, Basic) { |
37 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 48 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
38 | 49 |
39 HostCache cache(kMaxCacheEntries); | 50 HostCache cache(kMaxCacheEntries); |
40 | 51 |
41 // Start at t=0. | 52 // Start at t=0. |
42 base::TimeTicks now; | 53 base::TimeTicks now; |
43 | 54 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 // Disable caching. | 278 // Disable caching. |
268 HostCache cache(0); | 279 HostCache cache(0); |
269 EXPECT_TRUE(cache.caching_is_disabled()); | 280 EXPECT_TRUE(cache.caching_is_disabled()); |
270 | 281 |
271 // Set t=0. | 282 // Set t=0. |
272 base::TimeTicks now; | 283 base::TimeTicks now; |
273 | 284 |
274 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); | 285 HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
275 | 286 |
276 // Lookup and Set should have no effect. | 287 // Lookup and Set should have no effect. |
277 EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); | 288 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); |
278 cache.Set(Key("foobar.com"), entry, now, kTTL); | 289 cache.Set(Key("foobar.com"), entry, now, kTTL); |
279 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); | 290 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); |
280 | 291 |
281 EXPECT_EQ(0U, cache.size()); | 292 EXPECT_EQ(0U, cache.size()); |
282 } | 293 } |
283 | 294 |
284 TEST(HostCacheTest, Clear) { | 295 TEST(HostCacheTest, Clear) { |
285 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); | 296 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
286 | 297 |
287 HostCache cache(kMaxCacheEntries); | 298 HostCache cache(kMaxCacheEntries); |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 EXPECT_EQ(1u, result3->addresses().size()); | 756 EXPECT_EQ(1u, result3->addresses().size()); |
746 EXPECT_EQ(address_ipv4, result3->addresses().front().address()); | 757 EXPECT_EQ(address_ipv4, result3->addresses().front().address()); |
747 | 758 |
748 // The "foobar4.com" entry is still present and usable. | 759 // The "foobar4.com" entry is still present and usable. |
749 const HostCache::Entry* result4 = restored_cache.Lookup(key4, now); | 760 const HostCache::Entry* result4 = restored_cache.Lookup(key4, now); |
750 EXPECT_TRUE(result4); | 761 EXPECT_TRUE(result4); |
751 EXPECT_EQ(1u, result4->addresses().size()); | 762 EXPECT_EQ(1u, result4->addresses().size()); |
752 EXPECT_EQ(address_ipv4, result4->addresses().front().address()); | 763 EXPECT_EQ(address_ipv4, result4->addresses().front().address()); |
753 } | 764 } |
754 | 765 |
| 766 TEST(HostCacheTest, PersistenceDelegate) { |
| 767 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| 768 HostCache cache(kMaxCacheEntries); |
| 769 MockPersistenceDelegate delegate; |
| 770 cache.set_persistence_delegate(&delegate); |
| 771 |
| 772 HostCache::Key key1 = Key("foobar.com"); |
| 773 HostCache::Key key2 = Key("foobar2.com"); |
| 774 |
| 775 IPAddress address_ipv4(1, 2, 3, 4); |
| 776 IPAddress address_ipv6(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 777 IPEndPoint endpoint_ipv4(address_ipv4, 0); |
| 778 IPEndPoint endpoint_ipv6(address_ipv6, 0); |
| 779 |
| 780 HostCache::Entry entry1 = HostCache::Entry(OK, AddressList(endpoint_ipv4)); |
| 781 AddressList addresses2 = AddressList(endpoint_ipv6); |
| 782 addresses2.push_back(endpoint_ipv4); |
| 783 HostCache::Entry entry2 = HostCache::Entry(OK, addresses2); |
| 784 HostCache::Entry entry3 = |
| 785 HostCache::Entry(ERR_NAME_NOT_RESOLVED, AddressList()); |
| 786 HostCache::Entry entry4 = HostCache::Entry(OK, AddressList()); |
| 787 |
| 788 // Start at t=0. |
| 789 base::TimeTicks now; |
| 790 EXPECT_EQ(0u, cache.size()); |
| 791 |
| 792 // Add two entries at t=0. |
| 793 EXPECT_FALSE(cache.Lookup(key1, now)); |
| 794 cache.Set(key1, entry1, now, kTTL); |
| 795 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 796 EXPECT_EQ(1u, cache.size()); |
| 797 EXPECT_EQ(1, delegate.num_changes()); |
| 798 |
| 799 EXPECT_FALSE(cache.Lookup(key2, now)); |
| 800 cache.Set(key2, entry3, now, kTTL); |
| 801 EXPECT_TRUE(cache.Lookup(key2, now)); |
| 802 EXPECT_EQ(2u, cache.size()); |
| 803 EXPECT_EQ(2, delegate.num_changes()); |
| 804 |
| 805 // Advance to t=5. |
| 806 now += base::TimeDelta::FromSeconds(5); |
| 807 |
| 808 // Changes that shouldn't trigger a write: |
| 809 // Add an entry for "foobar.com" with different expiration time. |
| 810 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 811 cache.Set(key1, entry1, now, kTTL); |
| 812 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 813 EXPECT_EQ(2u, cache.size()); |
| 814 EXPECT_EQ(2, delegate.num_changes()); |
| 815 |
| 816 // Add an entry for "foobar.com" with different TTL. |
| 817 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 818 cache.Set(key1, entry1, now, kTTL - base::TimeDelta::FromSeconds(5)); |
| 819 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 820 EXPECT_EQ(2u, cache.size()); |
| 821 EXPECT_EQ(2, delegate.num_changes()); |
| 822 |
| 823 // Changes that should trigger a write: |
| 824 // Add an entry for "foobar.com" with different address list. |
| 825 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 826 cache.Set(key1, entry2, now, kTTL); |
| 827 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 828 EXPECT_EQ(2u, cache.size()); |
| 829 EXPECT_EQ(3, delegate.num_changes()); |
| 830 |
| 831 // Add an entry for "foobar2.com" with different error. |
| 832 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 833 cache.Set(key2, entry4, now, kTTL); |
| 834 EXPECT_TRUE(cache.Lookup(key1, now)); |
| 835 EXPECT_EQ(2u, cache.size()); |
| 836 EXPECT_EQ(4, delegate.num_changes()); |
| 837 } |
| 838 |
755 } // namespace net | 839 } // namespace net |
OLD | NEW |