123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- // Copyright 2017 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "net/nqe/network_quality_estimator_util.h"
- #include <memory>
- #include "base/test/scoped_feature_list.h"
- #include "base/test/task_environment.h"
- #include "build/build_config.h"
- #include "net/base/features.h"
- #include "net/base/host_port_pair.h"
- #include "net/base/net_errors.h"
- #include "net/base/network_isolation_key.h"
- #include "net/base/schemeful_site.h"
- #include "net/base/test_completion_callback.h"
- #include "net/dns/context_host_resolver.h"
- #include "net/dns/host_resolver.h"
- #include "net/dns/mock_host_resolver.h"
- #include "net/log/net_log.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "third_party/abseil-cpp/absl/types/optional.h"
- #include "url/gurl.h"
- namespace net::nqe::internal {
- namespace {
- #if BUILDFLAG(IS_IOS)
- // Flaky on iOS: crbug.com/672917.
- #define MAYBE_ReservedHost DISABLED_ReservedHost
- #else
- #define MAYBE_ReservedHost ReservedHost
- #endif
- // Verify that the cached network qualities from the prefs are not used if the
- // reading of the network quality prefs is not enabled..
- TEST(NetworkQualityEstimatorUtilTest, MAYBE_ReservedHost) {
- base::test::TaskEnvironment task_environment;
- MockCachingHostResolver mock_host_resolver;
- // example1.com resolves to a private IP address.
- mock_host_resolver.rules()->AddRule("example1.com", "127.0.0.3");
- // example2.com resolves to a public IP address.
- mock_host_resolver.rules()->AddRule("example2.com", "27.0.0.3");
- EXPECT_EQ(0u, mock_host_resolver.num_resolve());
- // Load hostnames into HostResolver cache.
- int rv = mock_host_resolver.LoadIntoCache(
- HostPortPair("example1.com", 443), NetworkIsolationKey(), absl::nullopt);
- EXPECT_EQ(OK, rv);
- rv = mock_host_resolver.LoadIntoCache(HostPortPair("example2.com", 443),
- NetworkIsolationKey(), absl::nullopt);
- EXPECT_EQ(OK, rv);
- EXPECT_EQ(2u, mock_host_resolver.num_non_local_resolves());
- EXPECT_FALSE(IsPrivateHostForTesting(
- &mock_host_resolver, HostPortPair("2607:f8b0:4006:819::200e", 80),
- NetworkIsolationKey()));
- EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("192.168.0.1", 443),
- NetworkIsolationKey()));
- EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("92.168.0.1", 443),
- NetworkIsolationKey()));
- EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example1.com", 443),
- NetworkIsolationKey()));
- EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example2.com", 443),
- NetworkIsolationKey()));
- // IsPrivateHostForTesting() should have queried only the resolver's cache.
- EXPECT_EQ(2u, mock_host_resolver.num_non_local_resolves());
- }
- #if BUILDFLAG(IS_IOS)
- // Flaky on iOS: crbug.com/672917.
- #define MAYBE_ReservedHostUncached DISABLED_ReservedHostUncached
- #else
- #define MAYBE_ReservedHostUncached ReservedHostUncached
- #endif
- // Verify that IsPrivateHostForTesting() returns false for a hostname whose DNS
- // resolution is not cached. Further, once the resolution is cached, verify that
- // the cached entry is used.
- TEST(NetworkQualityEstimatorUtilTest, MAYBE_ReservedHostUncached) {
- base::test::TaskEnvironment task_environment;
- MockCachingHostResolver mock_host_resolver;
- auto rules = base::MakeRefCounted<net::RuleBasedHostResolverProc>(nullptr);
- // Add example3.com resolution to the DNS cache.
- mock_host_resolver.rules()->AddRule("example3.com", "127.0.0.3");
- // Not in DNS host cache, so should not be marked as private.
- EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example3.com", 443),
- NetworkIsolationKey()));
- EXPECT_EQ(0u, mock_host_resolver.num_non_local_resolves());
- int rv = mock_host_resolver.LoadIntoCache(
- HostPortPair("example3.com", 443), NetworkIsolationKey(), absl::nullopt);
- EXPECT_EQ(OK, rv);
- EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
- EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example3.com", 443),
- NetworkIsolationKey()));
- // IsPrivateHostForTesting() should have queried only the resolver's cache.
- EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
- }
- #if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID)
- // Flaky on iOS: crbug.com/672917.
- // Flaky on Android: crbug.com/1223950
- #define MAYBE_ReservedHostUncachedWithNetworkIsolationKey \
- DISABLED_ReservedHostUncachedWithNetworkIsolationKey
- #else
- #define MAYBE_ReservedHostUncachedWithNetworkIsolationKey \
- ReservedHostUncachedWithNetworkIsolationKey
- #endif
- // Make sure that IsPrivateHostForTesting() uses the NetworkIsolationKey
- // provided to it.
- TEST(NetworkQualityEstimatorUtilTest,
- MAYBE_ReservedHostUncachedWithNetworkIsolationKey) {
- const SchemefulSite kSite(GURL("https://foo.test/"));
- const net::NetworkIsolationKey kNetworkIsolationKey(kSite, kSite);
- base::test::ScopedFeatureList feature_list;
- feature_list.InitAndEnableFeature(
- features::kSplitHostCacheByNetworkIsolationKey);
- base::test::TaskEnvironment task_environment;
- MockCachingHostResolver mock_host_resolver;
- // Add example3.com resolution to the DNS cache.
- mock_host_resolver.rules()->AddRule("example3.com", "127.0.0.3");
- // Not in DNS host cache, so should not be marked as private.
- EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example3.com", 443),
- kNetworkIsolationKey));
- EXPECT_EQ(0u, mock_host_resolver.num_non_local_resolves());
- int rv = mock_host_resolver.LoadIntoCache(
- HostPortPair("example3.com", 443), kNetworkIsolationKey, absl::nullopt);
- EXPECT_EQ(OK, rv);
- EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
- EXPECT_TRUE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example3.com", 443),
- kNetworkIsolationKey));
- // IsPrivateHostForTesting() should have queried only the resolver's cache.
- EXPECT_EQ(1u, mock_host_resolver.num_non_local_resolves());
- // IsPrivateHostForTesting should return false when using a different
- // NetworkIsolationKey (in this case, any empty one).
- EXPECT_FALSE(IsPrivateHostForTesting(&mock_host_resolver,
- HostPortPair("example3.com", 443),
- NetworkIsolationKey()));
- }
- #if BUILDFLAG(IS_IOS)
- // Flaky on iOS: crbug.com/672917.
- #define MAYBE_Localhost DISABLED_Localhost
- #else
- #define MAYBE_Localhost Localhost
- #endif
- // Verify that IsPrivateHostForTesting() returns correct results for local
- // hosts.
- TEST(NetworkQualityEstimatorUtilTest, MAYBE_Localhost) {
- base::test::TaskEnvironment task_environment;
- // Use actual HostResolver since MockCachingHostResolver does not determine
- // the correct answer for localhosts.
- std::unique_ptr<ContextHostResolver> resolver =
- HostResolver::CreateStandaloneContextResolver(NetLog::Get());
- auto rules = base::MakeRefCounted<net::RuleBasedHostResolverProc>(nullptr);
- EXPECT_TRUE(IsPrivateHostForTesting(
- resolver.get(), HostPortPair("localhost", 443), NetworkIsolationKey()));
- EXPECT_TRUE(IsPrivateHostForTesting(
- resolver.get(), HostPortPair("127.0.0.1", 80), NetworkIsolationKey()));
- EXPECT_TRUE(IsPrivateHostForTesting(
- resolver.get(), HostPortPair("0.0.0.0", 80), NetworkIsolationKey()));
- EXPECT_TRUE(IsPrivateHostForTesting(resolver.get(), HostPortPair("::1", 80),
- NetworkIsolationKey()));
- EXPECT_FALSE(IsPrivateHostForTesting(
- resolver.get(), HostPortPair("google.com", 80), NetworkIsolationKey()));
- // Legacy localhost names.
- EXPECT_FALSE(IsPrivateHostForTesting(
- resolver.get(), HostPortPair("localhost6", 443), NetworkIsolationKey()));
- EXPECT_FALSE(IsPrivateHostForTesting(
- resolver.get(), HostPortPair("localhost6.localdomain6", 443),
- NetworkIsolationKey()));
- }
- } // namespace
- } // namespace net::nqe::internal
|