summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/dns_probe_runner_unittest.cc
blob: 5020afc0bea87f7f61edb1244af7051669f6cff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "chrome/browser/net/dns_probe_runner.h"
#include "chrome/browser/net/dns_probe_test_util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/dns/dns_client.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::MessageLoopForIO;
using base::RunLoop;
using content::TestBrowserThreadBundle;
using net::DnsClient;
using net::DnsConfig;
using net::MockDnsClientRule;

namespace chrome_browser_net {

namespace {

class TestDnsProbeRunnerCallback {
 public:
  TestDnsProbeRunnerCallback()
      : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled,
                             base::Unretained(this))),
        called_(false) {}

  const base::Closure& callback() const { return callback_; }
  bool called() const { return called_; }

 private:
  void OnCalled() {
    EXPECT_FALSE(called_);
    called_ = true;
  }

  base::Closure callback_;
  bool called_;
};

class DnsProbeRunnerTest : public testing::Test {
 protected:
  void RunTest(MockDnsClientRule::Result query_result,
               DnsProbeRunner::Result expected_probe_result);

  TestBrowserThreadBundle bundle_;
  DnsProbeRunner runner_;
};

void DnsProbeRunnerTest::RunTest(
    MockDnsClientRule::Result query_result,
    DnsProbeRunner::Result expected_probe_result) {
  TestDnsProbeRunnerCallback callback;

  runner_.SetClient(CreateMockDnsClientForProbes(query_result));
  runner_.RunProbe(callback.callback());
  EXPECT_TRUE(runner_.IsRunning());

  RunLoop().RunUntilIdle();
  EXPECT_FALSE(runner_.IsRunning());
  EXPECT_TRUE(callback.called());
  EXPECT_EQ(expected_probe_result, runner_.result());
}

TEST_F(DnsProbeRunnerTest, Probe_OK) {
  RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
}

TEST_F(DnsProbeRunnerTest, Probe_EMPTY) {
  RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
}

TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) {
  RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE);
}

TEST_F(DnsProbeRunnerTest, Probe_FAIL) {
  RunTest(MockDnsClientRule::FAIL, DnsProbeRunner::INCORRECT);
}

TEST_F(DnsProbeRunnerTest, TwoProbes) {
  RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
  RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
}

TEST_F(DnsProbeRunnerTest, InvalidDnsConfig) {
  scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
  DnsConfig empty_config;
  dns_client->SetConfig(empty_config);
  ASSERT_EQ(NULL, dns_client->GetTransactionFactory());
  runner_.SetClient(dns_client.Pass());

  TestDnsProbeRunnerCallback callback;

  runner_.RunProbe(callback.callback());
  EXPECT_TRUE(runner_.IsRunning());

  RunLoop().RunUntilIdle();
  EXPECT_FALSE(runner_.IsRunning());
  EXPECT_TRUE(callback.called());
  EXPECT_EQ(DnsProbeRunner::UNKNOWN, runner_.result());
}

}  // namespace

}  // namespace chrome_browser_net