config_parser_unittest.cc 151 KB

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