host_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2015 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 <dhcp/tests/iface_mgr_test_config.h>
  16. #include <dhcp6/tests/dhcp6_test_utils.h>
  17. #include <dhcp6/tests/dhcp6_client.h>
  18. using namespace isc;
  19. using namespace isc::dhcp;
  20. using namespace isc::dhcp::test;
  21. using namespace isc::test;
  22. namespace {
  23. /// @brief Set of JSON configurations used by the Host reservation unit tests.
  24. ///
  25. /// - Configuration 0:
  26. /// Single subnet with two reservations, one with a hostname, one without
  27. const char* CONFIGS[] = {
  28. // Configuration 0:
  29. "{ "
  30. "\"interfaces-config\": {"
  31. " \"interfaces\": [ \"*\" ]"
  32. "},"
  33. "\"valid-lifetime\": 4000, "
  34. "\"preferred-lifetime\": 3000,"
  35. "\"rebind-timer\": 2000, "
  36. "\"renew-timer\": 1000, "
  37. "\"subnet6\": [ "
  38. " { "
  39. " \"subnet\": \"2001:db8:1::/48\", "
  40. " \"pools\": [ { \"pool\": \"2001:db8:1:1::/64\" } ],"
  41. " \"interface\" : \"eth0\" , "
  42. " \"reservations\": ["
  43. " {"
  44. " \"duid\": \"01:02:03:04\","
  45. " \"ip-addresses\": [ \"2001:db8:1:1::babe\" ],"
  46. " \"hostname\": \"alice\""
  47. " },"
  48. " {"
  49. " \"duid\": \"01:02:03:05\","
  50. " \"ip-addresses\": [ \"2001:db8:1:1::babf\" ]"
  51. " } ]"
  52. " } ]"
  53. "}"
  54. };
  55. /// @brief Test fixture class for testing host reservations
  56. class HostTest : public Dhcpv6SrvTest {
  57. public:
  58. /// @brief Constructor.
  59. ///
  60. /// Sets up fake interfaces.
  61. HostTest()
  62. : Dhcpv6SrvTest(),
  63. iface_mgr_test_config_(true) {
  64. }
  65. /// @brief Interface Manager's fake configuration control.
  66. IfaceMgrTestConfig iface_mgr_test_config_;
  67. };
  68. TEST_F(HostTest, basicSARRs) {
  69. Dhcp6Client client;
  70. configure(CONFIGS[0], *client.getServer());
  71. // Make sure we ended-up having expected number of subnets configured.
  72. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  73. getCfgSubnets6()->getAll();
  74. ASSERT_EQ(1, subnets->size());
  75. // Configure client to request IA_NA and aAppend IA_NA option
  76. // to the client's message.
  77. client.setDUID("01:02:03:04");
  78. client.useNA();
  79. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  80. // Perform 4-way exchange.
  81. ASSERT_NO_THROW(client.doSARR());
  82. // Verify that the client we got the reserved address
  83. ASSERT_EQ(1, client.getLeaseNum());
  84. Lease6 lease_client = client.getLease(0);
  85. EXPECT_EQ("2001:db8:1:1::babe", lease_client.addr_.toText());
  86. // Check that the server recorded the lease.
  87. // and lease has reserved hostname
  88. Lease6Ptr lease_server = checkLease(lease_client);
  89. ASSERT_TRUE(lease_server);
  90. EXPECT_EQ("alice", lease_server->hostname_);
  91. // Now redo the client, adding one to the DUID
  92. client.clearConfig();
  93. client.modifyDUID();
  94. client.useNA();
  95. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  96. // Perform 4-way exchange.
  97. ASSERT_NO_THROW(client.doSARR());
  98. // Verify that the client we got the reserved address
  99. ASSERT_EQ(1, client.getLeaseNum());
  100. lease_client = client.getLease(0);
  101. EXPECT_EQ("2001:db8:1:1::babf", lease_client.addr_.toText());
  102. // Check that the server recorded the lease.
  103. // and that the server lease has NO hostname
  104. lease_server = checkLease(lease_client);
  105. ASSERT_TRUE(lease_server);
  106. EXPECT_EQ("", lease_server->hostname_);
  107. // Now redo the client with yet another DUID and verify that
  108. // we get a dynamic address.
  109. client.clearConfig();
  110. client.modifyDUID();
  111. client.useNA();
  112. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  113. // Perform 4-way exchange.
  114. ASSERT_NO_THROW(client.doSARR());
  115. // Verify that the client got a dynamic address
  116. ASSERT_EQ(1, client.getLeaseNum());
  117. lease_client = client.getLease(0);
  118. EXPECT_EQ("2001:db8:1:1::", lease_client.addr_.toText());
  119. // Check that the server recorded the lease.
  120. // and that the server lease has NO hostname
  121. lease_server = checkLease(lease_client);
  122. ASSERT_TRUE(lease_server);
  123. EXPECT_EQ("", lease_server->hostname_);
  124. }
  125. } // end of anonymous namespace