d2_unittest.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. /// @file d2_unittest.h Defines classes for testing Dhcpv4srv with D2ClientMgr
  15. #ifndef D2_UNITTEST_H
  16. #define D2_UNITTEST_H
  17. #include <dhcp4/dhcp4_srv.h>
  18. #include <config/ccsession.h>
  19. #include <gtest/gtest.h>
  20. namespace isc {
  21. namespace dhcp {
  22. namespace test {
  23. /// @brief Test derivation of Dhcpv4Srv class used in D2 testing.
  24. /// Use of this class allows the intervention at strategic points in testing
  25. /// by permitting overridden methods and access to scope protected members.
  26. class D2Dhcpv4Srv : public Dhcpv4Srv {
  27. public:
  28. /// @brief Counts the number of times the client error handler is called.
  29. int error_count_;
  30. /// @brief Constructor
  31. D2Dhcpv4Srv()
  32. : Dhcpv4Srv(0, false, false), error_count_(0) {
  33. }
  34. /// @brief virtual Destructor.
  35. virtual ~D2Dhcpv4Srv() {
  36. }
  37. /// @brief Override the error handler.
  38. virtual void d2ClientErrorHandler(const dhcp_ddns::NameChangeSender::
  39. Result result,
  40. dhcp_ddns::NameChangeRequestPtr& ncr);
  41. };
  42. /// @brief Test fixture which permits testing the interaction between the
  43. /// D2ClientMgr and Dhcpv4Srv.
  44. class Dhcp4SrvD2Test : public ::testing::Test {
  45. public:
  46. /// @brief Mnemonic constants for calls to configuration methods.
  47. static const bool SHOULD_PASS = true;
  48. static const bool SHOULD_FAIL = false;
  49. /// @brief Constructor
  50. Dhcp4SrvD2Test();
  51. /// @brief virtual Destructor
  52. virtual ~Dhcp4SrvD2Test();
  53. /// @brief Resets the CfgMgr singleton to defaults.
  54. /// Primarily used in the test destructor as gtest doesn't exit between
  55. /// tests.
  56. /// @todo CfgMgr should provide a method to reset everything or maybe
  57. /// reconstruct the singleton.
  58. void reset();
  59. /// @brief Configures the server with D2 enabled or disabled
  60. ///
  61. /// Constructs a configuration string including dhcp-ddns with the
  62. /// parameters given and passes it into the server's configuration handler.
  63. ///
  64. /// @param enable_updates value to assign to the enable-updates parameter
  65. /// @param exp_result indicates if configuration should pass or fail
  66. /// @param server_ip IP address for the D2 server
  67. /// @param port port for the D2 server
  68. /// @param sender_ip NCR sender's IP address
  69. /// @param sender_port NCR sender port
  70. /// @param max_queue_size maximum number of NCRs allowed in sender's queue
  71. void configureD2(bool enable_updates, bool exp_result = SHOULD_PASS,
  72. const std::string& server_ip = "127.0.0.1",
  73. const size_t port = 53001,
  74. const std::string& sender_ip = "0.0.0.0",
  75. const size_t sender_port = 0,
  76. const size_t max_queue_size = 1024);
  77. /// @brief Configures the server with the given configuration
  78. ///
  79. /// Passes the given configuration string into the server's configuration
  80. /// handler. It accepts a flag indicating whether or not the configuration
  81. /// is expected to succeed or fail. This permits testing the server's
  82. /// response to both valid and invalid configurations.
  83. ///
  84. /// @param config JSON string containing the configuration
  85. /// @param exp_result indicates if configuration should pass or fail
  86. void configure(const std::string& config, bool exp_result = SHOULD_PASS);
  87. /// @brief Contructs a NameChangeRequest message from a fixed JSON string.
  88. ///
  89. /// @param dhcid_id_num Integer value to use as the DHCID.
  90. dhcp_ddns::NameChangeRequestPtr buildTestNcr(uint32_t
  91. dhcid_id_num = 0xdeadbeef);
  92. /// @brief Stores the return code of the last configuration attempt.
  93. int rcode_;
  94. /// @brief Stores the message component of the last configuration tattempt.
  95. isc::data::ConstElementPtr comment_;
  96. /// @brief Server object under test.
  97. D2Dhcpv4Srv srv_;
  98. };
  99. }; // end of isc::dhcp::test namespace
  100. }; // end of isc::dhcp namespace
  101. }; // end of isc namespace
  102. #endif // D2_UNITTEST_H