dhcp6_dhcp4o6_ipc_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/pkt6.h>
  17. #include <dhcp/iface_mgr.h>
  18. #include <dhcp/tests/iface_mgr_test_config.h>
  19. #include <dhcp/tests/pkt_filter6_test_stub.h>
  20. #include <dhcp6/dhcp6_dhcp4o6_ipc.h>
  21. #include <dhcpsrv/cfgmgr.h>
  22. #include <dhcpsrv/testutils/dhcp4o6_test_ipc.h>
  23. #include <gtest/gtest.h>
  24. #include <stdint.h>
  25. using namespace isc;
  26. using namespace isc::asiolink;
  27. using namespace isc::dhcp;
  28. using namespace isc::dhcp::test;
  29. using namespace isc::util;
  30. namespace {
  31. /// @brief Port number used in tests.
  32. const uint16_t TEST_PORT = 32000;
  33. /// @brief Define short name for the test IPC.
  34. typedef Dhcp4o6TestIpc TestIpc;
  35. /// @brief Test fixture class for DHCPv4 endpoint of DHCPv4o6 IPC.
  36. class Dhcp6to4IpcTest : public ::testing::Test {
  37. public:
  38. /// @brief Constructor
  39. ///
  40. /// Configures IPC to use a test port. It also provides a fake
  41. /// configuration of interfaces and opens IPv6 sockets.
  42. Dhcp6to4IpcTest()
  43. : iface_mgr_test_config_(true) {
  44. IfaceMgr::instance().openSockets6();
  45. configurePort(TEST_PORT);
  46. }
  47. /// @brief Configure DHCP4o6 port.
  48. ///
  49. /// @param port New port.
  50. void configurePort(const uint16_t port);
  51. /// @brief Creates an instance of the DHCPv4o6 Message option.
  52. ///
  53. /// @return Pointer to the instance of the DHCPv4-query Message option.
  54. OptionPtr createDHCPv4MsgOption() const;
  55. private:
  56. /// @brief Provides fake configuration of interfaces.
  57. IfaceMgrTestConfig iface_mgr_test_config_;
  58. };
  59. void
  60. Dhcp6to4IpcTest::configurePort(const uint16_t port) {
  61. CfgMgr::instance().getStagingCfg()->setDhcp4o6Port(port);
  62. }
  63. OptionPtr
  64. Dhcp6to4IpcTest::createDHCPv4MsgOption() const {
  65. // Create the DHCPv4 message.
  66. Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
  67. // Make a wire representation of the DHCPv4 message.
  68. pkt->pack();
  69. OutputBuffer& output_buffer = pkt->getBuffer();
  70. const uint8_t* data = static_cast<const uint8_t*>(output_buffer.getData());
  71. OptionBuffer option_buffer(data, data + output_buffer.getLength());
  72. // Create the DHCPv4 Message option holding the created message.
  73. OptionPtr opt_msg(new Option(Option::V6, D6O_DHCPV4_MSG, option_buffer));
  74. return (opt_msg);
  75. }
  76. // This test verifies that the IPC returns an error when trying to bind
  77. // to the out of range port.
  78. TEST_F(Dhcp6to4IpcTest, invalidPortError) {
  79. // Create instance of the IPC endpoint under test with out-of-range port.
  80. configurePort(65535);
  81. Dhcp6to4Ipc& ipc = Dhcp6to4Ipc::instance();
  82. EXPECT_THROW(ipc.open(), isc::OutOfRange);
  83. }
  84. // This test verifies that the DHCPv4 endpoint of the DHCPv4o6 IPC can
  85. // receive messages.
  86. TEST_F(Dhcp6to4IpcTest, receive) {
  87. // Create instance of the IPC endpoint under test.
  88. Dhcp6to4Ipc& ipc = Dhcp6to4Ipc::instance();
  89. // Create instance of the IPC endpoint being used as a source of messages.
  90. TestIpc src_ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
  91. // Open both endpoints.
  92. ASSERT_NO_THROW(ipc.open());
  93. ASSERT_NO_THROW(src_ipc.open());
  94. // Create message to be sent over IPC.
  95. Pkt6Ptr pkt(new Pkt6(DHCPV6_DHCPV4_QUERY, 1234));
  96. pkt->addOption(createDHCPv4MsgOption());
  97. pkt->setIface("eth0");
  98. pkt->setRemoteAddr(IOAddress("2001:db8:1::123"));
  99. ASSERT_NO_THROW(pkt->pack());
  100. // Send and wait up to 1 second to receive it.
  101. ASSERT_NO_THROW(src_ipc.send(pkt));
  102. ASSERT_NO_THROW(IfaceMgr::instance().receive6(1, 0));
  103. #if 0
  104. // The stub packet filter exposes static function to retrieve messages
  105. // sent over the fake sockets/interfaces. This is the message that the
  106. // IPC endpoint should forward to the client after receiving it
  107. // from the DHCPv4 server.
  108. Pkt6Ptr forwarded = PktFilter6TestStub::popSent();
  109. ASSERT_TRUE(forwarded);
  110. // Verify the packet received.
  111. EXPECT_EQ(DHCP6_CLIENT_PORT, forwarded->getRemotePort());
  112. EXPECT_EQ(forwarded->getType(), pkt->getType());
  113. EXPECT_TRUE(forwarded->getOption(D6O_DHCPV4_MSG));
  114. EXPECT_EQ("eth0", forwarded->getIface());
  115. EXPECT_EQ("2001:db8:1::123", forwarded->getRemoteAddr().toText());
  116. #endif
  117. }
  118. } // end of anonymous namespace