renew_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/io_address.h>
  8. #include <cc/data.h>
  9. #include <dhcp/docsis3_option_defs.h>
  10. #include <dhcp/option_string.h>
  11. #include <dhcp/option_vendor.h>
  12. #include <dhcp/tests/iface_mgr_test_config.h>
  13. #include <dhcp6/json_config_parser.h>
  14. #include <dhcp6/tests/dhcp6_message_test.h>
  15. #include <boost/pointer_cast.hpp>
  16. using namespace isc;
  17. using namespace isc::asiolink;
  18. using namespace isc::data;
  19. using namespace isc::dhcp;
  20. using namespace isc::dhcp::test;
  21. namespace {
  22. /// @brief Set of JSON configurations used throughout the Renew tests.
  23. ///
  24. /// - Configuration 0:
  25. /// - only addresses (no prefixes)
  26. /// - 1 subnet with 2001:db8:1::/64 pool
  27. ///
  28. /// - Configuration 1:
  29. /// - only prefixes (no addresses)
  30. /// - prefix pool: 3000::/72
  31. ///
  32. /// - Configuration 2:
  33. /// - addresses and prefixes
  34. /// - 1 subnet with one address pool and one prefix pool
  35. /// - address pool: 2001:db8:1::/64
  36. /// - prefix pool: 3000::/72
  37. ///
  38. /// - Configuration 3:
  39. /// - only addresses (no prefixes)
  40. /// - 1 subnet with 2001:db8:1::/64 pool
  41. /// - DOCSIS vendor config file sub-option
  42. ///
  43. const char* RENEW_CONFIGS[] = {
  44. // Configuration 0
  45. "{ \"interfaces-config\": {"
  46. " \"interfaces\": [ \"*\" ]"
  47. "},"
  48. "\"preferred-lifetime\": 3000,"
  49. "\"rebind-timer\": 2000, "
  50. "\"renew-timer\": 1000, "
  51. "\"subnet6\": [ { "
  52. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  53. " \"subnet\": \"2001:db8:1::/48\", "
  54. " \"interface-id\": \"\","
  55. " \"interface\": \"eth0\""
  56. " } ],"
  57. "\"valid-lifetime\": 4000 }",
  58. // Configuration 1
  59. "{ \"interfaces-config\": {"
  60. " \"interfaces\": [ \"*\" ]"
  61. "},"
  62. "\"preferred-lifetime\": 3000,"
  63. "\"rebind-timer\": 2000, "
  64. "\"renew-timer\": 1000, "
  65. "\"subnet6\": [ { "
  66. " \"pd-pools\": ["
  67. " { \"prefix\": \"3000::\", "
  68. " \"prefix-len\": 72, "
  69. " \"delegated-len\": 80"
  70. " } ],"
  71. " \"subnet\": \"2001:db8:1::/48\", "
  72. " \"interface-id\": \"\","
  73. " \"interface\": \"eth0\""
  74. " } ],"
  75. "\"valid-lifetime\": 4000 }",
  76. // Configuration 2
  77. "{ \"interfaces-config\": {"
  78. " \"interfaces\": [ \"*\" ]"
  79. "},"
  80. "\"preferred-lifetime\": 3000,"
  81. "\"rebind-timer\": 2000, "
  82. "\"renew-timer\": 1000, "
  83. "\"subnet6\": [ { "
  84. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  85. " \"pd-pools\": ["
  86. " { \"prefix\": \"3000::\", "
  87. " \"prefix-len\": 72, "
  88. " \"delegated-len\": 80"
  89. " } ],"
  90. " \"subnet\": \"2001:db8:1::/48\", "
  91. " \"interface-id\": \"\","
  92. " \"interface\": \"eth0\""
  93. " } ],"
  94. "\"valid-lifetime\": 4000 }",
  95. // Configuration 3
  96. "{ \"interfaces-config\": {"
  97. " \"interfaces\": [ \"*\" ]"
  98. "},"
  99. "\"preferred-lifetime\": 3000,"
  100. "\"rebind-timer\": 2000, "
  101. "\"renew-timer\": 1000, "
  102. " \"option-def\": [ {"
  103. " \"name\": \"config-file\","
  104. " \"code\": 33,"
  105. " \"type\": \"string\","
  106. " \"space\": \"vendor-4491\""
  107. " } ],"
  108. " \"option-data\": [ {"
  109. " \"name\": \"config-file\","
  110. " \"space\": \"vendor-4491\","
  111. " \"data\": \"normal_erouter_v6.cm\""
  112. " }],"
  113. "\"subnet6\": [ { "
  114. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  115. " \"subnet\": \"2001:db8:1::/48\", "
  116. " \"interface-id\": \"\","
  117. " \"interface\": \"eth0\""
  118. " } ],"
  119. "\"valid-lifetime\": 4000 }"
  120. };
  121. /// @brief Test fixture class for testing Renew.
  122. class RenewTest : public Dhcpv6MessageTest {
  123. public:
  124. /// @brief Constructor.
  125. ///
  126. /// Sets up fake interfaces.
  127. RenewTest()
  128. : Dhcpv6MessageTest(), na_iaid_(1234), pd_iaid_(5678) {
  129. }
  130. /// @brief IAID used for IA_NA.
  131. uint32_t na_iaid_;
  132. /// @brief IAID used for IA_PD.
  133. uint32_t pd_iaid_;
  134. };
  135. // This test verifies that the client can request the prefix delegation
  136. // while it is renewing an address lease.
  137. TEST_F(RenewTest, requestPrefixInRenew) {
  138. Dhcp6Client client;
  139. // Configure client to request IA_NA and IA_PD.
  140. client.useNA(na_iaid_);
  141. client.usePD(pd_iaid_);
  142. // Configure the server with NA pools only.
  143. ASSERT_NO_THROW(configure(RENEW_CONFIGS[0], *client.getServer()));
  144. // Perform 4-way exchange.
  145. ASSERT_NO_THROW(client.doSARR());
  146. // Simulate aging of leases.
  147. client.fastFwdTime(1000);
  148. // Make sure that the client has acquired NA lease.
  149. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  150. ASSERT_EQ(1, leases_client_na.size());
  151. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  152. // The client should not acquire a PD lease.
  153. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  154. ASSERT_TRUE(leases_client_pd.empty());
  155. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  156. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  157. ASSERT_NO_THROW(client.doRenew());
  158. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  159. ASSERT_TRUE(leases_client_pd.empty());
  160. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  161. std::vector<Lease6> leases_client_na_renewed =
  162. client.getLeasesByType(Lease::TYPE_NA);
  163. ASSERT_EQ(1, leases_client_na_renewed.size());
  164. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  165. // Reconfigure the server to use both NA and PD pools.
  166. configure(RENEW_CONFIGS[2], *client.getServer());
  167. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  168. ASSERT_NO_THROW(client.doRenew());
  169. // Make sure that the client has acquired NA lease.
  170. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  171. ASSERT_EQ(1, leases_client_na_renewed.size());
  172. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  173. // The lease should have been renewed.
  174. EXPECT_EQ(1000, leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_);
  175. // The client should now also acquire a PD lease.
  176. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  177. ASSERT_EQ(1, leases_client_pd.size());
  178. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  179. }
  180. // This test verifies that the client can request a prefix delegation
  181. // with a hint, while it is renewing an address lease.
  182. TEST_F(RenewTest, requestPrefixInRenewUseHint) {
  183. Dhcp6Client client;
  184. // Configure client to request IA_NA and IA_PD.
  185. client.useNA(na_iaid_);
  186. client.usePD(pd_iaid_);
  187. // Configure the server with NA pools only.
  188. ASSERT_NO_THROW(configure(RENEW_CONFIGS[0], *client.getServer()));
  189. // Perform 4-way exchange.
  190. ASSERT_NO_THROW(client.doSARR());
  191. // Simulate aging of leases.
  192. client.fastFwdTime(1000);
  193. // Make sure that the client has acquired NA lease.
  194. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  195. ASSERT_EQ(1, leases_client_na.size());
  196. // The client should not acquire a PD lease.
  197. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  198. ASSERT_TRUE(leases_client_pd.empty());
  199. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  200. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  201. ASSERT_NO_THROW(client.doRenew());
  202. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  203. ASSERT_TRUE(leases_client_pd.empty());
  204. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  205. std::vector<Lease6> leases_client_na_renewed =
  206. client.getLeasesByType(Lease::TYPE_NA);
  207. ASSERT_EQ(1, leases_client_na_renewed.size());
  208. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  209. // Specify the hint used for IA_PD.
  210. client.useHint(0, 0, 64, "::");
  211. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  212. ASSERT_NO_THROW(client.doRenew());
  213. // Make sure that the client has acquired NA lease.
  214. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  215. ASSERT_EQ(1, leases_client_na_renewed.size());
  216. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  217. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  218. ASSERT_TRUE(leases_client_pd.empty());
  219. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  220. // Reconfigure the server to use both NA and PD pools.
  221. configure(RENEW_CONFIGS[2], *client.getServer());
  222. // Specify the hint used for IA_PD.
  223. client.useHint(0, 0, 64, "::");
  224. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  225. ASSERT_NO_THROW(client.doRenew());
  226. // Make sure that the client has acquired NA lease.
  227. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  228. ASSERT_EQ(1, leases_client_na_renewed.size());
  229. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  230. // The lease should have been renewed.
  231. EXPECT_GE(leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_, 1000);
  232. // The client should now also acquire a PD lease.
  233. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  234. ASSERT_EQ(1, leases_client_pd.size());
  235. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  236. }
  237. // This test verifies that the client can request the prefix delegation
  238. // while it is renewing an address lease.
  239. TEST_F(RenewTest, requestAddressInRenew) {
  240. Dhcp6Client client;
  241. // Configure client to request IA_NA and IA_PD.
  242. client.useNA(na_iaid_);
  243. client.usePD(pd_iaid_);
  244. // Configure the server with PD pools only.
  245. ASSERT_NO_THROW(configure(RENEW_CONFIGS[1], *client.getServer()));
  246. // Perform 4-way exchange.
  247. ASSERT_NO_THROW(client.doSARR());
  248. // Simulate aging of leases.
  249. client.fastFwdTime(1000);
  250. // Make sure that the client has acquired PD lease.
  251. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  252. ASSERT_EQ(1, leases_client_pd.size());
  253. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  254. // The client should not acquire a NA lease.
  255. std::vector<Lease6> leases_client_na =
  256. client.getLeasesByType(Lease::TYPE_NA);
  257. ASSERT_EQ(0, leases_client_na.size());
  258. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  259. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  260. // The server should return NoAddrsAvail status code in this case.
  261. ASSERT_NO_THROW(client.doRenew());
  262. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  263. ASSERT_EQ(0, leases_client_na.size());
  264. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  265. std::vector<Lease6> leases_client_pd_renewed =
  266. client.getLeasesByType(Lease::TYPE_PD);
  267. ASSERT_EQ(1, leases_client_pd_renewed.size());
  268. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  269. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  270. // Reconfigure the server to use both NA and PD pools.
  271. configure(RENEW_CONFIGS[2], *client.getServer());
  272. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  273. ASSERT_NO_THROW(client.doRenew());
  274. // Make sure that the client has renewed PD lease.
  275. leases_client_pd_renewed = client.getLeasesByType(Lease::TYPE_PD);
  276. ASSERT_EQ(1, leases_client_pd_renewed.size());
  277. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  278. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  279. // The client should now also acquire a NA lease.
  280. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  281. ASSERT_EQ(1, leases_client_na.size());
  282. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  283. }
  284. // This test verifies that the client can request address assignment
  285. // while it is renewing an address lease, with a hint.
  286. TEST_F(RenewTest, requestAddressInRenewHint) {
  287. Dhcp6Client client;
  288. // Configure client to request IA_NA and IA_PD.
  289. client.useNA(na_iaid_);
  290. client.usePD(pd_iaid_);
  291. // Configure the server with PD pools only.
  292. ASSERT_NO_THROW(configure(RENEW_CONFIGS[1], *client.getServer()));
  293. // Perform 4-way exchange.
  294. ASSERT_NO_THROW(client.doSARR());
  295. // Simulate aging of leases.
  296. client.fastFwdTime(1000);
  297. // Make sure that the client has acquired PD lease.
  298. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  299. ASSERT_EQ(1, leases_client_pd.size());
  300. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  301. // The client should not acquire a NA lease.
  302. std::vector<Lease6> leases_client_na =
  303. client.getLeasesByType(Lease::TYPE_NA);
  304. ASSERT_EQ(0, leases_client_na.size());
  305. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  306. client.useHint(0, 0, "2001:db8:1::100");
  307. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  308. // The server should return NoAddrsAvail status code in this case.
  309. ASSERT_NO_THROW(client.doRenew());
  310. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  311. // The server should return the hint with the zero lifetimes.
  312. ASSERT_EQ(1, leases_client_na.size());
  313. EXPECT_EQ(0, leases_client_na[0].preferred_lft_);
  314. EXPECT_EQ(0, leases_client_na[0].valid_lft_);
  315. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  316. std::vector<Lease6> leases_client_pd_renewed =
  317. client.getLeasesByType(Lease::TYPE_PD);
  318. ASSERT_EQ(1, leases_client_pd_renewed.size());
  319. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  320. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  321. // Reconfigure the server to use both NA and PD pools.
  322. configure(RENEW_CONFIGS[2], *client.getServer());
  323. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  324. ASSERT_NO_THROW(client.doRenew());
  325. // Make sure that the client has renewed PD lease.
  326. leases_client_pd_renewed = client.getLeasesByType(Lease::TYPE_PD);
  327. ASSERT_EQ(1, leases_client_pd_renewed.size());
  328. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  329. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  330. // The client should now also acquire a NA lease.
  331. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  332. ASSERT_EQ(1, leases_client_na.size());
  333. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  334. }
  335. // This test verifies that the client can request the DOCSIS sub-options.
  336. TEST_F(RenewTest, requestDocsisORORenew) {
  337. Dhcp6Client client;
  338. // Configure client to request IA_NA.
  339. client.useNA(na_iaid_);
  340. // Configure the DOCSIS vendor ORO for 32, 33, 34, 37 and 38.
  341. client.requestDocsisOption(DOCSIS3_V6_TFTP_SERVERS);
  342. client.requestDocsisOption(DOCSIS3_V6_CONFIG_FILE);
  343. client.requestDocsisOption(DOCSIS3_V6_SYSLOG_SERVERS);
  344. client.requestDocsisOption(DOCSIS3_V6_TIME_SERVERS);
  345. client.requestDocsisOption(DOCSIS3_V6_TIME_OFFSET);
  346. // Don't add it for now.
  347. client.useDocsisORO(false);
  348. // Configure the server with NA pools and DOCSIS config file.
  349. ASSERT_NO_THROW(configure(RENEW_CONFIGS[3], *client.getServer()));
  350. // Perform 4-way exchange.
  351. ASSERT_NO_THROW(client.doSARR());
  352. // Simulate aging of leases.
  353. client.fastFwdTime(1000);
  354. // Make sure that the client has acquired NA lease.
  355. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  356. ASSERT_EQ(1, leases_client_na.size());
  357. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  358. // Send Renew message to the server.
  359. ASSERT_NO_THROW(client.doRenew());
  360. std::vector<Lease6> leases_client_na_renewed =
  361. client.getLeasesByType(Lease::TYPE_NA);
  362. ASSERT_EQ(1, leases_client_na_renewed.size());
  363. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  364. // No vendor option was included in the renew so there should be none
  365. // in the received configuration.
  366. OptionPtr opt = client.config_.findOption(D6O_VENDOR_OPTS);
  367. ASSERT_FALSE(opt);
  368. // Add a DOCSIS ORO.
  369. client.useDocsisORO(true);
  370. // Send Renew message to the server.
  371. ASSERT_NO_THROW(client.doRenew());
  372. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  373. ASSERT_EQ(1, leases_client_na_renewed.size());
  374. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  375. // Verify whether there is a vendor option.
  376. opt = client.config_.findOption(D6O_VENDOR_OPTS);
  377. ASSERT_TRUE(opt);
  378. // The vendor option must be a OptionVentor object.
  379. boost::shared_ptr<OptionVendor> vendor =
  380. boost::dynamic_pointer_cast<OptionVendor>(opt);
  381. ASSERT_TRUE(vendor);
  382. // The vendor-id should be DOCSIS.
  383. EXPECT_EQ(VENDOR_ID_CABLE_LABS, vendor->getVendorId());
  384. // There must be a config file sub-option.
  385. opt = vendor->getOption(DOCSIS3_V6_CONFIG_FILE);
  386. // With the expected content.
  387. OptionStringPtr config_file =
  388. boost::dynamic_pointer_cast<OptionString>(opt);
  389. ASSERT_TRUE(opt);
  390. EXPECT_EQ("normal_erouter_v6.cm", config_file->getValue());
  391. }
  392. } // end of anonymous namespace