Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 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 | #ifndef SHILL_DNS_SERVER_TESTER_H_ |
| 6 | #define SHILL_DNS_SERVER_TESTER_H_ |
| 7 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 8 | #include <memory> |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/callback.h> |
| 13 | #include <base/cancelable_callback.h> |
| 14 | #include <base/memory/ref_counted.h> |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
| 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 17 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 18 | #include "shill/net/ip_address.h" |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 19 | #include "shill/refptr_types.h" |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | class DNSClient; |
| 24 | class Error; |
| 25 | class EventDispatcher; |
| 26 | |
| 27 | // The DNSServerTester class implements the DNS health check |
| 28 | // facility in shill, which is responsible for checking to see |
| 29 | // if the given DNS servers are working or not. |
| 30 | // |
| 31 | // The tester support two modes of operation, continuous and |
| 32 | // non-continuous mode. With continuous mode (retry_until_success_ flag is set), |
| 33 | // the tester will continue to perform DNS test until the DNS test succeed or |
| 34 | // the DNS client failed to start. The callback is only invoke when the test |
| 35 | // succeed or we failed to start the DNS client. With non-continuous mode, |
| 36 | // only one DNS test is performed. And the callback is invoked regardless of |
| 37 | // the result of the test. |
| 38 | class DNSServerTester { |
| 39 | public: |
| 40 | enum Status { |
| 41 | kStatusFailure, |
| 42 | kStatusSuccess, |
| 43 | }; |
| 44 | |
| 45 | DNSServerTester(ConnectionRefPtr connection, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 46 | EventDispatcher* dispatcher, |
| 47 | const std::vector<std::string>& dns_servers, |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 48 | const bool retry_until_success, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 49 | const base::Callback<void(const Status)>& callback); |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 50 | virtual ~DNSServerTester(); |
| 51 | |
| 52 | // Start the test. |
| 53 | virtual void Start(); |
| 54 | |
| 55 | // End the current DNS test process if one exist, and do not call |
| 56 | // the callback. |
| 57 | virtual void Stop(); |
| 58 | |
| 59 | private: |
| 60 | friend class DNSServerTesterTest; |
| 61 | FRIEND_TEST(DNSServerTesterTest, StartAttempt); |
| 62 | FRIEND_TEST(DNSServerTesterTest, StartAttemptTask); |
| 63 | FRIEND_TEST(DNSServerTesterTest, AttemptCompleted); |
| 64 | FRIEND_TEST(DNSServerTesterTest, StopAttempt); |
| 65 | |
| 66 | static const char kDNSTestHostname[]; |
| 67 | static const int kDNSTestRetryIntervalMilliseconds; |
| 68 | static const int kDNSTimeoutMilliseconds; |
| 69 | |
| 70 | void StartAttempt(int delay_ms); |
| 71 | void StartAttemptTask(); |
| 72 | void StopAttempt(); |
| 73 | void CompleteAttempt(Status status); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 74 | void DNSClientCallback(const Error& error, const IPAddress& ip); |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 75 | |
| 76 | ConnectionRefPtr connection_; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 77 | EventDispatcher* dispatcher_; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 78 | // Flag indicating to continuously probing the DNS servers until it succeed. |
| 79 | // The callback is only invoke when the test succeed or test failed to start. |
| 80 | bool retry_until_success_; |
| 81 | base::WeakPtrFactory<DNSServerTester> weak_ptr_factory_; |
| 82 | base::CancelableClosure start_attempt_; |
| 83 | base::Callback<void(const Status)> dns_result_callback_; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 84 | base::Callback<void(const Error&, const IPAddress&)> |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 85 | dns_client_callback_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 86 | std::unique_ptr<DNSClient> dns_test_client_; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(DNSServerTester); |
| 89 | }; |
| 90 | |
| 91 | } // namespace shill |
| 92 | |
| 93 | #endif // SHILL_DNS_SERVER_TESTER_H_ |