| 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/mock_host_resolver.h" |
| 13 #include "net/log/test_net_log.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 namespace nqe { |
| 20 |
| 21 namespace internal { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Verify that the cached network qualities from the prefs are not used if the |
| 26 // reading of the network quality prefs is not enabled.. |
| 27 TEST(NetworkQualityEstimatorUtilTest, ReservedHost) { |
| 28 std::unique_ptr<BoundTestNetLog> net_log = |
| 29 base::MakeUnique<BoundTestNetLog>(); |
| 30 BoundTestNetLog* net_log_ptr = net_log.get(); |
| 31 |
| 32 MockCachingHostResolver mock_host_resolver; |
| 33 |
| 34 scoped_refptr<net::RuleBasedHostResolverProc> rules( |
| 35 new net::RuleBasedHostResolverProc(nullptr)); |
| 36 |
| 37 // example1.com resolves to a private IP address. |
| 38 rules->AddRule("example1.com", "127.0.0.3"); |
| 39 |
| 40 // example2.com resolves to a public IP address. |
| 41 rules->AddRule("example2.com", "27.0.0.3"); |
| 42 |
| 43 mock_host_resolver.set_rules(rules.get()); |
| 44 |
| 45 { |
| 46 // Resolve example1.com so that the resolution entry is cached. |
| 47 TestCompletionCallback callback; |
| 48 std::unique_ptr<HostResolver::Request> request; |
| 49 AddressList ignored; |
| 50 int rv = mock_host_resolver.Resolve( |
| 51 HostResolver::RequestInfo(HostPortPair("example1.com", 443)), |
| 52 DEFAULT_PRIORITY, &ignored, callback.callback(), &request, |
| 53 NetLogWithSource()); |
| 54 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 55 EXPECT_EQ(OK, callback.WaitForResult()); |
| 56 } |
| 57 |
| 58 { |
| 59 // Resolve example2.com so that the resolution entry is cached. |
| 60 TestCompletionCallback callback; |
| 61 std::unique_ptr<HostResolver::Request> request; |
| 62 AddressList ignored; |
| 63 int rv = mock_host_resolver.Resolve( |
| 64 HostResolver::RequestInfo(HostPortPair("example2.com", 443)), |
| 65 DEFAULT_PRIORITY, &ignored, callback.callback(), &request, |
| 66 NetLogWithSource()); |
| 67 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 68 EXPECT_EQ(OK, callback.WaitForResult()); |
| 69 } |
| 70 |
| 71 EXPECT_EQ(2u, mock_host_resolver.num_resolve()); |
| 72 |
| 73 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, HostPortPair("localhost", 443), |
| 74 net_log_ptr->bound())); |
| 75 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, HostPortPair("127.0.0.1", 80), |
| 76 net_log_ptr->bound())); |
| 77 |
| 78 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, |
| 79 HostPortPair("192.168.0.1", 443), |
| 80 net_log_ptr->bound())); |
| 81 EXPECT_EQ(0u, mock_host_resolver.num_resolve_from_cache()); |
| 82 |
| 83 EXPECT_FALSE(IsPrivateHost(&mock_host_resolver, |
| 84 HostPortPair("92.168.0.1", 443), |
| 85 net_log_ptr->bound())); |
| 86 EXPECT_EQ(1u, mock_host_resolver.num_resolve_from_cache()); |
| 87 |
| 88 EXPECT_TRUE(IsPrivateHost(&mock_host_resolver, |
| 89 HostPortPair("example1.com", 443), |
| 90 net_log_ptr->bound())); |
| 91 EXPECT_EQ(2u, mock_host_resolver.num_resolve_from_cache()); |
| 92 |
| 93 EXPECT_FALSE(IsPrivateHost(&mock_host_resolver, |
| 94 HostPortPair("example2.com", 443), |
| 95 net_log_ptr->bound())); |
| 96 EXPECT_EQ(3u, mock_host_resolver.num_resolve_from_cache()); |
| 97 |
| 98 // IsPrivateHost() should have queried only the resolver's cache. |
| 99 EXPECT_EQ(2u, mock_host_resolver.num_resolve()); |
| 100 } |
| 101 |
| 102 } // namespace |
| 103 |
| 104 } // namespace internal |
| 105 |
| 106 } // namespace nqe |
| 107 |
| 108 } // namespace net |
| OLD | NEW |