dhcp4_dhcp4o6_ipc_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (C) 2015-2016 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 <asiolink/io_address.h>
  16. #include <dhcp/pkt4o6.h>
  17. #include <dhcp/pkt6.h>
  18. #include <dhcp/tests/iface_mgr_test_config.h>
  19. #include <dhcp4/dhcp4_dhcp4o6_ipc.h>
  20. #include <dhcpsrv/cfgmgr.h>
  21. #include <dhcpsrv/testutils/dhcp4o6_test_ipc.h>
  22. #include <gtest/gtest.h>
  23. #include <stdint.h>
  24. using namespace isc;
  25. using namespace isc::asiolink;
  26. using namespace isc::dhcp;
  27. using namespace isc::dhcp::test;
  28. using namespace isc::util;
  29. namespace {
  30. /// @brief Port number used in tests.
  31. const uint16_t TEST_PORT = 32000;
  32. /// @brief Define short name for the test IPC.
  33. typedef Dhcp4o6TestIpc TestIpc;
  34. /// @brief Test fixture class for DHCPv4 endpoint of DHCPv4o6 IPC.
  35. class Dhcp4to6IpcTest : public ::testing::Test {
  36. public:
  37. /// @brief Constructor
  38. ///
  39. /// Configures IPC to use a test port. It also provides a fake
  40. /// configuration of interfaces.
  41. Dhcp4to6IpcTest()
  42. : iface_mgr_test_config_(true) {
  43. configurePort(TEST_PORT);
  44. }
  45. /// @brief Configure DHCP4o6 port.
  46. ///
  47. /// @param port New port.
  48. void configurePort(uint16_t port);
  49. /// @brief Creates an instance of the DHCPv4o6 Message option.
  50. ///
  51. /// @return Pointer to the instance of the DHCPv4-query Message option.
  52. OptionPtr createDHCPv4MsgOption() const;
  53. private:
  54. /// @brief Provides fake configuration of interfaces.
  55. IfaceMgrTestConfig iface_mgr_test_config_;
  56. };
  57. void
  58. Dhcp4to6IpcTest::configurePort(uint16_t port) {
  59. CfgMgr::instance().getStagingCfg()->setDhcp4o6Port(port);
  60. }
  61. OptionPtr
  62. Dhcp4to6IpcTest::createDHCPv4MsgOption() const {
  63. // Create the DHCPv4 message.
  64. Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
  65. // Make a wire representation of the DHCPv4 message.
  66. pkt->pack();
  67. OutputBuffer& output_buffer = pkt->getBuffer();
  68. const uint8_t* data = static_cast<const uint8_t*>(output_buffer.getData());
  69. OptionBuffer option_buffer(data, data + output_buffer.getLength());
  70. // Create the DHCPv4 Message option holding the created message.
  71. OptionPtr opt_msg(new Option(Option::V6, D6O_DHCPV4_MSG, option_buffer));
  72. return (opt_msg);
  73. }
  74. // This test verifies that the IPC returns an error when trying to bind
  75. // to the out of range port.
  76. TEST_F(Dhcp4to6IpcTest, invalidPortError) {
  77. // Create instance of the IPC endpoint under test with out-of-range port.
  78. configurePort(65535);
  79. Dhcp4to6Ipc& ipc = Dhcp4to6Ipc::instance();
  80. EXPECT_THROW(ipc.open(), isc::OutOfRange);
  81. }
  82. // This test verifies that the DHCPv4 endpoint of the DHCPv4o6 IPC can
  83. // receive messages.
  84. TEST_F(Dhcp4to6IpcTest, receive) {
  85. // Create instance of the IPC endpoint under test.
  86. Dhcp4to6Ipc& ipc = Dhcp4to6Ipc::instance();
  87. // Create instance of the IPC endpoint being used as a source of messages.
  88. TestIpc src_ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V6);
  89. // Open both endpoints.
  90. ASSERT_NO_THROW(ipc.open());
  91. ASSERT_NO_THROW(src_ipc.open());
  92. // Create message to be sent over IPC.
  93. Pkt6Ptr pkt(new Pkt6(DHCPV6_DHCPV4_QUERY, 1234));
  94. pkt->addOption(createDHCPv4MsgOption());
  95. pkt->setIface("eth0");
  96. pkt->setRemoteAddr(IOAddress("2001:db8:1::123"));
  97. ASSERT_NO_THROW(pkt->pack());
  98. // Send and wait up to 1 second to receive it.
  99. ASSERT_NO_THROW(src_ipc.send(pkt));
  100. ASSERT_NO_THROW(IfaceMgr::instance().receive6(1, 0));
  101. // Make sure that the message has been received.
  102. Pkt4o6Ptr pkt_received = ipc.getReceived();
  103. ASSERT_TRUE(pkt_received);
  104. Pkt6Ptr pkt6_received = pkt_received->getPkt6();
  105. ASSERT_TRUE(pkt6_received);
  106. EXPECT_EQ("eth0", pkt6_received->getIface());
  107. EXPECT_EQ("2001:db8:1::123", pkt6_received->getRemoteAddr().toText());
  108. }
  109. // This test verifies that message with multiple DHCPv4 query options
  110. // is rejected.
  111. TEST_F(Dhcp4to6IpcTest, receiveMultipleQueries) {
  112. // Create instance of the IPC endpoint under test.
  113. Dhcp4to6Ipc& ipc = Dhcp4to6Ipc::instance();
  114. // Create instance of the IPC endpoint being used as a source of messages.
  115. TestIpc src_ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V6);
  116. // Open both endpoints.
  117. ASSERT_NO_THROW(ipc.open());
  118. ASSERT_NO_THROW(src_ipc.open());
  119. // Create message to be sent over IPC.
  120. Pkt6Ptr pkt(new Pkt6(DHCPV6_DHCPV4_QUERY, 1234));
  121. // Add two DHCPv4 query options.
  122. pkt->addOption(createDHCPv4MsgOption());
  123. pkt->addOption(createDHCPv4MsgOption());
  124. pkt->setIface("eth0");
  125. pkt->setRemoteAddr(IOAddress("2001:db8:1::123"));
  126. ASSERT_NO_THROW(pkt->pack());
  127. // Send message.
  128. ASSERT_NO_THROW(src_ipc.send(pkt));
  129. // Reception handler should throw exception.
  130. EXPECT_THROW(IfaceMgr::instance().receive6(1, 0), Dhcp4o6IpcError);
  131. }
  132. // This test verifies that message with no DHCPv4 query options is rejected.
  133. TEST_F(Dhcp4to6IpcTest, receiveNoQueries) {
  134. // Create instance of the IPC endpoint under test.
  135. Dhcp4to6Ipc& ipc = Dhcp4to6Ipc::instance();
  136. // Create instance of the IPC endpoint being used as a source of messages.
  137. TestIpc src_ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V6);
  138. // Open both endpoints.
  139. ASSERT_NO_THROW(ipc.open());
  140. ASSERT_NO_THROW(src_ipc.open());
  141. // Create message to be sent over IPC without DHCPv4 query option.
  142. Pkt6Ptr pkt(new Pkt6(DHCPV6_DHCPV4_QUERY, 1234));
  143. pkt->setIface("eth0");
  144. pkt->setRemoteAddr(IOAddress("2001:db8:1::123"));
  145. ASSERT_NO_THROW(pkt->pack());
  146. // Send message.
  147. ASSERT_NO_THROW(src_ipc.send(pkt));
  148. // Reception handler should throw exception.
  149. EXPECT_THROW(IfaceMgr::instance().receive6(1, 0), Dhcp4o6IpcError);
  150. }
  151. } // end of anonymous namespace