rebind_unittest.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // Copyright (C) 2014-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. using namespace isc::test;
  26. namespace {
  27. /// @brief Set of JSON configurations used throughout the Rebind tests.
  28. ///
  29. /// - Configuration 0:
  30. /// - only addresses (no prefixes)
  31. /// - 2 subnets with 2001:db8:1::/64 and 2001:db8:2::64
  32. /// - 1 subnet for eth0 and 1 subnet for eth1
  33. ///
  34. /// - Configuration 1:
  35. /// - similar to Configuration 0 but different subnets
  36. /// - pools configured: 2001:db8:3::/64 and 2001:db8:4::/64
  37. ///
  38. /// - Configuration 2:
  39. /// - similar to Configuration 0 and Configuration 1
  40. /// - pools configured: 3000:1::/64 and 3000:2::/64
  41. /// - this specific configuration is used by tests using relays
  42. ///
  43. /// - Configuration 3:
  44. /// - similar to Configuration 2 but with different subnets
  45. /// - pools configured: 3000:3::/64 and 3000:4::/64
  46. /// - this specific configuration is used by tests using relays
  47. ///
  48. /// - Configuration 4:
  49. /// - only prefixes (no addresses)
  50. /// - 2 subnets: 2001:db8:1::/40 and 2001:db8:2::/40
  51. /// - 2 prefix pools: 3000::/72 and 2001:db8:2::/72
  52. /// - 1 subnet for eth0 and 1 subnet for eth1
  53. /// - this specific configuration is used by tests which don't use relays
  54. ///
  55. /// - Configuration 5:
  56. /// - similar to Configuration 5 but with different subnets
  57. /// - 2 subnets: 2001:db8:3::/40 and 2001:db8:4::/40
  58. /// - 2 prefix pools: 2001:db8:3::/72 and 2001:db8:4::/72
  59. /// - delegated length /80
  60. /// - this specific configuration is used by tests which don't use relays
  61. const char* REBIND_CONFIGS[] = {
  62. // Configuration 0
  63. "{ \"interfaces\": [ \"*\" ],"
  64. "\"preferred-lifetime\": 3000,"
  65. "\"rebind-timer\": 2000, "
  66. "\"renew-timer\": 1000, "
  67. "\"subnet6\": [ { "
  68. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  69. " \"subnet\": \"2001:db8:1::/48\", "
  70. " \"interface-id\": \"\","
  71. " \"interface\": \"eth0\""
  72. " },"
  73. " {"
  74. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  75. " \"subnet\": \"2001:db8:2::/48\", "
  76. " \"interface-id\": \"\","
  77. " \"interface\": \"eth1\""
  78. " } ],"
  79. "\"valid-lifetime\": 4000 }",
  80. // Configuration 1
  81. "{ \"interfaces\": [ \"*\" ],"
  82. "\"preferred-lifetime\": 3000,"
  83. "\"rebind-timer\": 2000, "
  84. "\"renew-timer\": 1000, "
  85. "\"subnet6\": [ { "
  86. " \"pools\": [ { \"pool\": \"2001:db8:3::/64\" } ],"
  87. " \"subnet\": \"2001:db8:3::/48\", "
  88. " \"interface-id\": \"\","
  89. " \"interface\": \"eth1\""
  90. " },"
  91. " {"
  92. " \"pools\": [ { \"pool\": \"2001:db8:4::/64\" } ],"
  93. " \"subnet\": \"2001:db8:4::/48\", "
  94. " \"interface-id\": \"\","
  95. " \"interface\": \"eth0\""
  96. " } ],"
  97. "\"valid-lifetime\": 4000 }",
  98. // Configuration 2
  99. "{ \"interfaces\": [ \"*\" ],"
  100. "\"preferred-lifetime\": 3000,"
  101. "\"rebind-timer\": 2000, "
  102. "\"renew-timer\": 1000, "
  103. "\"subnet6\": [ { "
  104. " \"pools\": [ { \"pool\": \"3000:1::/64\" } ],"
  105. " \"subnet\": \"3000:1::/48\", "
  106. " \"interface-id\": \"\","
  107. " \"interface\": \"eth0\""
  108. " },"
  109. " {"
  110. " \"pools\": [ { \"pool\": \"3000:2::/64\" } ],"
  111. " \"subnet\": \"3000:2::/48\", "
  112. " \"interface-id\": \"\","
  113. " \"interface\": \"eth1\""
  114. " } ],"
  115. "\"valid-lifetime\": 4000 }",
  116. // Configuration 3
  117. "{ \"interfaces\": [ \"*\" ],"
  118. "\"preferred-lifetime\": 3000,"
  119. "\"rebind-timer\": 2000, "
  120. "\"renew-timer\": 1000, "
  121. "\"subnet6\": [ { "
  122. " \"pools\": [ { \"pool\": \"3000:3::/64\" } ],"
  123. " \"subnet\": \"3000:3::/48\", "
  124. " \"interface-id\": \"\","
  125. " \"interface\": \"eth1\""
  126. " },"
  127. " {"
  128. " \"pools\": [ { \"pool\": \"3000:4::/64\" } ],"
  129. " \"subnet\": \"3000:4::/48\", "
  130. " \"interface-id\": \"\","
  131. " \"interface\": \"eth0\""
  132. " } ],"
  133. "\"valid-lifetime\": 4000 }",
  134. // Configuration 4
  135. "{ \"interfaces\": [ \"*\" ],"
  136. "\"preferred-lifetime\": 3000,"
  137. "\"rebind-timer\": 2000, "
  138. "\"renew-timer\": 1000, "
  139. "\"subnet6\": [ { "
  140. " \"pd-pools\": ["
  141. " { \"prefix\": \"3000::\", "
  142. " \"prefix-len\": 72, "
  143. " \"delegated-len\": 80"
  144. " } ],"
  145. " \"subnet\": \"2001:db8:1::/40\", "
  146. " \"interface-id\": \"\","
  147. " \"interface\": \"eth0\""
  148. " },"
  149. " {"
  150. " \"pd-pools\": ["
  151. " { \"prefix\": \"2001:db8:2::\", "
  152. " \"prefix-len\": 72, "
  153. " \"delegated-len\": 80"
  154. " } ],"
  155. " \"subnet\": \"2001:db8:2::/40\", "
  156. " \"interface-id\": \"\","
  157. " \"interface\": \"eth1\""
  158. " } ],"
  159. "\"valid-lifetime\": 4000 }",
  160. // Configuration 5
  161. "{ \"interfaces\": [ \"*\" ],"
  162. "\"preferred-lifetime\": 3000,"
  163. "\"rebind-timer\": 2000, "
  164. "\"renew-timer\": 1000, "
  165. "\"subnet6\": [ { "
  166. " \"pd-pools\": ["
  167. " { \"prefix\": \"2001:db8:3:01::\", "
  168. " \"prefix-len\": 72, "
  169. " \"delegated-len\": 80"
  170. " } ],"
  171. " \"subnet\": \"2001:db8:3::/40\", "
  172. " \"interface-id\": \"\","
  173. " \"interface\": \"eth1\""
  174. " },"
  175. " {"
  176. " \"pd-pools\": ["
  177. " { \"prefix\": \"2001:db8:4:01::\", "
  178. " \"prefix-len\": 72, "
  179. " \"delegated-len\": 80"
  180. " } ],"
  181. " \"subnet\": \"2001:db8:4::/40\", "
  182. " \"interface-id\": \"\","
  183. " \"interface\": \"eth0\""
  184. " } ],"
  185. "\"valid-lifetime\": 4000 }",
  186. };
  187. /// @brief Test fixture class for testing Rebind.
  188. class RebindTest : public Dhcpv6MessageTest {
  189. public:
  190. /// @brief Constructor.
  191. ///
  192. /// Sets up fake interfaces.
  193. RebindTest()
  194. : Dhcpv6MessageTest() {
  195. }
  196. };
  197. // Test that directly connected client's Rebind message is processed and Reply
  198. // message is sent back.
  199. TEST_F(RebindTest, directClient) {
  200. Dhcp6Client client;
  201. // Configure client to request IA_NA.
  202. client.useNA();
  203. // Make 4-way exchange to get the lease.
  204. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[0], 2, client));
  205. // Keep the client's lease for future reference.
  206. Lease6 lease_client = client.getLease(0);
  207. // Send Rebind message to the server.
  208. ASSERT_NO_THROW(client.doRebind());
  209. // The client should still have one lease which belong to one of the
  210. // subnets.
  211. ASSERT_EQ(1, client.getLeaseNum());
  212. Lease6 lease_client2 = client.getLease(0);
  213. ASSERT_TRUE(CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->
  214. selectSubnet(lease_client2.addr_, ClientClasses()));
  215. // The client's lease should have been extended. The client will
  216. // update the cltt to current time when the lease gets extended.
  217. ASSERT_GE(lease_client2.cltt_ - lease_client.cltt_, 1000);
  218. // Make sure, that the client's lease matches the lease held by the
  219. // server.
  220. Lease6Ptr lease_server2 = checkLease(lease_client2);
  221. EXPECT_TRUE(lease_server2);
  222. }
  223. // Test that server doesn't extend the lease when the configuration has changed
  224. // such that the existing subnet is replaced with a different subnet.
  225. TEST_F(RebindTest, directClientChangingSubnet) {
  226. Dhcp6Client client;
  227. // Configure client to request IA_NA.
  228. client.useNA();
  229. // Make 4-way exchange to get the lease.
  230. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[0], 2, client));
  231. // Keep the client's lease for future reference.
  232. Lease6 lease_client = client.getLease(0);
  233. // Reconfigure the server so as the new subnet is served on the
  234. // client's interface. Note that there will also be a new subnet
  235. // id assigned to the subnet on this interface.
  236. configure(REBIND_CONFIGS[1], *client.getServer());
  237. // Try to rebind, using the address that the client had acquired using
  238. // previous server configuration.
  239. ASSERT_NO_THROW(client.doRebind());
  240. // We are expecting that the server didn't extend the lease because
  241. // the address that client is using doesn't match the new subnet.
  242. // But, the client still has an old lease.
  243. ASSERT_EQ(1, client.getLeaseNum());
  244. Lease6 lease_client2 = client.getLease(0);
  245. // The current lease should be exactly the same as old lease,
  246. // because server shouldn't have extended.
  247. EXPECT_TRUE(lease_client == lease_client2);
  248. // Make sure, that the lease that client has, is matching the lease
  249. // in the lease database.
  250. Lease6Ptr lease_server2 = checkLease(lease_client2);
  251. EXPECT_TRUE(lease_server2);
  252. // Client should have received NoBinding status code.
  253. EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
  254. }
  255. // Check that the server doesn't extend the lease for the client when the
  256. // client sends IAID which doesn't belong to the lease that client has.
  257. TEST_F(RebindTest, directClientChangingIAID) {
  258. Dhcp6Client client;
  259. // Configure client to request IA_NA.
  260. client.useNA();
  261. // Make 4-way exchange to get the lease.
  262. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[0], 2, client));
  263. // Keep the client's lease for future reference.
  264. Lease6 lease_client = client.getLease(0);
  265. // Modify the IAID of the lease record that client stores. By adding
  266. // one to IAID we guarantee that the IAID will change.
  267. ++client.config_.leases_[0].lease_.iaid_;
  268. // Try to Rebind. Note that client will use a different IAID (which
  269. // is not matching IAID that server retains for the client). Server
  270. // should not find the lease that client is trying to extend and
  271. // should return NoBinding.
  272. ASSERT_NO_THROW(client.doRebind());
  273. // The lease obtained in 4-way exchange should not change after the Rebind
  274. // attempt.
  275. Lease6Ptr lease_server2 = checkLease(lease_client);
  276. EXPECT_TRUE(lease_server2);
  277. // The Status code returned to the client, should be NoBinding.
  278. EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
  279. }
  280. // Check that server sends NoBinding when the lease has been lost from
  281. // the database and client is trying to Rebind it.
  282. TEST_F(RebindTest, directClientLostLease) {
  283. Dhcp6Client client;
  284. // Configure client to request IA_NA.
  285. client.useNA();
  286. // Make 4-way exchange to get the lease.
  287. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[0], 2, client));
  288. // Keep the client's lease for future reference.
  289. Lease6 lease_client = client.getLease(0);
  290. // The lease has been acquired. Now, let's explicitly remove it from the
  291. // lease database.
  292. LeaseMgrFactory::instance().deleteLease(lease_client.addr_);
  293. // An attempt to Rebind should fail. The lease should not be found by
  294. // the server and the server should return NoBinding status code.
  295. ASSERT_NO_THROW(client.doRebind());
  296. ASSERT_EQ(1, client.getLeaseNum());
  297. EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
  298. }
  299. /// @todo Extend tests for direct client changing address.
  300. // Check that the client can Rebind existing lease through a relay.
  301. TEST_F(RebindTest, relayedClient) {
  302. Dhcp6Client client;
  303. // Configure client to request IA_NA.
  304. client.useNA();
  305. // Configure DHCPv6 client to simulate sending the message through a relay
  306. // agent. The default link-addr is 3001:1::1. This address should be used
  307. // by the server to pick the suitable subnet.
  308. client.useRelay();
  309. // Make 4-way exchange to get the lease. Pick the configuration #2 as it
  310. // specifies the subnet for the relay agent's link address.
  311. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[2], 2, client));
  312. // Keep the client's lease for future reference.
  313. Lease6 lease_client = client.getLease(0);
  314. // Send Rebind message to the server.
  315. ASSERT_NO_THROW(client.doRebind());
  316. // The client should still have one lease which belongs to one of the
  317. // subnets.
  318. ASSERT_EQ(1, client.getLeaseNum());
  319. Lease6 lease_client2 = client.getLease(0);
  320. ASSERT_TRUE(CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->
  321. selectSubnet(lease_client2.addr_, ClientClasses()));
  322. // The client's lease should have been extended. The client will
  323. // update the cltt to current time when the lease gets extended.
  324. ASSERT_GE(lease_client2.cltt_ - lease_client.cltt_, 1000);
  325. // Make sure, that the client's lease matches the lease held by the
  326. // server.
  327. Lease6Ptr lease_server2 = checkLease(lease_client2);
  328. EXPECT_TRUE(lease_server2);
  329. }
  330. // Check that the lease is not extended for the relayed client when the
  331. // configuration has changed such that the subnet that client is using
  332. // doesn't exist anymore.
  333. TEST_F(RebindTest, relayedClientChangingSubnet) {
  334. Dhcp6Client client;
  335. // Configure client to request IA_NA.
  336. client.useNA();
  337. // Configure DHCPv6 client to simulate sending the message through a relay
  338. // agent. The default link-addr is 3001:1::1. This address should be used
  339. // by the server to pick the suitable subnet.
  340. client.useRelay();
  341. // Make 4-way exchange to get the lease.
  342. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[2], 2, client));
  343. // Keep the client's lease for future reference.
  344. Lease6 lease_client = client.getLease(0);
  345. // Reconfigure the server so as the new subnet is served on the
  346. // client's interface. Note that there will also be a new subnet
  347. // id assigned to the subnet on this interface.
  348. configure(REBIND_CONFIGS[3], *client.getServer());
  349. // Update relay link address to match the new subnet.
  350. client.relay_link_addr_ = IOAddress("3001:4::1");
  351. // Try to rebind, using the address that the client had acquired using
  352. // previous server configuration.
  353. ASSERT_NO_THROW(client.doRebind());
  354. // We are expecting that the server didn't extend the lease because
  355. // the address that client is using doesn't match the new subnet.
  356. // But, the client still has an old lease.
  357. ASSERT_EQ(1, client.getLeaseNum());
  358. Lease6 lease_client2 = client.getLease(0);
  359. // The current lease should be exactly the same as old lease,
  360. // because server shouldn't have extended.
  361. EXPECT_TRUE(lease_client == lease_client2);
  362. // Make sure, that the lease that client has, is matching the lease
  363. // in the lease database.
  364. Lease6Ptr lease_server2 = checkLease(lease_client2);
  365. EXPECT_TRUE(lease_server2);
  366. // Client should have received NoBinding status code.
  367. EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
  368. }
  369. // Check that the lease is not extended for the relayed client when the IAID in
  370. // the Rebind message doesn't match the one recorded for the client.
  371. TEST_F(RebindTest, relayedClientChangingIAID) {
  372. Dhcp6Client client;
  373. // Configure client to request IA_NA.
  374. client.useNA();
  375. // Configure DHCPv6 client to simulate sending the message through a relay
  376. // agent. The default link-addr is 3001:1::1. This address should be used
  377. // by the server to pick the suitable subnet.
  378. client.useRelay();
  379. // Make 4-way exchange to get the lease.
  380. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[2], 2, client));
  381. // Keep the client's lease for future reference.
  382. Lease6 lease_client = client.getLease(0);
  383. // Modify the IAID of the lease record that client stores. By adding
  384. // one to IAID we guarantee that the IAID will change.
  385. ++client.config_.leases_[0].lease_.iaid_;
  386. // Try to Rebind. Note that client will use a different IAID (which
  387. // is not matching IAID that server retains for the client). Server
  388. // should not find the lease that client is trying to extend and
  389. // should return NoBinding.
  390. ASSERT_NO_THROW(client.doRebind());
  391. // The lease obtained in 4-way exchange should not change after the Rebind
  392. // attempt.
  393. Lease6Ptr lease_server2 = checkLease(lease_client);
  394. EXPECT_TRUE(lease_server2);
  395. // The Status code returned to the client, should be NoBinding.
  396. EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
  397. }
  398. // Check that the relayed client receives NoBinding when the lease that he
  399. // is Rebinding has been lost from the database.
  400. TEST_F(RebindTest, relayedClientLostLease) {
  401. Dhcp6Client client;
  402. // Configure client to request IA_NA.
  403. client.useNA();
  404. // Configure DHCPv6 client to simulate sending the message through a relay
  405. // agent. The default link-addr is 3001:1::1. This address should be used
  406. // by the server to pick the suitable subnet.
  407. client.useRelay();
  408. // Make 4-way exchange to get the lease.
  409. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[2], 2, client));
  410. // Keep the client's lease for future reference.
  411. Lease6 lease_client = client.getLease(0);
  412. // The lease has been acquired. Now, let's explicitly remove it from the
  413. // lease database.
  414. LeaseMgrFactory::instance().deleteLease(lease_client.addr_);
  415. // An attempt to Rebind should fail. The lease should not be found by
  416. // the server and the server should return NoBinding status code.
  417. ASSERT_NO_THROW(client.doRebind());
  418. ASSERT_EQ(1, client.getLeaseNum());
  419. EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
  420. }
  421. // Check that relayed client receives the IA with lifetimes of 0, when
  422. // client is tgrying to Rebind using an address it doesn't have.
  423. TEST_F(RebindTest, relayedClientChangingAddress) {
  424. Dhcp6Client client;
  425. // Configure client to request IA_NA.
  426. client.useNA();
  427. // Make 4-way exchange to get the lease.
  428. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[2], 2, client));
  429. // Keep the client's lease for future reference.
  430. Lease6 lease_client = client.getLease(0);
  431. // Modify the address of the lease record that client stores. The server
  432. // should check that the address is invalid (hasn't been allocated for
  433. // the particular IAID).
  434. client.config_.leases_[0].lease_.addr_ = IOAddress("3000::100");
  435. // Try to Rebind. The client will use correct IAID but will specify a
  436. // wrong address. The server will discover that the client has a binding
  437. // but the address will not match.
  438. ASSERT_NO_THROW(client.doRebind());
  439. // Make sure that the server has discarded client's message. In such case,
  440. // the message sent back to the client should be NULL.
  441. EXPECT_TRUE(client.getContext().response_)
  442. << "The server discarded the Rebind message, while it should have"
  443. " sent a response indicating that the client should stop using the"
  444. " lease, by setting lifetime values to 0.";
  445. // Get the client's lease.
  446. ASSERT_EQ(1, client.getLeaseNum());
  447. Lease6 lease_client2 = client.getLease(0);
  448. // The lifetimes should be set to 0, as an explicit notification to the
  449. // client to stop using invalid prefix.
  450. EXPECT_EQ(0, lease_client2.valid_lft_);
  451. EXPECT_EQ(0, lease_client2.preferred_lft_);
  452. // Check that server still has the same lease.
  453. Lease6Ptr lease_server = checkLease(lease_client);
  454. EXPECT_TRUE(lease_server);
  455. // Make sure that the lease in the data base hasn't been addected.
  456. EXPECT_NE(0, lease_server->valid_lft_);
  457. EXPECT_NE(0, lease_server->preferred_lft_);
  458. }
  459. // Check that the server extends the lease for the client having a prefix.
  460. TEST_F(RebindTest, directClientPD) {
  461. Dhcp6Client client;
  462. // Configure client to request IA_PD.
  463. client.usePD();
  464. // Make 4-way exchange to get the lease.
  465. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[4], 2, client));
  466. // Keep the client's lease for future reference.
  467. Lease6 lease_client = client.getLease(0);
  468. // Send Rebind message to the server.
  469. ASSERT_NO_THROW(client.doRebind());
  470. // The client should still have one lease which belong to one of the
  471. // subnets.
  472. ASSERT_EQ(1, client.getLeaseNum());
  473. Lease6 lease_client2 = client.getLease(0);
  474. // The client's lease should have been extended. The client will
  475. // update the cltt to current time when the lease gets extended.
  476. ASSERT_GE(lease_client2.cltt_ - lease_client.cltt_, 1000);
  477. // Make sure, that the client's lease matches the lease held by the
  478. // server.
  479. Lease6Ptr lease_server2 = checkLease(lease_client2);
  480. EXPECT_TRUE(lease_server2);
  481. }
  482. // Check that the prefix lifetime is not extended for the client in case
  483. // the configuration has been changed such, that the subnet he is using
  484. // doesn't exist anymore.
  485. TEST_F(RebindTest, directClientPDChangingSubnet) {
  486. Dhcp6Client client;
  487. // Configure client to request IA_PD.
  488. client.usePD();
  489. // Make 4-way exchange to get the lease.
  490. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[4], 2, client));
  491. // Keep the client's lease for future reference.
  492. Lease6 lease_client = client.getLease(0);
  493. // Reconfigure the server so as the new subnet is served on the
  494. // client's interface. Note that there will also be a new subnet
  495. // id assigned to the subnet on this interface.
  496. configure(REBIND_CONFIGS[5], *client.getServer());
  497. // Try to rebind, using the address that the client had acquired using
  498. // previous server configuration.
  499. ASSERT_NO_THROW(client.doRebind());
  500. // Make sure that the server has discarded client's message. In such case,
  501. // the message sent back to the client should be NULL.
  502. EXPECT_FALSE(client.getContext().response_)
  503. << "The server responded to the Rebind message, while it should have"
  504. " discarded it because there is no binding for the client.";
  505. // We are expecting that the server didn't extend the lease because
  506. // the address that client is using doesn't match the new subnet.
  507. // But, the client still has an old lease.
  508. ASSERT_EQ(1, client.getLeaseNum());
  509. Lease6 lease_client2 = client.getLease(0);
  510. // The current lease should be exactly the same as old lease,
  511. // because server shouldn't have extended.
  512. EXPECT_TRUE(lease_client == lease_client2);
  513. // Make sure, that the lease that client has, is matching the lease
  514. // in the lease database.
  515. Lease6Ptr lease_server2 = checkLease(lease_client2);
  516. EXPECT_TRUE(lease_server2);
  517. }
  518. // Check that the prefix lifetime is not extended for the client when the
  519. // IAID used in the Rebind is not matching the one recorded by the server
  520. // for the particular client.
  521. TEST_F(RebindTest, directClientPDChangingIAID) {
  522. Dhcp6Client client;
  523. // Configure client to request IA_PD.
  524. client.usePD();
  525. // Make 4-way exchange to get the lease.
  526. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[4], 2, client));
  527. // Keep the client's lease for future reference.
  528. Lease6 lease_client = client.getLease(0);
  529. // Modify the IAID of the lease record that client stores. By adding
  530. // one to IAID we guarantee that the IAID will change.
  531. ++client.config_.leases_[0].lease_.iaid_;
  532. // Try to Rebind. Note that client will use a different IAID (which
  533. // is not matching IAID that server retains for the client). This is
  534. // a condition described in RFC3633, section 12.2 as the server finds
  535. // no binding for the client. It is an indication that some other
  536. // server has probably allocated the lease for the client. Hence, our
  537. // server should discard the message.
  538. ASSERT_NO_THROW(client.doRebind());
  539. // Make sure that the server has discarded client's message. In such case,
  540. // the message sent back to the client should be NULL.
  541. EXPECT_FALSE(client.getContext().response_)
  542. << "The server responded to the Rebind message, while it should have"
  543. " discarded it because there is no binding for the client.";
  544. // Check that server still has the same lease.
  545. Lease6Ptr lease_server = checkLease(lease_client);
  546. EXPECT_TRUE(lease_server);
  547. }
  548. // Check that the prefix lifetime is not extended for the client when the
  549. // prefix used in Rebind message doesn't match the one that client has.
  550. TEST_F(RebindTest, directClientPDChangingPrefix) {
  551. Dhcp6Client client;
  552. // Configure client to request IA_PD.
  553. client.usePD();
  554. // Make 4-way exchange to get the lease.
  555. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[4], 2, client));
  556. // Keep the client's lease for future reference.
  557. Lease6 lease_client = client.getLease(0);
  558. // Modify the Prefix of the lease record that client stores. The server
  559. // should check that the prefix is invalid (hasn't been allocated for
  560. // the particular IAID).
  561. ASSERT_NE(client.config_.leases_[0].lease_.addr_,
  562. IOAddress("2001:db8:1:10::"));
  563. client.config_.leases_[0].lease_.addr_ = IOAddress("2001:db8:1:10::");
  564. // Try to Rebind. The client will use correct IAID but will specify a
  565. // wrong prefix. The server will discover that the client has a binding
  566. // but the prefix will not match. According to the RFC3633, section 12.2.
  567. // the server has to return the lease with lifetimes set to 0, when there
  568. // is a binding for the client but the prefix doesn't match.
  569. ASSERT_NO_THROW(client.doRebind());
  570. // Make sure that the server has discarded client's message. In such case,
  571. // the message sent back to the client should be NULL.
  572. EXPECT_TRUE(client.getContext().response_)
  573. << "The server discarded the Rebind message, while it should have"
  574. " sent a response indicating that the client should stop using the"
  575. " lease, by setting lifetime values to 0.";
  576. // Get the client's lease.
  577. ASSERT_EQ(1, client.getLeaseNum());
  578. Lease6 lease_client2 = client.getLease(0);
  579. // The lifetimes should be set to 0, as an explicit notification to the
  580. // client to stop using invalid prefix.
  581. EXPECT_EQ(0, lease_client2.valid_lft_);
  582. EXPECT_EQ(0, lease_client2.preferred_lft_);
  583. // Check that server still has the same lease.
  584. Lease6Ptr lease_server = checkLease(lease_client);
  585. ASSERT_TRUE(lease_server);
  586. // Make sure that the lease in the data base hasn't been addected.
  587. EXPECT_NE(0, lease_server->valid_lft_);
  588. EXPECT_NE(0, lease_server->preferred_lft_);
  589. }
  590. /// @todo Extend PD tests for relayed messages.
  591. /// @todo Extend PD tests to cover same prefix buyt different length.
  592. // This test checks that the Rebind message is discarded by the server if it
  593. // has been sent to unicast address (RFC3315, section 15).
  594. TEST_F(RebindTest, unicast) {
  595. Dhcp6Client client;
  596. // Configure client to request IA_NA.
  597. client.useNA();
  598. // Make 4-way exchange to get the lease.
  599. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[0], 2, client));
  600. // Keep the client's lease for future reference.
  601. Lease6 lease_client = client.getLease(0);
  602. // Set the unicast destination address for the Rebind message.
  603. // The Rebind should be discarded when sent to unicast address,
  604. // according to section 15 of RFC3315.
  605. client.setDestAddress(IOAddress("2001:db8:1::1"));
  606. // Send the Rebind message to a unicast address.
  607. ASSERT_NO_THROW(client.doRebind());
  608. // The client's lease should remain with no change (shouldn't be extended)
  609. // because server is supposed to drop the message sent to a unicast address.
  610. ASSERT_EQ(1, client.getLeaseNum());
  611. Lease6 lease_client2 = client.getLease(0);
  612. ASSERT_TRUE(lease_client2 == lease_client);
  613. // Check that server still has the lease.
  614. Lease6Ptr lease_server2 = checkLease(lease_client2);
  615. EXPECT_TRUE(lease_server2);
  616. // Make sure that the server has discarded client's message. In such case,
  617. // the message sent back to the client should be NULL.
  618. EXPECT_FALSE(client.getContext().response_);
  619. }
  620. // This test checks that the relayed Rebind message is processed by the server
  621. // when sent to unicast address.
  622. TEST_F(RebindTest, relayedUnicast) {
  623. Dhcp6Client client;
  624. // Configure client to request IA_NA.
  625. client.useNA();
  626. // Configure DHCPv6 client to simulate sending the message through a relay
  627. // agent. The default link-addr is 3001:1::1. This address should be used
  628. // by the server to pick the suitable subnet.
  629. client.useRelay();
  630. // Make 4-way exchange to get the lease. Pick the configuration #2 as it
  631. // specifies the subnet for the relay agent's link address.
  632. ASSERT_NO_FATAL_FAILURE(requestLease(REBIND_CONFIGS[2], 2, client));
  633. // Keep the client's lease for future reference.
  634. Lease6 lease_client = client.getLease(0);
  635. // Set the unicast destination address.
  636. client.setDestAddress(IOAddress("2001:db8:1::1"));
  637. // Send Rebind message to the server.
  638. ASSERT_NO_THROW(client.doRebind());
  639. // The client should still have one lease which belongs to one of the
  640. // subnets.
  641. ASSERT_EQ(1, client.getLeaseNum());
  642. Lease6 lease_client2 = client.getLease(0);
  643. ASSERT_TRUE(CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->
  644. selectSubnet(lease_client2.addr_, ClientClasses()));
  645. // The client's lease should have been extended. The client will
  646. // update the cltt to current time when the lease gets extended.
  647. ASSERT_GE(lease_client2.cltt_ - lease_client.cltt_, 1000);
  648. // Make sure, that the client's lease matches the lease held by the
  649. // server.
  650. Lease6Ptr lease_server2 = checkLease(lease_client2);
  651. EXPECT_TRUE(lease_server2);
  652. }
  653. } // end of anonymous namespace