Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 "shill/dns_server_tester.h" |
| 6 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 7 | #include <memory> |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include <base/bind.h> |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | #include "shill/mock_connection.h" |
| 15 | #include "shill/mock_control.h" |
| 16 | #include "shill/mock_device_info.h" |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 17 | #include "shill/mock_dns_client.h" |
| 18 | #include "shill/mock_dns_client_factory.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 19 | #include "shill/mock_event_dispatcher.h" |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 20 | #include "shill/mock_time.h" |
| 21 | |
| 22 | using base::Bind; |
| 23 | using base::Callback; |
| 24 | using base::Unretained; |
| 25 | using std::string; |
| 26 | using std::vector; |
| 27 | using testing::_; |
| 28 | using testing::AtLeast; |
| 29 | using testing::DoAll; |
| 30 | using testing::InSequence; |
| 31 | using testing::Mock; |
| 32 | using testing::NiceMock; |
| 33 | using testing::Return; |
| 34 | using testing::ReturnRef; |
| 35 | using testing::SetArgumentPointee; |
| 36 | using testing::StrictMock; |
| 37 | using testing::Test; |
| 38 | |
| 39 | namespace shill { |
| 40 | |
| 41 | namespace { |
| 42 | const char kInterfaceName[] = "int0"; |
| 43 | const char kDNSServer0[] = "8.8.8.8"; |
| 44 | const char kDNSServer1[] = "8.8.4.4"; |
| 45 | const char *kDNSServers[] = { kDNSServer0, kDNSServer1 }; |
| 46 | } // namespace |
| 47 | |
| 48 | class DNSServerTesterTest : public Test { |
| 49 | public: |
| 50 | DNSServerTesterTest() |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 51 | : device_info_( |
| 52 | new NiceMock<MockDeviceInfo>(&control_, nullptr, nullptr, nullptr)), |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 53 | connection_(new StrictMock<MockConnection>(device_info_.get())), |
| 54 | interface_name_(kInterfaceName), |
| 55 | dns_servers_(kDNSServers, kDNSServers + 2) {} |
| 56 | |
| 57 | virtual void SetUp() { |
| 58 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 59 | .WillRepeatedly(ReturnRef(interface_name_)); |
| 60 | dns_server_tester_.reset( |
| 61 | new DNSServerTester(connection_.get(), |
| 62 | &dispatcher_, |
| 63 | dns_servers_, |
| 64 | false, |
| 65 | callback_target_.result_callback())); |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | class CallbackTarget { |
| 70 | public: |
| 71 | CallbackTarget() |
| 72 | : result_callback_(Bind(&CallbackTarget::ResultCallback, |
| 73 | Unretained(this))) { |
| 74 | } |
| 75 | |
| 76 | MOCK_METHOD1(ResultCallback, void(const DNSServerTester::Status status)); |
| 77 | Callback<void(const DNSServerTester::Status)> &result_callback() { |
| 78 | return result_callback_; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | Callback<void(const DNSServerTester::Status)> result_callback_; |
| 83 | }; |
| 84 | |
| 85 | DNSServerTester *dns_server_tester() { return dns_server_tester_.get(); } |
| 86 | MockEventDispatcher &dispatcher() { return dispatcher_; } |
| 87 | CallbackTarget &callback_target() { return callback_target_; } |
| 88 | |
| 89 | void ExpectReset() { |
| 90 | EXPECT_TRUE(callback_target_.result_callback().Equals( |
| 91 | dns_server_tester_->dns_result_callback_)); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | StrictMock<MockEventDispatcher> dispatcher_; |
| 96 | MockControl control_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 97 | std::unique_ptr<MockDeviceInfo> device_info_; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 98 | scoped_refptr<MockConnection> connection_; |
| 99 | CallbackTarget callback_target_; |
| 100 | const string interface_name_; |
| 101 | vector<string> dns_servers_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 102 | std::unique_ptr<DNSServerTester> dns_server_tester_; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | TEST_F(DNSServerTesterTest, Constructor) { |
| 106 | ExpectReset(); |
| 107 | } |
| 108 | |
| 109 | TEST_F(DNSServerTesterTest, StartAttempt) { |
| 110 | // Start attempt with no delay. |
| 111 | EXPECT_CALL(dispatcher(), PostDelayedTask(_, 0)); |
| 112 | dns_server_tester()->StartAttempt(0); |
| 113 | |
| 114 | // Start attempt with delay. |
| 115 | EXPECT_CALL(dispatcher(), PostDelayedTask(_, 100)); |
| 116 | dns_server_tester()->StartAttempt(100); |
| 117 | } |
| 118 | |
| 119 | TEST_F(DNSServerTesterTest, StartAttemptTask) { |
| 120 | // Setup mock DNS test client. |
| 121 | MockDNSClient *dns_test_client = new MockDNSClient(); |
| 122 | dns_server_tester()->dns_test_client_.reset(dns_test_client); |
| 123 | |
| 124 | // DNS test task started successfully. |
| 125 | EXPECT_CALL(*dns_test_client, Start(_, _)).WillOnce(Return(true)); |
| 126 | EXPECT_CALL(callback_target(), ResultCallback(_)).Times(0); |
| 127 | dns_server_tester()->StartAttemptTask(); |
| 128 | Mock::VerifyAndClearExpectations(dns_test_client); |
| 129 | |
| 130 | // DNS test task failed to start. |
| 131 | EXPECT_CALL(*dns_test_client, Start(_, _)).WillOnce(Return(false)); |
| 132 | EXPECT_CALL(callback_target(), |
| 133 | ResultCallback(DNSServerTester::kStatusFailure)).Times(1); |
| 134 | dns_server_tester()->StartAttemptTask(); |
| 135 | Mock::VerifyAndClearExpectations(dns_test_client); |
| 136 | } |
| 137 | |
| 138 | TEST_F(DNSServerTesterTest, AttemptCompleted) { |
| 139 | // DNS test attempt succeed with retry_until_success_ not set. |
| 140 | dns_server_tester()->retry_until_success_ = false; |
| 141 | EXPECT_CALL(callback_target(), |
| 142 | ResultCallback(DNSServerTester::kStatusSuccess)).Times(1); |
| 143 | dns_server_tester()->CompleteAttempt(DNSServerTester::kStatusSuccess); |
| 144 | |
| 145 | // DNS test attempt succeed with retry_until_success_ being set. |
| 146 | dns_server_tester()->retry_until_success_ = true; |
| 147 | EXPECT_CALL(callback_target(), |
| 148 | ResultCallback(DNSServerTester::kStatusSuccess)).Times(1); |
| 149 | dns_server_tester()->CompleteAttempt(DNSServerTester::kStatusSuccess); |
| 150 | |
| 151 | // DNS test attempt failed with retry_until_success_ not set. |
| 152 | dns_server_tester()->retry_until_success_ = false; |
| 153 | EXPECT_CALL(callback_target(), |
| 154 | ResultCallback(DNSServerTester::kStatusFailure)).Times(1); |
| 155 | dns_server_tester()->CompleteAttempt(DNSServerTester::kStatusFailure); |
| 156 | |
| 157 | // DNS test attempt failed with retry_until_success_ being set. |
| 158 | dns_server_tester()->retry_until_success_ = true; |
| 159 | EXPECT_CALL(callback_target(), ResultCallback(_)).Times(0); |
| 160 | EXPECT_CALL(dispatcher(), PostDelayedTask(_, _)).Times(1); |
| 161 | dns_server_tester()->CompleteAttempt(DNSServerTester::kStatusFailure); |
| 162 | } |
| 163 | |
| 164 | TEST_F(DNSServerTesterTest, StopAttempt) { |
| 165 | // Setup mock DNS test client. |
| 166 | MockDNSClient *dns_test_client = new MockDNSClient(); |
| 167 | dns_server_tester()->dns_test_client_.reset(dns_test_client); |
| 168 | |
| 169 | // DNS test task started successfully. |
| 170 | EXPECT_CALL(*dns_test_client, Start(_, _)).WillOnce(Return(true)); |
| 171 | EXPECT_CALL(callback_target(), ResultCallback(_)).Times(0); |
| 172 | dns_server_tester()->StartAttemptTask(); |
| 173 | Mock::VerifyAndClearExpectations(dns_test_client); |
| 174 | |
| 175 | // Stop the DNS test attempt. |
| 176 | EXPECT_CALL(*dns_test_client, Stop()).Times(1); |
| 177 | EXPECT_CALL(callback_target(), ResultCallback(_)).Times(0); |
| 178 | dns_server_tester()->StopAttempt(); |
| 179 | Mock::VerifyAndClearExpectations(dns_test_client); |
| 180 | } |
| 181 | |
| 182 | } // namespace shill |