renew_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <asiolink/io_address.h>
  16. #include <cc/data.h>
  17. #include <dhcp/tests/iface_mgr_test_config.h>
  18. #include <dhcp6/json_config_parser.h>
  19. #include <dhcp6/tests/dhcp6_message_test.h>
  20. using namespace isc;
  21. using namespace isc::asiolink;
  22. using namespace isc::data;
  23. using namespace isc::dhcp;
  24. using namespace isc::dhcp::test;
  25. namespace {
  26. /// @brief Set of JSON configurations used throughout the Renew tests.
  27. ///
  28. /// - Configuration 0:
  29. /// - only addresses (no prefixes)
  30. /// - 1 subnet with 2001:db8:1::/64 pool
  31. ///
  32. /// - Configuration 1:
  33. /// - only prefixes (no addresses)
  34. /// - prefix pool: 3000::/72
  35. ///
  36. /// - Configuration 2:
  37. /// - addresses and prefixes
  38. /// - 1 subnet with one address pool and one prefix pool
  39. /// - address pool: 2001:db8:1::/64
  40. /// - prefix pool: 3000::/72
  41. ///
  42. const char* RENEW_CONFIGS[] = {
  43. // Configuration 0
  44. "{ \"interfaces-config\": {"
  45. " \"interfaces\": [ \"*\" ]"
  46. "},"
  47. "\"preferred-lifetime\": 3000,"
  48. "\"rebind-timer\": 2000, "
  49. "\"renew-timer\": 1000, "
  50. "\"subnet6\": [ { "
  51. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  52. " \"subnet\": \"2001:db8:1::/48\", "
  53. " \"interface-id\": \"\","
  54. " \"interface\": \"eth0\""
  55. " } ],"
  56. "\"valid-lifetime\": 4000 }",
  57. // Configuration 1
  58. "{ \"interfaces-config\": {"
  59. " \"interfaces\": [ \"*\" ]"
  60. "},"
  61. "\"preferred-lifetime\": 3000,"
  62. "\"rebind-timer\": 2000, "
  63. "\"renew-timer\": 1000, "
  64. "\"subnet6\": [ { "
  65. " \"pd-pools\": ["
  66. " { \"prefix\": \"3000::\", "
  67. " \"prefix-len\": 72, "
  68. " \"delegated-len\": 80"
  69. " } ],"
  70. " \"subnet\": \"2001:db8:1::/48\", "
  71. " \"interface-id\": \"\","
  72. " \"interface\": \"eth0\""
  73. " } ],"
  74. "\"valid-lifetime\": 4000 }",
  75. // Configuration 2
  76. "{ \"interfaces-config\": {"
  77. " \"interfaces\": [ \"*\" ]"
  78. "},"
  79. "\"preferred-lifetime\": 3000,"
  80. "\"rebind-timer\": 2000, "
  81. "\"renew-timer\": 1000, "
  82. "\"subnet6\": [ { "
  83. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  84. " \"pd-pools\": ["
  85. " { \"prefix\": \"3000::\", "
  86. " \"prefix-len\": 72, "
  87. " \"delegated-len\": 80"
  88. " } ],"
  89. " \"subnet\": \"2001:db8:1::/48\", "
  90. " \"interface-id\": \"\","
  91. " \"interface\": \"eth0\""
  92. " } ],"
  93. "\"valid-lifetime\": 4000 }"
  94. };
  95. /// @brief Test fixture class for testing Renew.
  96. class RenewTest : public Dhcpv6MessageTest {
  97. public:
  98. /// @brief Constructor.
  99. ///
  100. /// Sets up fake interfaces.
  101. RenewTest()
  102. : Dhcpv6MessageTest(), na_iaid_(1234), pd_iaid_(5678) {
  103. }
  104. /// @brief IAID used for IA_NA.
  105. uint32_t na_iaid_;
  106. /// @brief IAID used for IA_PD.
  107. uint32_t pd_iaid_;
  108. };
  109. // This test verifies that the client can request the prefix delegation
  110. // while it is renewing an address lease.
  111. TEST_F(RenewTest, requestPrefixInRenew) {
  112. Dhcp6Client client;
  113. // Configure client to request IA_NA and IA_PD.
  114. client.useNA(na_iaid_);
  115. client.usePD(pd_iaid_);
  116. // Configure the server with NA pools only.
  117. ASSERT_NO_THROW(configure(RENEW_CONFIGS[0], *client.getServer()));
  118. // Perform 4-way exchange.
  119. ASSERT_NO_THROW(client.doSARR());
  120. // Simulate aging of leases.
  121. client.fastFwdTime(1000);
  122. // Make sure that the client has acquired NA lease.
  123. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  124. ASSERT_EQ(1, leases_client_na.size());
  125. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  126. // The client should not acquire a PD lease.
  127. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  128. ASSERT_TRUE(leases_client_pd.empty());
  129. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  130. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  131. ASSERT_NO_THROW(client.doRenew());
  132. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  133. ASSERT_TRUE(leases_client_pd.empty());
  134. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  135. std::vector<Lease6> leases_client_na_renewed =
  136. client.getLeasesByType(Lease::TYPE_NA);
  137. ASSERT_EQ(1, leases_client_na_renewed.size());
  138. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  139. // Reconfigure the server to use both NA and PD pools.
  140. configure(RENEW_CONFIGS[2], *client.getServer());
  141. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  142. ASSERT_NO_THROW(client.doRenew());
  143. // Make sure that the client has acquired NA lease.
  144. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  145. ASSERT_EQ(1, leases_client_na_renewed.size());
  146. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  147. // The lease should have been renewed.
  148. EXPECT_EQ(1000, leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_);
  149. // The client should now also acquire a PD lease.
  150. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  151. ASSERT_EQ(1, leases_client_pd.size());
  152. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  153. }
  154. // This test verifies that the client can request a prefix delegation
  155. // with a hint, while it is renewing an address lease.
  156. TEST_F(RenewTest, requestPrefixInRenewUseHint) {
  157. Dhcp6Client client;
  158. // Configure client to request IA_NA and IA_PD.
  159. client.useNA(na_iaid_);
  160. client.usePD(pd_iaid_);
  161. // Configure the server with NA pools only.
  162. ASSERT_NO_THROW(configure(RENEW_CONFIGS[0], *client.getServer()));
  163. // Perform 4-way exchange.
  164. ASSERT_NO_THROW(client.doSARR());
  165. // Simulate aging of leases.
  166. client.fastFwdTime(1000);
  167. // Make sure that the client has acquired NA lease.
  168. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  169. ASSERT_EQ(1, leases_client_na.size());
  170. // The client should not acquire a PD lease.
  171. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  172. ASSERT_TRUE(leases_client_pd.empty());
  173. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  174. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  175. ASSERT_NO_THROW(client.doRenew());
  176. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  177. ASSERT_TRUE(leases_client_pd.empty());
  178. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  179. std::vector<Lease6> leases_client_na_renewed =
  180. client.getLeasesByType(Lease::TYPE_NA);
  181. ASSERT_EQ(1, leases_client_na_renewed.size());
  182. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  183. // Specify the hint used for IA_PD.
  184. client.useHint(0, 0, 64, "::");
  185. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  186. ASSERT_NO_THROW(client.doRenew());
  187. // Make sure that the client has acquired NA lease.
  188. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  189. ASSERT_EQ(1, leases_client_na_renewed.size());
  190. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  191. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  192. ASSERT_TRUE(leases_client_pd.empty());
  193. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  194. // Reconfigure the server to use both NA and PD pools.
  195. configure(RENEW_CONFIGS[2], *client.getServer());
  196. // Specify the hint used for IA_PD.
  197. client.useHint(0, 0, 64, "::");
  198. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  199. ASSERT_NO_THROW(client.doRenew());
  200. // Make sure that the client has acquired NA lease.
  201. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  202. ASSERT_EQ(1, leases_client_na_renewed.size());
  203. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  204. // The lease should have been renewed.
  205. EXPECT_GE(leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_, 1000);
  206. // The client should now also acquire a PD lease.
  207. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  208. ASSERT_EQ(1, leases_client_pd.size());
  209. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  210. }
  211. // This test verifies that the client can request the prefix delegation
  212. // while it is renewing an address lease.
  213. TEST_F(RenewTest, requestAddressInRenew) {
  214. Dhcp6Client client;
  215. // Configure client to request IA_NA and IA_PD.
  216. client.useNA(na_iaid_);
  217. client.usePD(pd_iaid_);
  218. // Configure the server with PD pools only.
  219. ASSERT_NO_THROW(configure(RENEW_CONFIGS[1], *client.getServer()));
  220. // Perform 4-way exchange.
  221. ASSERT_NO_THROW(client.doSARR());
  222. // Simulate aging of leases.
  223. client.fastFwdTime(1000);
  224. // Make sure that the client has acquired PD lease.
  225. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  226. ASSERT_EQ(1, leases_client_pd.size());
  227. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  228. // The client should not acquire a NA lease.
  229. std::vector<Lease6> leases_client_na =
  230. client.getLeasesByType(Lease::TYPE_NA);
  231. ASSERT_EQ(0, leases_client_na.size());
  232. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  233. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  234. // The server should return NoAddrsAvail status code in this case.
  235. ASSERT_NO_THROW(client.doRenew());
  236. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  237. ASSERT_EQ(0, leases_client_na.size());
  238. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  239. std::vector<Lease6> leases_client_pd_renewed =
  240. client.getLeasesByType(Lease::TYPE_PD);
  241. ASSERT_EQ(1, leases_client_pd_renewed.size());
  242. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  243. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  244. // Reconfigure the server to use both NA and PD pools.
  245. configure(RENEW_CONFIGS[2], *client.getServer());
  246. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  247. ASSERT_NO_THROW(client.doRenew());
  248. // Make sure that the client has renewed PD lease.
  249. leases_client_pd_renewed = client.getLeasesByType(Lease::TYPE_PD);
  250. ASSERT_EQ(1, leases_client_pd_renewed.size());
  251. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  252. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  253. // The client should now also acquire a NA lease.
  254. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  255. ASSERT_EQ(1, leases_client_na.size());
  256. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  257. }
  258. // This test verifies that the client can request address assignment
  259. // while it is renewing an address lease, with a hint.
  260. TEST_F(RenewTest, requestAddressInRenewHint) {
  261. Dhcp6Client client;
  262. // Configure client to request IA_NA and IA_PD.
  263. client.useNA(na_iaid_);
  264. client.usePD(pd_iaid_);
  265. // Configure the server with PD pools only.
  266. ASSERT_NO_THROW(configure(RENEW_CONFIGS[1], *client.getServer()));
  267. // Perform 4-way exchange.
  268. ASSERT_NO_THROW(client.doSARR());
  269. // Simulate aging of leases.
  270. client.fastFwdTime(1000);
  271. // Make sure that the client has acquired PD lease.
  272. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  273. ASSERT_EQ(1, leases_client_pd.size());
  274. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  275. // The client should not acquire a NA lease.
  276. std::vector<Lease6> leases_client_na =
  277. client.getLeasesByType(Lease::TYPE_NA);
  278. ASSERT_EQ(0, leases_client_na.size());
  279. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  280. client.useHint(0, 0, "2001:db8:1::100");
  281. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  282. // The server should return NoAddrsAvail status code in this case.
  283. ASSERT_NO_THROW(client.doRenew());
  284. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  285. // The server should return the hint with the zero lifetimes.
  286. ASSERT_EQ(1, leases_client_na.size());
  287. EXPECT_EQ(0, leases_client_na[0].preferred_lft_);
  288. EXPECT_EQ(0, leases_client_na[0].valid_lft_);
  289. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  290. std::vector<Lease6> leases_client_pd_renewed =
  291. client.getLeasesByType(Lease::TYPE_PD);
  292. ASSERT_EQ(1, leases_client_pd_renewed.size());
  293. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  294. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  295. // Reconfigure the server to use both NA and PD pools.
  296. configure(RENEW_CONFIGS[2], *client.getServer());
  297. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  298. ASSERT_NO_THROW(client.doRenew());
  299. // Make sure that the client has renewed PD lease.
  300. leases_client_pd_renewed = client.getLeasesByType(Lease::TYPE_PD);
  301. ASSERT_EQ(1, leases_client_pd_renewed.size());
  302. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  303. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  304. // The client should now also acquire a NA lease.
  305. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  306. ASSERT_EQ(1, leases_client_na.size());
  307. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  308. }
  309. } // end of anonymous namespace