dhcp6_srv_unittest.cc 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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(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(Lease::TYPE_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(Lease::TYPE_NA,
  857. addr);
  858. ASSERT_TRUE(l);
  859. // Check that T1, T2, preferred, valid and cltt really set and not using
  860. // previous (500, 501, etc.) values
  861. EXPECT_NE(l->t1_, subnet_->getT1());
  862. EXPECT_NE(l->t2_, subnet_->getT2());
  863. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  864. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  865. EXPECT_NE(l->cltt_, time(NULL));
  866. // Let's create a RENEW
  867. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  868. req->setRemoteAddr(IOAddress("fe80::abcd"));
  869. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  870. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  871. ia->addOption(renewed_addr_opt);
  872. req->addOption(ia);
  873. req->addOption(clientid);
  874. // Server-id is mandatory in RENEW
  875. req->addOption(srv.getServerID());
  876. // Pass it to the server and hope for a REPLY
  877. Pkt6Ptr reply = srv.processRenew(req);
  878. // Check if we get response at all
  879. checkResponse(reply, DHCPV6_REPLY, 1234);
  880. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  881. ASSERT_TRUE(tmp);
  882. // Check that IA_NA was returned and that there's an address included
  883. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  884. subnet_->getT2());
  885. ASSERT_TRUE(addr_opt);
  886. // Check that we've got the address we requested
  887. checkIAAddr(addr_opt, addr, subnet_->getPreferred(), subnet_->getValid());
  888. // Check DUIDs
  889. checkServerId(reply, srv.getServerID());
  890. checkClientId(reply, clientid);
  891. // Check that the lease is really in the database
  892. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  893. ASSERT_TRUE(l);
  894. // Check that T1, T2, preferred, valid and cltt were really updated
  895. EXPECT_EQ(l->t1_, subnet_->getT1());
  896. EXPECT_EQ(l->t2_, subnet_->getT2());
  897. EXPECT_EQ(l->preferred_lft_, subnet_->getPreferred());
  898. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  899. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  900. int32_t cltt = static_cast<int32_t>(l->cltt_);
  901. int32_t expected = static_cast<int32_t>(time(NULL));
  902. // equality or difference by 1 between cltt and expected is ok.
  903. EXPECT_GE(1, abs(cltt - expected));
  904. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
  905. }
  906. // This test verifies that incoming (invalid) RENEW can be handled properly.
  907. //
  908. // This test checks 3 scenarios:
  909. // 1. there is no such lease at all
  910. // 2. there is such a lease, but it is assigned to a different IAID
  911. // 3. there is such a lease, but it belongs to a different client
  912. //
  913. // expected:
  914. // - returned REPLY message has copy of client-id
  915. // - returned REPLY message has server-id
  916. // - returned REPLY message has IA that includes STATUS-CODE
  917. // - No lease in LeaseMgr
  918. TEST_F(Dhcpv6SrvTest, RenewReject) {
  919. NakedDhcpv6Srv srv(0);
  920. const IOAddress addr("2001:db8:1:1::dead");
  921. const uint32_t transid = 1234;
  922. const uint32_t valid_iaid = 234;
  923. const uint32_t bogus_iaid = 456;
  924. // Quick sanity check that the address we're about to use is ok
  925. ASSERT_TRUE(subnet_->inPool(addr));
  926. // GenerateClientId() also sets duid_
  927. OptionPtr clientid = generateClientId();
  928. // Check that the lease is NOT in the database
  929. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  930. addr);
  931. ASSERT_FALSE(l);
  932. // Let's create a RENEW
  933. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, transid));
  934. req->setRemoteAddr(IOAddress("fe80::abcd"));
  935. boost::shared_ptr<Option6IA> ia = generateIA(bogus_iaid, 1500, 3000);
  936. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  937. ia->addOption(renewed_addr_opt);
  938. req->addOption(ia);
  939. req->addOption(clientid);
  940. // Server-id is mandatory in RENEW
  941. req->addOption(srv.getServerID());
  942. // Case 1: No lease known to server
  943. // Pass it to the server and hope for a REPLY
  944. Pkt6Ptr reply = srv.processRenew(req);
  945. // Check if we get response at all
  946. checkResponse(reply, DHCPV6_REPLY, transid);
  947. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  948. ASSERT_TRUE(tmp);
  949. // Check that IA_NA was returned and that there's an address included
  950. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  951. ASSERT_TRUE(ia);
  952. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  953. // Check that there is no lease added
  954. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  955. ASSERT_FALSE(l);
  956. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  957. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  958. // value on purpose. They should be updated during RENEW.
  959. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, valid_iaid,
  960. 501, 502, 503, 504, subnet_->getID(), 0));
  961. lease->cltt_ = 123; // Let's use it as an indicator that the lease
  962. // was NOT updated.
  963. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  964. // Pass it to the server and hope for a REPLY
  965. reply = srv.processRenew(req);
  966. checkResponse(reply, DHCPV6_REPLY, transid);
  967. tmp = reply->getOption(D6O_IA_NA);
  968. ASSERT_TRUE(tmp);
  969. // Check that IA_NA was returned and that there's an address included
  970. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  971. ASSERT_TRUE(ia);
  972. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  973. // There is a iaid mis-match, so server should respond that there is
  974. // no such address to renew.
  975. // CASE 3: Lease belongs to a client with different client-id
  976. req->delOption(D6O_CLIENTID);
  977. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  978. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  979. req->addOption(generateClientId(13)); // generate different DUID
  980. // (with length 13)
  981. reply = srv.processRenew(req);
  982. checkResponse(reply, DHCPV6_REPLY, transid);
  983. tmp = reply->getOption(D6O_IA_NA);
  984. ASSERT_TRUE(tmp);
  985. // Check that IA_NA was returned and that there's an address included
  986. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  987. ASSERT_TRUE(ia);
  988. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  989. lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  990. ASSERT_TRUE(lease);
  991. // Verify that the lease was not updated.
  992. EXPECT_EQ(123, lease->cltt_);
  993. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  994. }
  995. // This test verifies that incoming (positive) RELEASE can be handled properly,
  996. // that a REPLY is generated, that the response has status code and that the
  997. // lease is indeed removed from the database.
  998. //
  999. // expected:
  1000. // - returned REPLY message has copy of client-id
  1001. // - returned REPLY message has server-id
  1002. // - returned REPLY message has IA that does not include an IAADDR
  1003. // - lease is actually removed from LeaseMgr
  1004. TEST_F(Dhcpv6SrvTest, ReleaseBasic) {
  1005. NakedDhcpv6Srv srv(0);
  1006. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1007. const uint32_t iaid = 234;
  1008. // Generate client-id also duid_
  1009. OptionPtr clientid = generateClientId();
  1010. // Check that the address we are about to use is indeed in pool
  1011. ASSERT_TRUE(subnet_->inPool(addr));
  1012. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1013. // value on purpose. They should be updated during RENEW.
  1014. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1015. 501, 502, 503, 504, subnet_->getID(), 0));
  1016. lease->cltt_ = 1234;
  1017. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1018. // Check that the lease is really in the database
  1019. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1020. addr);
  1021. ASSERT_TRUE(l);
  1022. // Let's create a RELEASE
  1023. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  1024. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1025. boost::shared_ptr<Option6IA> ia = generateIA(iaid, 1500, 3000);
  1026. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1027. ia->addOption(released_addr_opt);
  1028. req->addOption(ia);
  1029. req->addOption(clientid);
  1030. // Server-id is mandatory in RELEASE
  1031. req->addOption(srv.getServerID());
  1032. // Pass it to the server and hope for a REPLY
  1033. Pkt6Ptr reply = srv.processRelease(req);
  1034. // Check if we get response at all
  1035. checkResponse(reply, DHCPV6_REPLY, 1234);
  1036. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1037. ASSERT_TRUE(tmp);
  1038. // Check that IA_NA was returned and that there's an address included
  1039. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1040. checkIA_NAStatusCode(ia, STATUS_Success);
  1041. checkMsgStatusCode(reply, STATUS_Success);
  1042. // There should be no address returned in RELEASE (see RFC3315, 18.2.6)
  1043. EXPECT_FALSE(tmp->getOption(D6O_IAADDR));
  1044. // Check DUIDs
  1045. checkServerId(reply, srv.getServerID());
  1046. checkClientId(reply, clientid);
  1047. // Check that the lease is really gone in the database
  1048. // get lease by address
  1049. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1050. ASSERT_FALSE(l);
  1051. // get lease by subnetid/duid/iaid combination
  1052. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, *duid_, iaid,
  1053. subnet_->getID());
  1054. ASSERT_FALSE(l);
  1055. }
  1056. // This test verifies that incoming (invalid) RELEASE can be handled properly.
  1057. //
  1058. // This test checks 3 scenarios:
  1059. // 1. there is no such lease at all
  1060. // 2. there is such a lease, but it is assigned to a different IAID
  1061. // 3. there is such a lease, but it belongs to a different client
  1062. //
  1063. // expected:
  1064. // - returned REPLY message has copy of client-id
  1065. // - returned REPLY message has server-id
  1066. // - returned REPLY message has IA that includes STATUS-CODE
  1067. // - No lease in LeaseMgr
  1068. TEST_F(Dhcpv6SrvTest, ReleaseReject) {
  1069. NakedDhcpv6Srv srv(0);
  1070. const IOAddress addr("2001:db8:1:1::dead");
  1071. const uint32_t transid = 1234;
  1072. const uint32_t valid_iaid = 234;
  1073. const uint32_t bogus_iaid = 456;
  1074. // Quick sanity check that the address we're about to use is ok
  1075. ASSERT_TRUE(subnet_->inPool(addr));
  1076. // GenerateClientId() also sets duid_
  1077. OptionPtr clientid = generateClientId();
  1078. // Check that the lease is NOT in the database
  1079. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1080. addr);
  1081. ASSERT_FALSE(l);
  1082. // Let's create a RELEASE
  1083. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, transid));
  1084. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1085. boost::shared_ptr<Option6IA> ia = generateIA(bogus_iaid, 1500, 3000);
  1086. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1087. ia->addOption(released_addr_opt);
  1088. req->addOption(ia);
  1089. req->addOption(clientid);
  1090. // Server-id is mandatory in RENEW
  1091. req->addOption(srv.getServerID());
  1092. // Case 1: No lease known to server
  1093. SCOPED_TRACE("CASE 1: No lease known to server");
  1094. // Pass it to the server and hope for a REPLY
  1095. Pkt6Ptr reply = srv.processRelease(req);
  1096. // Check if we get response at all
  1097. checkResponse(reply, DHCPV6_REPLY, transid);
  1098. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1099. ASSERT_TRUE(tmp);
  1100. // Check that IA_NA was returned and that there's an address included
  1101. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1102. ASSERT_TRUE(ia);
  1103. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1104. checkMsgStatusCode(reply, STATUS_NoBinding);
  1105. // Check that the lease is not there
  1106. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1107. ASSERT_FALSE(l);
  1108. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  1109. SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but to a different IAID");
  1110. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, valid_iaid,
  1111. 501, 502, 503, 504, subnet_->getID(), 0));
  1112. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1113. // Pass it to the server and hope for a REPLY
  1114. reply = srv.processRelease(req);
  1115. checkResponse(reply, DHCPV6_REPLY, transid);
  1116. tmp = reply->getOption(D6O_IA_NA);
  1117. ASSERT_TRUE(tmp);
  1118. // Check that IA_NA was returned and that there's an address included
  1119. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1120. ASSERT_TRUE(ia);
  1121. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1122. checkMsgStatusCode(reply, STATUS_NoBinding);
  1123. // Check that the lease is still there
  1124. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1125. ASSERT_TRUE(l);
  1126. // CASE 3: Lease belongs to a client with different client-id
  1127. SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
  1128. req->delOption(D6O_CLIENTID);
  1129. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  1130. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  1131. req->addOption(generateClientId(13)); // generate different DUID
  1132. // (with length 13)
  1133. reply = srv.processRelease(req);
  1134. checkResponse(reply, DHCPV6_REPLY, transid);
  1135. tmp = reply->getOption(D6O_IA_NA);
  1136. ASSERT_TRUE(tmp);
  1137. // Check that IA_NA was returned and that there's an address included
  1138. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1139. ASSERT_TRUE(ia);
  1140. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1141. checkMsgStatusCode(reply, STATUS_NoBinding);
  1142. // Check that the lease is still there
  1143. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1144. ASSERT_TRUE(l);
  1145. // Finally, let's cleanup the database
  1146. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  1147. }
  1148. // This test verifies if the status code option is generated properly.
  1149. TEST_F(Dhcpv6SrvTest, StatusCode) {
  1150. NakedDhcpv6Srv srv(0);
  1151. // a dummy content for client-id
  1152. uint8_t expected[] = {
  1153. 0x0, 0xD, // option code = 13
  1154. 0x0, 0x7, // option length = 7
  1155. 0x0, 0x3, // status code = 3
  1156. 0x41, 0x42, 0x43, 0x44, 0x45 // string value ABCDE
  1157. };
  1158. // Create the option.
  1159. OptionPtr status = srv.createStatusCode(3, "ABCDE");
  1160. // Allocate an output buffer. We will store the option
  1161. // in wire format here.
  1162. OutputBuffer buf(sizeof(expected));
  1163. // Prepare the wire format.
  1164. ASSERT_NO_THROW(status->pack(buf));
  1165. // Check that the option buffer has valid length (option header + data).
  1166. ASSERT_EQ(sizeof(expected), buf.getLength());
  1167. // Verify the contents of the option.
  1168. EXPECT_EQ(0, memcmp(expected, buf.getData(), sizeof(expected)));
  1169. }
  1170. // This test verifies if the sanityCheck() really checks options presence.
  1171. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  1172. NakedDhcpv6Srv srv(0);
  1173. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1174. // Set link-local sender address, so appropriate subnet can be
  1175. // selected for this packet.
  1176. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1177. // client-id is optional for information-request, so
  1178. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  1179. // empty packet, no client-id, no server-id
  1180. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  1181. RFCViolation);
  1182. // This doesn't make much sense, but let's check it for completeness
  1183. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  1184. OptionPtr clientid = generateClientId();
  1185. pkt->addOption(clientid);
  1186. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  1187. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  1188. pkt->addOption(srv.getServerID());
  1189. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  1190. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  1191. // sane section ends here, let's do some negative tests as well
  1192. pkt->addOption(clientid);
  1193. pkt->addOption(clientid);
  1194. // with more than one client-id it should throw, no matter what
  1195. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  1196. RFCViolation);
  1197. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  1198. RFCViolation);
  1199. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  1200. RFCViolation);
  1201. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  1202. RFCViolation);
  1203. pkt->delOption(D6O_CLIENTID);
  1204. pkt->delOption(D6O_CLIENTID);
  1205. // again we have only one client-id
  1206. // let's try different type of insanity - several server-ids
  1207. pkt->addOption(srv.getServerID());
  1208. pkt->addOption(srv.getServerID());
  1209. // with more than one server-id it should throw, no matter what
  1210. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  1211. RFCViolation);
  1212. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  1213. RFCViolation);
  1214. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  1215. RFCViolation);
  1216. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  1217. RFCViolation);
  1218. }
  1219. // This test verifies if selectSubnet() selects proper subnet for a given
  1220. // source address.
  1221. TEST_F(Dhcpv6SrvTest, selectSubnetAddr) {
  1222. NakedDhcpv6Srv srv(0);
  1223. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1224. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1225. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1226. // CASE 1: We have only one subnet defined and we received local traffic.
  1227. // The only available subnet should be selected
  1228. CfgMgr::instance().deleteSubnets6();
  1229. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1230. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1231. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1232. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1233. EXPECT_EQ(selected, subnet1);
  1234. // CASE 2: We have only one subnet defined and we received relayed traffic.
  1235. // We should NOT select it.
  1236. // Identical steps as in case 1, but repeated for clarity
  1237. CfgMgr::instance().deleteSubnets6();
  1238. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1239. pkt->setRemoteAddr(IOAddress("2001:db8:abcd::2345"));
  1240. selected = srv.selectSubnet(pkt);
  1241. EXPECT_FALSE(selected);
  1242. // CASE 3: We have three subnets defined and we received local traffic.
  1243. // Nothing should be selected.
  1244. CfgMgr::instance().deleteSubnets6();
  1245. CfgMgr::instance().addSubnet6(subnet1);
  1246. CfgMgr::instance().addSubnet6(subnet2);
  1247. CfgMgr::instance().addSubnet6(subnet3);
  1248. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1249. selected = srv.selectSubnet(pkt);
  1250. EXPECT_FALSE(selected);
  1251. // CASE 4: We have three subnets defined and we received relayed traffic
  1252. // that came out of subnet 2. We should select subnet2 then
  1253. CfgMgr::instance().deleteSubnets6();
  1254. CfgMgr::instance().addSubnet6(subnet1);
  1255. CfgMgr::instance().addSubnet6(subnet2);
  1256. CfgMgr::instance().addSubnet6(subnet3);
  1257. pkt->setRemoteAddr(IOAddress("2001:db8:2::baca"));
  1258. selected = srv.selectSubnet(pkt);
  1259. EXPECT_EQ(selected, subnet2);
  1260. // CASE 5: We have three subnets defined and we received relayed traffic
  1261. // that came out of undefined subnet. We should select nothing
  1262. CfgMgr::instance().deleteSubnets6();
  1263. CfgMgr::instance().addSubnet6(subnet1);
  1264. CfgMgr::instance().addSubnet6(subnet2);
  1265. CfgMgr::instance().addSubnet6(subnet3);
  1266. pkt->setRemoteAddr(IOAddress("2001:db8:4::baca"));
  1267. selected = srv.selectSubnet(pkt);
  1268. EXPECT_FALSE(selected);
  1269. }
  1270. // This test verifies if selectSubnet() selects proper subnet for a given
  1271. // network interface name.
  1272. TEST_F(Dhcpv6SrvTest, selectSubnetIface) {
  1273. NakedDhcpv6Srv srv(0);
  1274. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1275. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1276. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1277. subnet1->setIface("eth0");
  1278. subnet3->setIface("wifi1");
  1279. // CASE 1: We have only one subnet defined and it is available via eth0.
  1280. // Packet came from eth0. The only available subnet should be selected
  1281. CfgMgr::instance().deleteSubnets6();
  1282. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1283. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1284. pkt->setIface("eth0");
  1285. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1286. EXPECT_EQ(selected, subnet1);
  1287. // CASE 2: We have only one subnet defined and it is available via eth0.
  1288. // Packet came from eth1. We should not select it
  1289. CfgMgr::instance().deleteSubnets6();
  1290. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1291. pkt->setIface("eth1");
  1292. selected = srv.selectSubnet(pkt);
  1293. EXPECT_FALSE(selected);
  1294. // CASE 3: We have only 3 subnets defined, one over eth0, one remote and
  1295. // one over wifi1.
  1296. // Packet came from eth1. We should not select it
  1297. CfgMgr::instance().deleteSubnets6();
  1298. CfgMgr::instance().addSubnet6(subnet1);
  1299. CfgMgr::instance().addSubnet6(subnet2);
  1300. CfgMgr::instance().addSubnet6(subnet3);
  1301. pkt->setIface("eth0");
  1302. EXPECT_EQ(subnet1, srv.selectSubnet(pkt));
  1303. pkt->setIface("eth3"); // no such interface
  1304. EXPECT_EQ(Subnet6Ptr(), srv.selectSubnet(pkt)); // nothing selected
  1305. pkt->setIface("wifi1");
  1306. EXPECT_EQ(subnet3, srv.selectSubnet(pkt));
  1307. }
  1308. // This test verifies if selectSubnet() selects proper subnet for a given
  1309. // linkaddr in RELAY-FORW message
  1310. TEST_F(Dhcpv6SrvTest, selectSubnetRelayLinkaddr) {
  1311. NakedDhcpv6Srv srv(0);
  1312. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1313. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1314. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1315. Pkt6::RelayInfo relay;
  1316. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1317. relay.peeraddr_ = IOAddress("fe80::1");
  1318. // CASE 1: We have only one subnet defined and we received relayed traffic.
  1319. // The only available subnet should NOT be selected.
  1320. CfgMgr::instance().deleteSubnets6();
  1321. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1322. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1323. pkt->relay_info_.push_back(relay);
  1324. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1325. EXPECT_FALSE(selected);
  1326. // CASE 2: We have three subnets defined and we received relayed traffic.
  1327. // Nothing should be selected.
  1328. CfgMgr::instance().deleteSubnets6();
  1329. CfgMgr::instance().addSubnet6(subnet1);
  1330. CfgMgr::instance().addSubnet6(subnet2);
  1331. CfgMgr::instance().addSubnet6(subnet3);
  1332. selected = srv.selectSubnet(pkt);
  1333. EXPECT_EQ(selected, subnet2);
  1334. // CASE 3: We have three subnets defined and we received relayed traffic
  1335. // that came out of subnet 2. We should select subnet2 then
  1336. CfgMgr::instance().deleteSubnets6();
  1337. CfgMgr::instance().addSubnet6(subnet1);
  1338. CfgMgr::instance().addSubnet6(subnet2);
  1339. CfgMgr::instance().addSubnet6(subnet3);
  1340. // Source of the packet should have no meaning. Selection is based
  1341. // on linkaddr field in the relay
  1342. pkt->setRemoteAddr(IOAddress("2001:db8:1::baca"));
  1343. selected = srv.selectSubnet(pkt);
  1344. EXPECT_EQ(selected, subnet2);
  1345. // CASE 4: We have three subnets defined and we received relayed traffic
  1346. // that came out of undefined subnet. We should select nothing
  1347. CfgMgr::instance().deleteSubnets6();
  1348. CfgMgr::instance().addSubnet6(subnet1);
  1349. CfgMgr::instance().addSubnet6(subnet2);
  1350. CfgMgr::instance().addSubnet6(subnet3);
  1351. pkt->relay_info_.clear();
  1352. relay.linkaddr_ = IOAddress("2001:db8:4::1234");
  1353. pkt->relay_info_.push_back(relay);
  1354. selected = srv.selectSubnet(pkt);
  1355. EXPECT_FALSE(selected);
  1356. }
  1357. // This test verifies if selectSubnet() selects proper subnet for a given
  1358. // interface-id option
  1359. TEST_F(Dhcpv6SrvTest, selectSubnetRelayInterfaceId) {
  1360. NakedDhcpv6Srv srv(0);
  1361. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1362. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1363. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1364. subnet1->setInterfaceId(generateInterfaceId("relay1"));
  1365. subnet2->setInterfaceId(generateInterfaceId("relay2"));
  1366. // CASE 1: We have only one subnet defined and it is for interface-id "relay1"
  1367. // Packet came with interface-id "relay2". We should not select subnet1
  1368. CfgMgr::instance().deleteSubnets6();
  1369. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1370. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1371. Pkt6::RelayInfo relay;
  1372. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1373. relay.peeraddr_ = IOAddress("fe80::1");
  1374. OptionPtr opt = generateInterfaceId("relay2");
  1375. relay.options_.insert(make_pair(opt->getType(), opt));
  1376. pkt->relay_info_.push_back(relay);
  1377. // There is only one subnet configured and we are outside of that subnet
  1378. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1379. EXPECT_FALSE(selected);
  1380. // CASE 2: We have only one subnet defined and it is for interface-id "relay2"
  1381. // Packet came with interface-id "relay2". We should select it
  1382. CfgMgr::instance().deleteSubnets6();
  1383. CfgMgr::instance().addSubnet6(subnet2); // just a single subnet
  1384. selected = srv.selectSubnet(pkt);
  1385. EXPECT_EQ(selected, subnet2);
  1386. // CASE 3: We have only 3 subnets defined: one remote for interface-id "relay1",
  1387. // one remote for interface-id "relay2" and third local
  1388. // packet comes with interface-id "relay2". We should select subnet2
  1389. CfgMgr::instance().deleteSubnets6();
  1390. CfgMgr::instance().addSubnet6(subnet1);
  1391. CfgMgr::instance().addSubnet6(subnet2);
  1392. CfgMgr::instance().addSubnet6(subnet3);
  1393. EXPECT_EQ(subnet2, srv.selectSubnet(pkt));
  1394. }
  1395. // This test verifies if the server-id disk operations (read, write) are
  1396. // working properly.
  1397. TEST_F(Dhcpv6SrvTest, ServerID) {
  1398. NakedDhcpv6Srv srv(0);
  1399. string duid1_text = "01:ff:02:03:06:80:90:ab:cd:ef";
  1400. uint8_t duid1[] = { 0x01, 0xff, 2, 3, 6, 0x80, 0x90, 0xab, 0xcd, 0xef };
  1401. OptionBuffer expected_duid1(duid1, duid1 + sizeof(duid1));
  1402. fstream file1(DUID_FILE, ios::out | ios::trunc);
  1403. file1 << duid1_text;
  1404. file1.close();
  1405. // Test reading from a file
  1406. EXPECT_TRUE(srv.loadServerID(DUID_FILE));
  1407. ASSERT_TRUE(srv.getServerID());
  1408. ASSERT_EQ(sizeof(duid1) + Option::OPTION6_HDR_LEN, srv.getServerID()->len());
  1409. ASSERT_TRUE(expected_duid1 == srv.getServerID()->getData());
  1410. // Now test writing to a file
  1411. EXPECT_EQ(0, unlink(DUID_FILE));
  1412. EXPECT_NO_THROW(srv.writeServerID(DUID_FILE));
  1413. fstream file2(DUID_FILE, ios::in);
  1414. ASSERT_TRUE(file2.good());
  1415. string text;
  1416. file2 >> text;
  1417. file2.close();
  1418. EXPECT_EQ(duid1_text, text);
  1419. }
  1420. // A set of tests verifying server's behaviour when it receives the DHCPv6
  1421. // Client Fqdn Option.
  1422. // @todo: Extend these tests once appropriate configuration parameters are
  1423. // implemented (ticket #3034).
  1424. // Test server's response when client requests that server performs AAAA update.
  1425. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdate) {
  1426. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S,
  1427. "myhost.example.com",
  1428. Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
  1429. "myhost.example.com.");
  1430. }
  1431. // Test server's response when client provides partial domain-name and requests
  1432. // that server performs AAAA update.
  1433. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdatePartialName) {
  1434. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S, "myhost",
  1435. Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
  1436. "myhost.example.com.");
  1437. }
  1438. // Test server's response when client provides empty domain-name and requests
  1439. // that server performs AAAA update.
  1440. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdateNoName) {
  1441. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S, "",
  1442. Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
  1443. "myhost.example.com.");
  1444. }
  1445. // Test server's response when client requests no DNS update.
  1446. TEST_F(FqdnDhcpv6SrvTest, noUpdate) {
  1447. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_N,
  1448. "myhost.example.com",
  1449. Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_N,
  1450. "myhost.example.com.");
  1451. }
  1452. // Test server's response when client requests that server delegates the AAAA
  1453. // update to the client and this delegation is not allowed.
  1454. TEST_F(FqdnDhcpv6SrvTest, clientAAAAUpdateNotAllowed) {
  1455. testFqdn(DHCPV6_SOLICIT, true, 0, "myhost.example.com.",
  1456. Option6ClientFqdn::FULL,
  1457. Option6ClientFqdn::FLAG_S | Option6ClientFqdn::FLAG_O,
  1458. "myhost.example.com.");
  1459. }
  1460. // Test that exception is thrown if supplied NULL answer packet when
  1461. // creating NameChangeRequests.
  1462. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAnswer) {
  1463. NakedDhcpv6Srv srv(0);
  1464. Pkt6Ptr answer;
  1465. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1466. "myhost.example.com",
  1467. Option6ClientFqdn::FULL);
  1468. EXPECT_THROW(srv.createNameChangeRequests(answer, fqdn),
  1469. isc::Unexpected);
  1470. }
  1471. // Test that exception is thrown if supplied answer from the server
  1472. // contains no DUID when creating NameChangeRequests.
  1473. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoDUID) {
  1474. NakedDhcpv6Srv srv(0);
  1475. Pkt6Ptr answer = Pkt6Ptr(new Pkt6(DHCPV6_REPLY, 1234));
  1476. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1477. "myhost.example.com",
  1478. Option6ClientFqdn::FULL);
  1479. EXPECT_THROW(srv.createNameChangeRequests(answer, fqdn),
  1480. isc::Unexpected);
  1481. }
  1482. // Test no NameChangeRequests are added if FQDN option is NULL.
  1483. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoFQDN) {
  1484. NakedDhcpv6Srv srv(0);
  1485. // Create Reply message with Client Id and Server id.
  1486. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1487. // Pass NULL FQDN option. No NameChangeRequests should be created.
  1488. Option6ClientFqdnPtr fqdn;
  1489. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1490. // There should be no new NameChangeRequests.
  1491. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1492. }
  1493. // Test that NameChangeRequests are not generated if an answer message
  1494. // contains no addresses.
  1495. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAddr) {
  1496. NakedDhcpv6Srv srv(0);
  1497. // Create Reply message with Client Id and Server id.
  1498. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1499. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1500. "myhost.example.com",
  1501. Option6ClientFqdn::FULL);
  1502. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1503. // We didn't add any IAs, so there should be no NameChangeRequests in th
  1504. // queue.
  1505. ASSERT_TRUE(srv.name_change_reqs_.empty());
  1506. }
  1507. // Test that a number of NameChangeRequests is created as a result of
  1508. // processing the answer message which holds 3 IAs and when FQDN is
  1509. // specified.
  1510. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequests) {
  1511. NakedDhcpv6Srv srv(0);
  1512. // Create Reply message with Client Id and Server id.
  1513. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1514. // Create three IAs, each having different address.
  1515. addIA(1234, IOAddress("2001:db8:1::1"), answer);
  1516. addIA(2345, IOAddress("2001:db8:1::2"), answer);
  1517. addIA(3456, IOAddress("2001:db8:1::3"), answer);
  1518. // Use domain name in upper case. It should be converted to lower-case
  1519. // before DHCID is calculated. So, we should get the same result as if
  1520. // we typed domain name in lower-case.
  1521. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1522. "MYHOST.EXAMPLE.COM",
  1523. Option6ClientFqdn::FULL);
  1524. // Create NameChangeRequests. Since we have added 3 IAs, it should
  1525. // result in generation of 3 distinct NameChangeRequests.
  1526. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1527. ASSERT_EQ(3, srv.name_change_reqs_.size());
  1528. // Verify that NameChangeRequests are correct. Each call to the
  1529. // verifyNameChangeRequest will pop verified request from the queue.
  1530. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1531. "2001:db8:1::1",
  1532. "000201415AA33D1187D148275136FA30300478"
  1533. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1534. 0, 500);
  1535. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1536. "2001:db8:1::2",
  1537. "000201415AA33D1187D148275136FA30300478"
  1538. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1539. 0, 500);
  1540. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1541. "2001:db8:1::3",
  1542. "000201415AA33D1187D148275136FA30300478"
  1543. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1544. 0, 500);
  1545. }
  1546. // Test creation of the NameChangeRequest to remove both forward and reverse
  1547. // mapping for the given lease.
  1548. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestFwdRev) {
  1549. NakedDhcpv6Srv srv(0);
  1550. lease_->fqdn_fwd_ = true;
  1551. lease_->fqdn_rev_ = true;
  1552. // Part of the domain name is in upper case, to test that it gets converted
  1553. // to lower case before DHCID is computed. So, we should get the same DHCID
  1554. // as if we typed domain-name in lower case.
  1555. lease_->hostname_ = "MYHOST.example.com.";
  1556. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1557. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1558. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1559. "2001:db8:1::1",
  1560. "000201415AA33D1187D148275136FA30300478"
  1561. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1562. 0, 502);
  1563. }
  1564. // Test creation of the NameChangeRequest to remove reverse mapping for the
  1565. // given lease.
  1566. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestRev) {
  1567. NakedDhcpv6Srv srv(0);
  1568. lease_->fqdn_fwd_ = false;
  1569. lease_->fqdn_rev_ = true;
  1570. lease_->hostname_ = "myhost.example.com.";
  1571. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1572. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1573. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, false,
  1574. "2001:db8:1::1",
  1575. "000201415AA33D1187D148275136FA30300478"
  1576. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1577. 0, 502);
  1578. }
  1579. // Test that NameChangeRequest to remove DNS records is not generated when
  1580. // neither forward nor reverse DNS update has been performed for a lease.
  1581. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoUpdate) {
  1582. NakedDhcpv6Srv srv(0);
  1583. lease_->fqdn_fwd_ = false;
  1584. lease_->fqdn_rev_ = false;
  1585. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1586. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1587. }
  1588. // Test that NameChangeRequest is not generated if the hostname hasn't been
  1589. // specified for a lease for which forward and reverse mapping has been set.
  1590. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoHostname) {
  1591. NakedDhcpv6Srv srv(0);
  1592. lease_->fqdn_fwd_ = true;
  1593. lease_->fqdn_rev_ = true;
  1594. lease_->hostname_ = "";
  1595. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1596. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1597. }
  1598. // Test that NameChangeRequest is not generated if the invalid hostname has
  1599. // been specified for a lease for which forward and reverse mapping has been
  1600. // set.
  1601. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestWrongHostname) {
  1602. NakedDhcpv6Srv srv(0);
  1603. lease_->fqdn_fwd_ = true;
  1604. lease_->fqdn_rev_ = true;
  1605. lease_->hostname_ = "myhost..example.com.";
  1606. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1607. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1608. }
  1609. // Test that Advertise message generated in a response to the Solicit will
  1610. // not result in generation if the NameChangeRequests.
  1611. TEST_F(FqdnDhcpv6SrvTest, processSolicit) {
  1612. NakedDhcpv6Srv srv(0);
  1613. // Create a Solicit message with FQDN option and generate server's
  1614. // response using processSolicit function.
  1615. testProcessMessage(DHCPV6_SOLICIT, "myhost.example.com", srv);
  1616. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1617. }
  1618. // Test that client may send two requests, each carrying FQDN option with
  1619. // a different domain-name. Server should use existing lease for the second
  1620. // request but modify the DNS entries for the lease according to the contents
  1621. // of the FQDN sent in the second request.
  1622. TEST_F(FqdnDhcpv6SrvTest, processTwoRequests) {
  1623. NakedDhcpv6Srv srv(0);
  1624. // Create a Request message with FQDN option and generate server's
  1625. // response using processRequest function. This will result in the
  1626. // creation of a new lease and the appropriate NameChangeRequest
  1627. // to add both reverse and forward mapping to DNS.
  1628. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1629. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1630. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1631. "2001:db8:1:1::dead:beef",
  1632. "000201415AA33D1187D148275136FA30300478"
  1633. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1634. 0, 4000);
  1635. // Client may send another request message with a new domain-name. In this
  1636. // case the same lease will be returned. The existing DNS entry needs to
  1637. // be replaced with a new one. Server should determine that the different
  1638. // FQDN has been already added to the DNS. As a result, the old DNS
  1639. // entries should be removed and the entries for the new domain-name
  1640. // should be added. Therefore, we expect two NameChangeRequests. One to
  1641. // remove the existing entries, one to add new entries.
  1642. testProcessMessage(DHCPV6_REQUEST, "otherhost.example.com", srv);
  1643. ASSERT_EQ(2, srv.name_change_reqs_.size());
  1644. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1645. "2001:db8:1:1::dead:beef",
  1646. "000201415AA33D1187D148275136FA30300478"
  1647. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1648. 0, 4000);
  1649. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1650. "2001:db8:1:1::dead:beef",
  1651. "000201D422AA463306223D269B6CB7AFE7AAD265FC"
  1652. "EA97F93623019B2E0D14E5323D5A",
  1653. 0, 4000);
  1654. }
  1655. // Test that client may send Request followed by the Renew, both holding
  1656. // FQDN options, but each option holding different domain-name. The Renew
  1657. // should result in generation of the two NameChangeRequests, one to remove
  1658. // DNS entry added previously when Request was processed, another one to
  1659. // add a new entry for the FQDN held in the Renew.
  1660. TEST_F(FqdnDhcpv6SrvTest, processRequestRenew) {
  1661. NakedDhcpv6Srv srv(0);
  1662. // Create a Request message with FQDN option and generate server's
  1663. // response using processRequest function. This will result in the
  1664. // creation of a new lease and the appropriate NameChangeRequest
  1665. // to add both reverse and forward mapping to DNS.
  1666. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1667. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1668. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1669. "2001:db8:1:1::dead:beef",
  1670. "000201415AA33D1187D148275136FA30300478"
  1671. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1672. 0, 4000);
  1673. // Client may send Renew message with a new domain-name. In this
  1674. // case the same lease will be returned. The existing DNS entry needs to
  1675. // be replaced with a new one. Server should determine that the different
  1676. // FQDN has been already added to the DNS. As a result, the old DNS
  1677. // entries should be removed and the entries for the new domain-name
  1678. // should be added. Therefore, we expect two NameChangeRequests. One to
  1679. // remove the existing entries, one to add new entries.
  1680. testProcessMessage(DHCPV6_RENEW, "otherhost.example.com", srv);
  1681. ASSERT_EQ(2, srv.name_change_reqs_.size());
  1682. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1683. "2001:db8:1:1::dead:beef",
  1684. "000201415AA33D1187D148275136FA30300478"
  1685. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1686. 0, 4000);
  1687. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1688. "2001:db8:1:1::dead:beef",
  1689. "000201D422AA463306223D269B6CB7AFE7AAD265FC"
  1690. "EA97F93623019B2E0D14E5323D5A",
  1691. 0, 4000);
  1692. }
  1693. TEST_F(FqdnDhcpv6SrvTest, processRequestRelease) {
  1694. NakedDhcpv6Srv srv(0);
  1695. // Create a Request message with FQDN option and generate server's
  1696. // response using processRequest function. This will result in the
  1697. // creation of a new lease and the appropriate NameChangeRequest
  1698. // to add both reverse and forward mapping to DNS.
  1699. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1700. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1701. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1702. "2001:db8:1:1::dead:beef",
  1703. "000201415AA33D1187D148275136FA30300478"
  1704. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1705. 0, 4000);
  1706. // Client may send Release message. In this case the lease should be
  1707. // removed and all existing DNS entries for this lease should be
  1708. // also removed. Therefore, we expect that single NameChangeRequest to
  1709. // remove DNS entries is generated.
  1710. testProcessMessage(DHCPV6_RELEASE, "otherhost.example.com", srv);
  1711. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1712. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1713. "2001:db8:1:1::dead:beef",
  1714. "000201415AA33D1187D148275136FA30300478"
  1715. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1716. 0, 4000);
  1717. }
  1718. /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
  1719. /// to call processX() methods.
  1720. } // end of anonymous namespace