dhcp6_test_utils.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // Copyright (C) 2013-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 <gtest/gtest.h>
  16. #include <dhcp6/tests/dhcp6_test_utils.h>
  17. #include <dhcp6/json_config_parser.h>
  18. #include <util/pointer_util.h>
  19. #include <cc/command_interpreter.h>
  20. #include <string.h>
  21. using namespace isc::data;
  22. using namespace isc::dhcp;
  23. using namespace isc::asiolink;
  24. namespace isc {
  25. namespace test {
  26. Dhcpv6SrvTest::Dhcpv6SrvTest()
  27. :srv_(0) {
  28. subnet_ = isc::dhcp::Subnet6Ptr
  29. (new isc::dhcp::Subnet6(isc::asiolink::IOAddress("2001:db8:1::"),
  30. 48, 1000, 2000, 3000, 4000));
  31. subnet_->setIface("eth0");
  32. pool_ = isc::dhcp::Pool6Ptr
  33. (new isc::dhcp::Pool6(isc::dhcp::Lease::TYPE_NA,
  34. isc::asiolink::IOAddress("2001:db8:1:1::"),
  35. 64));
  36. subnet_->addPool(pool_);
  37. isc::dhcp::CfgMgr::instance().clear();
  38. isc::dhcp::CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet_);
  39. isc::dhcp::CfgMgr::instance().commit();
  40. // configure PD pool
  41. pd_pool_ = isc::dhcp::Pool6Ptr
  42. (new isc::dhcp::Pool6(isc::dhcp::Lease::TYPE_PD,
  43. isc::asiolink::IOAddress("2001:db8:1:2::"),
  44. 64, 80));
  45. subnet_->addPool(pd_pool_);
  46. }
  47. // Checks that server response (ADVERTISE or REPLY) contains proper IA_NA option
  48. // It returns IAADDR option for each chaining with checkIAAddr method.
  49. boost::shared_ptr<Option6IAAddr>
  50. Dhcpv6SrvTest::checkIA_NA(const Pkt6Ptr& rsp, uint32_t expected_iaid,
  51. uint32_t expected_t1, uint32_t expected_t2) {
  52. OptionPtr tmp = rsp->getOption(D6O_IA_NA);
  53. // Can't use ASSERT_TRUE() in method that returns something
  54. if (!tmp) {
  55. ADD_FAILURE() << "IA_NA option not present in response";
  56. return (boost::shared_ptr<Option6IAAddr>());
  57. }
  58. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  59. if (!ia) {
  60. ADD_FAILURE() << "IA_NA cannot convert option ptr to Option6";
  61. return (boost::shared_ptr<Option6IAAddr>());
  62. }
  63. EXPECT_EQ(expected_iaid, ia->getIAID());
  64. EXPECT_EQ(expected_t1, ia->getT1());
  65. EXPECT_EQ(expected_t2, ia->getT2());
  66. tmp = ia->getOption(D6O_IAADDR);
  67. boost::shared_ptr<Option6IAAddr> addr = boost::dynamic_pointer_cast<Option6IAAddr>(tmp);
  68. return (addr);
  69. }
  70. boost::shared_ptr<Option6IAPrefix>
  71. Dhcpv6SrvTest::checkIA_PD(const Pkt6Ptr& rsp, uint32_t expected_iaid,
  72. uint32_t expected_t1, uint32_t expected_t2) {
  73. OptionPtr tmp = rsp->getOption(D6O_IA_PD);
  74. // Can't use ASSERT_TRUE() in method that returns something
  75. if (!tmp) {
  76. ADD_FAILURE() << "IA_PD option not present in response";
  77. return (boost::shared_ptr<Option6IAPrefix>());
  78. }
  79. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  80. if (!ia) {
  81. ADD_FAILURE() << "IA_PD cannot convert option ptr to Option6";
  82. return (boost::shared_ptr<Option6IAPrefix>());
  83. }
  84. EXPECT_EQ(expected_iaid, ia->getIAID());
  85. EXPECT_EQ(expected_t1, ia->getT1());
  86. EXPECT_EQ(expected_t2, ia->getT2());
  87. tmp = ia->getOption(D6O_IAPREFIX);
  88. boost::shared_ptr<Option6IAPrefix> addr = boost::dynamic_pointer_cast<Option6IAPrefix>(tmp);
  89. return (addr);
  90. }
  91. // Checks if the lease sent to client is present in the database
  92. // and is valid when checked agasint the configured subnet
  93. Lease6Ptr
  94. Dhcpv6SrvTest::checkLease(const DuidPtr& duid, const OptionPtr& ia_na,
  95. boost::shared_ptr<Option6IAAddr> addr) {
  96. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(ia_na);
  97. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  98. addr->getAddress());
  99. if (!lease) {
  100. std::cout << "Lease for " << addr->getAddress()
  101. << " not found in the database backend.";
  102. return (Lease6Ptr());
  103. }
  104. EXPECT_EQ(addr->getAddress(), lease->addr_);
  105. EXPECT_TRUE(*lease->duid_ == *duid);
  106. EXPECT_EQ(ia->getIAID(), lease->iaid_);
  107. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  108. return (lease);
  109. }
  110. isc::dhcp::Lease6Ptr
  111. Dhcpv6SrvTest::checkLease(const isc::dhcp::Lease6& lease) {
  112. Lease6Ptr lease_db = LeaseMgrFactory::instance().getLease6(lease.type_,
  113. lease.addr_);
  114. if (!lease_db) {
  115. return (Lease6Ptr());
  116. }
  117. EXPECT_TRUE(util::nullOrEqualValues(lease_db->hwaddr_, lease.hwaddr_));
  118. EXPECT_TRUE(util::nullOrEqualValues(lease_db->duid_, lease.duid_));
  119. return (lease_db);
  120. }
  121. Lease6Ptr
  122. Dhcpv6SrvTest::checkPdLease(const DuidPtr& duid, const OptionPtr& ia_pd,
  123. boost::shared_ptr<Option6IAPrefix> prefix){
  124. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(ia_pd);
  125. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD,
  126. prefix->getAddress());
  127. if (!lease) {
  128. std::cout << "PD lease for " << prefix->getAddress()
  129. << " not found in the database backend.";
  130. return (Lease6Ptr());
  131. }
  132. EXPECT_EQ(prefix->getAddress(), lease->addr_);
  133. EXPECT_TRUE(*lease->duid_ == *duid);
  134. EXPECT_EQ(ia->getIAID(), lease->iaid_);
  135. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  136. return (lease);
  137. }
  138. Pkt6Ptr
  139. Dhcpv6SrvTest::createMessage(uint8_t message_type, Lease::Type lease_type,
  140. const IOAddress& addr, const uint8_t prefix_len,
  141. const uint32_t iaid) {
  142. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(message_type, 1234));
  143. msg->setRemoteAddr(IOAddress("fe80::abcd"));
  144. msg->setIface("eth0");
  145. msg->addOption(createIA(lease_type, addr, prefix_len, iaid));
  146. return (msg);
  147. }
  148. Option6IAPtr
  149. Dhcpv6SrvTest::createIA(isc::dhcp::Lease::Type lease_type,
  150. const isc::asiolink::IOAddress& addr,
  151. const uint8_t prefix_len, const uint32_t iaid) {
  152. uint16_t code;
  153. OptionPtr subopt;
  154. switch (lease_type) {
  155. case Lease::TYPE_NA:
  156. code = D6O_IA_NA;
  157. subopt.reset(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  158. break;
  159. case Lease::TYPE_PD:
  160. code = D6O_IA_PD;
  161. subopt.reset(new Option6IAPrefix(D6O_IAPREFIX, addr, prefix_len,
  162. 300, 500));
  163. break;
  164. default:
  165. isc_throw(BadValue, "Invalid lease type specified "
  166. << static_cast<int>(lease_type));
  167. }
  168. Option6IAPtr ia = generateIA(code, iaid, 1500, 3000);
  169. ia->addOption(subopt);
  170. return (ia);
  171. }
  172. void
  173. Dhcpv6SrvTest::testRenewBasic(Lease::Type type, const std::string& existing_addr,
  174. const std::string& renew_addr,
  175. const uint8_t prefix_len, bool insert_before_renew) {
  176. NakedDhcpv6Srv srv(0);
  177. const IOAddress existing(existing_addr);
  178. const IOAddress renew(renew_addr);
  179. const uint32_t iaid = 234;
  180. // Generate client-id also duid_
  181. OptionPtr clientid = generateClientId();
  182. // Check that the address we are about to use is indeed in pool
  183. ASSERT_TRUE(subnet_->inPool(type, existing));
  184. Lease6Ptr l;
  185. if (insert_before_renew) {
  186. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  187. // value on purpose. They should be updated during RENEW.
  188. Lease6Ptr lease(new Lease6(type, existing, duid_, iaid, 501, 502, 503, 504,
  189. subnet_->getID(), HWAddrPtr(), prefix_len));
  190. lease->cltt_ = 1234;
  191. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  192. // Check that the lease is really in the database
  193. l = LeaseMgrFactory::instance().getLease6(type, existing);
  194. ASSERT_TRUE(l);
  195. // Check that T1, T2, preferred, valid and cltt really set and not using
  196. // previous (500, 501, etc.) values
  197. EXPECT_NE(l->t1_, subnet_->getT1());
  198. EXPECT_NE(l->t2_, subnet_->getT2());
  199. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  200. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  201. EXPECT_NE(l->cltt_, time(NULL));
  202. }
  203. Pkt6Ptr req = createMessage(DHCPV6_RENEW, type, IOAddress(renew_addr),
  204. prefix_len, iaid);
  205. req->addOption(clientid);
  206. req->addOption(srv.getServerID());
  207. // Pass it to the server and hope for a REPLY
  208. Pkt6Ptr reply = srv.processRenew(req);
  209. // Check if we get response at all
  210. checkResponse(reply, DHCPV6_REPLY, 1234);
  211. // Check DUIDs
  212. checkServerId(reply, srv.getServerID());
  213. checkClientId(reply, clientid);
  214. switch (type) {
  215. case Lease::TYPE_NA: {
  216. // Check that IA_NA was returned and that there's an address included
  217. boost::shared_ptr<Option6IAAddr>
  218. addr_opt = checkIA_NA(reply, 234, subnet_->getT1(), subnet_->getT2());
  219. ASSERT_TRUE(addr_opt);
  220. // Check that we've got the address we requested
  221. checkIAAddr(addr_opt, renew, Lease::TYPE_NA);
  222. // Check that the lease is really in the database
  223. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  224. ASSERT_TRUE(l);
  225. break;
  226. }
  227. case Lease::TYPE_PD: {
  228. // Check that IA_NA was returned and that there's an address included
  229. boost::shared_ptr<Option6IAPrefix> prefix_opt
  230. = checkIA_PD(reply, 234, subnet_->getT1(), subnet_->getT2());
  231. ASSERT_TRUE(prefix_opt);
  232. // Check that we've got the address we requested
  233. checkIAAddr(prefix_opt, renew, Lease::TYPE_PD);
  234. EXPECT_EQ(pd_pool_->getLength(), prefix_opt->getLength());
  235. // Check that the lease is really in the database
  236. l = checkPdLease(duid_, reply->getOption(D6O_IA_PD), prefix_opt);
  237. ASSERT_TRUE(l);
  238. break;
  239. }
  240. default:
  241. isc_throw(BadValue, "Invalid lease type");
  242. }
  243. // Check that T1, T2, preferred, valid and cltt were really updated
  244. EXPECT_EQ(subnet_->getT1(), l->t1_);
  245. EXPECT_EQ(subnet_->getT2(), l->t2_);
  246. EXPECT_EQ(subnet_->getPreferred(), l->preferred_lft_);
  247. EXPECT_EQ(subnet_->getValid(), l->valid_lft_);
  248. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  249. int32_t cltt = static_cast<int32_t>(l->cltt_);
  250. int32_t expected = static_cast<int32_t>(time(NULL));
  251. // equality or difference by 1 between cltt and expected is ok.
  252. EXPECT_GE(1, abs(cltt - expected));
  253. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(renew_addr));
  254. }
  255. void
  256. Dhcpv6SrvTest::testRenewWrongIAID(Lease::Type type, const IOAddress& addr) {
  257. NakedDhcpv6Srv srv(0);
  258. const uint32_t transid = 1234;
  259. const uint32_t valid_iaid = 234;
  260. const uint32_t bogus_iaid = 456;
  261. uint8_t prefix_len = (type == Lease::TYPE_PD) ? 128 : pd_pool_->getLength();
  262. // Quick sanity check that the address we're about to use is ok
  263. ASSERT_TRUE(subnet_->inPool(type, addr));
  264. // Check that the lease is NOT in the database
  265. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(type, addr);
  266. ASSERT_FALSE(l);
  267. // GenerateClientId() also sets duid_
  268. OptionPtr clientid = generateClientId();
  269. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  270. // value on purpose. They should be updated during RENEW.
  271. Lease6Ptr lease(new Lease6(type, addr, duid_, valid_iaid,
  272. 501, 502, 503, 504, subnet_->getID(),
  273. HWAddrPtr(), prefix_len));
  274. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  275. // Pass it to the server and hope for a REPLY
  276. // Let's create a RENEW
  277. Pkt6Ptr renew = createMessage(DHCPV6_RENEW, type, IOAddress(addr), prefix_len,
  278. bogus_iaid);
  279. renew->addOption(clientid);
  280. renew->addOption(srv.getServerID());
  281. // The duid and address matches, but the iaid is different. The server could
  282. // respond with NoBinding. However, according to
  283. // draft-ietf-dhc-dhcpv6-stateful-issues-10, the server can also assign a
  284. // new address. And that's what we expect here.
  285. Pkt6Ptr reply = srv.processRenew(renew);
  286. checkResponse(reply, DHCPV6_REPLY, transid);
  287. // Check that IA_NA was returned and that there's an address included
  288. boost::shared_ptr<Option6IAAddr>
  289. addr_opt = checkIA_NA(reply, bogus_iaid, subnet_->getT1(), subnet_->getT2());
  290. ASSERT_TRUE(addr_opt);
  291. // Check that we've got the an address
  292. checkIAAddr(addr_opt, addr_opt->getAddress(), Lease::TYPE_NA);
  293. // Check that we got a different address than was in the database.
  294. EXPECT_NE(addr_opt->getAddress().toText(), addr.toText());
  295. // Check that the lease is really in the database
  296. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  297. ASSERT_TRUE(l);
  298. }
  299. void
  300. Dhcpv6SrvTest::testRenewSomeoneElsesLease(Lease::Type type, const IOAddress& addr) {
  301. NakedDhcpv6Srv srv(0);
  302. const uint32_t valid_iaid = 234;
  303. const uint32_t transid = 1234;
  304. uint8_t prefix_len = (type == Lease::TYPE_PD) ? 128 : pd_pool_->getLength();
  305. // GenerateClientId() also sets duid_
  306. OptionPtr clientid = generateClientId();
  307. // Let's create a lease.
  308. Lease6Ptr lease(new Lease6(type, addr, duid_, valid_iaid,
  309. 501, 502, 503, 504, subnet_->getID(),
  310. HWAddrPtr(), prefix_len));
  311. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  312. // CASE 3: Lease belongs to a client with different client-id
  313. Pkt6Ptr renew = createMessage(DHCPV6_RENEW, type, IOAddress(addr), prefix_len,
  314. valid_iaid);
  315. renew->addOption(generateClientId(13)); // generate different DUID (length 13)
  316. renew->addOption(srv.getServerID());
  317. // The iaid and address matches, but the duid is different.
  318. // The server should not renew it, but assign something else.
  319. Pkt6Ptr reply = srv.processRenew(renew);
  320. checkResponse(reply, DHCPV6_REPLY, transid);
  321. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  322. ASSERT_TRUE(tmp);
  323. // Check that IA_?? was returned and that there's proper status code
  324. // Check that IA_NA was returned and that there's an address included
  325. boost::shared_ptr<Option6IAAddr>
  326. addr_opt = checkIA_NA(reply, valid_iaid, subnet_->getT1(), subnet_->getT2());
  327. ASSERT_TRUE(addr_opt);
  328. // Check that we've got the an address
  329. checkIAAddr(addr_opt, addr_opt->getAddress(), Lease::TYPE_NA);
  330. // Check that we got a different address than was in the database.
  331. EXPECT_NE(addr_opt->getAddress().toText(), addr.toText());
  332. // Check that the lease is really in the database
  333. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  334. ASSERT_TRUE(l);
  335. }
  336. void
  337. Dhcpv6SrvTest::testRenewReject(Lease::Type type, const IOAddress& addr) {
  338. NakedDhcpv6Srv srv(0);
  339. const uint32_t transid = 1234;
  340. const uint32_t valid_iaid = 234;
  341. const uint32_t bogus_iaid = 456;
  342. uint32_t code;
  343. uint8_t prefix_len;
  344. if (type == Lease::TYPE_NA) {
  345. code = D6O_IA_NA;
  346. prefix_len = 128;
  347. } else if (type == Lease::TYPE_PD) {
  348. code = D6O_IA_PD;
  349. prefix_len = pd_pool_->getLength();
  350. } else {
  351. isc_throw(BadValue, "Invalid lease type");
  352. }
  353. // Quick sanity check that the address we're about to use is ok
  354. ASSERT_TRUE(subnet_->inPool(type, addr));
  355. // GenerateClientId() also sets duid_
  356. OptionPtr clientid = generateClientId();
  357. // Check that the lease is NOT in the database
  358. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(type, addr);
  359. ASSERT_FALSE(l);
  360. // Let's create a RENEW
  361. Pkt6Ptr req = createMessage(DHCPV6_RENEW, type, IOAddress(addr), prefix_len,
  362. bogus_iaid);
  363. req->addOption(clientid);
  364. req->addOption(srv.getServerID());
  365. // Case 1: No lease known to server
  366. // Pass it to the server and hope for a REPLY
  367. Pkt6Ptr reply = srv.processRenew(req);
  368. // Check if we get response at all
  369. checkResponse(reply, DHCPV6_REPLY, transid);
  370. OptionPtr tmp = reply->getOption(code);
  371. ASSERT_TRUE(tmp);
  372. // Check that IA_?? was returned and that there's proper status code
  373. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  374. ASSERT_TRUE(ia);
  375. if (type == Lease::TYPE_PD) {
  376. // For PD, the check is easy. NoBinding and no prefixes
  377. checkIA_NAStatusCode(ia, STATUS_NoBinding, subnet_->getT1(), subnet_->getT2());
  378. } else {
  379. // For IA, it's more involved, as the server will reject the address
  380. // (and send it with 0 lifetimes), but will also assign a new address.
  381. // First, check that the requested address is rejected.
  382. bool found = false;
  383. dhcp::OptionCollection options = ia->getOptions();
  384. for (isc::dhcp::OptionCollection::iterator opt = options.begin();
  385. opt != options.end(); ++opt) {
  386. if (opt->second->getType() != D6O_IAADDR) {
  387. continue;
  388. }
  389. dhcp::Option6IAAddrPtr opt_addr =
  390. boost::dynamic_pointer_cast<isc::dhcp::Option6IAAddr>(opt->second);
  391. ASSERT_TRUE(opt_addr);
  392. if (opt_addr->getAddress() != addr) {
  393. // There may be other addresses, e.g. the newly assigned one
  394. continue;
  395. }
  396. found = true;
  397. EXPECT_NE(0, opt_addr->getPreferred());
  398. EXPECT_NE(0, opt_addr->getValid());
  399. }
  400. EXPECT_TRUE(found) << "Expected address " << addr.toText()
  401. << " with zero lifetimes not found.";
  402. }
  403. // Check that there is no lease added
  404. l = LeaseMgrFactory::instance().getLease6(type, addr);
  405. ASSERT_FALSE(l);
  406. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  407. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  408. // value on purpose. They should be updated during RENEW.
  409. Lease6Ptr lease(new Lease6(type, addr, duid_, valid_iaid,
  410. 501, 502, 503, 504, subnet_->getID(),
  411. HWAddrPtr(), prefix_len));
  412. lease->cltt_ = 123; // Let's use it as an indicator that the lease
  413. // was NOT updated.
  414. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  415. // Pass it to the server and hope for a REPLY
  416. reply = srv.processRenew(req);
  417. checkResponse(reply, DHCPV6_REPLY, transid);
  418. tmp = reply->getOption(code);
  419. ASSERT_TRUE(tmp);
  420. // Check that IA_?? was returned and that there's proper status code
  421. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  422. ASSERT_TRUE(ia);
  423. checkIA_NAStatusCode(ia, STATUS_NoBinding, subnet_->getT1(), subnet_->getT2());
  424. // There is a iaid mis-match, so server should respond that there is
  425. // no such address to renew.
  426. // CASE 3: Lease belongs to a client with different client-id
  427. req->delOption(D6O_CLIENTID);
  428. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(code));
  429. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  430. req->addOption(generateClientId(13)); // generate different DUID
  431. // (with length 13)
  432. reply = srv.processRenew(req);
  433. checkResponse(reply, DHCPV6_REPLY, transid);
  434. tmp = reply->getOption(code);
  435. ASSERT_TRUE(tmp);
  436. // Check that IA_?? was returned and that there's proper status code
  437. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  438. ASSERT_TRUE(ia);
  439. checkIA_NAStatusCode(ia, STATUS_NoBinding, subnet_->getT1(), subnet_->getT2());
  440. lease = LeaseMgrFactory::instance().getLease6(type, addr);
  441. ASSERT_TRUE(lease);
  442. // Verify that the lease was not updated.
  443. EXPECT_EQ(123, lease->cltt_);
  444. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  445. }
  446. void
  447. Dhcpv6SrvTest::testReleaseBasic(Lease::Type type, const IOAddress& existing,
  448. const IOAddress& release_addr) {
  449. NakedDhcpv6Srv srv(0);
  450. const uint32_t iaid = 234;
  451. uint32_t code; // option code of the container (IA_NA or IA_PD)
  452. uint8_t prefix_len;
  453. if (type == Lease::TYPE_NA) {
  454. code = D6O_IA_NA;
  455. prefix_len = 128;
  456. } else if (type == Lease::TYPE_PD) {
  457. code = D6O_IA_PD;
  458. prefix_len = pd_pool_->getLength();
  459. } else {
  460. isc_throw(BadValue, "Invalid lease type");
  461. }
  462. // Generate client-id also duid_
  463. OptionPtr clientid = generateClientId();
  464. // Check that the address we are about to use is indeed in pool
  465. ASSERT_TRUE(subnet_->inPool(type, existing));
  466. // Let's prepopulate the database
  467. Lease6Ptr lease(new Lease6(type, existing, duid_, iaid,
  468. 501, 502, 503, 504, subnet_->getID(),
  469. HWAddrPtr(), prefix_len));
  470. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  471. // Check that the lease is really in the database
  472. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(type, existing);
  473. ASSERT_TRUE(l);
  474. // Let's create a RELEASE
  475. Pkt6Ptr rel = createMessage(DHCPV6_RELEASE, type, release_addr, prefix_len,
  476. iaid);
  477. rel->addOption(clientid);
  478. rel->addOption(srv.getServerID());
  479. // Pass it to the server and hope for a REPLY
  480. Pkt6Ptr reply = srv.processRelease(rel);
  481. // Check if we get response at all
  482. checkResponse(reply, DHCPV6_REPLY, 1234);
  483. OptionPtr tmp = reply->getOption(code);
  484. ASSERT_TRUE(tmp);
  485. // Check that IA_NA was returned and that there's an address included
  486. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  487. checkIA_NAStatusCode(ia, STATUS_Success, 0, 0);
  488. checkMsgStatusCode(reply, STATUS_Success);
  489. // There should be no address returned in RELEASE (see RFC3315, 18.2.6)
  490. // There should be no prefix
  491. EXPECT_FALSE(tmp->getOption(D6O_IAADDR));
  492. EXPECT_FALSE(tmp->getOption(D6O_IAPREFIX));
  493. // Check DUIDs
  494. checkServerId(reply, srv.getServerID());
  495. checkClientId(reply, clientid);
  496. // Check that the lease is really gone in the database
  497. // get lease by address
  498. l = LeaseMgrFactory::instance().getLease6(type, release_addr);
  499. ASSERT_FALSE(l);
  500. // get lease by subnetid/duid/iaid combination
  501. l = LeaseMgrFactory::instance().getLease6(type, *duid_, iaid,
  502. subnet_->getID());
  503. ASSERT_FALSE(l);
  504. }
  505. void
  506. Dhcpv6SrvTest::testReleaseReject(Lease::Type type, const IOAddress& addr) {
  507. NakedDhcpv6Srv srv(0);
  508. const uint32_t transid = 1234;
  509. const uint32_t valid_iaid = 234;
  510. const uint32_t bogus_iaid = 456;
  511. uint32_t code; // option code of the container (IA_NA or IA_PD)
  512. uint8_t prefix_len;
  513. if (type == Lease::TYPE_NA) {
  514. code = D6O_IA_NA;
  515. prefix_len = 128;
  516. } else if (type == Lease::TYPE_PD) {
  517. code = D6O_IA_PD;
  518. prefix_len = pd_pool_->getLength();
  519. } else {
  520. isc_throw(BadValue, "Invalid lease type");
  521. }
  522. // Quick sanity check that the address we're about to use is ok
  523. ASSERT_TRUE(subnet_->inPool(type, addr));
  524. // GenerateClientId() also sets duid_
  525. OptionPtr clientid = generateClientId();
  526. // Check that the lease is NOT in the database
  527. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(type, addr);
  528. ASSERT_FALSE(l);
  529. // Let's create a RELEASE
  530. Pkt6Ptr rel = createMessage(DHCPV6_RELEASE, type, addr, prefix_len, valid_iaid);
  531. rel->addOption(clientid);
  532. rel->addOption(srv.getServerID());
  533. // Case 1: No lease known to server
  534. SCOPED_TRACE("CASE 1: No lease known to server");
  535. // Pass it to the server and hope for a REPLY
  536. Pkt6Ptr reply = srv.processRelease(rel);
  537. // Check if we get response at all
  538. checkResponse(reply, DHCPV6_REPLY, transid);
  539. OptionPtr tmp = reply->getOption(code);
  540. ASSERT_TRUE(tmp);
  541. // Check that IA_NA/IA_PD was returned and that there's status code in it
  542. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  543. ASSERT_TRUE(ia);
  544. checkIA_NAStatusCode(ia, STATUS_NoBinding, 0, 0);
  545. checkMsgStatusCode(reply, STATUS_NoBinding);
  546. // Check that the lease is not there
  547. l = LeaseMgrFactory::instance().getLease6(type, addr);
  548. ASSERT_FALSE(l);
  549. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  550. SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but to a different IAID");
  551. Lease6Ptr lease(new Lease6(type, addr, duid_, valid_iaid, 501, 502, 503,
  552. 504, subnet_->getID(), HWAddrPtr(), prefix_len));
  553. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  554. // Let's create a different RELEASE, with a bogus iaid
  555. rel = createMessage(DHCPV6_RELEASE, type, addr, prefix_len, bogus_iaid);
  556. rel->addOption(clientid);
  557. rel->addOption(srv.getServerID());
  558. // Pass it to the server and hope for a REPLY
  559. reply = srv.processRelease(rel);
  560. checkResponse(reply, DHCPV6_REPLY, transid);
  561. tmp = reply->getOption(code);
  562. ASSERT_TRUE(tmp);
  563. // Check that IA_?? was returned and that there's proper status code
  564. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  565. ASSERT_TRUE(ia);
  566. checkIA_NAStatusCode(ia, STATUS_NoBinding, 0, 0);
  567. checkMsgStatusCode(reply, STATUS_NoBinding);
  568. // Check that the lease is still there
  569. l = LeaseMgrFactory::instance().getLease6(type, addr);
  570. ASSERT_TRUE(l);
  571. // CASE 3: Lease belongs to a client with different client-id
  572. SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
  573. rel->delOption(D6O_CLIENTID);
  574. ia = boost::dynamic_pointer_cast<Option6IA>(rel->getOption(code));
  575. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  576. rel->addOption(generateClientId(13)); // generate different DUID
  577. // (with length 13)
  578. reply = srv.processRelease(rel);
  579. checkResponse(reply, DHCPV6_REPLY, transid);
  580. tmp = reply->getOption(code);
  581. ASSERT_TRUE(tmp);
  582. // Check that IA_?? was returned and that there's proper status code
  583. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  584. ASSERT_TRUE(ia);
  585. checkIA_NAStatusCode(ia, STATUS_NoBinding, 0, 0);
  586. checkMsgStatusCode(reply, STATUS_NoBinding);
  587. // Check that the lease is still there
  588. l = LeaseMgrFactory::instance().getLease6(type, addr);
  589. ASSERT_TRUE(l);
  590. // Finally, let's cleanup the database
  591. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  592. }
  593. void
  594. Dhcpv6SrvTest::configure(const std::string& config) {
  595. configure(config, srv_);
  596. }
  597. void
  598. Dhcpv6SrvTest::configure(const std::string& config, NakedDhcpv6Srv& srv) {
  599. ElementPtr json = data::Element::fromJSON(config);
  600. ConstElementPtr status;
  601. // Configure the server and make sure the config is accepted
  602. EXPECT_NO_THROW(status = configureDhcp6Server(srv, json));
  603. ASSERT_TRUE(status);
  604. int rcode;
  605. ConstElementPtr comment = isc::config::parseAnswer(rcode, status);
  606. ASSERT_EQ(0, rcode);
  607. CfgMgr::instance().commit();
  608. }
  609. // Generate IA_NA option with specified parameters
  610. boost::shared_ptr<Option6IA>
  611. NakedDhcpv6SrvTest::generateIA(uint16_t type, uint32_t iaid, uint32_t t1,
  612. uint32_t t2) {
  613. boost::shared_ptr<Option6IA> ia =
  614. boost::shared_ptr<Option6IA>(new Option6IA(type, iaid));
  615. ia->setT1(t1);
  616. ia->setT2(t2);
  617. return (ia);
  618. }
  619. bool
  620. Dhcpv6SrvTest::compareOptions(const isc::dhcp::OptionPtr& option1,
  621. const isc::dhcp::OptionPtr& option2) {
  622. if ((!option1 && option2) || (option1 && !option2)) {
  623. return (false);
  624. }
  625. if (!option1 && !option2) {
  626. return (true);
  627. }
  628. // We could start by comparing option codes and option lengths
  629. // here, but it's just a waste of time. These are tests, so they
  630. // don't have to be super performant. The pack+memcmp approach
  631. // verifies all in one go.
  632. isc::util::OutputBuffer buf1(0);
  633. isc::util::OutputBuffer buf2(0);
  634. option1->pack(buf1);
  635. option2->pack(buf2);
  636. if (buf1.getLength() != buf2.getLength()) {
  637. return (false);
  638. }
  639. // memcmp returns 0 when equal.
  640. return (!memcmp(buf1.getData(), buf2.getData(), buf1.getLength()));
  641. }
  642. void
  643. NakedDhcpv6SrvTest::checkIA_NAStatusCode(
  644. const boost::shared_ptr<isc::dhcp::Option6IA>& ia,
  645. uint16_t expected_status_code, uint32_t expected_t1, uint32_t expected_t2,
  646. bool check_addr)
  647. {
  648. // Make sure there is no address assigned. Depending on the situation,
  649. // the server will either not return the address at all and sometimes
  650. // it will return it with zeroed lifetimes.
  651. if (check_addr) {
  652. dhcp::OptionCollection options = ia->getOptions();
  653. for (isc::dhcp::OptionCollection::iterator opt = options.begin();
  654. opt != options.end(); ++opt) {
  655. if (opt->second->getType() != D6O_IAADDR) {
  656. continue;
  657. }
  658. dhcp::Option6IAAddrPtr addr =
  659. boost::dynamic_pointer_cast<isc::dhcp::Option6IAAddr>(opt->second);
  660. ASSERT_TRUE(addr);
  661. EXPECT_EQ(0, addr->getPreferred());
  662. EXPECT_EQ(0, addr->getValid());
  663. }
  664. }
  665. // T1, T2 should NOT be zeroed. draft-ietf-dhc-dhcpv6-stateful-issues-10,
  666. // section 4.4.6 says says that T1,T2 should be consistent along all
  667. // provided IA options.
  668. EXPECT_EQ(expected_t1, ia->getT1());
  669. EXPECT_EQ(expected_t2, ia->getT2());
  670. isc::dhcp::OptionCustomPtr status =
  671. boost::dynamic_pointer_cast<isc::dhcp::OptionCustom>
  672. (ia->getOption(D6O_STATUS_CODE));
  673. // It is ok to not include status success as this is the default
  674. // behavior
  675. if (expected_status_code == STATUS_Success && !status) {
  676. return;
  677. }
  678. EXPECT_TRUE(status);
  679. if (status) {
  680. // We don't have dedicated class for status code, so let's
  681. // just interpret first 2 bytes as status. Remainder of the
  682. // status code option content is just a text explanation
  683. // what went wrong.
  684. EXPECT_EQ(static_cast<uint16_t>(expected_status_code),
  685. status->readInteger<uint16_t>(0));
  686. }
  687. }
  688. }; // end of isc::test namespace
  689. }; // end of isc namespace