config_parser_unittest.cc 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694
  1. // Copyright (C) 2012-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 <config/ccsession.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option6_ia.h>
  18. #include <dhcp/iface_mgr.h>
  19. #include <dhcp/option_custom.h>
  20. #include <dhcp/option_int.h>
  21. #include <dhcp/tests/iface_mgr_test_config.h>
  22. #include <dhcp6/json_config_parser.h>
  23. #include <dhcp6/dhcp6_srv.h>
  24. #include <dhcpsrv/addr_utilities.h>
  25. #include <dhcpsrv/cfgmgr.h>
  26. #include <dhcpsrv/cfg_hosts.h>
  27. #include <dhcpsrv/subnet.h>
  28. #include <dhcpsrv/subnet_selector.h>
  29. #include <dhcpsrv/testutils/config_result_check.h>
  30. #include <hooks/hooks_manager.h>
  31. #include "test_data_files_config.h"
  32. #include "test_libraries.h"
  33. #include "marker_file.h"
  34. #include <boost/foreach.hpp>
  35. #include <gtest/gtest.h>
  36. #include <fstream>
  37. #include <iostream>
  38. #include <fstream>
  39. #include <sstream>
  40. #include <string>
  41. #include <vector>
  42. #include <arpa/inet.h>
  43. #include <unistd.h>
  44. using namespace isc;
  45. using namespace isc::asiolink;
  46. using namespace isc::config;
  47. using namespace isc::data;
  48. using namespace isc::dhcp;
  49. using namespace isc::dhcp::test;
  50. using namespace isc::hooks;
  51. using namespace std;
  52. namespace {
  53. std::string specfile(const std::string& name) {
  54. return (std::string(DHCP6_SRC_DIR) + "/" + name);
  55. }
  56. /// @brief Tests that the spec file is valid.
  57. /// Verifies that the DHCP6 configuration specification file is valid.
  58. TEST(Dhcp6SpecTest, basicSpec) {
  59. ASSERT_NO_THROW(isc::config::
  60. moduleSpecFromFile(specfile("dhcp6.spec")));
  61. }
  62. class Dhcp6ParserTest : public ::testing::Test {
  63. public:
  64. Dhcp6ParserTest() :rcode_(-1), srv_(0) {
  65. // srv_(0) means to not open any sockets. We don't want to
  66. // deal with sockets here, just check if configuration handling
  67. // is sane.
  68. const IfaceMgr::IfaceCollection& ifaces =
  69. IfaceMgr::instance().getIfaces();
  70. // There must be some interface detected
  71. if (ifaces.empty()) {
  72. // We can't use ASSERT in constructor
  73. ADD_FAILURE() << "No interfaces detected.";
  74. }
  75. valid_iface_ = ifaces.begin()->getName();
  76. bogus_iface_ = "nonexisting0";
  77. if (IfaceMgr::instance().getIface(bogus_iface_)) {
  78. ADD_FAILURE() << "The '" << bogus_iface_ << "' exists on this system"
  79. << " while the test assumes that it doesn't, to execute"
  80. << " some negative scenarios. Can't continue this test.";
  81. }
  82. // Reset configuration for each test.
  83. resetConfiguration();
  84. }
  85. // Check that no hooks libraries are loaded. This is a pre-condition for
  86. // a number of tests, so is checked in one place. As this uses an
  87. // ASSERT call - and it is not clear from the documentation that Gtest
  88. // predicates can be used in a constructor - the check is placed in SetUp.
  89. void SetUp() {
  90. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  91. ASSERT_TRUE(libraries.empty());
  92. }
  93. ~Dhcp6ParserTest() {
  94. // Reset configuration database after each test.
  95. resetConfiguration();
  96. // ... and delete the hooks library marker files if present
  97. unlink(LOAD_MARKER_FILE);
  98. unlink(UNLOAD_MARKER_FILE);
  99. };
  100. // Checks if config_result (result of DHCP server configuration) has
  101. // expected code (0 for success, other for failures).
  102. // Also stores result in rcode_ and comment_.
  103. void checkResult(ConstElementPtr status, int expected_code) {
  104. ASSERT_TRUE(status);
  105. comment_ = parseAnswer(rcode_, status);
  106. EXPECT_EQ(expected_code, rcode_);
  107. }
  108. /// @brief Create the simple configuration with single option.
  109. ///
  110. /// This function allows to set one of the parameters that configure
  111. /// option value. These parameters are: "name", "code", "data" and
  112. /// "csv-format".
  113. ///
  114. /// @param param_value string holding option parameter value to be
  115. /// injected into the configuration string.
  116. /// @param parameter name of the parameter to be configured with
  117. /// param value.
  118. std::string createConfigWithOption(const std::string& param_value,
  119. const std::string& parameter) {
  120. std::map<std::string, std::string> params;
  121. if (parameter == "name") {
  122. params["name"] = param_value;
  123. params["space"] = "dhcp6";
  124. params["code"] = "38";
  125. params["data"] = "ABCDEF0105";
  126. params["csv-format"] = "False";
  127. } else if (parameter == "space") {
  128. params["name"] = "subscriber-id";
  129. params["space"] = param_value;
  130. params["code"] = "38";
  131. params["data"] = "ABCDEF0105";
  132. params["csv-format"] = "False";
  133. } else if (parameter == "code") {
  134. params["name"] = "subscriber-id";
  135. params["space"] = "dhcp6";
  136. params["code"] = param_value;
  137. params["data"] = "ABCDEF0105";
  138. params["csv-format"] = "False";
  139. } else if (parameter == "data") {
  140. params["name"] = "subscriber-id";
  141. params["space"] = "dhcp6";
  142. params["code"] = "38";
  143. params["data"] = param_value;
  144. params["csv-format"] = "False";
  145. } else if (parameter == "csv-format") {
  146. params["name"] = "subscriber-id";
  147. params["space"] = "dhcp6";
  148. params["code"] = "38";
  149. params["data"] = "ABCDEF0105";
  150. params["csv-format"] = param_value;
  151. }
  152. return (createConfigWithOption(params));
  153. }
  154. /// @brief Create simple configuration with single option.
  155. ///
  156. /// This function creates a configuration for a single option with
  157. /// custom values for all parameters that describe the option.
  158. ///
  159. /// @params params map holding parameters and their values.
  160. /// @return configuration string containing custom values of parameters
  161. /// describing an option.
  162. std::string createConfigWithOption(const std::map<std::string,
  163. std::string>& params)
  164. {
  165. std::ostringstream stream;
  166. stream << "{ \"interfaces\": [ \"*\" ],"
  167. "\"preferred-lifetime\": 3000,"
  168. "\"rebind-timer\": 2000, "
  169. "\"renew-timer\": 1000, "
  170. "\"option-def\": [ {"
  171. " \"name\": \"bool-option\","
  172. " \"code\": 1000,"
  173. " \"type\": \"boolean\","
  174. " \"array\": False,"
  175. " \"record-types\": \"\","
  176. " \"space\": \"dhcp6\","
  177. " \"encapsulate\": \"\""
  178. "} ],"
  179. "\"subnet6\": [ { "
  180. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  181. " \"subnet\": \"2001:db8:1::/64\", "
  182. " \"option-data\": [ {";
  183. bool first = true;
  184. typedef std::pair<std::string, std::string> ParamPair;
  185. BOOST_FOREACH(ParamPair param, params) {
  186. if (!first) {
  187. stream << ", ";
  188. } else {
  189. // cppcheck-suppress unreadVariable
  190. first = false;
  191. }
  192. if (param.first == "name") {
  193. stream << "\"name\": \"" << param.second << "\"";
  194. } else if (param.first == "space") {
  195. stream << "\"space\": \"" << param.second << "\"";
  196. } else if (param.first == "code") {
  197. stream << "\"code\": " << param.second;;
  198. } else if (param.first == "data") {
  199. stream << "\"data\": \"" << param.second << "\"";
  200. } else if (param.first == "csv-format") {
  201. stream << "\"csv-format\": " << param.second;
  202. }
  203. }
  204. stream <<
  205. " } ]"
  206. " } ],"
  207. "\"valid-lifetime\": 4000 }";
  208. return (stream.str());
  209. }
  210. /// @brief Returns an option from the subnet.
  211. ///
  212. /// This function returns an option from a subnet to which the
  213. /// specified subnet address belongs. The option is identified
  214. /// by its code.
  215. ///
  216. /// @param subnet_address Address which belongs to the subnet from
  217. /// which the option is to be returned.
  218. /// @param option_code Code of the option to be returned.
  219. /// @param expected_options_count Expected number of options in
  220. /// the particular subnet.
  221. ///
  222. /// @return Descriptor of the option. If the descriptor holds a
  223. /// NULL option pointer, it means that there was no such option
  224. /// in the subnet.
  225. OptionDescriptor
  226. getOptionFromSubnet(const IOAddress& subnet_address,
  227. const uint16_t option_code,
  228. const uint16_t expected_options_count = 1) {
  229. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  230. selectSubnet(subnet_address, classify_);
  231. if (!subnet) {
  232. /// @todo replace toText() with the use of operator <<.
  233. ADD_FAILURE() << "A subnet for the specified address "
  234. << subnet_address.toText()
  235. << " does not exist in Config Manager";
  236. }
  237. OptionContainerPtr options =
  238. subnet->getCfgOption()->getAll("dhcp6");
  239. if (expected_options_count != options->size()) {
  240. ADD_FAILURE() << "The number of options in the subnet '"
  241. << subnet_address.toText() << "' is different "
  242. " than expected number of options '"
  243. << expected_options_count << "'";
  244. }
  245. // Get the search index. Index #1 is to search using option code.
  246. const OptionContainerTypeIndex& idx = options->get<1>();
  247. // Get the options for specified index. Expecting one option to be
  248. // returned but in theory we may have multiple options with the same
  249. // code so we get the range.
  250. std::pair<OptionContainerTypeIndex::const_iterator,
  251. OptionContainerTypeIndex::const_iterator> range =
  252. idx.equal_range(option_code);
  253. if (std::distance(range.first, range.second) > 1) {
  254. ADD_FAILURE() << "There is more than one option having the"
  255. " option code '" << option_code << "' in a subnet '"
  256. << subnet_address.toText() << "'. Expected "
  257. " at most one option";
  258. } else if (std::distance(range.first, range.second) == 0) {
  259. return (OptionDescriptor(OptionPtr(), false));
  260. }
  261. return (*range.first);
  262. }
  263. /// @brief Parse and Execute configuration
  264. ///
  265. /// Parses a configuration and executes a configuration of the server.
  266. /// If the operation fails, the current test will register a failure.
  267. ///
  268. /// @param config Configuration to parse
  269. /// @param operation Operation being performed. In the case of an error,
  270. /// the error text will include the string "unable to <operation>.".
  271. ///
  272. /// @return true if the configuration succeeded, false if not. In the
  273. /// latter case, a failure will have been added to the current test.
  274. bool
  275. executeConfiguration(const std::string& config, const char* operation) {
  276. ConstElementPtr status;
  277. try {
  278. ElementPtr json = Element::fromJSON(config);
  279. status = configureDhcp6Server(srv_, json);
  280. } catch (const std::exception& ex) {
  281. ADD_FAILURE() << "Unable to " << operation << ". "
  282. << "The following configuration was used: " << std::endl
  283. << config << std::endl
  284. << " and the following error message was returned:"
  285. << ex.what() << std::endl;
  286. return (false);
  287. }
  288. // The status object must not be NULL
  289. if (!status) {
  290. ADD_FAILURE() << "Unable to " << operation << ". "
  291. << "The configuration function returned a null pointer.";
  292. return (false);
  293. }
  294. // Store the answer if we need it.
  295. // Returned value should be 0 (configuration success)
  296. comment_ = parseAnswer(rcode_, status);
  297. if (rcode_ != 0) {
  298. string reason = "";
  299. if (comment_) {
  300. reason = string(" (") + comment_->stringValue() + string(")");
  301. }
  302. ADD_FAILURE() << "Unable to " << operation << ". "
  303. << "The configuration function returned error code "
  304. << rcode_ << reason;
  305. return (false);
  306. }
  307. return (true);
  308. }
  309. /// @brief Reset configuration database.
  310. ///
  311. /// This function resets configuration data base by removing all subnets
  312. /// option-data, and hooks libraries. The reset must be performed after each
  313. /// test to make sure that contents of the database do not affect the
  314. /// results of subsequent tests.
  315. void resetConfiguration() {
  316. string config = "{ \"interfaces\": [ \"*\" ],"
  317. "\"hooks-libraries\": [ ],"
  318. "\"preferred-lifetime\": 3000,"
  319. "\"rebind-timer\": 2000, "
  320. "\"renew-timer\": 1000, "
  321. "\"valid-lifetime\": 4000, "
  322. "\"subnet6\": [ ], "
  323. "\"dhcp-ddns\": { \"enable-updates\" : false }, "
  324. "\"option-def\": [ ], "
  325. "\"option-data\": [ ] }";
  326. static_cast<void>(executeConfiguration(config,
  327. "reset configuration database"));
  328. // The default setting is to listen on all interfaces. In order to
  329. // properly test interface configuration we disable listening on
  330. // all interfaces before each test and later check that this setting
  331. // has been overriden by the configuration used in the test.
  332. CfgMgr::instance().clear();
  333. // Create fresh context.
  334. globalContext()->copyContext(ParserContext(Option::V6));
  335. }
  336. /// @brief Test invalid option parameter value.
  337. ///
  338. /// This test function constructs the simple configuration
  339. /// string and injects invalid option configuration into it.
  340. /// It expects that parser will fail with provided option code.
  341. ///
  342. /// @param param_value string holding invalid option parameter value
  343. /// to be injected into configuration string.
  344. /// @param parameter name of the parameter to be configured with
  345. /// param_value (can be any of "name", "code", "data")
  346. void testInvalidOptionParam(const std::string& param_value,
  347. const std::string& parameter) {
  348. ConstElementPtr x;
  349. std::string config = createConfigWithOption(param_value, parameter);
  350. ElementPtr json = Element::fromJSON(config);
  351. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  352. checkResult(x, 1);
  353. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  354. CfgMgr::instance().clear();
  355. }
  356. /// @brief Test invalid option paramater value.
  357. ///
  358. /// This test function constructs the simple configuration
  359. /// string and injects invalid option configuration into it.
  360. /// It expects that parser will fail with provided option code.
  361. ///
  362. /// @param params Map of parameters defining an option.
  363. void
  364. testInvalidOptionParam(const std::map<std::string, std::string>& params) {
  365. ConstElementPtr x;
  366. std::string config = createConfigWithOption(params);
  367. ElementPtr json = Element::fromJSON(config);
  368. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  369. checkResult(x, 1);
  370. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  371. CfgMgr::instance().clear();
  372. }
  373. /// @brief Test option against given code and data.
  374. ///
  375. /// @param option_desc option descriptor that carries the option to
  376. /// be tested.
  377. /// @param expected_code expected code of the option.
  378. /// @param expected_data expected data in the option.
  379. /// @param expected_data_len length of the reference data.
  380. /// @param extra_data if true extra data is allowed in an option
  381. /// after tested data.
  382. void testOption(const OptionDescriptor& option_desc,
  383. uint16_t expected_code, const uint8_t* expected_data,
  384. size_t expected_data_len,
  385. bool extra_data = false) {
  386. // Check if option descriptor contains valid option pointer.
  387. ASSERT_TRUE(option_desc.option_);
  388. // Verify option type.
  389. EXPECT_EQ(expected_code, option_desc.option_->getType());
  390. // We may have many different option types being created. Some of them
  391. // have dedicated classes derived from Option class. In such case if
  392. // we want to verify the option contents against expected_data we have
  393. // to prepare raw buffer with the contents of the option. The easiest
  394. // way is to call pack() which will prepare on-wire data.
  395. util::OutputBuffer buf(option_desc.option_->getData().size());
  396. option_desc.option_->pack(buf);
  397. if (extra_data) {
  398. // The length of the buffer must be at least equal to size of the
  399. // reference data but it can sometimes be greater than that. This is
  400. // because some options carry suboptions that increase the overall
  401. // length.
  402. ASSERT_GE(buf.getLength() - option_desc.option_->getHeaderLen(),
  403. expected_data_len);
  404. } else {
  405. ASSERT_EQ(buf.getLength() - option_desc.option_->getHeaderLen(),
  406. expected_data_len);
  407. }
  408. // Verify that the data is correct. Do not verify suboptions and a header.
  409. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  410. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option_->getHeaderLen(),
  411. expected_data_len));
  412. }
  413. /// @brief Test option configuration.
  414. ///
  415. /// This function creates a configuration for a specified option using
  416. /// a map of parameters specified as the argument. The map holds
  417. /// name/value pairs which identifies option's configuration parameters:
  418. /// - name
  419. /// - space
  420. /// - code
  421. /// - data
  422. /// - csv-format.
  423. /// This function applies a new server configuration and checks that the
  424. /// option being configured is inserted into CfgMgr. The raw contents of
  425. /// this option are compared with the binary data specified as expected
  426. /// data passed to this function.
  427. ///
  428. /// @param params Map of parameters defining an option.
  429. /// @param option_code Option code.
  430. /// @param expected_data Array containing binary data expected to be stored
  431. /// in the configured option.
  432. /// @param expected_data_len Length of the array holding reference data.
  433. void testConfiguration(const std::map<std::string, std::string>& params,
  434. const uint16_t option_code,
  435. const uint8_t* expected_data,
  436. const size_t expected_data_len) {
  437. CfgMgr::instance().clear();
  438. std::string config = createConfigWithOption(params);
  439. ASSERT_TRUE(executeConfiguration(config, "parse option configuration"));
  440. // The subnet should now hold one option with the specified code.
  441. OptionDescriptor desc =
  442. getOptionFromSubnet(IOAddress("2001:db8:1::5"), option_code);
  443. ASSERT_TRUE(desc.option_);
  444. testOption(desc, option_code, expected_data, expected_data_len);
  445. CfgMgr::instance().clear();
  446. }
  447. int rcode_; ///< Return code (see @ref isc::config::parseAnswer)
  448. Dhcpv6Srv srv_; ///< Instance of the Dhcp6Srv used during tests
  449. ConstElementPtr comment_; ///< Comment (see @ref isc::config::parseAnswer)
  450. string valid_iface_; ///< Valid network interface name (present in system)
  451. string bogus_iface_; ///< invalid network interface name (not in system)
  452. isc::dhcp::ClientClasses classify_; ///< used in client classification
  453. };
  454. // Goal of this test is a verification if a very simple config update
  455. // with just a bumped version number. That's the simplest possible
  456. // config update.
  457. TEST_F(Dhcp6ParserTest, version) {
  458. ConstElementPtr x;
  459. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  460. Element::fromJSON("{\"version\": 0}")));
  461. // returned value must be 0 (configuration accepted)
  462. checkResult(x, 0);
  463. }
  464. /// The goal of this test is to verify that the code accepts only
  465. /// valid commands and malformed or unsupported parameters are rejected.
  466. TEST_F(Dhcp6ParserTest, bogusCommand) {
  467. ConstElementPtr x;
  468. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  469. Element::fromJSON("{\"bogus\": 5}")));
  470. // returned value must be 1 (configuration parse error)
  471. checkResult(x, 1);
  472. }
  473. /// The goal of this test is to verify if configuration without any
  474. /// subnets defined can be accepted.
  475. TEST_F(Dhcp6ParserTest, emptySubnet) {
  476. ConstElementPtr status;
  477. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  478. Element::fromJSON("{ \"interfaces\": [ \"*\" ],"
  479. "\"preferred-lifetime\": 3000,"
  480. "\"rebind-timer\": 2000, "
  481. "\"renew-timer\": 1000, "
  482. "\"subnet6\": [ ], "
  483. "\"valid-lifetime\": 4000 }")));
  484. // returned value should be 0 (success)
  485. checkResult(status, 0);
  486. }
  487. /// The goal of this test is to verify if defined subnet uses global
  488. /// parameter timer definitions.
  489. TEST_F(Dhcp6ParserTest, subnetGlobalDefaults) {
  490. ConstElementPtr status;
  491. string config = "{ \"interfaces\": [ \"*\" ],"
  492. "\"preferred-lifetime\": 3000,"
  493. "\"rebind-timer\": 2000, "
  494. "\"renew-timer\": 1000, "
  495. "\"subnet6\": [ { "
  496. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  497. " \"subnet\": \"2001:db8:1::/64\" } ],"
  498. "\"valid-lifetime\": 4000 }";
  499. ElementPtr json = Element::fromJSON(config);
  500. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  501. // check if returned status is OK
  502. checkResult(status, 0);
  503. // Now check if the configuration was indeed handled and we have
  504. // expected pool configured.
  505. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  506. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  507. ASSERT_TRUE(subnet);
  508. EXPECT_EQ(1000, subnet->getT1());
  509. EXPECT_EQ(2000, subnet->getT2());
  510. EXPECT_EQ(3000, subnet->getPreferred());
  511. EXPECT_EQ(4000, subnet->getValid());
  512. // Check that subnet-id is 1
  513. EXPECT_EQ(1, subnet->getID());
  514. }
  515. // This test checks that multiple subnets can be defined and handled properly.
  516. TEST_F(Dhcp6ParserTest, multipleSubnets) {
  517. ConstElementPtr x;
  518. // Collection of four subnets for which ids should be autogenerated
  519. // - ids are unspecified or set to 0.
  520. string config = "{ \"interfaces\": [ \"*\" ],"
  521. "\"preferred-lifetime\": 3000,"
  522. "\"rebind-timer\": 2000, "
  523. "\"renew-timer\": 1000, "
  524. "\"subnet6\": [ { "
  525. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  526. " \"subnet\": \"2001:db8:1::/64\" "
  527. " },"
  528. " {"
  529. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  530. " \"subnet\": \"2001:db8:2::/64\", "
  531. " \"id\": 0"
  532. " },"
  533. " {"
  534. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  535. " \"subnet\": \"2001:db8:3::/64\" "
  536. " },"
  537. " {"
  538. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  539. " \"subnet\": \"2001:db8:4::/64\" "
  540. " } ],"
  541. "\"valid-lifetime\": 4000 }";
  542. int cnt = 0; // Number of reconfigurations
  543. ElementPtr json = Element::fromJSON(config);
  544. do {
  545. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  546. checkResult(x, 0);
  547. CfgMgr::instance().commit();
  548. const Subnet6Collection* subnets =
  549. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  550. ASSERT_TRUE(subnets);
  551. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  552. // Check subnet-ids of each subnet (it should be monotonously increasing)
  553. EXPECT_EQ(1, subnets->at(0)->getID());
  554. EXPECT_EQ(2, subnets->at(1)->getID());
  555. EXPECT_EQ(3, subnets->at(2)->getID());
  556. EXPECT_EQ(4, subnets->at(3)->getID());
  557. // Repeat reconfiguration process 10 times and check that the subnet-id
  558. // is set to the same value. Technically, just two iterations would be
  559. // sufficient, but it's nice to have a test that exercises reconfiguration
  560. // a bit.
  561. } while (++cnt < 10);
  562. }
  563. // This checks that it is possible to assign arbitrary ids for subnets.
  564. TEST_F(Dhcp6ParserTest, multipleSubnetsExplicitIDs) {
  565. ConstElementPtr x;
  566. // Four subnets with arbitrary subnet ids.
  567. string config = "{ \"interfaces\": [ \"*\" ],"
  568. "\"preferred-lifetime\": 3000,"
  569. "\"rebind-timer\": 2000, "
  570. "\"renew-timer\": 1000, "
  571. "\"subnet6\": [ { "
  572. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  573. " \"subnet\": \"2001:db8:1::/64\", "
  574. " \"id\": 1024"
  575. " },"
  576. " {"
  577. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  578. " \"subnet\": \"2001:db8:2::/64\", "
  579. " \"id\": 100"
  580. " },"
  581. " {"
  582. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  583. " \"subnet\": \"2001:db8:3::/64\", "
  584. " \"id\": 1"
  585. " },"
  586. " {"
  587. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  588. " \"subnet\": \"2001:db8:4::/64\", "
  589. " \"id\": 34"
  590. " } ],"
  591. "\"valid-lifetime\": 4000 }";
  592. int cnt = 0; // Number of reconfigurations
  593. ElementPtr json = Element::fromJSON(config);
  594. do {
  595. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  596. checkResult(x, 0);
  597. CfgMgr::instance().commit();
  598. const Subnet6Collection* subnets =
  599. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  600. ASSERT_TRUE(subnets);
  601. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  602. // Check that subnet ids are as expected.
  603. EXPECT_EQ(1024, subnets->at(0)->getID());
  604. EXPECT_EQ(100, subnets->at(1)->getID());
  605. EXPECT_EQ(1, subnets->at(2)->getID());
  606. EXPECT_EQ(34, subnets->at(3)->getID());
  607. // Repeat reconfiguration process 10 times and check that the subnet-id
  608. // is set to the same value.
  609. } while (++cnt < 3);
  610. }
  611. // CHeck that the configuration with two subnets having the same id is rejected.
  612. TEST_F(Dhcp6ParserTest, multipleSubnetsOverlapingIDs) {
  613. ConstElementPtr x;
  614. // Four subnets, two of them have the same id.
  615. string config = "{ \"interfaces\": [ \"*\" ],"
  616. "\"preferred-lifetime\": 3000,"
  617. "\"rebind-timer\": 2000, "
  618. "\"renew-timer\": 1000, "
  619. "\"subnet6\": [ { "
  620. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  621. " \"subnet\": \"2001:db8:1::/64\", "
  622. " \"id\": 1024"
  623. " },"
  624. " {"
  625. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  626. " \"subnet\": \"2001:db8:2::/64\", "
  627. " \"id\": 100"
  628. " },"
  629. " {"
  630. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  631. " \"subnet\": \"2001:db8:3::/64\", "
  632. " \"id\": 1024"
  633. " },"
  634. " {"
  635. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  636. " \"subnet\": \"2001:db8:4::/64\", "
  637. " \"id\": 34"
  638. " } ],"
  639. "\"valid-lifetime\": 4000 }";
  640. ElementPtr json = Element::fromJSON(config);
  641. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  642. checkResult(x, 1);
  643. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  644. }
  645. // Goal of this test is to verify that a previously configured subnet can be
  646. // deleted in subsequent reconfiguration.
  647. TEST_F(Dhcp6ParserTest, reconfigureRemoveSubnet) {
  648. ConstElementPtr x;
  649. // All four subnets
  650. string config4 = "{ \"interfaces\": [ \"*\" ],"
  651. "\"preferred-lifetime\": 3000,"
  652. "\"rebind-timer\": 2000, "
  653. "\"renew-timer\": 1000, "
  654. "\"subnet6\": [ { "
  655. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  656. " \"subnet\": \"2001:db8:1::/64\", "
  657. " \"id\": 1"
  658. " },"
  659. " {"
  660. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  661. " \"subnet\": \"2001:db8:2::/64\", "
  662. " \"id\": 2"
  663. " },"
  664. " {"
  665. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  666. " \"subnet\": \"2001:db8:3::/64\", "
  667. " \"id\": 3"
  668. " },"
  669. " {"
  670. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  671. " \"subnet\": \"2001:db8:4::/64\", "
  672. " \"id\": 4"
  673. " } ],"
  674. "\"valid-lifetime\": 4000 }";
  675. // Three subnets (the last one removed)
  676. string config_first3 = "{ \"interfaces\": [ \"*\" ],"
  677. "\"preferred-lifetime\": 3000,"
  678. "\"rebind-timer\": 2000, "
  679. "\"renew-timer\": 1000, "
  680. "\"subnet6\": [ { "
  681. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  682. " \"subnet\": \"2001:db8:1::/64\", "
  683. " \"id\": 1"
  684. " },"
  685. " {"
  686. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  687. " \"subnet\": \"2001:db8:2::/64\", "
  688. " \"id\": 2"
  689. " },"
  690. " {"
  691. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  692. " \"subnet\": \"2001:db8:3::/64\", "
  693. " \"id\": 3"
  694. " } ],"
  695. "\"valid-lifetime\": 4000 }";
  696. // Second subnet removed
  697. string config_second_removed = "{ \"interfaces\": [ \"*\" ],"
  698. "\"preferred-lifetime\": 3000,"
  699. "\"rebind-timer\": 2000, "
  700. "\"renew-timer\": 1000, "
  701. "\"subnet6\": [ { "
  702. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  703. " \"subnet\": \"2001:db8:1::/64\", "
  704. " \"id\": 1"
  705. " },"
  706. " {"
  707. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  708. " \"subnet\": \"2001:db8:3::/64\", "
  709. " \"id\": 3"
  710. " },"
  711. " {"
  712. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  713. " \"subnet\": \"2001:db8:4::/64\", "
  714. " \"id\": 4"
  715. " } ],"
  716. "\"valid-lifetime\": 4000 }";
  717. // CASE 1: Configure 4 subnets, then reconfigure and remove the
  718. // last one.
  719. ElementPtr json = Element::fromJSON(config4);
  720. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  721. checkResult(x, 0);
  722. CfgMgr::instance().commit();
  723. const Subnet6Collection* subnets =
  724. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  725. ASSERT_TRUE(subnets);
  726. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  727. // Do the reconfiguration (the last subnet is removed)
  728. json = Element::fromJSON(config_first3);
  729. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  730. checkResult(x, 0);
  731. CfgMgr::instance().commit();
  732. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  733. ASSERT_TRUE(subnets);
  734. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets now (4th is removed)
  735. EXPECT_EQ(1, subnets->at(0)->getID());
  736. EXPECT_EQ(2, subnets->at(1)->getID());
  737. EXPECT_EQ(3, subnets->at(2)->getID());
  738. /// CASE 2: Configure 4 subnets, then reconfigure and remove one
  739. /// from in between (not first, not last)
  740. json = Element::fromJSON(config4);
  741. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  742. checkResult(x, 0);
  743. CfgMgr::instance().commit();
  744. // Do reconfiguration
  745. json = Element::fromJSON(config_second_removed);
  746. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  747. checkResult(x, 0);
  748. CfgMgr::instance().commit();
  749. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  750. ASSERT_TRUE(subnets);
  751. ASSERT_EQ(3, subnets->size()); // We expect 4 subnets
  752. EXPECT_EQ(1, subnets->at(0)->getID());
  753. // The second subnet (with subnet-id = 2) is no longer there
  754. EXPECT_EQ(3, subnets->at(1)->getID());
  755. EXPECT_EQ(4, subnets->at(2)->getID());
  756. }
  757. // This test checks if it is possible to override global values
  758. // on a per subnet basis.
  759. TEST_F(Dhcp6ParserTest, subnetLocal) {
  760. ConstElementPtr status;
  761. string config = "{ \"interfaces\": [ \"*\" ],"
  762. "\"preferred-lifetime\": 3000,"
  763. "\"rebind-timer\": 2000, "
  764. "\"renew-timer\": 1000, "
  765. "\"subnet6\": [ { "
  766. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  767. " \"renew-timer\": 1, "
  768. " \"rebind-timer\": 2, "
  769. " \"preferred-lifetime\": 3,"
  770. " \"valid-lifetime\": 4,"
  771. " \"subnet\": \"2001:db8:1::/64\" } ],"
  772. "\"valid-lifetime\": 4000 }";
  773. ElementPtr json = Element::fromJSON(config);
  774. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  775. // returned value should be 0 (configuration success)
  776. checkResult(status, 0);
  777. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  778. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  779. ASSERT_TRUE(subnet);
  780. EXPECT_EQ(1, subnet->getT1());
  781. EXPECT_EQ(2, subnet->getT2());
  782. EXPECT_EQ(3, subnet->getPreferred());
  783. EXPECT_EQ(4, subnet->getValid());
  784. }
  785. // This test checks if it is possible to define a subnet with an
  786. // interface defined.
  787. TEST_F(Dhcp6ParserTest, subnetInterface) {
  788. ConstElementPtr status;
  789. // There should be at least one interface
  790. string config = "{ \"interfaces\": [ \"*\" ],"
  791. "\"preferred-lifetime\": 3000,"
  792. "\"rebind-timer\": 2000, "
  793. "\"renew-timer\": 1000, "
  794. "\"subnet6\": [ { "
  795. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  796. " \"interface\": \"" + valid_iface_ + "\","
  797. " \"subnet\": \"2001:db8:1::/64\" } ],"
  798. "\"valid-lifetime\": 4000 }";
  799. cout << config << endl;
  800. ElementPtr json = Element::fromJSON(config);
  801. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  802. // returned value should be 0 (configuration success)
  803. checkResult(status, 0);
  804. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  805. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  806. ASSERT_TRUE(subnet);
  807. EXPECT_EQ(valid_iface_, subnet->getIface());
  808. }
  809. // This test checks if invalid interface name will be rejected in
  810. // Subnet6 definition.
  811. TEST_F(Dhcp6ParserTest, subnetInterfaceBogus) {
  812. ConstElementPtr status;
  813. // There should be at least one interface
  814. string config = "{ \"interfaces\": [ \"*\" ],"
  815. "\"preferred-lifetime\": 3000,"
  816. "\"rebind-timer\": 2000, "
  817. "\"renew-timer\": 1000, "
  818. "\"subnet6\": [ { "
  819. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  820. " \"interface\": \"" + bogus_iface_ + "\","
  821. " \"subnet\": \"2001:db8:1::/64\" } ],"
  822. "\"valid-lifetime\": 4000 }";
  823. cout << config << endl;
  824. ElementPtr json = Element::fromJSON(config);
  825. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  826. // returned value should be 1 (configuration error)
  827. checkResult(status, 1);
  828. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  829. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  830. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  831. EXPECT_FALSE(subnet);
  832. }
  833. // This test checks if it is not allowed to define global interface
  834. // parameter.
  835. TEST_F(Dhcp6ParserTest, interfaceGlobal) {
  836. ConstElementPtr status;
  837. string config = "{ \"interfaces\": [ \"*\" ],"
  838. "\"preferred-lifetime\": 3000,"
  839. "\"rebind-timer\": 2000, "
  840. "\"renew-timer\": 1000, "
  841. "\"interface\": \"" + valid_iface_ + "\"," // Not valid. Can be defined in subnet only
  842. "\"subnet6\": [ { "
  843. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  844. " \"subnet\": \"2001:db8:1::/64\" } ],"
  845. "\"valid-lifetime\": 4000 }";
  846. cout << config << endl;
  847. ElementPtr json = Element::fromJSON(config);
  848. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  849. // returned value should be 1 (parse error)
  850. checkResult(status, 1);
  851. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  852. }
  853. // This test checks if it is possible to define a subnet with an
  854. // interface-id option defined.
  855. TEST_F(Dhcp6ParserTest, subnetInterfaceId) {
  856. const string valid_interface_id = "foobar";
  857. const string bogus_interface_id = "blah";
  858. // There should be at least one interface
  859. const string config = "{ "
  860. "\"preferred-lifetime\": 3000,"
  861. "\"rebind-timer\": 2000, "
  862. "\"renew-timer\": 1000, "
  863. "\"subnet6\": [ { "
  864. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  865. " \"interface-id\": \"" + valid_interface_id + "\","
  866. " \"subnet\": \"2001:db8:1::/64\" } ],"
  867. "\"valid-lifetime\": 4000 }";
  868. ElementPtr json = Element::fromJSON(config);
  869. ConstElementPtr status;
  870. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  871. // Returned value should be 0 (configuration success)
  872. checkResult(status, 0);
  873. // Try to get a subnet based on bogus interface-id option
  874. OptionBuffer tmp(bogus_interface_id.begin(), bogus_interface_id.end());
  875. SubnetSelector selector;
  876. selector.first_relay_linkaddr_ = IOAddress("5000::1");
  877. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  878. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  879. selectSubnet(selector);
  880. EXPECT_FALSE(subnet);
  881. // Now try to get subnet for valid interface-id value
  882. tmp = OptionBuffer(valid_interface_id.begin(), valid_interface_id.end());
  883. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  884. subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  885. selectSubnet(selector);
  886. ASSERT_TRUE(subnet);
  887. EXPECT_TRUE(selector.interface_id_->equals(subnet->getInterfaceId()));
  888. }
  889. // This test checks if it is not allowed to define global interface
  890. // parameter.
  891. TEST_F(Dhcp6ParserTest, interfaceIdGlobal) {
  892. const string config = "{ \"interfaces\": [ \"*\" ],"
  893. "\"preferred-lifetime\": 3000,"
  894. "\"rebind-timer\": 2000, "
  895. "\"renew-timer\": 1000, "
  896. "\"interface-id\": \"foobar\"," // Not valid. Can be defined in subnet only
  897. "\"subnet6\": [ { "
  898. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  899. " \"subnet\": \"2001:db8:1::/64\" } ],"
  900. "\"valid-lifetime\": 4000 }";
  901. ElementPtr json = Element::fromJSON(config);
  902. ConstElementPtr status;
  903. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  904. // Returned value should be 1 (parse error)
  905. checkResult(status, 1);
  906. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  907. }
  908. // This test checks if it is not possible to define a subnet with an
  909. // interface (i.e. local subnet) and interface-id (remote subnet) defined.
  910. TEST_F(Dhcp6ParserTest, subnetInterfaceAndInterfaceId) {
  911. const string config = "{ \"preferred-lifetime\": 3000,"
  912. "\"rebind-timer\": 2000, "
  913. "\"renew-timer\": 1000, "
  914. "\"subnet6\": [ { "
  915. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  916. " \"interface\": \"" + valid_iface_ + "\","
  917. " \"interface-id\": \"foobar\","
  918. " \"subnet\": \"2001:db8:1::/64\" } ],"
  919. "\"valid-lifetime\": 4000 }";
  920. ElementPtr json = Element::fromJSON(config);
  921. ConstElementPtr status;
  922. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  923. // Returned value should be 1 (configuration error)
  924. checkResult(status, 1);
  925. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  926. }
  927. // This test checks that multiple pools can be defined and handled properly.
  928. // The test defines 2 subnets, each with 2 pools.
  929. TEST_F(Dhcp6ParserTest, multiplePools) {
  930. // Collection with two subnets, each with 2 pools.
  931. string config = "{ \"interfaces\": [ \"*\" ],"
  932. "\"preferred-lifetime\": 3000,"
  933. "\"rebind-timer\": 2000, "
  934. "\"renew-timer\": 1000, "
  935. "\"subnet6\": [ { "
  936. " \"pools\": [ "
  937. " { \"pool\": \"2001:db8:1::/96\" },"
  938. " { \"pool\": \"2001:db8:1:0:abcd::/112\" }"
  939. " ],"
  940. " \"subnet\": \"2001:db8:1::/64\" "
  941. " },"
  942. " {"
  943. " \"pools\": [ "
  944. " { \"pool\": \"2001:db8:2::1 - 2001:db8:2::ff\" },"
  945. " { \"pool\": \"2001:db8:2::300 - 2001:db8:2::3ff\" }"
  946. " ],"
  947. " \"subnet\": \"2001:db8:2::/64\""
  948. " } ],"
  949. "\"valid-lifetime\": 4000 }";
  950. ElementPtr json;
  951. ASSERT_NO_THROW(json = Element::fromJSON(config));
  952. ConstElementPtr status;
  953. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  954. checkResult(status, 0);
  955. const Subnet6Collection* subnets =
  956. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  957. ASSERT_TRUE(subnets);
  958. ASSERT_EQ(2, subnets->size()); // We expect 2 subnets
  959. // Check the first subnet
  960. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_NA);
  961. ASSERT_EQ(2, pools1.size());
  962. EXPECT_EQ("type=IA_NA, 2001:db8:1::-2001:db8:1::ffff:ffff, delegated_len=128",
  963. pools1[0]->toText());
  964. EXPECT_EQ("type=IA_NA, 2001:db8:1:0:abcd::-2001:db8:1:0:abcd::ffff, delegated_len=128",
  965. pools1[1]->toText());
  966. // There shouldn't be any TA or PD pools
  967. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  968. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  969. // Check the second subnet
  970. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_NA);
  971. ASSERT_EQ(2, pools2.size());
  972. EXPECT_EQ("type=IA_NA, 2001:db8:2::1-2001:db8:2::ff, delegated_len=128",
  973. pools2[0]->toText());
  974. EXPECT_EQ("type=IA_NA, 2001:db8:2::300-2001:db8:2::3ff, delegated_len=128",
  975. pools2[1]->toText());
  976. // There shouldn't be any TA or PD pools
  977. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  978. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  979. }
  980. // Test verifies that a subnet with pool values that do not belong to that
  981. // pool are rejected.
  982. TEST_F(Dhcp6ParserTest, poolOutOfSubnet) {
  983. ConstElementPtr status;
  984. string config = "{ \"interfaces\": [ \"*\" ],"
  985. "\"preferred-lifetime\": 3000,"
  986. "\"rebind-timer\": 2000, "
  987. "\"renew-timer\": 1000, "
  988. "\"subnet6\": [ { "
  989. " \"pools\": [ { \"pool\": \"4001:db8:1::/80\" } ],"
  990. " \"subnet\": \"2001:db8:1::/64\" } ],"
  991. "\"valid-lifetime\": 4000 }";
  992. ElementPtr json = Element::fromJSON(config);
  993. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  994. // returned value must be 1 (values error)
  995. // as the pool does not belong to that subnet
  996. checkResult(status, 1);
  997. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  998. }
  999. // Goal of this test is to verify if pools can be defined
  1000. // using prefix/length notation. There is no separate test for min-max
  1001. // notation as it was tested in several previous tests.
  1002. // Note this test also verifies that subnets can be configured without
  1003. // prefix delegation pools.
  1004. TEST_F(Dhcp6ParserTest, poolPrefixLen) {
  1005. ConstElementPtr x;
  1006. string config = "{ \"interfaces\": [ \"*\" ],"
  1007. "\"preferred-lifetime\": 3000,"
  1008. "\"rebind-timer\": 2000, "
  1009. "\"renew-timer\": 1000, "
  1010. "\"subnet6\": [ { "
  1011. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1012. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1013. "\"valid-lifetime\": 4000 }";
  1014. ElementPtr json = Element::fromJSON(config);
  1015. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1016. // returned value must be 1 (configuration parse error)
  1017. checkResult(x, 0);
  1018. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1019. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1020. ASSERT_TRUE(subnet);
  1021. EXPECT_EQ(1000, subnet->getT1());
  1022. EXPECT_EQ(2000, subnet->getT2());
  1023. EXPECT_EQ(3000, subnet->getPreferred());
  1024. EXPECT_EQ(4000, subnet->getValid());
  1025. }
  1026. // Goal of this test is to verify the basic parsing of a prefix delegation
  1027. // pool. It uses a single, valid pd pool.
  1028. TEST_F(Dhcp6ParserTest, pdPoolBasics) {
  1029. ConstElementPtr x;
  1030. // Define a single valid pd pool.
  1031. string config =
  1032. "{ \"interfaces\": [ \"*\" ],"
  1033. "\"preferred-lifetime\": 3000,"
  1034. "\"rebind-timer\": 2000, "
  1035. "\"renew-timer\": 1000, "
  1036. "\"subnet6\": [ { "
  1037. " \"subnet\": \"2001:db8:1::/64\","
  1038. " \"pd-pools\": ["
  1039. " { \"prefix\": \"2001:db8:1::\", "
  1040. " \"prefix-len\": 64, "
  1041. " \"delegated-len\": 128"
  1042. " } ],"
  1043. "\"valid-lifetime\": 4000 }"
  1044. "] }";
  1045. // Convert the JSON string into Elements.
  1046. ElementPtr json;
  1047. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1048. // Verify that DHCP6 configuration processing succeeds.
  1049. // Returned value must be non-empty ConstElementPtr to config result.
  1050. // rcode should be 0 which indicates successful configuration processing.
  1051. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1052. checkResult(x, 0);
  1053. // Test that we can retrieve the subnet.
  1054. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1055. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1056. ASSERT_TRUE(subnet);
  1057. // Fetch the collection of PD pools. It should have 1 entry.
  1058. PoolCollection pc;
  1059. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1060. EXPECT_EQ(1, pc.size());
  1061. // Get a pointer to the pd pool instance, and verify its contents.
  1062. Pool6Ptr p6;
  1063. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1064. ASSERT_TRUE(p6);
  1065. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1066. EXPECT_EQ(128, p6->getLength());
  1067. // prefix-len is not directly accessible after pool construction, so
  1068. // verify that it was interpreted correctly by checking the last address
  1069. // value.
  1070. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1071. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1072. }
  1073. // Goal of this test is verify that a list of PD pools can be configured.
  1074. // It also verifies that a subnet may be configured with both regular pools
  1075. // and pd pools.
  1076. TEST_F(Dhcp6ParserTest, pdPoolList) {
  1077. ConstElementPtr x;
  1078. // We will configure three pools of prefixes for the subnet. Note that
  1079. // the 3rd prefix is out of the subnet prefix (the prefix doesn't match
  1080. // the subnet prefix).
  1081. const char* prefixes[] = {
  1082. "2001:db8:1:1::",
  1083. "2001:db8:1:2::",
  1084. "3000:1:3::"
  1085. };
  1086. string config =
  1087. "{ \"interfaces\": [ \"*\" ],"
  1088. "\"preferred-lifetime\": 3000,"
  1089. "\"rebind-timer\": 2000, "
  1090. "\"renew-timer\": 1000, "
  1091. "\"subnet6\": [ { "
  1092. " \"pools\": [ { \"pool\": \"2001:db8:1:04::/80\" } ],"
  1093. " \"subnet\": \"2001:db8:1::/40\","
  1094. " \"pd-pools\": ["
  1095. " { \"prefix\": \"2001:db8:1:01::\", "
  1096. " \"prefix-len\": 72, "
  1097. " \"delegated-len\": 80"
  1098. " },"
  1099. " { \"prefix\": \"2001:db8:1:02::\", "
  1100. " \"prefix-len\": 72, "
  1101. " \"delegated-len\": 88"
  1102. " },"
  1103. " { \"prefix\": \"3000:1:03::\", "
  1104. " \"prefix-len\": 72, "
  1105. " \"delegated-len\": 96"
  1106. " }"
  1107. "],"
  1108. "\"valid-lifetime\": 4000 }"
  1109. "] }";
  1110. // Convert the JSON string into Elements.
  1111. ElementPtr json = Element::fromJSON(config);
  1112. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1113. // Verify that DHCP6 configuration processing succeeds.
  1114. // Returned value must be non-empty ConstElementPtr to config result.
  1115. // rcode should be 0 which indicates successful configuration processing.
  1116. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1117. checkResult(x, 0);
  1118. // Test that we can retrieve the subnet.
  1119. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1120. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1121. ASSERT_TRUE(subnet);
  1122. // Fetch the collection of NA pools. It should have 1 entry.
  1123. PoolCollection pc;
  1124. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_NA));
  1125. EXPECT_EQ(1, pc.size());
  1126. // Fetch the collection of PD pools. It should have 3 entries.
  1127. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1128. EXPECT_EQ(3, pc.size());
  1129. // Loop through the pools and verify their contents.
  1130. for (int i = 0; i < 3; i++) {
  1131. Pool6Ptr p6;
  1132. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[i]));
  1133. ASSERT_TRUE(p6);
  1134. EXPECT_EQ(prefixes[i], p6->getFirstAddress().toText());
  1135. EXPECT_EQ((80 + (i * 8)), p6->getLength());
  1136. }
  1137. }
  1138. // Goal of this test is to verify the a whole prefix can be delegated and that
  1139. // a whole subnet can be delegated.
  1140. TEST_F(Dhcp6ParserTest, subnetAndPrefixDelegated) {
  1141. ConstElementPtr x;
  1142. // Define a single valid pd pool.
  1143. string config =
  1144. "{ \"interfaces\": [ \"*\" ],"
  1145. "\"preferred-lifetime\": 3000,"
  1146. "\"rebind-timer\": 2000, "
  1147. "\"renew-timer\": 1000, "
  1148. "\"subnet6\": [ { "
  1149. " \"subnet\": \"2001:db8:1::/64\","
  1150. " \"pd-pools\": ["
  1151. " { \"prefix\": \"2001:db8:1::\", "
  1152. " \"prefix-len\": 64, "
  1153. " \"delegated-len\": 64"
  1154. " } ],"
  1155. "\"valid-lifetime\": 4000 }"
  1156. "] }";
  1157. // Convert the JSON string into Elements.
  1158. ElementPtr json;
  1159. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1160. // Verify that DHCP6 configuration processing succeeds.
  1161. // Returned value must be non-empty ConstElementPtr to config result.
  1162. // rcode should be 0 which indicates successful configuration processing.
  1163. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1164. checkResult(x, 0);
  1165. // Test that we can retrieve the subnet.
  1166. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1167. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1168. ASSERT_TRUE(subnet);
  1169. // Fetch the collection of PD pools. It should have 1 entry.
  1170. PoolCollection pc;
  1171. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1172. EXPECT_EQ(1, pc.size());
  1173. // Get a pointer to the pd pool instance, and verify its contents.
  1174. Pool6Ptr p6;
  1175. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1176. ASSERT_TRUE(p6);
  1177. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1178. EXPECT_EQ(64, p6->getLength());
  1179. // prefix-len is not directly accessible after pool construction, so
  1180. // verify that it was interpreted correctly by checking the last address
  1181. // value.
  1182. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1183. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1184. }
  1185. // Goal of this test is check for proper handling of invalid prefix delegation
  1186. // pool configuration. It uses an array of invalid configurations to check
  1187. // a variety of configuration errors.
  1188. TEST_F(Dhcp6ParserTest, invalidPdPools) {
  1189. ConstElementPtr x;
  1190. const char *config[] = {
  1191. // No prefix.
  1192. "{ \"interfaces\": [ \"*\" ],"
  1193. "\"preferred-lifetime\": 3000,"
  1194. "\"rebind-timer\": 2000, "
  1195. "\"renew-timer\": 1000, "
  1196. "\"subnet6\": [ { "
  1197. " \"subnet\": \"2001:db8:1::/64\","
  1198. " \"pd-pools\": ["
  1199. " { "
  1200. " \"prefix-len\": 64, "
  1201. " \"delegated-len\": 128"
  1202. " } ],"
  1203. "\"valid-lifetime\": 4000 }"
  1204. "] }",
  1205. // No prefix-len.
  1206. "{ \"interfaces\": [ \"*\" ],"
  1207. "\"preferred-lifetime\": 3000,"
  1208. "\"rebind-timer\": 2000, "
  1209. "\"renew-timer\": 1000, "
  1210. "\"subnet6\": [ { "
  1211. " \"subnet\": \"2001:db8:1::/64\","
  1212. " \"pd-pools\": ["
  1213. " { \"prefix\": \"2001:db8:1::\", "
  1214. " \"delegated-len\": 128"
  1215. " } ],"
  1216. "\"valid-lifetime\": 4000 }"
  1217. "] }",
  1218. // No delegated-len.
  1219. "{ \"interfaces\": [ \"*\" ],"
  1220. "\"preferred-lifetime\": 3000,"
  1221. "\"rebind-timer\": 2000, "
  1222. "\"renew-timer\": 1000, "
  1223. "\"subnet6\": [ { "
  1224. " \"subnet\": \"2001:db8:1::/64\","
  1225. " \"pd-pools\": ["
  1226. " { \"prefix\": \"2001:db8:1::\", "
  1227. " \"prefix-len\": 64 "
  1228. " } ],"
  1229. "\"valid-lifetime\": 4000 }"
  1230. "] }",
  1231. // Delegated length is too short.
  1232. "{ \"interfaces\": [ \"*\" ],"
  1233. "\"preferred-lifetime\": 3000,"
  1234. "\"rebind-timer\": 2000, "
  1235. "\"renew-timer\": 1000, "
  1236. "\"subnet6\": [ { "
  1237. " \"subnet\": \"2001:db8:1::/64\","
  1238. " \"pd-pools\": ["
  1239. " { \"prefix\": \"2001:db8:1::\", "
  1240. " \"prefix-len\": 128, "
  1241. " \"delegated-len\": 64"
  1242. " } ],"
  1243. "\"valid-lifetime\": 4000 }"
  1244. "] }"
  1245. };
  1246. ElementPtr json;
  1247. int num_msgs = sizeof(config)/sizeof(char*);
  1248. for (int i = 0; i < num_msgs; i++) {
  1249. // Convert JSON string to Elements.
  1250. ASSERT_NO_THROW(json = Element::fromJSON(config[i]));
  1251. // Configuration processing should fail without a throw.
  1252. ASSERT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1253. // Returned value must be non-empty ConstElementPtr to config result.
  1254. // rcode should be 1 which indicates configuration error.
  1255. checkResult(x, 1);
  1256. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  1257. }
  1258. }
  1259. // The goal of this test is to check whether an option definition
  1260. // that defines an option carrying an IPv6 address can be created.
  1261. TEST_F(Dhcp6ParserTest, optionDefIpv6Address) {
  1262. // Configuration string.
  1263. std::string config =
  1264. "{ \"option-def\": [ {"
  1265. " \"name\": \"foo\","
  1266. " \"code\": 100,"
  1267. " \"type\": \"ipv6-address\","
  1268. " \"array\": False,"
  1269. " \"record-types\": \"\","
  1270. " \"space\": \"isc\","
  1271. " \"encapsulate\": \"\""
  1272. " } ]"
  1273. "}";
  1274. ElementPtr json = Element::fromJSON(config);
  1275. // Make sure that the particular option definition does not exist.
  1276. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1277. getCfgOptionDef()->get("isc", 100);
  1278. ASSERT_FALSE(def);
  1279. // Use the configuration string to create new option definition.
  1280. ConstElementPtr status;
  1281. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1282. ASSERT_TRUE(status);
  1283. // The option definition should now be available in the CfgMgr.
  1284. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1285. ASSERT_TRUE(def);
  1286. // Verify that the option definition data is valid.
  1287. EXPECT_EQ("foo", def->getName());
  1288. EXPECT_EQ(100, def->getCode());
  1289. EXPECT_FALSE(def->getArrayType());
  1290. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, def->getType());
  1291. }
  1292. // The goal of this test is to check whether an option definition
  1293. // that defines an option carrying a record of data fields can
  1294. // be created.
  1295. TEST_F(Dhcp6ParserTest, optionDefRecord) {
  1296. // Configuration string.
  1297. std::string config =
  1298. "{ \"option-def\": [ {"
  1299. " \"name\": \"foo\","
  1300. " \"code\": 100,"
  1301. " \"type\": \"record\","
  1302. " \"array\": False,"
  1303. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  1304. " \"space\": \"isc\","
  1305. " \"encapsulate\": \"\""
  1306. " } ]"
  1307. "}";
  1308. ElementPtr json = Element::fromJSON(config);
  1309. // Make sure that the particular option definition does not exist.
  1310. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1311. getCfgOptionDef()->get("isc", 100);
  1312. ASSERT_FALSE(def);
  1313. // Use the configuration string to create new option definition.
  1314. ConstElementPtr status;
  1315. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1316. ASSERT_TRUE(status);
  1317. checkResult(status, 0);
  1318. // The option definition should now be available in the CfgMgr.
  1319. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1320. ASSERT_TRUE(def);
  1321. // Check the option data.
  1322. EXPECT_EQ("foo", def->getName());
  1323. EXPECT_EQ(100, def->getCode());
  1324. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  1325. EXPECT_FALSE(def->getArrayType());
  1326. // The option comprises the record of data fields. Verify that all
  1327. // fields are present and they are of the expected types.
  1328. const OptionDefinition::RecordFieldsCollection& record_fields =
  1329. def->getRecordFields();
  1330. ASSERT_EQ(4, record_fields.size());
  1331. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  1332. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  1333. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  1334. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  1335. }
  1336. // The goal of this test is to verify that multiple option definitions
  1337. // can be created.
  1338. TEST_F(Dhcp6ParserTest, optionDefMultiple) {
  1339. // Configuration string.
  1340. std::string config =
  1341. "{ \"option-def\": [ {"
  1342. " \"name\": \"foo\","
  1343. " \"code\": 100,"
  1344. " \"type\": \"uint32\","
  1345. " \"array\": False,"
  1346. " \"record-types\": \"\","
  1347. " \"space\": \"isc\","
  1348. " \"encapsulate\": \"\""
  1349. " },"
  1350. " {"
  1351. " \"name\": \"foo-2\","
  1352. " \"code\": 101,"
  1353. " \"type\": \"ipv4-address\","
  1354. " \"array\": False,"
  1355. " \"record-types\": \"\","
  1356. " \"space\": \"isc\","
  1357. " \"encapsulate\": \"\""
  1358. " } ]"
  1359. "}";
  1360. ElementPtr json = Element::fromJSON(config);
  1361. // Make sure that the option definitions do not exist yet.
  1362. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1363. getCfgOptionDef()->get("isc", 100));
  1364. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1365. getCfgOptionDef()->get("isc", 101));
  1366. // Use the configuration string to create new option definitions.
  1367. ConstElementPtr status;
  1368. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1369. ASSERT_TRUE(status);
  1370. checkResult(status, 0);
  1371. // Check the first definition we have created.
  1372. OptionDefinitionPtr def1 = CfgMgr::instance().getStagingCfg()->
  1373. getCfgOptionDef()->get("isc", 100);
  1374. ASSERT_TRUE(def1);
  1375. // Check the option data.
  1376. EXPECT_EQ("foo", def1->getName());
  1377. EXPECT_EQ(100, def1->getCode());
  1378. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  1379. EXPECT_FALSE(def1->getArrayType());
  1380. // Check the second option definition we have created.
  1381. OptionDefinitionPtr def2 = CfgMgr::instance().getStagingCfg()->
  1382. getCfgOptionDef()->get("isc", 101);
  1383. ASSERT_TRUE(def2);
  1384. // Check the option data.
  1385. EXPECT_EQ("foo-2", def2->getName());
  1386. EXPECT_EQ(101, def2->getCode());
  1387. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  1388. EXPECT_FALSE(def2->getArrayType());
  1389. }
  1390. // The goal of this test is to verify that the duplicated option
  1391. // definition is not accepted.
  1392. TEST_F(Dhcp6ParserTest, optionDefDuplicate) {
  1393. // Configuration string. Both option definitions have
  1394. // the same code and belong to the same option space.
  1395. // This configuration should not be accepted.
  1396. std::string config =
  1397. "{ \"option-def\": [ {"
  1398. " \"name\": \"foo\","
  1399. " \"code\": 100,"
  1400. " \"type\": \"uint32\","
  1401. " \"array\": False,"
  1402. " \"record-types\": \"\","
  1403. " \"space\": \"isc\","
  1404. " \"encapsulate\": \"\""
  1405. " },"
  1406. " {"
  1407. " \"name\": \"foo-2\","
  1408. " \"code\": 100,"
  1409. " \"type\": \"ipv4-address\","
  1410. " \"array\": False,"
  1411. " \"record-types\": \"\","
  1412. " \"space\": \"isc\","
  1413. " \"encapsulate\": \"\""
  1414. " } ]"
  1415. "}";
  1416. ElementPtr json = Element::fromJSON(config);
  1417. // Make sure that the option definition does not exist yet.
  1418. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1419. getCfgOptionDef()->get("isc", 100));
  1420. // Use the configuration string to create new option definitions.
  1421. ConstElementPtr status;
  1422. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1423. ASSERT_TRUE(status);
  1424. checkResult(status, 1);
  1425. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1426. }
  1427. // The goal of this test is to verify that the option definition
  1428. // comprising an array of uint32 values can be created.
  1429. TEST_F(Dhcp6ParserTest, optionDefArray) {
  1430. // Configuration string. Created option definition should
  1431. // comprise an array of uint32 values.
  1432. std::string config =
  1433. "{ \"option-def\": [ {"
  1434. " \"name\": \"foo\","
  1435. " \"code\": 100,"
  1436. " \"type\": \"uint32\","
  1437. " \"array\": True,"
  1438. " \"record-types\": \"\","
  1439. " \"space\": \"isc\","
  1440. " \"encapsulate\": \"\""
  1441. " } ]"
  1442. "}";
  1443. ElementPtr json = Element::fromJSON(config);
  1444. // Make sure that the particular option definition does not exist.
  1445. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1446. getCfgOptionDef()->get("isc", 100);
  1447. ASSERT_FALSE(def);
  1448. // Use the configuration string to create new option definition.
  1449. ConstElementPtr status;
  1450. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1451. ASSERT_TRUE(status);
  1452. checkResult(status, 0);
  1453. // The option definition should now be available in the CfgMgr.
  1454. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1455. ASSERT_TRUE(def);
  1456. // Check the option data.
  1457. EXPECT_EQ("foo", def->getName());
  1458. EXPECT_EQ(100, def->getCode());
  1459. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1460. EXPECT_TRUE(def->getArrayType());
  1461. }
  1462. // The purpose of this test to verify that encapsulated option
  1463. // space name may be specified.
  1464. TEST_F(Dhcp6ParserTest, optionDefEncapsulate) {
  1465. // Configuration string. Included the encapsulated
  1466. // option space name.
  1467. std::string config =
  1468. "{ \"option-def\": [ {"
  1469. " \"name\": \"foo\","
  1470. " \"code\": 100,"
  1471. " \"type\": \"uint32\","
  1472. " \"array\": False,"
  1473. " \"record-types\": \"\","
  1474. " \"space\": \"isc\","
  1475. " \"encapsulate\": \"sub-opts-space\""
  1476. " } ]"
  1477. "}";
  1478. ElementPtr json = Element::fromJSON(config);
  1479. // Make sure that the particular option definition does not exist.
  1480. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1481. getCfgOptionDef()->get("isc", 100);
  1482. ASSERT_FALSE(def);
  1483. // Use the configuration string to create new option definition.
  1484. ConstElementPtr status;
  1485. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1486. ASSERT_TRUE(status);
  1487. checkResult(status, 0);
  1488. // The option definition should now be available in the CfgMgr.
  1489. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1490. ASSERT_TRUE(def);
  1491. // Check the option data.
  1492. EXPECT_EQ("foo", def->getName());
  1493. EXPECT_EQ(100, def->getCode());
  1494. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1495. EXPECT_FALSE(def->getArrayType());
  1496. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  1497. }
  1498. /// The purpose of this test is to verify that the option definition
  1499. /// with invalid name is not accepted.
  1500. TEST_F(Dhcp6ParserTest, optionDefInvalidName) {
  1501. // Configuration string. The option name is invalid as it
  1502. // contains the % character.
  1503. std::string config =
  1504. "{ \"option-def\": [ {"
  1505. " \"name\": \"invalid%name\","
  1506. " \"code\": 100,"
  1507. " \"type\": \"string\","
  1508. " \"array\": False,"
  1509. " \"record-types\": \"\","
  1510. " \"space\": \"isc\","
  1511. " \"encapsulate\": \"\""
  1512. " } ]"
  1513. "}";
  1514. ElementPtr json = Element::fromJSON(config);
  1515. // Use the configuration string to create new option definition.
  1516. ConstElementPtr status;
  1517. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1518. ASSERT_TRUE(status);
  1519. // Expecting parsing error (error code 1).
  1520. checkResult(status, 1);
  1521. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1522. }
  1523. /// The purpose of this test is to verify that the option definition
  1524. /// with invalid type is not accepted.
  1525. TEST_F(Dhcp6ParserTest, optionDefInvalidType) {
  1526. // Configuration string. The option type is invalid. It is
  1527. // "sting" instead of "string".
  1528. std::string config =
  1529. "{ \"option-def\": [ {"
  1530. " \"name\": \"foo\","
  1531. " \"code\": 100,"
  1532. " \"type\": \"sting\","
  1533. " \"array\": False,"
  1534. " \"record-types\": \"\","
  1535. " \"space\": \"isc\","
  1536. " \"encapsulate\": \"\""
  1537. " } ]"
  1538. "}";
  1539. ElementPtr json = Element::fromJSON(config);
  1540. // Use the configuration string to create new option definition.
  1541. ConstElementPtr status;
  1542. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1543. ASSERT_TRUE(status);
  1544. // Expecting parsing error (error code 1).
  1545. checkResult(status, 1);
  1546. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1547. }
  1548. /// The purpose of this test is to verify that the option definition
  1549. /// with invalid type is not accepted.
  1550. TEST_F(Dhcp6ParserTest, optionDefInvalidRecordType) {
  1551. // Configuration string. The third of the record fields
  1552. // is invalid. It is "sting" instead of "string".
  1553. std::string config =
  1554. "{ \"option-def\": [ {"
  1555. " \"name\": \"foo\","
  1556. " \"code\": 100,"
  1557. " \"type\": \"record\","
  1558. " \"array\": False,"
  1559. " \"record-types\": \"uint32,uint8,sting\","
  1560. " \"space\": \"isc\","
  1561. " \"encapsulate\": \"\""
  1562. " } ]"
  1563. "}";
  1564. ElementPtr json = Element::fromJSON(config);
  1565. // Use the configuration string to create new option definition.
  1566. ConstElementPtr status;
  1567. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1568. ASSERT_TRUE(status);
  1569. // Expecting parsing error (error code 1).
  1570. checkResult(status, 1);
  1571. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1572. }
  1573. /// The goal of this test is to verify that the invalid encapsulated
  1574. /// option space name is not accepted.
  1575. TEST_F(Dhcp6ParserTest, optionDefInvalidEncapsulatedSpace) {
  1576. // Configuration string. The encapsulated option space
  1577. // name is invalid (% character is not allowed).
  1578. std::string config =
  1579. "{ \"option-def\": [ {"
  1580. " \"name\": \"foo\","
  1581. " \"code\": 100,"
  1582. " \"type\": \"uint32\","
  1583. " \"array\": False,"
  1584. " \"record-types\": \"\","
  1585. " \"space\": \"isc\","
  1586. " \"encapsulate\": \"invalid%space%name\""
  1587. " } ]"
  1588. "}";
  1589. ElementPtr json = Element::fromJSON(config);
  1590. // Use the configuration string to create new option definition.
  1591. ConstElementPtr status;
  1592. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1593. ASSERT_TRUE(status);
  1594. // Expecting parsing error (error code 1).
  1595. checkResult(status, 1);
  1596. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1597. }
  1598. /// The goal of this test is to verify that the encapsulated
  1599. /// option space name can't be specified for the option that
  1600. /// comprises an array of data fields.
  1601. TEST_F(Dhcp6ParserTest, optionDefEncapsulatedSpaceAndArray) {
  1602. // Configuration string. The encapsulated option space
  1603. // name is set to non-empty value and the array flag
  1604. // is set.
  1605. std::string config =
  1606. "{ \"option-def\": [ {"
  1607. " \"name\": \"foo\","
  1608. " \"code\": 100,"
  1609. " \"type\": \"uint32\","
  1610. " \"array\": True,"
  1611. " \"record-types\": \"\","
  1612. " \"space\": \"isc\","
  1613. " \"encapsulate\": \"valid-space-name\""
  1614. " } ]"
  1615. "}";
  1616. ElementPtr json = Element::fromJSON(config);
  1617. // Use the configuration string to create new option definition.
  1618. ConstElementPtr status;
  1619. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1620. ASSERT_TRUE(status);
  1621. // Expecting parsing error (error code 1).
  1622. checkResult(status, 1);
  1623. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1624. }
  1625. /// The goal of this test is to verify that the option may not
  1626. /// encapsulate option space it belongs to.
  1627. TEST_F(Dhcp6ParserTest, optionDefEncapsulateOwnSpace) {
  1628. // Configuration string. Option is set to encapsulate
  1629. // option space it belongs to.
  1630. std::string config =
  1631. "{ \"option-def\": [ {"
  1632. " \"name\": \"foo\","
  1633. " \"code\": 100,"
  1634. " \"type\": \"uint32\","
  1635. " \"array\": False,"
  1636. " \"record-types\": \"\","
  1637. " \"space\": \"isc\","
  1638. " \"encapsulate\": \"isc\""
  1639. " } ]"
  1640. "}";
  1641. ElementPtr json = Element::fromJSON(config);
  1642. // Use the configuration string to create new option definition.
  1643. ConstElementPtr status;
  1644. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1645. ASSERT_TRUE(status);
  1646. // Expecting parsing error (error code 1).
  1647. checkResult(status, 1);
  1648. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1649. }
  1650. /// The purpose of this test is to verify that it is not allowed
  1651. /// to override the standard option (that belongs to dhcp6 option
  1652. /// space and has its definition) and that it is allowed to define
  1653. /// option in the dhcp6 option space that has a code which is not
  1654. /// used by any of the standard options.
  1655. TEST_F(Dhcp6ParserTest, optionStandardDefOverride) {
  1656. // Configuration string. The option code 100 is unassigned
  1657. // so it can be used for a custom option definition in
  1658. // dhcp6 option space.
  1659. std::string config =
  1660. "{ \"option-def\": [ {"
  1661. " \"name\": \"foo\","
  1662. " \"code\": 100,"
  1663. " \"type\": \"string\","
  1664. " \"array\": False,"
  1665. " \"record-types\": \"\","
  1666. " \"space\": \"dhcp6\","
  1667. " \"encapsulate\": \"\""
  1668. " } ]"
  1669. "}";
  1670. ElementPtr json = Element::fromJSON(config);
  1671. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1672. getCfgOptionDef()->get("dhcp6", 100);
  1673. ASSERT_FALSE(def);
  1674. // Use the configuration string to create new option definition.
  1675. ConstElementPtr status;
  1676. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1677. ASSERT_TRUE(status);
  1678. checkResult(status, 0);
  1679. // The option definition should now be available in the CfgMgr.
  1680. def = CfgMgr::instance().getStagingCfg()->
  1681. getCfgOptionDef()->get("dhcp6", 100);
  1682. ASSERT_TRUE(def);
  1683. // Check the option data.
  1684. EXPECT_EQ("foo", def->getName());
  1685. EXPECT_EQ(100, def->getCode());
  1686. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1687. EXPECT_FALSE(def->getArrayType());
  1688. // The combination of option space and code is invalid. The 'dhcp6'
  1689. // option space groups standard options and the code 3 is reserved
  1690. // for one of them.
  1691. config =
  1692. "{ \"option-def\": [ {"
  1693. " \"name\": \"foo\","
  1694. " \"code\": 3,"
  1695. " \"type\": \"string\","
  1696. " \"array\": False,"
  1697. " \"record-types\": \"\","
  1698. " \"space\": \"dhcp6\","
  1699. " \"encapsulate\": \"\""
  1700. " } ]"
  1701. "}";
  1702. json = Element::fromJSON(config);
  1703. // Use the configuration string to create new option definition.
  1704. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1705. ASSERT_TRUE(status);
  1706. // Expecting parsing error (error code 1).
  1707. checkResult(status, 1);
  1708. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1709. /// @todo The option 59 is a standard DHCPv6 option. However, at this point
  1710. /// there is no definition for this option in libdhcp++, so it should be
  1711. /// allowed to define it from the configuration interface. This test will
  1712. /// have to be removed once definitions for remaining standard options are
  1713. /// created.
  1714. config =
  1715. "{ \"option-def\": [ {"
  1716. " \"name\": \"boot-file-name\","
  1717. " \"code\": 59,"
  1718. " \"type\": \"string\","
  1719. " \"array\": False,"
  1720. " \"record-types\": \"\","
  1721. " \"space\": \"dhcp6\","
  1722. " \"encapsulate\": \"\""
  1723. " } ]"
  1724. "}";
  1725. json = Element::fromJSON(config);
  1726. // Use the configuration string to create new option definition.
  1727. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1728. ASSERT_TRUE(status);
  1729. // Expecting success.
  1730. checkResult(status, 0);
  1731. def = CfgMgr::instance().getStagingCfg()->
  1732. getCfgOptionDef()->get("dhcp6", 59);
  1733. ASSERT_TRUE(def);
  1734. // Check the option data.
  1735. EXPECT_EQ("boot-file-name", def->getName());
  1736. EXPECT_EQ(59, def->getCode());
  1737. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1738. EXPECT_FALSE(def->getArrayType());
  1739. }
  1740. // Goal of this test is to verify that global option
  1741. // data is configured for the subnet if the subnet
  1742. // configuration does not include options configuration.
  1743. TEST_F(Dhcp6ParserTest, optionDataDefaults) {
  1744. ConstElementPtr x;
  1745. string config = "{ \"interfaces\": [ \"*\" ],"
  1746. "\"preferred-lifetime\": 3000,"
  1747. "\"rebind-timer\": 2000,"
  1748. "\"renew-timer\": 1000,"
  1749. "\"option-data\": [ {"
  1750. " \"name\": \"subscriber-id\","
  1751. " \"space\": \"dhcp6\","
  1752. " \"code\": 38,"
  1753. " \"data\": \"ABCDEF0105\","
  1754. " \"csv-format\": False"
  1755. " },"
  1756. " {"
  1757. " \"name\": \"preference\","
  1758. " \"space\": \"dhcp6\","
  1759. " \"code\": 7,"
  1760. " \"data\": \"01\","
  1761. " \"csv-format\": True"
  1762. " } ],"
  1763. "\"subnet6\": [ { "
  1764. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1765. " \"subnet\": \"2001:db8:1::/64\""
  1766. " } ],"
  1767. "\"valid-lifetime\": 4000 }";
  1768. ElementPtr json = Element::fromJSON(config);
  1769. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1770. checkResult(x, 0);
  1771. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1772. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1773. ASSERT_TRUE(subnet);
  1774. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  1775. ASSERT_EQ(2, options->size());
  1776. // Get the search index. Index #1 is to search using option code.
  1777. const OptionContainerTypeIndex& idx = options->get<1>();
  1778. // Get the options for specified index. Expecting one option to be
  1779. // returned but in theory we may have multiple options with the same
  1780. // code so we get the range.
  1781. std::pair<OptionContainerTypeIndex::const_iterator,
  1782. OptionContainerTypeIndex::const_iterator> range =
  1783. idx.equal_range(D6O_SUBSCRIBER_ID);
  1784. // Expect single option with the code equal to 38.
  1785. ASSERT_EQ(1, std::distance(range.first, range.second));
  1786. const uint8_t subid_expected[] = {
  1787. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1788. };
  1789. // Check if option is valid in terms of code and carried data.
  1790. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  1791. sizeof(subid_expected));
  1792. range = idx.equal_range(D6O_PREFERENCE);
  1793. ASSERT_EQ(1, std::distance(range.first, range.second));
  1794. // Do another round of testing with second option.
  1795. const uint8_t pref_expected[] = {
  1796. 0x01
  1797. };
  1798. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  1799. sizeof(pref_expected));
  1800. // Check that options with other option codes are not returned.
  1801. for (uint16_t code = 47; code < 57; ++code) {
  1802. range = idx.equal_range(code);
  1803. EXPECT_EQ(0, std::distance(range.first, range.second));
  1804. }
  1805. }
  1806. /// The goal of this test is to verify that two options having the same
  1807. /// option code can be added to different option spaces.
  1808. TEST_F(Dhcp6ParserTest, optionDataTwoSpaces) {
  1809. // This configuration string is to configure two options
  1810. // sharing the code 56 and having different definitions
  1811. // and belonging to the different option spaces.
  1812. // The option definition must be provided for the
  1813. // option that belongs to the 'isc' option space.
  1814. // The definition is not required for the option that
  1815. // belongs to the 'dhcp6' option space as it is the
  1816. // standard option.
  1817. string config = "{ \"interfaces\": [ \"*\" ],"
  1818. "\"preferred-lifetime\": 3000,"
  1819. "\"valid-lifetime\": 4000,"
  1820. "\"rebind-timer\": 2000,"
  1821. "\"renew-timer\": 1000,"
  1822. "\"option-data\": [ {"
  1823. " \"name\": \"subscriber-id\","
  1824. " \"space\": \"dhcp6\","
  1825. " \"code\": 38,"
  1826. " \"data\": \"ABCDEF0105\","
  1827. " \"csv-format\": False"
  1828. " },"
  1829. " {"
  1830. " \"name\": \"foo\","
  1831. " \"space\": \"isc\","
  1832. " \"code\": 38,"
  1833. " \"data\": \"1234\","
  1834. " \"csv-format\": True"
  1835. " } ],"
  1836. "\"option-def\": [ {"
  1837. " \"name\": \"foo\","
  1838. " \"code\": 38,"
  1839. " \"type\": \"uint32\","
  1840. " \"array\": False,"
  1841. " \"record-types\": \"\","
  1842. " \"space\": \"isc\","
  1843. " \"encapsulate\": \"\""
  1844. " } ],"
  1845. "\"subnet6\": [ { "
  1846. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1847. " \"subnet\": \"2001:db8:1::/64\""
  1848. " } ]"
  1849. "}";
  1850. ConstElementPtr status;
  1851. ElementPtr json = Element::fromJSON(config);
  1852. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1853. ASSERT_TRUE(status);
  1854. checkResult(status, 0);
  1855. // Options should be now available for the subnet.
  1856. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1857. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1858. ASSERT_TRUE(subnet);
  1859. // Try to get the option from the space dhcp6.
  1860. OptionDescriptor desc1 = subnet->getCfgOption()->get("dhcp6", 38);
  1861. ASSERT_TRUE(desc1.option_);
  1862. EXPECT_EQ(38, desc1.option_->getType());
  1863. // Try to get the option from the space isc.
  1864. OptionDescriptor desc2 = subnet->getCfgOption()->get("isc", 38);
  1865. ASSERT_TRUE(desc2.option_);
  1866. EXPECT_EQ(38, desc1.option_->getType());
  1867. // Try to get the non-existing option from the non-existing
  1868. // option space and expect that option is not returned.
  1869. OptionDescriptor desc3 = subnet->getCfgOption()->get("non-existing", 38);
  1870. ASSERT_FALSE(desc3.option_);
  1871. }
  1872. // The goal of this test is to verify that it is possible to
  1873. // encapsulate option space containing some options with
  1874. // another option. In this test we create base option that
  1875. // encapsulates option space 'isc' that comprises two other
  1876. // options. Also, for all options their definitions are
  1877. // created.
  1878. TEST_F(Dhcp6ParserTest, optionDataEncapsulate) {
  1879. // @todo DHCP configurations has many dependencies between
  1880. // parameters. First of all, configuration for subnet is
  1881. // inherited from the global values. Thus subnet has to be
  1882. // configured when all global values have been configured.
  1883. // Also, an option can encapsulate another option only
  1884. // if the latter has been configured. For this reason in this
  1885. // test we created two-stage configuration where first we
  1886. // created options that belong to encapsulated option space.
  1887. // In the second stage we add the base option. Also, the Subnet
  1888. // object is configured in the second stage so it is created
  1889. // at the very end (when all other parameters are configured).
  1890. // Starting stage 1. Configure sub-options and their definitions.
  1891. string config = "{ \"interfaces\": [ \"*\" ],"
  1892. "\"preferred-lifetime\": 3000,"
  1893. "\"valid-lifetime\": 4000,"
  1894. "\"rebind-timer\": 2000,"
  1895. "\"renew-timer\": 1000,"
  1896. "\"option-data\": [ {"
  1897. " \"name\": \"foo\","
  1898. " \"space\": \"isc\","
  1899. " \"code\": 110,"
  1900. " \"data\": \"1234\","
  1901. " \"csv-format\": True"
  1902. " },"
  1903. " {"
  1904. " \"name\": \"foo2\","
  1905. " \"space\": \"isc\","
  1906. " \"code\": 111,"
  1907. " \"data\": \"192.168.2.1\","
  1908. " \"csv-format\": True"
  1909. " } ],"
  1910. "\"option-def\": [ {"
  1911. " \"name\": \"foo\","
  1912. " \"code\": 110,"
  1913. " \"type\": \"uint32\","
  1914. " \"array\": False,"
  1915. " \"record-types\": \"\","
  1916. " \"space\": \"isc\","
  1917. " \"encapsulate\": \"\""
  1918. " },"
  1919. " {"
  1920. " \"name\": \"foo2\","
  1921. " \"code\": 111,"
  1922. " \"type\": \"ipv4-address\","
  1923. " \"array\": False,"
  1924. " \"record-types\": \"\","
  1925. " \"space\": \"isc\","
  1926. " \"encapsulate\": \"\""
  1927. " } ]"
  1928. "}";
  1929. ConstElementPtr status;
  1930. ElementPtr json = Element::fromJSON(config);
  1931. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1932. ASSERT_TRUE(status);
  1933. checkResult(status, 0);
  1934. CfgMgr::instance().clear();
  1935. // Stage 2. Configure base option and a subnet. Please note that
  1936. // the configuration from the stage 2 is repeated because BIND
  1937. // configuration manager sends whole configuration for the lists
  1938. // where at least one element is being modified or added.
  1939. config = "{ \"interfaces\": [ \"*\" ],"
  1940. "\"rebind-timer\": 2000,"
  1941. "\"renew-timer\": 1000,"
  1942. "\"option-data\": [ {"
  1943. " \"name\": \"base-option\","
  1944. " \"space\": \"dhcp6\","
  1945. " \"code\": 100,"
  1946. " \"data\": \"11\","
  1947. " \"csv-format\": True"
  1948. " },"
  1949. " {"
  1950. " \"name\": \"foo\","
  1951. " \"space\": \"isc\","
  1952. " \"code\": 110,"
  1953. " \"data\": \"1234\","
  1954. " \"csv-format\": True"
  1955. " },"
  1956. " {"
  1957. " \"name\": \"foo2\","
  1958. " \"space\": \"isc\","
  1959. " \"code\": 111,"
  1960. " \"data\": \"192.168.2.1\","
  1961. " \"csv-format\": True"
  1962. " } ],"
  1963. "\"option-def\": [ {"
  1964. " \"name\": \"base-option\","
  1965. " \"code\": 100,"
  1966. " \"type\": \"uint8\","
  1967. " \"array\": False,"
  1968. " \"record-types\": \"\","
  1969. " \"space\": \"dhcp6\","
  1970. " \"encapsulate\": \"isc\""
  1971. "},"
  1972. "{"
  1973. " \"name\": \"foo\","
  1974. " \"code\": 110,"
  1975. " \"type\": \"uint32\","
  1976. " \"array\": False,"
  1977. " \"record-types\": \"\","
  1978. " \"space\": \"isc\","
  1979. " \"encapsulate\": \"\""
  1980. " },"
  1981. " {"
  1982. " \"name\": \"foo2\","
  1983. " \"code\": 111,"
  1984. " \"type\": \"ipv4-address\","
  1985. " \"array\": False,"
  1986. " \"record-types\": \"\","
  1987. " \"space\": \"isc\","
  1988. " \"encapsulate\": \"\""
  1989. " } ],"
  1990. "\"subnet6\": [ { "
  1991. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1992. " \"subnet\": \"2001:db8:1::/64\""
  1993. " } ]"
  1994. "}";
  1995. json = Element::fromJSON(config);
  1996. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1997. ASSERT_TRUE(status);
  1998. checkResult(status, 0);
  1999. // Get the subnet.
  2000. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2001. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2002. ASSERT_TRUE(subnet);
  2003. // We should have one option available.
  2004. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2005. ASSERT_TRUE(options);
  2006. ASSERT_EQ(1, options->size());
  2007. // Get the option.
  2008. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", 100);
  2009. EXPECT_TRUE(desc.option_);
  2010. EXPECT_EQ(100, desc.option_->getType());
  2011. // This opton should comprise two sub-options.
  2012. // Onf of them is 'foo' with code 110.
  2013. OptionPtr option_foo = desc.option_->getOption(110);
  2014. ASSERT_TRUE(option_foo);
  2015. EXPECT_EQ(110, option_foo->getType());
  2016. // ...another one 'foo2' with code 111.
  2017. OptionPtr option_foo2 = desc.option_->getOption(111);
  2018. ASSERT_TRUE(option_foo2);
  2019. EXPECT_EQ(111, option_foo2->getType());
  2020. }
  2021. // Goal of this test is to verify options configuration
  2022. // for multiple subnets.
  2023. TEST_F(Dhcp6ParserTest, optionDataInMultipleSubnets) {
  2024. ConstElementPtr x;
  2025. string config = "{ \"interfaces\": [ \"*\" ],"
  2026. "\"preferred-lifetime\": 3000,"
  2027. "\"rebind-timer\": 2000, "
  2028. "\"renew-timer\": 1000, "
  2029. "\"subnet6\": [ { "
  2030. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2031. " \"subnet\": \"2001:db8:1::/64\", "
  2032. " \"option-data\": [ {"
  2033. " \"name\": \"subscriber-id\","
  2034. " \"space\": \"dhcp6\","
  2035. " \"code\": 38,"
  2036. " \"data\": \"0102030405060708090A\","
  2037. " \"csv-format\": False"
  2038. " } ]"
  2039. " },"
  2040. " {"
  2041. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2042. " \"subnet\": \"2001:db8:2::/64\", "
  2043. " \"option-data\": [ {"
  2044. " \"name\": \"user-class\","
  2045. " \"space\": \"dhcp6\","
  2046. " \"code\": 15,"
  2047. " \"data\": \"FFFEFDFCFB\","
  2048. " \"csv-format\": False"
  2049. " } ]"
  2050. " } ],"
  2051. "\"valid-lifetime\": 4000 }";
  2052. ElementPtr json = Element::fromJSON(config);
  2053. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2054. checkResult(x, 0);
  2055. Subnet6Ptr subnet1 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2056. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2057. ASSERT_TRUE(subnet1);
  2058. OptionContainerPtr options1 = subnet1->getCfgOption()->getAll("dhcp6");
  2059. ASSERT_EQ(1, options1->size());
  2060. // Get the search index. Index #1 is to search using option code.
  2061. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2062. // Get the options for specified index. Expecting one option to be
  2063. // returned but in theory we may have multiple options with the same
  2064. // code so we get the range.
  2065. std::pair<OptionContainerTypeIndex::const_iterator,
  2066. OptionContainerTypeIndex::const_iterator> range1 =
  2067. idx1.equal_range(D6O_SUBSCRIBER_ID);
  2068. // Expect single option with the code equal to 38.
  2069. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2070. const uint8_t subid_expected[] = {
  2071. 0x01, 0x02, 0x03, 0x04, 0x05,
  2072. 0x06, 0x07, 0x08, 0x09, 0x0A
  2073. };
  2074. // Check if option is valid in terms of code and carried data.
  2075. testOption(*range1.first, D6O_SUBSCRIBER_ID, subid_expected,
  2076. sizeof(subid_expected));
  2077. // Test another subnet in the same way.
  2078. Subnet6Ptr subnet2 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2079. selectSubnet(IOAddress("2001:db8:2::4"), classify_);
  2080. ASSERT_TRUE(subnet2);
  2081. OptionContainerPtr options2 = subnet2->getCfgOption()->getAll("dhcp6");
  2082. ASSERT_EQ(1, options2->size());
  2083. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2084. std::pair<OptionContainerTypeIndex::const_iterator,
  2085. OptionContainerTypeIndex::const_iterator> range2 =
  2086. idx2.equal_range(D6O_USER_CLASS);
  2087. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2088. const uint8_t user_class_expected[] = {
  2089. 0xFF, 0xFE, 0xFD, 0xFC, 0xFB
  2090. };
  2091. testOption(*range2.first, D6O_USER_CLASS, user_class_expected,
  2092. sizeof(user_class_expected));
  2093. }
  2094. // The goal of this test is to check that the option carrying a boolean
  2095. // value can be configured using one of the values: "true", "false", "0"
  2096. // or "1".
  2097. TEST_F(Dhcp6ParserTest, optionDataBoolean) {
  2098. // Create configuration. Use standard option 1000.
  2099. std::map<std::string, std::string> params;
  2100. params["name"] = "bool-option";
  2101. params["space"] = "dhcp6";
  2102. params["code"] = "1000";
  2103. params["data"] = "true";
  2104. params["csv-format"] = "true";
  2105. std::string config = createConfigWithOption(params);
  2106. ASSERT_TRUE(executeConfiguration(config, "parse configuration with a"
  2107. " boolean value"));
  2108. // The subnet should now hold one option with the code 1000.
  2109. OptionDescriptor desc =
  2110. getOptionFromSubnet(IOAddress("2001:db8:1::5"), 1000);
  2111. ASSERT_TRUE(desc.option_);
  2112. // This option should be set to "true", represented as 0x1 in the option
  2113. // buffer.
  2114. uint8_t expected_option_data[] = {
  2115. 0x1
  2116. };
  2117. testConfiguration(params, 1000, expected_option_data,
  2118. sizeof(expected_option_data));
  2119. // Configure the option with the "1" value. This should have the same
  2120. // effect as if "true" was specified.
  2121. params["data"] = "1";
  2122. testConfiguration(params, 1000, expected_option_data,
  2123. sizeof(expected_option_data));
  2124. // The value of "1" with a few leading zeros should work too.
  2125. params["data"] = "00001";
  2126. testConfiguration(params, 1000, expected_option_data,
  2127. sizeof(expected_option_data));
  2128. // Configure the option with the "false" value.
  2129. params["data"] = "false";
  2130. // The option buffer should now hold the value of 0.
  2131. expected_option_data[0] = 0;
  2132. testConfiguration(params, 1000, expected_option_data,
  2133. sizeof(expected_option_data));
  2134. // Specifying "0" should have the same effect as "false".
  2135. params["data"] = "0";
  2136. testConfiguration(params, 1000, expected_option_data,
  2137. sizeof(expected_option_data));
  2138. // The same effect should be for multiple 0 chars.
  2139. params["data"] = "00000";
  2140. testConfiguration(params, 1000, expected_option_data,
  2141. sizeof(expected_option_data));
  2142. // Bogus values should not be accepted.
  2143. params["data"] = "bugus";
  2144. testInvalidOptionParam(params);
  2145. params["data"] = "2";
  2146. testInvalidOptionParam(params);
  2147. // Now let's test that it is possible to use binary format.
  2148. params["data"] = "0";
  2149. params["csv-format"] = "false";
  2150. testConfiguration(params, 1000, expected_option_data,
  2151. sizeof(expected_option_data));
  2152. // The binary 1 should work as well.
  2153. params["data"] = "1";
  2154. expected_option_data[0] = 1;
  2155. testConfiguration(params, 1000, expected_option_data,
  2156. sizeof(expected_option_data));
  2157. // As well as an even number of digits.
  2158. params["data"] = "01";
  2159. testConfiguration(params, 1000, expected_option_data,
  2160. sizeof(expected_option_data));
  2161. }
  2162. // Verify that empty option name is rejected in the configuration.
  2163. TEST_F(Dhcp6ParserTest, optionNameEmpty) {
  2164. // Empty option names not allowed.
  2165. testInvalidOptionParam("", "name");
  2166. }
  2167. // Verify that empty option name with spaces is rejected
  2168. // in the configuration.
  2169. TEST_F(Dhcp6ParserTest, optionNameSpaces) {
  2170. // Spaces in option names not allowed.
  2171. testInvalidOptionParam("option foo", "name");
  2172. }
  2173. // Verify that negative option code is rejected in the configuration.
  2174. TEST_F(Dhcp6ParserTest, optionCodeNegative) {
  2175. // Check negative option code -4. This should fail too.
  2176. testInvalidOptionParam("-4", "code");
  2177. }
  2178. // Verify that out of bounds option code is rejected in the configuration.
  2179. TEST_F(Dhcp6ParserTest, optionCodeNonUint16) {
  2180. // The valid option codes are uint16_t values so passing
  2181. // uint16_t maximum value incremented by 1 should result
  2182. // in failure.
  2183. testInvalidOptionParam("65536", "code");
  2184. }
  2185. // Verify that out of bounds option code is rejected in the configuration.
  2186. TEST_F(Dhcp6ParserTest, optionCodeHighNonUint16) {
  2187. // Another check for uint16_t overflow but this time
  2188. // let's pass even greater option code value.
  2189. testInvalidOptionParam("70000", "code");
  2190. }
  2191. // Verify that zero option code is rejected in the configuration.
  2192. TEST_F(Dhcp6ParserTest, optionCodeZero) {
  2193. // Option code 0 is reserved and should not be accepted
  2194. // by configuration parser.
  2195. testInvalidOptionParam("0", "code");
  2196. }
  2197. // Verify that option data which contains non hexadecimal characters
  2198. // is rejected by the configuration.
  2199. TEST_F(Dhcp6ParserTest, optionDataInvalidChar) {
  2200. // Option code 0 is reserved and should not be accepted
  2201. // by configuration parser.
  2202. testInvalidOptionParam("01020R", "data");
  2203. }
  2204. // Verify that option data containing '0x' prefix is rejected
  2205. // by the configuration.
  2206. TEST_F(Dhcp6ParserTest, optionDataUnexpectedPrefix) {
  2207. // Option code 0 is reserved and should not be accepted
  2208. // by configuration parser.
  2209. testInvalidOptionParam("0x0102", "data");
  2210. }
  2211. // Verify that either lower or upper case characters are allowed
  2212. // to specify the option data.
  2213. TEST_F(Dhcp6ParserTest, optionDataLowerCase) {
  2214. ConstElementPtr x;
  2215. std::string config = createConfigWithOption("0a0b0C0D", "data");
  2216. ElementPtr json = Element::fromJSON(config);
  2217. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2218. checkResult(x, 0);
  2219. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2220. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2221. ASSERT_TRUE(subnet);
  2222. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2223. ASSERT_EQ(1, options->size());
  2224. // Get the search index. Index #1 is to search using option code.
  2225. const OptionContainerTypeIndex& idx = options->get<1>();
  2226. // Get the options for specified index. Expecting one option to be
  2227. // returned but in theory we may have multiple options with the same
  2228. // code so we get the range.
  2229. std::pair<OptionContainerTypeIndex::const_iterator,
  2230. OptionContainerTypeIndex::const_iterator> range =
  2231. idx.equal_range(D6O_SUBSCRIBER_ID);
  2232. // Expect single option with the code equal to 38.
  2233. ASSERT_EQ(1, std::distance(range.first, range.second));
  2234. const uint8_t subid_expected[] = {
  2235. 0x0A, 0x0B, 0x0C, 0x0D
  2236. };
  2237. // Check if option is valid in terms of code and carried data.
  2238. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  2239. sizeof(subid_expected));
  2240. }
  2241. // Verify that specific option object is returned for standard
  2242. // option which has dedicated option class derived from Option.
  2243. TEST_F(Dhcp6ParserTest, stdOptionData) {
  2244. ConstElementPtr x;
  2245. std::map<std::string, std::string> params;
  2246. params["name"] = "ia-na";
  2247. params["space"] = "dhcp6";
  2248. // Option code 3 means OPTION_IA_NA.
  2249. params["code"] = "3";
  2250. params["data"] = "12345, 6789, 1516";
  2251. params["csv-format"] = "True";
  2252. std::string config = createConfigWithOption(params);
  2253. ElementPtr json = Element::fromJSON(config);
  2254. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2255. checkResult(x, 0);
  2256. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2257. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2258. ASSERT_TRUE(subnet);
  2259. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2260. ASSERT_EQ(1, options->size());
  2261. // Get the search index. Index #1 is to search using option code.
  2262. const OptionContainerTypeIndex& idx = options->get<1>();
  2263. // Get the options for specified index. Expecting one option to be
  2264. // returned but in theory we may have multiple options with the same
  2265. // code so we get the range.
  2266. std::pair<OptionContainerTypeIndex::const_iterator,
  2267. OptionContainerTypeIndex::const_iterator> range =
  2268. idx.equal_range(D6O_IA_NA);
  2269. // Expect single option with the code equal to IA_NA option code.
  2270. ASSERT_EQ(1, std::distance(range.first, range.second));
  2271. // The actual pointer to the option is held in the option field
  2272. // in the structure returned.
  2273. OptionPtr option = range.first->option_;
  2274. ASSERT_TRUE(option);
  2275. // Option object returned for here is expected to be Option6IA
  2276. // which is derived from Option. This class is dedicated to
  2277. // represent standard option IA_NA.
  2278. boost::shared_ptr<Option6IA> optionIA =
  2279. boost::dynamic_pointer_cast<Option6IA>(option);
  2280. // If cast is unsuccessful than option returned was of a
  2281. // different type than Option6IA. This is wrong.
  2282. ASSERT_TRUE(optionIA);
  2283. // If cast was successful we may use accessors exposed by
  2284. // Option6IA to validate that the content of this option
  2285. // has been set correctly.
  2286. EXPECT_EQ(12345, optionIA->getIAID());
  2287. EXPECT_EQ(6789, optionIA->getT1());
  2288. EXPECT_EQ(1516, optionIA->getT2());
  2289. }
  2290. // This test checks if vendor options can be specified in the config file
  2291. // (in hex format), and later retrieved from configured subnet
  2292. TEST_F(Dhcp6ParserTest, vendorOptionsHex) {
  2293. // This configuration string is to configure two options
  2294. // sharing the code 1 and belonging to the different vendor spaces.
  2295. // (different vendor-id values).
  2296. string config = "{ \"interfaces\": [ \"*\" ],"
  2297. "\"preferred-lifetime\": 3000,"
  2298. "\"valid-lifetime\": 4000,"
  2299. "\"rebind-timer\": 2000,"
  2300. "\"renew-timer\": 1000,"
  2301. "\"option-data\": [ {"
  2302. " \"name\": \"option-one\","
  2303. " \"space\": \"vendor-4491\","
  2304. " \"code\": 100,"
  2305. " \"data\": \"ABCDEF0105\","
  2306. " \"csv-format\": False"
  2307. " },"
  2308. " {"
  2309. " \"name\": \"option-two\","
  2310. " \"space\": \"vendor-1234\","
  2311. " \"code\": 100,"
  2312. " \"data\": \"1234\","
  2313. " \"csv-format\": False"
  2314. " } ],"
  2315. "\"subnet6\": [ { "
  2316. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2317. " \"subnet\": \"2001:db8:1::/64\""
  2318. " } ]"
  2319. "}";
  2320. ConstElementPtr status;
  2321. ElementPtr json = Element::fromJSON(config);
  2322. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2323. ASSERT_TRUE(status);
  2324. checkResult(status, 0);
  2325. // Options should be now available for the subnet.
  2326. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2327. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2328. ASSERT_TRUE(subnet);
  2329. // Try to get the option from the vendor space 4491
  2330. OptionDescriptor desc1 = subnet->getCfgOption()->get(4491, 100);
  2331. ASSERT_TRUE(desc1.option_);
  2332. EXPECT_EQ(100, desc1.option_->getType());
  2333. // Try to get the option from the vendor space 1234
  2334. OptionDescriptor desc2 = subnet->getCfgOption()->get(1234, 100);
  2335. ASSERT_TRUE(desc2.option_);
  2336. EXPECT_EQ(100, desc1.option_->getType());
  2337. // Try to get the non-existing option from the non-existing
  2338. // option space and expect that option is not returned.
  2339. OptionDescriptor desc3 = subnet->getCfgOption()->get(5678, 38);
  2340. ASSERT_FALSE(desc3.option_);
  2341. }
  2342. // This test checks if vendor options can be specified in the config file,
  2343. // (in csv format), and later retrieved from configured subnet
  2344. TEST_F(Dhcp6ParserTest, vendorOptionsCsv) {
  2345. // This configuration string is to configure two options
  2346. // sharing the code 1 and belonging to the different vendor spaces.
  2347. // (different vendor-id values).
  2348. string config = "{ \"interfaces\": [ \"*\" ],"
  2349. "\"preferred-lifetime\": 3000,"
  2350. "\"valid-lifetime\": 4000,"
  2351. "\"rebind-timer\": 2000,"
  2352. "\"renew-timer\": 1000,"
  2353. "\"option-data\": [ {"
  2354. " \"name\": \"foo\","
  2355. " \"space\": \"vendor-4491\","
  2356. " \"code\": 100,"
  2357. " \"data\": \"this is a string vendor-opt\","
  2358. " \"csv-format\": True"
  2359. " } ],"
  2360. "\"option-def\": [ {"
  2361. " \"name\": \"foo\","
  2362. " \"code\": 100,"
  2363. " \"type\": \"string\","
  2364. " \"array\": False,"
  2365. " \"record-types\": \"\","
  2366. " \"space\": \"vendor-4491\","
  2367. " \"encapsulate\": \"\""
  2368. " } ],"
  2369. "\"subnet6\": [ { "
  2370. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2371. " \"subnet\": \"2001:db8:1::/64\""
  2372. " } ]"
  2373. "}";
  2374. ConstElementPtr status;
  2375. ElementPtr json = Element::fromJSON(config);
  2376. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2377. ASSERT_TRUE(status);
  2378. checkResult(status, 0);
  2379. // Options should be now available for the subnet.
  2380. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2381. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2382. ASSERT_TRUE(subnet);
  2383. // Try to get the option from the vendor space 4491
  2384. OptionDescriptor desc1 = subnet->getCfgOption()->get(4491, 100);
  2385. ASSERT_TRUE(desc1.option_);
  2386. EXPECT_EQ(100, desc1.option_->getType());
  2387. // Try to get the non-existing option from the non-existing
  2388. // option space and expect that option is not returned.
  2389. OptionDescriptor desc2 = subnet->getCfgOption()->get(5678, 100);
  2390. ASSERT_FALSE(desc2.option_);
  2391. }
  2392. /// @todo add tests similar to vendorOptionsCsv and vendorOptionsHex, but for
  2393. /// vendor options defined in a subnet.
  2394. // The goal of this test is to verify that the standard option can
  2395. // be configured to encapsulate multiple other options.
  2396. /// @todo This test is currently disabled because it relies on the option
  2397. /// 17 which is treated differently than all other options. There are no
  2398. /// other standard options used by Kea which would encapsulate other
  2399. /// options and for which values could be configured here.
  2400. TEST_F(Dhcp6ParserTest, DISABLED_stdOptionDataEncapsulate) {
  2401. // The configuration is two stage process in this test.
  2402. // In the first stahe we create definitions of suboptions
  2403. // that we will add to the base option.
  2404. // Let's create some dummy options: foo and foo2.
  2405. string config = "{ \"interfaces\": [ \"*\" ],"
  2406. "\"preferred-lifetime\": 3000,"
  2407. "\"valid-lifetime\": 4000,"
  2408. "\"rebind-timer\": 2000,"
  2409. "\"renew-timer\": 1000,"
  2410. "\"option-data\": [ {"
  2411. " \"name\": \"foo\","
  2412. " \"space\": \"vendor-opts-space\","
  2413. " \"code\": 110,"
  2414. " \"data\": \"1234\","
  2415. " \"csv-format\": True"
  2416. " },"
  2417. " {"
  2418. " \"name\": \"foo2\","
  2419. " \"space\": \"vendor-opts-space\","
  2420. " \"code\": 111,"
  2421. " \"data\": \"192.168.2.1\","
  2422. " \"csv-format\": True"
  2423. " } ],"
  2424. "\"option-def\": [ {"
  2425. " \"name\": \"foo\","
  2426. " \"code\": 110,"
  2427. " \"type\": \"uint32\","
  2428. " \"array\": False,"
  2429. " \"record-types\": \"\","
  2430. " \"space\": \"vendor-opts-space\","
  2431. " \"encapsulate\": \"\""
  2432. " },"
  2433. " {"
  2434. " \"name\": \"foo2\","
  2435. " \"code\": 111,"
  2436. " \"type\": \"ipv4-address\","
  2437. " \"array\": False,"
  2438. " \"record-types\": \"\","
  2439. " \"space\": \"vendor-opts-space\","
  2440. " \"encapsulate\": \"\""
  2441. " } ]"
  2442. "}";
  2443. ConstElementPtr status;
  2444. ElementPtr json = Element::fromJSON(config);
  2445. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2446. ASSERT_TRUE(status);
  2447. checkResult(status, 0);
  2448. CfgMgr::instance().clear();
  2449. // Once the definitions have been added we can configure the
  2450. // standard option #17. This option comprises an enterprise
  2451. // number and sub options. By convention (introduced in
  2452. // std_option_defs.h) option named 'vendor-opts'
  2453. // encapsulates the option space named 'vendor-opts-space'.
  2454. // We add our dummy options to this option space and thus
  2455. // they should be included as sub-options in the 'vendor-opts'
  2456. // option.
  2457. config = "{ \"interfaces\": [ \"*\" ],"
  2458. "\"rebind-timer\": 2000,"
  2459. "\"renew-timer\": 1000,"
  2460. "\"option-data\": [ {"
  2461. " \"name\": \"vendor-opts\","
  2462. " \"space\": \"dhcp6\","
  2463. " \"code\": 17,"
  2464. " \"data\": \"1234\","
  2465. " \"csv-format\": True"
  2466. " },"
  2467. " {"
  2468. " \"name\": \"foo\","
  2469. " \"space\": \"vendor-opts-space\","
  2470. " \"code\": 110,"
  2471. " \"data\": \"1234\","
  2472. " \"csv-format\": True"
  2473. " },"
  2474. " {"
  2475. " \"name\": \"foo2\","
  2476. " \"space\": \"vendor-opts-space\","
  2477. " \"code\": 111,"
  2478. " \"data\": \"192.168.2.1\","
  2479. " \"csv-format\": True"
  2480. " } ],"
  2481. "\"option-def\": [ {"
  2482. " \"name\": \"foo\","
  2483. " \"code\": 110,"
  2484. " \"type\": \"uint32\","
  2485. " \"array\": False,"
  2486. " \"record-types\": \"\","
  2487. " \"space\": \"vendor-opts-space\","
  2488. " \"encapsulate\": \"\""
  2489. " },"
  2490. " {"
  2491. " \"name\": \"foo2\","
  2492. " \"code\": 111,"
  2493. " \"type\": \"ipv4-address\","
  2494. " \"array\": False,"
  2495. " \"record-types\": \"\","
  2496. " \"space\": \"vendor-opts-space\","
  2497. " \"encapsulate\": \"\""
  2498. " } ],"
  2499. "\"subnet6\": [ { "
  2500. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2501. " \"subnet\": \"2001:db8:1::/64\""
  2502. " } ]"
  2503. "}";
  2504. json = Element::fromJSON(config);
  2505. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2506. ASSERT_TRUE(status);
  2507. checkResult(status, 0);
  2508. // Get the subnet.
  2509. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2510. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2511. ASSERT_TRUE(subnet);
  2512. // We should have one option available.
  2513. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2514. ASSERT_TRUE(options);
  2515. ASSERT_EQ(1, options->size());
  2516. // Get the option.
  2517. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", D6O_VENDOR_OPTS);
  2518. EXPECT_TRUE(desc.option_);
  2519. EXPECT_EQ(D6O_VENDOR_OPTS, desc.option_->getType());
  2520. // Option with the code 110 should be added as a sub-option.
  2521. OptionPtr option_foo = desc.option_->getOption(110);
  2522. ASSERT_TRUE(option_foo);
  2523. EXPECT_EQ(110, option_foo->getType());
  2524. // This option comprises a single uint32_t value thus it is
  2525. // represented by OptionInt<uint32_t> class. Let's get the
  2526. // object of this type.
  2527. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  2528. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  2529. ASSERT_TRUE(option_foo_uint32);
  2530. // Validate the value according to the configuration.
  2531. EXPECT_EQ(1234, option_foo_uint32->getValue());
  2532. // Option with the code 111 should be added as a sub-option.
  2533. OptionPtr option_foo2 = desc.option_->getOption(111);
  2534. ASSERT_TRUE(option_foo2);
  2535. EXPECT_EQ(111, option_foo2->getType());
  2536. // This option comprises the IPV4 address. Such option is
  2537. // represented by OptionCustom object.
  2538. OptionCustomPtr option_foo2_v4 =
  2539. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  2540. ASSERT_TRUE(option_foo2_v4);
  2541. // Get the IP address carried by this option and validate it.
  2542. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  2543. // Option with the code 112 should not be added.
  2544. EXPECT_FALSE(desc.option_->getOption(112));
  2545. }
  2546. // Tests of the hooks libraries configuration. All tests have the pre-
  2547. // condition (checked in the test fixture's SetUp() method) that no hooks
  2548. // libraries are loaded at the start of the tests.
  2549. // Helper function to return a configuration containing an arbitrary number
  2550. // of hooks libraries.
  2551. std::string
  2552. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  2553. const string quote("\"");
  2554. // Create the first part of the configuration string.
  2555. string config =
  2556. "{ \"interfaces\": [ \"*\" ],"
  2557. "\"hooks-libraries\": [";
  2558. // Append the libraries (separated by commas if needed)
  2559. for (int i = 0; i < libraries.size(); ++i) {
  2560. if (i > 0) {
  2561. config += string(", ");
  2562. }
  2563. config += (quote + libraries[i] + quote);
  2564. }
  2565. // Append the remainder of the configuration.
  2566. config += string(
  2567. "],"
  2568. "\"rebind-timer\": 2000,"
  2569. "\"renew-timer\": 1000,"
  2570. "\"option-data\": [ {"
  2571. " \"name\": \"foo\","
  2572. " \"space\": \"vendor-opts-space\","
  2573. " \"code\": 110,"
  2574. " \"data\": \"1234\","
  2575. " \"csv-format\": True"
  2576. " },"
  2577. " {"
  2578. " \"name\": \"foo2\","
  2579. " \"space\": \"vendor-opts-space\","
  2580. " \"code\": 111,"
  2581. " \"data\": \"192.168.2.1\","
  2582. " \"csv-format\": True"
  2583. " } ],"
  2584. "\"option-def\": [ {"
  2585. " \"name\": \"foo\","
  2586. " \"code\": 110,"
  2587. " \"type\": \"uint32\","
  2588. " \"array\": False,"
  2589. " \"record-types\": \"\","
  2590. " \"space\": \"vendor-opts-space\","
  2591. " \"encapsulate\": \"\""
  2592. " },"
  2593. " {"
  2594. " \"name\": \"foo2\","
  2595. " \"code\": 111,"
  2596. " \"type\": \"ipv4-address\","
  2597. " \"array\": False,"
  2598. " \"record-types\": \"\","
  2599. " \"space\": \"vendor-opts-space\","
  2600. " \"encapsulate\": \"\""
  2601. " } ]"
  2602. "}");
  2603. return (config);
  2604. }
  2605. // Convenience function for creating hooks library configuration with one or
  2606. // two character string constants.
  2607. std::string
  2608. buildHooksLibrariesConfig(const char* library1 = NULL,
  2609. const char* library2 = NULL) {
  2610. std::vector<std::string> libraries;
  2611. if (library1 != NULL) {
  2612. libraries.push_back(string(library1));
  2613. if (library2 != NULL) {
  2614. libraries.push_back(string(library2));
  2615. }
  2616. }
  2617. return (buildHooksLibrariesConfig(libraries));
  2618. }
  2619. // The goal of this test is to verify the configuration of hooks libraries if
  2620. // none are specified.
  2621. TEST_F(Dhcp6ParserTest, NoHooksLibraries) {
  2622. // Parse a configuration containing no names.
  2623. string config = buildHooksLibrariesConfig();
  2624. if (!executeConfiguration(config,
  2625. "set configuration with no hooks libraries")) {
  2626. FAIL() << "Unable to execute configuration";
  2627. } else {
  2628. // No libraries should be loaded at the end of the test.
  2629. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2630. EXPECT_TRUE(libraries.empty());
  2631. }
  2632. }
  2633. // Verify parsing fails with one library that will fail validation.
  2634. TEST_F(Dhcp6ParserTest, InvalidLibrary) {
  2635. // Parse a configuration containing a failing library.
  2636. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  2637. ConstElementPtr status;
  2638. ElementPtr json = Element::fromJSON(config);
  2639. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2640. // The status object must not be NULL
  2641. ASSERT_TRUE(status);
  2642. // Returned value should not be 0
  2643. comment_ = parseAnswer(rcode_, status);
  2644. EXPECT_NE(0, rcode_);
  2645. }
  2646. // Verify the configuration of hooks libraries with two being specified.
  2647. TEST_F(Dhcp6ParserTest, LibrariesSpecified) {
  2648. // Marker files should not be present.
  2649. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  2650. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2651. // Set up the configuration with two libraries and load them.
  2652. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  2653. CALLOUT_LIBRARY_2);
  2654. ASSERT_TRUE(executeConfiguration(config,
  2655. "load two valid libraries"));
  2656. // Expect two libraries to be loaded in the correct order (load marker file
  2657. // is present, no unload marker file).
  2658. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2659. ASSERT_EQ(2, libraries.size());
  2660. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2661. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2662. CfgMgr::instance().commit();
  2663. // Unload the libraries. The load file should not have changed, but
  2664. // the unload one should indicate the unload() functions have been run.
  2665. config = buildHooksLibrariesConfig();
  2666. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  2667. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2668. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  2669. // Expect the hooks system to say that none are loaded.
  2670. libraries = HooksManager::getLibraryNames();
  2671. EXPECT_TRUE(libraries.empty());
  2672. }
  2673. // This test verifies that it is possible to select subset of interfaces on
  2674. // which server should listen.
  2675. TEST_F(Dhcp6ParserTest, selectedInterfaces) {
  2676. IfaceMgrTestConfig test_config(true);
  2677. // Make sure there is no garbage interface configuration in the CfgMgr.
  2678. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2679. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2680. ConstElementPtr status;
  2681. string config = "{ \"interfaces\": [ \"eth0\" ],"
  2682. "\"preferred-lifetime\": 3000,"
  2683. "\"rebind-timer\": 2000, "
  2684. "\"renew-timer\": 1000, "
  2685. "\"valid-lifetime\": 4000 }";
  2686. ElementPtr json = Element::fromJSON(config);
  2687. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2688. // returned value must be 1 (values error)
  2689. // as the pool does not belong to that subnet
  2690. checkResult(status, 0);
  2691. CfgMgr::instance().getStagingCfg()->
  2692. getCfgIface().openSockets(AF_INET6, 10000);
  2693. // eth0 and eth1 were explicitly selected. eth2 was not.
  2694. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2695. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2696. }
  2697. // This test verifies that it is possible to configure the server to listen on
  2698. // all interfaces.
  2699. TEST_F(Dhcp6ParserTest, allInterfaces) {
  2700. IfaceMgrTestConfig test_config(true);
  2701. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2702. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2703. ConstElementPtr status;
  2704. // This configuration specifies two interfaces on which server should listen
  2705. // but also includes '*'. This keyword switches server into the
  2706. // mode when it listens on all interfaces regardless of what interface names
  2707. // were specified in the "interfaces" parameter.
  2708. string config = "{ \"interfaces\": [ \"eth0\", \"eth1\", \"*\" ],"
  2709. "\"preferred-lifetime\": 3000,"
  2710. "\"rebind-timer\": 2000, "
  2711. "\"renew-timer\": 1000, "
  2712. "\"valid-lifetime\": 4000 }";
  2713. ElementPtr json = Element::fromJSON(config);
  2714. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2715. checkResult(status, 0);
  2716. CfgMgr::instance().getStagingCfg()->
  2717. getCfgIface().openSockets(AF_INET6, 10000);
  2718. // All interfaces should be now active.
  2719. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2720. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET6));
  2721. }
  2722. // This test checks if it is possible to specify relay information
  2723. TEST_F(Dhcp6ParserTest, subnetRelayInfo) {
  2724. ConstElementPtr status;
  2725. // A config with relay information.
  2726. string config = "{ \"interfaces\": [ \"*\" ],"
  2727. "\"rebind-timer\": 2000, "
  2728. "\"renew-timer\": 1000, "
  2729. "\"subnet6\": [ { "
  2730. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  2731. " \"relay\": { "
  2732. " \"ip-address\": \"2001:db8:1::abcd\""
  2733. " },"
  2734. " \"subnet\": \"2001:db8:1::/64\" } ],"
  2735. "\"preferred-lifetime\": 3000, "
  2736. "\"valid-lifetime\": 4000 }";
  2737. ElementPtr json = Element::fromJSON(config);
  2738. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2739. // returned value should be 0 (configuration success)
  2740. checkResult(status, 0);
  2741. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2742. selectSubnet(IOAddress("2001:db8:1::1"), classify_);
  2743. ASSERT_TRUE(subnet);
  2744. EXPECT_EQ("2001:db8:1::abcd", subnet->getRelayInfo().addr_.toText());
  2745. }
  2746. // Goal of this test is to verify that multiple subnets can be configured
  2747. // with defined client classes.
  2748. TEST_F(Dhcp6ParserTest, classifySubnets) {
  2749. ConstElementPtr x;
  2750. string config = "{ \"interfaces\": [ \"*\" ],"
  2751. "\"preferred-lifetime\": 3000,"
  2752. "\"rebind-timer\": 2000, "
  2753. "\"renew-timer\": 1000, "
  2754. "\"subnet6\": [ { "
  2755. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2756. " \"subnet\": \"2001:db8:1::/64\", "
  2757. " \"client-class\": \"alpha\" "
  2758. " },"
  2759. " {"
  2760. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2761. " \"subnet\": \"2001:db8:2::/64\", "
  2762. " \"client-class\": \"beta\" "
  2763. " },"
  2764. " {"
  2765. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  2766. " \"subnet\": \"2001:db8:3::/64\", "
  2767. " \"client-class\": \"gamma\" "
  2768. " },"
  2769. " {"
  2770. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  2771. " \"subnet\": \"2001:db8:4::/64\" "
  2772. " } ],"
  2773. "\"valid-lifetime\": 4000 }";
  2774. ElementPtr json = Element::fromJSON(config);
  2775. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2776. checkResult(x, 0);
  2777. const Subnet6Collection* subnets =
  2778. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  2779. ASSERT_TRUE(subnets);
  2780. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  2781. // Let's check if client belonging to alpha class is supported in subnet[0]
  2782. // and not supported in any other subnet (except subnet[3], which allows
  2783. // everyone).
  2784. ClientClasses classes;
  2785. classes.insert("alpha");
  2786. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  2787. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2788. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2789. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2790. // Let's check if client belonging to beta class is supported in subnet[1]
  2791. // and not supported in any other subnet (except subnet[3], which allows
  2792. // everyone).
  2793. classes.clear();
  2794. classes.insert("beta");
  2795. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2796. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  2797. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2798. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2799. // Let's check if client belonging to gamma class is supported in subnet[2]
  2800. // and not supported in any other subnet (except subnet[3], which allows
  2801. // everyone).
  2802. classes.clear();
  2803. classes.insert("gamma");
  2804. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2805. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2806. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  2807. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2808. // Let's check if client belonging to some other class (not mentioned in
  2809. // the config) is supported only in subnet[3], which allows everyone.
  2810. classes.clear();
  2811. classes.insert("delta");
  2812. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2813. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2814. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2815. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2816. // Finally, let's check class-less client. He should be allowed only in
  2817. // the last subnet, which does not have any class restrictions.
  2818. classes.clear();
  2819. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2820. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2821. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2822. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2823. }
  2824. // This test checks the ability of the server to parse a configuration
  2825. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  2826. TEST_F(Dhcp6ParserTest, d2ClientConfig) {
  2827. ConstElementPtr status;
  2828. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2829. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2830. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2831. // Verify that the convenience method agrees.
  2832. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2833. string config_str = "{ \"interfaces\": [ \"*\" ],"
  2834. "\"preferred-lifetime\": 3000,"
  2835. "\"valid-lifetime\": 4000,"
  2836. "\"rebind-timer\": 2000, "
  2837. "\"renew-timer\": 1000, "
  2838. "\"subnet6\": [ { "
  2839. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2840. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2841. " \"dhcp-ddns\" : {"
  2842. " \"enable-updates\" : true, "
  2843. " \"server-ip\" : \"3001::1\", "
  2844. " \"server-port\" : 777, "
  2845. " \"sender-ip\" : \"3001::2\", "
  2846. " \"sender-port\" : 778, "
  2847. " \"max-queue-size\" : 2048, "
  2848. " \"ncr-protocol\" : \"UDP\", "
  2849. " \"ncr-format\" : \"JSON\", "
  2850. " \"always-include-fqdn\" : true, "
  2851. " \"allow-client-update\" : true, "
  2852. " \"override-no-update\" : true, "
  2853. " \"override-client-update\" : true, "
  2854. " \"replace-client-name\" : true, "
  2855. " \"generated-prefix\" : \"test.prefix\", "
  2856. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2857. "\"valid-lifetime\": 4000 }";
  2858. // Convert the JSON string to configuration elements.
  2859. ElementPtr config;
  2860. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2861. // Pass the configuration in for parsing.
  2862. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2863. // check if returned status is OK
  2864. checkResult(status, 0);
  2865. // Verify that DHCP-DDNS updating is enabled.
  2866. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  2867. // Verify that the D2 configuration can be retrieved.
  2868. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2869. ASSERT_TRUE(d2_client_config);
  2870. // Verify that the configuration values are correct.
  2871. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  2872. EXPECT_EQ("3001::1", d2_client_config->getServerIp().toText());
  2873. EXPECT_EQ(777, d2_client_config->getServerPort());
  2874. EXPECT_EQ("3001::2", d2_client_config->getSenderIp().toText());
  2875. EXPECT_EQ(778, d2_client_config->getSenderPort());
  2876. EXPECT_EQ(2048, d2_client_config->getMaxQueueSize());
  2877. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  2878. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  2879. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  2880. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  2881. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  2882. EXPECT_TRUE(d2_client_config->getReplaceClientName());
  2883. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  2884. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  2885. }
  2886. // This test checks the ability of the server to handle a configuration
  2887. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  2888. TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
  2889. ConstElementPtr status;
  2890. // Configuration string with an invalid D2 client config,
  2891. // "server-ip" is invalid.
  2892. string config_str = "{ \"interfaces\": [ \"*\" ],"
  2893. "\"rebind-timer\": 2000, "
  2894. "\"renew-timer\": 1000, "
  2895. "\"subnet6\": [ { "
  2896. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2897. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2898. " \"dhcp-ddns\" : {"
  2899. " \"enable-updates\" : true, "
  2900. " \"server-ip\" : \"bogus-value\", "
  2901. " \"server-port\" : 5301, "
  2902. " \"ncr-protocol\" : \"UDP\", "
  2903. " \"ncr-format\" : \"JSON\", "
  2904. " \"always-include-fqdn\" : true, "
  2905. " \"allow-client-update\" : true, "
  2906. " \"override-no-update\" : true, "
  2907. " \"override-client-update\" : true, "
  2908. " \"replace-client-name\" : true, "
  2909. " \"generated-prefix\" : \"test.prefix\", "
  2910. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2911. "\"valid-lifetime\": 4000 }";
  2912. // Convert the JSON string to configuration elements.
  2913. ElementPtr config;
  2914. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2915. // Configuration should not throw, but should fail.
  2916. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2917. // check if returned status is failed.
  2918. checkResult(status, 1);
  2919. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2920. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2921. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2922. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2923. // Verify that the convenience method agrees.
  2924. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2925. }
  2926. /// @brief Checks if the reservation is in the range of reservations.
  2927. ///
  2928. /// @param resrv Reservation to be searched for.
  2929. /// @param range Range of reservations returned by the @c Host object
  2930. /// in which the reservation will be searched.
  2931. bool reservationExists(const IPv6Resrv& resrv, const IPv6ResrvRange& range) {
  2932. for (IPv6ResrvIterator it = range.first; it != range.second;
  2933. ++it) {
  2934. if (resrv == it->second) {
  2935. return (true);
  2936. }
  2937. }
  2938. return (false);
  2939. }
  2940. // This test verifies that the host reservations can be specified for
  2941. // respective IPv6 subnets.
  2942. TEST_F(Dhcp6ParserTest, reservations) {
  2943. ConstElementPtr x;
  2944. string config = "{ \"interfaces\": [ \"*\" ],"
  2945. "\"rebind-timer\": 2000, "
  2946. "\"renew-timer\": 1000, "
  2947. "\"subnet6\": [ "
  2948. " { "
  2949. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2950. " \"subnet\": \"2001:db8:1::/64\", "
  2951. " \"id\": 123,"
  2952. " \"reservations\": ["
  2953. " ]"
  2954. " },"
  2955. " {"
  2956. " \"reservations\": ["
  2957. " {"
  2958. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  2959. " \"ip-addresses\": [ \"2001:db8:2::1234\" ],"
  2960. " \"hostname\": \"\""
  2961. " },"
  2962. " {"
  2963. " \"hw-address\": \"01:02:03:04:05:06\","
  2964. " \"ip-addresses\": [ \"2001:db8:2::abcd\" ],"
  2965. " \"hostname\": \"\""
  2966. " }"
  2967. " ],"
  2968. " \"pools\": [ ],"
  2969. " \"subnet\": \"2001:db8:2::/64\", "
  2970. " \"id\": 234"
  2971. " },"
  2972. " {"
  2973. " \"pools\": [ ],"
  2974. " \"subnet\": \"2001:db8:3::/64\", "
  2975. " \"id\": 542,"
  2976. " \"reservations\": ["
  2977. " {"
  2978. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  2979. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  2980. " \"hostname\": \"\""
  2981. " },"
  2982. " {"
  2983. " \"hw-address\": \"06:05:04:03:02:01\","
  2984. " \"prefixes\": [ \"2001:db8:3:1::/96\" ],"
  2985. " \"hostname\": \"\""
  2986. " }"
  2987. " ]"
  2988. " } "
  2989. "], "
  2990. "\"preferred-lifetime\": 3000,"
  2991. "\"valid-lifetime\": 4000 }";
  2992. ElementPtr json = Element::fromJSON(config);
  2993. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2994. checkResult(x, 0);
  2995. // Make sure all subnets have been successfully configured. There is no
  2996. // need to sanity check the subnet properties because it should have
  2997. // been already tested by other tests.
  2998. const Subnet6Collection* subnets =
  2999. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  3000. ASSERT_TRUE(subnets);
  3001. ASSERT_EQ(3, subnets->size());
  3002. // Hosts configuration must be available.
  3003. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3004. ASSERT_TRUE(hosts_cfg);
  3005. // Let's create an object holding hardware address of the host having
  3006. // a reservation in the subnet having id of 234. For simlicity the
  3007. // address is a collection of numbers from 1 to 6.
  3008. std::vector<uint8_t> hwaddr_vec;
  3009. for (int i = 1; i < 7; ++i) {
  3010. hwaddr_vec.push_back(static_cast<uint8_t>(i));
  3011. }
  3012. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  3013. // Retrieve the reservation and sanity check the address reserved.
  3014. ConstHostPtr host = hosts_cfg->get6(234, DuidPtr(), hwaddr);
  3015. ASSERT_TRUE(host);
  3016. IPv6ResrvRange resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3017. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3018. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3019. IOAddress("2001:db8:2::abcd")),
  3020. resrv));
  3021. // This reservation should be solely assigned to the subnet 234,
  3022. // and not to other two.
  3023. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3024. EXPECT_FALSE(hosts_cfg->get6(542, DuidPtr(), hwaddr));
  3025. // Do the same test for the DUID based reservation.
  3026. std::vector<uint8_t> duid_vec;
  3027. for (int i = 1; i < 0xb; ++i) {
  3028. duid_vec.push_back(static_cast<uint8_t>(i));
  3029. }
  3030. DuidPtr duid(new DUID(duid_vec));
  3031. host = hosts_cfg->get6(234, duid);
  3032. ASSERT_TRUE(host);
  3033. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3034. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3035. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3036. IOAddress("2001:db8:2::1234")),
  3037. resrv));
  3038. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3039. EXPECT_FALSE(hosts_cfg->get6(542, duid));
  3040. // The HW address used for one of the reservations in the subnet 542
  3041. // consists of numbers from 6 to 1. So, let's just reverse the order
  3042. // of the address from the previous test.
  3043. hwaddr->hwaddr_.assign(hwaddr_vec.rbegin(), hwaddr_vec.rend());
  3044. host = hosts_cfg->get6(542, DuidPtr(), hwaddr);
  3045. EXPECT_TRUE(host);
  3046. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3047. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3048. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3049. IOAddress("2001:db8:3:1::"),
  3050. 96), resrv));
  3051. // This reservation must not belong to other subnets.
  3052. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3053. EXPECT_FALSE(hosts_cfg->get6(234, DuidPtr(), hwaddr));
  3054. // Repeat the test for the DUID based reservation in this subnet.
  3055. duid.reset(new DUID(std::vector<uint8_t>(duid_vec.rbegin(),
  3056. duid_vec.rend())));
  3057. host = hosts_cfg->get6(542, duid);
  3058. ASSERT_TRUE(host);
  3059. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3060. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3061. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3062. IOAddress("2001:db8:3:2::"),
  3063. 96), resrv));
  3064. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3065. EXPECT_FALSE(hosts_cfg->get6(234, duid));
  3066. }
  3067. // This test verfies that the bogus host reservation would trigger a
  3068. // server configuration error.
  3069. TEST_F(Dhcp6ParserTest, reservationBogus) {
  3070. // Case 1: misspelled "duid" parameter.
  3071. ConstElementPtr x;
  3072. string config = "{ \"interfaces\": [ \"*\" ],"
  3073. "\"rebind-timer\": 2000, "
  3074. "\"renew-timer\": 1000, "
  3075. "\"subnet6\": [ "
  3076. " { "
  3077. " \"pools\": [ ],"
  3078. " \"subnet\": \"2001:db8:3::/64\", "
  3079. " \"id\": 542,"
  3080. " \"reservations\": ["
  3081. " {"
  3082. " \"dui\": \"0A:09:08:07:06:05:04:03:02:01\","
  3083. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3084. " \"hostname\": \"\""
  3085. " }"
  3086. " ]"
  3087. " } "
  3088. "], "
  3089. "\"preferred-lifetime\": 3000,"
  3090. "\"valid-lifetime\": 4000 }";
  3091. ElementPtr json = Element::fromJSON(config);
  3092. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3093. checkResult(x, 1);
  3094. // Case 2: DUID and HW Address both specified.
  3095. config = "{ \"interfaces\": [ \"*\" ],"
  3096. "\"rebind-timer\": 2000, "
  3097. "\"renew-timer\": 1000, "
  3098. "\"subnet6\": [ "
  3099. " { "
  3100. " \"pools\": [ ],"
  3101. " \"subnet\": \"2001:db8:3::/64\", "
  3102. " \"id\": 542,"
  3103. " \"reservations\": ["
  3104. " {"
  3105. " \"hw-address\": \"01:02:03:04:05:06\","
  3106. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3107. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3108. " \"hostname\": \"\""
  3109. " }"
  3110. " ]"
  3111. " } "
  3112. "], "
  3113. "\"preferred-lifetime\": 3000,"
  3114. "\"valid-lifetime\": 4000 }";
  3115. json = Element::fromJSON(config);
  3116. // Remove existing configuration, if any.
  3117. CfgMgr::instance().clear();
  3118. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3119. checkResult(x, 1);
  3120. // Case 3: Neither ip address nor hostname specified.
  3121. config = "{ \"interfaces\": [ \"*\" ],"
  3122. "\"rebind-timer\": 2000, "
  3123. "\"renew-timer\": 1000, "
  3124. "\"subnet6\": [ "
  3125. " { "
  3126. " \"pools\": [ ],"
  3127. " \"subnet\": \"2001:db8:3::/64\", "
  3128. " \"id\": 542,"
  3129. " \"reservations\": ["
  3130. " {"
  3131. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\""
  3132. " }"
  3133. " ]"
  3134. " } "
  3135. "], "
  3136. "\"preferred-lifetime\": 3000,"
  3137. "\"valid-lifetime\": 4000 }";
  3138. json = Element::fromJSON(config);
  3139. // Remove existing configuration, if any.
  3140. CfgMgr::instance().clear();
  3141. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3142. checkResult(x, 1);
  3143. }
  3144. /// The goal of this test is to verify that configuration can include
  3145. /// MAC/Hardware sources. This test also checks if the aliases are
  3146. /// handled properly (rfc6939 = client-addr-relay, rfc4649 = remote-id,
  3147. /// rfc4580 = subscriber-id).
  3148. TEST_F(Dhcp6ParserTest, macSources) {
  3149. ConstElementPtr status;
  3150. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3151. Element::fromJSON("{ \"interfaces\": [ \"*\" ],"
  3152. "\"mac-sources\": [ \"rfc6939\", \"rfc4649\", \"rfc4580\","
  3153. "\"client-link-addr-option\", \"remote-id\", \"subscriber-id\"],"
  3154. "\"preferred-lifetime\": 3000,"
  3155. "\"rebind-timer\": 2000, "
  3156. "\"renew-timer\": 1000, "
  3157. "\"subnet6\": [ ], "
  3158. "\"valid-lifetime\": 4000 }")));
  3159. // returned value should be 0 (success)
  3160. checkResult(status, 0);
  3161. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3162. ASSERT_EQ(6, mac_sources.size());
  3163. // Let's check the aliases. They should be recognized to their base methods.
  3164. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[0]);
  3165. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[1]);
  3166. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[2]);
  3167. // Let's check if the actual methods are recognized properly.
  3168. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[3]);
  3169. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[4]);
  3170. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[5]);
  3171. }
  3172. /// The goal of this test is to verify that MAC sources configuration can be
  3173. /// empty.
  3174. TEST_F(Dhcp6ParserTest, macSourcesEmpty) {
  3175. ConstElementPtr status;
  3176. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3177. Element::fromJSON("{ \"interfaces\": [ \"*\" ],"
  3178. "\"mac-sources\": [ ],"
  3179. "\"preferred-lifetime\": 3000,"
  3180. "\"rebind-timer\": 2000, "
  3181. "\"renew-timer\": 1000, "
  3182. "\"subnet6\": [ ], "
  3183. "\"valid-lifetime\": 4000 }")));
  3184. // returned value should be 0 (success)
  3185. checkResult(status, 0);
  3186. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3187. EXPECT_EQ(0, mac_sources.size());
  3188. }
  3189. /// The goal of this test is to verify that MAC sources configuration can
  3190. /// only use valid parameters.
  3191. TEST_F(Dhcp6ParserTest, macSourcesBogus) {
  3192. ConstElementPtr status;
  3193. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3194. Element::fromJSON("{ \"interfaces\": [ \"*\" ],"
  3195. "\"mac-sources\": [ \"from-wire\" ],"
  3196. "\"preferred-lifetime\": 3000,"
  3197. "\"rebind-timer\": 2000, "
  3198. "\"renew-timer\": 1000, "
  3199. "\"subnet6\": [ ], "
  3200. "\"valid-lifetime\": 4000 }")));
  3201. // returned value should be 1 (failure)
  3202. checkResult(status, 1);
  3203. }
  3204. };