1 // Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 /// @file d2_unittest.h Defines classes for testing Dhcpv4srv with D2ClientMgr
8 
9 #ifndef D2_UNITTEST_H
10 #define D2_UNITTEST_H
11 
12 #include <dhcp4/dhcp4_srv.h>
13 #include <cc/command_interpreter.h>
14 
15 #include <gtest/gtest.h>
16 
17 namespace isc {
18 namespace dhcp {
19 namespace test {
20 
21 /// @brief Test derivation of Dhcpv4Srv class used in D2 testing.
22 /// Use of this class allows the intervention at strategic points in testing
23 /// by permitting overridden methods and access to scope protected members.
24 class D2Dhcpv4Srv : public  Dhcpv4Srv {
25 public:
26     /// @brief Counts the number of times the client error handler is called.
27     int error_count_;
28 
29     /// @brief Constructor
D2Dhcpv4Srv()30     D2Dhcpv4Srv()
31         : Dhcpv4Srv(0, false, false), error_count_(0) {
32     }
33 
34     /// @brief virtual Destructor.
~D2Dhcpv4Srv()35     virtual ~D2Dhcpv4Srv() {
36     }
37 
38     /// @brief Override the error handler.
39     virtual void d2ClientErrorHandler(const dhcp_ddns::NameChangeSender::
40                                       Result result,
41                                       dhcp_ddns::NameChangeRequestPtr& ncr);
42 };
43 
44 /// @brief Test fixture which permits testing the interaction between the
45 /// D2ClientMgr and Dhcpv4Srv.
46 class Dhcp4SrvD2Test : public ::testing::Test {
47 public:
48     /// @brief Mnemonic constants for calls to configuration methods.
49     static const bool SHOULD_PASS = true;
50     static const bool SHOULD_FAIL = false;
51 
52     /// @brief Constructor
53     Dhcp4SrvD2Test();
54 
55     /// @brief virtual Destructor
56     virtual ~Dhcp4SrvD2Test();
57 
58     /// @brief Resets the CfgMgr singleton to defaults.
59     /// Primarily used in the test destructor as gtest doesn't exit between
60     /// tests.
61     /// @todo CfgMgr should provide a method to reset everything or maybe
62     /// reconstruct the singleton.
63     void reset();
64 
65     /// @brief Configures the server with D2 enabled or disabled
66     ///
67     /// Constructs a configuration string including dhcp-ddns with the
68     /// parameters given and passes it into the server's configuration handler.
69     ///
70     /// @param enable_updates value to assign to the enable-updates parameter
71     /// @param exp_result indicates if configuration should pass or fail
72     /// @param server_ip IP address for the D2 server
73     /// @param port  port for the D2 server
74     /// @param sender_ip NCR sender's IP address
75     /// @param sender_port NCR sender port
76     /// @param max_queue_size maximum number of NCRs allowed in sender's queue
77     void configureD2(bool enable_updates, bool exp_result = SHOULD_PASS,
78                      const std::string& server_ip = "127.0.0.1",
79                      const size_t port = 53001,
80                      const std::string& sender_ip = "0.0.0.0",
81                      const size_t sender_port = 0,
82                      const size_t max_queue_size = 1024);
83 
84     /// @brief Configures the server with the given configuration
85     ///
86     /// Passes the given configuration string into the server's configuration
87     /// handler.  It accepts a flag indicating whether or not the configuration
88     /// is expected to succeed or fail.  This permits testing the server's
89     /// response to both valid and invalid configurations.
90     ///
91     /// @param config JSON string containing the configuration
92     /// @param exp_result indicates if configuration should pass or fail
93     void configure(const std::string& config, bool exp_result = SHOULD_PASS);
94 
95     /// @brief Constructs a NameChangeRequest message from a fixed JSON string.
96     ///
97     /// @param dhcid_id_num Integer value to use as the DHCID.
98     dhcp_ddns::NameChangeRequestPtr buildTestNcr(uint32_t
99                                                  dhcid_id_num = 0xdeadbeef);
100 
101     /// @brief Stores the return code of the last configuration attempt.
102     int rcode_;
103 
104     /// @brief Stores the message component of the last configuration attempt.
105     isc::data::ConstElementPtr comment_;
106 
107     /// @brief Server object under test.
108     D2Dhcpv4Srv srv_;
109 };
110 
111 }; // end of isc::dhcp::test namespace
112 }; // end of isc::dhcp namespace
113 }; // end of isc namespace
114 
115 #endif // D2_UNITTEST_H
116