host_cache_persistence_manager_unittest.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/cronet/host_cache_persistence_manager.h"
  5. #include "base/test/scoped_mock_time_message_loop_task_runner.h"
  6. #include "base/test/task_environment.h"
  7. #include "base/values.h"
  8. #include "components/prefs/pref_registry_simple.h"
  9. #include "components/prefs/testing_pref_service.h"
  10. #include "net/base/ip_endpoint.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/base/network_isolation_key.h"
  13. #include "net/dns/host_cache.h"
  14. #include "net/dns/public/dns_query_type.h"
  15. #include "net/dns/public/host_resolver_source.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace cronet {
  18. class HostCachePersistenceManagerTest : public testing::Test {
  19. protected:
  20. void SetUp() override {
  21. cache_ = net::HostCache::CreateDefaultCache();
  22. pref_service_ = std::make_unique<TestingPrefServiceSimple>();
  23. pref_service_->registry()->RegisterListPref(kPrefName);
  24. }
  25. void MakePersistenceManager(base::TimeDelta delay) {
  26. persistence_manager_ = std::make_unique<HostCachePersistenceManager>(
  27. cache_.get(), pref_service_.get(), kPrefName, delay, nullptr);
  28. }
  29. // Sets an entry in the HostCache in order to trigger a pref write. The
  30. // caller is responsible for making sure this is a change that will trigger
  31. // a write, and the HostCache's interaction with its PersistenceDelegate is
  32. // assumed to work (it's tested in net/dns/host_cache_unittest.cc).
  33. void WriteToCache(const std::string& host) {
  34. net::HostCache::Key key(host, net::DnsQueryType::UNSPECIFIED, 0,
  35. net::HostResolverSource::ANY,
  36. net::NetworkIsolationKey());
  37. net::HostCache::Entry entry(net::OK, /*ip_endpoints=*/{}, /*aliases=*/{},
  38. net::HostCache::Entry::SOURCE_UNKNOWN);
  39. cache_->Set(key, entry, base::TimeTicks::Now(), base::Seconds(1));
  40. }
  41. // Reads the current value of the pref from the TestingPrefServiceSimple
  42. // and deserializes it into a temporary new HostCache. Only checks the size,
  43. // not the full contents, since the tests in this file are only intended
  44. // to test that writes happen when they're supposed to, not serialization
  45. // correctness.
  46. void CheckPref(size_t expected_size) {
  47. const base::Value* value = pref_service_->GetUserPref(kPrefName);
  48. net::HostCache temp_cache(10);
  49. if (value)
  50. temp_cache.RestoreFromListValue(value->GetList());
  51. ASSERT_EQ(expected_size, temp_cache.size());
  52. }
  53. // Generates a temporary HostCache with a few entries and uses it to
  54. // initialize the value in prefs.
  55. void InitializePref() {
  56. net::HostCache temp_cache(10);
  57. net::HostCache::Key key1("1.test", net::DnsQueryType::UNSPECIFIED, 0,
  58. net::HostResolverSource::ANY,
  59. net::NetworkIsolationKey());
  60. net::HostCache::Key key2("2.test", net::DnsQueryType::UNSPECIFIED, 0,
  61. net::HostResolverSource::ANY,
  62. net::NetworkIsolationKey());
  63. net::HostCache::Key key3("3.test", net::DnsQueryType::UNSPECIFIED, 0,
  64. net::HostResolverSource::ANY,
  65. net::NetworkIsolationKey());
  66. net::HostCache::Entry entry(net::OK, /*ip_endpoints=*/{}, /*aliases=*/{},
  67. net::HostCache::Entry::SOURCE_UNKNOWN);
  68. temp_cache.Set(key1, entry, base::TimeTicks::Now(), base::Seconds(1));
  69. temp_cache.Set(key2, entry, base::TimeTicks::Now(), base::Seconds(1));
  70. temp_cache.Set(key3, entry, base::TimeTicks::Now(), base::Seconds(1));
  71. base::Value::List list;
  72. temp_cache.GetList(list, false /* include_stale */,
  73. net::HostCache::SerializationType::kRestorable);
  74. pref_service_->SetList(kPrefName, std::move(list));
  75. }
  76. static const char kPrefName[];
  77. base::test::TaskEnvironment task_environment_;
  78. base::ScopedMockTimeMessageLoopTaskRunner task_runner_;
  79. // The HostCache and PrefService have to outlive the
  80. // HostCachePersistenceManager.
  81. std::unique_ptr<net::HostCache> cache_;
  82. std::unique_ptr<TestingPrefServiceSimple> pref_service_;
  83. std::unique_ptr<HostCachePersistenceManager> persistence_manager_;
  84. };
  85. const char HostCachePersistenceManagerTest::kPrefName[] = "net.test";
  86. // Make a single change to the HostCache and make sure that it's written
  87. // when the timer expires. Then repeat.
  88. TEST_F(HostCachePersistenceManagerTest, SeparateWrites) {
  89. MakePersistenceManager(base::Seconds(60));
  90. WriteToCache("1.test");
  91. task_runner_->FastForwardBy(base::Seconds(59));
  92. CheckPref(0);
  93. task_runner_->FastForwardBy(base::Seconds(1));
  94. CheckPref(1);
  95. WriteToCache("2.test");
  96. task_runner_->FastForwardBy(base::Seconds(59));
  97. CheckPref(1);
  98. task_runner_->FastForwardBy(base::Seconds(1));
  99. CheckPref(2);
  100. }
  101. // Write to the HostCache multiple times and make sure that all changes
  102. // are written to prefs at the appropriate times.
  103. TEST_F(HostCachePersistenceManagerTest, MultipleWrites) {
  104. MakePersistenceManager(base::Seconds(300));
  105. WriteToCache("1.test");
  106. WriteToCache("2.test");
  107. task_runner_->FastForwardBy(base::Seconds(299));
  108. CheckPref(0);
  109. task_runner_->FastForwardBy(base::Seconds(1));
  110. CheckPref(2);
  111. WriteToCache("3.test");
  112. WriteToCache("4.test");
  113. task_runner_->FastForwardBy(base::Seconds(299));
  114. CheckPref(2);
  115. task_runner_->FastForwardBy(base::Seconds(1));
  116. CheckPref(4);
  117. }
  118. // Make changes to the HostCache at different times and ensure that the writes
  119. // to prefs are batched as expected.
  120. TEST_F(HostCachePersistenceManagerTest, BatchedWrites) {
  121. MakePersistenceManager(base::Milliseconds(100));
  122. WriteToCache("1.test");
  123. task_runner_->FastForwardBy(base::Milliseconds(30));
  124. WriteToCache("2.test");
  125. task_runner_->FastForwardBy(base::Milliseconds(30));
  126. WriteToCache("3.test");
  127. CheckPref(0);
  128. task_runner_->FastForwardBy(base::Milliseconds(40));
  129. CheckPref(3);
  130. // Add a delay in between batches.
  131. task_runner_->FastForwardBy(base::Milliseconds(50));
  132. WriteToCache("4.test");
  133. task_runner_->FastForwardBy(base::Milliseconds(30));
  134. WriteToCache("5.test");
  135. task_runner_->FastForwardBy(base::Milliseconds(30));
  136. WriteToCache("6.test");
  137. CheckPref(3);
  138. task_runner_->FastForwardBy(base::Milliseconds(40));
  139. CheckPref(6);
  140. }
  141. // Set the pref before the HostCachePersistenceManager is created, and make
  142. // sure it gets picked up by the HostCache.
  143. TEST_F(HostCachePersistenceManagerTest, InitAfterPrefs) {
  144. CheckPref(0);
  145. InitializePref();
  146. CheckPref(3);
  147. MakePersistenceManager(base::Seconds(1));
  148. task_runner_->RunUntilIdle();
  149. ASSERT_EQ(3u, cache_->size());
  150. }
  151. // Set the pref after the HostCachePersistenceManager is created, and make
  152. // sure it gets picked up by the HostCache.
  153. TEST_F(HostCachePersistenceManagerTest, InitBeforePrefs) {
  154. MakePersistenceManager(base::Seconds(1));
  155. ASSERT_EQ(0u, cache_->size());
  156. CheckPref(0);
  157. InitializePref();
  158. CheckPref(3);
  159. ASSERT_EQ(3u, cache_->size());
  160. }
  161. } // namespace cronet