• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 "base/bind.h"
6 #include "base/memory/weak_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/net/dns_probe_runner.h"
10 #include "chrome/browser/net/dns_probe_test_util.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "net/dns/dns_client.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 using base::MessageLoopForIO;
16 using base::RunLoop;
17 using content::TestBrowserThreadBundle;
18 using net::DnsClient;
19 using net::DnsConfig;
20 using net::MockDnsClientRule;
21 
22 namespace chrome_browser_net {
23 
24 namespace {
25 
26 class TestDnsProbeRunnerCallback {
27  public:
TestDnsProbeRunnerCallback()28   TestDnsProbeRunnerCallback()
29       : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled,
30                              base::Unretained(this))),
31         called_(false) {}
32 
callback() const33   const base::Closure& callback() const { return callback_; }
called() const34   bool called() const { return called_; }
35 
36  private:
OnCalled()37   void OnCalled() {
38     EXPECT_FALSE(called_);
39     called_ = true;
40   }
41 
42   base::Closure callback_;
43   bool called_;
44 };
45 
46 class DnsProbeRunnerTest : public testing::Test {
47  protected:
48   void RunTest(MockDnsClientRule::Result query_result,
49                DnsProbeRunner::Result expected_probe_result);
50 
51   TestBrowserThreadBundle bundle_;
52   DnsProbeRunner runner_;
53 };
54 
RunTest(MockDnsClientRule::Result query_result,DnsProbeRunner::Result expected_probe_result)55 void DnsProbeRunnerTest::RunTest(
56     MockDnsClientRule::Result query_result,
57     DnsProbeRunner::Result expected_probe_result) {
58   TestDnsProbeRunnerCallback callback;
59 
60   runner_.SetClient(CreateMockDnsClientForProbes(query_result));
61   runner_.RunProbe(callback.callback());
62   EXPECT_TRUE(runner_.IsRunning());
63 
64   RunLoop().RunUntilIdle();
65   EXPECT_FALSE(runner_.IsRunning());
66   EXPECT_TRUE(callback.called());
67   EXPECT_EQ(expected_probe_result, runner_.result());
68 }
69 
TEST_F(DnsProbeRunnerTest,Probe_OK)70 TEST_F(DnsProbeRunnerTest, Probe_OK) {
71   RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
72 }
73 
TEST_F(DnsProbeRunnerTest,Probe_EMPTY)74 TEST_F(DnsProbeRunnerTest, Probe_EMPTY) {
75   RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
76 }
77 
TEST_F(DnsProbeRunnerTest,Probe_TIMEOUT)78 TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) {
79   RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE);
80 }
81 
TEST_F(DnsProbeRunnerTest,Probe_FAIL)82 TEST_F(DnsProbeRunnerTest, Probe_FAIL) {
83   RunTest(MockDnsClientRule::FAIL, DnsProbeRunner::INCORRECT);
84 }
85 
TEST_F(DnsProbeRunnerTest,TwoProbes)86 TEST_F(DnsProbeRunnerTest, TwoProbes) {
87   RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
88   RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
89 }
90 
TEST_F(DnsProbeRunnerTest,InvalidDnsConfig)91 TEST_F(DnsProbeRunnerTest, InvalidDnsConfig) {
92   scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
93   DnsConfig empty_config;
94   dns_client->SetConfig(empty_config);
95   ASSERT_EQ(NULL, dns_client->GetTransactionFactory());
96   runner_.SetClient(dns_client.Pass());
97 
98   TestDnsProbeRunnerCallback callback;
99 
100   runner_.RunProbe(callback.callback());
101   EXPECT_TRUE(runner_.IsRunning());
102 
103   RunLoop().RunUntilIdle();
104   EXPECT_FALSE(runner_.IsRunning());
105   EXPECT_TRUE(callback.called());
106   EXPECT_EQ(DnsProbeRunner::UNKNOWN, runner_.result());
107 }
108 
109 }  // namespace
110 
111 }  // namespace chrome_browser_net
112