dhcp6_srv_unittest.cc 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option6_addrlst.h>
  20. #include <dhcp/option6_ia.h>
  21. #include <dhcp/option6_iaaddr.h>
  22. #include <dhcp/option_int.h>
  23. #include <dhcp/option_int_array.h>
  24. #include <dhcp/option_string.h>
  25. #include <dhcp/option_vendor.h>
  26. #include <dhcp/option_vendor_class.h>
  27. #include <dhcp/iface_mgr.h>
  28. #include <dhcp6/json_config_parser.h>
  29. #include <dhcp/dhcp6.h>
  30. #include <dhcp/docsis3_option_defs.h>
  31. #include <dhcp/tests/iface_mgr_test_config.h>
  32. #include <dhcpsrv/cfgmgr.h>
  33. #include <dhcpsrv/lease_mgr.h>
  34. #include <dhcpsrv/lease_mgr_factory.h>
  35. #include <dhcpsrv/utils.h>
  36. #include <util/buffer.h>
  37. #include <util/range_utilities.h>
  38. #include <hooks/server_hooks.h>
  39. #include <dhcp6/tests/dhcp6_test_utils.h>
  40. #include <dhcp/tests/pkt_captures.h>
  41. #include <config/ccsession.h>
  42. #include <boost/pointer_cast.hpp>
  43. #include <boost/scoped_ptr.hpp>
  44. #include <gtest/gtest.h>
  45. #include <unistd.h>
  46. #include <fstream>
  47. #include <iostream>
  48. #include <sstream>
  49. using namespace isc;
  50. using namespace isc::data;
  51. using namespace isc::test;
  52. using namespace isc::asiolink;
  53. using namespace isc::dhcp;
  54. using namespace isc::dhcp::test;
  55. using namespace isc::util;
  56. using namespace isc::hooks;
  57. using namespace std;
  58. namespace {
  59. // This test verifies that incoming SOLICIT can be handled properly when
  60. // there are no subnets configured.
  61. //
  62. // This test sends a SOLICIT and the expected response
  63. // is an ADVERTISE with STATUS_NoAddrsAvail and no address provided in the
  64. // response
  65. TEST_F(NakedDhcpv6SrvTest, SolicitNoSubnet) {
  66. NakedDhcpv6Srv srv(0);
  67. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  68. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  69. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  70. OptionPtr clientid = generateClientId();
  71. sol->addOption(clientid);
  72. // Pass it to the server and get an advertise
  73. Pkt6Ptr reply = srv.processSolicit(sol);
  74. // check that we get the right NAK
  75. checkNakResponse (reply, DHCPV6_ADVERTISE, 1234, STATUS_NoAddrsAvail);
  76. }
  77. // This test verifies that incoming REQUEST can be handled properly when
  78. // there are no subnets configured.
  79. //
  80. // This test sends a REQUEST and the expected response
  81. // is an REPLY with STATUS_NoAddrsAvail and no address provided in the
  82. // response
  83. TEST_F(NakedDhcpv6SrvTest, RequestNoSubnet) {
  84. NakedDhcpv6Srv srv(0);
  85. // Let's create a REQUEST
  86. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  87. req->setRemoteAddr(IOAddress("fe80::abcd"));
  88. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  89. // with a hint
  90. IOAddress hint("2001:db8:1:1::dead:beef");
  91. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  92. ia->addOption(hint_opt);
  93. req->addOption(ia);
  94. OptionPtr clientid = generateClientId();
  95. req->addOption(clientid);
  96. // server-id is mandatory in REQUEST
  97. req->addOption(srv.getServerID());
  98. // Pass it to the server and hope for a REPLY
  99. Pkt6Ptr reply = srv.processRequest(req);
  100. // check that we get the right NAK
  101. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoAddrsAvail);
  102. }
  103. // This test verifies that incoming RENEW can be handled properly, even when
  104. // no subnets are configured.
  105. //
  106. // This test sends a RENEW and the expected response
  107. // is an REPLY with STATUS_NoBinding and no address provided in the
  108. // response
  109. TEST_F(NakedDhcpv6SrvTest, RenewNoSubnet) {
  110. NakedDhcpv6Srv srv(0);
  111. const IOAddress addr("2001:db8:1:1::cafe:babe");
  112. const uint32_t iaid = 234;
  113. // Generate client-id also duid_
  114. OptionPtr clientid = generateClientId();
  115. // Let's create a RENEW
  116. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
  117. req->setRemoteAddr(IOAddress("fe80::abcd"));
  118. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  119. OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  120. ia->addOption(renewed_addr_opt);
  121. req->addOption(ia);
  122. req->addOption(clientid);
  123. // Server-id is mandatory in RENEW
  124. req->addOption(srv.getServerID());
  125. // Pass it to the server and hope for a REPLY
  126. Pkt6Ptr reply = srv.processRenew(req);
  127. // check that we get the right NAK
  128. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding);
  129. }
  130. // This test verifies that incoming RELEASE can be handled properly, even when
  131. // no subnets are configured.
  132. //
  133. // This test sends a RELEASE and the expected response
  134. // is an REPLY with STATUS_NoBinding and no address provided in the
  135. // response
  136. TEST_F(NakedDhcpv6SrvTest, ReleaseNoSubnet) {
  137. NakedDhcpv6Srv srv(0);
  138. const IOAddress addr("2001:db8:1:1::cafe:babe");
  139. const uint32_t iaid = 234;
  140. // Generate client-id also duid_
  141. OptionPtr clientid = generateClientId();
  142. // Let's create a RELEASE
  143. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
  144. req->setRemoteAddr(IOAddress("fe80::abcd"));
  145. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
  146. OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
  147. ia->addOption(released_addr_opt);
  148. req->addOption(ia);
  149. req->addOption(clientid);
  150. // Server-id is mandatory in RELEASE
  151. req->addOption(srv.getServerID());
  152. // Pass it to the server and hope for a REPLY
  153. Pkt6Ptr reply = srv.processRelease(req);
  154. // check that we get the right NAK
  155. checkNakResponse (reply, DHCPV6_REPLY, 1234, STATUS_NoBinding);
  156. }
  157. // Test verifies that the Dhcpv6_srv class can be instantiated. It checks a mode
  158. // without open sockets and with sockets opened on a high port (to not require
  159. // root privileges).
  160. TEST_F(Dhcpv6SrvTest, basic) {
  161. // srv has stubbed interface detection. It will read
  162. // interfaces.txt instead. It will pretend to have detected
  163. // fe80::1234 link-local address on eth0 interface. Obviously
  164. // an attempt to bind this socket will fail.
  165. boost::scoped_ptr<Dhcpv6Srv> srv;
  166. ASSERT_NO_THROW( {
  167. // Skip opening any sockets
  168. srv.reset(new NakedDhcpv6Srv(0));
  169. });
  170. srv.reset();
  171. ASSERT_NO_THROW({
  172. // open an unpriviledged port
  173. srv.reset(new NakedDhcpv6Srv(DHCP6_SERVER_PORT + 10000));
  174. });
  175. }
  176. // Test checks that DUID is generated properly
  177. TEST_F(Dhcpv6SrvTest, DUID) {
  178. boost::scoped_ptr<Dhcpv6Srv> srv;
  179. ASSERT_NO_THROW( {
  180. srv.reset(new NakedDhcpv6Srv(0));
  181. });
  182. OptionPtr srvid = srv->getServerID();
  183. ASSERT_TRUE(srvid);
  184. EXPECT_EQ(D6O_SERVERID, srvid->getType());
  185. OutputBuffer buf(32);
  186. srvid->pack(buf);
  187. // length of the actual DUID
  188. size_t len = buf.getLength() - srvid->getHeaderLen();
  189. InputBuffer data(buf.getData(), buf.getLength());
  190. // ignore first four bytes (standard DHCPv6 header)
  191. data.readUint32();
  192. uint16_t duid_type = data.readUint16();
  193. cout << "Duid-type=" << duid_type << endl;
  194. switch(duid_type) {
  195. case DUID::DUID_LLT: {
  196. // DUID must contain at least 6 bytes long MAC
  197. // + 8 bytes of fixed header
  198. EXPECT_GE(14, len);
  199. uint16_t hw_type = data.readUint16();
  200. // there's no real way to find out "correct"
  201. // hardware type
  202. EXPECT_GT(hw_type, 0);
  203. // check that timer is counted since 1.1.2000,
  204. // not from 1.1.1970.
  205. uint32_t seconds = data.readUint32();
  206. EXPECT_LE(seconds, DUID_TIME_EPOCH);
  207. // this test will start failing after 2030.
  208. // Hopefully we will be at BIND12 by then.
  209. // MAC must not be zeros
  210. vector<uint8_t> mac(len-8);
  211. vector<uint8_t> zeros(len-8, 0);
  212. data.readVector(mac, len-8);
  213. EXPECT_TRUE(mac != zeros);
  214. break;
  215. }
  216. case DUID::DUID_EN: {
  217. // there's not much we can check. Just simple
  218. // check if it is not all zeros
  219. vector<uint8_t> content(len-2);
  220. data.readVector(content, len-2);
  221. EXPECT_FALSE(isRangeZero(content.begin(), content.end()));
  222. break;
  223. }
  224. case DUID::DUID_LL: {
  225. // not supported yet
  226. cout << "Test not implemented for DUID-LL." << endl;
  227. // No failure here. There's really no way for test LL DUID. It doesn't
  228. // even make sense to check if that Link Layer is actually present on
  229. // a physical interface. RFC3315 says a server should write its DUID
  230. // and keep it despite hardware changes.
  231. break;
  232. }
  233. case DUID::DUID_UUID: // not supported yet
  234. default:
  235. ADD_FAILURE() << "Not supported duid type=" << duid_type << endl;
  236. break;
  237. }
  238. }
  239. // This test checks if Option Request Option (ORO) is parsed correctly
  240. // and the requested options are actually assigned.
  241. TEST_F(Dhcpv6SrvTest, advertiseOptions) {
  242. IfaceMgrTestConfig test_config(true);
  243. ConstElementPtr x;
  244. string config = "{ \"interfaces\": [ \"*\" ],"
  245. "\"preferred-lifetime\": 3000,"
  246. "\"rebind-timer\": 2000, "
  247. "\"renew-timer\": 1000, "
  248. "\"subnet6\": [ { "
  249. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  250. " \"subnet\": \"2001:db8:1::/48\", "
  251. " \"interface\": \"eth0\", "
  252. " \"option-data\": [ {"
  253. " \"name\": \"dns-servers\","
  254. " \"space\": \"dhcp6\","
  255. " \"code\": 23,"
  256. " \"data\": \"2001:db8:1234:FFFF::1, 2001:db8:1234:FFFF::2\","
  257. " \"csv-format\": True"
  258. " },"
  259. " {"
  260. " \"name\": \"subscriber-id\","
  261. " \"space\": \"dhcp6\","
  262. " \"code\": 38,"
  263. " \"data\": \"1234\","
  264. " \"csv-format\": False"
  265. " } ]"
  266. " } ],"
  267. "\"valid-lifetime\": 4000 }";
  268. ASSERT_NO_THROW(configure(config));
  269. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  270. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  271. sol->setIface("eth0");
  272. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  273. OptionPtr clientid = generateClientId();
  274. sol->addOption(clientid);
  275. // Pass it to the server and get an advertise
  276. Pkt6Ptr adv = srv_.processSolicit(sol);
  277. // check if we get response at all
  278. ASSERT_TRUE(adv);
  279. // We have not requested any options so they should not
  280. // be included in the response.
  281. ASSERT_FALSE(adv->getOption(D6O_SUBSCRIBER_ID));
  282. ASSERT_FALSE(adv->getOption(D6O_NAME_SERVERS));
  283. // Let's now request some options. We expect that the server
  284. // will include them in its response.
  285. boost::shared_ptr<OptionIntArray<uint16_t> >
  286. option_oro(new OptionIntArray<uint16_t>(Option::V6, D6O_ORO));
  287. // Create vector with two option codes.
  288. std::vector<uint16_t> codes(2);
  289. codes[0] = D6O_SUBSCRIBER_ID;
  290. codes[1] = D6O_NAME_SERVERS;
  291. // Pass this code to option.
  292. option_oro->setValues(codes);
  293. // Append ORO to SOLICIT message.
  294. sol->addOption(option_oro);
  295. // Need to process SOLICIT again after requesting new option.
  296. adv = srv_.processSolicit(sol);
  297. ASSERT_TRUE(adv);
  298. OptionPtr tmp = adv->getOption(D6O_NAME_SERVERS);
  299. ASSERT_TRUE(tmp);
  300. boost::shared_ptr<Option6AddrLst> reply_nameservers =
  301. boost::dynamic_pointer_cast<Option6AddrLst>(tmp);
  302. ASSERT_TRUE(reply_nameservers);
  303. Option6AddrLst::AddressContainer addrs = reply_nameservers->getAddresses();
  304. ASSERT_EQ(2, addrs.size());
  305. EXPECT_TRUE(addrs[0] == IOAddress("2001:db8:1234:FFFF::1"));
  306. EXPECT_TRUE(addrs[1] == IOAddress("2001:db8:1234:FFFF::2"));
  307. // There is a dummy option with code 1000 we requested from a server.
  308. // Expect that this option is in server's response.
  309. tmp = adv->getOption(D6O_SUBSCRIBER_ID);
  310. ASSERT_TRUE(tmp);
  311. // Check that the option contains valid data (from configuration).
  312. std::vector<uint8_t> data = tmp->getData();
  313. ASSERT_EQ(2, data.size());
  314. const uint8_t foo_expected[] = {
  315. 0x12, 0x34
  316. };
  317. EXPECT_EQ(0, memcmp(&data[0], foo_expected, 2));
  318. // more checks to be implemented
  319. }
  320. // There are no dedicated tests for Dhcpv6Srv::handleIA_NA and Dhcpv6Srv::assignLeases
  321. // as they are indirectly tested in Solicit and Request tests.
  322. // This test verifies that incoming SOLICIT can be handled properly, that an
  323. // ADVERTISE is generated, that the response has an address and that address
  324. // really belongs to the configured pool.
  325. //
  326. // This test sends a SOLICIT without any hint in IA_NA.
  327. //
  328. // constructed very simple SOLICIT message with:
  329. // - client-id option (mandatory)
  330. // - IA option (a request for address, without any addresses)
  331. //
  332. // expected returned ADVERTISE message:
  333. // - copy of client-id
  334. // - server-id
  335. // - IA that includes IAADDR
  336. TEST_F(Dhcpv6SrvTest, SolicitBasic) {
  337. NakedDhcpv6Srv srv(0);
  338. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  339. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  340. sol->setIface("eth0");
  341. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  342. OptionPtr clientid = generateClientId();
  343. sol->addOption(clientid);
  344. // Pass it to the server and get an advertise
  345. Pkt6Ptr reply = srv.processSolicit(sol);
  346. // check if we get response at all
  347. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  348. // check that IA_NA was returned and that there's an address included
  349. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  350. subnet_->getT2());
  351. ASSERT_TRUE(addr);
  352. // Check that the assigned address is indeed from the configured pool
  353. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA);
  354. // check DUIDs
  355. checkServerId(reply, srv.getServerID());
  356. checkClientId(reply, clientid);
  357. }
  358. // This test verifies that incoming SOLICIT can be handled properly, that an
  359. // ADVERTISE is generated, that the response has a prefix and that prefix
  360. // really belongs to the configured pool.
  361. //
  362. // This test sends a SOLICIT without any hint in IA_PD.
  363. //
  364. // constructed very simple SOLICIT message with:
  365. // - client-id option (mandatory)
  366. // - IA option (a request for address, without any addresses)
  367. //
  368. // expected returned ADVERTISE message:
  369. // - copy of client-id
  370. // - server-id
  371. // - IA that includes IAPREFIX
  372. TEST_F(Dhcpv6SrvTest, pdSolicitBasic) {
  373. NakedDhcpv6Srv srv(0);
  374. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  375. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  376. sol->setIface("eth0");
  377. sol->addOption(generateIA(D6O_IA_PD, 234, 1500, 3000));
  378. OptionPtr clientid = generateClientId();
  379. sol->addOption(clientid);
  380. // Pass it to the server and get an advertise
  381. Pkt6Ptr reply = srv.processSolicit(sol);
  382. // check if we get response at all
  383. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  384. // check that IA_NA was returned and that there's an address included
  385. boost::shared_ptr<Option6IAPrefix> prefix = checkIA_PD(reply, 234, subnet_->getT1(),
  386. subnet_->getT2());
  387. ASSERT_TRUE(prefix);
  388. // Check that the assigned prefix is indeed from the configured pool
  389. checkIAAddr(prefix, prefix->getAddress(), Lease::TYPE_PD);
  390. EXPECT_EQ(pd_pool_->getLength(), prefix->getLength());
  391. // check DUIDs
  392. checkServerId(reply, srv.getServerID());
  393. checkClientId(reply, clientid);
  394. }
  395. // This test verifies that incoming SOLICIT can be handled properly, that an
  396. // ADVERTISE is generated, that the response has an address and that address
  397. // really belongs to the configured pool.
  398. //
  399. // This test sends a SOLICIT with IA_NA that contains a valid hint.
  400. //
  401. // constructed very simple SOLICIT message with:
  402. // - client-id option (mandatory)
  403. // - IA option (a request for address, with an address that belongs to the
  404. // configured pool, i.e. is valid as hint)
  405. //
  406. // expected returned ADVERTISE message:
  407. // - copy of client-id
  408. // - server-id
  409. // - IA that includes IAADDR
  410. TEST_F(Dhcpv6SrvTest, SolicitHint) {
  411. NakedDhcpv6Srv srv(0);
  412. // Let's create a SOLICIT
  413. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  414. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  415. sol->setIface("eth0");
  416. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  417. // with a valid hint
  418. IOAddress hint("2001:db8:1:1::dead:beef");
  419. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  420. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  421. ia->addOption(hint_opt);
  422. sol->addOption(ia);
  423. OptionPtr clientid = generateClientId();
  424. sol->addOption(clientid);
  425. // Pass it to the server and get an advertise
  426. Pkt6Ptr reply = srv.processSolicit(sol);
  427. // check if we get response at all
  428. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  429. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  430. ASSERT_TRUE(tmp);
  431. // check that IA_NA was returned and that there's an address included
  432. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  433. subnet_->getT2());
  434. ASSERT_TRUE(addr);
  435. // check that we've got the address we requested
  436. checkIAAddr(addr, hint, Lease::TYPE_NA);
  437. // check DUIDs
  438. checkServerId(reply, srv.getServerID());
  439. checkClientId(reply, clientid);
  440. }
  441. // This test verifies that incoming SOLICIT can be handled properly, that an
  442. // ADVERTISE is generated, that the response has an address and that address
  443. // really belongs to the configured pool.
  444. //
  445. // This test sends a SOLICIT with IA_NA that contains an invalid hint.
  446. //
  447. // constructed very simple SOLICIT message with:
  448. // - client-id option (mandatory)
  449. // - IA option (a request for address, with an address that does not
  450. // belong to the configured pool, i.e. is valid as hint)
  451. //
  452. // expected returned ADVERTISE message:
  453. // - copy of client-id
  454. // - server-id
  455. // - IA that includes IAADDR
  456. TEST_F(Dhcpv6SrvTest, SolicitInvalidHint) {
  457. NakedDhcpv6Srv srv(0);
  458. // Let's create a SOLICIT
  459. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  460. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  461. sol->setIface("eth0");
  462. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  463. IOAddress hint("2001:db8:1::cafe:babe");
  464. ASSERT_FALSE(subnet_->inPool(Lease::TYPE_NA, hint));
  465. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  466. ia->addOption(hint_opt);
  467. sol->addOption(ia);
  468. OptionPtr clientid = generateClientId();
  469. sol->addOption(clientid);
  470. // Pass it to the server and get an advertise
  471. Pkt6Ptr reply = srv.processSolicit(sol);
  472. // check if we get response at all
  473. checkResponse(reply, DHCPV6_ADVERTISE, 1234);
  474. // check that IA_NA was returned and that there's an address included
  475. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234, subnet_->getT1(),
  476. subnet_->getT2());
  477. ASSERT_TRUE(addr);
  478. // Check that the assigned address is indeed from the configured pool
  479. checkIAAddr(addr, addr->getAddress(), Lease::TYPE_NA);
  480. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr->getAddress()));
  481. // check DUIDs
  482. checkServerId(reply, srv.getServerID());
  483. checkClientId(reply, clientid);
  484. }
  485. /// @todo: Add a test that client sends hint that is in pool, but currently
  486. /// being used by a different client.
  487. // This test checks that the server is offering different addresses to different
  488. // clients in ADVERTISEs. Please note that ADVERTISE is not a guarantee that such
  489. // an address will be assigned. Had the pool was very small and contained only
  490. // 2 addresses, the third client would get the same advertise as the first one
  491. // and this is a correct behavior. It is REQUEST that will fail for the third
  492. // client. ADVERTISE is basically saying "if you send me a request, you will
  493. // probably get an address like this" (there are no guarantees).
  494. TEST_F(Dhcpv6SrvTest, ManySolicits) {
  495. NakedDhcpv6Srv srv(0);
  496. Pkt6Ptr sol1 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  497. Pkt6Ptr sol2 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 2345));
  498. Pkt6Ptr sol3 = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 3456));
  499. sol1->setRemoteAddr(IOAddress("fe80::abcd"));
  500. sol2->setRemoteAddr(IOAddress("fe80::1223"));
  501. sol3->setRemoteAddr(IOAddress("fe80::3467"));
  502. sol1->setIface("eth0");
  503. sol2->setIface("eth0");
  504. sol3->setIface("eth0");
  505. sol1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  506. sol2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  507. sol3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  508. // different client-id sizes
  509. OptionPtr clientid1 = generateClientId(12);
  510. OptionPtr clientid2 = generateClientId(14);
  511. OptionPtr clientid3 = generateClientId(16);
  512. sol1->addOption(clientid1);
  513. sol2->addOption(clientid2);
  514. sol3->addOption(clientid3);
  515. // Pass it to the server and get an advertise
  516. Pkt6Ptr reply1 = srv.processSolicit(sol1);
  517. Pkt6Ptr reply2 = srv.processSolicit(sol2);
  518. Pkt6Ptr reply3 = srv.processSolicit(sol3);
  519. // check if we get response at all
  520. checkResponse(reply1, DHCPV6_ADVERTISE, 1234);
  521. checkResponse(reply2, DHCPV6_ADVERTISE, 2345);
  522. checkResponse(reply3, DHCPV6_ADVERTISE, 3456);
  523. // check that IA_NA was returned and that there's an address included
  524. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  525. subnet_->getT2());
  526. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  527. subnet_->getT2());
  528. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  529. subnet_->getT2());
  530. ASSERT_TRUE(addr1);
  531. ASSERT_TRUE(addr2);
  532. ASSERT_TRUE(addr3);
  533. // Check that the assigned address is indeed from the configured pool
  534. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA);
  535. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA);
  536. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA);
  537. // check DUIDs
  538. checkServerId(reply1, srv.getServerID());
  539. checkServerId(reply2, srv.getServerID());
  540. checkServerId(reply3, srv.getServerID());
  541. checkClientId(reply1, clientid1);
  542. checkClientId(reply2, clientid2);
  543. checkClientId(reply3, clientid3);
  544. // Finally check that the addresses offered are different
  545. EXPECT_NE(addr1->getAddress(), addr2->getAddress());
  546. EXPECT_NE(addr2->getAddress(), addr3->getAddress());
  547. EXPECT_NE(addr3->getAddress(), addr1->getAddress());
  548. cout << "Offered address to client1=" << addr1->getAddress() << endl;
  549. cout << "Offered address to client2=" << addr2->getAddress() << endl;
  550. cout << "Offered address to client3=" << addr3->getAddress() << endl;
  551. }
  552. // This test verifies that incoming REQUEST can be handled properly, that a
  553. // REPLY is generated, that the response has an address and that address
  554. // really belongs to the configured pool.
  555. //
  556. // This test sends a REQUEST with IA_NA that contains a valid hint.
  557. //
  558. // constructed very simple REQUEST message with:
  559. // - client-id option (mandatory)
  560. // - IA option (a request for address, with an address that belongs to the
  561. // configured pool, i.e. is valid as hint)
  562. //
  563. // expected returned REPLY message:
  564. // - copy of client-id
  565. // - server-id
  566. // - IA that includes IAADDR
  567. TEST_F(Dhcpv6SrvTest, RequestBasic) {
  568. NakedDhcpv6Srv srv(0);
  569. // Let's create a REQUEST
  570. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  571. req->setRemoteAddr(IOAddress("fe80::abcd"));
  572. req->setIface("eth0");
  573. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
  574. // with a valid hint
  575. IOAddress hint("2001:db8:1:1::dead:beef");
  576. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, hint));
  577. OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
  578. ia->addOption(hint_opt);
  579. req->addOption(ia);
  580. OptionPtr clientid = generateClientId();
  581. req->addOption(clientid);
  582. // server-id is mandatory in REQUEST
  583. req->addOption(srv.getServerID());
  584. // Pass it to the server and hope for a REPLY
  585. Pkt6Ptr reply = srv.processRequest(req);
  586. // check if we get response at all
  587. checkResponse(reply, DHCPV6_REPLY, 1234);
  588. OptionPtr tmp = reply->getOption(D6O_IA_NA);
  589. ASSERT_TRUE(tmp);
  590. // check that IA_NA was returned and that there's an address included
  591. boost::shared_ptr<Option6IAAddr> addr = checkIA_NA(reply, 234,
  592. subnet_->getT1(),
  593. subnet_->getT2());
  594. ASSERT_TRUE(addr);
  595. // check that we've got the address we requested
  596. checkIAAddr(addr, hint, Lease::TYPE_NA);
  597. // check DUIDs
  598. checkServerId(reply, srv.getServerID());
  599. checkClientId(reply, clientid);
  600. // check that the lease is really in the database
  601. Lease6Ptr l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
  602. EXPECT_TRUE(l);
  603. LeaseMgrFactory::instance().deleteLease(addr->getAddress());
  604. }
  605. // This test verifies that incoming REQUEST can be handled properly, that a
  606. // REPLY is generated, that the response has a prefix and that prefix
  607. // really belongs to the configured pool.
  608. //
  609. // This test sends a REQUEST with IA_PD that contains a valid hint.
  610. //
  611. // constructed very simple REQUEST message with:
  612. // - client-id option (mandatory)
  613. // - IA option (a request for address, with an address that belongs to the
  614. // configured pool, i.e. is valid as hint)
  615. //
  616. // expected returned REPLY message:
  617. // - copy of client-id
  618. // - server-id
  619. // - IA that includes IAPREFIX
  620. TEST_F(Dhcpv6SrvTest, pdRequestBasic) {
  621. NakedDhcpv6Srv srv(0);
  622. // Let's create a REQUEST
  623. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  624. req->setRemoteAddr(IOAddress("fe80::abcd"));
  625. req->setIface("eth0");
  626. boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_PD, 234, 1500, 3000);
  627. // with a valid hint
  628. IOAddress hint("2001:db8:1:2:f::");
  629. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_PD, hint));
  630. OptionPtr hint_opt(new Option6IAPrefix(D6O_IAPREFIX, hint, 64, 300, 500));
  631. ia->addOption(hint_opt);
  632. req->addOption(ia);
  633. OptionPtr clientid = generateClientId();
  634. req->addOption(clientid);
  635. // server-id is mandatory in REQUEST
  636. req->addOption(srv.getServerID());
  637. // Pass it to the server and hope for a REPLY
  638. Pkt6Ptr reply = srv.processRequest(req);
  639. // check if we get response at all
  640. checkResponse(reply, DHCPV6_REPLY, 1234);
  641. OptionPtr tmp = reply->getOption(D6O_IA_PD);
  642. ASSERT_TRUE(tmp);
  643. // check that IA_NA was returned and that there's an address included
  644. boost::shared_ptr<Option6IAPrefix> prf = checkIA_PD(reply, 234,
  645. subnet_->getT1(),
  646. subnet_->getT2());
  647. ASSERT_TRUE(prf);
  648. // check that we've got the address we requested
  649. checkIAAddr(prf, hint, Lease::TYPE_PD);
  650. EXPECT_EQ(pd_pool_->getLength(), prf->getLength());
  651. // check DUIDs
  652. checkServerId(reply, srv.getServerID());
  653. checkClientId(reply, clientid);
  654. // check that the lease is really in the database
  655. Lease6Ptr l = checkPdLease(duid_, reply->getOption(D6O_IA_PD), prf);
  656. EXPECT_TRUE(l);
  657. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(prf->getAddress()));
  658. }
  659. // This test checks that the server is offering different addresses to different
  660. // clients in REQUEST. Please note that ADVERTISE is not a guarantee that such
  661. // and address will be assigned. Had the pool was very small and contained only
  662. // 2 addresses, the third client would get the same advertise as the first one
  663. // and this is a correct behavior. It is REQUEST that will fail for the third
  664. // client. ADVERTISE is basically saying "if you send me a request, you will
  665. // probably get an address like this" (there are no guarantees).
  666. TEST_F(Dhcpv6SrvTest, ManyRequests) {
  667. NakedDhcpv6Srv srv(0);
  668. ASSERT_TRUE(subnet_);
  669. Pkt6Ptr req1 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  670. Pkt6Ptr req2 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 2345));
  671. Pkt6Ptr req3 = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 3456));
  672. req1->setRemoteAddr(IOAddress("fe80::abcd"));
  673. req2->setRemoteAddr(IOAddress("fe80::1223"));
  674. req3->setRemoteAddr(IOAddress("fe80::3467"));
  675. req1->setIface("eth0");
  676. req2->setIface("eth0");
  677. req3->setIface("eth0");
  678. req1->addOption(generateIA(D6O_IA_NA, 1, 1500, 3000));
  679. req2->addOption(generateIA(D6O_IA_NA, 2, 1500, 3000));
  680. req3->addOption(generateIA(D6O_IA_NA, 3, 1500, 3000));
  681. // different client-id sizes
  682. OptionPtr clientid1 = generateClientId(12);
  683. OptionPtr clientid2 = generateClientId(14);
  684. OptionPtr clientid3 = generateClientId(16);
  685. req1->addOption(clientid1);
  686. req2->addOption(clientid2);
  687. req3->addOption(clientid3);
  688. // server-id is mandatory in REQUEST
  689. req1->addOption(srv.getServerID());
  690. req2->addOption(srv.getServerID());
  691. req3->addOption(srv.getServerID());
  692. // Pass it to the server and get an advertise
  693. Pkt6Ptr reply1 = srv.processRequest(req1);
  694. Pkt6Ptr reply2 = srv.processRequest(req2);
  695. Pkt6Ptr reply3 = srv.processRequest(req3);
  696. // check if we get response at all
  697. checkResponse(reply1, DHCPV6_REPLY, 1234);
  698. checkResponse(reply2, DHCPV6_REPLY, 2345);
  699. checkResponse(reply3, DHCPV6_REPLY, 3456);
  700. // check that IA_NA was returned and that there's an address included
  701. boost::shared_ptr<Option6IAAddr> addr1 = checkIA_NA(reply1, 1, subnet_->getT1(),
  702. subnet_->getT2());
  703. boost::shared_ptr<Option6IAAddr> addr2 = checkIA_NA(reply2, 2, subnet_->getT1(),
  704. subnet_->getT2());
  705. boost::shared_ptr<Option6IAAddr> addr3 = checkIA_NA(reply3, 3, subnet_->getT1(),
  706. subnet_->getT2());
  707. ASSERT_TRUE(addr1);
  708. ASSERT_TRUE(addr2);
  709. ASSERT_TRUE(addr3);
  710. // Check that the assigned address is indeed from the configured pool
  711. checkIAAddr(addr1, addr1->getAddress(), Lease::TYPE_NA);
  712. checkIAAddr(addr2, addr2->getAddress(), Lease::TYPE_NA);
  713. checkIAAddr(addr3, addr3->getAddress(), Lease::TYPE_NA);
  714. // check DUIDs
  715. checkServerId(reply1, srv.getServerID());
  716. checkServerId(reply2, srv.getServerID());
  717. checkServerId(reply3, srv.getServerID());
  718. checkClientId(reply1, clientid1);
  719. checkClientId(reply2, clientid2);
  720. checkClientId(reply3, clientid3);
  721. // Finally check that the addresses offered are different
  722. EXPECT_NE(addr1->getAddress(), addr2->getAddress());
  723. EXPECT_NE(addr2->getAddress(), addr3->getAddress());
  724. EXPECT_NE(addr3->getAddress(), addr1->getAddress());
  725. cout << "Assigned address to client1=" << addr1->getAddress() << endl;
  726. cout << "Assigned address to client2=" << addr2->getAddress() << endl;
  727. cout << "Assigned address to client3=" << addr3->getAddress() << endl;
  728. }
  729. // This test verifies that incoming (positive) RENEW can be handled properly, that a
  730. // REPLY is generated, that the response has an address and that address
  731. // really belongs to the configured pool and that lease is actually renewed.
  732. //
  733. // expected:
  734. // - returned REPLY message has copy of client-id
  735. // - returned REPLY message has server-id
  736. // - returned REPLY message has IA_NA that includes IAADDR
  737. // - lease is actually renewed in LeaseMgr
  738. TEST_F(Dhcpv6SrvTest, renewBasic) {
  739. testRenewBasic(Lease::TYPE_NA, "2001:db8:1:1::cafe:babe",
  740. "2001:db8:1:1::cafe:babe", 128);
  741. }
  742. // This test verifies that incoming (positive) PD RENEW can be handled properly,
  743. // that a REPLY is generated, that the response has a prefix and that prefix
  744. // really belongs to the configured pool and that lease is actually renewed.
  745. //
  746. // expected:
  747. // - returned REPLY message has copy of client-id
  748. // - returned REPLY message has server-id
  749. // - returned REPLY message has IA_PD that includes IAPREFIX
  750. // - lease is actually renewed in LeaseMgr
  751. TEST_F(Dhcpv6SrvTest, pdRenewBasic) {
  752. testRenewBasic(Lease::TYPE_PD, "2001:db8:1:2::",
  753. "2001:db8:1:2::", pd_pool_->getLength());
  754. }
  755. // This test verifies that incoming (invalid) RENEW with an address
  756. // can be handled properly.
  757. //
  758. // This test checks 3 scenarios:
  759. // 1. there is no such lease at all
  760. // 2. there is such a lease, but it is assigned to a different IAID
  761. // 3. there is such a lease, but it belongs to a different client
  762. //
  763. // expected:
  764. // - returned REPLY message has copy of client-id
  765. // - returned REPLY message has server-id
  766. // - returned REPLY message has IA_NA that includes STATUS-CODE
  767. // - No lease in LeaseMgr
  768. TEST_F(Dhcpv6SrvTest, RenewReject) {
  769. testRenewReject(Lease::TYPE_NA, IOAddress("2001:db8:1:1::dead"));
  770. }
  771. // This test verifies that incoming (invalid) RENEW with a prefix
  772. // can be handled properly.
  773. //
  774. // This test checks 3 scenarios:
  775. // 1. there is no such lease at all
  776. // 2. there is such a lease, but it is assigned to a different IAID
  777. // 3. there is such a lease, but it belongs to a different client
  778. //
  779. // expected:
  780. // - returned REPLY message has copy of client-id
  781. // - returned REPLY message has server-id
  782. // - returned REPLY message has IA_PD that includes STATUS-CODE
  783. // - No lease in LeaseMgr
  784. TEST_F(Dhcpv6SrvTest, pdRenewReject) {
  785. testRenewReject(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"));
  786. }
  787. // This test verifies that incoming (positive) RELEASE with address can be
  788. // handled properly, that a REPLY is generated, that the response has status
  789. // code and that the lease is indeed removed from the database.
  790. //
  791. // expected:
  792. // - returned REPLY message has copy of client-id
  793. // - returned REPLY message has server-id
  794. // - returned REPLY message has IA_NA that does not include an IAADDR
  795. // - lease is actually removed from LeaseMgr
  796. TEST_F(Dhcpv6SrvTest, ReleaseBasic) {
  797. testReleaseBasic(Lease::TYPE_NA, IOAddress("2001:db8:1:1::cafe:babe"),
  798. IOAddress("2001:db8:1:1::cafe:babe"));
  799. }
  800. // This test verifies that incoming (positive) RELEASE with prefix can be
  801. // handled properly, that a REPLY is generated, that the response has
  802. // status code and that the lease is indeed removed from the database.
  803. //
  804. // expected:
  805. // - returned REPLY message has copy of client-id
  806. // - returned REPLY message has server-id
  807. // - returned REPLY message has IA_PD that does not include an IAPREFIX
  808. // - lease is actually removed from LeaseMgr
  809. TEST_F(Dhcpv6SrvTest, pdReleaseBasic) {
  810. testReleaseBasic(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"),
  811. IOAddress("2001:db8:1:2::"));
  812. }
  813. // This test verifies that incoming (invalid) RELEASE with an address
  814. // can be handled properly.
  815. //
  816. // This test checks 3 scenarios:
  817. // 1. there is no such lease at all
  818. // 2. there is such a lease, but it is assigned to a different IAID
  819. // 3. there is such a lease, but it belongs to a different client
  820. //
  821. // expected:
  822. // - returned REPLY message has copy of client-id
  823. // - returned REPLY message has server-id
  824. // - returned REPLY message has IA_NA that includes STATUS-CODE
  825. // - No lease in LeaseMgr
  826. TEST_F(Dhcpv6SrvTest, ReleaseReject) {
  827. testReleaseReject(Lease::TYPE_NA, IOAddress("2001:db8:1:1::dead"));
  828. }
  829. // This test verifies that incoming (invalid) RELEASE with a prefix
  830. // can be handled properly.
  831. //
  832. // This test checks 3 scenarios:
  833. // 1. there is no such lease at all
  834. // 2. there is such a lease, but it is assigned to a different IAID
  835. // 3. there is such a lease, but it belongs to a different client
  836. //
  837. // expected:
  838. // - returned REPLY message has copy of client-id
  839. // - returned REPLY message has server-id
  840. // - returned REPLY message has IA_PD that includes STATUS-CODE
  841. // - No lease in LeaseMgr
  842. TEST_F(Dhcpv6SrvTest, pdReleaseReject) {
  843. testReleaseReject(Lease::TYPE_PD, IOAddress("2001:db8:1:2::"));
  844. }
  845. // This test verifies if the status code option is generated properly.
  846. TEST_F(Dhcpv6SrvTest, StatusCode) {
  847. NakedDhcpv6Srv srv(0);
  848. // a dummy content for client-id
  849. uint8_t expected[] = {
  850. 0x0, 0xD, // option code = 13
  851. 0x0, 0x7, // option length = 7
  852. 0x0, 0x3, // status code = 3
  853. 0x41, 0x42, 0x43, 0x44, 0x45 // string value ABCDE
  854. };
  855. // Create the option.
  856. OptionPtr status = srv.createStatusCode(3, "ABCDE");
  857. // Allocate an output buffer. We will store the option
  858. // in wire format here.
  859. OutputBuffer buf(sizeof(expected));
  860. // Prepare the wire format.
  861. ASSERT_NO_THROW(status->pack(buf));
  862. // Check that the option buffer has valid length (option header + data).
  863. ASSERT_EQ(sizeof(expected), buf.getLength());
  864. // Verify the contents of the option.
  865. EXPECT_EQ(0, memcmp(expected, buf.getData(), sizeof(expected)));
  866. }
  867. // This test verifies if the sanityCheck() really checks options presence.
  868. TEST_F(Dhcpv6SrvTest, sanityCheck) {
  869. NakedDhcpv6Srv srv(0);
  870. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  871. // Set link-local sender address, so appropriate subnet can be
  872. // selected for this packet.
  873. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  874. // client-id is optional for information-request, so
  875. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL));
  876. // empty packet, no client-id, no server-id
  877. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN),
  878. RFCViolation);
  879. // This doesn't make much sense, but let's check it for completeness
  880. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::FORBIDDEN, Dhcpv6Srv::FORBIDDEN));
  881. OptionPtr clientid = generateClientId();
  882. pkt->addOption(clientid);
  883. // client-id is mandatory, server-id is forbidden (as in SOLICIT or REBIND)
  884. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::FORBIDDEN));
  885. pkt->addOption(srv.getServerID());
  886. // both client-id and server-id are mandatory (as in REQUEST, RENEW, RELEASE, DECLINE)
  887. EXPECT_NO_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY));
  888. // sane section ends here, let's do some negative tests as well
  889. pkt->addOption(clientid);
  890. pkt->addOption(clientid);
  891. // with more than one client-id it should throw, no matter what
  892. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  893. RFCViolation);
  894. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  895. RFCViolation);
  896. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  897. RFCViolation);
  898. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  899. RFCViolation);
  900. pkt->delOption(D6O_CLIENTID);
  901. pkt->delOption(D6O_CLIENTID);
  902. // again we have only one client-id
  903. // let's try different type of insanity - several server-ids
  904. pkt->addOption(srv.getServerID());
  905. pkt->addOption(srv.getServerID());
  906. // with more than one server-id it should throw, no matter what
  907. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::OPTIONAL),
  908. RFCViolation);
  909. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::OPTIONAL),
  910. RFCViolation);
  911. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::OPTIONAL, Dhcpv6Srv::MANDATORY),
  912. RFCViolation);
  913. EXPECT_THROW(srv.sanityCheck(pkt, Dhcpv6Srv::MANDATORY, Dhcpv6Srv::MANDATORY),
  914. RFCViolation);
  915. }
  916. // Check that the server is testing if server identifier received in the
  917. // query, matches server identifier used by the server.
  918. TEST_F(Dhcpv6SrvTest, testServerID) {
  919. NakedDhcpv6Srv srv(0);
  920. Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_REQUEST, 1234));
  921. std::vector<uint8_t> bin;
  922. // diud_llt constructed with: time = 0, macaddress = 00:00:00:00:00:00
  923. // it's necessary to generate server identifier option
  924. isc::util::encode::decodeHex("0001000100000000000000000000", bin);
  925. // Now create server identifier option
  926. OptionPtr serverid = OptionPtr(new Option(Option::V6, D6O_SERVERID, bin));
  927. // Server identifier option is MANDATORY in Request message.
  928. // Add server identifier option with different value from one that
  929. // server is using.
  930. req->addOption(serverid);
  931. // Message shoud be dropped
  932. EXPECT_FALSE(srv.testServerID(req));
  933. // Delete server identifier option and add new one, with same value as
  934. // server's server identifier.
  935. req->delOption(D6O_SERVERID);
  936. req->addOption(srv.getServerID());
  937. // With proper server identifier we expect true
  938. EXPECT_TRUE(srv.testServerID(req));
  939. // server-id MUST NOT appear in Solicit, so check if server is
  940. // not dropping a message without server id.
  941. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  942. EXPECT_TRUE(srv.testServerID(req));
  943. }
  944. // Test that some messages are discarded by the server if they are sent to
  945. // unicast address.
  946. TEST_F(Dhcpv6SrvTest, testUnicast) {
  947. NakedDhcpv6Srv srv(0);
  948. // Explicitly list client's message types which must be discarded if
  949. // sent to unicast address.
  950. const uint8_t not_allowed_unicast[] = {
  951. DHCPV6_SOLICIT,
  952. DHCPV6_CONFIRM,
  953. DHCPV6_REBIND,
  954. DHCPV6_INFORMATION_REQUEST
  955. };
  956. // Iterate over these messages and make sure they are discarded.
  957. for (int i = 0; i < sizeof(not_allowed_unicast); ++i) {
  958. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(not_allowed_unicast[i], 1234));
  959. msg->setLocalAddr(IOAddress("2001:db8:1::1"));
  960. EXPECT_FALSE(srv.testUnicast(msg))
  961. << "server accepts message type "
  962. << static_cast<int>(not_allowed_unicast[i])
  963. << "being sent to unicast address; this message should"
  964. " be discarded according to section 15 of RFC3315";
  965. }
  966. // Explicitly list client/relay message types which are allowed to
  967. // be sent to unicast.
  968. const uint8_t allowed_unicast[] = {
  969. DHCPV6_REQUEST,
  970. DHCPV6_RENEW,
  971. DHCPV6_RELEASE,
  972. DHCPV6_DECLINE,
  973. DHCPV6_RELAY_FORW
  974. };
  975. // Iterate over these messages and check that they are accepted being
  976. // sent to unicast.
  977. for (int i = 0; i < sizeof(allowed_unicast); ++i) {
  978. Pkt6Ptr msg = Pkt6Ptr(new Pkt6(allowed_unicast[i], 1234));
  979. msg->setLocalAddr(IOAddress("2001:db8:1::1"));
  980. msg->addOption(srv.getServerID());
  981. EXPECT_TRUE(srv.testUnicast(msg))
  982. << "server doesn't accept message type "
  983. << static_cast<int>(allowed_unicast[i])
  984. << "being sent to unicast address";
  985. }
  986. }
  987. // This test verifies if selectSubnet() selects proper subnet for a given
  988. // source address.
  989. TEST_F(Dhcpv6SrvTest, selectSubnetAddr) {
  990. NakedDhcpv6Srv srv(0);
  991. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  992. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  993. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  994. // CASE 1: We have only one subnet defined and we received local traffic.
  995. // The only available subnet used to be picked, but not anymore
  996. CfgMgr::instance().clear();
  997. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  998. CfgMgr::instance().commit();
  999. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1000. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1001. // The clause for assuming local subnet if there is only one subnet is was
  1002. // removed.
  1003. EXPECT_FALSE(srv.selectSubnet(pkt));
  1004. // CASE 2: We have only one subnet defined and we received relayed traffic.
  1005. // We should NOT select it.
  1006. // Identical steps as in case 1, but repeated for clarity
  1007. CfgMgr::instance().clear();
  1008. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1009. CfgMgr::instance().commit();
  1010. pkt->setRemoteAddr(IOAddress("2001:db8:abcd::2345"));
  1011. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1012. EXPECT_FALSE(selected);
  1013. // CASE 3: We have three subnets defined and we received local traffic.
  1014. // Nothing should be selected.
  1015. CfgMgr::instance().clear();
  1016. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1017. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1018. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1019. CfgMgr::instance().commit();
  1020. pkt->setRemoteAddr(IOAddress("fe80::abcd"));
  1021. selected = srv.selectSubnet(pkt);
  1022. EXPECT_FALSE(selected);
  1023. // CASE 4: We have three subnets defined and we received relayed traffic
  1024. // that came out of subnet 2. We should select subnet2 then
  1025. CfgMgr::instance().clear();
  1026. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1027. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1028. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1029. CfgMgr::instance().commit();
  1030. pkt->setRemoteAddr(IOAddress("2001:db8:2::baca"));
  1031. selected = srv.selectSubnet(pkt);
  1032. EXPECT_EQ(selected, subnet2);
  1033. // CASE 5: We have three subnets defined and we received relayed traffic
  1034. // that came out of undefined subnet. We should select nothing
  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("2001:db8:4::baca"));
  1041. EXPECT_FALSE(srv.selectSubnet(pkt));
  1042. }
  1043. // This test verifies if selectSubnet() selects proper subnet for a given
  1044. // network interface name.
  1045. TEST_F(Dhcpv6SrvTest, selectSubnetIface) {
  1046. NakedDhcpv6Srv srv(0);
  1047. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1048. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1049. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1050. subnet1->setIface("eth0");
  1051. subnet3->setIface("wifi1");
  1052. // CASE 1: We have only one subnet defined and it is available via eth0.
  1053. // Packet came from eth0. The only available subnet should be selected
  1054. CfgMgr::instance().clear();
  1055. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1056. CfgMgr::instance().commit();
  1057. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1058. pkt->setIface("eth0");
  1059. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1060. EXPECT_EQ(selected, subnet1);
  1061. // CASE 2: We have only one subnet defined and it is available via eth0.
  1062. // Packet came from eth1. We should not select it
  1063. CfgMgr::instance().clear();
  1064. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1065. CfgMgr::instance().commit();
  1066. pkt->setIface("eth1");
  1067. selected = srv.selectSubnet(pkt);
  1068. EXPECT_FALSE(selected);
  1069. // CASE 3: We have only 3 subnets defined, one over eth0, one remote and
  1070. // one over wifi1.
  1071. // Packet came from eth1. We should not select it
  1072. CfgMgr::instance().clear();
  1073. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1074. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1075. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1076. CfgMgr::instance().commit();
  1077. pkt->setIface("eth0");
  1078. EXPECT_EQ(subnet1, srv.selectSubnet(pkt));
  1079. pkt->setIface("eth3"); // no such interface
  1080. EXPECT_EQ(Subnet6Ptr(), srv.selectSubnet(pkt)); // nothing selected
  1081. pkt->setIface("wifi1");
  1082. EXPECT_EQ(subnet3, srv.selectSubnet(pkt));
  1083. }
  1084. // This test verifies if selectSubnet() selects proper subnet for a given
  1085. // linkaddr in RELAY-FORW message
  1086. TEST_F(Dhcpv6SrvTest, selectSubnetRelayLinkaddr) {
  1087. NakedDhcpv6Srv srv(0);
  1088. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1089. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1090. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1091. Pkt6::RelayInfo relay;
  1092. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1093. relay.peeraddr_ = IOAddress("fe80::1");
  1094. // CASE 1: We have only one subnet defined and we received relayed traffic.
  1095. // The only available subnet should NOT be selected.
  1096. CfgMgr::instance().clear();
  1097. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1098. CfgMgr::instance().commit();
  1099. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1100. pkt->relay_info_.push_back(relay);
  1101. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1102. EXPECT_FALSE(selected);
  1103. // CASE 2: We have three subnets defined and we received relayed traffic.
  1104. // Nothing should be selected.
  1105. CfgMgr::instance().clear();
  1106. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1107. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1108. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1109. CfgMgr::instance().commit();
  1110. selected = srv.selectSubnet(pkt);
  1111. EXPECT_EQ(selected, subnet2);
  1112. // CASE 3: We have three subnets defined and we received relayed traffic
  1113. // that came out of subnet 2. We should select subnet2 then
  1114. CfgMgr::instance().clear();
  1115. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1116. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1117. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1118. CfgMgr::instance().commit();
  1119. // Source of the packet should have no meaning. Selection is based
  1120. // on linkaddr field in the relay
  1121. pkt->setRemoteAddr(IOAddress("2001:db8:1::baca"));
  1122. selected = srv.selectSubnet(pkt);
  1123. EXPECT_EQ(selected, subnet2);
  1124. // CASE 4: We have three subnets defined and we received relayed traffic
  1125. // that came out of undefined subnet. We should select nothing
  1126. CfgMgr::instance().clear();
  1127. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1128. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1129. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1130. CfgMgr::instance().commit();
  1131. pkt->relay_info_.clear();
  1132. relay.linkaddr_ = IOAddress("2001:db8:4::1234");
  1133. pkt->relay_info_.push_back(relay);
  1134. selected = srv.selectSubnet(pkt);
  1135. EXPECT_FALSE(selected);
  1136. }
  1137. // This test verifies if selectSubnet() selects proper subnet for a given
  1138. // interface-id option
  1139. TEST_F(Dhcpv6SrvTest, selectSubnetRelayInterfaceId) {
  1140. NakedDhcpv6Srv srv(0);
  1141. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
  1142. Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
  1143. Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
  1144. subnet1->setInterfaceId(generateInterfaceId("relay1"));
  1145. subnet2->setInterfaceId(generateInterfaceId("relay2"));
  1146. // CASE 1: We have only one subnet defined and it is for interface-id "relay1"
  1147. // Packet came with interface-id "relay2". We should not select subnet1
  1148. CfgMgr::instance().clear();
  1149. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1); // just a single subnet
  1150. CfgMgr::instance().commit();
  1151. Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1152. Pkt6::RelayInfo relay;
  1153. relay.linkaddr_ = IOAddress("2001:db8:2::1234");
  1154. relay.peeraddr_ = IOAddress("fe80::1");
  1155. OptionPtr opt = generateInterfaceId("relay2");
  1156. relay.options_.insert(make_pair(opt->getType(), opt));
  1157. pkt->relay_info_.push_back(relay);
  1158. // There is only one subnet configured and we are outside of that subnet
  1159. Subnet6Ptr selected = srv.selectSubnet(pkt);
  1160. EXPECT_FALSE(selected);
  1161. // CASE 2: We have only one subnet defined and it is for interface-id "relay2"
  1162. // Packet came with interface-id "relay2". We should select it
  1163. CfgMgr::instance().clear();
  1164. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2); // just a single subnet
  1165. CfgMgr::instance().commit();
  1166. selected = srv.selectSubnet(pkt);
  1167. EXPECT_EQ(selected, subnet2);
  1168. // CASE 3: We have only 3 subnets defined: one remote for interface-id "relay1",
  1169. // one remote for interface-id "relay2" and third local
  1170. // packet comes with interface-id "relay2". We should select subnet2
  1171. CfgMgr::instance().clear();
  1172. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet1);
  1173. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet2);
  1174. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet3);
  1175. CfgMgr::instance().commit();
  1176. EXPECT_EQ(subnet2, srv.selectSubnet(pkt));
  1177. }
  1178. // This test verifies if the server-id disk operations (read, write) are
  1179. // working properly.
  1180. TEST_F(Dhcpv6SrvTest, ServerID) {
  1181. NakedDhcpv6Srv srv(0);
  1182. string duid1_text = "01:ff:02:03:06:80:90:ab:cd:ef";
  1183. uint8_t duid1[] = { 0x01, 0xff, 2, 3, 6, 0x80, 0x90, 0xab, 0xcd, 0xef };
  1184. OptionBuffer expected_duid1(duid1, duid1 + sizeof(duid1));
  1185. fstream file1(DUID_FILE, ios::out | ios::trunc);
  1186. file1 << duid1_text;
  1187. file1.close();
  1188. // Test reading from a file
  1189. EXPECT_TRUE(srv.loadServerID(DUID_FILE));
  1190. ASSERT_TRUE(srv.getServerID());
  1191. ASSERT_EQ(sizeof(duid1) + Option::OPTION6_HDR_LEN, srv.getServerID()->len());
  1192. ASSERT_TRUE(expected_duid1 == srv.getServerID()->getData());
  1193. // Now test writing to a file
  1194. EXPECT_EQ(0, unlink(DUID_FILE));
  1195. EXPECT_NO_THROW(srv.writeServerID(DUID_FILE));
  1196. fstream file2(DUID_FILE, ios::in);
  1197. ASSERT_TRUE(file2.good());
  1198. string text;
  1199. file2 >> text;
  1200. file2.close();
  1201. EXPECT_EQ(duid1_text, text);
  1202. }
  1203. // Checks if server responses are sent to the proper port.
  1204. TEST_F(Dhcpv6SrvTest, portsDirectTraffic) {
  1205. NakedDhcpv6Srv srv(0);
  1206. // Let's create a simple SOLICIT
  1207. Pkt6Ptr sol = PktCaptures::captureSimpleSolicit();
  1208. // Simulate that we have received that traffic
  1209. srv.fakeReceive(sol);
  1210. // Server will now process to run its normal loop, but instead of calling
  1211. // IfaceMgr::receive6(), it will read all packets from the list set by
  1212. // fakeReceive()
  1213. srv.run();
  1214. // Get Advertise...
  1215. ASSERT_FALSE(srv.fake_sent_.empty());
  1216. Pkt6Ptr adv = srv.fake_sent_.front();
  1217. ASSERT_TRUE(adv);
  1218. // This is sent back to client directly, should be port 546
  1219. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1220. }
  1221. // Checks if server responses are sent to the proper port.
  1222. TEST_F(Dhcpv6SrvTest, portsRelayedTraffic) {
  1223. NakedDhcpv6Srv srv(0);
  1224. // Let's create a simple SOLICIT
  1225. Pkt6Ptr sol = PktCaptures::captureRelayedSolicit();
  1226. // Simulate that we have received that traffic
  1227. srv.fakeReceive(sol);
  1228. // Server will now process to run its normal loop, but instead of calling
  1229. // IfaceMgr::receive6(), it will read all packets from the list set by
  1230. // fakeReceive()
  1231. srv.run();
  1232. // Get Advertise...
  1233. ASSERT_FALSE(srv.fake_sent_.empty());
  1234. Pkt6Ptr adv = srv.fake_sent_.front();
  1235. ASSERT_TRUE(adv);
  1236. // This is sent back to relay, so port is 547
  1237. EXPECT_EQ(DHCP6_SERVER_PORT, adv->getRemotePort());
  1238. }
  1239. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1240. // @todo Uncomment this test as part of #3180 work.
  1241. // Kea code currently fails to handle docsis traffic.
  1242. TEST_F(Dhcpv6SrvTest, docsisTraffic) {
  1243. NakedDhcpv6Srv srv(0);
  1244. // Let's get a traffic capture from DOCSIS3.0 modem
  1245. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1246. // Simulate that we have received that traffic
  1247. srv.fakeReceive(sol);
  1248. // Server will now process to run its normal loop, but instead of calling
  1249. // IfaceMgr::receive6(), it will read all packets from the list set by
  1250. // fakeReceive()
  1251. srv.run();
  1252. // We should have an Advertise in response
  1253. ASSERT_FALSE(srv.fake_sent_.empty());
  1254. Pkt6Ptr adv = srv.fake_sent_.front();
  1255. ASSERT_TRUE(adv);
  1256. }
  1257. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  1258. TEST_F(Dhcpv6SrvTest, docsisVendorOptionsParse) {
  1259. // Let's get a traffic capture from DOCSIS3.0 modem
  1260. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1261. EXPECT_NO_THROW(sol->unpack());
  1262. // Check if the packet contain
  1263. OptionPtr opt = sol->getOption(D6O_VENDOR_OPTS);
  1264. ASSERT_TRUE(opt);
  1265. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1266. ASSERT_TRUE(vendor);
  1267. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_ORO));
  1268. EXPECT_TRUE(vendor->getOption(36));
  1269. EXPECT_TRUE(vendor->getOption(35));
  1270. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_DEVICE_TYPE));
  1271. EXPECT_TRUE(vendor->getOption(3));
  1272. EXPECT_TRUE(vendor->getOption(4));
  1273. EXPECT_TRUE(vendor->getOption(5));
  1274. EXPECT_TRUE(vendor->getOption(6));
  1275. EXPECT_TRUE(vendor->getOption(7));
  1276. EXPECT_TRUE(vendor->getOption(8));
  1277. EXPECT_TRUE(vendor->getOption(9));
  1278. EXPECT_TRUE(vendor->getOption(DOCSIS3_V6_VENDOR_NAME));
  1279. EXPECT_TRUE(vendor->getOption(15));
  1280. EXPECT_FALSE(vendor->getOption(20));
  1281. EXPECT_FALSE(vendor->getOption(11));
  1282. EXPECT_FALSE(vendor->getOption(17));
  1283. }
  1284. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  1285. TEST_F(Dhcpv6SrvTest, docsisVendorORO) {
  1286. NakedDhcpv6Srv srv(0);
  1287. // Let's get a traffic capture from DOCSIS3.0 modem
  1288. Pkt6Ptr sol = PktCaptures::captureDocsisRelayedSolicit();
  1289. ASSERT_NO_THROW(sol->unpack());
  1290. // Check if the packet contains vendor options option
  1291. OptionPtr opt = sol->getOption(D6O_VENDOR_OPTS);
  1292. ASSERT_TRUE(opt);
  1293. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  1294. ASSERT_TRUE(vendor);
  1295. opt = vendor->getOption(DOCSIS3_V6_ORO);
  1296. ASSERT_TRUE(opt);
  1297. OptionUint16ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint16Array>(opt);
  1298. EXPECT_TRUE(oro);
  1299. }
  1300. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  1301. // vendor options is parsed correctly and the requested options are actually assigned.
  1302. TEST_F(Dhcpv6SrvTest, vendorOptionsORO) {
  1303. IfaceMgrTestConfig test_config(true);
  1304. string config = "{ \"interfaces\": [ \"*\" ],"
  1305. "\"preferred-lifetime\": 3000,"
  1306. "\"rebind-timer\": 2000, "
  1307. "\"renew-timer\": 1000, "
  1308. " \"option-def\": [ {"
  1309. " \"name\": \"config-file\","
  1310. " \"code\": 33,"
  1311. " \"type\": \"string\","
  1312. " \"array\": False,"
  1313. " \"record-types\": \"\","
  1314. " \"space\": \"vendor-4491\","
  1315. " \"encapsulate\": \"\""
  1316. " } ],"
  1317. " \"option-data\": [ {"
  1318. " \"name\": \"config-file\","
  1319. " \"space\": \"vendor-4491\","
  1320. " \"code\": 33,"
  1321. " \"data\": \"normal_erouter_v6.cm\","
  1322. " \"csv-format\": True"
  1323. " }],"
  1324. "\"subnet6\": [ { "
  1325. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1326. " \"subnet\": \"2001:db8:1::/48\", "
  1327. " \"renew-timer\": 1000, "
  1328. " \"rebind-timer\": 1000, "
  1329. " \"preferred-lifetime\": 3000,"
  1330. " \"valid-lifetime\": 4000,"
  1331. " \"interface-id\": \"\","
  1332. " \"interface\": \"eth0\""
  1333. " } ],"
  1334. "\"valid-lifetime\": 4000 }";
  1335. ASSERT_NO_THROW(configure(config));
  1336. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1337. sol->setRemoteAddr(IOAddress("fe80::abcd"));
  1338. sol->setIface("eth0");
  1339. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1340. OptionPtr clientid = generateClientId();
  1341. sol->addOption(clientid);
  1342. // Pass it to the server and get an advertise
  1343. Pkt6Ptr adv = srv_.processSolicit(sol);
  1344. // check if we get response at all
  1345. ASSERT_TRUE(adv);
  1346. // We did not include any vendor opts in SOLCIT, so there should be none
  1347. // in ADVERTISE.
  1348. ASSERT_FALSE(adv->getOption(D6O_VENDOR_OPTS));
  1349. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  1350. // That suboption has code 1 and is a docsis ORO option.
  1351. boost::shared_ptr<OptionUint16Array> vendor_oro(new OptionUint16Array(Option::V6,
  1352. DOCSIS3_V6_ORO));
  1353. vendor_oro->addValue(DOCSIS3_V6_CONFIG_FILE); // Request option 33
  1354. OptionPtr vendor(new OptionVendor(Option::V6, 4491));
  1355. vendor->addOption(vendor_oro);
  1356. sol->addOption(vendor);
  1357. // Need to process SOLICIT again after requesting new option.
  1358. adv = srv_.processSolicit(sol);
  1359. ASSERT_TRUE(adv);
  1360. // Check if thre is vendor option response
  1361. OptionPtr tmp = adv->getOption(D6O_VENDOR_OPTS);
  1362. ASSERT_TRUE(tmp);
  1363. // The response should be OptionVendor object
  1364. boost::shared_ptr<OptionVendor> vendor_resp =
  1365. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  1366. ASSERT_TRUE(vendor_resp);
  1367. OptionPtr docsis33 = vendor_resp->getOption(33);
  1368. ASSERT_TRUE(docsis33);
  1369. OptionStringPtr config_file = boost::dynamic_pointer_cast<OptionString>(docsis33);
  1370. ASSERT_TRUE(config_file);
  1371. EXPECT_EQ("normal_erouter_v6.cm", config_file->getValue());
  1372. }
  1373. // Test checks whether it is possible to use option definitions defined in
  1374. // src/lib/dhcp/docsis3_option_defs.h.
  1375. TEST_F(Dhcpv6SrvTest, vendorOptionsDocsisDefinitions) {
  1376. ConstElementPtr x;
  1377. string config_prefix = "{ \"interfaces\": [ \"*\" ],"
  1378. "\"preferred-lifetime\": 3000,"
  1379. "\"rebind-timer\": 2000, "
  1380. "\"renew-timer\": 1000, "
  1381. " \"option-data\": [ {"
  1382. " \"name\": \"config-file\","
  1383. " \"space\": \"vendor-4491\","
  1384. " \"code\": ";
  1385. string config_postfix = ","
  1386. " \"data\": \"normal_erouter_v6.cm\","
  1387. " \"csv-format\": True"
  1388. " }],"
  1389. "\"subnet6\": [ { "
  1390. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1391. " \"subnet\": \"2001:db8:1::/48\", "
  1392. " \"renew-timer\": 1000, "
  1393. " \"rebind-timer\": 1000, "
  1394. " \"preferred-lifetime\": 3000,"
  1395. " \"valid-lifetime\": 4000,"
  1396. " \"interface-id\": \"\","
  1397. " \"interface\": \"\""
  1398. " } ],"
  1399. "\"valid-lifetime\": 4000 }";
  1400. // There is docsis3 (vendor-id=4491) vendor option 33, which is a
  1401. // config-file. Its format is a single string.
  1402. string config_valid = config_prefix + "33" + config_postfix;
  1403. // There is no option 99 defined in vendor-id=4491. As there is no
  1404. // definition, the config should fail.
  1405. string config_bogus = config_prefix + "99" + config_postfix;
  1406. ElementPtr json_bogus = Element::fromJSON(config_bogus);
  1407. ElementPtr json_valid = Element::fromJSON(config_valid);
  1408. NakedDhcpv6Srv srv(0);
  1409. // This should fail (missing option definition)
  1410. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json_bogus));
  1411. ASSERT_TRUE(x);
  1412. comment_ = isc::config::parseAnswer(rcode_, x);
  1413. ASSERT_EQ(1, rcode_);
  1414. // This should work (option definition present)
  1415. EXPECT_NO_THROW(x = configureDhcp6Server(srv, json_valid));
  1416. ASSERT_TRUE(x);
  1417. comment_ = isc::config::parseAnswer(rcode_, x);
  1418. ASSERT_EQ(0, rcode_);
  1419. }
  1420. // This test verifies that the following option structure can be parsed:
  1421. // - option (option space 'foobar')
  1422. // - sub option (option space 'foo')
  1423. // - sub option (option space 'bar')
  1424. TEST_F(Dhcpv6SrvTest, unpackOptions) {
  1425. // Create option definition for each level of encapsulation. Each option
  1426. // definition is for the option code 1. Options may have the same
  1427. // option code because they belong to different option spaces.
  1428. // Top level option encapsulates options which belong to 'space-foo'.
  1429. OptionDefinitionPtr opt_def(new OptionDefinition("option-foobar", 1, "uint32",
  1430. "space-foo"));\
  1431. // Middle option encapsulates options which belong to 'space-bar'
  1432. OptionDefinitionPtr opt_def2(new OptionDefinition("option-foo", 1, "uint16",
  1433. "space-bar"));
  1434. // Low level option doesn't encapsulate any option space.
  1435. OptionDefinitionPtr opt_def3(new OptionDefinition("option-bar", 1,
  1436. "uint8"));
  1437. // Add option definitions to the Configuration Manager. Each goes under
  1438. // different option space.
  1439. CfgOptionDefPtr cfg_option_def =
  1440. CfgMgr::instance().getStagingCfg()->getCfgOptionDef();
  1441. ASSERT_NO_THROW(cfg_option_def->add(opt_def, "space-foobar"));
  1442. ASSERT_NO_THROW(cfg_option_def->add(opt_def2, "space-foo"));
  1443. ASSERT_NO_THROW(cfg_option_def->add(opt_def3, "space-bar"));
  1444. CfgMgr::instance().commit();
  1445. // Create the buffer holding the structure of options.
  1446. const char raw_data[] = {
  1447. // First option starts here.
  1448. 0x00, 0x01, // option code = 1
  1449. 0x00, 0x0F, // option length = 15
  1450. 0x00, 0x01, 0x02, 0x03, // This option carries uint32 value
  1451. // Sub option starts here.
  1452. 0x00, 0x01, // option code = 1
  1453. 0x00, 0x07, // option length = 7
  1454. 0x01, 0x02, // this option carries uint16 value
  1455. // Last option starts here.
  1456. 0x00, 0x01, // option code = 1
  1457. 0x00, 0x01, // option length = 1
  1458. 0x00 // This option carries a single uint8 value and has no sub options.
  1459. };
  1460. OptionBuffer buf(raw_data, raw_data + sizeof(raw_data));
  1461. // Parse options.
  1462. NakedDhcpv6Srv srv(0);
  1463. OptionCollection options;
  1464. ASSERT_NO_THROW(srv.unpackOptions(buf, "space-foobar", options, 0, 0));
  1465. // There should be one top level option.
  1466. ASSERT_EQ(1, options.size());
  1467. boost::shared_ptr<OptionInt<uint32_t> > option_foobar =
  1468. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(options.begin()->
  1469. second);
  1470. ASSERT_TRUE(option_foobar);
  1471. EXPECT_EQ(1, option_foobar->getType());
  1472. EXPECT_EQ(0x00010203, option_foobar->getValue());
  1473. // There should be a middle level option held in option_foobar.
  1474. boost::shared_ptr<OptionInt<uint16_t> > option_foo =
  1475. boost::dynamic_pointer_cast<OptionInt<uint16_t> >(option_foobar->
  1476. getOption(1));
  1477. ASSERT_TRUE(option_foo);
  1478. EXPECT_EQ(1, option_foo->getType());
  1479. EXPECT_EQ(0x0102, option_foo->getValue());
  1480. // Finally, there should be a low level option under option_foo.
  1481. boost::shared_ptr<OptionInt<uint8_t> > option_bar =
  1482. boost::dynamic_pointer_cast<OptionInt<uint8_t> >(option_foo->getOption(1));
  1483. ASSERT_TRUE(option_bar);
  1484. EXPECT_EQ(1, option_bar->getType());
  1485. EXPECT_EQ(0x0, option_bar->getValue());
  1486. }
  1487. // Checks if client packets are classified properly
  1488. TEST_F(Dhcpv6SrvTest, clientClassification) {
  1489. NakedDhcpv6Srv srv(0);
  1490. // Let's create a relayed SOLICIT. This particular relayed SOLICIT has
  1491. // vendor-class set to docsis3.0
  1492. Pkt6Ptr sol1;
  1493. ASSERT_NO_THROW(sol1 = PktCaptures::captureDocsisRelayedSolicit());
  1494. ASSERT_NO_THROW(sol1->unpack());
  1495. srv.classifyPacket(sol1);
  1496. // It should belong to docsis3.0 class. It should not belong to eRouter1.0
  1497. EXPECT_TRUE(sol1->inClass("VENDOR_CLASS_docsis3.0"));
  1498. EXPECT_FALSE(sol1->inClass("eRouter1.0"));
  1499. // Let's get a relayed SOLICIT. This particular relayed SOLICIT has
  1500. // vendor-class set to eRouter1.0
  1501. Pkt6Ptr sol2;
  1502. ASSERT_NO_THROW(sol2 = PktCaptures::captureeRouterRelayedSolicit());
  1503. ASSERT_NO_THROW(sol2->unpack());
  1504. srv.classifyPacket(sol2);
  1505. EXPECT_TRUE(sol2->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  1506. EXPECT_FALSE(sol2->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  1507. }
  1508. // Checks if the client-class field is indeed used for subnet selection.
  1509. // Note that packet classification is already checked in Dhcpv6SrvTest
  1510. // .clientClassification above.
  1511. TEST_F(Dhcpv6SrvTest, clientClassify2) {
  1512. // This test configures 2 subnets. We actually only need the
  1513. // first one, but since there's still this ugly hack that picks
  1514. // the pool if there is only one, we must use more than one
  1515. // subnet. That ugly hack will be removed in #3242, currently
  1516. // under review.
  1517. // The second subnet does not play any role here. The client's
  1518. // IP address belongs to the first subnet, so only that first
  1519. // subnet it being tested.
  1520. string config = "{ \"interfaces\": [ \"*\" ],"
  1521. "\"preferred-lifetime\": 3000,"
  1522. "\"rebind-timer\": 2000, "
  1523. "\"renew-timer\": 1000, "
  1524. "\"subnet6\": [ "
  1525. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1526. " \"subnet\": \"2001:db8:1::/48\", "
  1527. " \"client-class\": \"foo\" "
  1528. " }, "
  1529. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1530. " \"subnet\": \"2001:db8:2::/48\", "
  1531. " \"client-class\": \"xyzzy\" "
  1532. " } "
  1533. "],"
  1534. "\"valid-lifetime\": 4000 }";
  1535. ASSERT_NO_THROW(configure(config));
  1536. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1537. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1538. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1539. OptionPtr clientid = generateClientId();
  1540. sol->addOption(clientid);
  1541. // This discover does not belong to foo class, so it will not
  1542. // be serviced
  1543. EXPECT_FALSE(srv_.selectSubnet(sol));
  1544. // Let's add the packet to bar class and try again.
  1545. sol->addClass("bar");
  1546. // Still not supported, because it belongs to wrong class.
  1547. EXPECT_FALSE(srv_.selectSubnet(sol));
  1548. // Let's add it to maching class.
  1549. sol->addClass("foo");
  1550. // This time it should work
  1551. EXPECT_TRUE(srv_.selectSubnet(sol));
  1552. }
  1553. // Tests whether a packet with custom vendor-class (not erouter or docsis)
  1554. // is classified properly.
  1555. TEST_F(Dhcpv6SrvTest, clientClassification3) {
  1556. NakedDhcpv6Srv srv(0);
  1557. // Let's create a SOLICIT.
  1558. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1559. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1560. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1561. OptionPtr clientid = generateClientId();
  1562. sol->addOption(clientid);
  1563. // Now let's add a vendor-class with id=1234 and content "foo"
  1564. OptionVendorClassPtr vendor_class(new OptionVendorClass(Option::V6, 1234));
  1565. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  1566. tuple = "foo";
  1567. vendor_class->addTuple(tuple);
  1568. sol->addOption(vendor_class);
  1569. // Now the server classifies the packet.
  1570. srv.classifyPacket(sol);
  1571. // The packet should now belong to VENDOR_CLASS_foo.
  1572. EXPECT_TRUE(sol->inClass(srv.VENDOR_CLASS_PREFIX + "foo"));
  1573. // It should not belong to "foo"
  1574. EXPECT_FALSE(sol->inClass("foo"));
  1575. }
  1576. // This test checks that the server will handle a Solicit with the Vendor Class
  1577. // having a length of 4 (enterprise-id only).
  1578. TEST_F(Dhcpv6SrvTest, cableLabsShortVendorClass) {
  1579. NakedDhcpv6Srv srv(0);
  1580. // Create a simple Solicit with the 4-byte long vendor class option.
  1581. Pkt6Ptr sol = PktCaptures::captureCableLabsShortVendorClass();
  1582. // Simulate that we have received that traffic
  1583. srv.fakeReceive(sol);
  1584. // Server will now process to run its normal loop, but instead of calling
  1585. // IfaceMgr::receive6(), it will read all packets from the list set by
  1586. // fakeReceive()
  1587. srv.run();
  1588. // Get Advertise...
  1589. ASSERT_FALSE(srv.fake_sent_.empty());
  1590. Pkt6Ptr adv = srv.fake_sent_.front();
  1591. ASSERT_TRUE(adv);
  1592. // This is sent back to client, so port is 546
  1593. EXPECT_EQ(DHCP6_CLIENT_PORT, adv->getRemotePort());
  1594. }
  1595. // Checks if relay IP address specified in the relay-info structure in
  1596. // subnet6 is being used properly.
  1597. TEST_F(Dhcpv6SrvTest, relayOverride) {
  1598. // We have 2 subnets defined. Note that both have a relay address
  1599. // defined. Both are not belonging to the subnets. That is
  1600. // important, because if the relay belongs to the subnet, there's
  1601. // no need to specify relay override.
  1602. string config = "{ \"interfaces\": [ \"*\" ],"
  1603. "\"preferred-lifetime\": 3000,"
  1604. "\"rebind-timer\": 2000, "
  1605. "\"renew-timer\": 1000, "
  1606. "\"subnet6\": [ "
  1607. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1608. " \"subnet\": \"2001:db8:1::/48\", "
  1609. " \"relay\": { "
  1610. " \"ip-address\": \"2001:db8:3::1\""
  1611. " }"
  1612. " }, "
  1613. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1614. " \"subnet\": \"2001:db8:2::/48\", "
  1615. " \"relay\": { "
  1616. " \"ip-address\": \"2001:db8:3::2\""
  1617. " }"
  1618. " } "
  1619. "],"
  1620. "\"valid-lifetime\": 4000 }";
  1621. // Use this config to set up the server
  1622. ASSERT_NO_THROW(configure(config));
  1623. // Let's get the subnet configuration objects
  1624. const Subnet6Collection* subnets =
  1625. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1626. ASSERT_EQ(2, subnets->size());
  1627. // Let's get them for easy reference
  1628. Subnet6Ptr subnet1 = (*subnets)[0];
  1629. Subnet6Ptr subnet2 = (*subnets)[1];
  1630. ASSERT_TRUE(subnet1);
  1631. ASSERT_TRUE(subnet2);
  1632. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1633. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1634. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1635. OptionPtr clientid = generateClientId();
  1636. sol->addOption(clientid);
  1637. // Now pretend the packet came via one relay.
  1638. Pkt6::RelayInfo relay;
  1639. relay.linkaddr_ = IOAddress("2001:db8:1::1");
  1640. relay.peeraddr_ = IOAddress("fe80::1");
  1641. sol->relay_info_.push_back(relay);
  1642. // This is just a sanity check, we're using regular method: the relay
  1643. // belongs to the first (2001:db8:1::/64) subnet, so it's an easy decision.
  1644. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1645. // Relay belongs to the second subnet, so it should be selected.
  1646. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:2::1");
  1647. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1648. // Now let's check if the relay override for the first subnets works
  1649. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:3::1");
  1650. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1651. // Now repeat that for relay matching the second subnet.
  1652. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:3::2");
  1653. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1654. // Finally, let's check that completely mismatched relay will not get us
  1655. // anything
  1656. sol->relay_info_.back().linkaddr_ = IOAddress("2001:db8:1234::1");
  1657. EXPECT_FALSE(srv_.selectSubnet(sol));
  1658. }
  1659. // Checks if relay IP address specified in the relay-info structure can be
  1660. // used together with client-classification.
  1661. TEST_F(Dhcpv6SrvTest, relayOverrideAndClientClass) {
  1662. // This test configures 2 subnets. They both are on the same link, so they
  1663. // have the same relay-ip address. Furthermore, the first subnet is
  1664. // reserved for clients that belong to class "foo".
  1665. string config = "{ \"interfaces\": [ \"*\" ],"
  1666. "\"preferred-lifetime\": 3000,"
  1667. "\"rebind-timer\": 2000, "
  1668. "\"renew-timer\": 1000, "
  1669. "\"subnet6\": [ "
  1670. " { \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  1671. " \"subnet\": \"2001:db8:1::/48\", "
  1672. " \"client-class\": \"foo\", "
  1673. " \"relay\": { "
  1674. " \"ip-address\": \"2001:db8:3::1\""
  1675. " }"
  1676. " }, "
  1677. " { \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  1678. " \"subnet\": \"2001:db8:2::/48\", "
  1679. " \"relay\": { "
  1680. " \"ip-address\": \"2001:db8:3::1\""
  1681. " }"
  1682. " } "
  1683. "],"
  1684. "\"valid-lifetime\": 4000 }";
  1685. // Use this config to set up the server
  1686. ASSERT_NO_THROW(configure(config));
  1687. // Let's get the subnet configuration objects
  1688. const Subnet6Collection* subnets =
  1689. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  1690. ASSERT_EQ(2, subnets->size());
  1691. // Let's get them for easy reference
  1692. Subnet6Ptr subnet1 = (*subnets)[0];
  1693. Subnet6Ptr subnet2 = (*subnets)[1];
  1694. ASSERT_TRUE(subnet1);
  1695. ASSERT_TRUE(subnet2);
  1696. Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
  1697. sol->setRemoteAddr(IOAddress("2001:db8:1::3"));
  1698. sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
  1699. OptionPtr clientid = generateClientId();
  1700. sol->addOption(clientid);
  1701. // Now pretend the packet came via one relay.
  1702. Pkt6::RelayInfo relay;
  1703. relay.linkaddr_ = IOAddress("2001:db8:3::1");
  1704. relay.peeraddr_ = IOAddress("fe80::1");
  1705. sol->relay_info_.push_back(relay);
  1706. // This packet does not belong to class foo, so it should be rejected in
  1707. // subnet[0], even though the relay-ip matches. It should be accepted in
  1708. // subnet[1], because the subnet matches and there are no class
  1709. // requirements.
  1710. EXPECT_TRUE(subnet2 == srv_.selectSubnet(sol));
  1711. // Now let's add this packet to class foo and recheck. This time it should
  1712. // be accepted in the first subnet, because both class and relay-ip match.
  1713. sol->addClass("foo");
  1714. EXPECT_TRUE(subnet1 == srv_.selectSubnet(sol));
  1715. }
  1716. /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
  1717. /// to call processX() methods.
  1718. } // end of anonymous namespace