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