| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #include "net/nqe/network_quality_estimator_util.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/dns/host_resolver.h" |
| 13 #include "net/dns/host_resolver_impl.h" |
| 14 #include "net/dns/mock_host_resolver.h" |
| 15 #include "net/log/test_net_log.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 namespace nqe { |
| 22 |
| 23 namespace internal { |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Verify that the cached network qualities from the prefs are not used if the |
| 28 // reading of the network quality prefs is not enabled.. |
| 29 TEST(NetworkQualityEstimatorUtilTest, ReservedHost) { |
| 30 std::unique_ptr<BoundTestNetLog> net_log = |
| 31 base::MakeUnique<BoundTestNetLog>(); |
| 32 BoundTestNetLog* net_log_ptr = net_log.get(); |
| 33 |
| 34 MockCachingHostResolver mock_host_resolver; |
| 35 |
| 36 scoped_refptr<net::RuleBasedHostResolverProc> rules( |
| 37 new net::RuleBasedHostResolverProc(nullptr)); |
| 38 |
| 39 // example1.com resolves to a private IP address. |
| 40 rules->AddRule("example1.com", "127.0.0.3"); |
| 41 |
| 42 // example2.com resolves to a public IP address. |
| 43 rules->AddRule("example2.com", "27.0.0.3"); |
| 44 |
| 45 mock_host_resolver.set_rules(rules.get()); |
| 46 |
| 47 EXPECT_EQ(0u, mock_host_resolver.num_resolve()); |
| 48 |
| 49 { |
| 50 // Resolve example1.com so that the resolution entry is cached. |
| 51 TestCompletionCallback callback; |
| 52 std::unique_ptr<HostResolver::Request> request; |
| 53 AddressList ignored; |
| 54 int rv = mock_host_resolver.Resolve( |
| 55 HostResolver::RequestInfo(HostPortPair("example1.com", 443)), |
| 56 DEFAULT_PRIORITY, &ignored, callback.callback(), &request, |
| 57 NetLogWithSource()); |
| 58 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 59 EXPECT_EQ(OK, callback.WaitForResult()); |
| 60 } |
| 61 |
| 62 { |
| 63 // Resolve example2.com so that the resolution entry is cached. |
| 64 TestCompletionCallback callback; |
| 65 std::unique_ptr<HostResolver::Request> request; |
| 66 AddressList ignored; |
| 67 int rv = mock_host_resolver.Resolve( |
| 68 HostResolver::RequestInfo(HostPortPair("example2.com", 443)), |
| 69 DEFAULT_PRIORITY, &ignored, callback.callback(), &request, |
| 70 NetLogWithSource()); |
| 71 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 72 EXPECT_EQ(OK, callback.WaitForResult()); |
| 73 } |
| 74 |
| 75 EXPECT_EQ(2u, mock_host_resolver.num_resolve()); |
| 76 |
| 77 EXPECT_FALSE(IsPrivateHost(&mock_host_resolver, |
| 78 HostPortPair("2607:f8b0:4006:819::200e", 80), |
| 79 net_log_ptr->bound())); |
| 80 EXPECT_EQ(1u, mock_host_resolver.num_resolve_from_cache()); |
| 81 |
| 82 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, |
| 83 HostPortPair("192.168.0.1", 443), |
| 84 net_log_ptr->bound())); |
| 85 EXPECT_EQ(2u, mock_host_resolver.num_resolve_from_cache()); |
| 86 |
| 87 EXPECT_FALSE(IsPrivateHost(&mock_host_resolver, |
| 88 HostPortPair("92.168.0.1", 443), |
| 89 net_log_ptr->bound())); |
| 90 EXPECT_EQ(3u, mock_host_resolver.num_resolve_from_cache()); |
| 91 |
| 92 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, |
| 93 HostPortPair("example1.com", 443), |
| 94 net_log_ptr->bound())); |
| 95 EXPECT_EQ(4u, mock_host_resolver.num_resolve_from_cache()); |
| 96 |
| 97 EXPECT_FALSE(IsPrivateHost(&mock_host_resolver, |
| 98 HostPortPair("example2.com", 443), |
| 99 net_log_ptr->bound())); |
| 100 EXPECT_EQ(5u, mock_host_resolver.num_resolve_from_cache()); |
| 101 |
| 102 // IsPrivateHost() should have queried only the resolver's cache. |
| 103 EXPECT_EQ(2u, mock_host_resolver.num_resolve()); |
| 104 } |
| 105 |
| 106 // Verify that IsPrivateHost() returns false for a hostname whose DNS |
| 107 // resolution is not cached. Further, once the resolution is cached, verify that |
| 108 // the cached entry is used. |
| 109 TEST(NetworkQualityEstimatorUtilTest, ReservedHostUncached) { |
| 110 std::unique_ptr<BoundTestNetLog> net_log = |
| 111 base::MakeUnique<BoundTestNetLog>(); |
| 112 BoundTestNetLog* net_log_ptr = net_log.get(); |
| 113 |
| 114 MockCachingHostResolver mock_host_resolver; |
| 115 |
| 116 scoped_refptr<net::RuleBasedHostResolverProc> rules( |
| 117 new net::RuleBasedHostResolverProc(nullptr)); |
| 118 |
| 119 // Add example3.com resolution to the DNS cache. |
| 120 rules->AddRule("example3.com", "127.0.0.3"); |
| 121 mock_host_resolver.set_rules(rules.get()); |
| 122 |
| 123 // Not in DNS host cache, so should not be marked as private. |
| 124 EXPECT_FALSE(IsPrivateHost(&mock_host_resolver, |
| 125 HostPortPair("example3.com", 443), |
| 126 net_log_ptr->bound())); |
| 127 EXPECT_EQ(0u, mock_host_resolver.num_resolve()); |
| 128 EXPECT_EQ(1u, mock_host_resolver.num_resolve_from_cache()); |
| 129 |
| 130 { |
| 131 // Resolve example3.com so that the resolution entry is cached. |
| 132 TestCompletionCallback callback; |
| 133 std::unique_ptr<HostResolver::Request> request; |
| 134 AddressList ignored; |
| 135 int rv = mock_host_resolver.Resolve( |
| 136 HostResolver::RequestInfo(HostPortPair("example3.com", 443)), |
| 137 DEFAULT_PRIORITY, &ignored, callback.callback(), &request, |
| 138 NetLogWithSource()); |
| 139 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 140 EXPECT_EQ(OK, callback.WaitForResult()); |
| 141 EXPECT_EQ(1u, mock_host_resolver.num_resolve()); |
| 142 } |
| 143 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, |
| 144 HostPortPair("example3.com", 443), |
| 145 net_log_ptr->bound())); |
| 146 |
| 147 // IsPrivateHost() should have queried only the resolver's cache. |
| 148 EXPECT_EQ(1u, mock_host_resolver.num_resolve()); |
| 149 EXPECT_EQ(2u, mock_host_resolver.num_resolve_from_cache()); |
| 150 } |
| 151 |
| 152 // Verify that IsPrivateHost() returns correct results for local hosts. |
| 153 TEST(NetworkQualityEstimatorUtilTest, Localhost) { |
| 154 std::unique_ptr<BoundTestNetLog> net_log = |
| 155 base::MakeUnique<BoundTestNetLog>(); |
| 156 BoundTestNetLog* net_log_ptr = net_log.get(); |
| 157 |
| 158 net::HostResolver::Options options; |
| 159 // Use HostResolverImpl since MockCachingHostResolver does not determine the |
| 160 // correct answer for localhosts. |
| 161 HostResolverImpl resolver(options, net_log_ptr->bound().net_log()); |
| 162 |
| 163 scoped_refptr<net::RuleBasedHostResolverProc> rules( |
| 164 new net::RuleBasedHostResolverProc(nullptr)); |
| 165 |
| 166 EXPECT_TRUE(IsPrivateHost(&resolver, HostPortPair("localhost", 443), |
| 167 net_log_ptr->bound())); |
| 168 EXPECT_TRUE(IsPrivateHost(&resolver, HostPortPair("localhost6", 443), |
| 169 net_log_ptr->bound())); |
| 170 EXPECT_TRUE(IsPrivateHost(&resolver, HostPortPair("127.0.0.1", 80), |
| 171 net_log_ptr->bound())); |
| 172 EXPECT_TRUE(IsPrivateHost(&resolver, HostPortPair("0.0.0.0", 80), |
| 173 net_log_ptr->bound())); |
| 174 EXPECT_TRUE( |
| 175 IsPrivateHost(&resolver, HostPortPair("::1", 80), net_log_ptr->bound())); |
| 176 EXPECT_FALSE(IsPrivateHost(&resolver, HostPortPair("google.com", 80), |
| 177 net_log_ptr->bound())); |
| 178 } |
| 179 |
| 180 } // namespace |
| 181 |
| 182 } // namespace internal |
| 183 |
| 184 } // namespace nqe |
| 185 |
| 186 } // namespace net |
| OLD | NEW |