rebind_unittest.cc 31 KB

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