dhcp6_srv_unittest.cc 87 KB

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