config_parser_unittest.cc 146 KB

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