dhcp6_test_utils.cc 30 KB

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