[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 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 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 5 | #include "chrome/browser/net/dns_probe_runner.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/memory/weak_ptr.h" |
[email protected] | b292d76 | 2013-07-17 21:22:09 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 12 | #include "base/run_loop.h" |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 13 | #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 | |
| 18 | using base::MessageLoopForIO; |
| 19 | using base::RunLoop; |
| 20 | using content::TestBrowserThreadBundle; |
[email protected] | c122e60 | 2013-07-19 19:46:39 | [diff] [blame] | 21 | using net::DnsClient; |
| 22 | using net::DnsConfig; |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 23 | using net::MockDnsClientRule; |
| 24 | |
| 25 | namespace chrome_browser_net { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class 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 | |
| 49 | class DnsProbeRunnerTest : public testing::Test { |
| 50 | protected: |
eroman | 1efc237c | 2016-12-14 00:00:45 | [diff] [blame] | 51 | void RunTest(MockDnsClientRule::ResultType query_result, |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 52 | DnsProbeRunner::Result expected_probe_result); |
| 53 | |
| 54 | TestBrowserThreadBundle bundle_; |
| 55 | DnsProbeRunner runner_; |
| 56 | }; |
| 57 | |
| 58 | void DnsProbeRunnerTest::RunTest( |
eroman | 1efc237c | 2016-12-14 00:00:45 | [diff] [blame] | 59 | MockDnsClientRule::ResultType query_result, |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 60 | 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 | |
| 73 | TEST_F(DnsProbeRunnerTest, Probe_OK) { |
| 74 | RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT); |
| 75 | } |
| 76 | |
| 77 | TEST_F(DnsProbeRunnerTest, Probe_EMPTY) { |
| 78 | RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT); |
| 79 | } |
| 80 | |
| 81 | TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) { |
| 82 | RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE); |
| 83 | } |
| 84 | |
[email protected] | a210eef9 | 2013-07-19 19:06:12 | [diff] [blame] | 85 | TEST_F(DnsProbeRunnerTest, Probe_FAIL) { |
| 86 | RunTest(MockDnsClientRule::FAIL, DnsProbeRunner::INCORRECT); |
[email protected] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(DnsProbeRunnerTest, TwoProbes) { |
| 90 | RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT); |
| 91 | RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT); |
| 92 | } |
| 93 | |
[email protected] | c122e60 | 2013-07-19 19:46:39 | [diff] [blame] | 94 | TEST_F(DnsProbeRunnerTest, InvalidDnsConfig) { |
dcheng | 4e7c042 | 2016-04-14 00:59:05 | [diff] [blame] | 95 | std::unique_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); |
[email protected] | c122e60 | 2013-07-19 19:46:39 | [diff] [blame] | 96 | DnsConfig empty_config; |
| 97 | dns_client->SetConfig(empty_config); |
| 98 | ASSERT_EQ(NULL, dns_client->GetTransactionFactory()); |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 99 | runner_.SetClient(std::move(dns_client)); |
[email protected] | c122e60 | 2013-07-19 19:46:39 | [diff] [blame] | 100 | |
| 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] | 2ea1efe | 2013-07-17 05:23:13 | [diff] [blame] | 112 | } // namespace |
| 113 | |
| 114 | } // namespace chrome_browser_net |