dhcp6_srv_unittest.cc 79 KB

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