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.
5 #include "chrome/browser/net/dns_probe_service.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/net/dns_probe_runner.h"
13 #include "chrome/browser/net/dns_probe_test_util.h"
14 #include "components/error_page/common/net_error_info.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "net/dns/dns_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using base::MessageLoopForIO
;
21 using chrome_common_net::DnsProbeStatus
;
22 using content::TestBrowserThreadBundle
;
23 using net::MockDnsClientRule
;
25 namespace chrome_browser_net
{
29 class DnsProbeServiceTest
: public testing::Test
{
32 : callback_called_(false),
33 callback_result_(chrome_common_net::DNS_PROBE_MAX
) {
37 service_
.ProbeDns(base::Bind(&DnsProbeServiceTest::ProbeCallback
,
38 base::Unretained(this)));
42 callback_called_
= false;
46 void SetRules(MockDnsClientRule::Result system_query_result
,
47 MockDnsClientRule::Result public_query_result
) {
48 service_
.SetSystemClientForTesting(
49 CreateMockDnsClientForProbes(system_query_result
));
50 service_
.SetPublicClientForTesting(
51 CreateMockDnsClientForProbes(public_query_result
));
54 void RunTest(MockDnsClientRule::Result system_query_result
,
55 MockDnsClientRule::Result public_query_result
,
56 DnsProbeStatus expected_result
) {
58 SetRules(system_query_result
, public_query_result
);
61 RunLoop().RunUntilIdle();
62 EXPECT_TRUE(callback_called_
);
63 EXPECT_EQ(expected_result
, callback_result_
);
66 void ClearCachedResult() {
67 service_
.ClearCachedResultForTesting();
71 void ProbeCallback(DnsProbeStatus result
) {
72 EXPECT_FALSE(callback_called_
);
73 callback_called_
= true;
74 callback_result_
= result
;
77 TestBrowserThreadBundle bundle_
;
78 DnsProbeService service_
;
79 bool callback_called_
;
80 DnsProbeStatus callback_result_
;
83 TEST_F(DnsProbeServiceTest
, Probe_OK_OK
) {
84 RunTest(MockDnsClientRule::OK
, MockDnsClientRule::OK
,
85 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN
);
88 TEST_F(DnsProbeServiceTest
, Probe_TIMEOUT_OK
) {
89 RunTest(MockDnsClientRule::TIMEOUT
, MockDnsClientRule::OK
,
90 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG
);
93 TEST_F(DnsProbeServiceTest
, Probe_TIMEOUT_TIMEOUT
) {
94 RunTest(MockDnsClientRule::TIMEOUT
, MockDnsClientRule::TIMEOUT
,
95 chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET
);
98 TEST_F(DnsProbeServiceTest
, Probe_OK_FAIL
) {
99 RunTest(MockDnsClientRule::OK
, MockDnsClientRule::FAIL
,
100 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN
);
103 TEST_F(DnsProbeServiceTest
, Probe_FAIL_OK
) {
104 RunTest(MockDnsClientRule::FAIL
, MockDnsClientRule::OK
,
105 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG
);
108 TEST_F(DnsProbeServiceTest
, Probe_FAIL_FAIL
) {
109 RunTest(MockDnsClientRule::FAIL
, MockDnsClientRule::FAIL
,
110 chrome_common_net::DNS_PROBE_FINISHED_INCONCLUSIVE
);
113 TEST_F(DnsProbeServiceTest
, Cache
) {
114 RunTest(MockDnsClientRule::OK
, MockDnsClientRule::OK
,
115 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN
);
116 // Cached NXDOMAIN result should persist, not the result from the new rules.
117 RunTest(MockDnsClientRule::TIMEOUT
, MockDnsClientRule::TIMEOUT
,
118 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN
);
121 TEST_F(DnsProbeServiceTest
, Expire
) {
122 RunTest(MockDnsClientRule::OK
, MockDnsClientRule::OK
,
123 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN
);
124 // Pretend cache expires.
126 // New rules should apply, since a new probe should be run.
127 RunTest(MockDnsClientRule::TIMEOUT
, MockDnsClientRule::TIMEOUT
,
128 chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET
);
133 } // namespace chrome_browser_net