ctrl_dhcp4_srv_unittest.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2011 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 <iostream>
  16. #include <fstream>
  17. #include <sstream>
  18. #include <arpa/inet.h>
  19. #include <gtest/gtest.h>
  20. #include <dhcp/dhcp4.h>
  21. #include <dhcp4/ctrl_dhcp4_srv.h>
  22. #include <config/ccsession.h>
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::dhcp;
  26. using namespace isc::asiolink;
  27. using namespace isc::data;
  28. using namespace isc::config;
  29. namespace {
  30. class NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv {
  31. // "naked" DHCPv4 server, exposes internal fields
  32. public:
  33. NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(DHCP4_SERVER_PORT + 10000) { }
  34. };
  35. class CtrlDhcpv4SrvTest : public ::testing::Test {
  36. public:
  37. CtrlDhcpv4SrvTest() {
  38. }
  39. ~CtrlDhcpv4SrvTest() {
  40. };
  41. };
  42. TEST_F(CtrlDhcpv4SrvTest, commands) {
  43. ControlledDhcpv4Srv* srv = NULL;
  44. ASSERT_NO_THROW({
  45. srv = new ControlledDhcpv4Srv(DHCP4_SERVER_PORT + 10000);
  46. });
  47. // use empty parameters list
  48. ElementPtr params(new isc::data::MapElement());
  49. int rcode = -1;
  50. // case 1: send bogus command
  51. ConstElementPtr result = ControlledDhcpv4Srv::execDhcpv4ServerCommand("blah", params);
  52. ConstElementPtr comment = parseAnswer(rcode, result);
  53. EXPECT_EQ(1, rcode); // expect failure (no such command as blah)
  54. // case 1: send shutdown command without any parameters
  55. result = ControlledDhcpv4Srv::execDhcpv4ServerCommand("shutdown", params);
  56. comment = parseAnswer(rcode, result);
  57. EXPECT_EQ(0, rcode); // expect success
  58. const pid_t pid(getpid());
  59. ConstElementPtr x(new isc::data::IntElement(pid));
  60. params->set("pid", x);
  61. // case 2: send shutdown command with 1 parameter: pid
  62. result = ControlledDhcpv4Srv::execDhcpv4ServerCommand("shutdown", params);
  63. comment = parseAnswer(rcode, result);
  64. EXPECT_EQ(0, rcode); // expect success
  65. delete srv;
  66. }
  67. } // end of anonymous namespace