OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "base/memory/weak_ptr.h" |
| 6 #include "base/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "chrome/browser/net/dns_probe_runner.h" |
| 9 #include "net/dns/dns_client.h" |
| 10 #include "net/dns/dns_config_service.h" |
| 11 #include "net/dns/dns_protocol.h" |
| 12 #include "net/dns/dns_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 using base::MessageLoopForIO; |
| 18 using base::RunLoop; |
| 19 using net::DnsClient; |
| 20 using net::DnsConfig; |
| 21 using net::IPAddressNumber; |
| 22 using net::IPEndPoint; |
| 23 using net::MockDnsClientRule; |
| 24 using net::MockDnsClientRuleList; |
| 25 using net::ParseIPLiteralToNumber; |
| 26 |
| 27 class TestDnsProbeRunnerCallback { |
| 28 public: |
| 29 TestDnsProbeRunnerCallback(DnsProbeRunner::Type type); |
| 30 const DnsProbeRunner::ProbeCallback& callback() const; |
| 31 bool called() const; |
| 32 DnsProbeRunner::Result result() const; |
| 33 |
| 34 private: |
| 35 void OnCalled(DnsProbeRunner::Type type, DnsProbeRunner::Result result); |
| 36 |
| 37 base::WeakPtrFactory<TestDnsProbeRunnerCallback> weak_factory_; |
| 38 DnsProbeRunner::Type type_; |
| 39 DnsProbeRunner::ProbeCallback callback_; |
| 40 bool called_; |
| 41 DnsProbeRunner::Result result_; |
| 42 }; |
| 43 |
| 44 TestDnsProbeRunnerCallback::TestDnsProbeRunnerCallback( |
| 45 DnsProbeRunner::Type type) |
| 46 : weak_factory_(this), |
| 47 type_(type), |
| 48 callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled, |
| 49 weak_factory_.GetWeakPtr())), |
| 50 called_(false), |
| 51 result_(DnsProbeRunner::UNKNOWN) {} |
| 52 |
| 53 const DnsProbeRunner::ProbeCallback& |
| 54 TestDnsProbeRunnerCallback::callback() const { |
| 55 return callback_; |
| 56 } |
| 57 |
| 58 bool TestDnsProbeRunnerCallback::called() const { |
| 59 return called_; |
| 60 } |
| 61 |
| 62 DnsProbeRunner::Result TestDnsProbeRunnerCallback::result() const { |
| 63 DCHECK(called_); |
| 64 return result_; |
| 65 } |
| 66 |
| 67 void TestDnsProbeRunnerCallback::OnCalled(DnsProbeRunner::Type type, |
| 68 DnsProbeRunner::Result result) { |
| 69 DCHECK(!called_); |
| 70 EXPECT_EQ(type_, type); |
| 71 called_ = true; |
| 72 result_ = result; |
| 73 } |
| 74 |
| 75 class DnsProbeRunnerTest : public testing::Test { |
| 76 protected: |
| 77 void SetResults(DnsProbeRunner::Result system_result, |
| 78 DnsProbeRunner::Result public_result); |
| 79 void SetUpClients(MockDnsClientRule::Result system_good_result, |
| 80 MockDnsClientRule::Result public_good_result); |
| 81 void RunUntilIdle(); |
| 82 |
| 83 MessageLoopForIO message_loop_; |
| 84 DnsProbeRunner runner_; |
| 85 }; |
| 86 |
| 87 void DnsProbeRunnerTest::SetResults(DnsProbeRunner::Result system_result, |
| 88 DnsProbeRunner::Result public_result) { |
| 89 runner_.SetResultsForTesting(system_result, public_result); |
| 90 } |
| 91 |
| 92 void DnsProbeRunnerTest::SetUpClients( |
| 93 MockDnsClientRule::Result system_good_result, |
| 94 MockDnsClientRule::Result public_good_result) { |
| 95 DnsConfig config; |
| 96 config.nameservers.clear(); |
| 97 IPAddressNumber dns_ip; |
| 98 ParseIPLiteralToNumber("192.168.1.1", &dns_ip); |
| 99 const uint16 kDnsPort = net::dns_protocol::kDefaultPort; |
| 100 config.nameservers.push_back(IPEndPoint(dns_ip, kDnsPort)); |
| 101 |
| 102 const uint16 kTypeA = net::dns_protocol::kTypeA; |
| 103 |
| 104 MockDnsClientRuleList system_rules; |
| 105 system_rules.push_back( |
| 106 MockDnsClientRule("google.com", kTypeA, system_good_result)); |
| 107 scoped_ptr<DnsClient> system_client = |
| 108 CreateMockDnsClient(config, system_rules); |
| 109 |
| 110 MockDnsClientRuleList public_rules; |
| 111 public_rules.push_back( |
| 112 MockDnsClientRule("google.com", kTypeA, public_good_result)); |
| 113 scoped_ptr<DnsClient> public_client = |
| 114 CreateMockDnsClient(config, public_rules); |
| 115 |
| 116 runner_.SetClientsForTesting(system_client.Pass(), public_client.Pass()); |
| 117 } |
| 118 |
| 119 |
| 120 void DnsProbeRunnerTest::RunUntilIdle() { |
| 121 RunLoop run_loop; |
| 122 run_loop.RunUntilIdle(); |
| 123 } |
| 124 |
| 125 TEST_F(DnsProbeRunnerTest, Null) {} |
| 126 |
| 127 TEST_F(DnsProbeRunnerTest, MockResults_Null) { |
| 128 SetResults(DnsProbeRunner::CORRECT, DnsProbeRunner::UNREACHABLE); |
| 129 } |
| 130 |
| 131 TEST_F(DnsProbeRunnerTest, MockResults_Basic) { |
| 132 SetResults(DnsProbeRunner::CORRECT, DnsProbeRunner::UNREACHABLE); |
| 133 { |
| 134 TestDnsProbeRunnerCallback cb(DnsProbeRunner::SYSTEM); |
| 135 runner_.RunProbe(DnsProbeRunner::SYSTEM, cb.callback()); |
| 136 RunUntilIdle(); |
| 137 EXPECT_TRUE(cb.called()); |
| 138 EXPECT_EQ(DnsProbeRunner::CORRECT, cb.result()); |
| 139 } |
| 140 { |
| 141 TestDnsProbeRunnerCallback cb(DnsProbeRunner::PUBLIC); |
| 142 runner_.RunProbe(DnsProbeRunner::PUBLIC, cb.callback()); |
| 143 RunUntilIdle(); |
| 144 EXPECT_TRUE(cb.called()); |
| 145 EXPECT_EQ(DnsProbeRunner::UNREACHABLE, cb.result()); |
| 146 } |
| 147 } |
| 148 |
| 149 TEST_F(DnsProbeRunnerTest, MockClients_Null) { |
| 150 SetUpClients(MockDnsClientRule::OK, MockDnsClientRule::FAIL_SYNC); |
| 151 } |
| 152 |
| 153 TEST_F(DnsProbeRunnerTest, MockClients_Basic) { |
| 154 SetUpClients(MockDnsClientRule::OK, MockDnsClientRule::FAIL_SYNC); |
| 155 { |
| 156 TestDnsProbeRunnerCallback cb(DnsProbeRunner::SYSTEM); |
| 157 runner_.RunProbe(DnsProbeRunner::SYSTEM, cb.callback()); |
| 158 RunUntilIdle(); |
| 159 EXPECT_TRUE(cb.called()); |
| 160 EXPECT_EQ(DnsProbeRunner::CORRECT, cb.result()); |
| 161 } |
| 162 { |
| 163 TestDnsProbeRunnerCallback cb(DnsProbeRunner::PUBLIC); |
| 164 runner_.RunProbe(DnsProbeRunner::PUBLIC, cb.callback()); |
| 165 RunUntilIdle(); |
| 166 EXPECT_TRUE(cb.called()); |
| 167 EXPECT_EQ(DnsProbeRunner::UNREACHABLE, cb.result()); |
| 168 } |
| 169 } |
| 170 |
| 171 TEST_F(DnsProbeRunnerTest, MockClients_Parallel) { |
| 172 SetUpClients(MockDnsClientRule::OK, MockDnsClientRule::TIMEOUT); |
| 173 |
| 174 TestDnsProbeRunnerCallback system_cb(DnsProbeRunner::SYSTEM); |
| 175 TestDnsProbeRunnerCallback public_cb(DnsProbeRunner::PUBLIC); |
| 176 |
| 177 runner_.RunProbe(DnsProbeRunner::SYSTEM, system_cb.callback()); |
| 178 runner_.RunProbe(DnsProbeRunner::PUBLIC, public_cb.callback()); |
| 179 |
| 180 EXPECT_FALSE(system_cb.called()); |
| 181 EXPECT_FALSE(public_cb.called()); |
| 182 |
| 183 RunUntilIdle(); |
| 184 |
| 185 EXPECT_TRUE(system_cb.called()); |
| 186 EXPECT_TRUE(public_cb.called()); |
| 187 EXPECT_EQ(DnsProbeRunner::CORRECT, system_cb.result()); |
| 188 EXPECT_EQ(DnsProbeRunner::UNREACHABLE, public_cb.result()); |
| 189 } |
| 190 |
| 191 } // namespace |
OLD | NEW |