dhcp6_srv_unittest.cc 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  1. // Copyright (C) 2011-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 <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp_ddns/ncr_msg.h>
  17. #include <dhcp/dhcp6.h>
  18. #include <dhcp/duid.h>
  19. #include <dhcp/option.h>
  20. #include <dhcp/option_custom.h>
  21. #include <dhcp/option6_addrlst.h>
  22. #include <dhcp/option6_client_fqdn.h>
  23. #include <dhcp/option6_ia.h>
  24. #include <dhcp/option6_iaaddr.h>
  25. #include <dhcp/option_int_array.h>
  26. #include <dhcp/iface_mgr.h>
  27. #include <dhcp6/config_parser.h>
  28. #include <dhcp/dhcp6.h>
  29. #include <dhcpsrv/cfgmgr.h>
  30. #include <dhcpsrv/lease_mgr.h>
  31. #include <dhcpsrv/lease_mgr_factory.h>
  32. #include <dhcpsrv/utils.h>
  33. #include <util/buffer.h>
  34. #include <util/range_utilities.h>
  35. #include <hooks/server_hooks.h>
  36. #include <dhcp6/tests/dhcp6_test_utils.h>
  37. #include <boost/pointer_cast.hpp>
  38. #include <boost/scoped_ptr.hpp>
  39. #include <gtest/gtest.h>
  40. #include <unistd.h>
  41. #include <fstream>
  42. #include <iostream>
  43. #include <sstream>
  44. using namespace isc;
  45. using namespace isc::test;
  46. using namespace isc::asiolink;
  47. using namespace isc::dhcp;
  48. using namespace isc::dhcp_ddns;
  49. using namespace isc::util;
  50. using namespace isc::hooks;
  51. using namespace std;
  52. // namespace has to be named, because friends are defined in Dhcpv6Srv class
  53. // Maybe it should be isc::test?
  54. namespace {
  55. // This is a test fixture class for testing the processing of the DHCPv6 Client
  56. // FQDN Option.
  57. class FqdnDhcpv6SrvTest : public Dhcpv6SrvTest {
  58. public:
  59. // Constructor
  60. FqdnDhcpv6SrvTest()
  61. : Dhcpv6SrvTest() {
  62. // generateClientId assigns DUID to duid_.
  63. generateClientId();
  64. lease_.reset(new Lease6(Lease6::LEASE_IA_NA, IOAddress("2001:db8:1::1"),
  65. duid_, 1234, 501, 502, 503,
  66. 504, 1, 0));
  67. }
  68. // Destructor
  69. virtual ~FqdnDhcpv6SrvTest() {
  70. }
  71. // Construct the DHCPv6 Client FQDN Option using flags and domain-name.
  72. Option6ClientFqdnPtr
  73. createClientFqdn(const uint8_t flags,
  74. const std::string& fqdn_name,
  75. const Option6ClientFqdn::DomainNameType fqdn_type) {
  76. return (Option6ClientFqdnPtr(new Option6ClientFqdn(flags,
  77. fqdn_name,
  78. fqdn_type)));
  79. }
  80. // Create a message holding DHCPv6 Client FQDN Option.
  81. Pkt6Ptr generatePktWithFqdn(uint8_t msg_type,
  82. const uint8_t fqdn_flags,
  83. const std::string& fqdn_domain_name,
  84. const Option6ClientFqdn::DomainNameType
  85. fqdn_type,
  86. const bool include_oro,
  87. OptionPtr srvid = OptionPtr()) {
  88. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(msg_type, 1234));
  89. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  90. Option6IAPtr ia = generateIA(234, 1500, 3000);
  91. if (msg_type != DHCPV6_REPLY) {
  92. IOAddress hint("2001:db8:1:1::dead:beef");
  93. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  94. ia->addOption(hint_opt);
  95. pkt->addOption(ia);
  96. }
  97. OptionPtr clientid = generateClientId();
  98. pkt->addOption(clientid);
  99. if (srvid && (msg_type != DHCPV6_SOLICIT)) {
  100. pkt->addOption(srvid);
  101. }
  102. pkt->addOption(createClientFqdn(fqdn_flags, fqdn_domain_name,
  103. fqdn_type));
  104. if (include_oro) {
  105. OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6,
  106. D6O_ORO));
  107. oro->addValue(D6O_CLIENT_FQDN);
  108. pkt->addOption(oro);
  109. }
  110. return (pkt);
  111. }
  112. // Creates instance of the DHCPv6 message with client id and server id.
  113. Pkt6Ptr generateMessageWithIds(const uint8_t msg_type,
  114. NakedDhcpv6Srv& srv) {
  115. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(msg_type, 1234));
  116. // Generate client-id.
  117. OptionPtr opt_clientid = generateClientId();
  118. pkt->addOption(opt_clientid);
  119. if (msg_type != DHCPV6_SOLICIT) {
  120. // Generate server-id.
  121. pkt->addOption(srv.getServerID());
  122. }
  123. return (pkt);
  124. }
  125. // Returns an instance of the option carrying FQDN.
  126. Option6ClientFqdnPtr getClientFqdnOption(const Pkt6Ptr& pkt) {
  127. return (boost::dynamic_pointer_cast<Option6ClientFqdn>
  128. (pkt->getOption(D6O_CLIENT_FQDN)));
  129. }
  130. // Adds IA option to the message. Option holds an address.
  131. void addIA(const uint32_t iaid, const IOAddress& addr, Pkt6Ptr& pkt) {
  132. Option6IAPtr opt_ia = generateIA(iaid, 1500, 3000);
  133. Option6IAAddrPtr opt_iaaddr(new Option6IAAddr(D6O_IAADDR, addr,
  134. 300, 500));
  135. opt_ia->addOption(opt_iaaddr);
  136. pkt->addOption(opt_ia);
  137. }
  138. // Adds IA option to the message. Option holds status code.
  139. void addIA(const uint32_t iaid, const uint16_t status_code, Pkt6Ptr& pkt) {
  140. Option6IAPtr opt_ia = generateIA(iaid, 1500, 3000);
  141. addStatusCode(status_code, "", opt_ia);
  142. pkt->addOption(opt_ia);
  143. }
  144. // Creates status code with the specified code and message.
  145. OptionCustomPtr createStatusCode(const uint16_t code,
  146. const std::string& msg) {
  147. OptionDefinition def("status-code", D6O_STATUS_CODE, "record");
  148. def.addRecordField("uint16");
  149. def.addRecordField("string");
  150. OptionCustomPtr opt_status(new OptionCustom(def, Option::V6));
  151. opt_status->writeInteger(code);
  152. if (!msg.empty()) {
  153. opt_status->writeString(msg, 1);
  154. }
  155. return (opt_status);
  156. }
  157. // Adds Status Code option to the IA.
  158. void addStatusCode(const uint16_t code, const std::string& msg,
  159. Option6IAPtr& opt_ia) {
  160. opt_ia->addOption(createStatusCode(code, msg));
  161. }
  162. // Test processing of the DHCPv6 Client FQDN Option.
  163. void testFqdn(const uint16_t msg_type,
  164. const bool use_oro,
  165. const uint8_t in_flags,
  166. const std::string& in_domain_name,
  167. const Option6ClientFqdn::DomainNameType in_domain_type,
  168. const uint8_t exp_flags,
  169. const std::string& exp_domain_name) {
  170. NakedDhcpv6Srv srv(0);
  171. Pkt6Ptr question = generatePktWithFqdn(msg_type,
  172. in_flags,
  173. in_domain_name,
  174. in_domain_type,
  175. use_oro);
  176. ASSERT_TRUE(getClientFqdnOption(question));
  177. Option6ClientFqdnPtr answ_fqdn;
  178. ASSERT_NO_THROW(answ_fqdn = srv.processClientFqdn(question));
  179. ASSERT_TRUE(answ_fqdn);
  180. const bool flag_n = (exp_flags & Option6ClientFqdn::FLAG_N) != 0;
  181. const bool flag_s = (exp_flags & Option6ClientFqdn::FLAG_S) != 0;
  182. const bool flag_o = (exp_flags & Option6ClientFqdn::FLAG_O) != 0;
  183. EXPECT_EQ(flag_n, answ_fqdn->getFlag(Option6ClientFqdn::FLAG_N));
  184. EXPECT_EQ(flag_s, answ_fqdn->getFlag(Option6ClientFqdn::FLAG_S));
  185. EXPECT_EQ(flag_o, answ_fqdn->getFlag(Option6ClientFqdn::FLAG_O));
  186. EXPECT_EQ(exp_domain_name, answ_fqdn->getDomainName());
  187. EXPECT_EQ(Option6ClientFqdn::FULL, answ_fqdn->getDomainNameType());
  188. }
  189. // Tests that the client message holding an FQDN is processed and the
  190. // lease is acquired.
  191. void testProcessMessage(const uint8_t msg_type,
  192. const std::string& hostname,
  193. NakedDhcpv6Srv& srv) {
  194. // Create a message of a specified type, add server id and
  195. // FQDN option.
  196. OptionPtr srvid = srv.getServerID();
  197. Pkt6Ptr req = generatePktWithFqdn(msg_type, Option6ClientFqdn::FLAG_S,
  198. hostname,
  199. Option6ClientFqdn::FULL,
  200. true, srvid);
  201. // For different client's message types we have to invoke different
  202. // functions to generate response.
  203. Pkt6Ptr reply;
  204. if (msg_type == DHCPV6_SOLICIT) {
  205. ASSERT_NO_THROW(reply = srv.processSolicit(req));
  206. } else if (msg_type == DHCPV6_REQUEST) {
  207. ASSERT_NO_THROW(reply = srv.processRequest(req));
  208. } else if (msg_type == DHCPV6_RENEW) {
  209. ASSERT_NO_THROW(reply = srv.processRequest(req));
  210. } else if (msg_type == DHCPV6_RELEASE) {
  211. // For Release no lease will be acquired so we have to leave
  212. // function here.
  213. ASSERT_NO_THROW(reply = srv.processRelease(req));
  214. return;
  215. } else {
  216. // We are not interested in testing other message types.
  217. return;
  218. }
  219. // For Solicit, we will get different message type obviously.
  220. if (msg_type == DHCPV6_SOLICIT) {
  221. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  222. } else {
  223. checkResponse(reply, DHCPV6_REPLY, 1234);
  224. }
  225. // Check verify that IA_NA is correct.
  226. Option6IAAddrPtr addr =
  227. checkIA_NA(reply, 234, subnet_->getT1(), subnet_->getT2());
  228. ASSERT_TRUE(addr);
  229. // Check that we have got the address we requested.
  230. checkIAAddr(addr, IOAddress("2001:db8:1:1::dead:beef"),
  231. subnet_->getPreferred(),
  232. subnet_->getValid());
  233. if (msg_type != DHCPV6_SOLICIT) {
  234. // Check that the lease exists.
  235. Lease6Ptr lease =
  236. checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  237. ASSERT_TRUE(lease);
  238. }
  239. }
  240. // Verify that NameChangeRequest holds valid values.
  241. void verifyNameChangeRequest(NakedDhcpv6Srv& srv,
  242. const isc::dhcp_ddns::NameChangeType type,
  243. const bool reverse, const bool forward,
  244. const std::string& addr,
  245. const std::string& dhcid,
  246. const uint16_t expires,
  247. const uint16_t len) {
  248. NameChangeRequest ncr = srv.name_change_reqs_.front();
  249. EXPECT_EQ(type, ncr.getChangeType());
  250. EXPECT_EQ(forward, ncr.isForwardChange());
  251. EXPECT_EQ(reverse, ncr.isReverseChange());
  252. EXPECT_EQ(addr, ncr.getIpAddress());
  253. EXPECT_EQ(dhcid, ncr.getDhcid().toStr());
  254. EXPECT_EQ(expires, ncr.getLeaseExpiresOn());
  255. EXPECT_EQ(len, ncr.getLeaseLength());
  256. EXPECT_EQ(isc::dhcp_ddns::ST_NEW, ncr.getStatus());
  257. srv.name_change_reqs_.pop();
  258. }
  259. // Holds a lease used by a test.
  260. Lease6Ptr lease_;
  261. };
  262. // This test verifies that incoming SOLICIT can be handled properly when
  263. // there are no subnets configured.
  264. //
  265. // This test sends a SOLICIT and the expected response
  266. // is an ADVERTISE with STATUS_NoAddrsAvail and no address provided in the
  267. // response
  268. TEST_F(NakedDhcpv6SrvTest, SolicitNoSubnet) {
  269. NakedDhcpv6Srv srv(0);
  270. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  271. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  272. sol->addOption(generateIA(234, 1500, 3000));
  273. OptionPtr clientid = generateClientId();
  274. sol->addOption(clientid);
  275. // Pass it to the server and get an advertise
  276. Pkt6Ptr reply = srv.processSolicit(sol);
  277. // check that we get the right NAK
  278. checkNakResponse (reply, DHCPV6_ADVERTISE, 1234, STATUS_NoAddrsAvail);
  279. }
  280. // This test verifies that incoming REQUEST can be handled properly when
  281. // there are no subnets configured.
  282. //
  283. // This test sends a REQUEST and the expected response
  284. // is an REPLY with STATUS_NoAddrsAvail and no address provided in the
  285. // response
  286. TEST_F(NakedDhcpv6SrvTest, RequestNoSubnet) {
  287. NakedDhcpv6Srv srv(0);
  288. // Let's create a REQUEST
  289. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  290. req->setRemoteAddr(IOAddress("fe80::abcd"));
  291. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  292. // with a hint
  293. IOAddress hint("2001:db8:1:1::dead:beef");
  294. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  295. ia->addOption(hint_opt);
  296. req->addOption(ia);
  297. OptionPtr clientid = generateClientId();
  298. req->addOption(clientid);
  299. // server-id is mandatory in REQUEST
  300. req->addOption(srv.getServerID());
  301. // Pass it to the server and hope for a REPLY
  302. Pkt6Ptr reply = srv.processRequest(req);
  303. // check that we get the right NAK
  304. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoAddrsAvail);
  305. }
  306. // This test verifies that incoming RENEW can be handled properly, even when
  307. // no subnets are configured.
  308. //
  309. // This test sends a RENEW and the expected response
  310. // is an REPLY with STATUS_NoBinding and no address provided in the
  311. // response
  312. TEST_F(NakedDhcpv6SrvTest, RenewNoSubnet) {
  313. NakedDhcpv6Srv srv(0);
  314. const IOAddress addr("2001:db8:1:1::cafe:babe");
  315. const uint32_t iaid = 234;
  316. // Generate client-id also duid_
  317. OptionPtr clientid = generateClientId();
  318. // Let's create a RENEW
  319. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  320. req->setRemoteAddr(IOAddress("fe80::abcd"));
  321. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  322. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  323. ia->addOption(renewed_addr_opt);
  324. req->addOption(ia);
  325. req->addOption(clientid);
  326. // Server-id is mandatory in RENEW
  327. req->addOption(srv.getServerID());
  328. // Pass it to the server and hope for a REPLY
  329. Pkt6Ptr reply = srv.processRenew(req);
  330. // check that we get the right NAK
  331. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding);
  332. }
  333. // This test verifies that incoming RELEASE can be handled properly, even when
  334. // no subnets are configured.
  335. //
  336. // This test sends a RELEASE and the expected response
  337. // is an REPLY with STATUS_NoBinding and no address provided in the
  338. // response
  339. TEST_F(NakedDhcpv6SrvTest, ReleaseNoSubnet) {
  340. NakedDhcpv6Srv srv(0);
  341. const IOAddress addr("2001:db8:1:1::cafe:babe");
  342. const uint32_t iaid = 234;
  343. // Generate client-id also duid_
  344. OptionPtr clientid = generateClientId();
  345. // Let's create a RELEASE
  346. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  347. req->setRemoteAddr(IOAddress("fe80::abcd"));
  348. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  349. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  350. ia->addOption(released_addr_opt);
  351. req->addOption(ia);
  352. req->addOption(clientid);
  353. // Server-id is mandatory in RELEASE
  354. req->addOption(srv.getServerID());
  355. // Pass it to the server and hope for a REPLY
  356. Pkt6Ptr reply = srv.processRelease(req);
  357. // check that we get the right NAK
  358. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding);
  359. }
  360. // Test verifies that the Dhcpv6_srv class can be instantiated. It checks a mode
  361. // without open sockets and with sockets opened on a high port (to not require
  362. // root privileges).
  363. TEST_F(Dhcpv6SrvTest, basic) {
  364. // srv has stubbed interface detection. It will read
  365. // interfaces.txt instead. It will pretend to have detected
  366. // fe80::1234 link-local address on eth0 interface. Obviously
  367. // an attempt to bind this socket will fail.
  368. boost::scoped_ptr<Dhcpv6Srv> srv;
  369. ASSERT_NO_THROW( {
  370. // Skip opening any sockets
  371. srv.reset(new Dhcpv6Srv(0));
  372. });
  373. srv.reset();
  374. ASSERT_NO_THROW({
  375. // open an unpriviledged port
  376. srv.reset(new Dhcpv6Srv(DHCP6_SERVER_PORT + 10000));
  377. });
  378. }
  379. // Test checks that DUID is generated properly
  380. TEST_F(Dhcpv6SrvTest, DUID) {
  381. boost::scoped_ptr<Dhcpv6Srv> srv;
  382. ASSERT_NO_THROW( {
  383. srv.reset(new NakedDhcpv6Srv(0));
  384. });
  385. OptionPtr srvid = srv->getServerID();
  386. ASSERT_TRUE(srvid);
  387. EXPECT_EQ(D6O_SERVERID, srvid->getType());
  388. OutputBuffer buf(32);
  389. srvid->pack(buf);
  390. // length of the actual DUID
  391. size_t len = buf.getLength() - srvid->getHeaderLen();
  392. InputBuffer data(buf.getData(), buf.getLength());
  393. // ignore first four bytes (standard DHCPv6 header)
  394. data.readUint32();
  395. uint16_t duid_type = data.readUint16();
  396. cout << "Duid-type=" << duid_type << endl;
  397. switch(duid_type) {
  398. case DUID::DUID_LLT: {
  399. // DUID must contain at least 6 bytes long MAC
  400. // + 8 bytes of fixed header
  401. EXPECT_GE(14, len);
  402. uint16_t hw_type = data.readUint16();
  403. // there's no real way to find out "correct"
  404. // hardware type
  405. EXPECT_GT(hw_type, 0);
  406. // check that timer is counted since 1.1.2000,
  407. // not from 1.1.1970.
  408. uint32_t seconds = data.readUint32();
  409. EXPECT_LE(seconds, DUID_TIME_EPOCH);
  410. // this test will start failing after 2030.
  411. // Hopefully we will be at BIND12 by then.
  412. // MAC must not be zeros
  413. vector<uint8_t> mac(len-8);
  414. vector<uint8_t> zeros(len-8, 0);
  415. data.readVector(mac, len-8);
  416. EXPECT_TRUE(mac != zeros);
  417. break;
  418. }
  419. case DUID::DUID_EN: {
  420. // there's not much we can check. Just simple
  421. // check if it is not all zeros
  422. vector<uint8_t> content(len-2);
  423. data.readVector(content, len-2);
  424. EXPECT_FALSE(isRangeZero(content.begin(), content.end()));
  425. break;
  426. }
  427. case DUID::DUID_LL: {
  428. // not supported yet
  429. cout << "Test not implemented for DUID-LL." << endl;
  430. // No failure here. There's really no way for test LL DUID. It doesn't
  431. // even make sense to check if that Link Layer is actually present on
  432. // a physical interface. RFC3315 says a server should write its DUID
  433. // and keep it despite hardware changes.
  434. break;
  435. }
  436. case DUID::DUID_UUID: // not supported yet
  437. default:
  438. ADD_FAILURE() << "Not supported duid type=" << duid_type << endl;
  439. break;
  440. }
  441. }
  442. // This test checks if Option Request Option (ORO) is parsed correctly
  443. // and the requested options are actually assigned.
  444. TEST_F(Dhcpv6SrvTest, advertiseOptions) {
  445. ConstElementPtr x;
  446. string config = "{ \"interfaces\": [ \"all\" ],"
  447. "\"preferred-lifetime\": 3000,"
  448. "\"rebind-timer\": 2000, "
  449. "\"renew-timer\": 1000, "
  450. "\"subnet6\": [ { "
  451. " \"pool\": [ \"2001:db8:1::/64\" ],"
  452. " \"subnet\": \"2001:db8:1::/48\", "
  453. " \"option-data\": [ {"
  454. " \"name\": \"dns-servers\","
  455. " \"space\": \"dhcp6\","
  456. " \"code\": 23,"
  457. " \"data\": \"2001:db8:1234:FFFF::1, 2001:db8:1234:FFFF::2\","
  458. " \"csv-format\": True"
  459. " },"
  460. " {"
  461. " \"name\": \"subscriber-id\","
  462. " \"space\": \"dhcp6\","
  463. " \"code\": 38,"
  464. " \"data\": \"1234\","
  465. " \"csv-format\": False"
  466. " } ]"
  467. " } ],"
  468. "\"valid-lifetime\": 4000 }";
  469. ElementPtr json = Element::fromJSON(config);
  470. NakedDhcpv6Srv srv(0);
  471. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json));
  472. ASSERT_TRUE(x);
  473. comment_ = parseAnswer(rcode_, x);
  474. ASSERT_EQ(0, rcode_);
  475. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  476. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  477. sol->addOption(generateIA(234, 1500, 3000));
  478. OptionPtr clientid = generateClientId();
  479. sol->addOption(clientid);
  480. // Pass it to the server and get an advertise
  481. Pkt6Ptr adv = srv.processSolicit(sol);
  482. // check if we get response at all
  483. ASSERT_TRUE(adv);
  484. // We have not requested any options so they should not
  485. // be included in the response.
  486. ASSERT_FALSE(adv->getOption(D6O_SUBSCRIBER_ID));
  487. ASSERT_FALSE(adv->getOption(D6O_NAME_SERVERS));
  488. // Let's now request some options. We expect that the server
  489. // will include them in its response.
  490. boost::shared_ptr<OptionIntArray<uint16_t> >
  491. option_oro(new OptionIntArray<uint16_t>(Option::V6, D6O_ORO));
  492. // Create vector with two option codes.
  493. std::vector<uint16_t> codes(2);
  494. codes[0] = D6O_SUBSCRIBER_ID;
  495. codes[1] = D6O_NAME_SERVERS;
  496. // Pass this code to option.
  497. option_oro->setValues(codes);
  498. // Append ORO to SOLICIT message.
  499. sol->addOption(option_oro);
  500. // Need to process SOLICIT again after requesting new option.
  501. adv = srv.processSolicit(sol);
  502. ASSERT_TRUE(adv);
  503. OptionPtr tmp = adv->getOption(D6O_NAME_SERVERS);
  504. ASSERT_TRUE(tmp);
  505. boost::shared_ptr<Option6AddrLst> reply_nameservers =
  506. boost::dynamic_pointer_cast<Option6AddrLst>(tmp);
  507. ASSERT_TRUE(reply_nameservers);
  508. Option6AddrLst::AddressContainer addrs = reply_nameservers->getAddresses();
  509. ASSERT_EQ(2, addrs.size());
  510. EXPECT_TRUE(addrs[0] == IOAddress("2001:db8:1234:FFFF::1"));
  511. EXPECT_TRUE(addrs[1] == IOAddress("2001:db8:1234:FFFF::2"));
  512. // There is a dummy option with code 1000 we requested from a server.
  513. // Expect that this option is in server's response.
  514. tmp = adv->getOption(D6O_SUBSCRIBER_ID);
  515. ASSERT_TRUE(tmp);
  516. // Check that the option contains valid data (from configuration).
  517. std::vector<uint8_t> data = tmp->getData();
  518. ASSERT_EQ(2, data.size());
  519. const uint8_t foo_expected[] = {
  520. 0x12, 0x34
  521. };
  522. EXPECT_EQ(0, memcmp(&data[0], foo_expected, 2));
  523. // more checks to be implemented
  524. }
  525. // There are no dedicated tests for Dhcpv6Srv::handleIA_NA and Dhcpv6Srv::assignLeases
  526. // as they are indirectly tested in Solicit and Request tests.
  527. // This test verifies that incoming SOLICIT can be handled properly, that an
  528. // ADVERTISE is generated, that the response has an address and that address
  529. // really belongs to the configured pool.
  530. //
  531. // This test sends a SOLICIT without any hint in IA_NA.
  532. //
  533. // constructed very simple SOLICIT message with:
  534. // - client-id option (mandatory)
  535. // - IA option (a request for address, without any addresses)
  536. //
  537. // expected returned ADVERTISE message:
  538. // - copy of client-id
  539. // - server-id
  540. // - IA that includes IAADDR
  541. TEST_F(Dhcpv6SrvTest, SolicitBasic) {
  542. NakedDhcpv6Srv srv(0);
  543. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  544. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  545. sol->addOption(generateIA(234, 1500, 3000));
  546. OptionPtr clientid = generateClientId();
  547. sol->addOption(clientid);
  548. // Pass it to the server and get an advertise
  549. Pkt6Ptr reply = srv.processSolicit(sol);
  550. // check if we get response at all
  551. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  552. // check that IA_NA was returned and that there's an address included
  553. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  554. subnet_->getT2());
  555. ASSERT_TRUE(addr);
  556. // Check that the assigned address is indeed from the configured pool
  557. checkIAAddr(addr, addr->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  558. // check DUIDs
  559. checkServerId(reply, srv.getServerID());
  560. checkClientId(reply, clientid);
  561. }
  562. // This test verifies that incoming SOLICIT can be handled properly, that an
  563. // ADVERTISE is generated, that the response has an address and that address
  564. // really belongs to the configured pool.
  565. //
  566. // This test sends a SOLICIT with IA_NA that contains a valid hint.
  567. //
  568. // constructed very simple SOLICIT message with:
  569. // - client-id option (mandatory)
  570. // - IA option (a request for address, with an address that belongs to the
  571. // configured pool, i.e. is valid as hint)
  572. //
  573. // expected returned ADVERTISE message:
  574. // - copy of client-id
  575. // - server-id
  576. // - IA that includes IAADDR
  577. TEST_F(Dhcpv6SrvTest, SolicitHint) {
  578. NakedDhcpv6Srv srv(0);
  579. // Let's create a SOLICIT
  580. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  581. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  582. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  583. // with a valid hint
  584. IOAddress hint("2001:db8:1:1::dead:beef");
  585. ASSERT_TRUE(subnet_->inPool(hint));
  586. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  587. ia->addOption(hint_opt);
  588. sol->addOption(ia);
  589. OptionPtr clientid = generateClientId();
  590. sol->addOption(clientid);
  591. // Pass it to the server and get an advertise
  592. Pkt6Ptr reply = srv.processSolicit(sol);
  593. // check if we get response at all
  594. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  595. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  596. ASSERT_TRUE(tmp);
  597. // check that IA_NA was returned and that there's an address included
  598. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  599. subnet_->getT2());
  600. ASSERT_TRUE(addr);
  601. // check that we've got the address we requested
  602. checkIAAddr(addr, hint, subnet_->getPreferred(), subnet_->getValid());
  603. // check DUIDs
  604. checkServerId(reply, srv.getServerID());
  605. checkClientId(reply, clientid);
  606. }
  607. // This test verifies that incoming SOLICIT can be handled properly, that an
  608. // ADVERTISE is generated, that the response has an address and that address
  609. // really belongs to the configured pool.
  610. //
  611. // This test sends a SOLICIT with IA_NA that contains an invalid hint.
  612. //
  613. // constructed very simple SOLICIT message with:
  614. // - client-id option (mandatory)
  615. // - IA option (a request for address, with an address that does not
  616. // belong to the configured pool, i.e. is valid as hint)
  617. //
  618. // expected returned ADVERTISE message:
  619. // - copy of client-id
  620. // - server-id
  621. // - IA that includes IAADDR
  622. TEST_F(Dhcpv6SrvTest, SolicitInvalidHint) {
  623. NakedDhcpv6Srv srv(0);
  624. // Let's create a SOLICIT
  625. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  626. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  627. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  628. IOAddress hint("2001:db8:1::cafe:babe");
  629. ASSERT_FALSE(subnet_->inPool(hint));
  630. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  631. ia->addOption(hint_opt);
  632. sol->addOption(ia);
  633. OptionPtr clientid = generateClientId();
  634. sol->addOption(clientid);
  635. // Pass it to the server and get an advertise
  636. Pkt6Ptr reply = srv.processSolicit(sol);
  637. // check if we get response at all
  638. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  639. // check that IA_NA was returned and that there's an address included
  640. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  641. subnet_->getT2());
  642. ASSERT_TRUE(addr);
  643. // Check that the assigned address is indeed from the configured pool
  644. checkIAAddr(addr, addr->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  645. EXPECT_TRUE(subnet_->inPool(addr->getAddress()));
  646. // check DUIDs
  647. checkServerId(reply, srv.getServerID());
  648. checkClientId(reply, clientid);
  649. }
  650. /// @todo: Add a test that client sends hint that is in pool, but currently
  651. /// being used by a different client.
  652. // This test checks that the server is offering different addresses to different
  653. // clients in ADVERTISEs. Please note that ADVERTISE is not a guarantee that such
  654. // an address will be assigned. Had the pool was very small and contained only
  655. // 2 addresses, the third client would get the same advertise as the first one
  656. // and this is a correct behavior. It is REQUEST that will fail for the third
  657. // client. ADVERTISE is basically saying "if you send me a request, you will
  658. // probably get an address like this" (there are no guarantees).
  659. TEST_F(Dhcpv6SrvTest, ManySolicits) {
  660. NakedDhcpv6Srv srv(0);
  661. Pkt6Ptr sol1 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  662. Pkt6Ptr sol2 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 2345));
  663. Pkt6Ptr sol3 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 3456));
  664. sol1->setRemoteAddr(IOAddress("fe80::abcd"));
  665. sol2->setRemoteAddr(IOAddress("fe80::1223"));
  666. sol3->setRemoteAddr(IOAddress("fe80::3467"));
  667. sol1->addOption(generateIA(1, 1500, 3000));
  668. sol2->addOption(generateIA(2, 1500, 3000));
  669. sol3->addOption(generateIA(3, 1500, 3000));
  670. // different client-id sizes
  671. OptionPtr clientid1 = generateClientId(12);
  672. OptionPtr clientid2 = generateClientId(14);
  673. OptionPtr clientid3 = generateClientId(16);
  674. sol1->addOption(clientid1);
  675. sol2->addOption(clientid2);
  676. sol3->addOption(clientid3);
  677. // Pass it to the server and get an advertise
  678. Pkt6Ptr reply1 = srv.processSolicit(sol1);
  679. Pkt6Ptr reply2 = srv.processSolicit(sol2);
  680. Pkt6Ptr reply3 = srv.processSolicit(sol3);
  681. // check if we get response at all
  682. checkResponse(reply1, DHCPV6_ADVERTISE, 1234);
  683. checkResponse(reply2, DHCPV6_ADVERTISE, 2345);
  684. checkResponse(reply3, DHCPV6_ADVERTISE, 3456);
  685. // check that IA_NA was returned and that there's an address included
  686. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  687. subnet_->getT2());
  688. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  689. subnet_->getT2());
  690. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  691. subnet_->getT2());
  692. ASSERT_TRUE(addr1);
  693. ASSERT_TRUE(addr2);
  694. ASSERT_TRUE(addr3);
  695. // Check that the assigned address is indeed from the configured pool
  696. checkIAAddr(addr1, addr1->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  697. checkIAAddr(addr2, addr2->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  698. checkIAAddr(addr3, addr3->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  699. // check DUIDs
  700. checkServerId(reply1, srv.getServerID());
  701. checkServerId(reply2, srv.getServerID());
  702. checkServerId(reply3, srv.getServerID());
  703. checkClientId(reply1, clientid1);
  704. checkClientId(reply2, clientid2);
  705. checkClientId(reply3, clientid3);
  706. // Finally check that the addresses offered are different
  707. EXPECT_NE(addr1->getAddress().toText(), addr2->getAddress().toText());
  708. EXPECT_NE(addr2->getAddress().toText(), addr3->getAddress().toText());
  709. EXPECT_NE(addr3->getAddress().toText(), addr1->getAddress().toText());
  710. cout << "Offered address to client1=" << addr1->getAddress().toText() << endl;
  711. cout << "Offered address to client2=" << addr2->getAddress().toText() << endl;
  712. cout << "Offered address to client3=" << addr3->getAddress().toText() << endl;
  713. }
  714. // This test verifies that incoming REQUEST can be handled properly, that a
  715. // REPLY is generated, that the response has an address and that address
  716. // really belongs to the configured pool.
  717. //
  718. // This test sends a REQUEST with IA_NA that contains a valid hint.
  719. //
  720. // constructed very simple REQUEST message with:
  721. // - client-id option (mandatory)
  722. // - IA option (a request for address, with an address that belongs to the
  723. // configured pool, i.e. is valid as hint)
  724. //
  725. // expected returned REPLY message:
  726. // - copy of client-id
  727. // - server-id
  728. // - IA that includes IAADDR
  729. TEST_F(Dhcpv6SrvTest, RequestBasic) {
  730. NakedDhcpv6Srv srv(0);
  731. // Let's create a REQUEST
  732. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  733. req->setRemoteAddr(IOAddress("fe80::abcd"));
  734. boost::shared_ptr<Option6IA> ia = generateIA(234, 1500, 3000);
  735. // with a valid hint
  736. IOAddress hint("2001:db8:1:1::dead:beef");
  737. ASSERT_TRUE(subnet_->inPool(hint));
  738. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  739. ia->addOption(hint_opt);
  740. req->addOption(ia);
  741. OptionPtr clientid = generateClientId();
  742. req->addOption(clientid);
  743. // server-id is mandatory in REQUEST
  744. req->addOption(srv.getServerID());
  745. // Pass it to the server and hope for a REPLY
  746. Pkt6Ptr reply = srv.processRequest(req);
  747. // check if we get response at all
  748. checkResponse(reply, DHCPV6_REPLY, 1234);
  749. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  750. ASSERT_TRUE(tmp);
  751. // check that IA_NA was returned and that there's an address included
  752. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  753. subnet_->getT2());
  754. ASSERT_TRUE(addr);
  755. // check that we've got the address we requested
  756. checkIAAddr(addr, hint, subnet_->getPreferred(), subnet_->getValid());
  757. // check DUIDs
  758. checkServerId(reply, srv.getServerID());
  759. checkClientId(reply, clientid);
  760. // check that the lease is really in the database
  761. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  762. EXPECT_TRUE(l);
  763. LeaseMgrFactory::instance().deleteLease(addr->getAddress());
  764. }
  765. // This test checks that the server is offering different addresses to different
  766. // clients in REQUEST. Please note that ADVERTISE is not a guarantee that such
  767. // and address will be assigned. Had the pool was very small and contained only
  768. // 2 addresses, the third client would get the same advertise as the first one
  769. // and this is a correct behavior. It is REQUEST that will fail for the third
  770. // client. ADVERTISE is basically saying "if you send me a request, you will
  771. // probably get an address like this" (there are no guarantees).
  772. TEST_F(Dhcpv6SrvTest, ManyRequests) {
  773. NakedDhcpv6Srv srv(0);
  774. ASSERT_TRUE(subnet_);
  775. Pkt6Ptr req1 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  776. Pkt6Ptr req2 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 2345));
  777. Pkt6Ptr req3 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 3456));
  778. req1->setRemoteAddr(IOAddress("fe80::abcd"));
  779. req2->setRemoteAddr(IOAddress("fe80::1223"));
  780. req3->setRemoteAddr(IOAddress("fe80::3467"));
  781. req1->addOption(generateIA(1, 1500, 3000));
  782. req2->addOption(generateIA(2, 1500, 3000));
  783. req3->addOption(generateIA(3, 1500, 3000));
  784. // different client-id sizes
  785. OptionPtr clientid1 = generateClientId(12);
  786. OptionPtr clientid2 = generateClientId(14);
  787. OptionPtr clientid3 = generateClientId(16);
  788. req1->addOption(clientid1);
  789. req2->addOption(clientid2);
  790. req3->addOption(clientid3);
  791. // server-id is mandatory in REQUEST
  792. req1->addOption(srv.getServerID());
  793. req2->addOption(srv.getServerID());
  794. req3->addOption(srv.getServerID());
  795. // Pass it to the server and get an advertise
  796. Pkt6Ptr reply1 = srv.processRequest(req1);
  797. Pkt6Ptr reply2 = srv.processRequest(req2);
  798. Pkt6Ptr reply3 = srv.processRequest(req3);
  799. // check if we get response at all
  800. checkResponse(reply1, DHCPV6_REPLY, 1234);
  801. checkResponse(reply2, DHCPV6_REPLY, 2345);
  802. checkResponse(reply3, DHCPV6_REPLY, 3456);
  803. // check that IA_NA was returned and that there's an address included
  804. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  805. subnet_->getT2());
  806. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  807. subnet_->getT2());
  808. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  809. subnet_->getT2());
  810. ASSERT_TRUE(addr1);
  811. ASSERT_TRUE(addr2);
  812. ASSERT_TRUE(addr3);
  813. // Check that the assigned address is indeed from the configured pool
  814. checkIAAddr(addr1, addr1->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  815. checkIAAddr(addr2, addr2->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  816. checkIAAddr(addr3, addr3->getAddress(), subnet_->getPreferred(), subnet_->getValid());
  817. // check DUIDs
  818. checkServerId(reply1, srv.getServerID());
  819. checkServerId(reply2, srv.getServerID());
  820. checkServerId(reply3, srv.getServerID());
  821. checkClientId(reply1, clientid1);
  822. checkClientId(reply2, clientid2);
  823. checkClientId(reply3, clientid3);
  824. // Finally check that the addresses offered are different
  825. EXPECT_NE(addr1->getAddress().toText(), addr2->getAddress().toText());
  826. EXPECT_NE(addr2->getAddress().toText(), addr3->getAddress().toText());
  827. EXPECT_NE(addr3->getAddress().toText(), addr1->getAddress().toText());
  828. cout << "Assigned address to client1=" << addr1->getAddress().toText() << endl;
  829. cout << "Assigned address to client2=" << addr2->getAddress().toText() << endl;
  830. cout << "Assigned address to client3=" << addr3->getAddress().toText() << endl;
  831. }
  832. // This test verifies that incoming (positive) RENEW can be handled properly, that a
  833. // REPLY is generated, that the response has an address and that address
  834. // really belongs to the configured pool and that lease is actually renewed.
  835. //
  836. // expected:
  837. // - returned REPLY message has copy of client-id
  838. // - returned REPLY message has server-id
  839. // - returned REPLY message has IA that includes IAADDR
  840. // - lease is actually renewed in LeaseMgr
  841. TEST_F(Dhcpv6SrvTest, RenewBasic) {
  842. NakedDhcpv6Srv srv(0);
  843. const IOAddress addr("2001:db8:1:1::cafe:babe");
  844. const uint32_t iaid = 234;
  845. // Generate client-id also duid_
  846. OptionPtr clientid = generateClientId();
  847. // Check that the address we are about to use is indeed in pool
  848. ASSERT_TRUE(subnet_->inPool(addr));
  849. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  850. // value on purpose. They should be updated during RENEW.
  851. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid_, iaid,
  852. 501, 502, 503, 504, subnet_->getID(), 0));
  853. lease->cltt_ = 1234;
  854. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  855. // Check that the lease is really in the database
  856. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(addr);
  857. ASSERT_TRUE(l);
  858. // Check that T1, T2, preferred, valid and cltt really set and not using
  859. // previous (500, 501, etc.) values
  860. EXPECT_NE(l->t1_, subnet_->getT1());
  861. EXPECT_NE(l->t2_, subnet_->getT2());
  862. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  863. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  864. EXPECT_NE(l->cltt_, time(NULL));
  865. // Let's create a RENEW
  866. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  867. req->setRemoteAddr(IOAddress("fe80::abcd"));
  868. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  869. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  870. ia->addOption(renewed_addr_opt);
  871. req->addOption(ia);
  872. req->addOption(clientid);
  873. // Server-id is mandatory in RENEW
  874. req->addOption(srv.getServerID());
  875. // Pass it to the server and hope for a REPLY
  876. Pkt6Ptr reply = srv.processRenew(req);
  877. // Check if we get response at all
  878. checkResponse(reply, DHCPV6_REPLY, 1234);
  879. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  880. ASSERT_TRUE(tmp);
  881. // Check that IA_NA was returned and that there's an address included
  882. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  883. subnet_->getT2());
  884. ASSERT_TRUE(addr_opt);
  885. // Check that we've got the address we requested
  886. checkIAAddr(addr_opt, addr, subnet_->getPreferred(), subnet_->getValid());
  887. // Check DUIDs
  888. checkServerId(reply, srv.getServerID());
  889. checkClientId(reply, clientid);
  890. // Check that the lease is really in the database
  891. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  892. ASSERT_TRUE(l);
  893. // Check that T1, T2, preferred, valid and cltt were really updated
  894. EXPECT_EQ(l->t1_, subnet_->getT1());
  895. EXPECT_EQ(l->t2_, subnet_->getT2());
  896. EXPECT_EQ(l->preferred_lft_, subnet_->getPreferred());
  897. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  898. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  899. int32_t cltt = static_cast<int32_t>(l->cltt_);
  900. int32_t expected = static_cast<int32_t>(time(NULL));
  901. // equality or difference by 1 between cltt and expected is ok.
  902. EXPECT_GE(1, abs(cltt - expected));
  903. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
  904. }
  905. // This test verifies that incoming (invalid) RENEW can be handled properly.
  906. //
  907. // This test checks 3 scenarios:
  908. // 1. there is no such lease at all
  909. // 2. there is such a lease, but it is assigned to a different IAID
  910. // 3. there is such a lease, but it belongs to a different client
  911. //
  912. // expected:
  913. // - returned REPLY message has copy of client-id
  914. // - returned REPLY message has server-id
  915. // - returned REPLY message has IA that includes STATUS-CODE
  916. // - No lease in LeaseMgr
  917. TEST_F(Dhcpv6SrvTest, RenewReject) {
  918. NakedDhcpv6Srv srv(0);
  919. const IOAddress addr("2001:db8:1:1::dead");
  920. const uint32_t transid = 1234;
  921. const uint32_t valid_iaid = 234;
  922. const uint32_t bogus_iaid = 456;
  923. // Quick sanity check that the address we're about to use is ok
  924. ASSERT_TRUE(subnet_->inPool(addr));
  925. // GenerateClientId() also sets duid_
  926. OptionPtr clientid = generateClientId();
  927. // Check that the lease is NOT in the database
  928. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(addr);
  929. ASSERT_FALSE(l);
  930. // Let's create a RENEW
  931. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, transid));
  932. req->setRemoteAddr(IOAddress("fe80::abcd"));
  933. boost::shared_ptr<Option6IA> ia = generateIA(bogus_iaid, 1500, 3000);
  934. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  935. ia->addOption(renewed_addr_opt);
  936. req->addOption(ia);
  937. req->addOption(clientid);
  938. // Server-id is mandatory in RENEW
  939. req->addOption(srv.getServerID());
  940. // Case 1: No lease known to server
  941. // Pass it to the server and hope for a REPLY
  942. Pkt6Ptr reply = srv.processRenew(req);
  943. // Check if we get response at all
  944. checkResponse(reply, DHCPV6_REPLY, transid);
  945. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  946. ASSERT_TRUE(tmp);
  947. // Check that IA_NA was returned and that there's an address included
  948. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  949. ASSERT_TRUE(ia);
  950. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  951. // Check that there is no lease added
  952. l = LeaseMgrFactory::instance().getLease6(addr);
  953. ASSERT_FALSE(l);
  954. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  955. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  956. // value on purpose. They should be updated during RENEW.
  957. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid_, valid_iaid,
  958. 501, 502, 503, 504, subnet_->getID(), 0));
  959. lease->cltt_ = 123; // Let's use it as an indicator that the lease
  960. // was NOT updated.
  961. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  962. // Pass it to the server and hope for a REPLY
  963. reply = srv.processRenew(req);
  964. checkResponse(reply, DHCPV6_REPLY, transid);
  965. tmp = reply->getOption(D6O_IA_NA);
  966. ASSERT_TRUE(tmp);
  967. // Check that IA_NA was returned and that there's an address included
  968. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  969. ASSERT_TRUE(ia);
  970. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  971. // There is a iaid mis-match, so server should respond that there is
  972. // no such address to renew.
  973. // CASE 3: Lease belongs to a client with different client-id
  974. req->delOption(D6O_CLIENTID);
  975. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  976. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  977. req->addOption(generateClientId(13)); // generate different DUID
  978. // (with length 13)
  979. reply = srv.processRenew(req);
  980. checkResponse(reply, DHCPV6_REPLY, transid);
  981. tmp = reply->getOption(D6O_IA_NA);
  982. ASSERT_TRUE(tmp);
  983. // Check that IA_NA was returned and that there's an address included
  984. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  985. ASSERT_TRUE(ia);
  986. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  987. lease = LeaseMgrFactory::instance().getLease6(addr);
  988. ASSERT_TRUE(lease);
  989. // Verify that the lease was not updated.
  990. EXPECT_EQ(123, lease->cltt_);
  991. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  992. }
  993. // This test verifies that incoming (positive) RELEASE can be handled properly,
  994. // that a REPLY is generated, that the response has status code and that the
  995. // lease is indeed removed from the database.
  996. //
  997. // expected:
  998. // - returned REPLY message has copy of client-id
  999. // - returned REPLY message has server-id
  1000. // - returned REPLY message has IA that does not include an IAADDR
  1001. // - lease is actually removed from LeaseMgr
  1002. TEST_F(Dhcpv6SrvTest, ReleaseBasic) {
  1003. NakedDhcpv6Srv srv(0);
  1004. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1005. const uint32_t iaid = 234;
  1006. // Generate client-id also duid_
  1007. OptionPtr clientid = generateClientId();
  1008. // Check that the address we are about to use is indeed in pool
  1009. ASSERT_TRUE(subnet_->inPool(addr));
  1010. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1011. // value on purpose. They should be updated during RENEW.
  1012. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid_, iaid,
  1013. 501, 502, 503, 504, subnet_->getID(), 0));
  1014. lease->cltt_ = 1234;
  1015. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1016. // Check that the lease is really in the database
  1017. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(addr);
  1018. ASSERT_TRUE(l);
  1019. // Let's create a RELEASE
  1020. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  1021. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1022. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  1023. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1024. ia->addOption(released_addr_opt);
  1025. req->addOption(ia);
  1026. req->addOption(clientid);
  1027. // Server-id is mandatory in RELEASE
  1028. req->addOption(srv.getServerID());
  1029. // Pass it to the server and hope for a REPLY
  1030. Pkt6Ptr reply = srv.processRelease(req);
  1031. // Check if we get response at all
  1032. checkResponse(reply, DHCPV6_REPLY, 1234);
  1033. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1034. ASSERT_TRUE(tmp);
  1035. // Check that IA_NA was returned and that there's an address included
  1036. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1037. checkIA_NAStatusCode(ia, STATUS_Success);
  1038. checkMsgStatusCode(reply, STATUS_Success);
  1039. // There should be no address returned in RELEASE (see RFC3315, 18.2.6)
  1040. EXPECT_FALSE(tmp->getOption(D6O_IAADDR));
  1041. // Check DUIDs
  1042. checkServerId(reply, srv.getServerID());
  1043. checkClientId(reply, clientid);
  1044. // Check that the lease is really gone in the database
  1045. // get lease by address
  1046. l = LeaseMgrFactory::instance().getLease6(addr);
  1047. ASSERT_FALSE(l);
  1048. // get lease by subnetid/duid/iaid combination
  1049. l = LeaseMgrFactory::instance().getLease6(*duid_, iaid, subnet_->getID());
  1050. ASSERT_FALSE(l);
  1051. }
  1052. // This test verifies that incoming (invalid) RELEASE can be handled properly.
  1053. //
  1054. // This test checks 3 scenarios:
  1055. // 1. there is no such lease at all
  1056. // 2. there is such a lease, but it is assigned to a different IAID
  1057. // 3. there is such a lease, but it belongs to a different client
  1058. //
  1059. // expected:
  1060. // - returned REPLY message has copy of client-id
  1061. // - returned REPLY message has server-id
  1062. // - returned REPLY message has IA that includes STATUS-CODE
  1063. // - No lease in LeaseMgr
  1064. TEST_F(Dhcpv6SrvTest, ReleaseReject) {
  1065. NakedDhcpv6Srv srv(0);
  1066. const IOAddress addr("2001:db8:1:1::dead");
  1067. const uint32_t transid = 1234;
  1068. const uint32_t valid_iaid = 234;
  1069. const uint32_t bogus_iaid = 456;
  1070. // Quick sanity check that the address we're about to use is ok
  1071. ASSERT_TRUE(subnet_->inPool(addr));
  1072. // GenerateClientId() also sets duid_
  1073. OptionPtr clientid = generateClientId();
  1074. // Check that the lease is NOT in the database
  1075. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(addr);
  1076. ASSERT_FALSE(l);
  1077. // Let's create a RELEASE
  1078. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, transid));
  1079. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1080. boost::shared_ptr<Option6IA> ia = generateIA(bogus_iaid, 1500, 3000);
  1081. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1082. ia->addOption(released_addr_opt);
  1083. req->addOption(ia);
  1084. req->addOption(clientid);
  1085. // Server-id is mandatory in RENEW
  1086. req->addOption(srv.getServerID());
  1087. // Case 1: No lease known to server
  1088. SCOPED_TRACE("CASE 1: No lease known to server");
  1089. // Pass it to the server and hope for a REPLY
  1090. Pkt6Ptr reply = srv.processRelease(req);
  1091. // Check if we get response at all
  1092. checkResponse(reply, DHCPV6_REPLY, transid);
  1093. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1094. ASSERT_TRUE(tmp);
  1095. // Check that IA_NA was returned and that there's an address included
  1096. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1097. ASSERT_TRUE(ia);
  1098. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1099. checkMsgStatusCode(reply, STATUS_NoBinding);
  1100. // Check that the lease is not there
  1101. l = LeaseMgrFactory::instance().getLease6(addr);
  1102. ASSERT_FALSE(l);
  1103. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  1104. SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but to a different IAID");
  1105. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid_, valid_iaid,
  1106. 501, 502, 503, 504, subnet_->getID(), 0));
  1107. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1108. // Pass it to the server and hope for a REPLY
  1109. reply = srv.processRelease(req);
  1110. checkResponse(reply, DHCPV6_REPLY, transid);
  1111. tmp = reply->getOption(D6O_IA_NA);
  1112. ASSERT_TRUE(tmp);
  1113. // Check that IA_NA was returned and that there's an address included
  1114. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1115. ASSERT_TRUE(ia);
  1116. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1117. checkMsgStatusCode(reply, STATUS_NoBinding);
  1118. // Check that the lease is still there
  1119. l = LeaseMgrFactory::instance().getLease6(addr);
  1120. ASSERT_TRUE(l);
  1121. // CASE 3: Lease belongs to a client with different client-id
  1122. SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
  1123. req->delOption(D6O_CLIENTID);
  1124. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  1125. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  1126. req->addOption(generateClientId(13)); // generate different DUID
  1127. // (with length 13)
  1128. reply = srv.processRelease(req);
  1129. checkResponse(reply, DHCPV6_REPLY, transid);
  1130. tmp = reply->getOption(D6O_IA_NA);
  1131. ASSERT_TRUE(tmp);
  1132. // Check that IA_NA was returned and that there's an address included
  1133. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1134. ASSERT_TRUE(ia);
  1135. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1136. checkMsgStatusCode(reply, STATUS_NoBinding);
  1137. // Check that the lease is still there
  1138. l = LeaseMgrFactory::instance().getLease6(addr);
  1139. ASSERT_TRUE(l);
  1140. // Finally, let's cleanup the database
  1141. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  1142. }
  1143. // This test verifies if the status code option is generated properly.
  1144. TEST_F(Dhcpv6SrvTest, StatusCode) {
  1145. NakedDhcpv6Srv srv(0);
  1146. // a dummy content for client-id
  1147. uint8_t expected[] = {
  1148. 0x0, 0xD, // option code = 13
  1149. 0x0, 0x7, // option length = 7
  1150. 0x0, 0x3, // status code = 3
  1151. 0x41, 0x42, 0x43, 0x44, 0x45 // string value ABCDE
  1152. };
  1153. // Create the option.
  1154. OptionPtr status = srv.createStatusCode(3, "ABCDE");
  1155. // Allocate an output buffer. We will store the option
  1156. // in wire format here.
  1157. OutputBuffer buf(sizeof(expected));
  1158. // Prepare the wire format.
  1159. ASSERT_NO_THROW(status->pack(buf));
  1160. // Check that the option buffer has valid length (option header + data).
  1161. ASSERT_EQ(sizeof(expected), buf.getLength());
  1162. // Verify the contents of the option.
  1163. EXPECT_EQ(0, memcmp(expected, buf.getData(), sizeof(expected)));
  1164. }
  1165. // This test verifies if the sanityCheck() really checks options presence.
  1166. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  1167. NakedDhcpv6Srv srv(0);
  1168. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1169. // Set link-local sender address, so appropriate subnet can be
  1170. // selected for this packet.
  1171. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1172. // client-id is optional for information-request, so
  1173. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  1174. // empty packet, no client-id, no server-id
  1175. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  1176. RFCViolation);
  1177. // This doesn't make much sense, but let's check it for completeness
  1178. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  1179. OptionPtr clientid = generateClientId();
  1180. pkt->addOption(clientid);
  1181. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  1182. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  1183. pkt->addOption(srv.getServerID());
  1184. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  1185. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  1186. // sane section ends here, let's do some negative tests as well
  1187. pkt->addOption(clientid);
  1188. pkt->addOption(clientid);
  1189. // with more than one client-id it should throw, no matter what
  1190. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  1191. RFCViolation);
  1192. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  1193. RFCViolation);
  1194. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  1195. RFCViolation);
  1196. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  1197. RFCViolation);
  1198. pkt->delOption(D6O_CLIENTID);
  1199. pkt->delOption(D6O_CLIENTID);
  1200. // again we have only one client-id
  1201. // let's try different type of insanity - several server-ids
  1202. pkt->addOption(srv.getServerID());
  1203. pkt->addOption(srv.getServerID());
  1204. // with more than one server-id it should throw, no matter what
  1205. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  1206. RFCViolation);
  1207. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  1208. RFCViolation);
  1209. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  1210. RFCViolation);
  1211. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  1212. RFCViolation);
  1213. }
  1214. // This test verifies if selectSubnet() selects proper subnet for a given
  1215. // source address.
  1216. TEST_F(Dhcpv6SrvTest, selectSubnetAddr) {
  1217. NakedDhcpv6Srv srv(0);
  1218. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1219. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1220. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1221. // CASE 1: We have only one subnet defined and we received local traffic.
  1222. // The only available subnet should be selected
  1223. CfgMgr::instance().deleteSubnets6();
  1224. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1225. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1226. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1227. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1228. EXPECT_EQ(selected, subnet1);
  1229. // CASE 2: We have only one subnet defined and we received relayed traffic.
  1230. // We should NOT select it.
  1231. // Identical steps as in case 1, but repeated for clarity
  1232. CfgMgr::instance().deleteSubnets6();
  1233. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1234. pkt->setRemoteAddr(IOAddress("2001:db8:abcd::2345"));
  1235. selected = srv.selectSubnet(pkt);
  1236. EXPECT_FALSE(selected);
  1237. // CASE 3: We have three subnets defined and we received local traffic.
  1238. // Nothing should be selected.
  1239. CfgMgr::instance().deleteSubnets6();
  1240. CfgMgr::instance().addSubnet6(subnet1);
  1241. CfgMgr::instance().addSubnet6(subnet2);
  1242. CfgMgr::instance().addSubnet6(subnet3);
  1243. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1244. selected = srv.selectSubnet(pkt);
  1245. EXPECT_FALSE(selected);
  1246. // CASE 4: We have three subnets defined and we received relayed traffic
  1247. // that came out of subnet 2. We should select subnet2 then
  1248. CfgMgr::instance().deleteSubnets6();
  1249. CfgMgr::instance().addSubnet6(subnet1);
  1250. CfgMgr::instance().addSubnet6(subnet2);
  1251. CfgMgr::instance().addSubnet6(subnet3);
  1252. pkt->setRemoteAddr(IOAddress("2001:db8:2::baca"));
  1253. selected = srv.selectSubnet(pkt);
  1254. EXPECT_EQ(selected, subnet2);
  1255. // CASE 5: We have three subnets defined and we received relayed traffic
  1256. // that came out of undefined subnet. We should select nothing
  1257. CfgMgr::instance().deleteSubnets6();
  1258. CfgMgr::instance().addSubnet6(subnet1);
  1259. CfgMgr::instance().addSubnet6(subnet2);
  1260. CfgMgr::instance().addSubnet6(subnet3);
  1261. pkt->setRemoteAddr(IOAddress("2001:db8:4::baca"));
  1262. selected = srv.selectSubnet(pkt);
  1263. EXPECT_FALSE(selected);
  1264. }
  1265. // This test verifies if selectSubnet() selects proper subnet for a given
  1266. // network interface name.
  1267. TEST_F(Dhcpv6SrvTest, selectSubnetIface) {
  1268. NakedDhcpv6Srv srv(0);
  1269. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1270. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1271. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1272. subnet1->setIface("eth0");
  1273. subnet3->setIface("wifi1");
  1274. // CASE 1: We have only one subnet defined and it is available via eth0.
  1275. // Packet came from eth0. The only available subnet should be selected
  1276. CfgMgr::instance().deleteSubnets6();
  1277. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1278. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1279. pkt->setIface("eth0");
  1280. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1281. EXPECT_EQ(selected, subnet1);
  1282. // CASE 2: We have only one subnet defined and it is available via eth0.
  1283. // Packet came from eth1. We should not select it
  1284. CfgMgr::instance().deleteSubnets6();
  1285. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1286. pkt->setIface("eth1");
  1287. selected = srv.selectSubnet(pkt);
  1288. EXPECT_FALSE(selected);
  1289. // CASE 3: We have only 3 subnets defined, one over eth0, one remote and
  1290. // one over wifi1.
  1291. // Packet came from eth1. We should not select it
  1292. CfgMgr::instance().deleteSubnets6();
  1293. CfgMgr::instance().addSubnet6(subnet1);
  1294. CfgMgr::instance().addSubnet6(subnet2);
  1295. CfgMgr::instance().addSubnet6(subnet3);
  1296. pkt->setIface("eth0");
  1297. EXPECT_EQ(subnet1, srv.selectSubnet(pkt));
  1298. pkt->setIface("eth3"); // no such interface
  1299. EXPECT_EQ(Subnet6Ptr(), srv.selectSubnet(pkt)); // nothing selected
  1300. pkt->setIface("wifi1");
  1301. EXPECT_EQ(subnet3, srv.selectSubnet(pkt));
  1302. }
  1303. // This test verifies if selectSubnet() selects proper subnet for a given
  1304. // linkaddr in RELAY-FORW message
  1305. TEST_F(Dhcpv6SrvTest, selectSubnetRelayLinkaddr) {
  1306. NakedDhcpv6Srv srv(0);
  1307. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1308. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1309. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1310. Pkt6::RelayInfo relay;
  1311. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1312. relay.peeraddr_ = IOAddress("fe80::1");
  1313. // CASE 1: We have only one subnet defined and we received relayed traffic.
  1314. // The only available subnet should NOT be selected.
  1315. CfgMgr::instance().deleteSubnets6();
  1316. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1317. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1318. pkt->relay_info_.push_back(relay);
  1319. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1320. EXPECT_FALSE(selected);
  1321. // CASE 2: We have three subnets defined and we received relayed traffic.
  1322. // Nothing should be selected.
  1323. CfgMgr::instance().deleteSubnets6();
  1324. CfgMgr::instance().addSubnet6(subnet1);
  1325. CfgMgr::instance().addSubnet6(subnet2);
  1326. CfgMgr::instance().addSubnet6(subnet3);
  1327. selected = srv.selectSubnet(pkt);
  1328. EXPECT_EQ(selected, subnet2);
  1329. // CASE 3: We have three subnets defined and we received relayed traffic
  1330. // that came out of subnet 2. We should select subnet2 then
  1331. CfgMgr::instance().deleteSubnets6();
  1332. CfgMgr::instance().addSubnet6(subnet1);
  1333. CfgMgr::instance().addSubnet6(subnet2);
  1334. CfgMgr::instance().addSubnet6(subnet3);
  1335. // Source of the packet should have no meaning. Selection is based
  1336. // on linkaddr field in the relay
  1337. pkt->setRemoteAddr(IOAddress("2001:db8:1::baca"));
  1338. selected = srv.selectSubnet(pkt);
  1339. EXPECT_EQ(selected, subnet2);
  1340. // CASE 4: We have three subnets defined and we received relayed traffic
  1341. // that came out of undefined subnet. We should select nothing
  1342. CfgMgr::instance().deleteSubnets6();
  1343. CfgMgr::instance().addSubnet6(subnet1);
  1344. CfgMgr::instance().addSubnet6(subnet2);
  1345. CfgMgr::instance().addSubnet6(subnet3);
  1346. pkt->relay_info_.clear();
  1347. relay.linkaddr_ = IOAddress("2001:db8:4::1234");
  1348. pkt->relay_info_.push_back(relay);
  1349. selected = srv.selectSubnet(pkt);
  1350. EXPECT_FALSE(selected);
  1351. }
  1352. // This test verifies if selectSubnet() selects proper subnet for a given
  1353. // interface-id option
  1354. TEST_F(Dhcpv6SrvTest, selectSubnetRelayInterfaceId) {
  1355. NakedDhcpv6Srv srv(0);
  1356. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1357. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1358. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1359. subnet1->setInterfaceId(generateInterfaceId("relay1"));
  1360. subnet2->setInterfaceId(generateInterfaceId("relay2"));
  1361. // CASE 1: We have only one subnet defined and it is for interface-id "relay1"
  1362. // Packet came with interface-id "relay2". We should not select subnet1
  1363. CfgMgr::instance().deleteSubnets6();
  1364. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1365. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1366. Pkt6::RelayInfo relay;
  1367. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1368. relay.peeraddr_ = IOAddress("fe80::1");
  1369. OptionPtr opt = generateInterfaceId("relay2");
  1370. relay.options_.insert(make_pair(opt->getType(), opt));
  1371. pkt->relay_info_.push_back(relay);
  1372. // There is only one subnet configured and we are outside of that subnet
  1373. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1374. EXPECT_FALSE(selected);
  1375. // CASE 2: We have only one subnet defined and it is for interface-id "relay2"
  1376. // Packet came with interface-id "relay2". We should select it
  1377. CfgMgr::instance().deleteSubnets6();
  1378. CfgMgr::instance().addSubnet6(subnet2); // just a single subnet
  1379. selected = srv.selectSubnet(pkt);
  1380. EXPECT_EQ(selected, subnet2);
  1381. // CASE 3: We have only 3 subnets defined: one remote for interface-id "relay1",
  1382. // one remote for interface-id "relay2" and third local
  1383. // packet comes with interface-id "relay2". We should select subnet2
  1384. CfgMgr::instance().deleteSubnets6();
  1385. CfgMgr::instance().addSubnet6(subnet1);
  1386. CfgMgr::instance().addSubnet6(subnet2);
  1387. CfgMgr::instance().addSubnet6(subnet3);
  1388. EXPECT_EQ(subnet2, srv.selectSubnet(pkt));
  1389. }
  1390. // This test verifies if the server-id disk operations (read, write) are
  1391. // working properly.
  1392. TEST_F(Dhcpv6SrvTest, ServerID) {
  1393. NakedDhcpv6Srv srv(0);
  1394. string duid1_text = "01:ff:02:03:06:80:90:ab:cd:ef";
  1395. uint8_t duid1[] = { 0x01, 0xff, 2, 3, 6, 0x80, 0x90, 0xab, 0xcd, 0xef };
  1396. OptionBuffer expected_duid1(duid1, duid1 + sizeof(duid1));
  1397. fstream file1(DUID_FILE, ios::out | ios::trunc);
  1398. file1 << duid1_text;
  1399. file1.close();
  1400. // Test reading from a file
  1401. EXPECT_TRUE(srv.loadServerID(DUID_FILE));
  1402. ASSERT_TRUE(srv.getServerID());
  1403. ASSERT_EQ(sizeof(duid1) + Option::OPTION6_HDR_LEN, srv.getServerID()->len());
  1404. ASSERT_TRUE(expected_duid1 == srv.getServerID()->getData());
  1405. // Now test writing to a file
  1406. EXPECT_EQ(0, unlink(DUID_FILE));
  1407. EXPECT_NO_THROW(srv.writeServerID(DUID_FILE));
  1408. fstream file2(DUID_FILE, ios::in);
  1409. ASSERT_TRUE(file2.good());
  1410. string text;
  1411. file2 >> text;
  1412. file2.close();
  1413. EXPECT_EQ(duid1_text, text);
  1414. }
  1415. // A set of tests verifying server's behaviour when it receives the DHCPv6
  1416. // Client Fqdn Option.
  1417. // @todo: Extend these tests once appropriate configuration parameters are
  1418. // implemented (ticket #3034).
  1419. // Test server's response when client requests that server performs AAAA update.
  1420. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdate) {
  1421. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S,
  1422. "myhost.example.com",
  1423. Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
  1424. "myhost.example.com.");
  1425. }
  1426. // Test server's response when client provides partial domain-name and requests
  1427. // that server performs AAAA update.
  1428. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdatePartialName) {
  1429. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S, "myhost",
  1430. Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
  1431. "myhost.example.com.");
  1432. }
  1433. // Test server's response when client provides empty domain-name and requests
  1434. // that server performs AAAA update.
  1435. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdateNoName) {
  1436. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S, "",
  1437. Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
  1438. "myhost.example.com.");
  1439. }
  1440. // Test server's response when client requests no DNS update.
  1441. TEST_F(FqdnDhcpv6SrvTest, noUpdate) {
  1442. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_N,
  1443. "myhost.example.com",
  1444. Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_N,
  1445. "myhost.example.com.");
  1446. }
  1447. // Test server's response when client requests that server delegates the AAAA
  1448. // update to the client and this delegation is not allowed.
  1449. TEST_F(FqdnDhcpv6SrvTest, clientAAAAUpdateNotAllowed) {
  1450. testFqdn(DHCPV6_SOLICIT, true, 0, "myhost.example.com.",
  1451. Option6ClientFqdn::FULL,
  1452. Option6ClientFqdn::FLAG_S | Option6ClientFqdn::FLAG_O,
  1453. "myhost.example.com.");
  1454. }
  1455. // Test that exception is thrown if supplied NULL answer packet when
  1456. // creating NameChangeRequests.
  1457. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAnswer) {
  1458. NakedDhcpv6Srv srv(0);
  1459. Pkt6Ptr answer;
  1460. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1461. "myhost.example.com",
  1462. Option6ClientFqdn::FULL);
  1463. EXPECT_THROW(srv.createNameChangeRequests(answer, fqdn),
  1464. isc::Unexpected);
  1465. }
  1466. // Test that exception is thrown if supplied answer from the server
  1467. // contains no DUID when creating NameChangeRequests.
  1468. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoDUID) {
  1469. NakedDhcpv6Srv srv(0);
  1470. Pkt6Ptr answer = Pkt6Ptr(new Pkt6(DHCPV6_REPLY, 1234));
  1471. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1472. "myhost.example.com",
  1473. Option6ClientFqdn::FULL);
  1474. EXPECT_THROW(srv.createNameChangeRequests(answer, fqdn),
  1475. isc::Unexpected);
  1476. }
  1477. // Test no NameChangeRequests are added if FQDN option is NULL.
  1478. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoFQDN) {
  1479. NakedDhcpv6Srv srv(0);
  1480. // Create Reply message with Client Id and Server id.
  1481. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1482. // Pass NULL FQDN option. No NameChangeRequests should be created.
  1483. Option6ClientFqdnPtr fqdn;
  1484. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1485. // There should be no new NameChangeRequests.
  1486. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1487. }
  1488. // Test that NameChangeRequests are not generated if an answer message
  1489. // contains no addresses.
  1490. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAddr) {
  1491. NakedDhcpv6Srv srv(0);
  1492. // Create Reply message with Client Id and Server id.
  1493. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1494. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1495. "myhost.example.com",
  1496. Option6ClientFqdn::FULL);
  1497. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1498. // We didn't add any IAs, so there should be no NameChangeRequests in th
  1499. // queue.
  1500. ASSERT_TRUE(srv.name_change_reqs_.empty());
  1501. }
  1502. // Test that a number of NameChangeRequests is created as a result of
  1503. // processing the answer message which holds 3 IAs and when FQDN is
  1504. // specified.
  1505. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequests) {
  1506. NakedDhcpv6Srv srv(0);
  1507. // Create Reply message with Client Id and Server id.
  1508. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1509. // Create three IAs, each having different address.
  1510. addIA(1234, IOAddress("2001:db8:1::1"), answer);
  1511. addIA(2345, IOAddress("2001:db8:1::2"), answer);
  1512. addIA(3456, IOAddress("2001:db8:1::3"), answer);
  1513. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1514. "myhost.example.com",
  1515. Option6ClientFqdn::FULL);
  1516. // Create NameChangeRequests. Since we have added 3 IAs, it should
  1517. // result in generation of 3 distinct NameChangeRequests.
  1518. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1519. ASSERT_EQ(3, srv.name_change_reqs_.size());
  1520. // Verify that NameChangeRequests are correct. Each call to the
  1521. // verifyNameChangeRequest will pop verified request from the queue.
  1522. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1523. "2001:db8:1::1",
  1524. "000201415AA33D1187D148275136FA30300478"
  1525. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1526. 0, 500);
  1527. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1528. "2001:db8:1::2",
  1529. "000201415AA33D1187D148275136FA30300478"
  1530. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1531. 0, 500);
  1532. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1533. "2001:db8:1::3",
  1534. "000201415AA33D1187D148275136FA30300478"
  1535. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1536. 0, 500);
  1537. }
  1538. // Test creation of the NameChangeRequest to remove both forward and reverse
  1539. // mapping for the given lease.
  1540. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestFwdRev) {
  1541. NakedDhcpv6Srv srv(0);
  1542. lease_->fqdn_fwd_ = true;
  1543. lease_->fqdn_rev_ = true;
  1544. lease_->hostname_ = "myhost.example.com.";
  1545. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1546. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1547. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1548. "2001:db8:1::1",
  1549. "000201415AA33D1187D148275136FA30300478"
  1550. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1551. 0, 502);
  1552. }
  1553. // Test creation of the NameChangeRequest to remove reverse mapping for the
  1554. // given lease.
  1555. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestRev) {
  1556. NakedDhcpv6Srv srv(0);
  1557. lease_->fqdn_fwd_ = false;
  1558. lease_->fqdn_rev_ = true;
  1559. lease_->hostname_ = "myhost.example.com.";
  1560. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1561. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1562. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, false,
  1563. "2001:db8:1::1",
  1564. "000201415AA33D1187D148275136FA30300478"
  1565. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1566. 0, 502);
  1567. }
  1568. // Test that NameChangeRequest to remove DNS records is not generated when
  1569. // neither forward nor reverse DNS update has been performed for a lease.
  1570. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoUpdate) {
  1571. NakedDhcpv6Srv srv(0);
  1572. lease_->fqdn_fwd_ = false;
  1573. lease_->fqdn_rev_ = false;
  1574. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1575. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1576. }
  1577. // Test that NameChangeRequest is not generated if the hostname hasn't been
  1578. // specified for a lease for which forward and reverse mapping has been set.
  1579. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoHostname) {
  1580. NakedDhcpv6Srv srv(0);
  1581. lease_->fqdn_fwd_ = true;
  1582. lease_->fqdn_rev_ = true;
  1583. lease_->hostname_ = "";
  1584. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1585. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1586. }
  1587. // Test that NameChangeRequest is not generated if the invalid hostname has
  1588. // been specified for a lease for which forward and reverse mapping has been
  1589. // set.
  1590. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestWrongHostname) {
  1591. NakedDhcpv6Srv srv(0);
  1592. lease_->fqdn_fwd_ = true;
  1593. lease_->fqdn_rev_ = true;
  1594. lease_->hostname_ = "myhost..example.com.";
  1595. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1596. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1597. }
  1598. // Test that Advertise message generated in a response to the Solicit will
  1599. // not result in generation if the NameChangeRequests.
  1600. TEST_F(FqdnDhcpv6SrvTest, processSolicit) {
  1601. NakedDhcpv6Srv srv(0);
  1602. // Create a Solicit message with FQDN option and generate server's
  1603. // response using processSolicit function.
  1604. testProcessMessage(DHCPV6_SOLICIT, "myhost.example.com", srv);
  1605. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1606. }
  1607. // Test that client may send two requests, each carrying FQDN option with
  1608. // a different domain-name. Server should use existing lease for the second
  1609. // request but modify the DNS entries for the lease according to the contents
  1610. // of the FQDN sent in the second request.
  1611. TEST_F(FqdnDhcpv6SrvTest, processTwoRequests) {
  1612. NakedDhcpv6Srv srv(0);
  1613. // Create a Request message with FQDN option and generate server's
  1614. // response using processRequest function. This will result in the
  1615. // creation of a new lease and the appropriate NameChangeRequest
  1616. // to add both reverse and forward mapping to DNS.
  1617. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1618. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1619. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1620. "2001:db8:1:1::dead:beef",
  1621. "000201415AA33D1187D148275136FA30300478"
  1622. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1623. 0, 4000);
  1624. // Client may send another request message with a new domain-name. In this
  1625. // case the same lease will be returned. The existing DNS entry needs to
  1626. // be replaced with a new one. Server should determine that the different
  1627. // FQDN has been already added to the DNS. As a result, the old DNS
  1628. // entries should be removed and the entries for the new domain-name
  1629. // should be added. Therefore, we expect two NameChangeRequests. One to
  1630. // remove the existing entries, one to add new entries.
  1631. testProcessMessage(DHCPV6_REQUEST, "otherhost.example.com", srv);
  1632. ASSERT_EQ(2, srv.name_change_reqs_.size());
  1633. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1634. "2001:db8:1:1::dead:beef",
  1635. "000201415AA33D1187D148275136FA30300478"
  1636. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1637. 0, 4000);
  1638. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1639. "2001:db8:1:1::dead:beef",
  1640. "000201D422AA463306223D269B6CB7AFE7AAD265FC"
  1641. "EA97F93623019B2E0D14E5323D5A",
  1642. 0, 4000);
  1643. }
  1644. // Test that client may send Request followed by the Renew, both holding
  1645. // FQDN options, but each option holding different domain-name. The Renew
  1646. // should result in generation of the two NameChangeRequests, one to remove
  1647. // DNS entry added previously when Request was processed, another one to
  1648. // add a new entry for the FQDN held in the Renew.
  1649. TEST_F(FqdnDhcpv6SrvTest, processRequestRenew) {
  1650. NakedDhcpv6Srv srv(0);
  1651. // Create a Request message with FQDN option and generate server's
  1652. // response using processRequest function. This will result in the
  1653. // creation of a new lease and the appropriate NameChangeRequest
  1654. // to add both reverse and forward mapping to DNS.
  1655. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1656. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1657. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1658. "2001:db8:1:1::dead:beef",
  1659. "000201415AA33D1187D148275136FA30300478"
  1660. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1661. 0, 4000);
  1662. // Client may send Renew message with a new domain-name. In this
  1663. // case the same lease will be returned. The existing DNS entry needs to
  1664. // be replaced with a new one. Server should determine that the different
  1665. // FQDN has been already added to the DNS. As a result, the old DNS
  1666. // entries should be removed and the entries for the new domain-name
  1667. // should be added. Therefore, we expect two NameChangeRequests. One to
  1668. // remove the existing entries, one to add new entries.
  1669. testProcessMessage(DHCPV6_RENEW, "otherhost.example.com", srv);
  1670. ASSERT_EQ(2, srv.name_change_reqs_.size());
  1671. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1672. "2001:db8:1:1::dead:beef",
  1673. "000201415AA33D1187D148275136FA30300478"
  1674. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1675. 0, 4000);
  1676. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1677. "2001:db8:1:1::dead:beef",
  1678. "000201D422AA463306223D269B6CB7AFE7AAD265FC"
  1679. "EA97F93623019B2E0D14E5323D5A",
  1680. 0, 4000);
  1681. }
  1682. TEST_F(FqdnDhcpv6SrvTest, processRequestRelease) {
  1683. NakedDhcpv6Srv srv(0);
  1684. // Create a Request message with FQDN option and generate server's
  1685. // response using processRequest function. This will result in the
  1686. // creation of a new lease and the appropriate NameChangeRequest
  1687. // to add both reverse and forward mapping to DNS.
  1688. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1689. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1690. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1691. "2001:db8:1:1::dead:beef",
  1692. "000201415AA33D1187D148275136FA30300478"
  1693. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1694. 0, 4000);
  1695. // Client may send Release message. In this case the lease should be
  1696. // removed and all existing DNS entries for this lease should be
  1697. // also removed. Therefore, we expect that single NameChangeRequest to
  1698. // remove DNS entries is generated.
  1699. testProcessMessage(DHCPV6_RELEASE, "otherhost.example.com", srv);
  1700. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1701. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1702. "2001:db8:1:1::dead:beef",
  1703. "000201415AA33D1187D148275136FA30300478"
  1704. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1705. 0, 4000);
  1706. }
  1707. /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
  1708. /// to call processX() methods.
  1709. } // end of anonymous namespace