Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1223)

Side by Side Diff: net/dns/host_cache_unittest.cc

Issue 2943143002: Add PersistenceDelegate to HostCache (Closed)
Patch Set: check for change on clear Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/dns/host_cache.cc ('K') | « net/dns/host_cache.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_++; }
mef 2017/06/20 16:18:12 nit: I think ++num_changes_ is preferred.
mgersh 2017/06/20 17:16:29 The rule is technically only for iterators, but su
38
39 int num_changes() { return num_changes_; }
mef 2017/06/20 16:18:12 nit: const
mgersh 2017/06/20 17:16:29 Done.
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
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
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
787 // Start at t=0.
788 base::TimeTicks now;
789 EXPECT_EQ(0u, cache.size());
790
791 // Add two entries at t=0.
792 EXPECT_FALSE(cache.Lookup(key1, now));
793 cache.Set(key1, entry1, now, kTTL);
794 EXPECT_TRUE(cache.Lookup(key1, now));
795 EXPECT_EQ(1u, cache.size());
796 EXPECT_EQ(1, delegate.num_changes());
797
798 EXPECT_FALSE(cache.Lookup(key2, now));
799 cache.Set(key2, entry1, now, kTTL);
800 EXPECT_TRUE(cache.Lookup(key2, now));
801 EXPECT_EQ(2u, cache.size());
802 EXPECT_EQ(2, delegate.num_changes());
803
804 // Advance to t=5.
805 now += base::TimeDelta::FromSeconds(5);
806
807 // Changes that shouldn't trigger a write:
808 // Add an entry for "foobar.com" with different expiration time.
809 EXPECT_TRUE(cache.Lookup(key1, now));
810 cache.Set(key1, entry1, now, kTTL);
811 EXPECT_TRUE(cache.Lookup(key1, now));
812 EXPECT_EQ(2u, cache.size());
813 EXPECT_EQ(2, delegate.num_changes());
814
815 // Add an entry for "foobar.com" with different TTL.
816 EXPECT_TRUE(cache.Lookup(key1, now));
817 cache.Set(key1, entry1, now, kTTL - base::TimeDelta::FromSeconds(5));
818 EXPECT_TRUE(cache.Lookup(key1, now));
819 EXPECT_EQ(2u, cache.size());
820 EXPECT_EQ(2, delegate.num_changes());
821
822 // Changes that should trigger a write:
823 // Add an entry for "foobar.com" with different address list.
824 EXPECT_TRUE(cache.Lookup(key1, now));
825 cache.Set(key1, entry2, now, kTTL);
826 EXPECT_TRUE(cache.Lookup(key1, now));
827 EXPECT_EQ(2u, cache.size());
828 EXPECT_EQ(3, delegate.num_changes());
829
830 // Add an entry for "foobar.com" with different error.
831 EXPECT_TRUE(cache.Lookup(key1, now));
832 cache.Set(key1, entry3, now, kTTL);
833 EXPECT_TRUE(cache.Lookup(key1, now));
834 EXPECT_EQ(2u, cache.size());
835 EXPECT_EQ(4, delegate.num_changes());
836 }
837
755 } // namespace net 838 } // namespace net
OLDNEW
« net/dns/host_cache.cc ('K') | « net/dns/host_cache.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698