OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/net/dns_probe_job.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "net/base/net_log.h" | |
11 #include "net/dns/dns_client.h" | |
12 #include "net/dns/dns_config_service.h" | |
13 #include "net/dns/dns_protocol.h" | |
14 #include "net/dns/dns_test_util.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using net::DnsClient; | |
18 using net::DnsConfig; | |
19 using net::IPAddressNumber; | |
20 using net::IPEndPoint; | |
21 using net::ParseIPLiteralToNumber; | |
22 using net::MockDnsClientRule; | |
23 using net::MockDnsClientRuleList; | |
24 using net::NetLog; | |
25 | |
26 namespace chrome_browser_net { | |
27 | |
28 namespace { | |
29 | |
30 class DnsProbeJobTest : public testing::Test { | |
31 public: | |
32 void RunProbe(MockDnsClientRule::Result expected_good_result, | |
33 MockDnsClientRule::Result expected_bad_result); | |
34 | |
35 protected: | |
36 void OnProbeFinished(DnsProbeJob* job, DnsProbeJob::Result result); | |
37 | |
38 bool callback_called_; | |
39 DnsProbeJob::Result callback_result_; | |
40 }; | |
41 | |
42 // Runs a probe and waits for the callback. |good_result| and |bad_result| | |
43 // are the result of the good and bad transactions that the DnsProbeJob will | |
44 // receive. | |
45 void DnsProbeJobTest::RunProbe(MockDnsClientRule::Result good_result, | |
46 MockDnsClientRule::Result bad_result) { | |
47 DnsConfig config; | |
48 config.nameservers.clear(); | |
49 IPAddressNumber dns_ip; | |
50 ParseIPLiteralToNumber("192.168.1.1", &dns_ip); | |
51 const uint16 kDnsPort = net::dns_protocol::kDefaultPort; | |
52 config.nameservers.push_back(IPEndPoint(dns_ip, kDnsPort)); | |
53 | |
54 const uint16 kTypeA = net::dns_protocol::kTypeA; | |
55 MockDnsClientRuleList rules; | |
56 rules.push_back(MockDnsClientRule("google.com", kTypeA, good_result)); | |
57 rules.push_back(MockDnsClientRule(std::string(), kTypeA, bad_result)); | |
58 | |
59 scoped_ptr<DnsClient> dns_client = CreateMockDnsClient(config, rules); | |
60 dns_client->SetConfig(config); | |
61 | |
62 NetLog* net_log = NULL; | |
63 DnsProbeJob::CallbackType callback = base::Bind( | |
64 &DnsProbeJobTest::OnProbeFinished, | |
65 base::Unretained(this)); | |
66 | |
67 // Need to set these before creating job, because it can call the callback | |
68 // synchronously in the constructor if both transactions fail to start. | |
69 callback_called_ = false; | |
70 callback_result_ = DnsProbeJob::SERVERS_UNKNOWN; | |
71 | |
72 // DnsProbeJob needs somewhere to post the callback. | |
73 scoped_ptr<base::MessageLoop> message_loop_(new base::MessageLoopForIO()); | |
74 | |
75 scoped_ptr<DnsProbeJob> job( | |
76 DnsProbeJob::CreateJob(dns_client.Pass(), callback, net_log)); | |
77 | |
78 // Force callback to run. | |
79 base::RunLoop run_loop; | |
80 run_loop.RunUntilIdle(); | |
81 } | |
82 | |
83 void DnsProbeJobTest::OnProbeFinished(DnsProbeJob* job, | |
84 DnsProbeJob::Result result) { | |
85 EXPECT_FALSE(callback_called_); | |
86 | |
87 callback_called_ = true; | |
88 callback_result_ = result; | |
89 } | |
90 | |
91 struct TestCase { | |
92 MockDnsClientRule::Result good_result; | |
93 MockDnsClientRule::Result bad_result; | |
94 DnsProbeJob::Result expected_probe_result; | |
95 }; | |
96 | |
97 TEST_F(DnsProbeJobTest, Test) { | |
98 static const TestCase kTestCases[] = { | |
99 { MockDnsClientRule::OK, | |
100 MockDnsClientRule::EMPTY, | |
101 DnsProbeJob::SERVERS_CORRECT }, | |
102 { MockDnsClientRule::EMPTY, | |
103 MockDnsClientRule::EMPTY, | |
104 DnsProbeJob::SERVERS_INCORRECT }, | |
105 // TODO(ttuttle): Test that triggers QUERY_DNS_ERROR. | |
106 // (Need to add another mock behavior to MockDnsClient.) | |
107 { MockDnsClientRule::FAIL_ASYNC, | |
108 MockDnsClientRule::FAIL_ASYNC, | |
109 DnsProbeJob::SERVERS_FAILING }, | |
110 { MockDnsClientRule::FAIL_SYNC, | |
111 MockDnsClientRule::FAIL_SYNC, | |
112 DnsProbeJob::SERVERS_FAILING }, | |
113 { MockDnsClientRule::TIMEOUT, | |
114 MockDnsClientRule::TIMEOUT, | |
115 DnsProbeJob::SERVERS_UNREACHABLE }, | |
116 }; | |
117 for (size_t i = 0; i < arraysize(kTestCases); i++) { | |
118 const TestCase* test_case = &kTestCases[i]; | |
119 RunProbe(test_case->good_result, test_case->bad_result); | |
120 EXPECT_TRUE(callback_called_); | |
121 EXPECT_EQ(test_case->expected_probe_result, callback_result_); | |
122 } | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 } // namespace chrome_browser_net | |
OLD | NEW |