blob: c32804b932c8150e98d31e2a5141e019131988e8 [file] [log] [blame]
[email protected]2ea1efe2013-07-17 05:23:131// 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
dchenge73d8520c2015-12-27 01:19:095#include "chrome/browser/net/dns_probe_runner.h"
6
7#include <utility>
8
[email protected]2ea1efe2013-07-17 05:23:139#include "base/bind.h"
10#include "base/memory/weak_ptr.h"
[email protected]b292d762013-07-17 21:22:0911#include "base/message_loop/message_loop.h"
[email protected]2ea1efe2013-07-17 05:23:1312#include "base/run_loop.h"
[email protected]2ea1efe2013-07-17 05:23:1313#include "chrome/browser/net/dns_probe_test_util.h"
14#include "content/public/test/test_browser_thread_bundle.h"
15#include "net/dns/dns_client.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18using base::MessageLoopForIO;
19using base::RunLoop;
20using content::TestBrowserThreadBundle;
[email protected]c122e602013-07-19 19:46:3921using net::DnsClient;
22using net::DnsConfig;
[email protected]2ea1efe2013-07-17 05:23:1323using net::MockDnsClientRule;
24
25namespace chrome_browser_net {
26
27namespace {
28
29class TestDnsProbeRunnerCallback {
30 public:
31 TestDnsProbeRunnerCallback()
32 : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled,
33 base::Unretained(this))),
34 called_(false) {}
35
36 const base::Closure& callback() const { return callback_; }
37 bool called() const { return called_; }
38
39 private:
40 void OnCalled() {
41 EXPECT_FALSE(called_);
42 called_ = true;
43 }
44
45 base::Closure callback_;
46 bool called_;
47};
48
49class DnsProbeRunnerTest : public testing::Test {
50 protected:
eroman1efc237c2016-12-14 00:00:4551 void RunTest(MockDnsClientRule::ResultType query_result,
[email protected]2ea1efe2013-07-17 05:23:1352 DnsProbeRunner::Result expected_probe_result);
53
54 TestBrowserThreadBundle bundle_;
55 DnsProbeRunner runner_;
56};
57
58void DnsProbeRunnerTest::RunTest(
eroman1efc237c2016-12-14 00:00:4559 MockDnsClientRule::ResultType query_result,
[email protected]2ea1efe2013-07-17 05:23:1360 DnsProbeRunner::Result expected_probe_result) {
61 TestDnsProbeRunnerCallback callback;
62
63 runner_.SetClient(CreateMockDnsClientForProbes(query_result));
64 runner_.RunProbe(callback.callback());
65 EXPECT_TRUE(runner_.IsRunning());
66
67 RunLoop().RunUntilIdle();
68 EXPECT_FALSE(runner_.IsRunning());
69 EXPECT_TRUE(callback.called());
70 EXPECT_EQ(expected_probe_result, runner_.result());
71}
72
73TEST_F(DnsProbeRunnerTest, Probe_OK) {
74 RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
75}
76
77TEST_F(DnsProbeRunnerTest, Probe_EMPTY) {
78 RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
79}
80
81TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) {
82 RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE);
83}
84
[email protected]a210eef92013-07-19 19:06:1285TEST_F(DnsProbeRunnerTest, Probe_FAIL) {
86 RunTest(MockDnsClientRule::FAIL, DnsProbeRunner::INCORRECT);
[email protected]2ea1efe2013-07-17 05:23:1387}
88
89TEST_F(DnsProbeRunnerTest, TwoProbes) {
90 RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
91 RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
92}
93
[email protected]c122e602013-07-19 19:46:3994TEST_F(DnsProbeRunnerTest, InvalidDnsConfig) {
dcheng4e7c0422016-04-14 00:59:0595 std::unique_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
[email protected]c122e602013-07-19 19:46:3996 DnsConfig empty_config;
97 dns_client->SetConfig(empty_config);
98 ASSERT_EQ(NULL, dns_client->GetTransactionFactory());
dchenge73d8520c2015-12-27 01:19:0999 runner_.SetClient(std::move(dns_client));
[email protected]c122e602013-07-19 19:46:39100
101 TestDnsProbeRunnerCallback callback;
102
103 runner_.RunProbe(callback.callback());
104 EXPECT_TRUE(runner_.IsRunning());
105
106 RunLoop().RunUntilIdle();
107 EXPECT_FALSE(runner_.IsRunning());
108 EXPECT_TRUE(callback.called());
109 EXPECT_EQ(DnsProbeRunner::UNKNOWN, runner_.result());
110}
111
[email protected]2ea1efe2013-07-17 05:23:13112} // namespace
113
114} // namespace chrome_browser_net