blob: 41ef352abad4c2061d8a9a5e8abf1cf0b8ec4fc4 [file] [log] [blame]
Peter Qiud670d032014-06-03 15:04:43 -07001// 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 Chancd477322014-10-17 14:19:30 -07008#include <memory>
Peter Qiud670d032014-06-03 15:04:43 -07009#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 Qiud670d032014-06-03 15:04:43 -070015#include <base/memory/weak_ptr.h>
16#include <gtest/gtest_prod.h> // for FRIEND_TEST
17
Peter Qiu8d6b5972014-10-28 15:33:34 -070018#include "shill/net/ip_address.h"
Peter Qiud670d032014-06-03 15:04:43 -070019#include "shill/refptr_types.h"
20
21namespace shill {
22
23class DNSClient;
24class Error;
25class 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.
38class DNSServerTester {
39 public:
40 enum Status {
41 kStatusFailure,
42 kStatusSuccess,
43 };
44
45 DNSServerTester(ConnectionRefPtr connection,
Paul Stewarta794cd62015-06-16 13:13:10 -070046 EventDispatcher* dispatcher,
47 const std::vector<std::string>& dns_servers,
Peter Qiud670d032014-06-03 15:04:43 -070048 const bool retry_until_success,
Paul Stewarta794cd62015-06-16 13:13:10 -070049 const base::Callback<void(const Status)>& callback);
Peter Qiud670d032014-06-03 15:04:43 -070050 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 Stewarta794cd62015-06-16 13:13:10 -070074 void DNSClientCallback(const Error& error, const IPAddress& ip);
Peter Qiud670d032014-06-03 15:04:43 -070075
76 ConnectionRefPtr connection_;
Paul Stewarta794cd62015-06-16 13:13:10 -070077 EventDispatcher* dispatcher_;
Peter Qiud670d032014-06-03 15:04:43 -070078 // 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 Stewarta794cd62015-06-16 13:13:10 -070084 base::Callback<void(const Error&, const IPAddress&)>
Peter Qiud670d032014-06-03 15:04:43 -070085 dns_client_callback_;
Ben Chancd477322014-10-17 14:19:30 -070086 std::unique_ptr<DNSClient> dns_test_client_;
Peter Qiud670d032014-06-03 15:04:43 -070087
88 DISALLOW_COPY_AND_ASSIGN(DNSServerTester);
89};
90
91} // namespace shill
92
93#endif // SHILL_DNS_SERVER_TESTER_H_