dhcp6_srv_unittest.cc 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. // Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/io_address.h>
  8. #include <dhcp/dhcp6.h>
  9. #include <dhcp/duid.h>
  10. #include <dhcp/option.h>
  11. #include <dhcp/option6_addrlst.h>
  12. #include <dhcp/option6_ia.h>
  13. #include <dhcp/option6_iaaddr.h>
  14. #include <dhcp/option_int.h>
  15. #include <dhcp/option_int_array.h>
  16. #include <dhcp/option_string.h>
  17. #include <dhcp/option_vendor.h>
  18. #include <dhcp/option_vendor_class.h>
  19. #include <dhcp/iface_mgr.h>
  20. #include <dhcp6/json_config_parser.h>
  21. #include <dhcp/dhcp6.h>
  22. #include <dhcp/docsis3_option_defs.h>
  23. #include <dhcp/tests/iface_mgr_test_config.h>
  24. #include <dhcpsrv/cfgmgr.h>
  25. #include <dhcpsrv/lease_mgr.h>
  26. #include <dhcpsrv/lease_mgr_factory.h>
  27. #include <dhcpsrv/host_mgr.h>
  28. #include <dhcpsrv/utils.h>
  29. #include <util/buffer.h>
  30. #include <util/range_utilities.h>
  31. #include <util/encode/hex.h>
  32. #include <stats/stats_mgr.h>
  33. #include <dhcp6/tests/dhcp6_test_utils.h>
  34. #include <dhcp6/tests/dhcp6_client.h>
  35. #include <dhcp/tests/pkt_captures.h>
  36. #include <cc/command_interpreter.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::data;
  46. using namespace isc::asiolink;
  47. using namespace isc::dhcp;
  48. using namespace isc::dhcp::test;
  49. using namespace isc::util;
  50. using namespace std;
  51. namespace {
  52. const char* CONFIGS[] = {
  53. // Configuration 0:
  54. // - used in advertiseOptions
  55. "{ \"interfaces-config\": {"
  56. " \"interfaces\": [ \"*\" ]"
  57. "},"
  58. "\"preferred-lifetime\": 3000,"
  59. "\"rebind-timer\": 2000, "
  60. "\"renew-timer\": 1000, "
  61. "\"subnet6\": [ { "
  62. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  63. " \"subnet\": \"2001:db8:1::/48\", "
  64. " \"interface\": \"eth0\", "
  65. " \"option-data\": [ {"
  66. " \"name\": \"dns-servers\","
  67. " \"data\": \"2001:db8:1234:FFFF::1, 2001:db8:1234:FFFF::2\""
  68. " },"
  69. " {"
  70. " \"name\": \"subscriber-id\","
  71. " \"data\": \"1234\","
  72. " \"csv-format\": false"
  73. " } ]"
  74. " } ],"
  75. "\"valid-lifetime\": 4000 }",
  76. // Configuration 1:
  77. // - a single subnet
  78. // - MySQL host data source
  79. "{ \"interfaces-config\": {"
  80. " \"interfaces\": [ \"*\" ]"
  81. "},"
  82. "\"hosts-database\": {"
  83. " \"type\": \"mysql\","
  84. " \"name\": \"keatest\","
  85. " \"user\": \"keatest\","
  86. " \"password\": \"keatest\""
  87. "},"
  88. "\"preferred-lifetime\": 3000,"
  89. "\"rebind-timer\": 2000, "
  90. "\"renew-timer\": 1000, "
  91. "\"subnet6\": [ { "
  92. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  93. " \"subnet\": \"2001:db8:1::/48\" "
  94. " } ],"
  95. "\"valid-lifetime\": 4000 }",
  96. // Configuration 2:
  97. // - a single subnet
  98. // - two global options (one enforced with always-send)
  99. "{"
  100. " \"interfaces-config\": { \"interfaces\": [ \"*\" ] }, "
  101. " \"preferred-lifetime\": 3000, "
  102. " \"rebind-timer\": 2000, "
  103. " \"renew-timer\": 1000, "
  104. " \"valid-lifetime\": 4000, "
  105. " \"subnet6\": [ {"
  106. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ], "
  107. " \"subnet\": \"2001:db8:1::/48\""
  108. " } ], "
  109. " \"option-data\": ["
  110. " {"
  111. " \"name\": \"dns-servers\", "
  112. " \"data\": \"2001:db8:1234:FFFF::1\""
  113. " }, "
  114. " {"
  115. " \"name\": \"subscriber-id\", "
  116. " \"data\": \"1234\", "
  117. " \"always-send\": true"
  118. " }"
  119. " ]"
  120. "}"
  121. };
  122. // This test verifies that incoming SOLICIT can be handled properly when
  123. // there are no subnets configured.
  124. //
  125. // This test sends a SOLICIT and the expected response
  126. // is an ADVERTISE with STATUS_NoAddrsAvail and no address provided in the
  127. // response
  128. TEST_F(NakedDhcpv6SrvTest, SolicitNoSubnet) {
  129. NakedDhcpv6Srv srv(0);
  130. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  131. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  132. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  133. OptionPtr clientid = generateClientId();
  134. sol->addOption(clientid);
  135. // Pass it to the server and get an advertise
  136. Pkt6Ptr reply = srv.processSolicit(sol);
  137. // check that we get the right NAK
  138. checkNakResponse(reply, DHCPV6_ADVERTISE, 1234, STATUS_NoAddrsAvail,
  139. 0, 0);
  140. }
  141. // This test verifies that incoming REQUEST can be handled properly when
  142. // there are no subnets configured.
  143. //
  144. // This test sends a REQUEST and the expected response
  145. // is an REPLY with STATUS_NoAddrsAvail and no address provided in the
  146. // response
  147. TEST_F(NakedDhcpv6SrvTest, RequestNoSubnet) {
  148. NakedDhcpv6Srv srv(0);
  149. // Let's create a REQUEST
  150. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  151. req->setRemoteAddr(IOAddress("fe80::abcd"));
  152. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  153. // with a hint
  154. IOAddress hint("2001:db8:1:1::dead:beef");
  155. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  156. ia->addOption(hint_opt);
  157. req->addOption(ia);
  158. OptionPtr clientid = generateClientId();
  159. req->addOption(clientid);
  160. // server-id is mandatory in REQUEST
  161. req->addOption(srv.getServerID());
  162. // Pass it to the server and hope for a REPLY
  163. Pkt6Ptr reply = srv.processRequest(req);
  164. // check that we get the right NAK
  165. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoAddrsAvail,
  166. 0, 0);
  167. }
  168. // This test verifies that incoming RENEW can be handled properly, even when
  169. // no subnets are configured.
  170. //
  171. // This test sends a RENEW and the expected response
  172. // is an REPLY with STATUS_NoBinding and no address provided in the
  173. // response
  174. TEST_F(NakedDhcpv6SrvTest, RenewNoSubnet) {
  175. NakedDhcpv6Srv srv(0);
  176. const IOAddress addr("2001:db8:1:1::cafe:babe");
  177. const uint32_t iaid = 234;
  178. // Generate client-id also duid_
  179. OptionPtr clientid = generateClientId();
  180. // Let's create a RENEW
  181. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  182. req->setRemoteAddr(IOAddress("fe80::abcd"));
  183. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  184. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  185. ia->addOption(renewed_addr_opt);
  186. req->addOption(ia);
  187. req->addOption(clientid);
  188. // Server-id is mandatory in RENEW
  189. req->addOption(srv.getServerID());
  190. // Pass it to the server and hope for a REPLY
  191. Pkt6Ptr reply = srv.processRenew(req);
  192. // check that we get the right NAK
  193. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding,
  194. 0, 0);
  195. }
  196. // This test verifies that incoming RELEASE can be handled properly, even when
  197. // no subnets are configured.
  198. //
  199. // This test sends a RELEASE and the expected response
  200. // is an REPLY with STATUS_NoBinding and no address provided in the
  201. // response
  202. TEST_F(NakedDhcpv6SrvTest, ReleaseNoSubnet) {
  203. NakedDhcpv6Srv srv(0);
  204. const IOAddress addr("2001:db8:1:1::cafe:babe");
  205. const uint32_t iaid = 234;
  206. // Generate client-id also duid_
  207. OptionPtr clientid = generateClientId();
  208. // Let's create a RELEASE
  209. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  210. req->setRemoteAddr(IOAddress("fe80::abcd"));
  211. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  212. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  213. ia->addOption(released_addr_opt);
  214. req->addOption(ia);
  215. req->addOption(clientid);
  216. // Server-id is mandatory in RELEASE
  217. req->addOption(srv.getServerID());
  218. // Pass it to the server and hope for a REPLY
  219. Pkt6Ptr reply = srv.processRelease(req);
  220. // check that we get the right NAK
  221. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding, 0, 0);
  222. }
  223. // Test verifies that the Dhcpv6_srv class can be instantiated. It checks a mode
  224. // without open sockets and with sockets opened on a high port (to not require
  225. // root privileges).
  226. TEST_F(Dhcpv6SrvTest, basic) {
  227. boost::scoped_ptr<Dhcpv6Srv> srv;
  228. ASSERT_NO_THROW( {
  229. // Skip opening any sockets
  230. srv.reset(new NakedDhcpv6Srv(0));
  231. });
  232. srv.reset();
  233. ASSERT_NO_THROW({
  234. // open an unprivileged port
  235. srv.reset(new NakedDhcpv6Srv(DHCP6_SERVER_PORT + 10000));
  236. });
  237. }
  238. // Test checks that DUID is generated properly
  239. TEST_F(Dhcpv6SrvTest, DUID) {
  240. boost::scoped_ptr<Dhcpv6Srv> srv;
  241. ASSERT_NO_THROW( {
  242. srv.reset(new NakedDhcpv6Srv(0));
  243. });
  244. OptionPtr srvid = srv->getServerID();
  245. ASSERT_TRUE(srvid);
  246. EXPECT_EQ(D6O_SERVERID, srvid->getType());
  247. OutputBuffer buf(32);
  248. srvid->pack(buf);
  249. // length of the actual DUID
  250. size_t len = buf.getLength() - srvid->getHeaderLen();
  251. InputBuffer data(buf.getData(), buf.getLength());
  252. // ignore first four bytes (standard DHCPv6 header)
  253. data.readUint32();
  254. uint16_t duid_type = data.readUint16();
  255. cout << "Duid-type=" << duid_type << endl;
  256. switch(duid_type) {
  257. case DUID::DUID_LLT: {
  258. // DUID must contain at least 6 bytes long MAC
  259. // + 8 bytes of fixed header
  260. EXPECT_GE(len, 14);
  261. uint16_t hw_type = data.readUint16();
  262. // there's no real way to find out "correct"
  263. // hardware type
  264. EXPECT_GT(hw_type, 0);
  265. // check that timer is counted since 1.1.2000,
  266. // not from 1.1.1970.
  267. uint32_t seconds = data.readUint32();
  268. EXPECT_LE(seconds, DUID_TIME_EPOCH);
  269. // this test will start failing after 2030.
  270. // Hopefully we will be at BIND12 by then.
  271. // MAC must not be zeros
  272. vector<uint8_t> mac(len-8);
  273. vector<uint8_t> zeros(len-8, 0);
  274. data.readVector(mac, len-8);
  275. EXPECT_TRUE(mac != zeros);
  276. break;
  277. }
  278. case DUID::DUID_EN: {
  279. // there's not much we can check. Just simple
  280. // check if it is not all zeros
  281. vector<uint8_t> content(len-2);
  282. data.readVector(content, len-2);
  283. EXPECT_FALSE(isRangeZero(content.begin(), content.end()));
  284. break;
  285. }
  286. case DUID::DUID_LL: {
  287. // not supported yet
  288. cout << "Test not implemented for DUID-LL." << endl;
  289. // No failure here. There's really no way for test LL DUID. It doesn't
  290. // even make sense to check if that Link Layer is actually present on
  291. // a physical interface. RFC3315 says a server should write its DUID
  292. // and keep it despite hardware changes.
  293. break;
  294. }
  295. case DUID::DUID_UUID: // not supported yet
  296. default:
  297. ADD_FAILURE() << "Not supported duid type=" << duid_type << endl;
  298. break;
  299. }
  300. }
  301. // This test checks if Option Request Option (ORO) is parsed correctly
  302. // and the requested options are actually assigned.
  303. TEST_F(Dhcpv6SrvTest, advertiseOptions) {
  304. IfaceMgrTestConfig test_config(true);
  305. ConstElementPtr x;
  306. ASSERT_NO_THROW(configure(CONFIGS[0]));
  307. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  308. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  309. sol->setIface("eth0");
  310. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  311. OptionPtr clientid = generateClientId();
  312. sol->addOption(clientid);
  313. // Pass it to the server and get an advertise
  314. Pkt6Ptr adv = srv_.processSolicit(sol);
  315. // check if we get response at all
  316. ASSERT_TRUE(adv);
  317. // We have not requested any options so they should not
  318. // be included in the response.
  319. ASSERT_FALSE(adv->getOption(D6O_SUBSCRIBER_ID));
  320. ASSERT_FALSE(adv->getOption(D6O_NAME_SERVERS));
  321. // Let's now request some options. We expect that the server
  322. // will include them in its response.
  323. boost::shared_ptr<OptionIntArray<uint16_t> >
  324. option_oro(new OptionIntArray<uint16_t>(Option::V6, D6O_ORO));
  325. // Create vector with two option codes.
  326. std::vector<uint16_t> codes(2);
  327. codes[0] = D6O_SUBSCRIBER_ID;
  328. codes[1] = D6O_NAME_SERVERS;
  329. // Pass this code to option.
  330. option_oro->setValues(codes);
  331. // Append ORO to SOLICIT message.
  332. sol->addOption(option_oro);
  333. // Need to process SOLICIT again after requesting new option.
  334. adv = srv_.processSolicit(sol);
  335. ASSERT_TRUE(adv);
  336. OptionPtr tmp = adv->getOption(D6O_NAME_SERVERS);
  337. ASSERT_TRUE(tmp);
  338. boost::shared_ptr<Option6AddrLst> reply_nameservers =
  339. boost::dynamic_pointer_cast<Option6AddrLst>(tmp);
  340. ASSERT_TRUE(reply_nameservers);
  341. Option6AddrLst::AddressContainer addrs = reply_nameservers->getAddresses();
  342. ASSERT_EQ(2, addrs.size());
  343. EXPECT_TRUE(addrs[0] == IOAddress("2001:db8:1234:FFFF::1"));
  344. EXPECT_TRUE(addrs[1] == IOAddress("2001:db8:1234:FFFF::2"));
  345. // There is a dummy option with code 1000 we requested from a server.
  346. // Expect that this option is in server's response.
  347. tmp = adv->getOption(D6O_SUBSCRIBER_ID);
  348. ASSERT_TRUE(tmp);
  349. // Check that the option contains valid data (from configuration).
  350. std::vector<uint8_t> data = tmp->getData();
  351. ASSERT_EQ(2, data.size());
  352. const uint8_t foo_expected[] = {
  353. 0x12, 0x34
  354. };
  355. EXPECT_EQ(0, memcmp(&data[0], foo_expected, 2));
  356. // more checks to be implemented
  357. }
  358. // There are no dedicated tests for Dhcpv6Srv::handleIA_NA and Dhcpv6Srv::assignLeases
  359. // as they are indirectly tested in Solicit and Request tests.
  360. // This test verifies that incoming SOLICIT can be handled properly, that an
  361. // ADVERTISE is generated, that the response has an address and that address
  362. // really belongs to the configured pool.
  363. //
  364. // This test sends a SOLICIT without any hint in IA_NA.
  365. //
  366. // constructed very simple SOLICIT message with:
  367. // - client-id option (mandatory)
  368. // - IA option (a request for address, without any addresses)
  369. //
  370. // expected returned ADVERTISE message:
  371. // - copy of client-id
  372. // - server-id
  373. // - IA that includes IAADDR
  374. TEST_F(Dhcpv6SrvTest, SolicitBasic) {
  375. NakedDhcpv6Srv srv(0);
  376. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  377. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  378. sol->setIface("eth0");
  379. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  380. OptionPtr clientid = generateClientId();
  381. sol->addOption(clientid);
  382. // Pass it to the server and get an advertise
  383. Pkt6Ptr reply = srv.processSolicit(sol);
  384. // check if we get response at all
  385. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  386. // check that IA_NA was returned and that there's an address included
  387. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  388. subnet_->getT2());
  389. ASSERT_TRUE(addr);
  390. // Check that the assigned address is indeed from the configured pool
  391. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA);
  392. // check DUIDs
  393. checkServerId(reply, srv.getServerID());
  394. checkClientId(reply, clientid);
  395. }
  396. // This test verifies that incoming SOLICIT can be handled properly, that an
  397. // ADVERTISE is generated, that the response has a prefix and that prefix
  398. // really belongs to the configured pool.
  399. //
  400. // This test sends a SOLICIT without any hint in IA_PD.
  401. //
  402. // constructed very simple SOLICIT message with:
  403. // - client-id option (mandatory)
  404. // - IA option (a request for address, without any addresses)
  405. //
  406. // expected returned ADVERTISE message:
  407. // - copy of client-id
  408. // - server-id
  409. // - IA that includes IAPREFIX
  410. TEST_F(Dhcpv6SrvTest, pdSolicitBasic) {
  411. NakedDhcpv6Srv srv(0);
  412. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  413. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  414. sol->setIface("eth0");
  415. sol->addOption(generateIA(D6O_IA_PD, 234, 1500, 3000));
  416. OptionPtr clientid = generateClientId();
  417. sol->addOption(clientid);
  418. // Pass it to the server and get an advertise
  419. Pkt6Ptr reply = srv.processSolicit(sol);
  420. // check if we get response at all
  421. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  422. // check that IA_NA was returned and that there's an address included
  423. boost::shared_ptr<Option6IAPrefix> prefix = checkIA_PD(reply, 234, subnet_->getT1(),
  424. subnet_->getT2());
  425. ASSERT_TRUE(prefix);
  426. // Check that the assigned prefix is indeed from the configured pool
  427. checkIAAddr(prefix, prefix->getAddress(), Lease::TYPE_PD);
  428. EXPECT_EQ(pd_pool_->getLength(), prefix->getLength());
  429. // check DUIDs
  430. checkServerId(reply, srv.getServerID());
  431. checkClientId(reply, clientid);
  432. }
  433. // This test verifies that incoming SOLICIT can be handled properly, that an
  434. // ADVERTISE is generated, that the response has an address and that address
  435. // really belongs to the configured pool.
  436. //
  437. // This test sends a SOLICIT with IA_NA that contains a valid hint.
  438. //
  439. // constructed very simple SOLICIT message with:
  440. // - client-id option (mandatory)
  441. // - IA option (a request for address, with an address that belongs to the
  442. // configured pool, i.e. is valid as hint)
  443. //
  444. // expected returned ADVERTISE message:
  445. // - copy of client-id
  446. // - server-id
  447. // - IA that includes IAADDR
  448. TEST_F(Dhcpv6SrvTest, SolicitHint) {
  449. NakedDhcpv6Srv srv(0);
  450. // Let's create a SOLICIT
  451. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  452. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  453. sol->setIface("eth0");
  454. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  455. // with a valid hint
  456. IOAddress hint("2001:db8:1:1::dead:beef");
  457. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  458. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  459. ia->addOption(hint_opt);
  460. sol->addOption(ia);
  461. OptionPtr clientid = generateClientId();
  462. sol->addOption(clientid);
  463. // Pass it to the server and get an advertise
  464. Pkt6Ptr reply = srv.processSolicit(sol);
  465. // check if we get response at all
  466. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  467. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  468. ASSERT_TRUE(tmp);
  469. // check that IA_NA was returned and that there's an address included
  470. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  471. subnet_->getT2());
  472. ASSERT_TRUE(addr);
  473. // check that we've got the address we requested
  474. checkIAAddr(addr, hint, Lease::TYPE_NA);
  475. // check DUIDs
  476. checkServerId(reply, srv.getServerID());
  477. checkClientId(reply, clientid);
  478. }
  479. // This test verifies that incoming SOLICIT can be handled properly, that an
  480. // ADVERTISE is generated, that the response has an address and that address
  481. // really belongs to the configured pool.
  482. //
  483. // This test sends a SOLICIT with IA_NA that contains an invalid hint.
  484. //
  485. // constructed very simple SOLICIT message with:
  486. // - client-id option (mandatory)
  487. // - IA option (a request for address, with an address that does not
  488. // belong to the configured pool, i.e. is valid as hint)
  489. //
  490. // expected returned ADVERTISE message:
  491. // - copy of client-id
  492. // - server-id
  493. // - IA that includes IAADDR
  494. TEST_F(Dhcpv6SrvTest, SolicitInvalidHint) {
  495. NakedDhcpv6Srv srv(0);
  496. // Let's create a SOLICIT
  497. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  498. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  499. sol->setIface("eth0");
  500. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  501. IOAddress hint("2001:db8:1::cafe:babe");
  502. ASSERT_FALSE(subnet_->inPool(Lease::TYPE_NA, hint));
  503. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  504. ia->addOption(hint_opt);
  505. sol->addOption(ia);
  506. OptionPtr clientid = generateClientId();
  507. sol->addOption(clientid);
  508. // Pass it to the server and get an advertise
  509. Pkt6Ptr reply = srv.processSolicit(sol);
  510. // check if we get response at all
  511. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  512. // check that IA_NA was returned and that there's an address included
  513. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  514. subnet_->getT2());
  515. ASSERT_TRUE(addr);
  516. // Check that the assigned address is indeed from the configured pool
  517. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA);
  518. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr->getAddress()));
  519. // check DUIDs
  520. checkServerId(reply, srv.getServerID());
  521. checkClientId(reply, clientid);
  522. }
  523. /// @todo: Add a test that client sends hint that is in pool, but currently
  524. /// being used by a different client.
  525. // This test checks that the server is offering different addresses to different
  526. // clients in ADVERTISEs. Please note that ADVERTISE is not a guarantee that such
  527. // an address will be assigned. Had the pool was very small and contained only
  528. // 2 addresses, the third client would get the same advertise as the first one
  529. // and this is a correct behavior. It is REQUEST that will fail for the third
  530. // client. ADVERTISE is basically saying "if you send me a request, you will
  531. // probably get an address like this" (there are no guarantees).
  532. TEST_F(Dhcpv6SrvTest, ManySolicits) {
  533. NakedDhcpv6Srv srv(0);
  534. Pkt6Ptr sol1 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  535. Pkt6Ptr sol2 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 2345));
  536. Pkt6Ptr sol3 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 3456));
  537. sol1->setRemoteAddr(IOAddress("fe80::abcd"));
  538. sol2->setRemoteAddr(IOAddress("fe80::1223"));
  539. sol3->setRemoteAddr(IOAddress("fe80::3467"));
  540. sol1->setIface("eth0");
  541. sol2->setIface("eth0");
  542. sol3->setIface("eth0");
  543. sol1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  544. sol2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  545. sol3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  546. // different client-id sizes
  547. OptionPtr clientid1 = generateClientId(12);
  548. OptionPtr clientid2 = generateClientId(14);
  549. OptionPtr clientid3 = generateClientId(16);
  550. sol1->addOption(clientid1);
  551. sol2->addOption(clientid2);
  552. sol3->addOption(clientid3);
  553. // Pass it to the server and get an advertise
  554. Pkt6Ptr reply1 = srv.processSolicit(sol1);
  555. Pkt6Ptr reply2 = srv.processSolicit(sol2);
  556. Pkt6Ptr reply3 = srv.processSolicit(sol3);
  557. // check if we get response at all
  558. checkResponse(reply1, DHCPV6_ADVERTISE, 1234);
  559. checkResponse(reply2, DHCPV6_ADVERTISE, 2345);
  560. checkResponse(reply3, DHCPV6_ADVERTISE, 3456);
  561. // check that IA_NA was returned and that there's an address included
  562. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  563. subnet_->getT2());
  564. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  565. subnet_->getT2());
  566. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  567. subnet_->getT2());
  568. ASSERT_TRUE(addr1);
  569. ASSERT_TRUE(addr2);
  570. ASSERT_TRUE(addr3);
  571. // Check that the assigned address is indeed from the configured pool
  572. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA);
  573. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA);
  574. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA);
  575. // check DUIDs
  576. checkServerId(reply1, srv.getServerID());
  577. checkServerId(reply2, srv.getServerID());
  578. checkServerId(reply3, srv.getServerID());
  579. checkClientId(reply1, clientid1);
  580. checkClientId(reply2, clientid2);
  581. checkClientId(reply3, clientid3);
  582. // Finally check that the addresses offered are different
  583. EXPECT_NE(addr1->getAddress(), addr2->getAddress());
  584. EXPECT_NE(addr2->getAddress(), addr3->getAddress());
  585. EXPECT_NE(addr3->getAddress(), addr1->getAddress());
  586. cout << "Offered address to client1=" << addr1->getAddress() << endl;
  587. cout << "Offered address to client2=" << addr2->getAddress() << endl;
  588. cout << "Offered address to client3=" << addr3->getAddress() << endl;
  589. }
  590. // This test verifies that incoming REQUEST can be handled properly, that a
  591. // REPLY is generated, that the response has an address and that address
  592. // really belongs to the configured pool.
  593. //
  594. // This test sends a REQUEST with IA_NA that contains a valid hint.
  595. //
  596. // constructed very simple REQUEST message with:
  597. // - client-id option (mandatory)
  598. // - IA option (a request for address, with an address that belongs to the
  599. // configured pool, i.e. is valid as hint)
  600. //
  601. // expected returned REPLY message:
  602. // - copy of client-id
  603. // - server-id
  604. // - IA that includes IAADDR
  605. TEST_F(Dhcpv6SrvTest, RequestBasic) {
  606. NakedDhcpv6Srv srv(0);
  607. // Let's create a REQUEST
  608. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  609. req->setRemoteAddr(IOAddress("fe80::abcd"));
  610. req->setIface("eth0");
  611. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  612. // with a valid hint
  613. IOAddress hint("2001:db8:1:1::dead:beef");
  614. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  615. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  616. ia->addOption(hint_opt);
  617. req->addOption(ia);
  618. OptionPtr clientid = generateClientId();
  619. req->addOption(clientid);
  620. // server-id is mandatory in REQUEST
  621. req->addOption(srv.getServerID());
  622. // Pass it to the server and hope for a REPLY
  623. Pkt6Ptr reply = srv.processRequest(req);
  624. // check if we get response at all
  625. checkResponse(reply, DHCPV6_REPLY, 1234);
  626. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  627. ASSERT_TRUE(tmp);
  628. // check that IA_NA was returned and that there's an address included
  629. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234,
  630. subnet_->getT1(),
  631. subnet_->getT2());
  632. ASSERT_TRUE(addr);
  633. // check that we've got the address we requested
  634. checkIAAddr(addr, hint, Lease::TYPE_NA);
  635. // check DUIDs
  636. checkServerId(reply, srv.getServerID());
  637. checkClientId(reply, clientid);
  638. // check that the lease is really in the database
  639. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  640. EXPECT_TRUE(l);
  641. LeaseMgrFactory::instance().deleteLease(addr->getAddress());
  642. }
  643. // This test verifies that incoming REQUEST can be handled properly, that a
  644. // REPLY is generated, that the response has a prefix and that prefix
  645. // really belongs to the configured pool.
  646. //
  647. // This test sends a REQUEST with IA_PD that contains a valid hint.
  648. //
  649. // constructed very simple REQUEST message with:
  650. // - client-id option (mandatory)
  651. // - IA option (a request for address, with an address that belongs to the
  652. // configured pool, i.e. is valid as hint)
  653. //
  654. // expected returned REPLY message:
  655. // - copy of client-id
  656. // - server-id
  657. // - IA that includes IAPREFIX
  658. TEST_F(Dhcpv6SrvTest, pdRequestBasic) {
  659. NakedDhcpv6Srv srv(0);
  660. // Let's create a REQUEST
  661. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  662. req->setRemoteAddr(IOAddress("fe80::abcd"));
  663. req->setIface("eth0");
  664. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_PD, 234, 1500, 3000);
  665. // with a valid hint
  666. IOAddress hint("2001:db8:1:2:f::");
  667. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_PD, hint));
  668. OptionPtr hint_opt(new Option6IAPrefix(D6O_IAPREFIX, hint, 64, 300, 500));
  669. ia->addOption(hint_opt);
  670. req->addOption(ia);
  671. OptionPtr clientid = generateClientId();
  672. req->addOption(clientid);
  673. // server-id is mandatory in REQUEST
  674. req->addOption(srv.getServerID());
  675. // Pass it to the server and hope for a REPLY
  676. Pkt6Ptr reply = srv.processRequest(req);
  677. // check if we get response at all
  678. checkResponse(reply, DHCPV6_REPLY, 1234);
  679. OptionPtr tmp = reply->getOption(D6O_IA_PD);
  680. ASSERT_TRUE(tmp);
  681. // check that IA_NA was returned and that there's an address included
  682. boost::shared_ptr<Option6IAPrefix> prf = checkIA_PD(reply, 234,
  683. subnet_->getT1(),
  684. subnet_->getT2());
  685. ASSERT_TRUE(prf);
  686. // check that we've got the address we requested
  687. checkIAAddr(prf, hint, Lease::TYPE_PD);
  688. EXPECT_EQ(pd_pool_->getLength(), prf->getLength());
  689. // check DUIDs
  690. checkServerId(reply, srv.getServerID());
  691. checkClientId(reply, clientid);
  692. // check that the lease is really in the database
  693. Lease6Ptr l = checkPdLease(duid_, reply->getOption(D6O_IA_PD), prf);
  694. EXPECT_TRUE(l);
  695. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(prf->getAddress()));
  696. }
  697. // This test checks that the server is offering different addresses to different
  698. // clients in REQUEST. Please note that ADVERTISE is not a guarantee that such
  699. // and address will be assigned. Had the pool was very small and contained only
  700. // 2 addresses, the third client would get the same advertise as the first one
  701. // and this is a correct behavior. It is REQUEST that will fail for the third
  702. // client. ADVERTISE is basically saying "if you send me a request, you will
  703. // probably get an address like this" (there are no guarantees).
  704. TEST_F(Dhcpv6SrvTest, ManyRequests) {
  705. NakedDhcpv6Srv srv(0);
  706. ASSERT_TRUE(subnet_);
  707. Pkt6Ptr req1 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  708. Pkt6Ptr req2 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 2345));
  709. Pkt6Ptr req3 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 3456));
  710. req1->setRemoteAddr(IOAddress("fe80::abcd"));
  711. req2->setRemoteAddr(IOAddress("fe80::1223"));
  712. req3->setRemoteAddr(IOAddress("fe80::3467"));
  713. req1->setIface("eth0");
  714. req2->setIface("eth0");
  715. req3->setIface("eth0");
  716. req1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  717. req2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  718. req3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  719. // different client-id sizes
  720. OptionPtr clientid1 = generateClientId(12);
  721. OptionPtr clientid2 = generateClientId(14);
  722. OptionPtr clientid3 = generateClientId(16);
  723. req1->addOption(clientid1);
  724. req2->addOption(clientid2);
  725. req3->addOption(clientid3);
  726. // server-id is mandatory in REQUEST
  727. req1->addOption(srv.getServerID());
  728. req2->addOption(srv.getServerID());
  729. req3->addOption(srv.getServerID());
  730. // Pass it to the server and get an advertise
  731. Pkt6Ptr reply1 = srv.processRequest(req1);
  732. Pkt6Ptr reply2 = srv.processRequest(req2);
  733. Pkt6Ptr reply3 = srv.processRequest(req3);
  734. // check if we get response at all
  735. checkResponse(reply1, DHCPV6_REPLY, 1234);
  736. checkResponse(reply2, DHCPV6_REPLY, 2345);
  737. checkResponse(reply3, DHCPV6_REPLY, 3456);
  738. // check that IA_NA was returned and that there's an address included
  739. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  740. subnet_->getT2());
  741. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  742. subnet_->getT2());
  743. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  744. subnet_->getT2());
  745. ASSERT_TRUE(addr1);
  746. ASSERT_TRUE(addr2);
  747. ASSERT_TRUE(addr3);
  748. // Check that the assigned address is indeed from the configured pool
  749. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA);
  750. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA);
  751. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA);
  752. // check DUIDs
  753. checkServerId(reply1, srv.getServerID());
  754. checkServerId(reply2, srv.getServerID());
  755. checkServerId(reply3, srv.getServerID());
  756. checkClientId(reply1, clientid1);
  757. checkClientId(reply2, clientid2);
  758. checkClientId(reply3, clientid3);
  759. // Finally check that the addresses offered are different
  760. EXPECT_NE(addr1->getAddress(), addr2->getAddress());
  761. EXPECT_NE(addr2->getAddress(), addr3->getAddress());
  762. EXPECT_NE(addr3->getAddress(), addr1->getAddress());
  763. cout << "Assigned address to client1=" << addr1->getAddress() << endl;
  764. cout << "Assigned address to client2=" << addr2->getAddress() << endl;
  765. cout << "Assigned address to client3=" << addr3->getAddress() << endl;
  766. }
  767. // This test verifies that incoming (positive) RENEW can be handled properly, that a
  768. // REPLY is generated, that the response has an address and that address
  769. // really belongs to the configured pool and that lease is actually renewed.
  770. //
  771. // expected:
  772. // - returned REPLY message has copy of client-id
  773. // - returned REPLY message has server-id
  774. // - returned REPLY message has IA_NA that includes IAADDR
  775. // - lease is actually renewed in LeaseMgr
  776. TEST_F(Dhcpv6SrvTest, renewBasic) {
  777. testRenewBasic(Lease::TYPE_NA, "2001:db8:1:1::cafe:babe",
  778. "2001:db8:1:1::cafe:babe", 128);
  779. }
  780. // This test verifies that incoming (positive) PD RENEW can be handled properly,
  781. // that a REPLY is generated, that the response has a prefix and that prefix
  782. // really belongs to the configured pool and that lease is actually renewed.
  783. //
  784. // expected:
  785. // - returned REPLY message has copy of client-id
  786. // - returned REPLY message has server-id
  787. // - returned REPLY message has IA_PD that includes IAPREFIX
  788. // - lease is actually renewed in LeaseMgr
  789. TEST_F(Dhcpv6SrvTest, pdRenewBasic) {
  790. testRenewBasic(Lease::TYPE_PD, "2001:db8:1:2::",
  791. "2001:db8:1:2::", pd_pool_->getLength());
  792. }
  793. // This test verifies that incoming (invalid) RENEW with an address
  794. // can be handled properly. This has changed with #3565. The server
  795. // is now able to allocate a lease in Renew if it's available.
  796. // Previous testRenewReject is now split into 3 tests.
  797. //
  798. // This test checks the first scenario: There is no lease at all.
  799. // The server will try to assign it. Since it is not used by anyone else,
  800. // the server will assign it. This is convenient for various types
  801. // of recoveries, e.g. when the server lost its database.
  802. TEST_F(Dhcpv6SrvTest, RenewUnknown) {
  803. // False means that the lease should not be created before renewal attempt
  804. testRenewBasic(Lease::TYPE_NA, "2001:db8:1:1::abc", "2001:db8:1:1::abc",
  805. 128, false);
  806. }
  807. // This test checks that a client that renews existing lease, but uses
  808. // a wrong IAID, will be processed correctly. As there is no lease for
  809. // this (duid, type, iaid) tuple, this is treated as a new IA, regardless
  810. // if the client inserted an address that is used in a different IA.
  811. // After #3565 was implemented, the server will attempt to assign a lease.
  812. // The one that client requested is already used with different IAID, so
  813. // it will just pick a different lease. This is the second out of three
  814. // scenarios tests by old RenewReject test.
  815. TEST_F(Dhcpv6SrvTest, RenewWrongIAID) {
  816. testRenewWrongIAID(Lease::TYPE_NA, IOAddress("2001:db8:1:1::abc"));
  817. }
  818. // This test checks whether client A can renew an address that is currently
  819. // leased by client B. The server should detect that the lease belong to
  820. // someone else and assign a different lease. This is the third out of three
  821. // scenarios tests by old RenewReject test.
  822. TEST_F(Dhcpv6SrvTest, RenewSomeoneElesesLease) {
  823. testRenewSomeoneElsesLease(Lease::TYPE_NA, IOAddress("2001:db8::1"));
  824. }
  825. // This test verifies that incoming (positive) RELEASE with address can be
  826. // handled properly, that a REPLY is generated, that the response has status
  827. // code and that the lease is indeed removed from the database.
  828. //
  829. // expected:
  830. // - returned REPLY message has copy of client-id
  831. // - returned REPLY message has server-id
  832. // - returned REPLY message has IA_NA that does not include an IAADDR
  833. // - lease is actually removed from LeaseMgr
  834. // - assigned-nas stats counter is properly decremented
  835. TEST_F(Dhcpv6SrvTest, ReleaseBasic) {
  836. testReleaseBasic(Lease::TYPE_NA, IOAddress("2001:db8:1:1::cafe:babe"),
  837. IOAddress("2001:db8:1:1::cafe:babe"));
  838. }
  839. // This test verifies that incoming (positive) RELEASE with prefix can be
  840. // handled properly, that a REPLY is generated, that the response has
  841. // status code and that the lease is indeed removed from the database.
  842. //
  843. // expected:
  844. // - returned REPLY message has copy of client-id
  845. // - returned REPLY message has server-id
  846. // - returned REPLY message has IA_PD that does not include an IAPREFIX
  847. // - lease is actually removed from LeaseMgr
  848. // - assigned-pds stats counter is properly decremented
  849. TEST_F(Dhcpv6SrvTest, pdReleaseBasic) {
  850. testReleaseBasic(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"),
  851. IOAddress("2001:db8:1:2::"));
  852. }
  853. // This test verifies that incoming (invalid) RELEASE with an address
  854. // can be handled properly.
  855. //
  856. // This test checks 3 scenarios:
  857. // 1. there is no such lease at all
  858. // 2. there is such a lease, but it is assigned to a different IAID
  859. // 3. there is such a lease, but it belongs to a different client
  860. //
  861. // expected:
  862. // - returned REPLY message has copy of client-id
  863. // - returned REPLY message has server-id
  864. // - returned REPLY message has IA_NA that includes STATUS-CODE
  865. // - No lease in LeaseMgr
  866. // - assigned-nas stats counter is properly not decremented
  867. TEST_F(Dhcpv6SrvTest, ReleaseReject) {
  868. testReleaseReject(Lease::TYPE_NA, IOAddress("2001:db8:1:1::dead"));
  869. }
  870. // This test verifies that incoming (invalid) RELEASE with a prefix
  871. // can be handled properly.
  872. //
  873. // This test checks 3 scenarios:
  874. // 1. there is no such lease at all
  875. // 2. there is such a lease, but it is assigned to a different IAID
  876. // 3. there is such a lease, but it belongs to a different client
  877. //
  878. // expected:
  879. // - returned REPLY message has copy of client-id
  880. // - returned REPLY message has server-id
  881. // - returned REPLY message has IA_PD that includes STATUS-CODE
  882. // - No lease in LeaseMgr
  883. // - assigned-pds stats counter is properly not decremented
  884. TEST_F(Dhcpv6SrvTest, pdReleaseReject) {
  885. testReleaseReject(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"));
  886. }
  887. // This test verifies if the sanityCheck() really checks options presence.
  888. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  889. NakedDhcpv6Srv srv(0);
  890. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  891. // Set link-local sender address, so appropriate subnet can be
  892. // selected for this packet.
  893. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  894. // client-id is optional for information-request, so
  895. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  896. // empty packet, no client-id, no server-id
  897. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  898. RFCViolation);
  899. // This doesn't make much sense, but let's check it for completeness
  900. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  901. OptionPtr clientid = generateClientId();
  902. pkt->addOption(clientid);
  903. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  904. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  905. pkt->addOption(srv.getServerID());
  906. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  907. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  908. // sane section ends here, let's do some negative tests as well
  909. pkt->addOption(clientid);
  910. pkt->addOption(clientid);
  911. // with more than one client-id it should throw, no matter what
  912. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  913. RFCViolation);
  914. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  915. RFCViolation);
  916. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  917. RFCViolation);
  918. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  919. RFCViolation);
  920. pkt->delOption(D6O_CLIENTID);
  921. pkt->delOption(D6O_CLIENTID);
  922. // again we have only one client-id
  923. // let's try different type of insanity - several server-ids
  924. pkt->addOption(srv.getServerID());
  925. pkt->addOption(srv.getServerID());
  926. // with more than one server-id it should throw, no matter what
  927. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  928. RFCViolation);
  929. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  930. RFCViolation);
  931. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  932. RFCViolation);
  933. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  934. RFCViolation);
  935. }
  936. // Check that the server is testing if server identifier received in the
  937. // query, matches server identifier used by the server.
  938. TEST_F(Dhcpv6SrvTest, testServerID) {
  939. NakedDhcpv6Srv srv(0);
  940. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  941. std::vector<uint8_t> bin;
  942. // duid_llt constructed with: time = 0, macaddress = 00:00:00:00:00:00
  943. // it's necessary to generate server identifier option
  944. isc::util::encode::decodeHex("0001000100000000000000000000", bin);
  945. // Now create server identifier option
  946. OptionPtr serverid = OptionPtr(new Option(Option::V6, D6O_SERVERID, bin));
  947. // Server identifier option is MANDATORY in Request message.
  948. // Add server identifier option with different value from one that
  949. // server is using.
  950. req->addOption(serverid);
  951. // Message should be dropped
  952. EXPECT_FALSE(srv.testServerID(req));
  953. // Delete server identifier option and add new one, with same value as
  954. // server's server identifier.
  955. req->delOption(D6O_SERVERID);
  956. req->addOption(srv.getServerID());
  957. // With proper server identifier we expect true
  958. EXPECT_TRUE(srv.testServerID(req));
  959. // server-id MUST NOT appear in Solicit, so check if server is
  960. // not dropping a message without server id.
  961. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  962. EXPECT_TRUE(srv.testServerID(req));
  963. }
  964. // Test that some messages are discarded by the server if they are sent to
  965. // unicast address.
  966. TEST_F(Dhcpv6SrvTest, testUnicast) {
  967. NakedDhcpv6Srv srv(0);
  968. // Explicitly list client's message types which must be discarded if
  969. // sent to unicast address.
  970. const uint8_t not_allowed_unicast[] = {
  971. DHCPV6_SOLICIT,
  972. DHCPV6_CONFIRM,
  973. DHCPV6_REBIND,
  974. DHCPV6_INFORMATION_REQUEST
  975. };
  976. // Iterate over these messages and make sure they are discarded.
  977. for (int i = 0; i < sizeof(not_allowed_unicast); ++i) {
  978. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(not_allowed_unicast[i], 1234));
  979. msg->setLocalAddr(IOAddress("2001:db8:1::1"));
  980. EXPECT_FALSE(srv.testUnicast(msg))
  981. << "server accepts message type "
  982. << static_cast<int>(not_allowed_unicast[i])
  983. << "being sent to unicast address; this message should"
  984. " be discarded according to section 15 of RFC3315";
  985. }
  986. // Explicitly list client/relay message types which are allowed to
  987. // be sent to unicast.
  988. const uint8_t allowed_unicast[] = {
  989. DHCPV6_REQUEST,
  990. DHCPV6_RENEW,
  991. DHCPV6_RELEASE,
  992. DHCPV6_DECLINE,
  993. DHCPV6_RELAY_FORW
  994. };
  995. // Iterate over these messages and check that they are accepted being
  996. // sent to unicast.
  997. for (int i = 0; i < sizeof(allowed_unicast); ++i) {
  998. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(allowed_unicast[i], 1234));
  999. msg->setLocalAddr(IOAddress("2001:db8:1::1"));
  1000. msg->addOption(srv.getServerID());
  1001. EXPECT_TRUE(srv.testUnicast(msg))
  1002. << "server doesn't accept message type "
  1003. << static_cast<int>(allowed_unicast[i])
  1004. << "being sent to unicast address";
  1005. }
  1006. }
  1007. // This test verifies if selectSubnet() selects proper subnet for a given
  1008. // source address.
  1009. TEST_F(Dhcpv6SrvTest, selectSubnetAddr) {
  1010. NakedDhcpv6Srv srv(0);
  1011. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1012. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1013. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1014. // CASE 1: We have only one subnet defined and we received local traffic.
  1015. // The only available subnet used to be picked, but not anymore
  1016. CfgMgr::instance().clear();
  1017. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1018. CfgMgr::instance().commit();
  1019. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1020. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1021. // The clause for assuming local subnet if there is only one subnet is was
  1022. // removed.
  1023. EXPECT_FALSE(srv.selectSubnet(pkt));
  1024. // CASE 2: We have only one subnet defined and we received relayed traffic.
  1025. // We should NOT select it.
  1026. // Identical steps as in case 1, but repeated for clarity
  1027. CfgMgr::instance().clear();
  1028. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1029. CfgMgr::instance().commit();
  1030. pkt->setRemoteAddr(IOAddress("2001:db8:abcd::2345"));
  1031. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1032. EXPECT_FALSE(selected);
  1033. // CASE 3: We have three subnets defined and we received local traffic.
  1034. // Nothing should be selected.
  1035. CfgMgr::instance().clear();
  1036. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1037. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1038. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1039. CfgMgr::instance().commit();
  1040. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1041. selected = srv.selectSubnet(pkt);
  1042. EXPECT_FALSE(selected);
  1043. // CASE 4: We have three subnets defined and we received relayed traffic
  1044. // that came out of subnet 2. We should select subnet2 then
  1045. CfgMgr::instance().clear();
  1046. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1047. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1048. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1049. CfgMgr::instance().commit();
  1050. pkt->setRemoteAddr(IOAddress("2001:db8:2::baca"));
  1051. selected = srv.selectSubnet(pkt);
  1052. EXPECT_EQ(selected, subnet2);
  1053. // CASE 5: We have three subnets defined and we received relayed traffic
  1054. // that came out of undefined subnet. We should select nothing
  1055. CfgMgr::instance().clear();
  1056. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1057. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1058. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1059. CfgMgr::instance().commit();
  1060. pkt->setRemoteAddr(IOAddress("2001:db8:4::baca"));
  1061. EXPECT_FALSE(srv.selectSubnet(pkt));
  1062. }
  1063. // This test verifies if selectSubnet() selects proper subnet for a given
  1064. // network interface name.
  1065. TEST_F(Dhcpv6SrvTest, selectSubnetIface) {
  1066. NakedDhcpv6Srv srv(0);
  1067. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1068. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1069. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1070. subnet1->setIface("eth0");
  1071. subnet3->setIface("wifi1");
  1072. // CASE 1: We have only one subnet defined and it is available via eth0.
  1073. // Packet came from eth0. The only available subnet should be selected
  1074. CfgMgr::instance().clear();
  1075. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1076. CfgMgr::instance().commit();
  1077. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1078. pkt->setIface("eth0");
  1079. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1080. EXPECT_EQ(selected, subnet1);
  1081. // CASE 2: We have only one subnet defined and it is available via eth0.
  1082. // Packet came from eth1. We should not select it
  1083. CfgMgr::instance().clear();
  1084. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1085. CfgMgr::instance().commit();
  1086. pkt->setIface("eth1");
  1087. selected = srv.selectSubnet(pkt);
  1088. EXPECT_FALSE(selected);
  1089. // CASE 3: We have only 3 subnets defined, one over eth0, one remote and
  1090. // one over wifi1.
  1091. // Packet came from eth1. We should not select it
  1092. CfgMgr::instance().clear();
  1093. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1094. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1095. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1096. CfgMgr::instance().commit();
  1097. pkt->setIface("eth0");
  1098. EXPECT_EQ(subnet1, srv.selectSubnet(pkt));
  1099. pkt->setIface("eth3"); // no such interface
  1100. EXPECT_EQ(Subnet6Ptr(), srv.selectSubnet(pkt)); // nothing selected
  1101. pkt->setIface("wifi1");
  1102. EXPECT_EQ(subnet3, srv.selectSubnet(pkt));
  1103. }
  1104. // This test verifies if selectSubnet() selects proper subnet for a given
  1105. // linkaddr in RELAY-FORW message
  1106. TEST_F(Dhcpv6SrvTest, selectSubnetRelayLinkaddr) {
  1107. NakedDhcpv6Srv srv(0);
  1108. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1109. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1110. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1111. Pkt6::RelayInfo relay;
  1112. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1113. relay.peeraddr_ = IOAddress("fe80::1");
  1114. // CASE 1: We have only one subnet defined and we received relayed traffic.
  1115. // The only available subnet should NOT be selected.
  1116. CfgMgr::instance().clear();
  1117. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1118. CfgMgr::instance().commit();
  1119. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1120. pkt->relay_info_.push_back(relay);
  1121. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1122. EXPECT_FALSE(selected);
  1123. // CASE 2: We have three subnets defined and we received relayed traffic
  1124. // that came out of subnet 2. We should select subnet2 then
  1125. CfgMgr::instance().clear();
  1126. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1127. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1128. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1129. CfgMgr::instance().commit();
  1130. selected = srv.selectSubnet(pkt);
  1131. EXPECT_EQ(selected, subnet2);
  1132. // Source of the packet should have no meaning. Selection is based
  1133. // on linkaddr field in the relay
  1134. pkt->setRemoteAddr(IOAddress("2001:db8:1::baca"));
  1135. selected = srv.selectSubnet(pkt);
  1136. EXPECT_EQ(selected, subnet2);
  1137. // But not when this linkaddr field is not usable.
  1138. Pkt6::RelayInfo relay2;
  1139. relay2.peeraddr_ = IOAddress("fe80::1");
  1140. pkt->relay_info_.clear();
  1141. pkt->relay_info_.push_back(relay2);
  1142. selected = srv.selectSubnet(pkt);
  1143. EXPECT_EQ(selected, subnet1);
  1144. // CASE 3: We have three subnets defined and we received relayed traffic
  1145. // that came out a layer 2 relay on subnet 2. We should select subnet2 then
  1146. CfgMgr::instance().clear();
  1147. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1148. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1149. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1150. CfgMgr::instance().commit();
  1151. pkt->relay_info_.clear();
  1152. pkt->relay_info_.push_back(relay);
  1153. relay2.hop_count_ = 1;
  1154. pkt->relay_info_.push_back(relay2);
  1155. selected = srv.selectSubnet(pkt);
  1156. EXPECT_EQ(selected, subnet2);
  1157. // The number of level 2 relay doesn't matter
  1158. pkt->relay_info_.clear();
  1159. Pkt6::RelayInfo relay20;
  1160. relay20.peeraddr_ = IOAddress("fe80::1");
  1161. pkt->relay_info_.push_back(relay20);
  1162. Pkt6::RelayInfo relay21;
  1163. relay21.peeraddr_ = IOAddress("fe80::1");
  1164. relay21.hop_count_ = 1;
  1165. pkt->relay_info_.push_back(relay21);
  1166. relay.hop_count_ = 2;
  1167. pkt->relay_info_.push_back(relay);
  1168. Pkt6::RelayInfo relay22;
  1169. relay22.peeraddr_ = IOAddress("fe80::1");
  1170. relay22.hop_count_ = 3;
  1171. pkt->relay_info_.push_back(relay22);
  1172. Pkt6::RelayInfo relay23;
  1173. relay23.peeraddr_ = IOAddress("fe80::1");
  1174. relay23.hop_count_ = 4;
  1175. pkt->relay_info_.push_back(relay23);
  1176. selected = srv.selectSubnet(pkt);
  1177. EXPECT_EQ(selected, subnet2);
  1178. // Only the inner/last relay with a usable address matters
  1179. pkt->relay_info_.clear();
  1180. pkt->relay_info_.push_back(relay20);
  1181. pkt->relay_info_.push_back(relay21);
  1182. pkt->relay_info_.push_back(relay);
  1183. pkt->relay_info_.push_back(relay22);
  1184. Pkt6::RelayInfo relay3;
  1185. relay3.linkaddr_ = IOAddress("2001:db8:3::1234");
  1186. relay3.peeraddr_ = IOAddress("fe80::1");
  1187. relay3.hop_count_ = 4;
  1188. pkt->relay_info_.push_back(relay3);
  1189. selected = srv.selectSubnet(pkt);
  1190. EXPECT_EQ(selected, subnet3);
  1191. // CASE 4: We have three subnets defined and we received relayed traffic
  1192. // that came out of undefined subnet. We should select nothing
  1193. CfgMgr::instance().clear();
  1194. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1195. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1196. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1197. CfgMgr::instance().commit();
  1198. pkt->relay_info_.clear();
  1199. relay.hop_count_ = 0;
  1200. relay.linkaddr_ = IOAddress("2001:db8:4::1234");
  1201. pkt->relay_info_.push_back(relay);
  1202. selected = srv.selectSubnet(pkt);
  1203. EXPECT_FALSE(selected);
  1204. }
  1205. // This test verifies if selectSubnet() selects proper subnet for a given
  1206. // interface-id option
  1207. TEST_F(Dhcpv6SrvTest, selectSubnetRelayInterfaceId) {
  1208. NakedDhcpv6Srv srv(0);
  1209. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1210. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1211. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1212. subnet1->setInterfaceId(generateInterfaceId("relay1"));
  1213. subnet2->setInterfaceId(generateInterfaceId("relay2"));
  1214. // CASE 1: We have only one subnet defined and it is for interface-id "relay1"
  1215. // Packet came with interface-id "relay2". We should not select subnet1
  1216. CfgMgr::instance().clear();
  1217. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1218. CfgMgr::instance().commit();
  1219. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1220. Pkt6::RelayInfo relay;
  1221. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1222. relay.peeraddr_ = IOAddress("fe80::1");
  1223. OptionPtr opt = generateInterfaceId("relay2");
  1224. relay.options_.insert(make_pair(opt->getType(), opt));
  1225. pkt->relay_info_.push_back(relay);
  1226. // There is only one subnet configured and we are outside of that subnet
  1227. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1228. EXPECT_FALSE(selected);
  1229. // CASE 2: We have only one subnet defined and it is for interface-id "relay2"
  1230. // Packet came with interface-id "relay2". We should select it
  1231. CfgMgr::instance().clear();
  1232. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2); // just a single subnet
  1233. CfgMgr::instance().commit();
  1234. selected = srv.selectSubnet(pkt);
  1235. EXPECT_EQ(selected, subnet2);
  1236. // CASE 3: We have only 3 subnets defined: one remote for interface-id "relay1",
  1237. // one remote for interface-id "relay2" and third local
  1238. // packet comes with interface-id "relay2". We should select subnet2
  1239. CfgMgr::instance().clear();
  1240. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1241. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1242. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1243. CfgMgr::instance().commit();
  1244. EXPECT_EQ(subnet2, srv.selectSubnet(pkt));
  1245. }
  1246. // Checks if server responses are sent to the proper port.
  1247. TEST_F(Dhcpv6SrvTest, portsDirectTraffic) {
  1248. NakedDhcpv6Srv srv(0);
  1249. // Let's create a simple SOLICIT
  1250. Pkt6Ptr sol = PktCaptures::captureSimpleSolicit();
  1251. // Simulate that we have received that traffic
  1252. srv.fakeReceive(sol);
  1253. // Server will now process to run its normal loop, but instead of calling
  1254. // IfaceMgr::receive6(), it will read all packets from the list set by
  1255. // fakeReceive()
  1256. srv.run();
  1257. // Get Advertise...
  1258. ASSERT_FALSE(srv.fake_sent_.empty());
  1259. Pkt6Ptr adv = srv.fake_sent_.front();
  1260. ASSERT_TRUE(adv);
  1261. // This is sent back to client directly, should be port 546
  1262. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1263. }
  1264. // Checks if server responses are sent to the proper port.
  1265. TEST_F(Dhcpv6SrvTest, portsRelayedTraffic) {
  1266. NakedDhcpv6Srv srv(0);
  1267. // Let's create a simple SOLICIT
  1268. Pkt6Ptr sol = PktCaptures::captureRelayedSolicit();
  1269. // Simulate that we have received that traffic
  1270. srv.fakeReceive(sol);
  1271. // Server will now process to run its normal loop, but instead of calling
  1272. // IfaceMgr::receive6(), it will read all packets from the list set by
  1273. // fakeReceive()
  1274. srv.run();
  1275. // Get Advertise...
  1276. ASSERT_FALSE(srv.fake_sent_.empty());
  1277. Pkt6Ptr adv = srv.fake_sent_.front();
  1278. ASSERT_TRUE(adv);
  1279. // This is sent back to relay, so port is 547
  1280. EXPECT_EQ(DHCP6_SERVER_PORT, adv->getRemotePort());
  1281. }
  1282. // Checks effect of persistency (aka always-true) flag on the ORO
  1283. TEST_F(Dhcpv6SrvTest, prlPersistency) {
  1284. IfaceMgrTestConfig test_config(true);
  1285. ASSERT_NO_THROW(configure(CONFIGS[2]));
  1286. // Create a packet with enough to select the subnet and go through
  1287. // the SOLICIT processing
  1288. Pkt6Ptr sol(new Pkt6(DHCPV6_SOLICIT, 1234));
  1289. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1290. sol->setIface("eth0");
  1291. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1292. OptionPtr clientid = generateClientId();
  1293. sol->addOption(clientid);
  1294. // Create and add an ORO for another option
  1295. OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6, D6O_ORO));
  1296. ASSERT_TRUE(oro);
  1297. oro->addValue(D6O_SNTP_SERVERS);
  1298. sol->addOption(oro);
  1299. // Let the server process it and generate a response.
  1300. Pkt6Ptr response = srv_.processSolicit(sol);
  1301. // The server should add a subscriber-id option
  1302. ASSERT_TRUE(response->getOption(D6O_SUBSCRIBER_ID));
  1303. // But no dns-servers
  1304. ASSERT_FALSE(response->getOption(D6O_NAME_SERVERS));
  1305. // Nor a sntp-servers
  1306. ASSERT_FALSE(response->getOption(D6O_SNTP_SERVERS));
  1307. // Reset ORO adding dns-servers
  1308. sol->delOption(D6O_ORO);
  1309. oro->addValue(D6O_NAME_SERVERS);
  1310. sol->addOption(oro);
  1311. // Let the server process it again. This time the name-servers
  1312. // option should be present.
  1313. response = srv_.processSolicit(sol);
  1314. // Processing should add a subscriber-id option
  1315. ASSERT_TRUE(response->getOption(D6O_SUBSCRIBER_ID));
  1316. // and now a dns-servers
  1317. ASSERT_TRUE(response->getOption(D6O_NAME_SERVERS));
  1318. // and still no sntp-servers
  1319. ASSERT_FALSE(response->getOption(D6O_SNTP_SERVERS));
  1320. }
  1321. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1322. // @todo Uncomment this test as part of #3180 work.
  1323. // Kea code currently fails to handle docsis traffic.
  1324. TEST_F(Dhcpv6SrvTest, docsisTraffic) {
  1325. NakedDhcpv6Srv srv(0);
  1326. // Let's get a traffic capture from DOCSIS3.0 modem
  1327. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1328. // Simulate that we have received that traffic
  1329. srv.fakeReceive(sol);
  1330. // Server will now process to run its normal loop, but instead of calling
  1331. // IfaceMgr::receive6(), it will read all packets from the list set by
  1332. // fakeReceive()
  1333. srv.run();
  1334. // We should have an Advertise in response
  1335. ASSERT_FALSE(srv.fake_sent_.empty());
  1336. Pkt6Ptr adv = srv.fake_sent_.front();
  1337. ASSERT_TRUE(adv);
  1338. }
  1339. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1340. TEST_F(Dhcpv6SrvTest, docsisVendorOptionsParse) {
  1341. // Let's get a traffic capture from DOCSIS3.0 modem
  1342. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1343. EXPECT_NO_THROW(sol->unpack());
  1344. // Check if the packet contain
  1345. OptionPtr opt = sol->getOption(D6O_VENDOR_OPTS);
  1346. ASSERT_TRUE(opt);
  1347. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1348. ASSERT_TRUE(vendor);
  1349. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_ORO));
  1350. EXPECT_TRUE(vendor->getOption(36));
  1351. EXPECT_TRUE(vendor->getOption(35));
  1352. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_DEVICE_TYPE));
  1353. EXPECT_TRUE(vendor->getOption(3));
  1354. EXPECT_TRUE(vendor->getOption(4));
  1355. EXPECT_TRUE(vendor->getOption(5));
  1356. EXPECT_TRUE(vendor->getOption(6));
  1357. EXPECT_TRUE(vendor->getOption(7));
  1358. EXPECT_TRUE(vendor->getOption(8));
  1359. EXPECT_TRUE(vendor->getOption(9));
  1360. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_VENDOR_NAME));
  1361. EXPECT_TRUE(vendor->getOption(15));
  1362. EXPECT_FALSE(vendor->getOption(20));
  1363. EXPECT_FALSE(vendor->getOption(11));
  1364. EXPECT_FALSE(vendor->getOption(17));
  1365. }
  1366. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  1367. TEST_F(Dhcpv6SrvTest, docsisVendorORO) {
  1368. NakedDhcpv6Srv srv(0);
  1369. // Let's get a traffic capture from DOCSIS3.0 modem
  1370. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1371. ASSERT_NO_THROW(sol->unpack());
  1372. // Check if the packet contains vendor options option
  1373. OptionPtr opt = sol->getOption(D6O_VENDOR_OPTS);
  1374. ASSERT_TRUE(opt);
  1375. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1376. ASSERT_TRUE(vendor);
  1377. opt = vendor->getOption(DOCSIS3_V6_ORO);
  1378. ASSERT_TRUE(opt);
  1379. OptionUint16ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint16Array>(opt);
  1380. EXPECT_TRUE(oro);
  1381. }
  1382. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1383. // vendor options is parsed correctly and the requested options are actually assigned.
  1384. TEST_F(Dhcpv6SrvTest, vendorOptionsORO) {
  1385. IfaceMgrTestConfig test_config(true);
  1386. string config = "{ \"interfaces-config\": {"
  1387. " \"interfaces\": [ \"*\" ]"
  1388. "},"
  1389. "\"preferred-lifetime\": 3000,"
  1390. "\"rebind-timer\": 2000, "
  1391. "\"renew-timer\": 1000, "
  1392. " \"option-def\": [ {"
  1393. " \"name\": \"config-file\","
  1394. " \"code\": 33,"
  1395. " \"type\": \"string\","
  1396. " \"space\": \"vendor-4491\""
  1397. " } ],"
  1398. " \"option-data\": [ {"
  1399. " \"name\": \"config-file\","
  1400. " \"space\": \"vendor-4491\","
  1401. " \"data\": \"normal_erouter_v6.cm\""
  1402. " }],"
  1403. "\"subnet6\": [ { "
  1404. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1405. " \"subnet\": \"2001:db8:1::/48\", "
  1406. " \"renew-timer\": 1000, "
  1407. " \"rebind-timer\": 1000, "
  1408. " \"preferred-lifetime\": 3000,"
  1409. " \"valid-lifetime\": 4000,"
  1410. " \"interface-id\": \"\","
  1411. " \"interface\": \"eth0\""
  1412. " } ],"
  1413. "\"valid-lifetime\": 4000 }";
  1414. ASSERT_NO_THROW(configure(config));
  1415. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1416. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1417. sol->setIface("eth0");
  1418. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1419. OptionPtr clientid = generateClientId();
  1420. sol->addOption(clientid);
  1421. // Pass it to the server and get an advertise
  1422. Pkt6Ptr adv = srv_.processSolicit(sol);
  1423. // check if we get response at all
  1424. ASSERT_TRUE(adv);
  1425. // We did not include any vendor opts in SOLICIT, so there should be none
  1426. // in ADVERTISE.
  1427. ASSERT_FALSE(adv->getOption(D6O_VENDOR_OPTS));
  1428. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  1429. // That suboption has code 1 and is a docsis ORO option.
  1430. boost::shared_ptr<OptionUint16Array> vendor_oro(new OptionUint16Array(Option::V6,
  1431. DOCSIS3_V6_ORO));
  1432. vendor_oro->addValue(DOCSIS3_V6_CONFIG_FILE); // Request option 33
  1433. OptionPtr vendor(new OptionVendor(Option::V6, 4491));
  1434. vendor->addOption(vendor_oro);
  1435. sol->addOption(vendor);
  1436. // Need to process SOLICIT again after requesting new option.
  1437. adv = srv_.processSolicit(sol);
  1438. ASSERT_TRUE(adv);
  1439. // Check if there is vendor option response
  1440. OptionPtr tmp = adv->getOption(D6O_VENDOR_OPTS);
  1441. ASSERT_TRUE(tmp);
  1442. // The response should be OptionVendor object
  1443. boost::shared_ptr<OptionVendor> vendor_resp =
  1444. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1445. ASSERT_TRUE(vendor_resp);
  1446. OptionPtr docsis33 = vendor_resp->getOption(33);
  1447. ASSERT_TRUE(docsis33);
  1448. OptionStringPtr config_file = boost::dynamic_pointer_cast<OptionString>(docsis33);
  1449. ASSERT_TRUE(config_file);
  1450. EXPECT_EQ("normal_erouter_v6.cm", config_file->getValue());
  1451. }
  1452. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1453. // vendor options is parsed correctly and the persistent options are actually assigned.
  1454. TEST_F(Dhcpv6SrvTest, vendorPersistentOptions) {
  1455. IfaceMgrTestConfig test_config(true);
  1456. string config = "{ \"interfaces-config\": {"
  1457. " \"interfaces\": [ \"*\" ]"
  1458. "},"
  1459. "\"preferred-lifetime\": 3000,"
  1460. "\"rebind-timer\": 2000, "
  1461. "\"renew-timer\": 1000, "
  1462. " \"option-def\": [ {"
  1463. " \"name\": \"config-file\","
  1464. " \"code\": 33,"
  1465. " \"type\": \"string\","
  1466. " \"space\": \"vendor-4491\""
  1467. " } ],"
  1468. " \"option-data\": [ {"
  1469. " \"name\": \"config-file\","
  1470. " \"space\": \"vendor-4491\","
  1471. " \"data\": \"normal_erouter_v6.cm\","
  1472. " \"always-send\": true"
  1473. " }],"
  1474. "\"subnet6\": [ { "
  1475. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1476. " \"subnet\": \"2001:db8:1::/48\", "
  1477. " \"renew-timer\": 1000, "
  1478. " \"rebind-timer\": 1000, "
  1479. " \"preferred-lifetime\": 3000,"
  1480. " \"valid-lifetime\": 4000,"
  1481. " \"interface-id\": \"\","
  1482. " \"interface\": \"eth0\""
  1483. " } ],"
  1484. "\"valid-lifetime\": 4000 }";
  1485. ASSERT_NO_THROW(configure(config));
  1486. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1487. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1488. sol->setIface("eth0");
  1489. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1490. OptionPtr clientid = generateClientId();
  1491. sol->addOption(clientid);
  1492. // Let's add a vendor-option (vendor-id=4491).
  1493. OptionPtr vendor(new OptionVendor(Option::V6, 4491));
  1494. sol->addOption(vendor);
  1495. // Pass it to the server and get an advertise
  1496. Pkt6Ptr adv = srv_.processSolicit(sol);
  1497. // check if we get response at all
  1498. ASSERT_TRUE(adv);
  1499. // Check if there is vendor option response
  1500. OptionPtr tmp = adv->getOption(D6O_VENDOR_OPTS);
  1501. ASSERT_TRUE(tmp);
  1502. // The response should be OptionVendor object
  1503. boost::shared_ptr<OptionVendor> vendor_resp =
  1504. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1505. ASSERT_TRUE(vendor_resp);
  1506. OptionPtr docsis33 = vendor_resp->getOption(33);
  1507. ASSERT_TRUE(docsis33);
  1508. OptionStringPtr config_file = boost::dynamic_pointer_cast<OptionString>(docsis33);
  1509. ASSERT_TRUE(config_file);
  1510. EXPECT_EQ("normal_erouter_v6.cm", config_file->getValue());
  1511. }
  1512. // Test checks whether it is possible to use option definitions defined in
  1513. // src/lib/dhcp/docsis3_option_defs.h.
  1514. TEST_F(Dhcpv6SrvTest, vendorOptionsDocsisDefinitions) {
  1515. ConstElementPtr x;
  1516. string config_prefix = "{ \"interfaces-config\": {"
  1517. " \"interfaces\": [ ]"
  1518. "},"
  1519. "\"preferred-lifetime\": 3000,"
  1520. "\"rebind-timer\": 2000, "
  1521. "\"renew-timer\": 1000, "
  1522. " \"option-data\": [ {"
  1523. " \"name\": \"config-file\","
  1524. " \"space\": \"vendor-4491\","
  1525. " \"code\": ";
  1526. string config_postfix = ","
  1527. " \"data\": \"normal_erouter_v6.cm\","
  1528. " \"csv-format\": true"
  1529. " }],"
  1530. "\"subnet6\": [ { "
  1531. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1532. " \"subnet\": \"2001:db8:1::/48\", "
  1533. " \"renew-timer\": 1000, "
  1534. " \"rebind-timer\": 1000, "
  1535. " \"preferred-lifetime\": 3000,"
  1536. " \"valid-lifetime\": 4000,"
  1537. " \"interface-id\": \"\","
  1538. " \"interface\": \"\""
  1539. " } ],"
  1540. "\"valid-lifetime\": 4000 }";
  1541. // There is docsis3 (vendor-id=4491) vendor option 33, which is a
  1542. // config-file. Its format is a single string.
  1543. string config_valid = config_prefix + "33" + config_postfix;
  1544. // There is no option 99 defined in vendor-id=4491. As there is no
  1545. // definition, the config should fail.
  1546. string config_bogus = config_prefix + "99" + config_postfix;
  1547. ConstElementPtr json_bogus;
  1548. ASSERT_NO_THROW(json_bogus = parseDHCP6(config_bogus));
  1549. ConstElementPtr json_valid;
  1550. ASSERT_NO_THROW(json_valid = parseDHCP6(config_valid));
  1551. NakedDhcpv6Srv srv(0);
  1552. // This should fail (missing option definition)
  1553. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json_bogus));
  1554. ASSERT_TRUE(x);
  1555. comment_ = isc::config::parseAnswer(rcode_, x);
  1556. ASSERT_EQ(1, rcode_);
  1557. // This should work (option definition present)
  1558. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json_valid));
  1559. ASSERT_TRUE(x);
  1560. comment_ = isc::config::parseAnswer(rcode_, x);
  1561. ASSERT_EQ(0, rcode_);
  1562. }
  1563. // This test checks that the server will handle a Solicit with the Vendor Class
  1564. // having a length of 4 (enterprise-id only).
  1565. TEST_F(Dhcpv6SrvTest, cableLabsShortVendorClass) {
  1566. NakedDhcpv6Srv srv(0);
  1567. // Create a simple Solicit with the 4-byte long vendor class option.
  1568. Pkt6Ptr sol = PktCaptures::captureCableLabsShortVendorClass();
  1569. // Simulate that we have received that traffic
  1570. srv.fakeReceive(sol);
  1571. // Server will now process to run its normal loop, but instead of calling
  1572. // IfaceMgr::receive6(), it will read all packets from the list set by
  1573. // fakeReceive()
  1574. srv.run();
  1575. // Get Advertise...
  1576. ASSERT_FALSE(srv.fake_sent_.empty());
  1577. Pkt6Ptr adv = srv.fake_sent_.front();
  1578. ASSERT_TRUE(adv);
  1579. // This is sent back to client, so port is 546
  1580. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1581. }
  1582. // Checks if relay IP address specified in the relay-info structure in
  1583. // subnet6 is being used properly.
  1584. TEST_F(Dhcpv6SrvTest, relayOverride) {
  1585. // We have 2 subnets defined. Note that both have a relay address
  1586. // defined. Both are not belonging to the subnets. That is
  1587. // important, because if the relay belongs to the subnet, there's
  1588. // no need to specify relay override.
  1589. string config = "{ \"interfaces-config\": {"
  1590. " \"interfaces\": [ \"*\" ]"
  1591. "},"
  1592. "\"preferred-lifetime\": 3000,"
  1593. "\"rebind-timer\": 2000, "
  1594. "\"renew-timer\": 1000, "
  1595. "\"subnet6\": [ "
  1596. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1597. " \"subnet\": \"2001:db8:1::/48\", "
  1598. " \"relay\": { "
  1599. " \"ip-address\": \"2001:db8:3::1\""
  1600. " }"
  1601. " }, "
  1602. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1603. " \"subnet\": \"2001:db8:2::/48\", "
  1604. " \"relay\": { "
  1605. " \"ip-address\": \"2001:db8:3::2\""
  1606. " }"
  1607. " } "
  1608. "],"
  1609. "\"valid-lifetime\": 4000 }";
  1610. // Use this config to set up the server
  1611. ASSERT_NO_THROW(configure(config));
  1612. // Let's get the subnet configuration objects
  1613. const Subnet6Collection* subnets =
  1614. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1615. ASSERT_EQ(2, subnets->size());
  1616. // Let's get them for easy reference
  1617. Subnet6Ptr subnet1 = (*subnets)[0];
  1618. Subnet6Ptr subnet2 = (*subnets)[1];
  1619. ASSERT_TRUE(subnet1);
  1620. ASSERT_TRUE(subnet2);
  1621. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1622. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1623. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1624. OptionPtr clientid = generateClientId();
  1625. sol->addOption(clientid);
  1626. // Now pretend the packet came via one relay.
  1627. Pkt6::RelayInfo relay;
  1628. relay.linkaddr_ = IOAddress("2001:db8:1::1");
  1629. relay.peeraddr_ = IOAddress("fe80::1");
  1630. sol->relay_info_.push_back(relay);
  1631. // This is just a sanity check, we're using regular method: the relay
  1632. // belongs to the first (2001:db8:1::/64) subnet, so it's an easy decision.
  1633. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1634. // Relay belongs to the second subnet, so it should be selected.
  1635. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:2::1");
  1636. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1637. // Now let's check if the relay override for the first subnets works
  1638. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:3::1");
  1639. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1640. // Now repeat that for relay matching the second subnet.
  1641. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:3::2");
  1642. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1643. // Finally, let's check that completely mismatched relay will not get us
  1644. // anything
  1645. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:1234::1");
  1646. EXPECT_FALSE(srv_.selectSubnet(sol));
  1647. }
  1648. /// @brief Creates RSOO option with suboptions
  1649. ///
  1650. /// Creates Relay-Supplied Options option that includes nested options. The
  1651. /// codes of those nested options are specified in codes parameter. Content of
  1652. /// the options is controlled with payload parameter. When it is zero, option
  1653. /// code will be used (e.g. option 100 will contain repeating bytes of value 100).
  1654. /// When non-zero is used, payload will be used. Each suboption length is always
  1655. /// set to the arbitrarily chosen value of 10.
  1656. ///
  1657. /// @param codes a vector of option codes to be created
  1658. /// @param payload specified payload (0 = fill payload with repeating option code)
  1659. /// @return RSOO with nested options
  1660. OptionPtr createRSOO(const std::vector<uint16_t>& codes, uint8_t payload = 0) {
  1661. OptionDefinitionPtr def = LibDHCP::getOptionDef(DHCP6_OPTION_SPACE, D6O_RSOO);
  1662. if (!def) {
  1663. isc_throw(BadValue, "Can't find RSOO definition");
  1664. }
  1665. OptionPtr rsoo_container(new OptionCustom(*def, Option::V6));
  1666. for (size_t i = 0; i < codes.size(); ++i) {
  1667. OptionBuffer buf(10, payload ? payload : codes[i]); // let's make the option 10 bytes long
  1668. rsoo_container->addOption(OptionPtr(new Option(Option::V6, codes[i], buf)));
  1669. }
  1670. return (rsoo_container);
  1671. }
  1672. // Test that the server processes RSOO (Relay Supplied Options option) correctly,
  1673. // i.e. it includes in its response the options that are inserted by the relay.
  1674. // The server must do this only for options that are RSOO-enabled.
  1675. TEST_F(Dhcpv6SrvTest, rsoo) {
  1676. Dhcp6Client client;
  1677. string config =
  1678. "{"
  1679. " \"relay-supplied-options\": [ \"110\", \"120\", \"130\" ],"
  1680. " \"preferred-lifetime\": 3000,"
  1681. " \"rebind-timer\": 2000, "
  1682. " \"renew-timer\": 1000, "
  1683. " \"subnet6\": [ { "
  1684. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  1685. " \"subnet\": \"2001:db8::/48\" "
  1686. " } ],"
  1687. " \"valid-lifetime\": 4000"
  1688. "}";
  1689. EXPECT_NO_THROW(configure(config, *client.getServer()));
  1690. // Now pretend the packet came via one relay.
  1691. Pkt6::RelayInfo relay;
  1692. relay.msg_type_ = DHCPV6_RELAY_FORW;
  1693. relay.hop_count_ = 1;
  1694. relay.linkaddr_ = IOAddress("2001:db8::1");
  1695. relay.peeraddr_ = IOAddress("fe80::1");
  1696. vector<uint16_t> rsoo1;
  1697. rsoo1.push_back(109);
  1698. rsoo1.push_back(110);
  1699. rsoo1.push_back(111);
  1700. // The relay will request echoing back 3 options: 109, 110, 111.
  1701. // The configuration allows echoing back only 110.
  1702. OptionPtr opt = createRSOO(rsoo1);
  1703. relay.options_.insert(make_pair(opt->getType(), opt));
  1704. client.relay_info_.push_back(relay);
  1705. client.doSARR();
  1706. // Option 110 should be copied to the client
  1707. EXPECT_FALSE(client.config_.options_.find(110) == client.config_.options_.end());
  1708. // Options 109 and 111 should not be copied (they are not RSOO-enabled)
  1709. EXPECT_TRUE(client.config_.options_.find(109) == client.config_.options_.end());
  1710. EXPECT_TRUE(client.config_.options_.find(111) == client.config_.options_.end());
  1711. }
  1712. // Test that the server processes RSOO (Relay Supplied Options option) correctly
  1713. // when there are more relays. In particular, the following case is tested:
  1714. // if relay1 inserts option A and B, relay2 inserts option B and C, the response
  1715. // should include options A, B and C. The server must use instance of option B
  1716. // that comes from the first relay, not the second one.
  1717. TEST_F(Dhcpv6SrvTest, rsoo2relays) {
  1718. Dhcp6Client client;
  1719. string config =
  1720. "{"
  1721. " \"relay-supplied-options\": [ \"110\", \"120\", \"130\" ],"
  1722. " \"preferred-lifetime\": 3000,"
  1723. " \"rebind-timer\": 2000, "
  1724. " \"renew-timer\": 1000, "
  1725. " \"subnet6\": [ { "
  1726. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  1727. " \"subnet\": \"2001:db8::/48\" "
  1728. " } ],"
  1729. " \"valid-lifetime\": 4000"
  1730. "}";
  1731. EXPECT_NO_THROW(configure(config, *client.getServer()));
  1732. // Now pretend the packet came via two relays.
  1733. // This situation reflects the following case:
  1734. // client----relay1----relay2----server
  1735. // Fabricate the first relay.
  1736. Pkt6::RelayInfo relay1;
  1737. relay1.msg_type_ = DHCPV6_RELAY_FORW;
  1738. relay1.hop_count_ = 1;
  1739. relay1.linkaddr_ = IOAddress("2001:db8::1");
  1740. relay1.peeraddr_ = IOAddress("fe80::1");
  1741. vector<uint16_t> rsoo1;
  1742. rsoo1.push_back(110); // The relay1 will send 2 options: 110, 120
  1743. rsoo1.push_back(120);
  1744. OptionPtr opt = createRSOO(rsoo1, 1); // use 0x1 as payload
  1745. relay1.options_.insert(make_pair(opt->getType(), opt));
  1746. // Now the second relay.
  1747. Pkt6::RelayInfo relay2;
  1748. relay2.msg_type_ = DHCPV6_RELAY_FORW;
  1749. relay2.hop_count_ = 2;
  1750. relay2.linkaddr_ = IOAddress("2001:db8::2");
  1751. relay2.peeraddr_ = IOAddress("fe80::2");
  1752. vector<uint16_t> rsoo2;
  1753. rsoo2.push_back(120); // The relay2 will send 2 options: 120, 130
  1754. rsoo2.push_back(130);
  1755. opt = createRSOO(rsoo2, 2); // use 0x2 as payload
  1756. relay2.options_.insert(make_pair(opt->getType(), opt));
  1757. // The relays encapsulate packet in this order: relay1, relay2,
  1758. // but the server decapsulates the packet in reverse order.
  1759. client.relay_info_.push_back(relay2);
  1760. client.relay_info_.push_back(relay1);
  1761. // There's a conflict here. Both relays want the server to echo back option
  1762. // 120. According to RFC6422, section 6:
  1763. //
  1764. // When such a conflict exists, the DHCP server MUST choose no more than
  1765. // one of these options to forward to the client. The DHCP server MUST
  1766. // NOT forward more than one of these options to the client.
  1767. //
  1768. // By default, the DHCP server MUST choose the innermost value -- the
  1769. // value supplied by the relay agent closest to the DHCP client -- to
  1770. // forward to the DHCP client.
  1771. // Let the client do his thing.
  1772. client.doSARR();
  1773. int count110 = 0; // Let's count how many times option 110 was echoed back
  1774. int count120 = 0; // Let's count how many times option 120 was echoed back
  1775. int count130 = 0; // Let's count how many times option 130 was echoed back
  1776. OptionPtr opt120;
  1777. for (OptionCollection::const_iterator it = client.config_.options_.begin();
  1778. it != client.config_.options_.end(); ++it) {
  1779. switch (it->second->getType()) {
  1780. case 110:
  1781. count110++;
  1782. break;
  1783. case 120:
  1784. count120++;
  1785. opt120 = it->second;
  1786. break;
  1787. case 130:
  1788. count130++;
  1789. break;
  1790. default:
  1791. break;
  1792. }
  1793. }
  1794. // We expect to have exactly one instance of each option code.
  1795. EXPECT_EQ(1, count110);
  1796. EXPECT_EQ(1, count120);
  1797. EXPECT_EQ(1, count130);
  1798. // Now, let's check if the proper instance of option 120 was sent. It should
  1799. // match the content of what the first relay had sent.
  1800. ASSERT_TRUE(opt120);
  1801. vector<uint8_t> expected(10, 1);
  1802. EXPECT_TRUE(expected == opt120->getData());
  1803. }
  1804. // This test verifies that the server will send the option for which it
  1805. // has a candidate, rather than the option sent by the relay in the RSOO.
  1806. TEST_F(Dhcpv6SrvTest, rsooOverride) {
  1807. Dhcp6Client client;
  1808. // The client will be requesting specific options.
  1809. client.useORO(true);
  1810. // The following configuration enables RSOO options: 110 and 120.
  1811. // It also configures the server with option 120 which should
  1812. // "override" the option 120 sent in the RSOO by the relay.
  1813. string config =
  1814. "{"
  1815. " \"relay-supplied-options\": [ \"110\", \"120\" ],"
  1816. " \"option-def\": [ {"
  1817. " \"name\": \"foo\","
  1818. " \"code\": 120,"
  1819. " \"type\": \"binary\""
  1820. " } ],"
  1821. " \"option-data\": [ {"
  1822. " \"code\": 120,"
  1823. " \"csv-format\": false,"
  1824. " \"data\": \"05\""
  1825. " } ],"
  1826. " \"preferred-lifetime\": 3000,"
  1827. " \"rebind-timer\": 2000, "
  1828. " \"renew-timer\": 1000, "
  1829. " \"subnet6\": [ { "
  1830. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  1831. " \"subnet\": \"2001:db8::/48\" "
  1832. " } ],"
  1833. " \"valid-lifetime\": 4000"
  1834. "}";
  1835. EXPECT_NO_THROW(configure(config, *client.getServer()));
  1836. // Fabricate the relay.
  1837. Pkt6::RelayInfo relay;
  1838. relay.msg_type_ = DHCPV6_RELAY_FORW;
  1839. relay.hop_count_ = 1;
  1840. relay.linkaddr_ = IOAddress("2001:db8::1");
  1841. relay.peeraddr_ = IOAddress("fe80::1");
  1842. vector<uint16_t> rsoo;
  1843. // The relay will send 2 options: 110, 120
  1844. rsoo.push_back(110);
  1845. rsoo.push_back(120);
  1846. // Use 0x1 as payload
  1847. OptionPtr opt = createRSOO(rsoo, 1);
  1848. relay.options_.insert(make_pair(opt->getType(), opt));
  1849. client.relay_info_.push_back(relay);
  1850. // Client should request option 120 in the ORO so as the server
  1851. // sends the configured option 120 to the client.
  1852. client.requestOption(120);
  1853. client.doSARR();
  1854. // The option 110 should be the one injected by the relay.
  1855. opt = client.config_.findOption(110);
  1856. ASSERT_TRUE(opt);
  1857. // We check that this is the option injected by the relay by
  1858. // checking option length. It should have 10 bytes long payload.
  1859. ASSERT_EQ(10, opt->getData().size());
  1860. // The second option should be the one configured on the server,
  1861. // rather than the one injected by the relay.
  1862. opt = client.config_.findOption(120);
  1863. ASSERT_TRUE(opt);
  1864. // It should have the size of 1.
  1865. ASSERT_EQ(1, opt->getData().size());
  1866. }
  1867. // Test checks if pkt6-advertise-received is bumped up correctly.
  1868. // Note that in properly configured network the server never receives Advertise
  1869. // messages.
  1870. TEST_F(Dhcpv6SrvTest, receiveAdvertiseStat) {
  1871. testReceiveStats(DHCPV6_ADVERTISE, "pkt6-advertise-received");
  1872. }
  1873. // Test checks if pkt6-reply-received is bumped up correctly.
  1874. // Note that in properly configured network the server never receives Reply
  1875. // messages.
  1876. TEST_F(Dhcpv6SrvTest, receiveReplyStat) {
  1877. testReceiveStats(DHCPV6_REPLY, "pkt6-reply-received");
  1878. }
  1879. // Test checks if pkt6-dhcpv4-response-received is bumped up correctly.
  1880. // Note that in properly configured network the server never receives
  1881. // Dhcpv4-Response messages.
  1882. TEST_F(Dhcpv6SrvTest, receiveDhcpv4ResponseStat) {
  1883. testReceiveStats(DHCPV6_DHCPV4_RESPONSE, "pkt6-dhcpv4-response-received");
  1884. }
  1885. // Test checks if pkt6-unknown-received is bumped up correctly.
  1886. TEST_F(Dhcpv6SrvTest, receiveUnknownStat) {
  1887. testReceiveStats(123, "pkt6-unknown-received");
  1888. }
  1889. // Test checks if pkt6-renew-received is bumped up correctly.
  1890. TEST_F(Dhcpv6SrvTest, receiveRenewStat) {
  1891. testReceiveStats(DHCPV6_RENEW, "pkt6-renew-received");
  1892. }
  1893. // Test checks if pkt6-rebind-received is bumped up correctly.
  1894. TEST_F(Dhcpv6SrvTest, receiveRebindStat) {
  1895. testReceiveStats(DHCPV6_REBIND, "pkt6-rebind-received");
  1896. }
  1897. // Test checks if pkt6-release-received is bumped up correctly.
  1898. TEST_F(Dhcpv6SrvTest, receiveReleaseStat) {
  1899. testReceiveStats(DHCPV6_RELEASE, "pkt6-release-received");
  1900. }
  1901. // Test checks if pkt6-decline-received is bumped up correctly.
  1902. TEST_F(Dhcpv6SrvTest, receiveDeclineStat) {
  1903. testReceiveStats(DHCPV6_DECLINE, "pkt6-decline-received");
  1904. }
  1905. // Test checks if pkt6-dhcpv4-query-received is bumped up correctly.
  1906. TEST_F(Dhcpv6SrvTest, receiveDhcpv4QueryStat) {
  1907. testReceiveStats(DHCPV6_DHCPV4_QUERY, "pkt6-dhcpv4-query-received");
  1908. }
  1909. // Test checks if reception of a malformed packet increases pkt-parse-failed
  1910. // and pkt6-receive-drop
  1911. TEST_F(Dhcpv6SrvTest, receiveParseFailedStat) {
  1912. using namespace isc::stats;
  1913. StatsMgr& mgr = StatsMgr::instance();
  1914. NakedDhcpv6Srv srv(0);
  1915. // Let's get a simple SOLICIT...
  1916. Pkt6Ptr pkt = PktCaptures::captureSimpleSolicit();
  1917. // And pretend it's packet is only 3 bytes long.
  1918. pkt->data_.resize(3);
  1919. // Check that those statistics are not set before the test
  1920. ObservationPtr pkt6_rcvd = mgr.getObservation("pkt6-received");
  1921. ObservationPtr parse_fail = mgr.getObservation("pkt6-parse-failed");
  1922. ObservationPtr recv_drop = mgr.getObservation("pkt6-receive-drop");
  1923. EXPECT_FALSE(pkt6_rcvd);
  1924. EXPECT_FALSE(parse_fail);
  1925. EXPECT_FALSE(recv_drop);
  1926. // Simulate that we have received that traffic
  1927. srv.fakeReceive(pkt);
  1928. // Server will now process to run its normal loop, but instead of calling
  1929. // IfaceMgr::receive6(), it will read all packets from the list set by
  1930. // fakeReceive()
  1931. srv.run();
  1932. // All expected statistics must be present.
  1933. pkt6_rcvd = mgr.getObservation("pkt6-received");
  1934. parse_fail = mgr.getObservation("pkt6-parse-failed");
  1935. recv_drop = mgr.getObservation("pkt6-receive-drop");
  1936. ASSERT_TRUE(pkt6_rcvd);
  1937. ASSERT_TRUE(parse_fail);
  1938. ASSERT_TRUE(recv_drop);
  1939. // They also must have expected values.
  1940. EXPECT_EQ(1, pkt6_rcvd->getInteger().first);
  1941. EXPECT_EQ(1, parse_fail->getInteger().first);
  1942. EXPECT_EQ(1, recv_drop->getInteger().first);
  1943. }
  1944. // This test verifies that the server is able to handle an empty DUID (client-id)
  1945. // in incoming client message.
  1946. TEST_F(Dhcpv6SrvTest, emptyClientId) {
  1947. Dhcp6Client client;
  1948. // The following configuration enables RSOO options: 110 and 120.
  1949. // It also configures the server with option 120 which should
  1950. // "override" the option 120 sent in the RSOO by the relay.
  1951. string config =
  1952. "{"
  1953. " \"preferred-lifetime\": 3000,"
  1954. " \"rebind-timer\": 2000, "
  1955. " \"renew-timer\": 1000, "
  1956. " \"subnet6\": [ { "
  1957. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  1958. " \"subnet\": \"2001:db8::/48\" "
  1959. " } ],"
  1960. " \"valid-lifetime\": 4000"
  1961. "}";
  1962. EXPECT_NO_THROW(configure(config, *client.getServer()));
  1963. // Tell the client to not send client-id on its own.
  1964. client.useClientId(false);
  1965. // Instead, tell him to send this extra option, which happens to be
  1966. // an empty client-id.
  1967. OptionPtr empty_client_id(new Option(Option::V6, D6O_CLIENTID));
  1968. client.addExtraOption(empty_client_id);
  1969. // Let's check whether the server is able to process this packet without
  1970. // throwing any exceptions. We don't care whether the server sent any
  1971. // responses or not. The goal is to check that the server didn't throw
  1972. // any exceptions.
  1973. EXPECT_NO_THROW(client.doSARR());
  1974. }
  1975. // This test verifies that the server is able to handle an empty DUID (server-id)
  1976. // in incoming client message.
  1977. TEST_F(Dhcpv6SrvTest, emptyServerId) {
  1978. Dhcp6Client client;
  1979. // The following configuration enables RSOO options: 110 and 120.
  1980. // It also configures the server with option 120 which should
  1981. // "override" the option 120 sent in the RSOO by the relay.
  1982. string config =
  1983. "{"
  1984. " \"preferred-lifetime\": 3000,"
  1985. " \"rebind-timer\": 2000, "
  1986. " \"renew-timer\": 1000, "
  1987. " \"subnet6\": [ { "
  1988. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  1989. " \"subnet\": \"2001:db8::/48\" "
  1990. " } ],"
  1991. " \"valid-lifetime\": 4000"
  1992. "}";
  1993. EXPECT_NO_THROW(configure(config, *client.getServer()));
  1994. // Tell the client to use this specific server-id.
  1995. OptionPtr empty_server_id(new Option(Option::V6, D6O_SERVERID));
  1996. client.useServerId(empty_server_id);
  1997. // Let's check whether the server is able to process this packet without
  1998. // throwing any exceptions. We don't care whether the server sent any
  1999. // responses or not. The goal is to check that the server didn't throw
  2000. // any exceptions.
  2001. EXPECT_NO_THROW(client.doSARR());
  2002. }
  2003. // This test verifies that the server is able to handle a too large DUID (server-id)
  2004. // in incoming client message.
  2005. TEST_F(Dhcpv6SrvTest, tooLongServerId) {
  2006. Dhcp6Client client;
  2007. // The following configuration enables RSOO options: 110 and 120.
  2008. // It also configures the server with option 120 which should
  2009. // "override" the option 120 sent in the RSOO by the relay.
  2010. string config =
  2011. "{"
  2012. " \"preferred-lifetime\": 3000,"
  2013. " \"rebind-timer\": 2000, "
  2014. " \"renew-timer\": 1000, "
  2015. " \"subnet6\": [ { "
  2016. " \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
  2017. " \"subnet\": \"2001:db8::/48\" "
  2018. " } ],"
  2019. " \"valid-lifetime\": 4000"
  2020. "}";
  2021. EXPECT_NO_THROW(configure(config, *client.getServer()));
  2022. // Tell the client to use this specific server-id.
  2023. std::vector<uint8_t> data(250, 250);
  2024. OptionPtr long_server_id(new Option(Option::V6, D6O_SERVERID, data));
  2025. client.useServerId(long_server_id);
  2026. // Let's check whether the server is able to process this packet without
  2027. // throwing any exceptions. We don't care whether the server sent any
  2028. // responses or not. The goal is to check that the server didn't throw
  2029. // any exceptions.
  2030. EXPECT_NO_THROW(client.doSARR());
  2031. }
  2032. /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
  2033. /// to call processX() methods.
  2034. /// @todo: Implement proper tests for MySQL lease/host database,
  2035. /// see ticket #4214.
  2036. } // end of anonymous namespace