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

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

Issue 2791653002: Serialize and deserialize HostCache for persistence (Closed)
Patch Set: fix warning 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
« no previous file with comments | « net/dns/host_cache.cc ('k') | net/log/net_log_util.cc » ('j') | 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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/format_macros.h" 11 #include "base/format_macros.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/values.h"
15 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace net { 19 namespace net {
19 20
20 namespace { 21 namespace {
21 22
22 const int kMaxCacheEntries = 10; 23 const int kMaxCacheEntries = 10;
23 24
24 // Builds a key for |hostname|, defaulting the address family to unspecified. 25 // Builds a key for |hostname|, defaulting the address family to unspecified.
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 case 1: 634 case 1:
634 EXPECT_FALSE(key1 < key2); 635 EXPECT_FALSE(key1 < key2);
635 EXPECT_TRUE(key2 < key1); 636 EXPECT_TRUE(key2 < key1);
636 break; 637 break;
637 default: 638 default:
638 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; 639 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
639 } 640 }
640 } 641 }
641 } 642 }
642 643
644 TEST(HostCacheTest, SerializeAndDeserialize) {
645 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
646
647 HostCache cache(kMaxCacheEntries);
648
649 // Start at t=0.
650 base::TimeTicks now;
651
652 HostCache::Key key1 = Key("foobar.com");
653 HostCache::Key key2 = Key("foobar2.com");
654 HostCache::Key key3 = Key("foobar3.com");
655 HostCache::Key key4 = Key("foobar4.com");
656
657 IPAddress address_ipv4(1, 2, 3, 4);
658 IPAddress address_ipv6(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
659 IPEndPoint endpoint_ipv4(address_ipv4, 0);
660 IPEndPoint endpoint_ipv6(address_ipv6, 0);
661
662 HostCache::Entry entry1 = HostCache::Entry(OK, AddressList(endpoint_ipv4));
663 AddressList addresses2 = AddressList(endpoint_ipv6);
664 addresses2.push_back(endpoint_ipv4);
665 HostCache::Entry entry2 = HostCache::Entry(OK, addresses2);
666 HostCache::Entry entry3 = HostCache::Entry(OK, AddressList(endpoint_ipv6));
667 HostCache::Entry entry4 = HostCache::Entry(OK, AddressList(endpoint_ipv4));
668
669 EXPECT_EQ(0u, cache.size());
670
671 // Add an entry for "foobar.com" at t=0.
672 EXPECT_FALSE(cache.Lookup(key1, now));
673 cache.Set(key1, entry1, now, kTTL);
674 EXPECT_TRUE(cache.Lookup(key1, now));
675 EXPECT_TRUE(cache.Lookup(key1, now)->error() == entry1.error());
676
677 EXPECT_EQ(1u, cache.size());
678
679 // Advance to t=5.
680 now += base::TimeDelta::FromSeconds(5);
681
682 // Add entries for "foobar2.com" and "foobar3.com" at t=5.
683 EXPECT_FALSE(cache.Lookup(key2, now));
684 cache.Set(key2, entry2, now, kTTL);
685 EXPECT_TRUE(cache.Lookup(key2, now));
686 EXPECT_EQ(2u, cache.size());
687
688 EXPECT_FALSE(cache.Lookup(key3, now));
689 cache.Set(key3, entry3, now, kTTL);
690 EXPECT_TRUE(cache.Lookup(key3, now));
691 EXPECT_EQ(3u, cache.size());
692
693 // Advance to t=12, ansd serialize the cache.
694 now += base::TimeDelta::FromSeconds(7);
695
696 std::unique_ptr<base::ListValue> serialized_cache =
697 cache.GetAsListValue(/*include_staleness=*/false);
698 HostCache restored_cache(kMaxCacheEntries);
699
700 // Add entries for "foobar3.com" and "foobar4.com" to the cache before
701 // restoring it. The "foobar3.com" result is different from the original.
702 EXPECT_FALSE(restored_cache.Lookup(key3, now));
703 restored_cache.Set(key3, entry1, now, kTTL);
704 EXPECT_TRUE(restored_cache.Lookup(key3, now));
705 EXPECT_EQ(1u, restored_cache.size());
706
707 EXPECT_FALSE(restored_cache.Lookup(key4, now));
708 restored_cache.Set(key4, entry4, now, kTTL);
709 EXPECT_TRUE(restored_cache.Lookup(key4, now));
710 EXPECT_EQ(2u, restored_cache.size());
711
712 restored_cache.RestoreFromListValue(*serialized_cache);
713
714 HostCache::EntryStaleness stale;
715
716 // The "foobar.com" entry is stale due to both network changes and expiration
717 // time.
718 EXPECT_FALSE(restored_cache.Lookup(key1, now));
719 const HostCache::Entry* result1 =
720 restored_cache.LookupStale(key1, now, &stale);
721 EXPECT_TRUE(result1);
722 EXPECT_EQ(1u, result1->addresses().size());
723 EXPECT_EQ(address_ipv4, result1->addresses().front().address());
724 EXPECT_EQ(1, stale.network_changes);
725 // Time to TimeTicks conversion is fuzzy, so just check that expected and
726 // actual expiration times are close.
727 EXPECT_GT(base::TimeDelta::FromMilliseconds(1),
728 (base::TimeDelta::FromSeconds(2) - stale.expired_by).magnitude());
729
730 // The "foobar2.com" entry is stale only due to network changes.
731 EXPECT_FALSE(restored_cache.Lookup(key2, now));
732 const HostCache::Entry* result2 =
733 restored_cache.LookupStale(key2, now, &stale);
734 EXPECT_TRUE(result2);
735 EXPECT_EQ(2u, result2->addresses().size());
736 EXPECT_EQ(address_ipv6, result2->addresses().front().address());
737 EXPECT_EQ(address_ipv4, result2->addresses().back().address());
738 EXPECT_EQ(1, stale.network_changes);
739 EXPECT_GT(base::TimeDelta::FromMilliseconds(1),
740 (base::TimeDelta::FromSeconds(-3) - stale.expired_by).magnitude());
741
742 // The "foobar3.com" entry is the new one, not the restored one.
743 const HostCache::Entry* result3 = restored_cache.Lookup(key3, now);
744 EXPECT_TRUE(result3);
745 EXPECT_EQ(1u, result3->addresses().size());
746 EXPECT_EQ(address_ipv4, result3->addresses().front().address());
747
748 // The "foobar4.com" entry is still present and usable.
749 const HostCache::Entry* result4 = restored_cache.Lookup(key4, now);
750 EXPECT_TRUE(result4);
751 EXPECT_EQ(1u, result4->addresses().size());
752 EXPECT_EQ(address_ipv4, result4->addresses().front().address());
753 }
754
643 } // namespace net 755 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/host_cache.cc ('k') | net/log/net_log_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698