renew_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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-data\": [ {"
  103. " \"name\": \"config-file\","
  104. " \"space\": \"vendor-4491\","
  105. " \"data\": \"normal_erouter_v6.cm\""
  106. "}],"
  107. "\"subnet6\": [ { "
  108. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  109. " \"subnet\": \"2001:db8:1::/48\", "
  110. " \"interface-id\": \"\","
  111. " \"interface\": \"eth0\""
  112. " } ],"
  113. "\"valid-lifetime\": 4000 }"
  114. };
  115. /// @brief Test fixture class for testing Renew.
  116. class RenewTest : public Dhcpv6MessageTest {
  117. public:
  118. /// @brief Constructor.
  119. ///
  120. /// Sets up fake interfaces.
  121. RenewTest()
  122. : Dhcpv6MessageTest(), na_iaid_(1234), pd_iaid_(5678) {
  123. }
  124. /// @brief IAID used for IA_NA.
  125. uint32_t na_iaid_;
  126. /// @brief IAID used for IA_PD.
  127. uint32_t pd_iaid_;
  128. };
  129. // This test verifies that the client can request the prefix delegation
  130. // while it is renewing an address lease.
  131. TEST_F(RenewTest, requestPrefixInRenew) {
  132. Dhcp6Client client;
  133. // Configure client to request IA_NA and IA_PD.
  134. client.useNA(na_iaid_);
  135. client.usePD(pd_iaid_);
  136. // Configure the server with NA pools only.
  137. ASSERT_NO_THROW(configure(RENEW_CONFIGS[0], *client.getServer()));
  138. // Perform 4-way exchange.
  139. ASSERT_NO_THROW(client.doSARR());
  140. // Simulate aging of leases.
  141. client.fastFwdTime(1000);
  142. // Make sure that the client has acquired NA lease.
  143. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  144. ASSERT_EQ(1, leases_client_na.size());
  145. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  146. // The client should not acquire a PD lease.
  147. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  148. ASSERT_TRUE(leases_client_pd.empty());
  149. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  150. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  151. ASSERT_NO_THROW(client.doRenew());
  152. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  153. ASSERT_TRUE(leases_client_pd.empty());
  154. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  155. std::vector<Lease6> leases_client_na_renewed =
  156. client.getLeasesByType(Lease::TYPE_NA);
  157. ASSERT_EQ(1, leases_client_na_renewed.size());
  158. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  159. // Reconfigure the server to use both NA and PD pools.
  160. configure(RENEW_CONFIGS[2], *client.getServer());
  161. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  162. ASSERT_NO_THROW(client.doRenew());
  163. // Make sure that the client has acquired NA lease.
  164. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  165. ASSERT_EQ(1, leases_client_na_renewed.size());
  166. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  167. // The lease should have been renewed.
  168. EXPECT_EQ(1000, leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_);
  169. // The client should now also acquire a PD lease.
  170. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  171. ASSERT_EQ(1, leases_client_pd.size());
  172. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  173. }
  174. // This test verifies that the client can request a prefix delegation
  175. // with a hint, while it is renewing an address lease.
  176. TEST_F(RenewTest, requestPrefixInRenewUseHint) {
  177. Dhcp6Client client;
  178. // Configure client to request IA_NA and IA_PD.
  179. client.useNA(na_iaid_);
  180. client.usePD(pd_iaid_);
  181. // Configure the server with NA pools only.
  182. ASSERT_NO_THROW(configure(RENEW_CONFIGS[0], *client.getServer()));
  183. // Perform 4-way exchange.
  184. ASSERT_NO_THROW(client.doSARR());
  185. // Simulate aging of leases.
  186. client.fastFwdTime(1000);
  187. // Make sure that the client has acquired NA lease.
  188. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  189. ASSERT_EQ(1, leases_client_na.size());
  190. // The client should not acquire a PD lease.
  191. std::vector<Lease6> 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. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  195. ASSERT_NO_THROW(client.doRenew());
  196. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  197. ASSERT_TRUE(leases_client_pd.empty());
  198. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  199. std::vector<Lease6> leases_client_na_renewed =
  200. client.getLeasesByType(Lease::TYPE_NA);
  201. ASSERT_EQ(1, leases_client_na_renewed.size());
  202. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  203. // Specify the hint used for IA_PD.
  204. client.useHint(0, 0, 64, "::");
  205. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  206. ASSERT_NO_THROW(client.doRenew());
  207. // Make sure that the client has acquired NA lease.
  208. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  209. ASSERT_EQ(1, leases_client_na_renewed.size());
  210. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  211. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  212. ASSERT_TRUE(leases_client_pd.empty());
  213. ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(pd_iaid_));
  214. // Reconfigure the server to use both NA and PD pools.
  215. configure(RENEW_CONFIGS[2], *client.getServer());
  216. // Specify the hint used for IA_PD.
  217. client.useHint(0, 0, 64, "::");
  218. // Send Renew message to the server, including IA_NA and requesting IA_PD.
  219. ASSERT_NO_THROW(client.doRenew());
  220. // Make sure that the client has acquired NA lease.
  221. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  222. ASSERT_EQ(1, leases_client_na_renewed.size());
  223. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  224. // The lease should have been renewed.
  225. EXPECT_GE(leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_, 1000);
  226. // The client should now also acquire a PD lease.
  227. leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  228. ASSERT_EQ(1, leases_client_pd.size());
  229. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  230. }
  231. // This test verifies that the client can request the prefix delegation
  232. // while it is renewing an address lease.
  233. TEST_F(RenewTest, requestAddressInRenew) {
  234. Dhcp6Client client;
  235. // Configure client to request IA_NA and IA_PD.
  236. client.useNA(na_iaid_);
  237. client.usePD(pd_iaid_);
  238. // Configure the server with PD pools only.
  239. ASSERT_NO_THROW(configure(RENEW_CONFIGS[1], *client.getServer()));
  240. // Perform 4-way exchange.
  241. ASSERT_NO_THROW(client.doSARR());
  242. // Simulate aging of leases.
  243. client.fastFwdTime(1000);
  244. // Make sure that the client has acquired PD lease.
  245. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  246. ASSERT_EQ(1, leases_client_pd.size());
  247. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  248. // The client should not acquire a NA lease.
  249. std::vector<Lease6> leases_client_na =
  250. client.getLeasesByType(Lease::TYPE_NA);
  251. ASSERT_EQ(0, leases_client_na.size());
  252. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  253. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  254. // The server should return NoAddrsAvail status code in this case.
  255. ASSERT_NO_THROW(client.doRenew());
  256. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  257. ASSERT_EQ(0, leases_client_na.size());
  258. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  259. std::vector<Lease6> leases_client_pd_renewed =
  260. client.getLeasesByType(Lease::TYPE_PD);
  261. ASSERT_EQ(1, leases_client_pd_renewed.size());
  262. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  263. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  264. // Reconfigure the server to use both NA and PD pools.
  265. configure(RENEW_CONFIGS[2], *client.getServer());
  266. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  267. ASSERT_NO_THROW(client.doRenew());
  268. // Make sure that the client has renewed PD lease.
  269. leases_client_pd_renewed = client.getLeasesByType(Lease::TYPE_PD);
  270. ASSERT_EQ(1, leases_client_pd_renewed.size());
  271. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  272. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  273. // The client should now also acquire a NA lease.
  274. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  275. ASSERT_EQ(1, leases_client_na.size());
  276. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  277. }
  278. // This test verifies that the client can request address assignment
  279. // while it is renewing an address lease, with a hint.
  280. TEST_F(RenewTest, requestAddressInRenewHint) {
  281. Dhcp6Client client;
  282. // Configure client to request IA_NA and IA_PD.
  283. client.useNA(na_iaid_);
  284. client.usePD(pd_iaid_);
  285. // Configure the server with PD pools only.
  286. ASSERT_NO_THROW(configure(RENEW_CONFIGS[1], *client.getServer()));
  287. // Perform 4-way exchange.
  288. ASSERT_NO_THROW(client.doSARR());
  289. // Simulate aging of leases.
  290. client.fastFwdTime(1000);
  291. // Make sure that the client has acquired PD lease.
  292. std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
  293. ASSERT_EQ(1, leases_client_pd.size());
  294. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  295. // The client should not acquire a NA lease.
  296. std::vector<Lease6> leases_client_na =
  297. client.getLeasesByType(Lease::TYPE_NA);
  298. ASSERT_EQ(0, leases_client_na.size());
  299. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  300. client.useHint(0, 0, "2001:db8:1::100");
  301. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  302. // The server should return NoAddrsAvail status code in this case.
  303. ASSERT_NO_THROW(client.doRenew());
  304. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  305. // The server should return the hint with the zero lifetimes.
  306. ASSERT_EQ(1, leases_client_na.size());
  307. EXPECT_EQ(0, leases_client_na[0].preferred_lft_);
  308. EXPECT_EQ(0, leases_client_na[0].valid_lft_);
  309. ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(na_iaid_));
  310. std::vector<Lease6> leases_client_pd_renewed =
  311. client.getLeasesByType(Lease::TYPE_PD);
  312. ASSERT_EQ(1, leases_client_pd_renewed.size());
  313. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  314. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  315. // Reconfigure the server to use both NA and PD pools.
  316. configure(RENEW_CONFIGS[2], *client.getServer());
  317. // Send Renew message to the server, including IA_PD and requesting IA_NA.
  318. ASSERT_NO_THROW(client.doRenew());
  319. // Make sure that the client has renewed PD lease.
  320. leases_client_pd_renewed = client.getLeasesByType(Lease::TYPE_PD);
  321. ASSERT_EQ(1, leases_client_pd_renewed.size());
  322. EXPECT_EQ(STATUS_Success, client.getStatusCode(pd_iaid_));
  323. EXPECT_GE(leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_, 1000);
  324. // The client should now also acquire a NA lease.
  325. leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  326. ASSERT_EQ(1, leases_client_na.size());
  327. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  328. }
  329. // This test verifies that the client can request the DOCSIS sub-options.
  330. TEST_F(RenewTest, docsisORO) {
  331. Dhcp6Client client;
  332. // Configure client to request IA_NA.
  333. client.useNA(na_iaid_);
  334. // Configure the DOCSIS vendor ORO for 32, 33, 34, 37 and 38.
  335. client.requestDocsisOption(DOCSIS3_V6_TFTP_SERVERS);
  336. client.requestDocsisOption(DOCSIS3_V6_CONFIG_FILE);
  337. client.requestDocsisOption(DOCSIS3_V6_SYSLOG_SERVERS);
  338. client.requestDocsisOption(DOCSIS3_V6_TIME_SERVERS);
  339. client.requestDocsisOption(DOCSIS3_V6_TIME_OFFSET);
  340. // Don't add it for now.
  341. client.useDocsisORO(false);
  342. // Configure the server with NA pools and DOCSIS config file.
  343. ASSERT_NO_THROW(configure(RENEW_CONFIGS[3], *client.getServer()));
  344. // Perform 4-way exchange.
  345. ASSERT_NO_THROW(client.doSARR());
  346. // Simulate aging of leases.
  347. client.fastFwdTime(1000);
  348. // Make sure that the client has acquired NA lease.
  349. std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
  350. ASSERT_EQ(1, leases_client_na.size());
  351. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  352. // Send Renew message to the server.
  353. ASSERT_NO_THROW(client.doRenew());
  354. std::vector<Lease6> leases_client_na_renewed =
  355. client.getLeasesByType(Lease::TYPE_NA);
  356. ASSERT_EQ(1, leases_client_na_renewed.size());
  357. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  358. // No vendor option was included in the renew so there should be none
  359. // in the received configuration.
  360. OptionPtr opt = client.config_.findOption(D6O_VENDOR_OPTS);
  361. ASSERT_FALSE(opt);
  362. // Add a DOCSIS ORO.
  363. client.useDocsisORO(true);
  364. // Send Renew message to the server.
  365. ASSERT_NO_THROW(client.doRenew());
  366. leases_client_na_renewed = client.getLeasesByType(Lease::TYPE_NA);
  367. ASSERT_EQ(1, leases_client_na_renewed.size());
  368. EXPECT_EQ(STATUS_Success, client.getStatusCode(na_iaid_));
  369. // Verify whether there is a vendor option.
  370. opt = client.config_.findOption(D6O_VENDOR_OPTS);
  371. ASSERT_TRUE(opt);
  372. // The vendor option must be a OptionVendor object.
  373. boost::shared_ptr<OptionVendor> vendor =
  374. boost::dynamic_pointer_cast<OptionVendor>(opt);
  375. ASSERT_TRUE(vendor);
  376. // The vendor-id should be DOCSIS.
  377. EXPECT_EQ(VENDOR_ID_CABLE_LABS, vendor->getVendorId());
  378. // There must be a config file sub-option.
  379. opt = vendor->getOption(DOCSIS3_V6_CONFIG_FILE);
  380. // With the expected content.
  381. OptionStringPtr config_file =
  382. boost::dynamic_pointer_cast<OptionString>(opt);
  383. ASSERT_TRUE(opt);
  384. EXPECT_EQ("normal_erouter_v6.cm", config_file->getValue());
  385. }
  386. } // end of anonymous namespace