dhcp6_srv_unittest.cc 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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, 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(D6O_IA_NA, 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(D6O_IA_NA, 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(D6O_IA_NA, 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(D6O_IA_NA, 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(D6O_IA_NA, 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(D6O_IA_NA, 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(), Lease::TYPE_NA, subnet_->getPreferred(),
  558. subnet_->getValid());
  559. // check DUIDs
  560. checkServerId(reply, srv.getServerID());
  561. checkClientId(reply, clientid);
  562. }
  563. // This test verifies that incoming SOLICIT can be handled properly, that an
  564. // ADVERTISE is generated, that the response has a prefix and that prefix
  565. // really belongs to the configured pool.
  566. //
  567. // This test sends a SOLICIT without any hint in IA_PD.
  568. //
  569. // constructed very simple SOLICIT message with:
  570. // - client-id option (mandatory)
  571. // - IA option (a request for address, without any addresses)
  572. //
  573. // expected returned ADVERTISE message:
  574. // - copy of client-id
  575. // - server-id
  576. // - IA that includes IAPREFIX
  577. TEST_F(Dhcpv6SrvTest, pdSolicitBasic) {
  578. configurePdPool();
  579. NakedDhcpv6Srv srv(0);
  580. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  581. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  582. sol->addOption(generateIA(D6O_IA_PD, 234, 1500, 3000));
  583. OptionPtr clientid = generateClientId();
  584. sol->addOption(clientid);
  585. // Pass it to the server and get an advertise
  586. Pkt6Ptr reply = srv.processSolicit(sol);
  587. // check if we get response at all
  588. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  589. // check that IA_NA was returned and that there's an address included
  590. boost::shared_ptr<Option6IAPrefix> prefix = checkIA_PD(reply, 234, subnet_->getT1(),
  591. subnet_->getT2());
  592. ASSERT_TRUE(prefix);
  593. // Check that the assigned prefix is indeed from the configured pool
  594. checkIAAddr(prefix, prefix->getAddress(), Lease::TYPE_PD,
  595. subnet_->getPreferred(), subnet_->getValid());
  596. EXPECT_EQ(pd_pool_->getLength(), prefix->getLength());
  597. // check DUIDs
  598. checkServerId(reply, srv.getServerID());
  599. checkClientId(reply, clientid);
  600. }
  601. // This test verifies that incoming SOLICIT can be handled properly, that an
  602. // ADVERTISE is generated, that the response has an address and that address
  603. // really belongs to the configured pool.
  604. //
  605. // This test sends a SOLICIT with IA_NA that contains a valid hint.
  606. //
  607. // constructed very simple SOLICIT message with:
  608. // - client-id option (mandatory)
  609. // - IA option (a request for address, with an address that belongs to the
  610. // configured pool, i.e. is valid as hint)
  611. //
  612. // expected returned ADVERTISE message:
  613. // - copy of client-id
  614. // - server-id
  615. // - IA that includes IAADDR
  616. TEST_F(Dhcpv6SrvTest, SolicitHint) {
  617. NakedDhcpv6Srv srv(0);
  618. // Let's create a SOLICIT
  619. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  620. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  621. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  622. // with a valid hint
  623. IOAddress hint("2001:db8:1:1::dead:beef");
  624. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  625. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  626. ia->addOption(hint_opt);
  627. sol->addOption(ia);
  628. OptionPtr clientid = generateClientId();
  629. sol->addOption(clientid);
  630. // Pass it to the server and get an advertise
  631. Pkt6Ptr reply = srv.processSolicit(sol);
  632. // check if we get response at all
  633. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  634. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  635. ASSERT_TRUE(tmp);
  636. // check that IA_NA was returned and that there's an address included
  637. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  638. subnet_->getT2());
  639. ASSERT_TRUE(addr);
  640. // check that we've got the address we requested
  641. checkIAAddr(addr, hint, Lease::TYPE_NA, subnet_->getPreferred(),
  642. subnet_->getValid());
  643. // check DUIDs
  644. checkServerId(reply, srv.getServerID());
  645. checkClientId(reply, clientid);
  646. }
  647. // This test verifies that incoming SOLICIT can be handled properly, that an
  648. // ADVERTISE is generated, that the response has an address and that address
  649. // really belongs to the configured pool.
  650. //
  651. // This test sends a SOLICIT with IA_NA that contains an invalid hint.
  652. //
  653. // constructed very simple SOLICIT message with:
  654. // - client-id option (mandatory)
  655. // - IA option (a request for address, with an address that does not
  656. // belong to the configured pool, i.e. is valid as hint)
  657. //
  658. // expected returned ADVERTISE message:
  659. // - copy of client-id
  660. // - server-id
  661. // - IA that includes IAADDR
  662. TEST_F(Dhcpv6SrvTest, SolicitInvalidHint) {
  663. NakedDhcpv6Srv srv(0);
  664. // Let's create a SOLICIT
  665. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  666. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  667. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  668. IOAddress hint("2001:db8:1::cafe:babe");
  669. ASSERT_FALSE(subnet_->inPool(Lease::TYPE_NA, hint));
  670. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  671. ia->addOption(hint_opt);
  672. sol->addOption(ia);
  673. OptionPtr clientid = generateClientId();
  674. sol->addOption(clientid);
  675. // Pass it to the server and get an advertise
  676. Pkt6Ptr reply = srv.processSolicit(sol);
  677. // check if we get response at all
  678. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  679. // check that IA_NA was returned and that there's an address included
  680. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  681. subnet_->getT2());
  682. ASSERT_TRUE(addr);
  683. // Check that the assigned address is indeed from the configured pool
  684. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA, subnet_->getPreferred(),
  685. subnet_->getValid());
  686. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr->getAddress()));
  687. // check DUIDs
  688. checkServerId(reply, srv.getServerID());
  689. checkClientId(reply, clientid);
  690. }
  691. /// @todo: Add a test that client sends hint that is in pool, but currently
  692. /// being used by a different client.
  693. // This test checks that the server is offering different addresses to different
  694. // clients in ADVERTISEs. Please note that ADVERTISE is not a guarantee that such
  695. // an address will be assigned. Had the pool was very small and contained only
  696. // 2 addresses, the third client would get the same advertise as the first one
  697. // and this is a correct behavior. It is REQUEST that will fail for the third
  698. // client. ADVERTISE is basically saying "if you send me a request, you will
  699. // probably get an address like this" (there are no guarantees).
  700. TEST_F(Dhcpv6SrvTest, ManySolicits) {
  701. NakedDhcpv6Srv srv(0);
  702. Pkt6Ptr sol1 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  703. Pkt6Ptr sol2 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 2345));
  704. Pkt6Ptr sol3 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 3456));
  705. sol1->setRemoteAddr(IOAddress("fe80::abcd"));
  706. sol2->setRemoteAddr(IOAddress("fe80::1223"));
  707. sol3->setRemoteAddr(IOAddress("fe80::3467"));
  708. sol1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  709. sol2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  710. sol3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  711. // different client-id sizes
  712. OptionPtr clientid1 = generateClientId(12);
  713. OptionPtr clientid2 = generateClientId(14);
  714. OptionPtr clientid3 = generateClientId(16);
  715. sol1->addOption(clientid1);
  716. sol2->addOption(clientid2);
  717. sol3->addOption(clientid3);
  718. // Pass it to the server and get an advertise
  719. Pkt6Ptr reply1 = srv.processSolicit(sol1);
  720. Pkt6Ptr reply2 = srv.processSolicit(sol2);
  721. Pkt6Ptr reply3 = srv.processSolicit(sol3);
  722. // check if we get response at all
  723. checkResponse(reply1, DHCPV6_ADVERTISE, 1234);
  724. checkResponse(reply2, DHCPV6_ADVERTISE, 2345);
  725. checkResponse(reply3, DHCPV6_ADVERTISE, 3456);
  726. // check that IA_NA was returned and that there's an address included
  727. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  728. subnet_->getT2());
  729. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  730. subnet_->getT2());
  731. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  732. subnet_->getT2());
  733. ASSERT_TRUE(addr1);
  734. ASSERT_TRUE(addr2);
  735. ASSERT_TRUE(addr3);
  736. // Check that the assigned address is indeed from the configured pool
  737. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA,
  738. subnet_->getPreferred(), subnet_->getValid());
  739. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA,
  740. subnet_->getPreferred(), subnet_->getValid());
  741. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA,
  742. subnet_->getPreferred(), subnet_->getValid());
  743. // check DUIDs
  744. checkServerId(reply1, srv.getServerID());
  745. checkServerId(reply2, srv.getServerID());
  746. checkServerId(reply3, srv.getServerID());
  747. checkClientId(reply1, clientid1);
  748. checkClientId(reply2, clientid2);
  749. checkClientId(reply3, clientid3);
  750. // Finally check that the addresses offered are different
  751. EXPECT_NE(addr1->getAddress().toText(), addr2->getAddress().toText());
  752. EXPECT_NE(addr2->getAddress().toText(), addr3->getAddress().toText());
  753. EXPECT_NE(addr3->getAddress().toText(), addr1->getAddress().toText());
  754. cout << "Offered address to client1=" << addr1->getAddress().toText() << endl;
  755. cout << "Offered address to client2=" << addr2->getAddress().toText() << endl;
  756. cout << "Offered address to client3=" << addr3->getAddress().toText() << endl;
  757. }
  758. // This test verifies that incoming REQUEST can be handled properly, that a
  759. // REPLY is generated, that the response has an address and that address
  760. // really belongs to the configured pool.
  761. //
  762. // This test sends a REQUEST with IA_NA that contains a valid hint.
  763. //
  764. // constructed very simple REQUEST message with:
  765. // - client-id option (mandatory)
  766. // - IA option (a request for address, with an address that belongs to the
  767. // configured pool, i.e. is valid as hint)
  768. //
  769. // expected returned REPLY message:
  770. // - copy of client-id
  771. // - server-id
  772. // - IA that includes IAADDR
  773. TEST_F(Dhcpv6SrvTest, RequestBasic) {
  774. NakedDhcpv6Srv srv(0);
  775. // Let's create a REQUEST
  776. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  777. req->setRemoteAddr(IOAddress("fe80::abcd"));
  778. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  779. // with a valid hint
  780. IOAddress hint("2001:db8:1:1::dead:beef");
  781. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  782. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  783. ia->addOption(hint_opt);
  784. req->addOption(ia);
  785. OptionPtr clientid = generateClientId();
  786. req->addOption(clientid);
  787. // server-id is mandatory in REQUEST
  788. req->addOption(srv.getServerID());
  789. // Pass it to the server and hope for a REPLY
  790. Pkt6Ptr reply = srv.processRequest(req);
  791. // check if we get response at all
  792. checkResponse(reply, DHCPV6_REPLY, 1234);
  793. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  794. ASSERT_TRUE(tmp);
  795. // check that IA_NA was returned and that there's an address included
  796. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234,
  797. subnet_->getT1(),
  798. subnet_->getT2());
  799. ASSERT_TRUE(addr);
  800. // check that we've got the address we requested
  801. checkIAAddr(addr, hint, Lease::TYPE_NA, subnet_->getPreferred(),
  802. subnet_->getValid());
  803. // check DUIDs
  804. checkServerId(reply, srv.getServerID());
  805. checkClientId(reply, clientid);
  806. // check that the lease is really in the database
  807. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  808. EXPECT_TRUE(l);
  809. LeaseMgrFactory::instance().deleteLease(addr->getAddress());
  810. }
  811. // This test verifies that incoming REQUEST can be handled properly, that a
  812. // REPLY is generated, that the response has a prefix and that prefix
  813. // really belongs to the configured pool.
  814. //
  815. // This test sends a REQUEST with IA_PD that contains a valid hint.
  816. //
  817. // constructed very simple REQUEST message with:
  818. // - client-id option (mandatory)
  819. // - IA option (a request for address, with an address that belongs to the
  820. // configured pool, i.e. is valid as hint)
  821. //
  822. // expected returned REPLY message:
  823. // - copy of client-id
  824. // - server-id
  825. // - IA that includes IAPREFIX
  826. TEST_F(Dhcpv6SrvTest, pdRequestBasic) {
  827. configurePdPool();
  828. NakedDhcpv6Srv srv(0);
  829. // Let's create a REQUEST
  830. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  831. req->setRemoteAddr(IOAddress("fe80::abcd"));
  832. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_PD, 234, 1500, 3000);
  833. // with a valid hint
  834. IOAddress hint("2001:db8:1:2:f::");
  835. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_PD, hint));
  836. OptionPtr hint_opt(new Option6IAPrefix(D6O_IAPREFIX, hint, 64, 300, 500));
  837. ia->addOption(hint_opt);
  838. req->addOption(ia);
  839. OptionPtr clientid = generateClientId();
  840. req->addOption(clientid);
  841. // server-id is mandatory in REQUEST
  842. req->addOption(srv.getServerID());
  843. // Pass it to the server and hope for a REPLY
  844. Pkt6Ptr reply = srv.processRequest(req);
  845. // check if we get response at all
  846. checkResponse(reply, DHCPV6_REPLY, 1234);
  847. OptionPtr tmp = reply->getOption(D6O_IA_PD);
  848. ASSERT_TRUE(tmp);
  849. // check that IA_NA was returned and that there's an address included
  850. boost::shared_ptr<Option6IAPrefix> prf = checkIA_PD(reply, 234,
  851. subnet_->getT1(),
  852. subnet_->getT2());
  853. ASSERT_TRUE(prf);
  854. // check that we've got the address we requested
  855. checkIAAddr(prf, hint, Lease::TYPE_PD, subnet_->getPreferred(),
  856. subnet_->getValid());
  857. EXPECT_EQ(pd_pool_->getLength(), prf->getLength());
  858. // check DUIDs
  859. checkServerId(reply, srv.getServerID());
  860. checkClientId(reply, clientid);
  861. // check that the lease is really in the database
  862. Lease6Ptr l = checkPdLease(duid_, reply->getOption(D6O_IA_PD), prf);
  863. EXPECT_TRUE(l);
  864. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(prf->getAddress()));
  865. }
  866. // This test checks that the server is offering different addresses to different
  867. // clients in REQUEST. Please note that ADVERTISE is not a guarantee that such
  868. // and address will be assigned. Had the pool was very small and contained only
  869. // 2 addresses, the third client would get the same advertise as the first one
  870. // and this is a correct behavior. It is REQUEST that will fail for the third
  871. // client. ADVERTISE is basically saying "if you send me a request, you will
  872. // probably get an address like this" (there are no guarantees).
  873. TEST_F(Dhcpv6SrvTest, ManyRequests) {
  874. NakedDhcpv6Srv srv(0);
  875. ASSERT_TRUE(subnet_);
  876. Pkt6Ptr req1 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  877. Pkt6Ptr req2 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 2345));
  878. Pkt6Ptr req3 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 3456));
  879. req1->setRemoteAddr(IOAddress("fe80::abcd"));
  880. req2->setRemoteAddr(IOAddress("fe80::1223"));
  881. req3->setRemoteAddr(IOAddress("fe80::3467"));
  882. req1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  883. req2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  884. req3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  885. // different client-id sizes
  886. OptionPtr clientid1 = generateClientId(12);
  887. OptionPtr clientid2 = generateClientId(14);
  888. OptionPtr clientid3 = generateClientId(16);
  889. req1->addOption(clientid1);
  890. req2->addOption(clientid2);
  891. req3->addOption(clientid3);
  892. // server-id is mandatory in REQUEST
  893. req1->addOption(srv.getServerID());
  894. req2->addOption(srv.getServerID());
  895. req3->addOption(srv.getServerID());
  896. // Pass it to the server and get an advertise
  897. Pkt6Ptr reply1 = srv.processRequest(req1);
  898. Pkt6Ptr reply2 = srv.processRequest(req2);
  899. Pkt6Ptr reply3 = srv.processRequest(req3);
  900. // check if we get response at all
  901. checkResponse(reply1, DHCPV6_REPLY, 1234);
  902. checkResponse(reply2, DHCPV6_REPLY, 2345);
  903. checkResponse(reply3, DHCPV6_REPLY, 3456);
  904. // check that IA_NA was returned and that there's an address included
  905. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  906. subnet_->getT2());
  907. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  908. subnet_->getT2());
  909. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  910. subnet_->getT2());
  911. ASSERT_TRUE(addr1);
  912. ASSERT_TRUE(addr2);
  913. ASSERT_TRUE(addr3);
  914. // Check that the assigned address is indeed from the configured pool
  915. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA,
  916. subnet_->getPreferred(), subnet_->getValid());
  917. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA,
  918. subnet_->getPreferred(), subnet_->getValid());
  919. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA,
  920. subnet_->getPreferred(), subnet_->getValid());
  921. // check DUIDs
  922. checkServerId(reply1, srv.getServerID());
  923. checkServerId(reply2, srv.getServerID());
  924. checkServerId(reply3, srv.getServerID());
  925. checkClientId(reply1, clientid1);
  926. checkClientId(reply2, clientid2);
  927. checkClientId(reply3, clientid3);
  928. // Finally check that the addresses offered are different
  929. EXPECT_NE(addr1->getAddress().toText(), addr2->getAddress().toText());
  930. EXPECT_NE(addr2->getAddress().toText(), addr3->getAddress().toText());
  931. EXPECT_NE(addr3->getAddress().toText(), addr1->getAddress().toText());
  932. cout << "Assigned address to client1=" << addr1->getAddress().toText() << endl;
  933. cout << "Assigned address to client2=" << addr2->getAddress().toText() << endl;
  934. cout << "Assigned address to client3=" << addr3->getAddress().toText() << endl;
  935. }
  936. // This test verifies that incoming (positive) RENEW can be handled properly, that a
  937. // REPLY is generated, that the response has an address and that address
  938. // really belongs to the configured pool and that lease is actually renewed.
  939. //
  940. // expected:
  941. // - returned REPLY message has copy of client-id
  942. // - returned REPLY message has server-id
  943. // - returned REPLY message has IA that includes IAADDR
  944. // - lease is actually renewed in LeaseMgr
  945. TEST_F(Dhcpv6SrvTest, RenewBasic) {
  946. NakedDhcpv6Srv srv(0);
  947. const IOAddress addr("2001:db8:1:1::cafe:babe");
  948. const uint32_t iaid = 234;
  949. // Generate client-id also duid_
  950. OptionPtr clientid = generateClientId();
  951. // Check that the address we are about to use is indeed in pool
  952. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  953. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  954. // value on purpose. They should be updated during RENEW.
  955. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  956. 501, 502, 503, 504, subnet_->getID(), 0));
  957. lease->cltt_ = 1234;
  958. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  959. // Check that the lease is really in the database
  960. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  961. addr);
  962. ASSERT_TRUE(l);
  963. // Check that T1, T2, preferred, valid and cltt really set and not using
  964. // previous (500, 501, etc.) values
  965. EXPECT_NE(l->t1_, subnet_->getT1());
  966. EXPECT_NE(l->t2_, subnet_->getT2());
  967. EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
  968. EXPECT_NE(l->valid_lft_, subnet_->getValid());
  969. EXPECT_NE(l->cltt_, time(NULL));
  970. // Let's create a RENEW
  971. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  972. req->setRemoteAddr(IOAddress("fe80::abcd"));
  973. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  974. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  975. ia->addOption(renewed_addr_opt);
  976. req->addOption(ia);
  977. req->addOption(clientid);
  978. // Server-id is mandatory in RENEW
  979. req->addOption(srv.getServerID());
  980. // Pass it to the server and hope for a REPLY
  981. Pkt6Ptr reply = srv.processRenew(req);
  982. // Check if we get response at all
  983. checkResponse(reply, DHCPV6_REPLY, 1234);
  984. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  985. ASSERT_TRUE(tmp);
  986. // Check that IA_NA was returned and that there's an address included
  987. boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
  988. subnet_->getT2());
  989. ASSERT_TRUE(addr_opt);
  990. // Check that we've got the address we requested
  991. checkIAAddr(addr_opt, addr, Lease::TYPE_NA, subnet_->getPreferred(),
  992. subnet_->getValid());
  993. // Check DUIDs
  994. checkServerId(reply, srv.getServerID());
  995. checkClientId(reply, clientid);
  996. // Check that the lease is really in the database
  997. l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
  998. ASSERT_TRUE(l);
  999. // Check that T1, T2, preferred, valid and cltt were really updated
  1000. EXPECT_EQ(l->t1_, subnet_->getT1());
  1001. EXPECT_EQ(l->t2_, subnet_->getT2());
  1002. EXPECT_EQ(l->preferred_lft_, subnet_->getPreferred());
  1003. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  1004. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  1005. int32_t cltt = static_cast<int32_t>(l->cltt_);
  1006. int32_t expected = static_cast<int32_t>(time(NULL));
  1007. // equality or difference by 1 between cltt and expected is ok.
  1008. EXPECT_GE(1, abs(cltt - expected));
  1009. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
  1010. }
  1011. // This test verifies that incoming (invalid) RENEW can be handled properly.
  1012. //
  1013. // This test checks 3 scenarios:
  1014. // 1. there is no such lease at all
  1015. // 2. there is such a lease, but it is assigned to a different IAID
  1016. // 3. there is such a lease, but it belongs to a different client
  1017. //
  1018. // expected:
  1019. // - returned REPLY message has copy of client-id
  1020. // - returned REPLY message has server-id
  1021. // - returned REPLY message has IA that includes STATUS-CODE
  1022. // - No lease in LeaseMgr
  1023. TEST_F(Dhcpv6SrvTest, RenewReject) {
  1024. NakedDhcpv6Srv srv(0);
  1025. const IOAddress addr("2001:db8:1:1::dead");
  1026. const uint32_t transid = 1234;
  1027. const uint32_t valid_iaid = 234;
  1028. const uint32_t bogus_iaid = 456;
  1029. // Quick sanity check that the address we're about to use is ok
  1030. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1031. // GenerateClientId() also sets duid_
  1032. OptionPtr clientid = generateClientId();
  1033. // Check that the lease is NOT in the database
  1034. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1035. addr);
  1036. ASSERT_FALSE(l);
  1037. // Let's create a RENEW
  1038. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, transid));
  1039. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1040. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, bogus_iaid, 1500, 3000);
  1041. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1042. ia->addOption(renewed_addr_opt);
  1043. req->addOption(ia);
  1044. req->addOption(clientid);
  1045. // Server-id is mandatory in RENEW
  1046. req->addOption(srv.getServerID());
  1047. // Case 1: No lease known to server
  1048. // Pass it to the server and hope for a REPLY
  1049. Pkt6Ptr reply = srv.processRenew(req);
  1050. // Check if we get response at all
  1051. checkResponse(reply, DHCPV6_REPLY, transid);
  1052. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1053. ASSERT_TRUE(tmp);
  1054. // Check that IA_NA was returned and that there's an address included
  1055. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1056. ASSERT_TRUE(ia);
  1057. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1058. // Check that there is no lease added
  1059. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1060. ASSERT_FALSE(l);
  1061. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  1062. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1063. // value on purpose. They should be updated during RENEW.
  1064. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, valid_iaid,
  1065. 501, 502, 503, 504, subnet_->getID(), 0));
  1066. lease->cltt_ = 123; // Let's use it as an indicator that the lease
  1067. // was NOT updated.
  1068. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1069. // Pass it to the server and hope for a REPLY
  1070. reply = srv.processRenew(req);
  1071. checkResponse(reply, DHCPV6_REPLY, transid);
  1072. tmp = reply->getOption(D6O_IA_NA);
  1073. ASSERT_TRUE(tmp);
  1074. // Check that IA_NA was returned and that there's an address included
  1075. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1076. ASSERT_TRUE(ia);
  1077. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1078. // There is a iaid mis-match, so server should respond that there is
  1079. // no such address to renew.
  1080. // CASE 3: Lease belongs to a client with different client-id
  1081. req->delOption(D6O_CLIENTID);
  1082. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  1083. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  1084. req->addOption(generateClientId(13)); // generate different DUID
  1085. // (with length 13)
  1086. reply = srv.processRenew(req);
  1087. checkResponse(reply, DHCPV6_REPLY, transid);
  1088. tmp = reply->getOption(D6O_IA_NA);
  1089. ASSERT_TRUE(tmp);
  1090. // Check that IA_NA was returned and that there's an address included
  1091. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1092. ASSERT_TRUE(ia);
  1093. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1094. lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1095. ASSERT_TRUE(lease);
  1096. // Verify that the lease was not updated.
  1097. EXPECT_EQ(123, lease->cltt_);
  1098. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  1099. }
  1100. // This test verifies that incoming (positive) RELEASE can be handled properly,
  1101. // that a REPLY is generated, that the response has status code and that the
  1102. // lease is indeed removed from the database.
  1103. //
  1104. // expected:
  1105. // - returned REPLY message has copy of client-id
  1106. // - returned REPLY message has server-id
  1107. // - returned REPLY message has IA that does not include an IAADDR
  1108. // - lease is actually removed from LeaseMgr
  1109. TEST_F(Dhcpv6SrvTest, ReleaseBasic) {
  1110. NakedDhcpv6Srv srv(0);
  1111. const IOAddress addr("2001:db8:1:1::cafe:babe");
  1112. const uint32_t iaid = 234;
  1113. // Generate client-id also duid_
  1114. OptionPtr clientid = generateClientId();
  1115. // Check that the address we are about to use is indeed in pool
  1116. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1117. // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
  1118. // value on purpose. They should be updated during RENEW.
  1119. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
  1120. 501, 502, 503, 504, subnet_->getID(), 0));
  1121. lease->cltt_ = 1234;
  1122. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1123. // Check that the lease is really in the database
  1124. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1125. addr);
  1126. ASSERT_TRUE(l);
  1127. // Let's create a RELEASE
  1128. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  1129. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1130. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  1131. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1132. ia->addOption(released_addr_opt);
  1133. req->addOption(ia);
  1134. req->addOption(clientid);
  1135. // Server-id is mandatory in RELEASE
  1136. req->addOption(srv.getServerID());
  1137. // Pass it to the server and hope for a REPLY
  1138. Pkt6Ptr reply = srv.processRelease(req);
  1139. // Check if we get response at all
  1140. checkResponse(reply, DHCPV6_REPLY, 1234);
  1141. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1142. ASSERT_TRUE(tmp);
  1143. // Check that IA_NA was returned and that there's an address included
  1144. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1145. checkIA_NAStatusCode(ia, STATUS_Success);
  1146. checkMsgStatusCode(reply, STATUS_Success);
  1147. // There should be no address returned in RELEASE (see RFC3315, 18.2.6)
  1148. EXPECT_FALSE(tmp->getOption(D6O_IAADDR));
  1149. // Check DUIDs
  1150. checkServerId(reply, srv.getServerID());
  1151. checkClientId(reply, clientid);
  1152. // Check that the lease is really gone in the database
  1153. // get lease by address
  1154. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1155. ASSERT_FALSE(l);
  1156. // get lease by subnetid/duid/iaid combination
  1157. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, *duid_, iaid,
  1158. subnet_->getID());
  1159. ASSERT_FALSE(l);
  1160. }
  1161. // This test verifies that incoming (invalid) RELEASE can be handled properly.
  1162. //
  1163. // This test checks 3 scenarios:
  1164. // 1. there is no such lease at all
  1165. // 2. there is such a lease, but it is assigned to a different IAID
  1166. // 3. there is such a lease, but it belongs to a different client
  1167. //
  1168. // expected:
  1169. // - returned REPLY message has copy of client-id
  1170. // - returned REPLY message has server-id
  1171. // - returned REPLY message has IA that includes STATUS-CODE
  1172. // - No lease in LeaseMgr
  1173. TEST_F(Dhcpv6SrvTest, ReleaseReject) {
  1174. NakedDhcpv6Srv srv(0);
  1175. const IOAddress addr("2001:db8:1:1::dead");
  1176. const uint32_t transid = 1234;
  1177. const uint32_t valid_iaid = 234;
  1178. const uint32_t bogus_iaid = 456;
  1179. // Quick sanity check that the address we're about to use is ok
  1180. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
  1181. // GenerateClientId() also sets duid_
  1182. OptionPtr clientid = generateClientId();
  1183. // Check that the lease is NOT in the database
  1184. Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1185. addr);
  1186. ASSERT_FALSE(l);
  1187. // Let's create a RELEASE
  1188. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, transid));
  1189. req->setRemoteAddr(IOAddress("fe80::abcd"));
  1190. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, bogus_iaid, 1500, 3000);
  1191. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  1192. ia->addOption(released_addr_opt);
  1193. req->addOption(ia);
  1194. req->addOption(clientid);
  1195. // Server-id is mandatory in RENEW
  1196. req->addOption(srv.getServerID());
  1197. // Case 1: No lease known to server
  1198. SCOPED_TRACE("CASE 1: No lease known to server");
  1199. // Pass it to the server and hope for a REPLY
  1200. Pkt6Ptr reply = srv.processRelease(req);
  1201. // Check if we get response at all
  1202. checkResponse(reply, DHCPV6_REPLY, transid);
  1203. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  1204. ASSERT_TRUE(tmp);
  1205. // Check that IA_NA was returned and that there's an address included
  1206. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1207. ASSERT_TRUE(ia);
  1208. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1209. checkMsgStatusCode(reply, STATUS_NoBinding);
  1210. // Check that the lease is not there
  1211. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1212. ASSERT_FALSE(l);
  1213. // CASE 2: Lease is known and belongs to this client, but to a different IAID
  1214. SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but to a different IAID");
  1215. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, valid_iaid,
  1216. 501, 502, 503, 504, subnet_->getID(), 0));
  1217. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1218. // Pass it to the server and hope for a REPLY
  1219. reply = srv.processRelease(req);
  1220. checkResponse(reply, DHCPV6_REPLY, transid);
  1221. tmp = reply->getOption(D6O_IA_NA);
  1222. ASSERT_TRUE(tmp);
  1223. // Check that IA_NA was returned and that there's an address included
  1224. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1225. ASSERT_TRUE(ia);
  1226. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1227. checkMsgStatusCode(reply, STATUS_NoBinding);
  1228. // Check that the lease is still there
  1229. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1230. ASSERT_TRUE(l);
  1231. // CASE 3: Lease belongs to a client with different client-id
  1232. SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
  1233. req->delOption(D6O_CLIENTID);
  1234. ia = boost::dynamic_pointer_cast<Option6IA>(req->getOption(D6O_IA_NA));
  1235. ia->setIAID(valid_iaid); // Now iaid in renew matches that in leasemgr
  1236. req->addOption(generateClientId(13)); // generate different DUID
  1237. // (with length 13)
  1238. reply = srv.processRelease(req);
  1239. checkResponse(reply, DHCPV6_REPLY, transid);
  1240. tmp = reply->getOption(D6O_IA_NA);
  1241. ASSERT_TRUE(tmp);
  1242. // Check that IA_NA was returned and that there's an address included
  1243. ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
  1244. ASSERT_TRUE(ia);
  1245. checkIA_NAStatusCode(ia, STATUS_NoBinding);
  1246. checkMsgStatusCode(reply, STATUS_NoBinding);
  1247. // Check that the lease is still there
  1248. l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
  1249. ASSERT_TRUE(l);
  1250. // Finally, let's cleanup the database
  1251. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  1252. }
  1253. // This test verifies if the status code option is generated properly.
  1254. TEST_F(Dhcpv6SrvTest, StatusCode) {
  1255. NakedDhcpv6Srv srv(0);
  1256. // a dummy content for client-id
  1257. uint8_t expected[] = {
  1258. 0x0, 0xD, // option code = 13
  1259. 0x0, 0x7, // option length = 7
  1260. 0x0, 0x3, // status code = 3
  1261. 0x41, 0x42, 0x43, 0x44, 0x45 // string value ABCDE
  1262. };
  1263. // Create the option.
  1264. OptionPtr status = srv.createStatusCode(3, "ABCDE");
  1265. // Allocate an output buffer. We will store the option
  1266. // in wire format here.
  1267. OutputBuffer buf(sizeof(expected));
  1268. // Prepare the wire format.
  1269. ASSERT_NO_THROW(status->pack(buf));
  1270. // Check that the option buffer has valid length (option header + data).
  1271. ASSERT_EQ(sizeof(expected), buf.getLength());
  1272. // Verify the contents of the option.
  1273. EXPECT_EQ(0, memcmp(expected, buf.getData(), sizeof(expected)));
  1274. }
  1275. // This test verifies if the sanityCheck() really checks options presence.
  1276. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  1277. NakedDhcpv6Srv srv(0);
  1278. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1279. // Set link-local sender address, so appropriate subnet can be
  1280. // selected for this packet.
  1281. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1282. // client-id is optional for information-request, so
  1283. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  1284. // empty packet, no client-id, no server-id
  1285. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  1286. RFCViolation);
  1287. // This doesn't make much sense, but let's check it for completeness
  1288. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  1289. OptionPtr clientid = generateClientId();
  1290. pkt->addOption(clientid);
  1291. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  1292. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  1293. pkt->addOption(srv.getServerID());
  1294. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  1295. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  1296. // sane section ends here, let's do some negative tests as well
  1297. pkt->addOption(clientid);
  1298. pkt->addOption(clientid);
  1299. // with more than one client-id it should throw, no matter what
  1300. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  1301. RFCViolation);
  1302. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  1303. RFCViolation);
  1304. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  1305. RFCViolation);
  1306. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  1307. RFCViolation);
  1308. pkt->delOption(D6O_CLIENTID);
  1309. pkt->delOption(D6O_CLIENTID);
  1310. // again we have only one client-id
  1311. // let's try different type of insanity - several server-ids
  1312. pkt->addOption(srv.getServerID());
  1313. pkt->addOption(srv.getServerID());
  1314. // with more than one server-id it should throw, no matter what
  1315. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  1316. RFCViolation);
  1317. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  1318. RFCViolation);
  1319. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  1320. RFCViolation);
  1321. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  1322. RFCViolation);
  1323. }
  1324. // This test verifies if selectSubnet() selects proper subnet for a given
  1325. // source address.
  1326. TEST_F(Dhcpv6SrvTest, selectSubnetAddr) {
  1327. NakedDhcpv6Srv srv(0);
  1328. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1329. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1330. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1331. // CASE 1: We have only one subnet defined and we received local traffic.
  1332. // The only available subnet should be selected
  1333. CfgMgr::instance().deleteSubnets6();
  1334. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1335. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1336. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1337. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1338. EXPECT_EQ(selected, subnet1);
  1339. // CASE 2: We have only one subnet defined and we received relayed traffic.
  1340. // We should NOT select it.
  1341. // Identical steps as in case 1, but repeated for clarity
  1342. CfgMgr::instance().deleteSubnets6();
  1343. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1344. pkt->setRemoteAddr(IOAddress("2001:db8:abcd::2345"));
  1345. selected = srv.selectSubnet(pkt);
  1346. EXPECT_FALSE(selected);
  1347. // CASE 3: We have three subnets defined and we received local traffic.
  1348. // Nothing should be selected.
  1349. CfgMgr::instance().deleteSubnets6();
  1350. CfgMgr::instance().addSubnet6(subnet1);
  1351. CfgMgr::instance().addSubnet6(subnet2);
  1352. CfgMgr::instance().addSubnet6(subnet3);
  1353. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1354. selected = srv.selectSubnet(pkt);
  1355. EXPECT_FALSE(selected);
  1356. // CASE 4: We have three subnets defined and we received relayed traffic
  1357. // that came out of subnet 2. We should select subnet2 then
  1358. CfgMgr::instance().deleteSubnets6();
  1359. CfgMgr::instance().addSubnet6(subnet1);
  1360. CfgMgr::instance().addSubnet6(subnet2);
  1361. CfgMgr::instance().addSubnet6(subnet3);
  1362. pkt->setRemoteAddr(IOAddress("2001:db8:2::baca"));
  1363. selected = srv.selectSubnet(pkt);
  1364. EXPECT_EQ(selected, subnet2);
  1365. // CASE 5: We have three subnets defined and we received relayed traffic
  1366. // that came out of undefined subnet. We should select nothing
  1367. CfgMgr::instance().deleteSubnets6();
  1368. CfgMgr::instance().addSubnet6(subnet1);
  1369. CfgMgr::instance().addSubnet6(subnet2);
  1370. CfgMgr::instance().addSubnet6(subnet3);
  1371. pkt->setRemoteAddr(IOAddress("2001:db8:4::baca"));
  1372. selected = srv.selectSubnet(pkt);
  1373. EXPECT_FALSE(selected);
  1374. }
  1375. // This test verifies if selectSubnet() selects proper subnet for a given
  1376. // network interface name.
  1377. TEST_F(Dhcpv6SrvTest, selectSubnetIface) {
  1378. NakedDhcpv6Srv srv(0);
  1379. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1380. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1381. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1382. subnet1->setIface("eth0");
  1383. subnet3->setIface("wifi1");
  1384. // CASE 1: We have only one subnet defined and it is available via eth0.
  1385. // Packet came from eth0. The only available subnet should be selected
  1386. CfgMgr::instance().deleteSubnets6();
  1387. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1388. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1389. pkt->setIface("eth0");
  1390. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1391. EXPECT_EQ(selected, subnet1);
  1392. // CASE 2: We have only one subnet defined and it is available via eth0.
  1393. // Packet came from eth1. We should not select it
  1394. CfgMgr::instance().deleteSubnets6();
  1395. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1396. pkt->setIface("eth1");
  1397. selected = srv.selectSubnet(pkt);
  1398. EXPECT_FALSE(selected);
  1399. // CASE 3: We have only 3 subnets defined, one over eth0, one remote and
  1400. // one over wifi1.
  1401. // Packet came from eth1. We should not select it
  1402. CfgMgr::instance().deleteSubnets6();
  1403. CfgMgr::instance().addSubnet6(subnet1);
  1404. CfgMgr::instance().addSubnet6(subnet2);
  1405. CfgMgr::instance().addSubnet6(subnet3);
  1406. pkt->setIface("eth0");
  1407. EXPECT_EQ(subnet1, srv.selectSubnet(pkt));
  1408. pkt->setIface("eth3"); // no such interface
  1409. EXPECT_EQ(Subnet6Ptr(), srv.selectSubnet(pkt)); // nothing selected
  1410. pkt->setIface("wifi1");
  1411. EXPECT_EQ(subnet3, srv.selectSubnet(pkt));
  1412. }
  1413. // This test verifies if selectSubnet() selects proper subnet for a given
  1414. // linkaddr in RELAY-FORW message
  1415. TEST_F(Dhcpv6SrvTest, selectSubnetRelayLinkaddr) {
  1416. NakedDhcpv6Srv srv(0);
  1417. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1418. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1419. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1420. Pkt6::RelayInfo relay;
  1421. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1422. relay.peeraddr_ = IOAddress("fe80::1");
  1423. // CASE 1: We have only one subnet defined and we received relayed traffic.
  1424. // The only available subnet should NOT be selected.
  1425. CfgMgr::instance().deleteSubnets6();
  1426. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1427. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1428. pkt->relay_info_.push_back(relay);
  1429. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1430. EXPECT_FALSE(selected);
  1431. // CASE 2: We have three subnets defined and we received relayed traffic.
  1432. // Nothing should be selected.
  1433. CfgMgr::instance().deleteSubnets6();
  1434. CfgMgr::instance().addSubnet6(subnet1);
  1435. CfgMgr::instance().addSubnet6(subnet2);
  1436. CfgMgr::instance().addSubnet6(subnet3);
  1437. selected = srv.selectSubnet(pkt);
  1438. EXPECT_EQ(selected, subnet2);
  1439. // CASE 3: We have three subnets defined and we received relayed traffic
  1440. // that came out of subnet 2. We should select subnet2 then
  1441. CfgMgr::instance().deleteSubnets6();
  1442. CfgMgr::instance().addSubnet6(subnet1);
  1443. CfgMgr::instance().addSubnet6(subnet2);
  1444. CfgMgr::instance().addSubnet6(subnet3);
  1445. // Source of the packet should have no meaning. Selection is based
  1446. // on linkaddr field in the relay
  1447. pkt->setRemoteAddr(IOAddress("2001:db8:1::baca"));
  1448. selected = srv.selectSubnet(pkt);
  1449. EXPECT_EQ(selected, subnet2);
  1450. // CASE 4: We have three subnets defined and we received relayed traffic
  1451. // that came out of undefined subnet. We should select nothing
  1452. CfgMgr::instance().deleteSubnets6();
  1453. CfgMgr::instance().addSubnet6(subnet1);
  1454. CfgMgr::instance().addSubnet6(subnet2);
  1455. CfgMgr::instance().addSubnet6(subnet3);
  1456. pkt->relay_info_.clear();
  1457. relay.linkaddr_ = IOAddress("2001:db8:4::1234");
  1458. pkt->relay_info_.push_back(relay);
  1459. selected = srv.selectSubnet(pkt);
  1460. EXPECT_FALSE(selected);
  1461. }
  1462. // This test verifies if selectSubnet() selects proper subnet for a given
  1463. // interface-id option
  1464. TEST_F(Dhcpv6SrvTest, selectSubnetRelayInterfaceId) {
  1465. NakedDhcpv6Srv srv(0);
  1466. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1467. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1468. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1469. subnet1->setInterfaceId(generateInterfaceId("relay1"));
  1470. subnet2->setInterfaceId(generateInterfaceId("relay2"));
  1471. // CASE 1: We have only one subnet defined and it is for interface-id "relay1"
  1472. // Packet came with interface-id "relay2". We should not select subnet1
  1473. CfgMgr::instance().deleteSubnets6();
  1474. CfgMgr::instance().addSubnet6(subnet1); // just a single subnet
  1475. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1476. Pkt6::RelayInfo relay;
  1477. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1478. relay.peeraddr_ = IOAddress("fe80::1");
  1479. OptionPtr opt = generateInterfaceId("relay2");
  1480. relay.options_.insert(make_pair(opt->getType(), opt));
  1481. pkt->relay_info_.push_back(relay);
  1482. // There is only one subnet configured and we are outside of that subnet
  1483. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1484. EXPECT_FALSE(selected);
  1485. // CASE 2: We have only one subnet defined and it is for interface-id "relay2"
  1486. // Packet came with interface-id "relay2". We should select it
  1487. CfgMgr::instance().deleteSubnets6();
  1488. CfgMgr::instance().addSubnet6(subnet2); // just a single subnet
  1489. selected = srv.selectSubnet(pkt);
  1490. EXPECT_EQ(selected, subnet2);
  1491. // CASE 3: We have only 3 subnets defined: one remote for interface-id "relay1",
  1492. // one remote for interface-id "relay2" and third local
  1493. // packet comes with interface-id "relay2". We should select subnet2
  1494. CfgMgr::instance().deleteSubnets6();
  1495. CfgMgr::instance().addSubnet6(subnet1);
  1496. CfgMgr::instance().addSubnet6(subnet2);
  1497. CfgMgr::instance().addSubnet6(subnet3);
  1498. EXPECT_EQ(subnet2, srv.selectSubnet(pkt));
  1499. }
  1500. // This test verifies if the server-id disk operations (read, write) are
  1501. // working properly.
  1502. TEST_F(Dhcpv6SrvTest, ServerID) {
  1503. NakedDhcpv6Srv srv(0);
  1504. string duid1_text = "01:ff:02:03:06:80:90:ab:cd:ef";
  1505. uint8_t duid1[] = { 0x01, 0xff, 2, 3, 6, 0x80, 0x90, 0xab, 0xcd, 0xef };
  1506. OptionBuffer expected_duid1(duid1, duid1 + sizeof(duid1));
  1507. fstream file1(DUID_FILE, ios::out | ios::trunc);
  1508. file1 << duid1_text;
  1509. file1.close();
  1510. // Test reading from a file
  1511. EXPECT_TRUE(srv.loadServerID(DUID_FILE));
  1512. ASSERT_TRUE(srv.getServerID());
  1513. ASSERT_EQ(sizeof(duid1) + Option::OPTION6_HDR_LEN, srv.getServerID()->len());
  1514. ASSERT_TRUE(expected_duid1 == srv.getServerID()->getData());
  1515. // Now test writing to a file
  1516. EXPECT_EQ(0, unlink(DUID_FILE));
  1517. EXPECT_NO_THROW(srv.writeServerID(DUID_FILE));
  1518. fstream file2(DUID_FILE, ios::in);
  1519. ASSERT_TRUE(file2.good());
  1520. string text;
  1521. file2 >> text;
  1522. file2.close();
  1523. EXPECT_EQ(duid1_text, text);
  1524. }
  1525. // A set of tests verifying server's behaviour when it receives the DHCPv6
  1526. // Client Fqdn Option.
  1527. // @todo: Extend these tests once appropriate configuration parameters are
  1528. // implemented (ticket #3034).
  1529. // Test server's response when client requests that server performs AAAA update.
  1530. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdate) {
  1531. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S,
  1532. "myhost.example.com",
  1533. Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
  1534. "myhost.example.com.");
  1535. }
  1536. // Test server's response when client provides partial domain-name and requests
  1537. // that server performs AAAA update.
  1538. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdatePartialName) {
  1539. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S, "myhost",
  1540. Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
  1541. "myhost.example.com.");
  1542. }
  1543. // Test server's response when client provides empty domain-name and requests
  1544. // that server performs AAAA update.
  1545. TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdateNoName) {
  1546. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_S, "",
  1547. Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
  1548. "myhost.example.com.");
  1549. }
  1550. // Test server's response when client requests no DNS update.
  1551. TEST_F(FqdnDhcpv6SrvTest, noUpdate) {
  1552. testFqdn(DHCPV6_SOLICIT, true, Option6ClientFqdn::FLAG_N,
  1553. "myhost.example.com",
  1554. Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_N,
  1555. "myhost.example.com.");
  1556. }
  1557. // Test server's response when client requests that server delegates the AAAA
  1558. // update to the client and this delegation is not allowed.
  1559. TEST_F(FqdnDhcpv6SrvTest, clientAAAAUpdateNotAllowed) {
  1560. testFqdn(DHCPV6_SOLICIT, true, 0, "myhost.example.com.",
  1561. Option6ClientFqdn::FULL,
  1562. Option6ClientFqdn::FLAG_S | Option6ClientFqdn::FLAG_O,
  1563. "myhost.example.com.");
  1564. }
  1565. // Test that exception is thrown if supplied NULL answer packet when
  1566. // creating NameChangeRequests.
  1567. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAnswer) {
  1568. NakedDhcpv6Srv srv(0);
  1569. Pkt6Ptr answer;
  1570. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1571. "myhost.example.com",
  1572. Option6ClientFqdn::FULL);
  1573. EXPECT_THROW(srv.createNameChangeRequests(answer, fqdn),
  1574. isc::Unexpected);
  1575. }
  1576. // Test that exception is thrown if supplied answer from the server
  1577. // contains no DUID when creating NameChangeRequests.
  1578. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoDUID) {
  1579. NakedDhcpv6Srv srv(0);
  1580. Pkt6Ptr answer = Pkt6Ptr(new Pkt6(DHCPV6_REPLY, 1234));
  1581. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1582. "myhost.example.com",
  1583. Option6ClientFqdn::FULL);
  1584. EXPECT_THROW(srv.createNameChangeRequests(answer, fqdn),
  1585. isc::Unexpected);
  1586. }
  1587. // Test no NameChangeRequests are added if FQDN option is NULL.
  1588. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoFQDN) {
  1589. NakedDhcpv6Srv srv(0);
  1590. // Create Reply message with Client Id and Server id.
  1591. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1592. // Pass NULL FQDN option. No NameChangeRequests should be created.
  1593. Option6ClientFqdnPtr fqdn;
  1594. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1595. // There should be no new NameChangeRequests.
  1596. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1597. }
  1598. // Test that NameChangeRequests are not generated if an answer message
  1599. // contains no addresses.
  1600. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAddr) {
  1601. NakedDhcpv6Srv srv(0);
  1602. // Create Reply message with Client Id and Server id.
  1603. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1604. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1605. "myhost.example.com",
  1606. Option6ClientFqdn::FULL);
  1607. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1608. // We didn't add any IAs, so there should be no NameChangeRequests in th
  1609. // queue.
  1610. ASSERT_TRUE(srv.name_change_reqs_.empty());
  1611. }
  1612. // Test that a number of NameChangeRequests is created as a result of
  1613. // processing the answer message which holds 3 IAs and when FQDN is
  1614. // specified.
  1615. TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequests) {
  1616. NakedDhcpv6Srv srv(0);
  1617. // Create Reply message with Client Id and Server id.
  1618. Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY, srv);
  1619. // Create three IAs, each having different address.
  1620. addIA(1234, IOAddress("2001:db8:1::1"), answer);
  1621. addIA(2345, IOAddress("2001:db8:1::2"), answer);
  1622. addIA(3456, IOAddress("2001:db8:1::3"), answer);
  1623. // Use domain name in upper case. It should be converted to lower-case
  1624. // before DHCID is calculated. So, we should get the same result as if
  1625. // we typed domain name in lower-case.
  1626. Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
  1627. "MYHOST.EXAMPLE.COM",
  1628. Option6ClientFqdn::FULL);
  1629. // Create NameChangeRequests. Since we have added 3 IAs, it should
  1630. // result in generation of 3 distinct NameChangeRequests.
  1631. ASSERT_NO_THROW(srv.createNameChangeRequests(answer, fqdn));
  1632. ASSERT_EQ(3, srv.name_change_reqs_.size());
  1633. // Verify that NameChangeRequests are correct. Each call to the
  1634. // verifyNameChangeRequest will pop verified request from the queue.
  1635. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1636. "2001:db8:1::1",
  1637. "000201415AA33D1187D148275136FA30300478"
  1638. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1639. 0, 500);
  1640. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1641. "2001:db8:1::2",
  1642. "000201415AA33D1187D148275136FA30300478"
  1643. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1644. 0, 500);
  1645. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1646. "2001:db8:1::3",
  1647. "000201415AA33D1187D148275136FA30300478"
  1648. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1649. 0, 500);
  1650. }
  1651. // Test creation of the NameChangeRequest to remove both forward and reverse
  1652. // mapping for the given lease.
  1653. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestFwdRev) {
  1654. NakedDhcpv6Srv srv(0);
  1655. lease_->fqdn_fwd_ = true;
  1656. lease_->fqdn_rev_ = true;
  1657. // Part of the domain name is in upper case, to test that it gets converted
  1658. // to lower case before DHCID is computed. So, we should get the same DHCID
  1659. // as if we typed domain-name in lower case.
  1660. lease_->hostname_ = "MYHOST.example.com.";
  1661. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1662. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1663. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1664. "2001:db8:1::1",
  1665. "000201415AA33D1187D148275136FA30300478"
  1666. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1667. 0, 502);
  1668. }
  1669. // Test creation of the NameChangeRequest to remove reverse mapping for the
  1670. // given lease.
  1671. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestRev) {
  1672. NakedDhcpv6Srv srv(0);
  1673. lease_->fqdn_fwd_ = false;
  1674. lease_->fqdn_rev_ = true;
  1675. lease_->hostname_ = "myhost.example.com.";
  1676. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1677. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1678. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, false,
  1679. "2001:db8:1::1",
  1680. "000201415AA33D1187D148275136FA30300478"
  1681. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1682. 0, 502);
  1683. }
  1684. // Test that NameChangeRequest to remove DNS records is not generated when
  1685. // neither forward nor reverse DNS update has been performed for a lease.
  1686. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoUpdate) {
  1687. NakedDhcpv6Srv srv(0);
  1688. lease_->fqdn_fwd_ = false;
  1689. lease_->fqdn_rev_ = false;
  1690. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1691. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1692. }
  1693. // Test that NameChangeRequest is not generated if the hostname hasn't been
  1694. // specified for a lease for which forward and reverse mapping has been set.
  1695. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoHostname) {
  1696. NakedDhcpv6Srv srv(0);
  1697. lease_->fqdn_fwd_ = true;
  1698. lease_->fqdn_rev_ = true;
  1699. lease_->hostname_ = "";
  1700. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1701. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1702. }
  1703. // Test that NameChangeRequest is not generated if the invalid hostname has
  1704. // been specified for a lease for which forward and reverse mapping has been
  1705. // set.
  1706. TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestWrongHostname) {
  1707. NakedDhcpv6Srv srv(0);
  1708. lease_->fqdn_fwd_ = true;
  1709. lease_->fqdn_rev_ = true;
  1710. lease_->hostname_ = "myhost..example.com.";
  1711. ASSERT_NO_THROW(srv.createRemovalNameChangeRequest(lease_));
  1712. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1713. }
  1714. // Test that Advertise message generated in a response to the Solicit will
  1715. // not result in generation if the NameChangeRequests.
  1716. TEST_F(FqdnDhcpv6SrvTest, processSolicit) {
  1717. NakedDhcpv6Srv srv(0);
  1718. // Create a Solicit message with FQDN option and generate server's
  1719. // response using processSolicit function.
  1720. testProcessMessage(DHCPV6_SOLICIT, "myhost.example.com", srv);
  1721. EXPECT_TRUE(srv.name_change_reqs_.empty());
  1722. }
  1723. // Test that client may send two requests, each carrying FQDN option with
  1724. // a different domain-name. Server should use existing lease for the second
  1725. // request but modify the DNS entries for the lease according to the contents
  1726. // of the FQDN sent in the second request.
  1727. TEST_F(FqdnDhcpv6SrvTest, processTwoRequests) {
  1728. NakedDhcpv6Srv srv(0);
  1729. // Create a Request message with FQDN option and generate server's
  1730. // response using processRequest function. This will result in the
  1731. // creation of a new lease and the appropriate NameChangeRequest
  1732. // to add both reverse and forward mapping to DNS.
  1733. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1734. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1735. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1736. "2001:db8:1:1::dead:beef",
  1737. "000201415AA33D1187D148275136FA30300478"
  1738. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1739. 0, 4000);
  1740. // Client may send another request message with a new domain-name. In this
  1741. // case the same lease will be returned. The existing DNS entry needs to
  1742. // be replaced with a new one. Server should determine that the different
  1743. // FQDN has been already added to the DNS. As a result, the old DNS
  1744. // entries should be removed and the entries for the new domain-name
  1745. // should be added. Therefore, we expect two NameChangeRequests. One to
  1746. // remove the existing entries, one to add new entries.
  1747. testProcessMessage(DHCPV6_REQUEST, "otherhost.example.com", srv);
  1748. ASSERT_EQ(2, srv.name_change_reqs_.size());
  1749. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1750. "2001:db8:1:1::dead:beef",
  1751. "000201415AA33D1187D148275136FA30300478"
  1752. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1753. 0, 4000);
  1754. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1755. "2001:db8:1:1::dead:beef",
  1756. "000201D422AA463306223D269B6CB7AFE7AAD265FC"
  1757. "EA97F93623019B2E0D14E5323D5A",
  1758. 0, 4000);
  1759. }
  1760. // Test that client may send Request followed by the Renew, both holding
  1761. // FQDN options, but each option holding different domain-name. The Renew
  1762. // should result in generation of the two NameChangeRequests, one to remove
  1763. // DNS entry added previously when Request was processed, another one to
  1764. // add a new entry for the FQDN held in the Renew.
  1765. TEST_F(FqdnDhcpv6SrvTest, processRequestRenew) {
  1766. NakedDhcpv6Srv srv(0);
  1767. // Create a Request message with FQDN option and generate server's
  1768. // response using processRequest function. This will result in the
  1769. // creation of a new lease and the appropriate NameChangeRequest
  1770. // to add both reverse and forward mapping to DNS.
  1771. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1772. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1773. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1774. "2001:db8:1:1::dead:beef",
  1775. "000201415AA33D1187D148275136FA30300478"
  1776. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1777. 0, 4000);
  1778. // Client may send Renew message with a new domain-name. In this
  1779. // case the same lease will be returned. The existing DNS entry needs to
  1780. // be replaced with a new one. Server should determine that the different
  1781. // FQDN has been already added to the DNS. As a result, the old DNS
  1782. // entries should be removed and the entries for the new domain-name
  1783. // should be added. Therefore, we expect two NameChangeRequests. One to
  1784. // remove the existing entries, one to add new entries.
  1785. testProcessMessage(DHCPV6_RENEW, "otherhost.example.com", srv);
  1786. ASSERT_EQ(2, srv.name_change_reqs_.size());
  1787. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1788. "2001:db8:1:1::dead:beef",
  1789. "000201415AA33D1187D148275136FA30300478"
  1790. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1791. 0, 4000);
  1792. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1793. "2001:db8:1:1::dead:beef",
  1794. "000201D422AA463306223D269B6CB7AFE7AAD265FC"
  1795. "EA97F93623019B2E0D14E5323D5A",
  1796. 0, 4000);
  1797. }
  1798. TEST_F(FqdnDhcpv6SrvTest, processRequestRelease) {
  1799. NakedDhcpv6Srv srv(0);
  1800. // Create a Request message with FQDN option and generate server's
  1801. // response using processRequest function. This will result in the
  1802. // creation of a new lease and the appropriate NameChangeRequest
  1803. // to add both reverse and forward mapping to DNS.
  1804. testProcessMessage(DHCPV6_REQUEST, "myhost.example.com", srv);
  1805. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1806. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_ADD, true, true,
  1807. "2001:db8:1:1::dead:beef",
  1808. "000201415AA33D1187D148275136FA30300478"
  1809. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1810. 0, 4000);
  1811. // Client may send Release message. In this case the lease should be
  1812. // removed and all existing DNS entries for this lease should be
  1813. // also removed. Therefore, we expect that single NameChangeRequest to
  1814. // remove DNS entries is generated.
  1815. testProcessMessage(DHCPV6_RELEASE, "otherhost.example.com", srv);
  1816. ASSERT_EQ(1, srv.name_change_reqs_.size());
  1817. verifyNameChangeRequest(srv, isc::dhcp_ddns::CHG_REMOVE, true, true,
  1818. "2001:db8:1:1::dead:beef",
  1819. "000201415AA33D1187D148275136FA30300478"
  1820. "FAAAA3EBD29826B5C907B2C9268A6F52",
  1821. 0, 4000);
  1822. }
  1823. // Checks if server responses are sent to the proper port.
  1824. TEST_F(Dhcpv6SrvTest, portsDirectTraffic) {
  1825. NakedDhcpv6Srv srv(0);
  1826. // Let's create a simple SOLICIT
  1827. Pkt6Ptr sol = captureSimpleSolicit();
  1828. // Simulate that we have received that traffic
  1829. srv.fakeReceive(sol);
  1830. // Server will now process to run its normal loop, but instead of calling
  1831. // IfaceMgr::receive6(), it will read all packets from the list set by
  1832. // fakeReceive()
  1833. srv.run();
  1834. // Get Advertise...
  1835. ASSERT_FALSE(srv.fake_sent_.empty());
  1836. Pkt6Ptr adv = srv.fake_sent_.front();
  1837. ASSERT_TRUE(adv);
  1838. // This is sent back to client directly, should be port 546
  1839. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1840. }
  1841. // Checks if server responses are sent to the proper port.
  1842. TEST_F(Dhcpv6SrvTest, portsRelayedTraffic) {
  1843. NakedDhcpv6Srv srv(0);
  1844. // Let's create a simple SOLICIT
  1845. Pkt6Ptr sol = captureRelayedSolicit();
  1846. // Simulate that we have received that traffic
  1847. srv.fakeReceive(sol);
  1848. // Server will now process to run its normal loop, but instead of calling
  1849. // IfaceMgr::receive6(), it will read all packets from the list set by
  1850. // fakeReceive()
  1851. srv.run();
  1852. // Get Advertise...
  1853. ASSERT_FALSE(srv.fake_sent_.empty());
  1854. Pkt6Ptr adv = srv.fake_sent_.front();
  1855. ASSERT_TRUE(adv);
  1856. // This is sent back to relay, so port is 547
  1857. EXPECT_EQ(DHCP6_SERVER_PORT, adv->getRemotePort());
  1858. }
  1859. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1860. // @todo Uncomment this test as part of #3180 work.
  1861. // Kea code currently fails to handle docsis traffic.
  1862. TEST_F(Dhcpv6SrvTest, DISABLED_docsisTraffic) {
  1863. NakedDhcpv6Srv srv(0);
  1864. // Let's get a traffic capture from DOCSIS3.0 modem
  1865. Pkt6Ptr sol = captureDocsisRelayedSolicit();
  1866. // Simulate that we have received that traffic
  1867. srv.fakeReceive(sol);
  1868. // Server will now process to run its normal loop, but instead of calling
  1869. // IfaceMgr::receive6(), it will read all packets from the list set by
  1870. // fakeReceive()
  1871. srv.run();
  1872. // We should have an Advertise in response
  1873. ASSERT_FALSE(srv.fake_sent_.empty());
  1874. Pkt6Ptr adv = srv.fake_sent_.front();
  1875. ASSERT_TRUE(adv);
  1876. /// @todo Check that the ADVERTISE is ok, that it includes all options,
  1877. /// that is relayed properly, etc.
  1878. }
  1879. /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
  1880. /// to call processX() methods.
  1881. } // end of anonymous namespace