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_service.h"
6
7 #include "base/bind.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 "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"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using base::MessageLoopForIO;
20 using base::RunLoop;
21 using chrome_common_net::DnsProbeStatus;
22 using content::TestBrowserThreadBundle;
23 using net::MockDnsClientRule;
24
25 namespace chrome_browser_net {
26
27 namespace {
28
29 class DnsProbeServiceTest : public testing::Test {
30 public:
DnsProbeServiceTest()31 DnsProbeServiceTest()
32 : callback_called_(false),
33 callback_result_(chrome_common_net::DNS_PROBE_MAX) {
34 }
35
Probe()36 void Probe() {
37 service_.ProbeDns(base::Bind(&DnsProbeServiceTest::ProbeCallback,
38 base::Unretained(this)));
39 }
40
Reset()41 void Reset() {
42 callback_called_ = false;
43 }
44
45 protected:
SetRules(MockDnsClientRule::Result system_query_result,MockDnsClientRule::Result public_query_result)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));
52 }
53
RunTest(MockDnsClientRule::Result system_query_result,MockDnsClientRule::Result public_query_result,DnsProbeStatus expected_result)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
ClearCachedResult()66 void ClearCachedResult() {
67 service_.ClearCachedResultForTesting();
68 }
69
70 private:
ProbeCallback(DnsProbeStatus result)71 void ProbeCallback(DnsProbeStatus result) {
72 EXPECT_FALSE(callback_called_);
73 callback_called_ = true;
74 callback_result_ = result;
75 }
76
77 DnsProbeService service_;
78 bool callback_called_;
79 DnsProbeStatus callback_result_;
80 TestBrowserThreadBundle bundle_;
81 };
82
TEST_F(DnsProbeServiceTest,Probe_OK_OK)83 TEST_F(DnsProbeServiceTest, Probe_OK_OK) {
84 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK,
85 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
86 }
87
TEST_F(DnsProbeServiceTest,Probe_TIMEOUT_OK)88 TEST_F(DnsProbeServiceTest, Probe_TIMEOUT_OK) {
89 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::OK,
90 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG);
91 }
92
TEST_F(DnsProbeServiceTest,Probe_TIMEOUT_TIMEOUT)93 TEST_F(DnsProbeServiceTest, Probe_TIMEOUT_TIMEOUT) {
94 RunTest(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT,
95 chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET);
96 }
97
TEST_F(DnsProbeServiceTest,Probe_OK_FAIL)98 TEST_F(DnsProbeServiceTest, Probe_OK_FAIL) {
99 RunTest(MockDnsClientRule::OK, MockDnsClientRule::FAIL,
100 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
101 }
102
TEST_F(DnsProbeServiceTest,Probe_FAIL_OK)103 TEST_F(DnsProbeServiceTest, Probe_FAIL_OK) {
104 RunTest(MockDnsClientRule::FAIL, MockDnsClientRule::OK,
105 chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG);
106 }
107
TEST_F(DnsProbeServiceTest,Probe_FAIL_FAIL)108 TEST_F(DnsProbeServiceTest, Probe_FAIL_FAIL) {
109 RunTest(MockDnsClientRule::FAIL, MockDnsClientRule::FAIL,
110 chrome_common_net::DNS_PROBE_FINISHED_INCONCLUSIVE);
111 }
112
TEST_F(DnsProbeServiceTest,Cache)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);
119 }
120
TEST_F(DnsProbeServiceTest,Expire)121 TEST_F(DnsProbeServiceTest, Expire) {
122 RunTest(MockDnsClientRule::OK, MockDnsClientRule::OK,
123 chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
124 // Pretend cache expires.
125 ClearCachedResult();
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);
129 }
130
131 } // namespace
132
133 } // namespace chrome_browser_net
134