Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(829)

Side by Side Diff: chrome/browser/net/dns_probe_service_unittest.cc

Issue 13270005: Display DNS probe results. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix one last nit Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/net/dns_probe_service.cc ('k') | chrome/browser/net/dns_probe_test_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
60 Probe();
61 RunLoop().RunUntilIdle();
62 EXPECT_TRUE(callback_called_);
63 EXPECT_EQ(expected_result, callback_result_);
64 }
65
66 void ClearCachedResult() {
67 service_.ClearCachedResultForTesting();
68 }
138 69
139 private: 70 private:
140 void ProbeCallback(DnsProbeResult result) { 71 void ProbeCallback(DnsProbeStatus result) {
72 EXPECT_FALSE(callback_called_);
141 callback_called_ = true; 73 callback_called_ = true;
142 callback_result_ = result; 74 callback_result_ = result;
143 } 75 }
76
77 DnsProbeService service_;
78 bool callback_called_;
79 DnsProbeStatus callback_result_;
80 TestBrowserThreadBundle bundle_;
144 }; 81 };
145 82
146 TEST_F(DnsProbeServiceTest, Null) { 83 TEST_F(DnsProbeServiceTest, Probe_OK_OK) {
84 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK,
85 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
147 } 86 }
148 87
149 TEST_F(DnsProbeServiceTest, Probe) { 88 TEST_F(DnsProbeServiceTest, Probe_TIMEOUT_OK) {
150 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, 89 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::OK,
151 DnsProbeJob::SERVERS_CORRECT); 90 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG);
91 }
152 92
153 Probe(); 93 TEST_F(DnsProbeServiceTest, Probe_TIMEOUT_TIMEOUT) {
154 EXPECT_TRUE(service_.jobs_created()); 94 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT,
155 EXPECT_FALSE(callback_called_); 95 chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET);
96 }
156 97
157 RunUntilIdle(); 98 TEST_F(DnsProbeServiceTest, Probe_OK_FAIL_SYNC) {
158 EXPECT_TRUE(callback_called_); 99 RunTest(MockDnsClientRule::OK, MockDnsClientRule::FAIL_SYNC,
159 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); 100 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
101 }
102
103 TEST_F(DnsProbeServiceTest, Probe_FAIL_SYNC_OK) {
104 RunTest(MockDnsClientRule::FAIL_SYNC, MockDnsClientRule::OK,
105 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG);
106 }
107
108 TEST_F(DnsProbeServiceTest, Probe_FAIL_SYNC_FAIL_SYNC) {
109 RunTest(MockDnsClientRule::FAIL_SYNC, MockDnsClientRule::FAIL_SYNC,
110 chrome_common_net::DNS_PROBE_FINISHED_INCONCLUSIVE);
160 } 111 }
161 112
162 TEST_F(DnsProbeServiceTest, Cache) { 113 TEST_F(DnsProbeServiceTest, Cache) {
163 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, 114 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK,
164 DnsProbeJob::SERVERS_CORRECT); 115 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
165 116 // Cached NXDOMAIN result should persist, not the result from the new rules.
166 Probe(); 117 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT,
167 RunUntilIdle(); 118 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 } 119 }
179 120
180 TEST_F(DnsProbeServiceTest, Expired) { 121 TEST_F(DnsProbeServiceTest, Expire) {
181 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, 122 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK,
182 DnsProbeJob::SERVERS_CORRECT); 123 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
183 124 // Pretend cache expires.
184 Probe(); 125 ClearCachedResult();
185 EXPECT_TRUE(service_.jobs_created()); 126 // New rules should apply, since a new probe should be run.
186 127 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT,
187 RunUntilIdle(); 128 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 } 129 }
217 130
218 } // namespace 131 } // namespace
219 132
220 } // namespace chrome_browser_net 133 } // namespace chrome_browser_net
OLDNEW
« no previous file with comments | « chrome/browser/net/dns_probe_service.cc ('k') | chrome/browser/net/dns_probe_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698