host_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 basic SARR scenarios against a server configured with one subnet
  69. // containing two reservations. One reservation with a hostname, one
  70. // without a hostname. Scenarios:
  71. //
  72. // - Verify that a client when matched to a host reservation with a hostname
  73. // gets that reservation and the lease hostname matches the reserved hostname
  74. //
  75. // - Verify that a client when matched to a host reservation without a hostname
  76. // gets that reservation and the lease hostname is blank
  77. //
  78. // - Verify that a client that does not match a host reservation gets a dynamic
  79. // lease and the hostname for the lease is blank.
  80. //
  81. TEST_F(HostTest, basicSarrs) {
  82. Dhcp6Client client;
  83. configure(CONFIGS[0], *client.getServer());
  84. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  85. getCfgSubnets6()->getAll();
  86. ASSERT_EQ(1, subnets->size());
  87. // Configure client to request IA_NA and aAppend IA_NA option
  88. // to the client's message.
  89. client.setDUID("01:02:03:04");
  90. client.useNA();
  91. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  92. // Perform 4-way exchange.
  93. ASSERT_NO_THROW(client.doSARR());
  94. // Verify that the client we got the reserved address
  95. ASSERT_EQ(1, client.getLeaseNum());
  96. Lease6 lease_client = client.getLease(0);
  97. EXPECT_EQ("2001:db8:1:1::babe", lease_client.addr_.toText());
  98. // Check that the server recorded the lease.
  99. // and lease has reserved hostname
  100. Lease6Ptr lease_server = checkLease(lease_client);
  101. ASSERT_TRUE(lease_server);
  102. EXPECT_EQ("alice", lease_server->hostname_);
  103. // Now redo the client, adding one to the DUID
  104. client.clearConfig();
  105. client.modifyDUID();
  106. client.useNA();
  107. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  108. // Perform 4-way exchange.
  109. ASSERT_NO_THROW(client.doSARR());
  110. // Verify that the client we got the reserved address
  111. ASSERT_EQ(1, client.getLeaseNum());
  112. lease_client = client.getLease(0);
  113. EXPECT_EQ("2001:db8:1:1::babf", lease_client.addr_.toText());
  114. // Check that the server recorded the lease.
  115. // and that the server lease has NO hostname
  116. lease_server = checkLease(lease_client);
  117. ASSERT_TRUE(lease_server);
  118. EXPECT_EQ("", lease_server->hostname_);
  119. // Now redo the client with yet another DUID and verify that
  120. // we get a dynamic address.
  121. client.clearConfig();
  122. client.modifyDUID();
  123. client.useNA();
  124. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  125. // Perform 4-way exchange.
  126. ASSERT_NO_THROW(client.doSARR());
  127. // Verify that the client got a dynamic address
  128. ASSERT_EQ(1, client.getLeaseNum());
  129. lease_client = client.getLease(0);
  130. EXPECT_EQ("2001:db8:1:1::", lease_client.addr_.toText());
  131. // Check that the server recorded the lease.
  132. // and that the server lease has NO hostname
  133. lease_server = checkLease(lease_client);
  134. ASSERT_TRUE(lease_server);
  135. EXPECT_EQ("", lease_server->hostname_);
  136. }
  137. // Test basic SARR and renew situation with a client that matches a host
  138. // reservation
  139. TEST_F(HostTest, sarrAndRenew) {
  140. Dhcp6Client client;
  141. configure(CONFIGS[0], *client.getServer());
  142. // Configure client to request IA_NA.
  143. client.useNA();
  144. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  145. getCfgSubnets6()->getAll();
  146. ASSERT_EQ(1, subnets->size());
  147. // Configure client to request IA_NA and aAppend IA_NA option
  148. // to the client's message.
  149. client.setDUID("01:02:03:04");
  150. client.useNA();
  151. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  152. // Perform 4-way exchange.
  153. ASSERT_NO_THROW(client.doSARR());
  154. // Now play with time
  155. client.fastFwdTime(1000);
  156. // Verify that the client we got the reserved address
  157. ASSERT_EQ(1, client.getLeaseNum());
  158. Lease6 lease_client = client.getLease(0);
  159. EXPECT_EQ("2001:db8:1:1::babe", lease_client.addr_.toText());
  160. // Send Renew message to the server.
  161. ASSERT_NO_THROW(client.doRenew());
  162. // Verify that we got an extended lease back
  163. ASSERT_EQ(1, client.getLeaseNum());
  164. Lease6 lease_client2 = client.getLease(0);
  165. EXPECT_EQ("2001:db8:1:1::babe", lease_client2.addr_.toText());
  166. // The client's lease should have been extended. The client will
  167. // update the cltt to current time when the lease gets extended.
  168. ASSERT_GE(lease_client2.cltt_ - lease_client.cltt_, 1000);
  169. // Make sure, that the client's lease matches the lease held by the
  170. // server and that we have the reserved host name.
  171. Lease6Ptr lease_server2 = checkLease(lease_client2);
  172. EXPECT_TRUE(lease_server2);
  173. EXPECT_EQ("alice", lease_server2->hostname_);
  174. }
  175. // Test basic SARR and rebind situation with a client that matches a host
  176. // reservation.
  177. TEST_F(HostTest, sarrAndRebind) {
  178. Dhcp6Client client;
  179. configure(CONFIGS[0], *client.getServer());
  180. // Configure client to request IA_NA.
  181. client.useNA();
  182. const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
  183. getCfgSubnets6()->getAll();
  184. ASSERT_EQ(1, subnets->size());
  185. // Configure client to request IA_NA and aAppend IA_NA option
  186. // to the client's message.
  187. client.setDUID("01:02:03:04");
  188. client.useNA();
  189. ASSERT_NO_THROW(client.useHint(100, 200, 64, "2001:db8:1:1::dead:beef"));
  190. // Perform 4-way exchange.
  191. ASSERT_NO_THROW(client.doSARR());
  192. // Now play with time
  193. client.fastFwdTime(1000);
  194. // Verify that the client we got the reserved address
  195. ASSERT_EQ(1, client.getLeaseNum());
  196. Lease6 lease_client = client.getLease(0);
  197. EXPECT_EQ("2001:db8:1:1::babe", lease_client.addr_.toText());
  198. // Send Rebind message to the server.
  199. ASSERT_NO_THROW(client.doRebind());
  200. // Verify that we got an extended lease back
  201. ASSERT_EQ(1, client.getLeaseNum());
  202. Lease6 lease_client2 = client.getLease(0);
  203. EXPECT_EQ("2001:db8:1:1::babe", lease_client2.addr_.toText());
  204. // The client's lease should have been extended. The client will
  205. // update the cltt to current time when the lease gets extended.
  206. ASSERT_GE(lease_client2.cltt_ - lease_client.cltt_, 1000);
  207. // Make sure, that the client's lease matches the lease held by the
  208. // server and that we have the reserved host name.
  209. Lease6Ptr lease_server2 = checkLease(lease_client2);
  210. EXPECT_TRUE(lease_server2);
  211. EXPECT_EQ("alice", lease_server2->hostname_);
  212. }
  213. } // end of anonymous namespace