network_quality_estimator_util_unittest.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "net/nqe/network_quality_estimator_util.h"
  5. #include <memory>
  6. #include "base/test/scoped_feature_list.h"
  7. #include "base/test/task_environment.h"
  8. #include "build/build_config.h"
  9. #include "net/base/features.h"
  10. #include "net/base/host_port_pair.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/base/network_isolation_key.h"
  13. #include "net/base/schemeful_site.h"
  14. #include "net/base/test_completion_callback.h"
  15. #include "net/dns/context_host_resolver.h"
  16. #include "net/dns/host_resolver.h"
  17. #include "net/dns/mock_host_resolver.h"
  18. #include "net/log/net_log.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "third_party/abseil-cpp/absl/types/optional.h"
  21. #include "url/gurl.h"
  22. namespace net::nqe::internal {
  23. namespace {
  24. #if BUILDFLAG(IS_IOS)
  25. // Flaky on iOS: crbug.com/672917.
  26. #define MAYBE_ReservedHost DISABLED_ReservedHost
  27. #else
  28. #define MAYBE_ReservedHost ReservedHost
  29. #endif
  30. // Verify that the cached network qualities from the prefs are not used if the
  31. // reading of the network quality prefs is not enabled..
  32. TEST(NetworkQualityEstimatorUtilTest, MAYBE_ReservedHost) {
  33. base::test::TaskEnvironment task_environment;
  34. MockCachingHostResolver mock_host_resolver;
  35. // example1.com resolves to a private IP address.
  36. mock_host_resolver.rules()->AddRule("example1.com", "127.0.0.3");
  37. // example2.com resolves to a public IP address.
  38. mock_host_resolver.rules()->AddRule("example2.com", "27.0.0.3");
  39. EXPECT_EQ(0u, mock_host_resolver.num_resolve());
  40. // Load hostnames into HostResolver cache.
  41. int rv = mock_host_resolver.LoadIntoCache(
  42. HostPortPair("example1.com", 443), NetworkIsolationKey(), absl::nullopt);
  43. EXPECT_EQ(OK, rv);
  44. rv = mock_host_resolver.LoadIntoCache(HostPortPair("example2.com", 443),
  45. NetworkIsolationKey(), absl::nullopt);
  46. EXPECT_EQ(OK, rv);
  47. EXPECT_EQ(2u, mock_host_resolver.num_non_local_resolves());
  48. EXPECT_FALSE(IsPrivateHostForTesting(
  49. &mock_host_resolver, HostPortPair("2607:f8b0:4006:819::200e", 80),
  50. NetworkIsolationKey()));
  51. EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
  52. HostPortPair("192.168.0.1", 443),
  53. NetworkIsolationKey()));
  54. EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
  55. HostPortPair("92.168.0.1", 443),
  56. NetworkIsolationKey()));
  57. EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
  58. HostPortPair("example1.com", 443),
  59. NetworkIsolationKey()));
  60. EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
  61. HostPortPair("example2.com", 443),
  62. NetworkIsolationKey()));
  63. // IsPrivateHostForTesting() should have queried only the resolver's cache.
  64. EXPECT_EQ(2u, mock_host_resolver.num_non_local_resolves());
  65. }
  66. #if BUILDFLAG(IS_IOS)
  67. // Flaky on iOS: crbug.com/672917.
  68. #define MAYBE_ReservedHostUncached DISABLED_ReservedHostUncached
  69. #else
  70. #define MAYBE_ReservedHostUncached ReservedHostUncached
  71. #endif
  72. // Verify that IsPrivateHostForTesting() returns false for a hostname whose DNS
  73. // resolution is not cached. Further, once the resolution is cached, verify that
  74. // the cached entry is used.
  75. TEST(NetworkQualityEstimatorUtilTest, MAYBE_ReservedHostUncached) {
  76. base::test::TaskEnvironment task_environment;
  77. MockCachingHostResolver mock_host_resolver;
  78. auto rules = base::MakeRefCounted<net::RuleBasedHostResolverProc>(nullptr);
  79. // Add example3.com resolution to the DNS cache.
  80. mock_host_resolver.rules()->AddRule("example3.com", "127.0.0.3");
  81. // Not in DNS host cache, so should not be marked as private.
  82. EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
  83. HostPortPair("example3.com", 443),
  84. NetworkIsolationKey()));
  85. EXPECT_EQ(0u, mock_host_resolver.num_non_local_resolves());
  86. int rv = mock_host_resolver.LoadIntoCache(
  87. HostPortPair("example3.com", 443), NetworkIsolationKey(), absl::nullopt);
  88. EXPECT_EQ(OK, rv);
  89. EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
  90. EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
  91. HostPortPair("example3.com", 443),
  92. NetworkIsolationKey()));
  93. // IsPrivateHostForTesting() should have queried only the resolver's cache.
  94. EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
  95. }
  96. #if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID)
  97. // Flaky on iOS: crbug.com/672917.
  98. // Flaky on Android: crbug.com/1223950
  99. #define MAYBE_ReservedHostUncachedWithNetworkIsolationKey \
  100. DISABLED_ReservedHostUncachedWithNetworkIsolationKey
  101. #else
  102. #define MAYBE_ReservedHostUncachedWithNetworkIsolationKey \
  103. ReservedHostUncachedWithNetworkIsolationKey
  104. #endif
  105. // Make sure that IsPrivateHostForTesting() uses the NetworkIsolationKey
  106. // provided to it.
  107. TEST(NetworkQualityEstimatorUtilTest,
  108. MAYBE_ReservedHostUncachedWithNetworkIsolationKey) {
  109. const SchemefulSite kSite(GURL("https://foo.test/"));
  110. const net::NetworkIsolationKey kNetworkIsolationKey(kSite, kSite);
  111. base::test::ScopedFeatureList feature_list;
  112. feature_list.InitAndEnableFeature(
  113. features::kSplitHostCacheByNetworkIsolationKey);
  114. base::test::TaskEnvironment task_environment;
  115. MockCachingHostResolver mock_host_resolver;
  116. // Add example3.com resolution to the DNS cache.
  117. mock_host_resolver.rules()->AddRule("example3.com", "127.0.0.3");
  118. // Not in DNS host cache, so should not be marked as private.
  119. EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
  120. HostPortPair("example3.com", 443),
  121. kNetworkIsolationKey));
  122. EXPECT_EQ(0u, mock_host_resolver.num_non_local_resolves());
  123. int rv = mock_host_resolver.LoadIntoCache(
  124. HostPortPair("example3.com", 443), kNetworkIsolationKey, absl::nullopt);
  125. EXPECT_EQ(OK, rv);
  126. EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
  127. EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
  128. HostPortPair("example3.com", 443),
  129. kNetworkIsolationKey));
  130. // IsPrivateHostForTesting() should have queried only the resolver's cache.
  131. EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
  132. // IsPrivateHostForTesting should return false when using a different
  133. // NetworkIsolationKey (in this case, any empty one).
  134. EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
  135. HostPortPair("example3.com", 443),
  136. NetworkIsolationKey()));
  137. }
  138. #if BUILDFLAG(IS_IOS)
  139. // Flaky on iOS: crbug.com/672917.
  140. #define MAYBE_Localhost DISABLED_Localhost
  141. #else
  142. #define MAYBE_Localhost Localhost
  143. #endif
  144. // Verify that IsPrivateHostForTesting() returns correct results for local
  145. // hosts.
  146. TEST(NetworkQualityEstimatorUtilTest, MAYBE_Localhost) {
  147. base::test::TaskEnvironment task_environment;
  148. // Use actual HostResolver since MockCachingHostResolver does not determine
  149. // the correct answer for localhosts.
  150. std::unique_ptr<ContextHostResolver> resolver =
  151. HostResolver::CreateStandaloneContextResolver(NetLog::Get());
  152. auto rules = base::MakeRefCounted<net::RuleBasedHostResolverProc>(nullptr);
  153. EXPECT_TRUE(IsPrivateHostForTesting(
  154. resolver.get(), HostPortPair("localhost", 443), NetworkIsolationKey()));
  155. EXPECT_TRUE(IsPrivateHostForTesting(
  156. resolver.get(), HostPortPair("127.0.0.1", 80), NetworkIsolationKey()));
  157. EXPECT_TRUE(IsPrivateHostForTesting(
  158. resolver.get(), HostPortPair("0.0.0.0", 80), NetworkIsolationKey()));
  159. EXPECT_TRUE(IsPrivateHostForTesting(resolver.get(), HostPortPair("::1", 80),
  160. NetworkIsolationKey()));
  161. EXPECT_FALSE(IsPrivateHostForTesting(
  162. resolver.get(), HostPortPair("google.com", 80), NetworkIsolationKey()));
  163. // Legacy localhost names.
  164. EXPECT_FALSE(IsPrivateHostForTesting(
  165. resolver.get(), HostPortPair("localhost6", 443), NetworkIsolationKey()));
  166. EXPECT_FALSE(IsPrivateHostForTesting(
  167. resolver.get(), HostPortPair("localhost6.localdomain6", 443),
  168. NetworkIsolationKey()));
  169. }
  170. } // namespace
  171. } // namespace net::nqe::internal