ctrl_dhcp4_srv_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2012-2013 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. #include <config.h>
  15. #include <config/ccsession.h>
  16. #include <dhcp/dhcp4.h>
  17. #include <dhcp4/ctrl_dhcp4_srv.h>
  18. #include <hooks/hooks_manager.h>
  19. #include "marker_file.h"
  20. #include "test_libraries.h"
  21. #include <boost/scoped_ptr.hpp>
  22. #include <gtest/gtest.h>
  23. #include <fstream>
  24. #include <iostream>
  25. #include <sstream>
  26. #include <arpa/inet.h>
  27. #include <unistd.h>
  28. using namespace std;
  29. using namespace isc;
  30. using namespace isc::asiolink;
  31. using namespace isc::config;
  32. using namespace isc::data;
  33. using namespace isc::dhcp;
  34. using namespace isc::dhcp::test;
  35. using namespace isc::hooks;
  36. namespace {
  37. class NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv {
  38. // "Naked" DHCPv4 server, exposes internal fields
  39. public:
  40. NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(DHCP4_SERVER_PORT + 10000) { }
  41. };
  42. class CtrlDhcpv4SrvTest : public ::testing::Test {
  43. public:
  44. CtrlDhcpv4SrvTest() {
  45. reset();
  46. }
  47. ~CtrlDhcpv4SrvTest() {
  48. reset();
  49. };
  50. /// @brief Reset hooks data
  51. ///
  52. /// Resets the data for the hooks-related portion of the test by ensuring
  53. /// that no libraries are loaded and that any marker files are deleted.
  54. void reset() {
  55. // Unload any previously-loaded libraries.
  56. HooksManager::unloadLibraries();
  57. // Get rid of any marker files.
  58. static_cast<void>(unlink(LOAD_MARKER_FILE));
  59. static_cast<void>(unlink(UNLOAD_MARKER_FILE));
  60. }
  61. };
  62. TEST_F(CtrlDhcpv4SrvTest, commands) {
  63. boost::scoped_ptr<ControlledDhcpv4Srv> srv;
  64. ASSERT_NO_THROW(
  65. srv.reset(new ControlledDhcpv4Srv(DHCP4_SERVER_PORT + 10000))
  66. );
  67. // Use empty parameters list
  68. ElementPtr params(new isc::data::MapElement());
  69. int rcode = -1;
  70. // Case 1: send bogus command
  71. ConstElementPtr result = ControlledDhcpv4Srv::execDhcpv4ServerCommand("blah", params);
  72. ConstElementPtr comment = parseAnswer(rcode, result);
  73. EXPECT_EQ(1, rcode); // expect failure (no such command as blah)
  74. // Case 2: send shutdown command without any parameters
  75. result = ControlledDhcpv4Srv::execDhcpv4ServerCommand("shutdown", params);
  76. comment = parseAnswer(rcode, result);
  77. EXPECT_EQ(0, rcode); // expect success
  78. const pid_t pid(getpid());
  79. ConstElementPtr x(new isc::data::IntElement(pid));
  80. params->set("pid", x);
  81. // Case 3: send shutdown command with 1 parameter: pid
  82. result = ControlledDhcpv4Srv::execDhcpv4ServerCommand("shutdown", params);
  83. comment = parseAnswer(rcode, result);
  84. EXPECT_EQ(0, rcode); // expect success
  85. }
  86. // Check that the "libreload" command will reload libraries
  87. TEST_F(CtrlDhcpv4SrvTest, libreload) {
  88. // Ensure no marker files to start with.
  89. ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  90. ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  91. // Load two libraries
  92. std::vector<std::string> libraries;
  93. libraries.push_back(CALLOUT_LIBRARY_1);
  94. libraries.push_back(CALLOUT_LIBRARY_2);
  95. HooksManager::loadLibraries(libraries);
  96. // Check they are loaded.
  97. std::vector<std::string> loaded_libraries =
  98. HooksManager::getLibraryNames();
  99. ASSERT_TRUE(libraries == loaded_libraries);
  100. // ... which also included checking that the marker file created by the
  101. // load functions exists and holds the correct value (of "12" - the
  102. // first library appends "1" to the file, the second appends "2"). Also
  103. // check that the unload marker file does not yet exist.
  104. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  105. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  106. // Now execute the "libreload" command. This should cause the libraries
  107. // to unload and to reload.
  108. // Use empty parameters list
  109. ElementPtr params(new isc::data::MapElement());
  110. int rcode = -1;
  111. ConstElementPtr result =
  112. ControlledDhcpv4Srv::execDhcpv4ServerCommand("libreload", params);
  113. ConstElementPtr comment = parseAnswer(rcode, result);
  114. EXPECT_EQ(0, rcode); // Expect success
  115. // Check that the libraries have unloaded and reloaded. The libraries are
  116. // unloaded in the reverse order to which they are loaded. When they load,
  117. // they should append information to the loading marker file.
  118. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  119. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212"));
  120. }
  121. } // End of anonymous namespace