OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/dns_probe_service.h" | 5 #include "chrome/browser/net/dns_probe_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "chrome/browser/net/dns_probe_job.h" | 12 #include "chrome/browser/net/dns_probe_runner.h" |
13 #include "chrome/browser/net/dns_probe_test_util.h" | |
13 #include "chrome/common/net/net_error_info.h" | 14 #include "chrome/common/net/net_error_info.h" |
15 #include "content/public/test/test_browser_thread_bundle.h" | |
16 #include "net/dns/dns_test_util.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
15 | 18 |
16 using chrome_common_net::DnsProbeResult; | 19 using base::MessageLoopForIO; |
20 using base::RunLoop; | |
21 using chrome_common_net::DnsProbeStatus; | |
22 using content::TestBrowserThreadBundle; | |
23 using net::MockDnsClientRule; | |
17 | 24 |
18 namespace chrome_browser_net { | 25 namespace chrome_browser_net { |
19 | 26 |
20 namespace { | 27 namespace { |
21 | 28 |
22 class MockDnsProbeJob : public DnsProbeJob { | |
23 public: | |
24 MockDnsProbeJob(const CallbackType& callback, | |
25 DnsProbeJob::Result result) | |
26 : weak_factory_(this) { | |
27 base::MessageLoop::current()->PostTask( | |
28 FROM_HERE, | |
29 base::Bind(&MockDnsProbeJob::CallCallback, | |
30 weak_factory_.GetWeakPtr(), | |
31 callback, | |
32 result)); | |
33 } | |
34 | |
35 virtual ~MockDnsProbeJob() { } | |
36 | |
37 private: | |
38 void CallCallback(const CallbackType& callback, Result result) { | |
39 callback.Run(this, result); | |
40 } | |
41 | |
42 base::WeakPtrFactory<MockDnsProbeJob> weak_factory_; | |
43 }; | |
44 | |
45 class TestDnsProbeService : public DnsProbeService { | |
46 public: | |
47 TestDnsProbeService() | |
48 : DnsProbeService(), | |
49 system_job_created_(false), | |
50 public_job_created_(false), | |
51 mock_system_result_(DnsProbeJob::SERVERS_UNKNOWN), | |
52 mock_public_result_(DnsProbeJob::SERVERS_UNKNOWN), | |
53 mock_system_fail_(false) { | |
54 } | |
55 | |
56 virtual ~TestDnsProbeService() { } | |
57 | |
58 void set_mock_results( | |
59 DnsProbeJob::Result mock_system_result, | |
60 DnsProbeJob::Result mock_public_result) { | |
61 mock_system_result_ = mock_system_result; | |
62 mock_public_result_ = mock_public_result; | |
63 } | |
64 | |
65 void set_mock_system_fail(bool mock_system_fail) { | |
66 mock_system_fail_ = mock_system_fail; | |
67 } | |
68 | |
69 bool jobs_created(void) { | |
70 return system_job_created_ && public_job_created_; | |
71 } | |
72 | |
73 void ResetJobsCreated() { | |
74 system_job_created_ = false; | |
75 public_job_created_ = false; | |
76 } | |
77 | |
78 void MockExpireResults() { | |
79 ExpireResults(); | |
80 } | |
81 | |
82 bool system_job_created_; | |
83 bool public_job_created_; | |
84 | |
85 private: | |
86 // Override methods in DnsProbeService to return mock jobs: | |
87 | |
88 virtual scoped_ptr<DnsProbeJob> CreateSystemProbeJob( | |
89 const DnsProbeJob::CallbackType& job_callback) OVERRIDE { | |
90 if (mock_system_fail_) | |
91 return scoped_ptr<DnsProbeJob>(); | |
92 | |
93 system_job_created_ = true; | |
94 return scoped_ptr<DnsProbeJob>( | |
95 new MockDnsProbeJob(job_callback, | |
96 mock_system_result_)); | |
97 } | |
98 | |
99 virtual scoped_ptr<DnsProbeJob> CreatePublicProbeJob( | |
100 const DnsProbeJob::CallbackType& job_callback) OVERRIDE { | |
101 public_job_created_ = true; | |
102 return scoped_ptr<DnsProbeJob>( | |
103 new MockDnsProbeJob(job_callback, | |
104 mock_public_result_)); | |
105 } | |
106 | |
107 DnsProbeJob::Result mock_system_result_; | |
108 DnsProbeJob::Result mock_public_result_; | |
109 bool mock_system_fail_; | |
110 }; | |
111 | |
112 class DnsProbeServiceTest : public testing::Test { | 29 class DnsProbeServiceTest : public testing::Test { |
113 public: | 30 public: |
114 DnsProbeServiceTest() | 31 DnsProbeServiceTest() |
115 : callback_called_(false), | 32 : callback_called_(false), |
116 callback_result_(chrome_common_net::DNS_PROBE_UNKNOWN) { | 33 callback_result_(chrome_common_net::DNS_PROBE_MAX) { |
117 } | 34 } |
118 | 35 |
119 void Probe() { | 36 void Probe() { |
120 service_.ProbeDns(base::Bind(&DnsProbeServiceTest::ProbeCallback, | 37 service_.ProbeDns(base::Bind(&DnsProbeServiceTest::ProbeCallback, |
121 base::Unretained(this))); | 38 base::Unretained(this))); |
122 } | 39 } |
123 | 40 |
124 void RunUntilIdle() { | |
125 base::RunLoop run_loop; | |
126 run_loop.RunUntilIdle(); | |
127 } | |
128 | |
129 void Reset() { | 41 void Reset() { |
130 service_.ResetJobsCreated(); | |
131 callback_called_ = false; | 42 callback_called_ = false; |
132 } | 43 } |
133 | 44 |
134 base::MessageLoopForIO message_loop_; | 45 protected: |
135 TestDnsProbeService service_; | 46 void SetRules(MockDnsClientRule::Result system_query_result, |
136 bool callback_called_; | 47 MockDnsClientRule::Result public_query_result) { |
137 DnsProbeResult callback_result_; | 48 service_.SetSystemClientForTesting( |
49 CreateMockDnsClientForProbes(system_query_result)); | |
50 service_.SetPublicClientForTesting( | |
51 CreateMockDnsClientForProbes(public_query_result)); | |
52 } | |
53 | |
54 void RunTest(MockDnsClientRule::Result system_query_result, | |
55 MockDnsClientRule::Result public_query_result, | |
56 DnsProbeStatus expected_result) { | |
57 Reset(); | |
58 SetRules(system_query_result, public_query_result); | |
59 EXPECT_FALSE(callback_called()); | |
mmenke
2013/07/12 19:51:22
nit: I don't think this EXPECT is needed, since w
Deprecated (see juliatuttle)
2013/07/12 20:55:55
Done.
| |
60 | |
61 Probe(); | |
62 RunLoop().RunUntilIdle(); | |
63 EXPECT_TRUE(callback_called()); | |
64 EXPECT_EQ(expected_result, callback_result()); | |
65 } | |
66 | |
67 void ClearCachedResult() { | |
68 service_.ClearCachedResultForTesting(); | |
69 } | |
70 | |
71 bool callback_called() { return callback_called_; } | |
72 DnsProbeStatus callback_result() { return callback_result_; } | |
mmenke
2013/07/12 19:51:22
nit: Both these can be const. Actually, are they
Deprecated (see juliatuttle)
2013/07/12 20:55:55
Done.
| |
138 | 73 |
139 private: | 74 private: |
140 void ProbeCallback(DnsProbeResult result) { | 75 void ProbeCallback(DnsProbeStatus result) { |
mmenke
2013/07/12 19:51:22
Maybe EXPECT_FALSE(callback_called_)?
Deprecated (see juliatuttle)
2013/07/12 20:55:55
Done.
| |
141 callback_called_ = true; | 76 callback_called_ = true; |
142 callback_result_ = result; | 77 callback_result_ = result; |
143 } | 78 } |
79 | |
80 DnsProbeService service_; | |
81 bool callback_called_; | |
82 DnsProbeStatus callback_result_; | |
83 TestBrowserThreadBundle bundle_; | |
144 }; | 84 }; |
145 | 85 |
146 TEST_F(DnsProbeServiceTest, Null) { | 86 TEST_F(DnsProbeServiceTest, Probe_OK_OK) { |
87 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK, | |
88 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); | |
147 } | 89 } |
148 | 90 |
149 TEST_F(DnsProbeServiceTest, Probe) { | 91 TEST_F(DnsProbeServiceTest, Probe_TIMEOUT_OK) { |
150 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, | 92 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::OK, |
151 DnsProbeJob::SERVERS_CORRECT); | 93 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG); |
94 } | |
152 | 95 |
153 Probe(); | 96 TEST_F(DnsProbeServiceTest, Probe_TIMEOUT_TIMEOUT) { |
154 EXPECT_TRUE(service_.jobs_created()); | 97 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT, |
155 EXPECT_FALSE(callback_called_); | 98 chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET); |
99 } | |
156 | 100 |
157 RunUntilIdle(); | 101 TEST_F(DnsProbeServiceTest, Probe_OK_FAIL_SYNC) { |
158 EXPECT_TRUE(callback_called_); | 102 RunTest(MockDnsClientRule::OK, MockDnsClientRule::FAIL_SYNC, |
159 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); | 103 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); |
104 } | |
105 | |
106 TEST_F(DnsProbeServiceTest, Probe_FAIL_SYNC_OK) { | |
107 RunTest(MockDnsClientRule::FAIL_SYNC, MockDnsClientRule::OK, | |
108 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG); | |
109 } | |
110 | |
111 TEST_F(DnsProbeServiceTest, Probe_FAIL_SYNC_FAIL_SYNC) { | |
112 RunTest(MockDnsClientRule::FAIL_SYNC, MockDnsClientRule::FAIL_SYNC, | |
113 chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN); | |
160 } | 114 } |
161 | 115 |
162 TEST_F(DnsProbeServiceTest, Cache) { | 116 TEST_F(DnsProbeServiceTest, Cache) { |
163 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, | 117 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK, |
164 DnsProbeJob::SERVERS_CORRECT); | 118 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); |
165 | 119 // Cached NXDOMAIN result should persist, not the result from the new rules. |
166 Probe(); | 120 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT, |
167 RunUntilIdle(); | 121 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); |
168 Reset(); | |
169 | |
170 // Cached NXDOMAIN result should persist. | |
171 | |
172 Probe(); | |
173 EXPECT_FALSE(service_.jobs_created()); | |
174 | |
175 RunUntilIdle(); | |
176 EXPECT_TRUE(callback_called_); | |
177 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); | |
178 } | 122 } |
179 | 123 |
180 TEST_F(DnsProbeServiceTest, Expired) { | 124 TEST_F(DnsProbeServiceTest, Expire) { |
181 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, | 125 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK, |
182 DnsProbeJob::SERVERS_CORRECT); | 126 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); |
183 | 127 // Pretend cache expires. |
184 Probe(); | 128 ClearCachedResult(); |
185 EXPECT_TRUE(service_.jobs_created()); | 129 // New rules should apply, since a new probe should be run. |
186 | 130 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT, |
187 RunUntilIdle(); | 131 chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET); |
188 EXPECT_TRUE(callback_called_); | |
189 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); | |
190 | |
191 Reset(); | |
192 | |
193 service_.MockExpireResults(); | |
194 | |
195 Probe(); | |
196 EXPECT_TRUE(service_.jobs_created()); | |
197 | |
198 RunUntilIdle(); | |
199 EXPECT_TRUE(callback_called_); | |
200 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); | |
201 } | |
202 | |
203 TEST_F(DnsProbeServiceTest, SystemFail) { | |
204 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, | |
205 DnsProbeJob::SERVERS_CORRECT); | |
206 service_.set_mock_system_fail(true); | |
207 | |
208 Probe(); | |
209 EXPECT_TRUE(callback_called_); | |
210 EXPECT_EQ(chrome_common_net::DNS_PROBE_UNKNOWN, callback_result_); | |
211 | |
212 Reset(); | |
213 | |
214 RunUntilIdle(); | |
215 EXPECT_FALSE(callback_called_); | |
216 } | 132 } |
217 | 133 |
218 } // namespace | 134 } // namespace |
219 | 135 |
220 } // namespace chrome_browser_net | 136 } // namespace chrome_browser_net |
OLD | NEW |