dhcp6_test_utils.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (C) 2013 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. namespace isc {
  17. namespace test {
  18. // Checks that server response (ADVERTISE or REPLY) contains proper IA_NA option
  19. // It returns IAADDR option for each chaining with checkIAAddr method.
  20. boost::shared_ptr<Option6IAAddr>
  21. Dhcpv6SrvTest::checkIA_NA(const Pkt6Ptr& rsp, uint32_t expected_iaid,
  22. uint32_t expected_t1, uint32_t expected_t2) {
  23. OptionPtr tmp = rsp->getOption(D6O_IA_NA);
  24. // Can't use ASSERT_TRUE() in method that returns something
  25. if (!tmp) {
  26. ADD_FAILURE() << "IA_NA option not present in response";
  27. return (boost::shared_ptr<Option6IAAddr>());
  28. }
  29. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  30. if (!ia) {
  31. ADD_FAILURE() << "IA_NA cannot convert option ptr to Option6";
  32. return (boost::shared_ptr<Option6IAAddr>());
  33. }
  34. EXPECT_EQ(expected_iaid, ia->getIAID());
  35. EXPECT_EQ(expected_t1, ia->getT1());
  36. EXPECT_EQ(expected_t2, ia->getT2());
  37. tmp = ia->getOption(D6O_IAADDR);
  38. boost::shared_ptr<Option6IAAddr> addr = boost::dynamic_pointer_cast<Option6IAAddr>(tmp);
  39. return (addr);
  40. }
  41. boost::shared_ptr<Option6IAPrefix>
  42. Dhcpv6SrvTest::checkIA_PD(const Pkt6Ptr& rsp, uint32_t expected_iaid,
  43. uint32_t expected_t1, uint32_t expected_t2) {
  44. OptionPtr tmp = rsp->getOption(D6O_IA_PD);
  45. // Can't use ASSERT_TRUE() in method that returns something
  46. if (!tmp) {
  47. ADD_FAILURE() << "IA_PD option not present in response";
  48. return (boost::shared_ptr<Option6IAPrefix>());
  49. }
  50. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  51. if (!ia) {
  52. ADD_FAILURE() << "IA_PD cannot convert option ptr to Option6";
  53. return (boost::shared_ptr<Option6IAPrefix>());
  54. }
  55. EXPECT_EQ(expected_iaid, ia->getIAID());
  56. EXPECT_EQ(expected_t1, ia->getT1());
  57. EXPECT_EQ(expected_t2, ia->getT2());
  58. tmp = ia->getOption(D6O_IAPREFIX);
  59. boost::shared_ptr<Option6IAPrefix> addr = boost::dynamic_pointer_cast<Option6IAPrefix>(tmp);
  60. return (addr);
  61. }
  62. // Checks if the lease sent to client is present in the database
  63. // and is valid when checked agasint the configured subnet
  64. Lease6Ptr
  65. Dhcpv6SrvTest::checkLease(const DuidPtr& duid, const OptionPtr& ia_na,
  66. boost::shared_ptr<Option6IAAddr> addr) {
  67. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(ia_na);
  68. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  69. addr->getAddress());
  70. if (!lease) {
  71. std::cout << "Lease for " << addr->getAddress().toText()
  72. << " not found in the database backend.";
  73. return (Lease6Ptr());
  74. }
  75. EXPECT_EQ(addr->getAddress().toText(), lease->addr_.toText());
  76. EXPECT_TRUE(*lease->duid_ == *duid);
  77. EXPECT_EQ(ia->getIAID(), lease->iaid_);
  78. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  79. return (lease);
  80. }
  81. Lease6Ptr
  82. Dhcpv6SrvTest::checkPdLease(const DuidPtr& duid, const OptionPtr& ia_pd,
  83. boost::shared_ptr<Option6IAPrefix> prefix){
  84. boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(ia_pd);
  85. Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_PD,
  86. prefix->getAddress());
  87. if (!lease) {
  88. std::cout << "PD lease for " << prefix->getAddress().toText()
  89. << " not found in the database backend.";
  90. return (Lease6Ptr());
  91. }
  92. EXPECT_EQ(prefix->getAddress().toText(), lease->addr_.toText());
  93. EXPECT_TRUE(*lease->duid_ == *duid);
  94. EXPECT_EQ(ia->getIAID(), lease->iaid_);
  95. EXPECT_EQ(subnet_->getID(), lease->subnet_id_);
  96. return (lease);
  97. }
  98. void
  99. Dhcpv6SrvTest::testRenewBasic(Lease::Type type, const std::string& existing_addr,
  100. const std::string& renew_addr) {
  101. NakedDhcpv6Srv srv(0);
  102. const IOAddress existing(existing_addr);
  103. const IOAddress renew(renew_addr);
  104. const uint32_t iaid = 234;
  105. // Generate client-id also duid_
  106. OptionPtr clientid = generateClientId();
  107. // Check that the address we are about to use is indeed in pool
  108. ASSERT_TRUE(subnet_->inPool(type, existing));
  109. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  110. // value on purpose. They should be updated during RENEW.
  111. Lease6Ptr lease(new Lease6(type, existing, duid_, iaid,
  112. 501, 502, 503, 504, subnet_->getID(), 128));
  113. lease->cltt_ = 1234;
  114. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  115. // Check that the lease is really in the database
  116. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(type, existing);
  117. ASSERT_TRUE(l);
  118. // Check that T1, T2, preferred, valid and cltt really set and not using
  119. // previous (500, 501, etc.) values
  120. EXPECT_NE(l->t1_, subnet_->getT1());
  121. EXPECT_NE(l->t2_, subnet_->getT2());
  122. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  123. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  124. EXPECT_NE(l->cltt_, time(NULL));
  125. // Let's create a RENEW
  126. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  127. req->setRemoteAddr(IOAddress("fe80::abcd"));
  128. uint16_t code = D6O_IA_NA;
  129. OptionPtr subopt;
  130. switch (type) {
  131. case Lease::TYPE_NA:
  132. code = D6O_IA_NA;
  133. subopt.reset(new Option6IAAddr(D6O_IAADDR, renew, 300, 500));
  134. break;
  135. case Lease::TYPE_PD:
  136. code = D6O_IA_PD;
  137. subopt.reset(new Option6IAPrefix(D6O_IAPREFIX, renew, 32u, 300, 500));
  138. default:
  139. isc_throw(BadValue, "Invalid lease type specified");
  140. }
  141. boost::shared_ptr<Option6IA> ia = generateIA(code, iaid, 1500, 3000);
  142. ia->addOption(subopt);
  143. req->addOption(ia);
  144. req->addOption(clientid);
  145. // Server-id is mandatory in RENEW
  146. req->addOption(srv.getServerID());
  147. // Pass it to the server and hope for a REPLY
  148. Pkt6Ptr reply = srv.processRenew(req);
  149. // Check if we get response at all
  150. checkResponse(reply, DHCPV6_REPLY, 1234);
  151. OptionPtr tmp = reply->getOption(code);
  152. ASSERT_TRUE(tmp);
  153. // Check DUIDs
  154. checkServerId(reply, srv.getServerID());
  155. checkClientId(reply, clientid);
  156. switch (type) {
  157. case Lease::TYPE_NA: {
  158. // Check that IA_NA was returned and that there's an address included
  159. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  160. subnet_->getT2());
  161. ASSERT_TRUE(addr_opt);
  162. // Check that we've got the address we requested
  163. checkIAAddr(addr_opt, renew, Lease::TYPE_NA);
  164. // Check that the lease is really in the database
  165. l = checkLease(duid_, reply->getOption(code), addr_opt);
  166. ASSERT_TRUE(l);
  167. break;
  168. }
  169. case Lease::TYPE_PD: {
  170. // Check that IA_NA was returned and that there's an address included
  171. boost::shared_ptr<Option6IAPrefix> prefix_opt
  172. = checkIA_PD(reply, 234, subnet_->getT1(), subnet_->getT2());
  173. ASSERT_TRUE(prefix_opt);
  174. // Check that we've got the address we requested
  175. checkIAAddr(prefix_opt, renew, Lease::TYPE_PD);
  176. EXPECT_EQ(pd_pool_->getLength(), prefix_opt->getLength());
  177. // Check that the lease is really in the database
  178. l = checkLease(duid_, reply->getOption(code), prefix_opt);
  179. ASSERT_TRUE(l);
  180. break;
  181. }
  182. default:
  183. isc_throw(BadValue, "Invalid lease type");
  184. }
  185. // Check that T1, T2, preferred, valid and cltt were really updated
  186. EXPECT_EQ(l->t1_, subnet_->getT1());
  187. EXPECT_EQ(l->t2_, subnet_->getT2());
  188. EXPECT_EQ(l->preferred_lft_, subnet_->getPreferred());
  189. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  190. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  191. int32_t cltt = static_cast<int32_t>(l->cltt_);
  192. int32_t expected = static_cast<int32_t>(time(NULL));
  193. // equality or difference by 1 between cltt and expected is ok.
  194. EXPECT_GE(1, abs(cltt - expected));
  195. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(renew_addr));
  196. }
  197. // Generate IA_NA option with specified parameters
  198. boost::shared_ptr<Option6IA>
  199. NakedDhcpv6SrvTest::generateIA(uint16_t type, uint32_t iaid, uint32_t t1,
  200. uint32_t t2) {
  201. boost::shared_ptr<Option6IA> ia =
  202. boost::shared_ptr<Option6IA>(new Option6IA(type, iaid));
  203. ia->setT1(t1);
  204. ia->setT2(t2);
  205. return (ia);
  206. }
  207. }; // end of isc::test namespace
  208. }; // end of isc namespace