config_parser_unittest.cc 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <arpa/inet.h>
  8. #include <gtest/gtest.h>
  9. #include <cc/command_interpreter.h>
  10. #include <config/module_spec.h>
  11. #include <dhcp4/dhcp4_srv.h>
  12. #include <dhcp4/json_config_parser.h>
  13. #include <dhcp/option4_addrlst.h>
  14. #include <dhcp/option_custom.h>
  15. #include <dhcp/option_int.h>
  16. #include <dhcp/docsis3_option_defs.h>
  17. #include <dhcp/classify.h>
  18. #include <dhcp/tests/iface_mgr_test_config.h>
  19. #include <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/cfgmgr.h>
  21. #include <dhcpsrv/cfg_expiration.h>
  22. #include <dhcpsrv/cfg_hosts.h>
  23. #include <dhcpsrv/cfg_subnets4.h>
  24. #include <dhcpsrv/testutils/config_result_check.h>
  25. #include <hooks/hooks_manager.h>
  26. #include "marker_file.h"
  27. #include "test_libraries.h"
  28. #include "test_data_files_config.h"
  29. #include "dhcp4_test_utils.h"
  30. #include "get_config_unittest.h"
  31. #include <boost/foreach.hpp>
  32. #include <boost/scoped_ptr.hpp>
  33. #include <iostream>
  34. #include <fstream>
  35. #include <sstream>
  36. #include <limits.h>
  37. using namespace isc;
  38. using namespace isc::asiolink;
  39. using namespace isc::config;
  40. using namespace isc::data;
  41. using namespace isc::dhcp;
  42. using namespace isc::dhcp::test;
  43. using namespace isc::hooks;
  44. using namespace std;
  45. namespace {
  46. const char* PARSER_CONFIGS[] = {
  47. // CONFIGURATION 0: one subnet with one pool, no user contexts
  48. "{"
  49. " \"interfaces-config\": {"
  50. " \"interfaces\": [\"*\" ]"
  51. " },"
  52. " \"valid-lifetime\": 4000,"
  53. " \"rebind-timer\": 2000,"
  54. " \"renew-timer\": 1000,"
  55. " \"subnet4\": [ {"
  56. " \"pools\": [ "
  57. " { \"pool\": \"192.0.2.0/28\" }"
  58. " ],"
  59. " \"subnet\": \"192.0.2.0/24\""
  60. " } ]"
  61. "}",
  62. // Configuration 1: one pool with empty user context
  63. "{"
  64. " \"interfaces-config\": {"
  65. " \"interfaces\": [\"*\" ]"
  66. " },"
  67. " \"valid-lifetime\": 4000,"
  68. " \"rebind-timer\": 2000,"
  69. " \"renew-timer\": 1000,"
  70. " \"subnet4\": [ {"
  71. " \"pools\": [ "
  72. " { \"pool\": \"192.0.2.0/28\","
  73. " \"user-context\": {"
  74. " }"
  75. " }"
  76. " ],"
  77. " \"subnet\": \"192.0.2.0/24\""
  78. " } ]"
  79. "}",
  80. // Configuration 2: one pool with user context containing lw4over6 parameters
  81. "{"
  82. " \"interfaces-config\": {"
  83. " \"interfaces\": [\"*\" ]"
  84. " },"
  85. " \"valid-lifetime\": 4000,"
  86. " \"rebind-timer\": 2000,"
  87. " \"renew-timer\": 1000,"
  88. " \"subnet4\": [ {"
  89. " \"pools\": [ "
  90. " { \"pool\": \"192.0.2.0/28\","
  91. " \"user-context\": {"
  92. " \"integer-param\": 42,"
  93. " \"string-param\": \"Sagittarius\","
  94. " \"bool-param\": true"
  95. " }"
  96. " }"
  97. " ],"
  98. " \"subnet\": \"192.0.2.0/24\""
  99. " } ]"
  100. "}",
  101. // Configuration 3: one min-max pool with user context containing lw4over6 parameters
  102. "{"
  103. " \"interfaces-config\": {"
  104. " \"interfaces\": [\"*\" ]"
  105. " },"
  106. " \"valid-lifetime\": 4000,"
  107. " \"rebind-timer\": 2000,"
  108. " \"renew-timer\": 1000,"
  109. " \"subnet4\": [ {"
  110. " \"pools\": [ "
  111. " { \"pool\": \"192.0.2.0 - 192.0.2.15\","
  112. " \"user-context\": {"
  113. " \"integer-param\": 42,"
  114. " \"string-param\": \"Sagittarius\","
  115. " \"bool-param\": true"
  116. " }"
  117. " }"
  118. " ],"
  119. " \"subnet\": \"192.0.2.0/24\""
  120. " } ]"
  121. "}"
  122. };
  123. /// @brief Prepends the given name with the DHCP4 source directory
  124. ///
  125. /// @param name file name of the desired file
  126. /// @return string containing the absolute path of the file in the DHCP source
  127. /// directory.
  128. std::string specfile(const std::string& name) {
  129. return (std::string(DHCP4_SRC_DIR) + "/" + name);
  130. }
  131. /// @brief Tests that the spec file is valid.
  132. /// Verifies that the Kea DHCPv4 configuration specification file is valid.
  133. TEST(Dhcp4SpecTest, basicSpec) {
  134. (isc::config::moduleSpecFromFile(specfile("dhcp4.spec")));
  135. ASSERT_NO_THROW(isc::config::moduleSpecFromFile(specfile("dhcp4.spec")));
  136. }
  137. class Dhcp4ParserTest : public ::testing::Test {
  138. protected:
  139. // Check that no hooks libraries are loaded. This is a pre-condition for
  140. // a number of tests, so is checked in one place. As this uses an
  141. // ASSERT call - and it is not clear from the documentation that Gtest
  142. // predicates can be used in a constructor - the check is placed in SetUp.
  143. virtual void SetUp() {
  144. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  145. ASSERT_TRUE(libraries.empty());
  146. }
  147. public:
  148. Dhcp4ParserTest()
  149. : rcode_(-1) {
  150. // Open port 0 means to not do anything at all. We don't want to
  151. // deal with sockets here, just check if configuration handling
  152. // is sane.
  153. srv_.reset(new Dhcpv4Srv(0));
  154. // Create fresh context.
  155. resetConfiguration();
  156. }
  157. public:
  158. // Checks if the result of DHCP server configuration has
  159. // expected code (0 for success, other for failures).
  160. // Also stores result in rcode_ and comment_.
  161. void checkResult(ConstElementPtr status, int expected_code) {
  162. ASSERT_TRUE(status);
  163. comment_ = parseAnswer(rcode_, status);
  164. EXPECT_EQ(expected_code, rcode_);
  165. }
  166. ~Dhcp4ParserTest() {
  167. resetConfiguration();
  168. // ... and delete the hooks library marker files if present
  169. static_cast<void>(remove(LOAD_MARKER_FILE));
  170. static_cast<void>(remove(UNLOAD_MARKER_FILE));
  171. };
  172. /// @brief Returns an interface configuration used by the most of the
  173. /// unit tests.
  174. std::string genIfaceConfig() const {
  175. return ("\"interfaces-config\": {"
  176. " \"interfaces\": [ \"*\" ]"
  177. "}");
  178. }
  179. /// @brief Create the simple configuration with single option.
  180. ///
  181. /// This function allows to set one of the parameters that configure
  182. /// option value. These parameters are: "name", "code", "data",
  183. /// "csv-format" and "space".
  184. ///
  185. /// @param param_value string holding option parameter value to be
  186. /// injected into the configuration string.
  187. /// @param parameter name of the parameter to be configured with
  188. /// param value.
  189. /// @return configuration string containing custom values of parameters
  190. /// describing an option.
  191. std::string createConfigWithOption(const std::string& param_value,
  192. const std::string& parameter) {
  193. std::map<std::string, std::string> params;
  194. if (parameter == "name") {
  195. params["name"] = param_value;
  196. params["space"] = DHCP4_OPTION_SPACE;
  197. params["code"] = "56";
  198. params["data"] = "ABCDEF0105";
  199. params["csv-format"] = "false";
  200. } else if (parameter == "space") {
  201. params["name"] = "dhcp-message";
  202. params["space"] = param_value;
  203. params["code"] = "56";
  204. params["data"] = "ABCDEF0105";
  205. params["csv-format"] = "false";
  206. } else if (parameter == "code") {
  207. params["name"] = "dhcp-message";
  208. params["space"] = DHCP4_OPTION_SPACE;
  209. params["code"] = param_value;
  210. params["data"] = "ABCDEF0105";
  211. params["csv-format"] = "false";
  212. } else if (parameter == "data") {
  213. params["name"] = "dhcp-message";
  214. params["space"] = DHCP4_OPTION_SPACE;
  215. params["code"] = "56";
  216. params["data"] = param_value;
  217. params["csv-format"] = "false";
  218. } else if (parameter == "csv-format") {
  219. params["name"] = "dhcp-message";
  220. params["space"] = DHCP4_OPTION_SPACE;
  221. params["code"] = "56";
  222. params["data"] = "ABCDEF0105";
  223. params["csv-format"] = param_value;
  224. }
  225. return (createConfigWithOption(params));
  226. }
  227. /// @brief Create simple configuration with single option.
  228. ///
  229. /// This function creates a configuration for a single option with
  230. /// custom values for all parameters that describe the option.
  231. ///
  232. /// @params params map holding parameters and their values.
  233. /// @return configuration string containing custom values of parameters
  234. /// describing an option.
  235. std::string createConfigWithOption(const std::map<std::string, std::string>& params) {
  236. std::ostringstream stream;
  237. stream << "{ " << genIfaceConfig() << "," <<
  238. "\"rebind-timer\": 2000, "
  239. "\"renew-timer\": 1000, "
  240. "\"subnet4\": [ { "
  241. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  242. " \"subnet\": \"192.0.2.0/24\", "
  243. " \"option-data\": [ {";
  244. bool first = true;
  245. typedef std::pair<std::string, std::string> ParamPair;
  246. BOOST_FOREACH(ParamPair param, params) {
  247. if (!first) {
  248. stream << ", ";
  249. } else {
  250. // cppcheck-suppress unreadVariable
  251. first = false;
  252. }
  253. if (param.first == "name") {
  254. stream << "\"name\": \"" << param.second << "\"";
  255. } else if (param.first == "space") {
  256. stream << "\"space\": \"" << param.second << "\"";
  257. } else if (param.first == "code") {
  258. stream << "\"code\": " << param.second << "";
  259. } else if (param.first == "data") {
  260. stream << "\"data\": \"" << param.second << "\"";
  261. } else if (param.first == "csv-format") {
  262. stream << "\"csv-format\": " << param.second;
  263. }
  264. }
  265. stream <<
  266. " } ]"
  267. " } ],"
  268. "\"valid-lifetime\": 4000 }";
  269. return (stream.str());
  270. }
  271. /// @brief Returns an option from the subnet.
  272. ///
  273. /// This function returns an option from a subnet to which the
  274. /// specified subnet address belongs. The option is identified
  275. /// by its code.
  276. ///
  277. /// @param subnet_address Address which belongs to the subnet from
  278. /// which the option is to be returned.
  279. /// @param option_code Code of the option to be returned.
  280. /// @param expected_options_count Expected number of options in
  281. /// the particular subnet.
  282. ///
  283. /// @return Descriptor of the option. If the descriptor holds a
  284. /// NULL option pointer, it means that there was no such option
  285. /// in the subnet.
  286. OptionDescriptor
  287. getOptionFromSubnet(const IOAddress& subnet_address,
  288. const uint16_t option_code,
  289. const uint16_t expected_options_count = 1) {
  290. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  291. getCfgSubnets4()->selectSubnet(subnet_address);
  292. if (!subnet) {
  293. /// @todo replace toText() with the use of operator <<.
  294. ADD_FAILURE() << "A subnet for the specified address "
  295. << subnet_address.toText()
  296. << "does not exist in Config Manager";
  297. }
  298. OptionContainerPtr options =
  299. subnet->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  300. if (expected_options_count != options->size()) {
  301. ADD_FAILURE() << "The number of options in the subnet '"
  302. << subnet_address.toText() << "' is different "
  303. " than expected number of options '"
  304. << expected_options_count << "'";
  305. }
  306. // Get the search index. Index #1 is to search using option code.
  307. const OptionContainerTypeIndex& idx = options->get<1>();
  308. // Get the options for specified index. Expecting one option to be
  309. // returned but in theory we may have multiple options with the same
  310. // code so we get the range.
  311. std::pair<OptionContainerTypeIndex::const_iterator,
  312. OptionContainerTypeIndex::const_iterator> range =
  313. idx.equal_range(option_code);
  314. if (std::distance(range.first, range.second) > 1) {
  315. ADD_FAILURE() << "There is more than one option having the"
  316. " option code '" << option_code << "' in a subnet '"
  317. << subnet_address.toText() << "'. Expected "
  318. " at most one option";
  319. } else if (std::distance(range.first, range.second) == 0) {
  320. return (OptionDescriptor(OptionPtr(), false));
  321. }
  322. return (*range.first);
  323. }
  324. /// @brief Test invalid option parameter value.
  325. ///
  326. /// This test function constructs the simple configuration
  327. /// string and injects invalid option configuration into it.
  328. /// It expects that parser will fail with provided option code.
  329. ///
  330. /// @param param_value string holding invalid option parameter value
  331. /// to be injected into configuration string.
  332. /// @param parameter name of the parameter to be configured with
  333. /// param_value (can be any of "name", "code", "data")
  334. void testInvalidOptionParam(const std::string& param_value,
  335. const std::string& parameter) {
  336. ConstElementPtr x;
  337. std::string config = createConfigWithOption(param_value, parameter);
  338. ConstElementPtr json = parseDHCP4(config);
  339. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  340. checkResult(x, 1);
  341. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  342. }
  343. /// @brief Test invalid option parameter value.
  344. ///
  345. /// This test function constructs the simple configuration
  346. /// string and injects invalid option configuration into it.
  347. /// It expects that parser will fail with provided option code.
  348. ///
  349. /// @param params Map of parameters defining an option.
  350. void
  351. testInvalidOptionParam(const std::map<std::string, std::string>& params) {
  352. ConstElementPtr x;
  353. std::string config = createConfigWithOption(params);
  354. ConstElementPtr json = parseDHCP4(config);
  355. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  356. checkResult(x, 1);
  357. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  358. }
  359. /// @brief Test option against given code and data.
  360. ///
  361. /// @param option_desc option descriptor that carries the option to
  362. /// be tested.
  363. /// @param expected_code expected code of the option.
  364. /// @param expected_data expected data in the option.
  365. /// @param expected_data_len length of the reference data.
  366. /// @param extra_data if true extra data is allowed in an option
  367. /// after tested data.
  368. void testOption(const OptionDescriptor& option_desc,
  369. uint16_t expected_code, const uint8_t* expected_data,
  370. size_t expected_data_len,
  371. bool extra_data = false) {
  372. // Check if option descriptor contains valid option pointer.
  373. ASSERT_TRUE(option_desc.option_);
  374. // Verify option type.
  375. EXPECT_EQ(expected_code, option_desc.option_->getType());
  376. // We may have many different option types being created. Some of them
  377. // have dedicated classes derived from Option class. In such case if
  378. // we want to verify the option contents against expected_data we have
  379. // to prepare raw buffer with the contents of the option. The easiest
  380. // way is to call pack() which will prepare on-wire data.
  381. util::OutputBuffer buf(option_desc.option_->getData().size());
  382. option_desc.option_->pack(buf);
  383. if (extra_data) {
  384. // The length of the buffer must be at least equal to size of the
  385. // reference data but it can sometimes be greater than that. This is
  386. // because some options carry suboptions that increase the overall
  387. // length.
  388. ASSERT_GE(buf.getLength() - option_desc.option_->getHeaderLen(),
  389. expected_data_len);
  390. } else {
  391. ASSERT_EQ(buf.getLength() - option_desc.option_->getHeaderLen(),
  392. expected_data_len);
  393. }
  394. // Verify that the data is correct. Do not verify suboptions and a header.
  395. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  396. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option_->getHeaderLen(),
  397. expected_data_len));
  398. }
  399. /// @brief Test option configuration.
  400. ///
  401. /// This function creates a configuration for a specified option using
  402. /// a map of parameters specified as the argument. The map holds
  403. /// name/value pairs which identifies option's configuration parameters:
  404. /// - name
  405. /// - space
  406. /// - code
  407. /// - data
  408. /// - csv-format.
  409. /// This function applies a new server configuration and checks that the
  410. /// option being configured is inserted into CfgMgr. The raw contents of
  411. /// this option are compared with the binary data specified as expected
  412. /// data passed to this function.
  413. ///
  414. /// @param params Map of parameters defining an option.
  415. /// @param option_code Option code.
  416. /// @param expected_data Array containing binary data expected to be stored
  417. /// in the configured option.
  418. /// @param expected_data_len Length of the array holding reference data.
  419. void testConfiguration(const std::map<std::string, std::string>& params,
  420. const uint16_t option_code,
  421. const uint8_t* expected_data,
  422. const size_t expected_data_len) {
  423. std::string config = createConfigWithOption(params);
  424. ASSERT_TRUE(executeConfiguration(config, "parse option configuration"));
  425. // The subnet should now hold one option with the specified option code.
  426. OptionDescriptor desc =
  427. getOptionFromSubnet(IOAddress("192.0.2.24"), option_code);
  428. ASSERT_TRUE(desc.option_);
  429. testOption(desc, option_code, expected_data, expected_data_len);
  430. }
  431. /// @brief Parse and Execute configuration
  432. ///
  433. /// Parses a configuration and executes a configuration of the server.
  434. /// If the operation fails, the current test will register a failure.
  435. ///
  436. /// @param config Configuration to parse
  437. /// @param operation Operation being performed. In the case of an error,
  438. /// the error text will include the string "unable to <operation>.".
  439. ///
  440. /// @return true if the configuration succeeded, false if not. In the
  441. /// latter case, a failure will have been added to the current test.
  442. bool
  443. executeConfiguration(const std::string& config, const char* operation) {
  444. CfgMgr::instance().clear();
  445. ConstElementPtr json;
  446. ConstElementPtr status;
  447. try {
  448. json = parseJSON(config);
  449. status = configureDhcp4Server(*srv_, json);
  450. } catch (const std::exception& ex) {
  451. ADD_FAILURE() << "Unable to " << operation << ". "
  452. << "The following configuration was used: " << std::endl
  453. << config << std::endl
  454. << " and the following error message was returned:"
  455. << ex.what() << std::endl;
  456. return (false);
  457. }
  458. // The status object must not be NULL
  459. if (!status) {
  460. ADD_FAILURE() << "Unable to " << operation << ". "
  461. << "The configuration function returned a null pointer.";
  462. return (false);
  463. }
  464. // Store the answer if we need it.
  465. // Returned value should be 0 (configuration success)
  466. comment_ = parseAnswer(rcode_, status);
  467. if (rcode_ != 0) {
  468. string reason = "";
  469. if (comment_) {
  470. reason = string(" (") + comment_->stringValue() + string(")");
  471. }
  472. ADD_FAILURE() << "Unable to " << operation << ". "
  473. << "The configuration function returned error code "
  474. << rcode_ << reason;
  475. return (false);
  476. }
  477. return (true);
  478. }
  479. /// @brief Reset configuration database.
  480. ///
  481. /// This function resets configuration data base by
  482. /// removing all subnets and option-data. Reset must
  483. /// be performed after each test to make sure that
  484. /// contents of the database do not affect result of
  485. /// subsequent tests.
  486. void resetConfiguration() {
  487. string config = "{ " + genIfaceConfig() + "," +
  488. "\"hooks-libraries\": [ ], "
  489. "\"valid-lifetime\": 4000, "
  490. "\"subnet4\": [ ], "
  491. "\"dhcp-ddns\": { \"enable-updates\" : false }, "
  492. "\"option-def\": [ ], "
  493. "\"option-data\": [ ] }";
  494. static_cast<void>(executeConfiguration(config,
  495. "reset configuration database"));
  496. CfgMgr::instance().clear();
  497. }
  498. /// @brief Retrieve an option associated with a host.
  499. ///
  500. /// The option is retrieved from the "dhcp4" option space.
  501. ///
  502. /// @param host Reference to a host for which an option should be retrieved.
  503. /// @param option_code Option code.
  504. /// @tparam ReturnType Type of the pointer object returned.
  505. ///
  506. /// @return Pointer to an option or NULL pointer if not found.
  507. template<typename ReturnType>
  508. ReturnType
  509. retrieveOption(const Host& host, const uint16_t option_code) const {
  510. return (retrieveOption<ReturnType>(host, DHCP4_OPTION_SPACE, option_code));
  511. }
  512. /// @brief Retrieve an option associated with a host.
  513. ///
  514. /// @param host Reference to a host for which an option should be retrieved.
  515. /// @param space Option space from which option should be retrieved.
  516. /// @param option_code Option code.
  517. /// @tparam ReturnType Type of the pointer object returned.
  518. ///
  519. /// @return Pointer to an option or NULL pointer if not found.
  520. template<typename ReturnType>
  521. ReturnType
  522. retrieveOption(const Host& host, const std::string& space,
  523. const uint16_t option_code) const {
  524. ConstCfgOptionPtr cfg_option = host.getCfgOption4();
  525. if (cfg_option) {
  526. OptionDescriptor opt_desc = cfg_option->get(space, option_code);
  527. if (opt_desc.option_) {
  528. return (boost::dynamic_pointer_cast<
  529. typename ReturnType::element_type>(opt_desc.option_));
  530. }
  531. }
  532. return (ReturnType());
  533. }
  534. /// @brief This utility method attempts to configure using specified
  535. /// config and then returns requested pool from requested subnet
  536. ///
  537. /// @param config configuration to be applied
  538. /// @param subnet_index index of the subnet to be returned (0 - the first subnet)
  539. /// @param pool_index index of the pool within a subnet (0 - the first pool)
  540. /// @param pool [out] Pool pointer will be stored here (if found)
  541. void getPool(const std::string& config, size_t subnet_index,
  542. size_t pool_index, PoolPtr& pool) {
  543. ConstElementPtr status;
  544. ConstElementPtr json;
  545. EXPECT_NO_THROW(json = parseDHCP4(config, true));
  546. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  547. ASSERT_TRUE(status);
  548. checkResult(status, 0);
  549. ConstCfgSubnets4Ptr subnets4 = CfgMgr::instance().getStagingCfg()->getCfgSubnets4();
  550. ASSERT_TRUE(subnets4);
  551. const Subnet4Collection* subnets = subnets4->getAll();
  552. ASSERT_TRUE(subnets);
  553. ASSERT_GE(subnets->size(), subnet_index + 1);
  554. const PoolCollection pools = subnets->at(subnet_index)->getPools(Lease::TYPE_V4);
  555. ASSERT_GE(pools.size(), pool_index + 1);
  556. pool = pools.at(pool_index);
  557. EXPECT_TRUE(pool);
  558. }
  559. boost::scoped_ptr<Dhcpv4Srv> srv_; ///< DHCP4 server under test
  560. int rcode_; ///< Return code from element parsing
  561. ConstElementPtr comment_; ///< Reason for parse fail
  562. isc::dhcp::ClientClasses classify_; ///< used in client classification
  563. };
  564. /// The goal of this test is to verify that the code accepts only
  565. /// valid commands and malformed or unsupported parameters are rejected.
  566. TEST_F(Dhcp4ParserTest, bogusCommand) {
  567. ConstElementPtr x;
  568. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  569. parseJSON("{\"bogus\": 5}")));
  570. // returned value must be 1 (configuration parse error)
  571. checkResult(x, 1);
  572. // it should be refused by syntax too
  573. EXPECT_THROW(parseDHCP4("{\"bogus\": 5}"), Dhcp4ParseError);
  574. }
  575. /// The goal of this test is to verify empty interface-config is accepted.
  576. TEST_F(Dhcp4ParserTest, emptyInterfaceConfig) {
  577. ConstElementPtr json;
  578. EXPECT_NO_THROW(json = parseDHCP4("{ \"rebind-timer\": 2000, "
  579. "\"renew-timer\": 1000, "
  580. "\"valid-lifetime\": 4000 }"));
  581. ConstElementPtr status;
  582. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  583. // returned value should be 0 (success)
  584. checkResult(status, 0);
  585. }
  586. /// The goal of this test is to verify if wrongly defined subnet will
  587. /// be rejected. Properly defined subnet must include at least one
  588. /// pool definition.
  589. TEST_F(Dhcp4ParserTest, emptySubnet) {
  590. std::string config = "{ " + genIfaceConfig() + "," +
  591. "\"rebind-timer\": 2000, "
  592. "\"renew-timer\": 1000, "
  593. "\"subnet4\": [ ], "
  594. "\"valid-lifetime\": 4000 }";
  595. ConstElementPtr json;
  596. EXPECT_NO_THROW(json = parseDHCP4(config));
  597. extractConfig(config);
  598. ConstElementPtr status;
  599. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  600. // returned value should be 0 (success)
  601. checkResult(status, 0);
  602. }
  603. /// Check that the renew-timer doesn't have to be specified, in which case
  604. /// it is marked unspecified in the Subnet.
  605. TEST_F(Dhcp4ParserTest, unspecifiedRenewTimer) {
  606. string config = "{ " + genIfaceConfig() + "," +
  607. "\"rebind-timer\": 2000, "
  608. "\"subnet4\": [ { "
  609. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  610. " \"subnet\": \"192.0.2.0/24\" } ],"
  611. "\"valid-lifetime\": 4000 }";
  612. ConstElementPtr json;
  613. ASSERT_NO_THROW(json = parseDHCP4(config));
  614. extractConfig(config);
  615. ConstElementPtr status;
  616. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  617. // returned value should be 0 (success)
  618. checkResult(status, 0);
  619. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  620. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  621. ASSERT_TRUE(subnet);
  622. EXPECT_FALSE(subnet->getT1().unspecified());
  623. EXPECT_FALSE(subnet->getT2().unspecified());
  624. EXPECT_EQ(900, subnet->getT1()); // that's the default value
  625. EXPECT_EQ(2000, subnet->getT2());
  626. EXPECT_EQ(4000, subnet->getValid());
  627. // Check that subnet-id is 1
  628. EXPECT_EQ(1, subnet->getID());
  629. }
  630. /// Check that the rebind-timer doesn't have to be specified, in which case
  631. /// it is marked unspecified in the Subnet.
  632. TEST_F(Dhcp4ParserTest, unspecifiedRebindTimer) {
  633. string config = "{ " + genIfaceConfig() + "," +
  634. "\"renew-timer\": 1000, "
  635. "\"subnet4\": [ { "
  636. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  637. " \"subnet\": \"192.0.2.0/24\" } ],"
  638. "\"valid-lifetime\": 4000 }";
  639. ConstElementPtr json;
  640. ASSERT_NO_THROW(json = parseDHCP4(config));
  641. extractConfig(config);
  642. ConstElementPtr status;
  643. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  644. // returned value should be 0 (success)
  645. checkResult(status, 0);
  646. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  647. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  648. ASSERT_TRUE(subnet);
  649. EXPECT_FALSE(subnet->getT1().unspecified());
  650. EXPECT_EQ(1000, subnet->getT1());
  651. EXPECT_FALSE(subnet->getT2().unspecified());
  652. EXPECT_EQ(1800, subnet->getT2()); // that's the default value
  653. EXPECT_EQ(4000, subnet->getValid());
  654. // Check that subnet-id is 1
  655. EXPECT_EQ(1, subnet->getID());
  656. }
  657. /// The goal of this test is to verify if defined subnet uses global
  658. /// parameter timer definitions.
  659. TEST_F(Dhcp4ParserTest, subnetGlobalDefaults) {
  660. string config = "{ " + genIfaceConfig() + "," +
  661. "\"rebind-timer\": 2000, "
  662. "\"renew-timer\": 1000, "
  663. "\"subnet4\": [ { "
  664. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  665. " \"subnet\": \"192.0.2.0/24\" } ],"
  666. "\"valid-lifetime\": 4000 }";
  667. ConstElementPtr json;
  668. ASSERT_NO_THROW(json = parseDHCP4(config));
  669. extractConfig(config);
  670. ConstElementPtr status;
  671. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  672. // check if returned status is OK
  673. checkResult(status, 0);
  674. // Now check if the configuration was indeed handled and we have
  675. // expected pool configured.
  676. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  677. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  678. ASSERT_TRUE(subnet);
  679. EXPECT_EQ(1000, subnet->getT1());
  680. EXPECT_EQ(2000, subnet->getT2());
  681. EXPECT_EQ(4000, subnet->getValid());
  682. // Check that subnet-id is 1
  683. EXPECT_EQ(1, subnet->getID());
  684. }
  685. // Goal of this test is to verify that multiple subnets get unique
  686. // subnet-ids. Also, test checks that it's possible to do reconfiguration
  687. // multiple times.
  688. TEST_F(Dhcp4ParserTest, multipleSubnets) {
  689. ConstElementPtr x;
  690. // Collection of four subnets for which subnet ids should be
  691. // autogenerated - ids are unspecified or set to 0.
  692. string config = "{ " + genIfaceConfig() + "," +
  693. "\"rebind-timer\": 2000, "
  694. "\"renew-timer\": 1000, "
  695. "\"subnet4\": [ { "
  696. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  697. " \"subnet\": \"192.0.2.0/24\" "
  698. " },"
  699. " {"
  700. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  701. " \"subnet\": \"192.0.3.0/24\", "
  702. " \"id\": 0 "
  703. " },"
  704. " {"
  705. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  706. " \"subnet\": \"192.0.4.0/24\" "
  707. " },"
  708. " {"
  709. " \"pools\": [ { \"pool\": \"192.0.5.101 - 192.0.5.150\" } ],"
  710. " \"subnet\": \"192.0.5.0/24\" "
  711. " } ],"
  712. "\"valid-lifetime\": 4000 }";
  713. ConstElementPtr json;
  714. ASSERT_NO_THROW(json = parseDHCP4(config));
  715. extractConfig(config);
  716. int cnt = 0; // Number of reconfigurations
  717. do {
  718. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  719. checkResult(x, 0);
  720. CfgMgr::instance().commit();
  721. const Subnet4Collection* subnets =
  722. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  723. ASSERT_TRUE(subnets);
  724. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  725. // Check subnet-ids of each subnet (it should be monotonously increasing)
  726. EXPECT_EQ(1, subnets->at(0)->getID());
  727. EXPECT_EQ(2, subnets->at(1)->getID());
  728. EXPECT_EQ(3, subnets->at(2)->getID());
  729. EXPECT_EQ(4, subnets->at(3)->getID());
  730. // Repeat reconfiguration process 10 times and check that the subnet-id
  731. // is set to the same value. Technically, just two iterations would be
  732. // sufficient, but it's nice to have a test that exercises reconfiguration
  733. // a bit.
  734. } while (++cnt < 10);
  735. }
  736. // This test checks that it is possible to assign arbitrary ids for subnets.
  737. TEST_F(Dhcp4ParserTest, multipleSubnetsExplicitIDs) {
  738. ConstElementPtr x;
  739. // Four subnets with arbitrary subnet ids.
  740. string config = "{ " + genIfaceConfig() + "," +
  741. "\"rebind-timer\": 2000, "
  742. "\"renew-timer\": 1000, "
  743. "\"subnet4\": [ { "
  744. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  745. " \"subnet\": \"192.0.2.0/24\", "
  746. " \"id\": 1024 "
  747. " },"
  748. " {"
  749. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  750. " \"subnet\": \"192.0.3.0/24\", "
  751. " \"id\": 100 "
  752. " },"
  753. " {"
  754. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  755. " \"subnet\": \"192.0.4.0/24\", "
  756. " \"id\": 1 "
  757. " },"
  758. " {"
  759. " \"pools\": [ { \"pool\": \"192.0.5.101 - 192.0.5.150\" } ],"
  760. " \"subnet\": \"192.0.5.0/24\", "
  761. " \"id\": 34 "
  762. " } ],"
  763. "\"valid-lifetime\": 4000 }";
  764. ConstElementPtr json;
  765. ASSERT_NO_THROW(json = parseDHCP4(config));
  766. extractConfig(config);
  767. int cnt = 0; // Number of reconfigurations
  768. do {
  769. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  770. checkResult(x, 0);
  771. CfgMgr::instance().commit();
  772. const Subnet4Collection* subnets =
  773. CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
  774. ASSERT_TRUE(subnets);
  775. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  776. // Verify that subnet ids are as expected.
  777. EXPECT_EQ(1024, subnets->at(0)->getID());
  778. EXPECT_EQ(100, subnets->at(1)->getID());
  779. EXPECT_EQ(1, subnets->at(2)->getID());
  780. EXPECT_EQ(34, subnets->at(3)->getID());
  781. // Repeat reconfiguration process 10 times and check that the subnet-id
  782. // is set to the same value.
  783. } while (++cnt < 3);
  784. }
  785. // Check that the configuration with two subnets having the same id is rejected.
  786. TEST_F(Dhcp4ParserTest, multipleSubnetsOverlapingIDs) {
  787. ConstElementPtr x;
  788. // Four subnets, two of them having the same id.
  789. string config = "{ " + genIfaceConfig() + "," +
  790. "\"rebind-timer\": 2000, "
  791. "\"renew-timer\": 1000, "
  792. "\"subnet4\": [ { "
  793. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  794. " \"subnet\": \"192.0.2.0/24\", "
  795. " \"id\": 1024 "
  796. " },"
  797. " {"
  798. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  799. " \"subnet\": \"192.0.3.0/24\", "
  800. " \"id\": 100 "
  801. " },"
  802. " {"
  803. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  804. " \"subnet\": \"192.0.4.0/24\", "
  805. " \"id\": 1024 "
  806. " },"
  807. " {"
  808. " \"pools\": [ { \"pool\": \"192.0.5.101 - 192.0.5.150\" } ],"
  809. " \"subnet\": \"192.0.5.0/24\", "
  810. " \"id\": 34 "
  811. " } ],"
  812. "\"valid-lifetime\": 4000 }";
  813. ConstElementPtr json;
  814. ASSERT_NO_THROW(json = parseDHCP4(config));
  815. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  816. checkResult(x, 1);
  817. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  818. }
  819. // Goal of this test is to verify that a previously configured subnet can be
  820. // deleted in subsequent reconfiguration.
  821. TEST_F(Dhcp4ParserTest, reconfigureRemoveSubnet) {
  822. ConstElementPtr x;
  823. // All four subnets
  824. string config4 = "{ " + genIfaceConfig() + "," +
  825. "\"rebind-timer\": 2000, "
  826. "\"renew-timer\": 1000, "
  827. "\"subnet4\": [ { "
  828. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  829. " \"subnet\": \"192.0.2.0/24\", "
  830. " \"id\": 1 "
  831. " },"
  832. " {"
  833. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  834. " \"subnet\": \"192.0.3.0/24\", "
  835. " \"id\": 2 "
  836. " },"
  837. " {"
  838. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  839. " \"subnet\": \"192.0.4.0/24\", "
  840. " \"id\": 3 "
  841. " },"
  842. " {"
  843. " \"pools\": [ { \"pool\": \"192.0.5.101 - 192.0.5.150\" } ],"
  844. " \"subnet\": \"192.0.5.0/24\", "
  845. " \"id\": 4 "
  846. " } ],"
  847. "\"valid-lifetime\": 4000 }";
  848. // Three subnets (the last one removed)
  849. string config_first3 = "{ " + genIfaceConfig() + "," +
  850. "\"rebind-timer\": 2000, "
  851. "\"renew-timer\": 1000, "
  852. "\"subnet4\": [ { "
  853. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  854. " \"subnet\": \"192.0.2.0/24\", "
  855. " \"id\": 1 "
  856. " },"
  857. " {"
  858. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  859. " \"subnet\": \"192.0.3.0/24\", "
  860. " \"id\": 2 "
  861. " },"
  862. " {"
  863. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  864. " \"subnet\": \"192.0.4.0/24\", "
  865. " \"id\": 3 "
  866. " } ],"
  867. "\"valid-lifetime\": 4000 }";
  868. // Second subnet removed
  869. string config_second_removed = "{ " + genIfaceConfig() + "," +
  870. "\"rebind-timer\": 2000, "
  871. "\"renew-timer\": 1000, "
  872. "\"subnet4\": [ { "
  873. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  874. " \"subnet\": \"192.0.2.0/24\", "
  875. " \"id\": 1 "
  876. " },"
  877. " {"
  878. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  879. " \"subnet\": \"192.0.4.0/24\", "
  880. " \"id\": 3 "
  881. " },"
  882. " {"
  883. " \"pools\": [ { \"pool\": \"192.0.5.101 - 192.0.5.150\" } ],"
  884. " \"subnet\": \"192.0.5.0/24\", "
  885. " \"id\": 4 "
  886. " } ],"
  887. "\"valid-lifetime\": 4000 }";
  888. // CASE 1: Configure 4 subnets, then reconfigure and remove the
  889. // last one.
  890. ConstElementPtr json;
  891. ASSERT_NO_THROW(json = parseDHCP4(config4));
  892. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  893. checkResult(x, 0);
  894. const Subnet4Collection* subnets =
  895. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->getAll();
  896. ASSERT_TRUE(subnets);
  897. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  898. CfgMgr::instance().clear();
  899. // Do the reconfiguration (the last subnet is removed)
  900. ASSERT_NO_THROW(json = parseDHCP4(config_first3));
  901. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  902. checkResult(x, 0);
  903. subnets = CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->getAll();
  904. ASSERT_TRUE(subnets);
  905. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets now (4th is removed)
  906. // Check subnet-ids of each subnet (it should be monotonously increasing)
  907. EXPECT_EQ(1, subnets->at(0)->getID());
  908. EXPECT_EQ(2, subnets->at(1)->getID());
  909. EXPECT_EQ(3, subnets->at(2)->getID());
  910. CfgMgr::instance().clear();
  911. /// CASE 2: Configure 4 subnets, then reconfigure and remove one
  912. /// from in between (not first, not last)
  913. /// @todo: Uncomment subnet removal test as part of #3281.
  914. ASSERT_NO_THROW(json = parseDHCP4(config4));
  915. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  916. checkResult(x, 0);
  917. CfgMgr::instance().clear();
  918. // Do reconfiguration
  919. ASSERT_NO_THROW(json = parseDHCP4(config_second_removed));
  920. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  921. checkResult(x, 0);
  922. subnets = CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->getAll();
  923. ASSERT_TRUE(subnets);
  924. ASSERT_EQ(3, subnets->size()); // We expect 4 subnets
  925. EXPECT_EQ(1, subnets->at(0)->getID());
  926. // The second subnet (with subnet-id = 2) is no longer there
  927. EXPECT_EQ(3, subnets->at(1)->getID());
  928. EXPECT_EQ(4, subnets->at(2)->getID());
  929. }
  930. /// @todo: implement subnet removal test as part of #3281.
  931. // Checks if the next-server defined as global parameter is taken into
  932. // consideration.
  933. TEST_F(Dhcp4ParserTest, nextServerGlobal) {
  934. string config = "{ " + genIfaceConfig() + "," +
  935. "\"rebind-timer\": 2000, "
  936. "\"renew-timer\": 1000, "
  937. "\"next-server\": \"1.2.3.4\", "
  938. "\"subnet4\": [ { "
  939. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  940. " \"subnet\": \"192.0.2.0/24\" } ],"
  941. "\"valid-lifetime\": 4000 }";
  942. ConstElementPtr json;
  943. ASSERT_NO_THROW(json = parseDHCP4(config));
  944. extractConfig(config);
  945. ConstElementPtr status;
  946. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  947. // check if returned status is OK
  948. checkResult(status, 0);
  949. // Now check if the configuration was indeed handled and we have
  950. // expected pool configured.
  951. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  952. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  953. ASSERT_TRUE(subnet);
  954. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  955. }
  956. // Checks if the next-server defined as subnet parameter is taken into
  957. // consideration.
  958. TEST_F(Dhcp4ParserTest, nextServerSubnet) {
  959. string config = "{ " + genIfaceConfig() + "," +
  960. "\"rebind-timer\": 2000, "
  961. "\"renew-timer\": 1000, "
  962. "\"subnet4\": [ { "
  963. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  964. " \"next-server\": \"1.2.3.4\", "
  965. " \"subnet\": \"192.0.2.0/24\" } ],"
  966. "\"valid-lifetime\": 4000 }";
  967. ConstElementPtr json;
  968. ASSERT_NO_THROW(json = parseDHCP4(config));
  969. extractConfig(config);
  970. ConstElementPtr status;
  971. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  972. // check if returned status is OK
  973. checkResult(status, 0);
  974. // Now check if the configuration was indeed handled and we have
  975. // expected pool configured.
  976. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  977. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  978. ASSERT_TRUE(subnet);
  979. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  980. }
  981. // Test checks several negative scenarios for next-server configuration: bogus
  982. // address, IPv6 adddress and empty string.
  983. TEST_F(Dhcp4ParserTest, nextServerNegative) {
  984. IfaceMgrTestConfig test_config(true);
  985. // Config with junk instead of next-server address
  986. string config_bogus1 = "{ " + genIfaceConfig() + "," +
  987. "\"rebind-timer\": 2000, "
  988. "\"renew-timer\": 1000, "
  989. "\"subnet4\": [ { "
  990. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  991. " \"rebind-timer\": 2000, "
  992. " \"renew-timer\": 1000, "
  993. " \"next-server\": \"a.b.c.d\", "
  994. " \"subnet\": \"192.0.2.0/24\" } ],"
  995. "\"valid-lifetime\": 4000 }";
  996. // Config with IPv6 next server address
  997. string config_bogus2 = "{ " + genIfaceConfig() + "," +
  998. "\"rebind-timer\": 2000, "
  999. "\"renew-timer\": 1000, "
  1000. "\"subnet4\": [ { "
  1001. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1002. " \"rebind-timer\": 2000, "
  1003. " \"renew-timer\": 1000, "
  1004. " \"next-server\": \"2001:db8::1\", "
  1005. " \"subnet\": \"192.0.2.0/24\" } ],"
  1006. "\"valid-lifetime\": 4000 }";
  1007. // Config with empty next server address
  1008. string config_bogus3 = "{ " + genIfaceConfig() + "," +
  1009. "\"rebind-timer\": 2000, "
  1010. "\"renew-timer\": 1000, "
  1011. "\"subnet4\": [ { "
  1012. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1013. " \"rebind-timer\": 2000, "
  1014. " \"renew-timer\": 1000, "
  1015. " \"next-server\": \"\", "
  1016. " \"subnet\": \"192.0.2.0/24\" } ],"
  1017. "\"valid-lifetime\": 4000 }";
  1018. ConstElementPtr json1;
  1019. ASSERT_NO_THROW(json1 = parseDHCP4(config_bogus1));
  1020. ConstElementPtr json2;
  1021. ASSERT_NO_THROW(json2 = parseDHCP4(config_bogus2));
  1022. ConstElementPtr json3;
  1023. ASSERT_NO_THROW(json3 = parseDHCP4(config_bogus3));
  1024. // check if returned status is always a failure
  1025. ConstElementPtr status;
  1026. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json1));
  1027. checkResult(status, 1);
  1028. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1029. CfgMgr::instance().clear();
  1030. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json2));
  1031. checkResult(status, 1);
  1032. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1033. CfgMgr::instance().clear();
  1034. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json3));
  1035. checkResult(status, 0);
  1036. EXPECT_FALSE(errorContainsPosition(status, "<string>"));
  1037. }
  1038. // Checks if the next-server defined as global value is overridden by subnet
  1039. // specific value.
  1040. TEST_F(Dhcp4ParserTest, nextServerOverride) {
  1041. string config = "{ " + genIfaceConfig() + "," +
  1042. "\"rebind-timer\": 2000, "
  1043. "\"renew-timer\": 1000, "
  1044. "\"next-server\": \"192.0.0.1\", "
  1045. "\"subnet4\": [ { "
  1046. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1047. " \"next-server\": \"1.2.3.4\", "
  1048. " \"subnet\": \"192.0.2.0/24\" } ],"
  1049. "\"valid-lifetime\": 4000 }";
  1050. ConstElementPtr json;
  1051. ASSERT_NO_THROW(json = parseDHCP4(config));
  1052. extractConfig(config);
  1053. ConstElementPtr status;
  1054. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1055. // check if returned status is OK
  1056. checkResult(status, 0);
  1057. // Now check if the configuration was indeed handled and we have
  1058. // expected pool configured.
  1059. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  1060. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  1061. ASSERT_TRUE(subnet);
  1062. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  1063. }
  1064. // Check whether it is possible to configure echo-client-id
  1065. TEST_F(Dhcp4ParserTest, echoClientId) {
  1066. string config_false = "{ " + genIfaceConfig() + "," +
  1067. "\"rebind-timer\": 2000, "
  1068. "\"renew-timer\": 1000, "
  1069. "\"echo-client-id\": false,"
  1070. "\"subnet4\": [ { "
  1071. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1072. " \"subnet\": \"192.0.2.0/24\" } ],"
  1073. "\"valid-lifetime\": 4000 }";
  1074. string config_true = "{ " + genIfaceConfig() + "," +
  1075. "\"rebind-timer\": 2000, "
  1076. "\"renew-timer\": 1000, "
  1077. "\"echo-client-id\": true,"
  1078. "\"subnet4\": [ { "
  1079. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1080. " \"subnet\": \"192.0.2.0/24\" } ],"
  1081. "\"valid-lifetime\": 4000 }";
  1082. ConstElementPtr json_false;
  1083. ASSERT_NO_THROW(json_false = parseDHCP4(config_false));
  1084. extractConfig(config_false);
  1085. ConstElementPtr json_true;
  1086. ASSERT_NO_THROW(json_true = parseDHCP4(config_true));
  1087. extractConfig(config_true);
  1088. // Let's check the default. It should be true
  1089. ASSERT_TRUE(CfgMgr::instance().getStagingCfg()->getEchoClientId());
  1090. // Now check that "false" configuration is really applied.
  1091. ConstElementPtr status;
  1092. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json_false));
  1093. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->getEchoClientId());
  1094. CfgMgr::instance().clear();
  1095. // Now check that "true" configuration is really applied.
  1096. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json_true));
  1097. ASSERT_TRUE(CfgMgr::instance().getStagingCfg()->getEchoClientId());
  1098. // In any case revert back to the default value (true)
  1099. CfgMgr::instance().getStagingCfg()->setEchoClientId(true);
  1100. }
  1101. // This test checks that the global match-client-id parameter is optional
  1102. // and that values under the subnet are used.
  1103. TEST_F(Dhcp4ParserTest, matchClientIdNoGlobal) {
  1104. std::string config = "{ " + genIfaceConfig() + "," +
  1105. "\"rebind-timer\": 2000, "
  1106. "\"renew-timer\": 1000, "
  1107. "\"subnet4\": [ "
  1108. "{"
  1109. " \"match-client-id\": true,"
  1110. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1111. " \"subnet\": \"192.0.2.0/24\""
  1112. "},"
  1113. "{"
  1114. " \"match-client-id\": false,"
  1115. " \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1116. " \"subnet\": \"192.0.3.0/24\""
  1117. "} ],"
  1118. "\"valid-lifetime\": 4000 }";
  1119. ConstElementPtr json;
  1120. ASSERT_NO_THROW(json = parseDHCP4(config));
  1121. extractConfig(config);
  1122. ConstElementPtr status;
  1123. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1124. checkResult(status, 0);
  1125. CfgSubnets4Ptr cfg = CfgMgr::instance().getStagingCfg()->getCfgSubnets4();
  1126. Subnet4Ptr subnet1 = cfg->selectSubnet(IOAddress("192.0.2.1"));
  1127. ASSERT_TRUE(subnet1);
  1128. EXPECT_TRUE(subnet1->getMatchClientId());
  1129. Subnet4Ptr subnet2 = cfg->selectSubnet(IOAddress("192.0.3.1"));
  1130. ASSERT_TRUE(subnet2);
  1131. EXPECT_FALSE(subnet2->getMatchClientId());
  1132. }
  1133. // This test checks that the global match-client-id parameter is used
  1134. // when there is no such parameter under subnet and that the parameter
  1135. // specified for a subnet overrides the global setting.
  1136. TEST_F(Dhcp4ParserTest, matchClientIdGlobal) {
  1137. std::string config = "{ " + genIfaceConfig() + "," +
  1138. "\"rebind-timer\": 2000, "
  1139. "\"renew-timer\": 1000, "
  1140. "\"match-client-id\": true,"
  1141. "\"subnet4\": [ "
  1142. "{"
  1143. " \"match-client-id\": false,"
  1144. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1145. " \"subnet\": \"192.0.2.0/24\""
  1146. "},"
  1147. "{"
  1148. " \"pools\": [ { \"pool\": \"192.0.3.1 - 192.0.3.100\" } ],"
  1149. " \"subnet\": \"192.0.3.0/24\""
  1150. "} ],"
  1151. "\"valid-lifetime\": 4000 }";
  1152. ConstElementPtr json;
  1153. ASSERT_NO_THROW(json = parseDHCP4(config));
  1154. extractConfig(config);
  1155. ConstElementPtr status;
  1156. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1157. checkResult(status, 0);
  1158. CfgSubnets4Ptr cfg = CfgMgr::instance().getStagingCfg()->getCfgSubnets4();
  1159. Subnet4Ptr subnet1 = cfg->selectSubnet(IOAddress("192.0.2.1"));
  1160. ASSERT_TRUE(subnet1);
  1161. EXPECT_FALSE(subnet1->getMatchClientId());
  1162. Subnet4Ptr subnet2 = cfg->selectSubnet(IOAddress("192.0.3.1"));
  1163. ASSERT_TRUE(subnet2);
  1164. EXPECT_TRUE(subnet2->getMatchClientId());
  1165. }
  1166. // This test checks if it is possible to override global values
  1167. // on a per subnet basis.
  1168. TEST_F(Dhcp4ParserTest, subnetLocal) {
  1169. string config = "{ " + genIfaceConfig() + "," +
  1170. "\"rebind-timer\": 2000, "
  1171. "\"renew-timer\": 1000, "
  1172. "\"subnet4\": [ { "
  1173. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1174. " \"renew-timer\": 1, "
  1175. " \"rebind-timer\": 2, "
  1176. " \"valid-lifetime\": 4,"
  1177. " \"subnet\": \"192.0.2.0/24\" } ],"
  1178. "\"valid-lifetime\": 4000 }";
  1179. ConstElementPtr json;
  1180. ASSERT_NO_THROW(json = parseDHCP4(config));
  1181. extractConfig(config);
  1182. ConstElementPtr status;
  1183. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1184. // returned value should be 0 (configuration success)
  1185. checkResult(status, 0);
  1186. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  1187. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  1188. ASSERT_TRUE(subnet);
  1189. EXPECT_EQ(1, subnet->getT1());
  1190. EXPECT_EQ(2, subnet->getT2());
  1191. EXPECT_EQ(4, subnet->getValid());
  1192. }
  1193. // This test checks that multiple pools can be defined and handled properly.
  1194. // The test defines 2 subnets, each with 2 pools.
  1195. TEST_F(Dhcp4ParserTest, multiplePools) {
  1196. // Collection with two subnets, each with 2 pools.
  1197. string config = "{ " + genIfaceConfig() + "," +
  1198. "\"rebind-timer\": 2000, "
  1199. "\"renew-timer\": 1000, "
  1200. "\"subnet4\": [ { "
  1201. " \"pools\": [ "
  1202. " { \"pool\": \"192.0.2.0/28\" },"
  1203. " { \"pool\": \"192.0.2.200-192.0.2.255\" }"
  1204. " ],"
  1205. " \"subnet\": \"192.0.2.0/24\" "
  1206. " },"
  1207. " {"
  1208. " \"pools\": [ "
  1209. " { \"pool\": \"192.0.3.0/25\" },"
  1210. " { \"pool\": \"192.0.3.128/25\" }"
  1211. " ],"
  1212. " \"subnet\": \"192.0.3.0/24\""
  1213. " } ],"
  1214. "\"valid-lifetime\": 4000 }";
  1215. ConstElementPtr json;
  1216. ASSERT_NO_THROW(json = parseDHCP4(config));
  1217. extractConfig(config);
  1218. ConstElementPtr status;
  1219. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1220. checkResult(status, 0);
  1221. const Subnet4Collection* subnets =
  1222. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->getAll();
  1223. ASSERT_TRUE(subnets);
  1224. ASSERT_EQ(2, subnets->size()); // We expect 2 subnets
  1225. // Check the first subnet
  1226. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_V4);
  1227. ASSERT_EQ(2, pools1.size());
  1228. EXPECT_EQ("type=V4, 192.0.2.0-192.0.2.15",
  1229. pools1[0]->toText());
  1230. EXPECT_EQ("type=V4, 192.0.2.200-192.0.2.255",
  1231. pools1[1]->toText());
  1232. // There shouldn't be any TA or PD pools
  1233. EXPECT_THROW(subnets->at(0)->getPools(Lease::TYPE_TA), BadValue);
  1234. EXPECT_THROW(subnets->at(0)->getPools(Lease::TYPE_PD), BadValue);
  1235. // Check the second subnet
  1236. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_V4);
  1237. ASSERT_EQ(2, pools2.size());
  1238. EXPECT_EQ("type=V4, 192.0.3.0-192.0.3.127",
  1239. pools2[0]->toText());
  1240. EXPECT_EQ("type=V4, 192.0.3.128-192.0.3.255",
  1241. pools2[1]->toText());
  1242. // There shouldn't be any TA or PD pools
  1243. EXPECT_THROW(subnets->at(0)->getPools(Lease::TYPE_TA).empty(), BadValue);
  1244. EXPECT_THROW(subnets->at(0)->getPools(Lease::TYPE_PD).empty(), BadValue);
  1245. }
  1246. // Test verifies that a subnet with pool values that do not belong to that
  1247. // pool are rejected.
  1248. TEST_F(Dhcp4ParserTest, poolOutOfSubnet) {
  1249. string config = "{ " + genIfaceConfig() + "," +
  1250. "\"rebind-timer\": 2000, "
  1251. "\"renew-timer\": 1000, "
  1252. "\"subnet4\": [ { "
  1253. " \"pools\": [ { \"pool\": \"192.0.4.0/28\" } ],"
  1254. " \"subnet\": \"192.0.2.0/24\" } ],"
  1255. "\"valid-lifetime\": 4000 }";
  1256. ConstElementPtr json;
  1257. ASSERT_NO_THROW(json = parseDHCP4(config));
  1258. ConstElementPtr status;
  1259. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1260. // returned value must be 1 (values error)
  1261. // as the pool does not belong to that subnet
  1262. checkResult(status, 1);
  1263. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1264. }
  1265. // Goal of this test is to verify if pools can be defined
  1266. // using prefix/length notation. There is no separate test for min-max
  1267. // notation as it was tested in several previous tests.
  1268. TEST_F(Dhcp4ParserTest, poolPrefixLen) {
  1269. string config = "{ " + genIfaceConfig() + "," +
  1270. "\"rebind-timer\": 2000, "
  1271. "\"renew-timer\": 1000, "
  1272. "\"subnet4\": [ { "
  1273. " \"pools\": [ { \"pool\": \"192.0.2.128/28\" } ],"
  1274. " \"subnet\": \"192.0.2.0/24\" } ],"
  1275. "\"valid-lifetime\": 4000 }";
  1276. ConstElementPtr json;
  1277. ASSERT_NO_THROW(json = parseDHCP4(config));
  1278. extractConfig(config);
  1279. ConstElementPtr status;
  1280. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1281. // returned value must be 0 (configuration accepted)
  1282. checkResult(status, 0);
  1283. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  1284. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  1285. ASSERT_TRUE(subnet);
  1286. EXPECT_EQ(1000, subnet->getT1());
  1287. EXPECT_EQ(2000, subnet->getT2());
  1288. EXPECT_EQ(4000, subnet->getValid());
  1289. }
  1290. // Goal of this test is to verify if invalid pool definitions
  1291. // return a location in the error message.
  1292. TEST_F(Dhcp4ParserTest, badPools) {
  1293. // not a prefix
  1294. string config_bogus1 = "{ " + genIfaceConfig() + "," +
  1295. "\"rebind-timer\": 2000, "
  1296. "\"renew-timer\": 1000, "
  1297. "\"subnet4\": [ { "
  1298. " \"pools\": [ { \"pool\": \"foo/28\" } ],"
  1299. " \"subnet\": \"192.0.2.0/24\" } ],"
  1300. "\"valid-lifetime\": 4000 }";
  1301. // not a length
  1302. string config_bogus2 = "{ " + genIfaceConfig() + "," +
  1303. "\"rebind-timer\": 2000, "
  1304. "\"renew-timer\": 1000, "
  1305. "\"subnet4\": [ { "
  1306. " \"pools\": [ { \"pool\": \"192.0.2.128/foo\" } ],"
  1307. " \"subnet\": \"192.0.2.0/24\" } ],"
  1308. "\"valid-lifetime\": 4000 }";
  1309. // invalid prefix length
  1310. string config_bogus3 = "{ " + genIfaceConfig() + "," +
  1311. "\"rebind-timer\": 2000, "
  1312. "\"renew-timer\": 1000, "
  1313. "\"subnet4\": [ { "
  1314. " \"pools\": [ { \"pool\": \"192.0.2.128/100\" } ],"
  1315. " \"subnet\": \"192.0.2.0/24\" } ],"
  1316. "\"valid-lifetime\": 4000 }";
  1317. // not a prefix nor a min-max
  1318. string config_bogus4 = "{ " + genIfaceConfig() + "," +
  1319. "\"rebind-timer\": 2000, "
  1320. "\"renew-timer\": 1000, "
  1321. "\"subnet4\": [ { "
  1322. " \"pools\": [ { \"pool\": \"foo\" } ],"
  1323. " \"subnet\": \"192.0.2.0/24\" } ],"
  1324. "\"valid-lifetime\": 4000 }";
  1325. // not an address
  1326. string config_bogus5 = "{ " + genIfaceConfig() + "," +
  1327. "\"rebind-timer\": 2000, "
  1328. "\"renew-timer\": 1000, "
  1329. "\"subnet4\": [ { "
  1330. " \"pools\": [ { \"pool\": \"foo - bar\" } ],"
  1331. " \"subnet\": \"192.0.2.0/24\" } ],"
  1332. "\"valid-lifetime\": 4000 }";
  1333. // min > max
  1334. string config_bogus6 = "{ " + genIfaceConfig() + "," +
  1335. "\"rebind-timer\": 2000, "
  1336. "\"renew-timer\": 1000, "
  1337. "\"subnet4\": [ { "
  1338. " \"pools\": [ { \"pool\": \"192.0.2.200 - 192.0.2.100\" } ],"
  1339. " \"subnet\": \"192.0.2.0/24\" } ],"
  1340. "\"valid-lifetime\": 4000 }";
  1341. // out of range prefix length (new check)
  1342. string config_bogus7 = "{ " + genIfaceConfig() + "," +
  1343. "\"rebind-timer\": 2000, "
  1344. "\"renew-timer\": 1000, "
  1345. "\"subnet4\": [ { "
  1346. " \"pools\": [ { \"pool\": \"192.0.2.128/1052\" } ],"
  1347. " \"subnet\": \"192.0.2.0/24\" } ],"
  1348. "\"valid-lifetime\": 4000 }";
  1349. ConstElementPtr json1;
  1350. ASSERT_NO_THROW(json1 = parseDHCP4(config_bogus1));
  1351. ConstElementPtr json2;
  1352. ASSERT_NO_THROW(json2 = parseDHCP4(config_bogus2));
  1353. ConstElementPtr json3;
  1354. ASSERT_NO_THROW(json3 = parseDHCP4(config_bogus3));
  1355. ConstElementPtr json4;
  1356. ASSERT_NO_THROW(json4 = parseDHCP4(config_bogus4));
  1357. ConstElementPtr json5;
  1358. ASSERT_NO_THROW(json5 = parseDHCP4(config_bogus5));
  1359. ConstElementPtr json6;
  1360. ASSERT_NO_THROW(json6 = parseDHCP4(config_bogus6));
  1361. ConstElementPtr json7;
  1362. ASSERT_NO_THROW(json7 = parseDHCP4(config_bogus7));
  1363. ConstElementPtr status;
  1364. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json1));
  1365. // check if returned status is always a failure
  1366. checkResult(status, 1);
  1367. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1368. CfgMgr::instance().clear();
  1369. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json2));
  1370. checkResult(status, 1);
  1371. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1372. CfgMgr::instance().clear();
  1373. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json3));
  1374. checkResult(status, 1);
  1375. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1376. CfgMgr::instance().clear();
  1377. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json4));
  1378. checkResult(status, 1);
  1379. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1380. CfgMgr::instance().clear();
  1381. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json5));
  1382. checkResult(status, 1);
  1383. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1384. CfgMgr::instance().clear();
  1385. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json6));
  1386. checkResult(status, 1);
  1387. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1388. CfgMgr::instance().clear();
  1389. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json7));
  1390. checkResult(status, 1);
  1391. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1392. }
  1393. // The goal of this test is to check whether an option definition
  1394. // that defines an option carrying an IPv4 address can be created.
  1395. TEST_F(Dhcp4ParserTest, optionDefIpv4Address) {
  1396. // Configuration string.
  1397. std::string config =
  1398. "{ \"option-def\": [ {"
  1399. " \"name\": \"foo\","
  1400. " \"code\": 100,"
  1401. " \"type\": \"ipv4-address\","
  1402. " \"space\": \"isc\""
  1403. " } ]"
  1404. "}";
  1405. ConstElementPtr json;
  1406. ASSERT_NO_THROW(json = parseOPTION_DEF(config, true));
  1407. extractConfig(config);
  1408. // Make sure that the particular option definition does not exist.
  1409. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1410. getCfgOptionDef()->get("isc", 100);
  1411. ASSERT_FALSE(def);
  1412. // Use the configuration string to create new option definition.
  1413. ConstElementPtr status;
  1414. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1415. ASSERT_TRUE(status);
  1416. checkResult(status, 0);
  1417. // We need to commit option definitions because later in this test we
  1418. // will be checking if they get removed when "option-def" parameter
  1419. // is removed from a configuration.
  1420. LibDHCP::commitRuntimeOptionDefs();
  1421. // The option definition should now be available in the CfgMgr.
  1422. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1423. ASSERT_TRUE(def);
  1424. // Verify that the option definition data is valid.
  1425. EXPECT_EQ("foo", def->getName());
  1426. EXPECT_EQ(100, def->getCode());
  1427. EXPECT_FALSE(def->getArrayType());
  1428. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  1429. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  1430. // The copy of the option definition should be available in the libdhcp++.
  1431. OptionDefinitionPtr def_libdhcp = LibDHCP::getRuntimeOptionDef("isc", 100);
  1432. ASSERT_TRUE(def_libdhcp);
  1433. // Both definitions should be held in distinct pointers but they should
  1434. // be equal.
  1435. EXPECT_TRUE(def_libdhcp != def);
  1436. EXPECT_TRUE(*def_libdhcp == *def);
  1437. // Let's apply empty configuration. This removes the option definitions
  1438. // configuration and should result in removal of the option 100 from the
  1439. // libdhcp++.
  1440. config = "{ }";
  1441. ASSERT_NO_THROW(json = parseOPTION_DEF(config, true));
  1442. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1443. checkResult(status, 0);
  1444. EXPECT_FALSE(LibDHCP::getRuntimeOptionDef("isc", 100));
  1445. }
  1446. // The goal of this test is to check whether an option definition
  1447. // that defines an option carrying a record of data fields can
  1448. // be created.
  1449. TEST_F(Dhcp4ParserTest, optionDefRecord) {
  1450. // Configuration string.
  1451. std::string config =
  1452. "{ \"option-def\": [ {"
  1453. " \"name\": \"foo\","
  1454. " \"code\": 100,"
  1455. " \"type\": \"record\","
  1456. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  1457. " \"space\": \"isc\""
  1458. " } ]"
  1459. "}";
  1460. ConstElementPtr json;
  1461. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1462. extractConfig(config);
  1463. // Make sure that the particular option definition does not exist.
  1464. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1465. getCfgOptionDef()->get("isc", 100);
  1466. ASSERT_FALSE(def);
  1467. // Use the configuration string to create new option definition.
  1468. ConstElementPtr status;
  1469. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1470. ASSERT_TRUE(status);
  1471. checkResult(status, 0);
  1472. // The option definition should now be available in the CfgMgr.
  1473. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1474. ASSERT_TRUE(def);
  1475. // Check the option data.
  1476. EXPECT_EQ("foo", def->getName());
  1477. EXPECT_EQ(100, def->getCode());
  1478. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  1479. EXPECT_FALSE(def->getArrayType());
  1480. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  1481. // The option comprises the record of data fields. Verify that all
  1482. // fields are present and they are of the expected types.
  1483. const OptionDefinition::RecordFieldsCollection& record_fields =
  1484. def->getRecordFields();
  1485. ASSERT_EQ(4, record_fields.size());
  1486. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  1487. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  1488. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  1489. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  1490. }
  1491. // The goal of this test is to verify that multiple option definitions
  1492. // can be created.
  1493. TEST_F(Dhcp4ParserTest, optionDefMultiple) {
  1494. // Configuration string.
  1495. std::string config =
  1496. "{ \"option-def\": [ {"
  1497. " \"name\": \"foo\","
  1498. " \"code\": 100,"
  1499. " \"type\": \"uint32\","
  1500. " \"space\": \"isc\""
  1501. " },"
  1502. " {"
  1503. " \"name\": \"foo-2\","
  1504. " \"code\": 101,"
  1505. " \"type\": \"ipv4-address\","
  1506. " \"space\": \"isc\""
  1507. " } ]"
  1508. "}";
  1509. ConstElementPtr json;
  1510. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1511. extractConfig(config);
  1512. // Make sure that the option definitions do not exist yet.
  1513. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1514. getCfgOptionDef()->get("isc", 100));
  1515. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1516. getCfgOptionDef()->get("isc", 101));
  1517. // Use the configuration string to create new option definitions.
  1518. ConstElementPtr status;
  1519. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1520. ASSERT_TRUE(status);
  1521. checkResult(status, 0);
  1522. // Check the first definition we have created.
  1523. OptionDefinitionPtr def1 = CfgMgr::instance().getStagingCfg()->
  1524. getCfgOptionDef()->get("isc", 100);
  1525. ASSERT_TRUE(def1);
  1526. // Check the option data.
  1527. EXPECT_EQ("foo", def1->getName());
  1528. EXPECT_EQ(100, def1->getCode());
  1529. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  1530. EXPECT_FALSE(def1->getArrayType());
  1531. EXPECT_TRUE(def1->getEncapsulatedSpace().empty());
  1532. // Check the second option definition we have created.
  1533. OptionDefinitionPtr def2 = CfgMgr::instance().getStagingCfg()->
  1534. getCfgOptionDef()->get("isc", 101);
  1535. ASSERT_TRUE(def2);
  1536. // Check the option data.
  1537. EXPECT_EQ("foo-2", def2->getName());
  1538. EXPECT_EQ(101, def2->getCode());
  1539. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  1540. EXPECT_FALSE(def2->getArrayType());
  1541. EXPECT_TRUE(def2->getEncapsulatedSpace().empty());
  1542. }
  1543. // The goal of this test is to verify that the duplicated option
  1544. // definition is not accepted.
  1545. TEST_F(Dhcp4ParserTest, optionDefDuplicate) {
  1546. // Preconfigure libdhcp++ with option definitions. The new configuration
  1547. // should override it, but when the new configuration fails, it should
  1548. // revert to this original configuration.
  1549. OptionDefSpaceContainer defs;
  1550. OptionDefinitionPtr def(new OptionDefinition("bar", 233, "string"));
  1551. defs.addItem(def, "isc");
  1552. LibDHCP::setRuntimeOptionDefs(defs);
  1553. LibDHCP::commitRuntimeOptionDefs();
  1554. // Configuration string. Both option definitions have
  1555. // the same code and belong to the same option space.
  1556. // This configuration should not be accepted.
  1557. std::string config =
  1558. "{ \"option-def\": [ {"
  1559. " \"name\": \"foo\","
  1560. " \"code\": 100,"
  1561. " \"type\": \"uint32\","
  1562. " \"space\": \"isc\""
  1563. " },"
  1564. " {"
  1565. " \"name\": \"foo-2\","
  1566. " \"code\": 100,"
  1567. " \"type\": \"ipv4-address\","
  1568. " \"space\": \"isc\""
  1569. " } ]"
  1570. "}";
  1571. ConstElementPtr json;
  1572. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1573. // Make sure that the option definition does not exist yet.
  1574. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1575. getCfgOptionDef()->get("isc", 100));
  1576. // Use the configuration string to create new option definitions.
  1577. ConstElementPtr status;
  1578. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1579. ASSERT_TRUE(status);
  1580. checkResult(status, 1);
  1581. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1582. // The new configuration should have inserted option 100, but
  1583. // once configuration failed (on the duplicate option definition)
  1584. // the original configuration in libdhcp++ should be reverted.
  1585. EXPECT_FALSE(LibDHCP::getRuntimeOptionDef("isc", 100));
  1586. def = LibDHCP::getRuntimeOptionDef("isc", 233);
  1587. ASSERT_TRUE(def);
  1588. EXPECT_EQ("bar", def->getName());
  1589. EXPECT_EQ(233, def->getCode());
  1590. }
  1591. // The goal of this test is to verify that the option definition
  1592. // comprising an array of uint32 values can be created.
  1593. TEST_F(Dhcp4ParserTest, optionDefArray) {
  1594. // Configuration string. Created option definition should
  1595. // comprise an array of uint32 values.
  1596. std::string config =
  1597. "{ \"option-def\": [ {"
  1598. " \"name\": \"foo\","
  1599. " \"code\": 100,"
  1600. " \"type\": \"uint32\","
  1601. " \"array\": true,"
  1602. " \"space\": \"isc\""
  1603. " } ]"
  1604. "}";
  1605. ConstElementPtr json;
  1606. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1607. extractConfig(config);
  1608. // Make sure that the particular option definition does not exist.
  1609. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1610. getCfgOptionDef()->get("isc", 100);
  1611. ASSERT_FALSE(def);
  1612. // Use the configuration string to create new option definition.
  1613. ConstElementPtr status;
  1614. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1615. ASSERT_TRUE(status);
  1616. checkResult(status, 0);
  1617. // The option definition should now be available in the CfgMgr.
  1618. def = CfgMgr::instance().getStagingCfg()->
  1619. getCfgOptionDef()->get("isc", 100);
  1620. ASSERT_TRUE(def);
  1621. // Check the option data.
  1622. EXPECT_EQ("foo", def->getName());
  1623. EXPECT_EQ(100, def->getCode());
  1624. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1625. EXPECT_TRUE(def->getArrayType());
  1626. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  1627. }
  1628. // The purpose of this test to verify that encapsulated option
  1629. // space name may be specified.
  1630. TEST_F(Dhcp4ParserTest, optionDefEncapsulate) {
  1631. // Configuration string. Included the encapsulated
  1632. // option space name.
  1633. std::string config =
  1634. "{ \"option-def\": [ {"
  1635. " \"name\": \"foo\","
  1636. " \"code\": 100,"
  1637. " \"type\": \"uint32\","
  1638. " \"space\": \"isc\","
  1639. " \"encapsulate\": \"sub-opts-space\""
  1640. " } ]"
  1641. "}";
  1642. ConstElementPtr json;
  1643. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1644. extractConfig(config);
  1645. // Make sure that the particular option definition does not exist.
  1646. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1647. getCfgOptionDef()->get("isc", 100);
  1648. ASSERT_FALSE(def);
  1649. // Use the configuration string to create new option definition.
  1650. ConstElementPtr status;
  1651. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1652. ASSERT_TRUE(status);
  1653. checkResult(status, 0);
  1654. // The option definition should now be available in the CfgMgr.
  1655. def = CfgMgr::instance().getStagingCfg()->
  1656. getCfgOptionDef()->get("isc", 100);
  1657. ASSERT_TRUE(def);
  1658. // Check the option data.
  1659. EXPECT_EQ("foo", def->getName());
  1660. EXPECT_EQ(100, def->getCode());
  1661. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1662. EXPECT_FALSE(def->getArrayType());
  1663. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  1664. }
  1665. /// The purpose of this test is to verify that the option definition
  1666. /// with invalid name is not accepted.
  1667. TEST_F(Dhcp4ParserTest, optionDefInvalidName) {
  1668. // Configuration string. The option name is invalid as it
  1669. // contains the % character.
  1670. std::string config =
  1671. "{ \"option-def\": [ {"
  1672. " \"name\": \"invalid%name\","
  1673. " \"code\": 100,"
  1674. " \"type\": \"string\","
  1675. " \"space\": \"isc\""
  1676. " } ]"
  1677. "}";
  1678. ConstElementPtr json;
  1679. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1680. // Use the configuration string to create new option definition.
  1681. ConstElementPtr status;
  1682. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1683. ASSERT_TRUE(status);
  1684. // Expecting parsing error (error code 1).
  1685. checkResult(status, 1);
  1686. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1687. }
  1688. /// The purpose of this test is to verify that the option definition
  1689. /// with invalid type is not accepted.
  1690. TEST_F(Dhcp4ParserTest, optionDefInvalidType) {
  1691. // Configuration string. The option type is invalid. It is
  1692. // "sting" instead of "string".
  1693. std::string config =
  1694. "{ \"option-def\": [ {"
  1695. " \"name\": \"foo\","
  1696. " \"code\": 100,"
  1697. " \"type\": \"sting\","
  1698. " \"space\": \"isc\""
  1699. " } ]"
  1700. "}";
  1701. ConstElementPtr json;
  1702. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1703. // Use the configuration string to create new option definition.
  1704. ConstElementPtr status;
  1705. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1706. ASSERT_TRUE(status);
  1707. // Expecting parsing error (error code 1).
  1708. checkResult(status, 1);
  1709. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1710. }
  1711. /// The purpose of this test is to verify that the option definition
  1712. /// with invalid type is not accepted.
  1713. TEST_F(Dhcp4ParserTest, optionDefInvalidRecordType) {
  1714. // Configuration string. The third of the record fields
  1715. // is invalid. It is "sting" instead of "string".
  1716. std::string config =
  1717. "{ \"option-def\": [ {"
  1718. " \"name\": \"foo\","
  1719. " \"code\": 100,"
  1720. " \"type\": \"record\","
  1721. " \"record-types\": \"uint32,uint8,sting\","
  1722. " \"space\": \"isc\""
  1723. " } ]"
  1724. "}";
  1725. ConstElementPtr json;
  1726. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1727. // Use the configuration string to create new option definition.
  1728. ConstElementPtr status;
  1729. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1730. ASSERT_TRUE(status);
  1731. // Expecting parsing error (error code 1).
  1732. checkResult(status, 1);
  1733. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1734. }
  1735. /// The purpose of this test is to verify that various integer types
  1736. /// are supported.
  1737. TEST_F(Dhcp4ParserTest, optionIntegerTypes) {
  1738. // Configuration string. The third of the record fields
  1739. // is invalid. It is "sting" instead of "string".
  1740. std::string config =
  1741. "{ \"option-def\": [ {"
  1742. " \"name\": \"foo\","
  1743. " \"code\": 100,"
  1744. " \"type\": \"record\","
  1745. " \"record-types\": \"uint8,uint16,uint32,int8,int16,int32\","
  1746. " \"space\": \"isc\""
  1747. " } ]"
  1748. "}";
  1749. ConstElementPtr json;
  1750. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1751. // Use the configuration string to create new option definition.
  1752. ConstElementPtr status;
  1753. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1754. ASSERT_TRUE(status);
  1755. // Expecting parsing error (error code 1).
  1756. checkResult(status, 0);
  1757. }
  1758. /// The goal of this test is to verify that the invalid encapsulated
  1759. /// option space name is not accepted.
  1760. TEST_F(Dhcp4ParserTest, optionDefInvalidEncapsulatedSpace) {
  1761. // Configuration string. The encapsulated option space
  1762. // name is invalid (% character is not allowed).
  1763. std::string config =
  1764. "{ \"option-def\": [ {"
  1765. " \"name\": \"foo\","
  1766. " \"code\": 100,"
  1767. " \"type\": \"uint32\","
  1768. " \"space\": \"isc\","
  1769. " \"encapsulate\": \"invalid%space%name\""
  1770. " } ]"
  1771. "}";
  1772. ConstElementPtr json;
  1773. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1774. // Use the configuration string to create new option definition.
  1775. ConstElementPtr status;
  1776. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1777. ASSERT_TRUE(status);
  1778. // Expecting parsing error (error code 1).
  1779. checkResult(status, 1);
  1780. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1781. }
  1782. /// The goal of this test is to verify that the encapsulated
  1783. /// option space name can't be specified for the option that
  1784. /// comprises an array of data fields.
  1785. TEST_F(Dhcp4ParserTest, optionDefEncapsulatedSpaceAndArray) {
  1786. // Configuration string. The encapsulated option space
  1787. // name is set to non-empty value and the array flag
  1788. // is set.
  1789. std::string config =
  1790. "{ \"option-def\": [ {"
  1791. " \"name\": \"foo\","
  1792. " \"code\": 100,"
  1793. " \"type\": \"uint32\","
  1794. " \"array\": true,"
  1795. " \"space\": \"isc\","
  1796. " \"encapsulate\": \"valid-space-name\""
  1797. " } ]"
  1798. "}";
  1799. ConstElementPtr json;
  1800. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1801. // Use the configuration string to create new option definition.
  1802. ConstElementPtr status;
  1803. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1804. ASSERT_TRUE(status);
  1805. // Expecting parsing error (error code 1).
  1806. checkResult(status, 1);
  1807. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1808. }
  1809. /// The goal of this test is to verify that the option may not
  1810. /// encapsulate option space it belongs to.
  1811. TEST_F(Dhcp4ParserTest, optionDefEncapsulateOwnSpace) {
  1812. // Configuration string. Option is set to encapsulate
  1813. // option space it belongs to.
  1814. std::string config =
  1815. "{ \"option-def\": [ {"
  1816. " \"name\": \"foo\","
  1817. " \"code\": 100,"
  1818. " \"type\": \"uint32\","
  1819. " \"space\": \"isc\","
  1820. " \"encapsulate\": \"isc\""
  1821. " } ]"
  1822. "}";
  1823. ConstElementPtr json;
  1824. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1825. // Use the configuration string to create new option definition.
  1826. ConstElementPtr status;
  1827. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1828. ASSERT_TRUE(status);
  1829. // Expecting parsing error (error code 1).
  1830. checkResult(status, 1);
  1831. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1832. }
  1833. /// The purpose of this test is to verify that it is not allowed
  1834. /// to override the standard option (that belongs to dhcp4 option
  1835. /// space and has its definition) and that it is allowed to define
  1836. /// option in the dhcp4 option space that has a code which is not
  1837. /// used by any of the standard options.
  1838. TEST_F(Dhcp4ParserTest, optionStandardDefOverride) {
  1839. // Configuration string. The option code 109 is unassigned so it
  1840. // can be used for a custom option definition in dhcp4 option space.
  1841. std::string config =
  1842. "{ \"option-def\": [ {"
  1843. " \"name\": \"foo\","
  1844. " \"code\": 109,"
  1845. " \"type\": \"string\","
  1846. " \"space\": \"dhcp4\""
  1847. " } ]"
  1848. "}";
  1849. ConstElementPtr json;
  1850. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1851. extractConfig(config);
  1852. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1853. getCfgOptionDef()->get(DHCP4_OPTION_SPACE, 109);
  1854. ASSERT_FALSE(def);
  1855. // Use the configuration string to create new option definition.
  1856. ConstElementPtr status;
  1857. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1858. ASSERT_TRUE(status);
  1859. checkResult(status, 0);
  1860. // The option definition should now be available in the CfgMgr.
  1861. def = CfgMgr::instance().getStagingCfg()->
  1862. getCfgOptionDef()->get(DHCP4_OPTION_SPACE, 109);
  1863. ASSERT_TRUE(def);
  1864. // Check the option data.
  1865. EXPECT_EQ("foo", def->getName());
  1866. EXPECT_EQ(109, def->getCode());
  1867. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1868. EXPECT_FALSE(def->getArrayType());
  1869. // The combination of option space and code is invalid. The 'dhcp4' option
  1870. // space groups standard options and the code 3 is reserved for one of
  1871. // them.
  1872. config =
  1873. "{ \"option-def\": [ {"
  1874. " \"name\": \"routers\","
  1875. " \"code\": 3,"
  1876. " \"type\": \"ipv4-address\","
  1877. " \"space\": \"dhcp4\""
  1878. " } ]"
  1879. "}";
  1880. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1881. // Use the configuration string to create new option definition.
  1882. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1883. ASSERT_TRUE(status);
  1884. // Expecting parsing error (error code 1).
  1885. checkResult(status, 1);
  1886. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1887. /// @todo The option 213 is a standard DHCPv4 option. However, at
  1888. /// this point there is no definition for this option in
  1889. /// libdhcp++, so it should be allowed to define it from the
  1890. /// configuration interface. This test will have to be removed
  1891. /// once definitions for remaining standard options are created.
  1892. config =
  1893. "{ \"option-def\": [ {"
  1894. " \"name\": \"access-network-domain-name\","
  1895. " \"code\": 213,"
  1896. " \"type\": \"string\","
  1897. " \"space\": \"dhcp4\""
  1898. " } ]"
  1899. "}";
  1900. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1901. extractConfig(config);
  1902. // Use the configuration string to create new option definition.
  1903. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1904. ASSERT_TRUE(status);
  1905. // Expecting success.
  1906. checkResult(status, 0);
  1907. def = CfgMgr::instance().getStagingCfg()->
  1908. getCfgOptionDef()->get(DHCP4_OPTION_SPACE, 213);
  1909. ASSERT_TRUE(def);
  1910. // Check the option data.
  1911. EXPECT_EQ("access-network-domain-name", def->getName());
  1912. EXPECT_EQ(213, def->getCode());
  1913. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1914. EXPECT_FALSE(def->getArrayType());
  1915. }
  1916. // Goal of this test is to verify that global option data is configured
  1917. TEST_F(Dhcp4ParserTest, optionDataDefaultsGlobal) {
  1918. ConstElementPtr x;
  1919. string config = "{ " + genIfaceConfig() + "," +
  1920. "\"rebind-timer\": 2000,"
  1921. "\"renew-timer\": 1000,"
  1922. "\"option-data\": [ {"
  1923. " \"name\": \"dhcp-message\","
  1924. " \"data\": \"ABCDEF0105\","
  1925. " \"csv-format\": false"
  1926. " },"
  1927. " {"
  1928. " \"name\": \"default-ip-ttl\","
  1929. " \"data\": \"01\","
  1930. " \"csv-format\": false"
  1931. " } ],"
  1932. "\"subnet4\": [ { "
  1933. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1934. " \"subnet\": \"192.0.2.0/24\""
  1935. " } ],"
  1936. "\"valid-lifetime\": 4000 }";
  1937. ConstElementPtr json;
  1938. ASSERT_NO_THROW(json = parseDHCP4(config));
  1939. extractConfig(config);
  1940. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1941. checkResult(x, 0);
  1942. // These options are global
  1943. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  1944. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  1945. ASSERT_TRUE(subnet);
  1946. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  1947. ASSERT_EQ(0, options->size());
  1948. options = CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  1949. ASSERT_EQ(2, options->size());
  1950. // Get the search index. Index #1 is to search using option code.
  1951. const OptionContainerTypeIndex& idx = options->get<1>();
  1952. // Get the options for specified index. Expecting one option to be
  1953. // returned but in theory we may have multiple options with the same
  1954. // code so we get the range.
  1955. std::pair<OptionContainerTypeIndex::const_iterator,
  1956. OptionContainerTypeIndex::const_iterator> range =
  1957. idx.equal_range(56);
  1958. // Expect single option with the code equal to 56.
  1959. ASSERT_EQ(1, std::distance(range.first, range.second));
  1960. const uint8_t foo_expected[] = {
  1961. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1962. };
  1963. // Check if option is valid in terms of code and carried data.
  1964. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1965. range = idx.equal_range(23);
  1966. ASSERT_EQ(1, std::distance(range.first, range.second));
  1967. // Do another round of testing with second option.
  1968. const uint8_t foo2_expected[] = {
  1969. 0x01
  1970. };
  1971. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  1972. // Check that options with other option codes are not returned.
  1973. for (uint8_t code = 24; code < 35; ++code) {
  1974. range = idx.equal_range(code);
  1975. EXPECT_EQ(0, std::distance(range.first, range.second));
  1976. }
  1977. }
  1978. // Goal of this test is to verify that subnet option data is configured
  1979. TEST_F(Dhcp4ParserTest, optionDataDefaultsSubnet) {
  1980. ConstElementPtr x;
  1981. string config = "{ " + genIfaceConfig() + "," +
  1982. "\"rebind-timer\": 2000,"
  1983. "\"renew-timer\": 1000,"
  1984. "\"subnet4\": [ { "
  1985. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  1986. " \"subnet\": \"192.0.2.0/24\","
  1987. " \"option-data\": [ {"
  1988. " \"name\": \"dhcp-message\","
  1989. " \"data\": \"ABCDEF0105\","
  1990. " \"csv-format\": false"
  1991. " },"
  1992. " {"
  1993. " \"name\": \"default-ip-ttl\","
  1994. " \"data\": \"01\","
  1995. " \"csv-format\": false"
  1996. " } ]"
  1997. " } ],"
  1998. "\"valid-lifetime\": 4000 }";
  1999. ConstElementPtr json;
  2000. ASSERT_NO_THROW(json = parseDHCP4(config));
  2001. extractConfig(config);
  2002. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2003. checkResult(x, 0);
  2004. // These options are subnet options
  2005. OptionContainerPtr options =
  2006. CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2007. ASSERT_EQ(0, options->size());
  2008. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  2009. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  2010. ASSERT_TRUE(subnet);
  2011. options = subnet->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2012. ASSERT_EQ(2, options->size());
  2013. // Get the search index. Index #1 is to search using option code.
  2014. const OptionContainerTypeIndex& idx = options->get<1>();
  2015. // Get the options for specified index. Expecting one option to be
  2016. // returned but in theory we may have multiple options with the same
  2017. // code so we get the range.
  2018. std::pair<OptionContainerTypeIndex::const_iterator,
  2019. OptionContainerTypeIndex::const_iterator> range =
  2020. idx.equal_range(56);
  2021. // Expect single option with the code equal to 56.
  2022. ASSERT_EQ(1, std::distance(range.first, range.second));
  2023. const uint8_t foo_expected[] = {
  2024. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  2025. };
  2026. // Check if option is valid in terms of code and carried data.
  2027. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  2028. range = idx.equal_range(23);
  2029. ASSERT_EQ(1, std::distance(range.first, range.second));
  2030. // Do another round of testing with second option.
  2031. const uint8_t foo2_expected[] = {
  2032. 0x01
  2033. };
  2034. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  2035. }
  2036. /// The goal of this test is to verify that two options having the same
  2037. /// option code can be added to different option spaces.
  2038. TEST_F(Dhcp4ParserTest, optionDataTwoSpaces) {
  2039. // This configuration string is to configure two options
  2040. // sharing the code 56 and having different definitions
  2041. // and belonging to the different option spaces.
  2042. // The option definition must be provided for the
  2043. // option that belongs to the 'isc' option space.
  2044. // The definition is not required for the option that
  2045. // belongs to the 'dhcp4' option space as it is the
  2046. // standard option.
  2047. string config = "{ " + genIfaceConfig() + "," +
  2048. "\"valid-lifetime\": 4000,"
  2049. "\"rebind-timer\": 2000,"
  2050. "\"renew-timer\": 1000,"
  2051. "\"option-data\": [ {"
  2052. " \"name\": \"dhcp-message\","
  2053. " \"data\": \"ABCDEF0105\","
  2054. " \"csv-format\": false"
  2055. " },"
  2056. " {"
  2057. " \"name\": \"foo\","
  2058. " \"space\": \"isc\","
  2059. " \"data\": \"1234\""
  2060. " } ],"
  2061. "\"option-def\": [ {"
  2062. " \"name\": \"foo\","
  2063. " \"code\": 56,"
  2064. " \"type\": \"uint32\","
  2065. " \"space\": \"isc\""
  2066. " } ],"
  2067. "\"subnet4\": [ { "
  2068. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2069. " \"subnet\": \"192.0.2.0/24\""
  2070. " } ]"
  2071. "}";
  2072. ConstElementPtr json;
  2073. ASSERT_NO_THROW(json = parseDHCP4(config));
  2074. extractConfig(config);
  2075. ConstElementPtr status;
  2076. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2077. ASSERT_TRUE(status);
  2078. checkResult(status, 0);
  2079. // Options should be now available
  2080. // Try to get the option from the space dhcp4.
  2081. OptionDescriptor desc1 =
  2082. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(DHCP4_OPTION_SPACE, 56);
  2083. ASSERT_TRUE(desc1.option_);
  2084. EXPECT_EQ(56, desc1.option_->getType());
  2085. // Try to get the option from the space isc.
  2086. OptionDescriptor desc2 =
  2087. CfgMgr::instance().getStagingCfg()->getCfgOption()->get("isc", 56);
  2088. ASSERT_TRUE(desc2.option_);
  2089. EXPECT_EQ(56, desc1.option_->getType());
  2090. // Try to get the non-existing option from the non-existing
  2091. // option space and expect that option is not returned.
  2092. OptionDescriptor desc3 =
  2093. CfgMgr::instance().getStagingCfg()->getCfgOption()->get("non-existing", 56);
  2094. ASSERT_FALSE(desc3.option_);
  2095. }
  2096. // The goal of this test is to verify that it is possible to
  2097. // encapsulate option space containing some options with
  2098. // another option. In this test we create base option that
  2099. // encapsulates option space 'isc' that comprises two other
  2100. // options. Also, for all options their definitions are
  2101. // created.
  2102. TEST_F(Dhcp4ParserTest, optionDataEncapsulate) {
  2103. // @todo DHCP configurations has many dependencies between
  2104. // parameters. First of all, configuration for subnet was
  2105. // inherited from the global values. Thus subnet had to be
  2106. // configured when all global values have been configured.
  2107. // Also, an option can encapsulate another option only
  2108. // if the latter has been configured. For this reason in this
  2109. // test we created two-stage configuration where first we
  2110. // created options that belong to encapsulated option space.
  2111. // In the second stage we add the base option. Also, the Subnet
  2112. // object is configured in the second stage so it is created
  2113. // at the very end (when all other parameters are configured).
  2114. // Starting stage 1. Configure sub-options and their definitions.
  2115. string config = "{ " + genIfaceConfig() + "," +
  2116. "\"valid-lifetime\": 4000,"
  2117. "\"rebind-timer\": 2000,"
  2118. "\"renew-timer\": 1000,"
  2119. "\"option-data\": [ {"
  2120. " \"name\": \"foo\","
  2121. " \"space\": \"isc\","
  2122. " \"data\": \"1234\""
  2123. " },"
  2124. " {"
  2125. " \"name\": \"foo2\","
  2126. " \"space\": \"isc\","
  2127. " \"data\": \"192.168.2.1\""
  2128. " } ],"
  2129. "\"option-def\": [ {"
  2130. " \"name\": \"foo\","
  2131. " \"code\": 1,"
  2132. " \"type\": \"uint32\","
  2133. " \"space\": \"isc\""
  2134. " },"
  2135. " {"
  2136. " \"name\": \"foo2\","
  2137. " \"code\": 2,"
  2138. " \"type\": \"ipv4-address\","
  2139. " \"space\": \"isc\""
  2140. " } ]"
  2141. "}";
  2142. ConstElementPtr json;
  2143. ASSERT_NO_THROW(json = parseDHCP4(config));
  2144. extractConfig(config);
  2145. ConstElementPtr status;
  2146. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2147. ASSERT_TRUE(status);
  2148. checkResult(status, 0);
  2149. CfgMgr::instance().clear();
  2150. // Stage 2. Configure base option and a subnet. Please note that
  2151. // the configuration from the stage 2 is repeated because Kea
  2152. // configuration manager sends whole configuration for the lists
  2153. // where at least one element is being modified or added.
  2154. config = "{ " + genIfaceConfig() + "," +
  2155. "\"valid-lifetime\": 3000,"
  2156. "\"rebind-timer\": 2000,"
  2157. "\"renew-timer\": 1000,"
  2158. "\"option-data\": [ {"
  2159. " \"name\": \"base-option\","
  2160. " \"data\": \"11\""
  2161. " },"
  2162. " {"
  2163. " \"name\": \"foo\","
  2164. " \"space\": \"isc\","
  2165. " \"data\": \"1234\""
  2166. " },"
  2167. " {"
  2168. " \"name\": \"foo2\","
  2169. " \"space\": \"isc\","
  2170. " \"data\": \"192.168.2.1\""
  2171. " } ],"
  2172. "\"option-def\": [ {"
  2173. " \"name\": \"base-option\","
  2174. " \"code\": 222,"
  2175. " \"type\": \"uint8\","
  2176. " \"space\": \"dhcp4\","
  2177. " \"encapsulate\": \"isc\""
  2178. "},"
  2179. "{"
  2180. " \"name\": \"foo\","
  2181. " \"code\": 1,"
  2182. " \"type\": \"uint32\","
  2183. " \"space\": \"isc\""
  2184. " },"
  2185. " {"
  2186. " \"name\": \"foo2\","
  2187. " \"code\": 2,"
  2188. " \"type\": \"ipv4-address\","
  2189. " \"space\": \"isc\""
  2190. " } ],"
  2191. "\"subnet4\": [ { "
  2192. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2193. " \"subnet\": \"192.0.2.0/24\""
  2194. " } ]"
  2195. "}";
  2196. ASSERT_NO_THROW(json = parseDHCP4(config));
  2197. extractConfig(config);
  2198. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2199. ASSERT_TRUE(status);
  2200. checkResult(status, 0);
  2201. // We should have one option available.
  2202. OptionContainerPtr options =
  2203. CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2204. ASSERT_TRUE(options);
  2205. ASSERT_EQ(1, options->size());
  2206. // Get the option.
  2207. OptionDescriptor desc =
  2208. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(DHCP4_OPTION_SPACE, 222);
  2209. EXPECT_TRUE(desc.option_);
  2210. EXPECT_EQ(222, desc.option_->getType());
  2211. // This option should comprise two sub-options.
  2212. // One of them is 'foo' with code 1.
  2213. OptionPtr option_foo = desc.option_->getOption(1);
  2214. ASSERT_TRUE(option_foo);
  2215. EXPECT_EQ(1, option_foo->getType());
  2216. // ...another one 'foo2' with code 2.
  2217. OptionPtr option_foo2 = desc.option_->getOption(2);
  2218. ASSERT_TRUE(option_foo2);
  2219. EXPECT_EQ(2, option_foo2->getType());
  2220. }
  2221. // Goal of this test is to verify options configuration
  2222. // for a single subnet. In particular this test checks
  2223. // that local options configuration overrides global
  2224. // option setting.
  2225. TEST_F(Dhcp4ParserTest, optionDataInSingleSubnet) {
  2226. ConstElementPtr x;
  2227. string config = "{ " + genIfaceConfig() + "," +
  2228. "\"rebind-timer\": 2000, "
  2229. "\"renew-timer\": 1000, "
  2230. "\"option-data\": [ {"
  2231. " \"name\": \"dhcp-message\","
  2232. " \"data\": \"AB\","
  2233. " \"csv-format\": false"
  2234. " } ],"
  2235. "\"subnet4\": [ { "
  2236. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2237. " \"subnet\": \"192.0.2.0/24\", "
  2238. " \"option-data\": [ {"
  2239. " \"name\": \"dhcp-message\","
  2240. " \"data\": \"ABCDEF0105\","
  2241. " \"csv-format\": false"
  2242. " },"
  2243. " {"
  2244. " \"name\": \"default-ip-ttl\","
  2245. " \"data\": \"01\","
  2246. " \"csv-format\": false"
  2247. " } ]"
  2248. " } ],"
  2249. "\"valid-lifetime\": 4000 }";
  2250. ConstElementPtr json;
  2251. ASSERT_NO_THROW(json = parseDHCP4(config));
  2252. extractConfig(config);
  2253. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2254. checkResult(x, 0);
  2255. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  2256. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.24"));
  2257. ASSERT_TRUE(subnet);
  2258. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2259. ASSERT_EQ(2, options->size());
  2260. // Get the search index. Index #1 is to search using option code.
  2261. const OptionContainerTypeIndex& idx = options->get<1>();
  2262. // Get the options for specified index. Expecting one option to be
  2263. // returned but in theory we may have multiple options with the same
  2264. // code so we get the range.
  2265. std::pair<OptionContainerTypeIndex::const_iterator,
  2266. OptionContainerTypeIndex::const_iterator> range =
  2267. idx.equal_range(56);
  2268. // Expect single option with the code equal to 100.
  2269. ASSERT_EQ(1, std::distance(range.first, range.second));
  2270. const uint8_t foo_expected[] = {
  2271. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  2272. };
  2273. // Check if option is valid in terms of code and carried data.
  2274. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  2275. range = idx.equal_range(23);
  2276. ASSERT_EQ(1, std::distance(range.first, range.second));
  2277. // Do another round of testing with second option.
  2278. const uint8_t foo2_expected[] = {
  2279. 0x01
  2280. };
  2281. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  2282. }
  2283. // The goal of this test is to check that the option carrying a boolean
  2284. // value can be configured using one of the values: "true", "false", "0"
  2285. // or "1".
  2286. TEST_F(Dhcp4ParserTest, optionDataBoolean) {
  2287. // Create configuration. Use standard option 19 (ip-forwarding).
  2288. std::map<std::string, std::string> params;
  2289. params["name"] = "ip-forwarding";
  2290. params["space"] = DHCP4_OPTION_SPACE;
  2291. params["code"] = "19";
  2292. params["data"] = "true";
  2293. params["csv-format"] = "true";
  2294. std::string config = createConfigWithOption(params);
  2295. ASSERT_TRUE(executeConfiguration(config, "parse configuration with a"
  2296. " boolean value"));
  2297. // The subnet should now hold one option with the code 19.
  2298. OptionDescriptor desc = getOptionFromSubnet(IOAddress("192.0.2.24"),
  2299. 19);
  2300. ASSERT_TRUE(desc.option_);
  2301. // This option should be set to "true", represented as 0x1 in the option
  2302. // buffer.
  2303. uint8_t expected_option_data[] = {
  2304. 0x1
  2305. };
  2306. testConfiguration(params, 19, expected_option_data,
  2307. sizeof(expected_option_data));
  2308. // Configure the option with the "1" value. This should have the same
  2309. // effect as if "true" was specified.
  2310. params["data"] = "1";
  2311. testConfiguration(params, 19, expected_option_data,
  2312. sizeof(expected_option_data));
  2313. // The value of "1" with a few leading zeros should work too.
  2314. params["data"] = "00001";
  2315. testConfiguration(params, 19, expected_option_data,
  2316. sizeof(expected_option_data));
  2317. // Configure the option with the "false" value.
  2318. params["data"] = "false";
  2319. // The option buffer should now hold the value of 0.
  2320. expected_option_data[0] = 0;
  2321. testConfiguration(params, 19, expected_option_data,
  2322. sizeof(expected_option_data));
  2323. // Specifying "0" should have the same effect as "false".
  2324. params["data"] = "0";
  2325. testConfiguration(params, 19, expected_option_data,
  2326. sizeof(expected_option_data));
  2327. // The same effect should be for multiple 0 chars.
  2328. params["data"] = "00000";
  2329. testConfiguration(params, 19, expected_option_data,
  2330. sizeof(expected_option_data));
  2331. // Bogus values should not be accepted.
  2332. params["data"] = "bogus";
  2333. testInvalidOptionParam(params);
  2334. params["data"] = "2";
  2335. testInvalidOptionParam(params);
  2336. // Now let's test that it is possible to use binary format.
  2337. params["data"] = "0";
  2338. params["csv-format"] = "false";
  2339. testConfiguration(params, 19, expected_option_data,
  2340. sizeof(expected_option_data));
  2341. // The binary 1 should work as well.
  2342. params["data"] = "1";
  2343. expected_option_data[0] = 1;
  2344. testConfiguration(params, 19, expected_option_data,
  2345. sizeof(expected_option_data));
  2346. // As well as an even number of digits.
  2347. params["data"] = "01";
  2348. testConfiguration(params, 19, expected_option_data,
  2349. sizeof(expected_option_data));
  2350. }
  2351. // Goal of this test is to verify options configuration
  2352. // for multiple subnets.
  2353. TEST_F(Dhcp4ParserTest, optionDataInMultipleSubnets) {
  2354. ConstElementPtr x;
  2355. string config = "{ " + genIfaceConfig() + "," +
  2356. "\"rebind-timer\": 2000, "
  2357. "\"renew-timer\": 1000, "
  2358. "\"subnet4\": [ { "
  2359. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2360. " \"subnet\": \"192.0.2.0/24\", "
  2361. " \"option-data\": [ {"
  2362. " \"name\": \"dhcp-message\","
  2363. " \"data\": \"0102030405060708090A\","
  2364. " \"csv-format\": false"
  2365. " } ]"
  2366. " },"
  2367. " {"
  2368. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  2369. " \"subnet\": \"192.0.3.0/24\", "
  2370. " \"option-data\": [ {"
  2371. " \"name\": \"default-ip-ttl\","
  2372. " \"data\": \"FF\","
  2373. " \"csv-format\": false"
  2374. " } ]"
  2375. " } ],"
  2376. "\"valid-lifetime\": 4000 }";
  2377. ConstElementPtr json;
  2378. ASSERT_NO_THROW(json = parseDHCP4(config));
  2379. extractConfig(config);
  2380. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2381. checkResult(x, 0);
  2382. Subnet4Ptr subnet1 = CfgMgr::instance().getStagingCfg()->
  2383. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.100"));
  2384. ASSERT_TRUE(subnet1);
  2385. OptionContainerPtr options1 = subnet1->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2386. ASSERT_EQ(1, options1->size());
  2387. // Get the search index. Index #1 is to search using option code.
  2388. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2389. // Get the options for specified index. Expecting one option to be
  2390. // returned but in theory we may have multiple options with the same
  2391. // code so we get the range.
  2392. std::pair<OptionContainerTypeIndex::const_iterator,
  2393. OptionContainerTypeIndex::const_iterator> range1 =
  2394. idx1.equal_range(56);
  2395. // Expect single option with the code equal to 56.
  2396. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2397. const uint8_t foo_expected[] = {
  2398. 0x01, 0x02, 0x03, 0x04, 0x05,
  2399. 0x06, 0x07, 0x08, 0x09, 0x0A
  2400. };
  2401. // Check if option is valid in terms of code and carried data.
  2402. testOption(*range1.first, 56, foo_expected, sizeof(foo_expected));
  2403. // Test another subnet in the same way.
  2404. Subnet4Ptr subnet2 = CfgMgr::instance().getStagingCfg()->
  2405. getCfgSubnets4()->selectSubnet(IOAddress("192.0.3.102"));
  2406. ASSERT_TRUE(subnet2);
  2407. OptionContainerPtr options2 = subnet2->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2408. ASSERT_EQ(1, options2->size());
  2409. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2410. std::pair<OptionContainerTypeIndex::const_iterator,
  2411. OptionContainerTypeIndex::const_iterator> range2 =
  2412. idx2.equal_range(23);
  2413. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2414. const uint8_t foo2_expected[] = { 0xFF };
  2415. testOption(*range2.first, 23, foo2_expected, sizeof(foo2_expected));
  2416. }
  2417. // This test verifies that it is possible to specify options on
  2418. // pool levels.
  2419. TEST_F(Dhcp4ParserTest, optionDataSinglePool) {
  2420. ConstElementPtr x;
  2421. string config = "{ " + genIfaceConfig() + ","
  2422. "\"rebind-timer\": 2000, "
  2423. "\"renew-timer\": 1000, "
  2424. "\"subnet4\": [ { "
  2425. " \"pools\": [ { "
  2426. " \"pool\": \"192.0.2.1 - 192.0.2.100\","
  2427. " \"option-data\": [ {"
  2428. " \"name\": \"dhcp-message\","
  2429. " \"data\": \"ABCDEF0105\","
  2430. " \"csv-format\": false"
  2431. " },"
  2432. " {"
  2433. " \"name\": \"default-ip-ttl\","
  2434. " \"data\": \"01\","
  2435. " \"csv-format\": false"
  2436. " } ]"
  2437. " } ],"
  2438. " \"subnet\": \"192.0.2.0/24\""
  2439. " } ],"
  2440. "\"valid-lifetime\": 4000 }";
  2441. ConstElementPtr json;
  2442. ASSERT_NO_THROW(json = parseDHCP4(config));
  2443. extractConfig(config);
  2444. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2445. checkResult(x, 0);
  2446. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->
  2447. selectSubnet(IOAddress("192.0.2.24"), classify_);
  2448. ASSERT_TRUE(subnet);
  2449. PoolPtr pool = subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.2.24"), false);
  2450. ASSERT_TRUE(pool);
  2451. Pool4Ptr pool4 = boost::dynamic_pointer_cast<Pool4>(pool);
  2452. ASSERT_TRUE(pool4);
  2453. OptionContainerPtr options = pool4->getCfgOption()->getAll("dhcp4");
  2454. ASSERT_EQ(2, options->size());
  2455. // Get the search index. Index #1 is to search using option code.
  2456. const OptionContainerTypeIndex& idx = options->get<1>();
  2457. // Get the options for specified index. Expecting one option to be
  2458. // returned but in theory we may have multiple options with the same
  2459. // code so we get the range.
  2460. std::pair<OptionContainerTypeIndex::const_iterator,
  2461. OptionContainerTypeIndex::const_iterator> range =
  2462. idx.equal_range(56);
  2463. // Expect a single option with the code equal to 100.
  2464. ASSERT_EQ(1, std::distance(range.first, range.second));
  2465. const uint8_t foo_expected[] = {
  2466. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  2467. };
  2468. // Check if option is valid in terms of code and carried data.
  2469. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  2470. range = idx.equal_range(23);
  2471. ASSERT_EQ(1, std::distance(range.first, range.second));
  2472. // Do another round of testing with second option.
  2473. const uint8_t foo2_expected[] = {
  2474. 0x01
  2475. };
  2476. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  2477. }
  2478. // This test verifies that it's possible to define different options in
  2479. // different pools and those options are not confused.
  2480. TEST_F(Dhcp4ParserTest, optionDataMultiplePools) {
  2481. ConstElementPtr x;
  2482. string config = "{ " + genIfaceConfig() + ","
  2483. "\"rebind-timer\": 2000, "
  2484. "\"renew-timer\": 1000, "
  2485. "\"subnet4\": [ { "
  2486. " \"pools\": [ { "
  2487. " \"pool\": \"192.0.2.1 - 192.0.2.100\","
  2488. " \"option-data\": [ {"
  2489. " \"name\": \"dhcp-message\","
  2490. " \"data\": \"ABCDEF0105\","
  2491. " \"csv-format\": false"
  2492. " } ]"
  2493. " },"
  2494. " {"
  2495. " \"pool\": \"192.0.2.200 - 192.0.2.250\","
  2496. " \"option-data\": [ {"
  2497. " \"name\": \"default-ip-ttl\","
  2498. " \"data\": \"01\","
  2499. " \"csv-format\": false"
  2500. " } ]"
  2501. " } ],"
  2502. " \"subnet\": \"192.0.2.0/24\""
  2503. " } ],"
  2504. "\"valid-lifetime\": 4000 }";
  2505. ConstElementPtr json;
  2506. ASSERT_NO_THROW(json = parseDHCP4(config));
  2507. extractConfig(config);
  2508. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2509. checkResult(x, 0);
  2510. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->
  2511. selectSubnet(IOAddress("192.0.2.24"), classify_);
  2512. ASSERT_TRUE(subnet);
  2513. PoolPtr pool1 = subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.2.24"), false);
  2514. ASSERT_TRUE(pool1);
  2515. Pool4Ptr pool41 = boost::dynamic_pointer_cast<Pool4>(pool1);
  2516. ASSERT_TRUE(pool41);
  2517. OptionContainerPtr options1 = pool41->getCfgOption()->getAll("dhcp4");
  2518. ASSERT_EQ(1, options1->size());
  2519. // Get the search index. Index #1 is to search using option code.
  2520. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2521. // Get the options for specified index. Expecting one option to be
  2522. // returned but in theory we may have multiple options with the same
  2523. // code so we get the range.
  2524. std::pair<OptionContainerTypeIndex::const_iterator,
  2525. OptionContainerTypeIndex::const_iterator> range1 =
  2526. idx1.equal_range(56);
  2527. // Expect a single option with the code equal to 100.
  2528. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2529. const uint8_t foo_expected[] = {
  2530. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  2531. };
  2532. // Check if option is valid in terms of code and carried data.
  2533. testOption(*range1.first, 56, foo_expected, sizeof(foo_expected));
  2534. // Test another pool in the same way.
  2535. PoolPtr pool2 = subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.2.240"), false);
  2536. ASSERT_TRUE(pool2);
  2537. Pool4Ptr pool42 = boost::dynamic_pointer_cast<Pool4>(pool2);
  2538. ASSERT_TRUE(pool42);
  2539. OptionContainerPtr options2 = pool42->getCfgOption()->getAll("dhcp4");
  2540. ASSERT_EQ(1, options2->size());
  2541. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2542. std::pair<OptionContainerTypeIndex::const_iterator,
  2543. OptionContainerTypeIndex::const_iterator> range2 =
  2544. idx2.equal_range(23);
  2545. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2546. const uint8_t foo2_expected[] = {
  2547. 0x01
  2548. };
  2549. testOption(*range2.first, 23, foo2_expected, sizeof(foo2_expected));
  2550. }
  2551. // Verify that empty option name is rejected in the configuration.
  2552. TEST_F(Dhcp4ParserTest, optionNameEmpty) {
  2553. // Empty option names not allowed.
  2554. testInvalidOptionParam("", "name");
  2555. }
  2556. // Verify that empty option name with spaces is rejected
  2557. // in the configuration.
  2558. TEST_F(Dhcp4ParserTest, optionNameSpaces) {
  2559. // Spaces in option names not allowed.
  2560. testInvalidOptionParam("option foo", "name");
  2561. }
  2562. // Verify that negative option code is rejected in the configuration.
  2563. TEST_F(Dhcp4ParserTest, optionCodeNegative) {
  2564. // Check negative option code -4. This should fail too.
  2565. testInvalidOptionParam("-4", "code");
  2566. }
  2567. // Verify that out of bounds option code is rejected in the configuration.
  2568. TEST_F(Dhcp4ParserTest, optionCodeNonUint8) {
  2569. // The valid option codes are uint16_t values so passing
  2570. // uint16_t maximum value incremented by 1 should result
  2571. // in failure.
  2572. testInvalidOptionParam("257", "code");
  2573. }
  2574. // Verify that zero option code is rejected in the configuration.
  2575. TEST_F(Dhcp4ParserTest, optionCodeZero) {
  2576. // Option code 0 is reserved and should not be accepted
  2577. // by configuration parser.
  2578. testInvalidOptionParam("0", "code");
  2579. }
  2580. // Verify that option data which contains non hexadecimal characters
  2581. // is rejected by the configuration.
  2582. TEST_F(Dhcp4ParserTest, optionDataInvalidChar) {
  2583. // Option code 0 is reserved and should not be accepted
  2584. // by configuration parser.
  2585. testInvalidOptionParam("01020R", "data");
  2586. }
  2587. // Verify that option data containing '0x' prefix is rejected
  2588. // by the configuration.
  2589. TEST_F(Dhcp4ParserTest, optionDataUnexpectedPrefix) {
  2590. // Option code 0 is reserved and should not be accepted
  2591. // by configuration parser.
  2592. testInvalidOptionParam("0x0102", "data");
  2593. }
  2594. // Verify that either lower or upper case characters are allowed
  2595. // to specify the option data.
  2596. TEST_F(Dhcp4ParserTest, optionDataLowerCase) {
  2597. ConstElementPtr x;
  2598. std::string config = createConfigWithOption("0a0b0C0D", "data");
  2599. ConstElementPtr json;
  2600. ASSERT_NO_THROW(json = parseDHCP4(config));
  2601. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2602. checkResult(x, 0);
  2603. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  2604. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.5"));
  2605. ASSERT_TRUE(subnet);
  2606. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2607. ASSERT_EQ(1, options->size());
  2608. // Get the search index. Index #1 is to search using option code.
  2609. const OptionContainerTypeIndex& idx = options->get<1>();
  2610. // Get the options for specified index. Expecting one option to be
  2611. // returned but in theory we may have multiple options with the same
  2612. // code so we get the range.
  2613. std::pair<OptionContainerTypeIndex::const_iterator,
  2614. OptionContainerTypeIndex::const_iterator> range =
  2615. idx.equal_range(56);
  2616. // Expect single option with the code equal to 100.
  2617. ASSERT_EQ(1, std::distance(range.first, range.second));
  2618. const uint8_t foo_expected[] = {
  2619. 0x0A, 0x0B, 0x0C, 0x0D
  2620. };
  2621. // Check if option is valid in terms of code and carried data.
  2622. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  2623. }
  2624. // Verify that specific option object is returned for standard
  2625. // option which has dedicated option class derived from Option.
  2626. TEST_F(Dhcp4ParserTest, stdOptionData) {
  2627. ConstElementPtr x;
  2628. std::map<std::string, std::string> params;
  2629. params["name"] = "nis-servers";
  2630. params["space"] = DHCP4_OPTION_SPACE;
  2631. // Option code 41 means nis-servers.
  2632. params["code"] = "41";
  2633. // Specify option values in a CSV (user friendly) format.
  2634. params["data"] = "192.0.2.10, 192.0.2.1, 192.0.2.3";
  2635. params["csv-format"] = "true";
  2636. std::string config = createConfigWithOption(params);
  2637. ConstElementPtr json;
  2638. ASSERT_NO_THROW(json = parseDHCP4(config));
  2639. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2640. checkResult(x, 0);
  2641. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  2642. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.5"));
  2643. ASSERT_TRUE(subnet);
  2644. OptionContainerPtr options =
  2645. subnet->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2646. ASSERT_TRUE(options);
  2647. ASSERT_EQ(1, options->size());
  2648. // Get the search index. Index #1 is to search using option code.
  2649. const OptionContainerTypeIndex& idx = options->get<1>();
  2650. // Get the options for specified index. Expecting one option to be
  2651. // returned but in theory we may have multiple options with the same
  2652. // code so we get the range.
  2653. std::pair<OptionContainerTypeIndex::const_iterator,
  2654. OptionContainerTypeIndex::const_iterator> range =
  2655. idx.equal_range(DHO_NIS_SERVERS);
  2656. // Expect single option with the code equal to NIS_SERVERS option code.
  2657. ASSERT_EQ(1, std::distance(range.first, range.second));
  2658. // The actual pointer to the option is held in the option field
  2659. // in the structure returned.
  2660. OptionPtr option = range.first->option_;
  2661. ASSERT_TRUE(option);
  2662. // Option object returned for here is expected to be Option6IA
  2663. // which is derived from Option. This class is dedicated to
  2664. // represent standard option IA_NA.
  2665. boost::shared_ptr<Option4AddrLst> option_addrs =
  2666. boost::dynamic_pointer_cast<Option4AddrLst>(option);
  2667. // If cast is unsuccessful than option returned was of a
  2668. // different type than Option6IA. This is wrong.
  2669. ASSERT_TRUE(option_addrs);
  2670. // Get addresses from the option.
  2671. Option4AddrLst::AddressContainer addrs = option_addrs->getAddresses();
  2672. // Verify that the addresses have been configured correctly.
  2673. ASSERT_EQ(3, addrs.size());
  2674. EXPECT_EQ("192.0.2.10", addrs[0].toText());
  2675. EXPECT_EQ("192.0.2.1", addrs[1].toText());
  2676. EXPECT_EQ("192.0.2.3", addrs[2].toText());
  2677. }
  2678. /// This test checks if Uint32Parser can really parse the whole range
  2679. /// and properly err of out of range values. As we can't call Uint32Parser
  2680. /// directly, we are exploiting the fact that it is used to parse global
  2681. /// parameter renew-timer and the results are stored in uint32_defaults.
  2682. /// We get the uint32_defaults using a getUint32Defaults functions which
  2683. /// is defined only to access the values from this test.
  2684. TEST_F(Dhcp4ParserTest, DISABLED_Uint32Parser) {
  2685. ConstElementPtr status;
  2686. // CASE 1: 0 - minimum value, should work
  2687. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2688. parseDHCP4("{\"renew-timer\": 0}")));
  2689. // returned value must be ok (0 is a proper value)
  2690. checkResult(status, 0);
  2691. /// @todo: check that the renew-timer is really 0
  2692. // CASE 2: 4294967295U (UINT_MAX) should work as well
  2693. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2694. parseDHCP4("{\"renew-timer\": 4294967295}")));
  2695. // returned value must be ok (0 is a proper value)
  2696. checkResult(status, 0);
  2697. /// @todo: check that the renew-timer is really 4294967295U
  2698. // CASE 3: 4294967296U (UINT_MAX + 1) should not work
  2699. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2700. parseJSON("{\"renew-timer\": 4294967296}")));
  2701. // returned value must be rejected (1 configuration error)
  2702. checkResult(status, 1);
  2703. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2704. // CASE 4: -1 (UINT_MIN -1 ) should not work
  2705. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2706. parseJSON("{\"renew-timer\": -1}")));
  2707. // returned value must be rejected (1 configuration error)
  2708. checkResult(status, 1);
  2709. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2710. }
  2711. // The goal of this test is to verify that the domain-search option
  2712. // can be set using domain names
  2713. TEST_F(Dhcp4ParserTest, domainSearchOption) {
  2714. // Create configuration.
  2715. std::map<std::string, std::string> params;
  2716. params["name"] = "domain-search";
  2717. params["space"] = DHCP4_OPTION_SPACE;
  2718. params["code"] = "119"; // DHO_DOMAIN_SEARCH
  2719. params["data"] = "mydomain.example.com, example.com";
  2720. params["csv-format"] = "true";
  2721. std::string config = createConfigWithOption(params);
  2722. EXPECT_TRUE(executeConfiguration(config, "parse configuration with a"
  2723. " domain-search option"));
  2724. }
  2725. // The goal of this test is to verify that the standard option can
  2726. // be configured to encapsulate multiple other options.
  2727. TEST_F(Dhcp4ParserTest, stdOptionDataEncapsulate) {
  2728. // The configuration is two stage process in this test.
  2729. // In the first stage we create definitions of suboptions
  2730. // that we will add to the base option.
  2731. // Let's create some dummy options: foo and foo2.
  2732. string config = "{ " + genIfaceConfig() + "," +
  2733. "\"valid-lifetime\": 4000,"
  2734. "\"rebind-timer\": 2000,"
  2735. "\"renew-timer\": 1000,"
  2736. "\"option-data\": [ {"
  2737. " \"name\": \"foo\","
  2738. " \"space\": \"vendor-encapsulated-options-space\","
  2739. " \"data\": \"1234\""
  2740. " },"
  2741. " {"
  2742. " \"name\": \"foo2\","
  2743. " \"space\": \"vendor-encapsulated-options-space\","
  2744. " \"data\": \"192.168.2.1\""
  2745. " } ],"
  2746. "\"option-def\": [ {"
  2747. " \"name\": \"foo\","
  2748. " \"code\": 1,"
  2749. " \"type\": \"uint32\","
  2750. " \"space\": \"vendor-encapsulated-options-space\""
  2751. " },"
  2752. " {"
  2753. " \"name\": \"foo2\","
  2754. " \"code\": 2,"
  2755. " \"type\": \"ipv4-address\","
  2756. " \"space\": \"vendor-encapsulated-options-space\""
  2757. " } ]"
  2758. "}";
  2759. ConstElementPtr json;
  2760. ASSERT_NO_THROW(json = parseDHCP4(config));
  2761. extractConfig(config);
  2762. ConstElementPtr status;
  2763. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2764. ASSERT_TRUE(status);
  2765. checkResult(status, 0);
  2766. CfgMgr::instance().clear();
  2767. // Once the definitions have been added we can configure the
  2768. // standard option #17. This option comprises an enterprise
  2769. // number and sub options. By convention (introduced in
  2770. // std_option_defs.h) option named 'vendor-opts'
  2771. // encapsulates the option space named 'vendor-opts-space'.
  2772. // We add our dummy options to this option space and thus
  2773. // they should be included as sub-options in the 'vendor-opts'
  2774. // option.
  2775. config = "{ " + genIfaceConfig() + "," +
  2776. "\"valid-lifetime\": 3000,"
  2777. "\"rebind-timer\": 2000,"
  2778. "\"renew-timer\": 1000,"
  2779. "\"option-data\": [ {"
  2780. " \"name\": \"vendor-encapsulated-options\","
  2781. " \"csv-format\": false"
  2782. " },"
  2783. " {"
  2784. " \"name\": \"foo\","
  2785. " \"space\": \"vendor-encapsulated-options-space\","
  2786. " \"data\": \"1234\""
  2787. " },"
  2788. " {"
  2789. " \"name\": \"foo2\","
  2790. " \"space\": \"vendor-encapsulated-options-space\","
  2791. " \"code\": 2,"
  2792. " \"data\": \"192.168.2.1\","
  2793. " \"csv-format\": true"
  2794. " } ],"
  2795. "\"option-def\": [ {"
  2796. " \"name\": \"foo\","
  2797. " \"code\": 1,"
  2798. " \"type\": \"uint32\","
  2799. " \"space\": \"vendor-encapsulated-options-space\""
  2800. " },"
  2801. " {"
  2802. " \"name\": \"foo2\","
  2803. " \"code\": 2,"
  2804. " \"type\": \"ipv4-address\","
  2805. " \"space\": \"vendor-encapsulated-options-space\""
  2806. " } ],"
  2807. "\"subnet4\": [ { "
  2808. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2809. " \"subnet\": \"192.0.2.0/24\""
  2810. " } ]"
  2811. "}";
  2812. ASSERT_NO_THROW(json = parseDHCP4(config));
  2813. extractConfig(config);
  2814. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2815. ASSERT_TRUE(status);
  2816. checkResult(status, 0);
  2817. // We should have one option available.
  2818. OptionContainerPtr options =
  2819. CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP4_OPTION_SPACE);
  2820. ASSERT_TRUE(options);
  2821. ASSERT_EQ(1, options->size());
  2822. // Get the option.
  2823. OptionDescriptor desc = CfgMgr::instance().getStagingCfg()->
  2824. getCfgOption()->get(DHCP4_OPTION_SPACE, DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2825. EXPECT_TRUE(desc.option_);
  2826. EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, desc.option_->getType());
  2827. // Option with the code 1 should be added as a sub-option.
  2828. OptionPtr option_foo = desc.option_->getOption(1);
  2829. ASSERT_TRUE(option_foo);
  2830. EXPECT_EQ(1, option_foo->getType());
  2831. // This option comprises a single uint32_t value thus it is
  2832. // represented by OptionInt<uint32_t> class. Let's get the
  2833. // object of this type.
  2834. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  2835. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  2836. ASSERT_TRUE(option_foo_uint32);
  2837. // Validate the value according to the configuration.
  2838. EXPECT_EQ(1234, option_foo_uint32->getValue());
  2839. // Option with the code 2 should be added as a sub-option.
  2840. OptionPtr option_foo2 = desc.option_->getOption(2);
  2841. ASSERT_TRUE(option_foo2);
  2842. EXPECT_EQ(2, option_foo2->getType());
  2843. // This option comprises the IPV4 address. Such option is
  2844. // represented by OptionCustom object.
  2845. OptionCustomPtr option_foo2_v4 =
  2846. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  2847. ASSERT_TRUE(option_foo2_v4);
  2848. // Get the IP address carried by this option and validate it.
  2849. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  2850. // Option with the code 3 should not be added.
  2851. EXPECT_FALSE(desc.option_->getOption(3));
  2852. }
  2853. // This test checks if vendor options can be specified in the config file
  2854. // (in hex format), and later retrieved
  2855. TEST_F(Dhcp4ParserTest, vendorOptionsHex) {
  2856. // This configuration string is to configure two options
  2857. // sharing the code 1 and belonging to the different vendor spaces.
  2858. // (different vendor-id values).
  2859. string config = "{ " + genIfaceConfig() + "," +
  2860. "\"valid-lifetime\": 4000,"
  2861. "\"rebind-timer\": 2000,"
  2862. "\"renew-timer\": 1000,"
  2863. "\"option-data\": [ {"
  2864. " \"name\": \"option-one\","
  2865. " \"space\": \"vendor-4491\"," // VENDOR_ID_CABLE_LABS = 4491
  2866. " \"code\": 100," // just a random code
  2867. " \"data\": \"ABCDEF0105\","
  2868. " \"csv-format\": false"
  2869. " },"
  2870. " {"
  2871. " \"name\": \"option-two\","
  2872. " \"space\": \"vendor-1234\","
  2873. " \"code\": 100,"
  2874. " \"data\": \"1234\","
  2875. " \"csv-format\": false"
  2876. " } ],"
  2877. "\"subnet4\": [ { "
  2878. " \"pools\": [ { \"pool\": \"192.0.2.1-192.0.2.10\" } ],"
  2879. " \"subnet\": \"192.0.2.0/24\""
  2880. " } ]"
  2881. "}";
  2882. ConstElementPtr json;
  2883. ASSERT_NO_THROW(json = parseDHCP4(config));
  2884. extractConfig(config);
  2885. ConstElementPtr status;
  2886. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2887. ASSERT_TRUE(status);
  2888. checkResult(status, 0);
  2889. // Try to get the option from the vendor space 4491
  2890. OptionDescriptor desc1 = CfgMgr::instance().getStagingCfg()->
  2891. getCfgOption()->get(VENDOR_ID_CABLE_LABS, 100);
  2892. ASSERT_TRUE(desc1.option_);
  2893. EXPECT_EQ(100, desc1.option_->getType());
  2894. // Try to get the option from the vendor space 1234
  2895. OptionDescriptor desc2 =
  2896. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(1234, 100);
  2897. ASSERT_TRUE(desc2.option_);
  2898. EXPECT_EQ(100, desc1.option_->getType());
  2899. // Try to get the non-existing option from the non-existing
  2900. // option space and expect that option is not returned.
  2901. OptionDescriptor desc3 =
  2902. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(5678, 100);
  2903. ASSERT_FALSE(desc3.option_);
  2904. }
  2905. // This test checks if vendor options can be specified in the config file,
  2906. // (in csv format), and later retrieved
  2907. TEST_F(Dhcp4ParserTest, vendorOptionsCsv) {
  2908. // This configuration string is to configure two options
  2909. // sharing the code 1 and belonging to the different vendor spaces.
  2910. // (different vendor-id values).
  2911. string config = "{ " + genIfaceConfig() + "," +
  2912. "\"valid-lifetime\": 4000,"
  2913. "\"rebind-timer\": 2000,"
  2914. "\"renew-timer\": 1000,"
  2915. "\"option-data\": [ {"
  2916. " \"name\": \"foo\","
  2917. " \"space\": \"vendor-4491\","
  2918. " \"code\": 100,"
  2919. " \"data\": \"this is a string vendor-opt\""
  2920. " } ],"
  2921. "\"option-def\": [ {"
  2922. " \"name\": \"foo\","
  2923. " \"code\": 100,"
  2924. " \"type\": \"string\","
  2925. " \"space\": \"vendor-4491\""
  2926. " } ],"
  2927. "\"subnet4\": [ { "
  2928. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2929. " \"subnet\": \"192.0.2.0/24\" "
  2930. " } ]"
  2931. "}";
  2932. ConstElementPtr json;
  2933. ASSERT_NO_THROW(json = parseDHCP4(config));
  2934. extractConfig(config);
  2935. ConstElementPtr status;
  2936. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2937. ASSERT_TRUE(status);
  2938. checkResult(status, 0);
  2939. // Try to get the option from the vendor space 4491
  2940. OptionDescriptor desc1 = CfgMgr::instance().getStagingCfg()->
  2941. getCfgOption()->get(VENDOR_ID_CABLE_LABS, 100);
  2942. ASSERT_TRUE(desc1.option_);
  2943. EXPECT_EQ(100, desc1.option_->getType());
  2944. // Try to get the non-existing option from the non-existing
  2945. // option space and expect that option is not returned.
  2946. OptionDescriptor desc2 =
  2947. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(5678, 100);
  2948. ASSERT_FALSE(desc2.option_);
  2949. }
  2950. // Tests of the hooks libraries configuration. All tests have the pre-
  2951. // condition (checked in the test fixture's SetUp() method) that no hooks
  2952. // libraries are loaded at the start of the tests.
  2953. // Helper function to return a configuration containing an arbitrary number
  2954. // of hooks libraries.
  2955. std::string
  2956. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  2957. // Create the first part of the configuration string.
  2958. string config =
  2959. "{ \"interfaces-config\": {"
  2960. " \"interfaces\": [ \"*\" ]"
  2961. "},"
  2962. "\"hooks-libraries\": [";
  2963. // Append the libraries (separated by commas if needed)
  2964. for (unsigned int i = 0; i < libraries.size(); ++i) {
  2965. if (i > 0) {
  2966. config += string(", ");
  2967. }
  2968. config += (string("{ \"library\": \"") + libraries[i] + string("\" }"));
  2969. }
  2970. // Append the remainder of the configuration.
  2971. config += string(
  2972. "],"
  2973. "\"valid-lifetime\": 4000,"
  2974. "\"rebind-timer\": 2000,"
  2975. "\"renew-timer\": 1000,"
  2976. "\"option-data\": [ {"
  2977. " \"name\": \"dhcp-message\","
  2978. " \"data\": \"ABCDEF0105\","
  2979. " \"csv-format\": false"
  2980. " },"
  2981. " {"
  2982. " \"name\": \"foo\","
  2983. " \"space\": \"isc\","
  2984. " \"data\": \"1234\""
  2985. " } ],"
  2986. "\"option-def\": [ {"
  2987. " \"name\": \"foo\","
  2988. " \"code\": 56,"
  2989. " \"type\": \"uint32\","
  2990. " \"space\": \"isc\""
  2991. " } ],"
  2992. "\"subnet4\": [ { "
  2993. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  2994. " \"subnet\": \"192.0.2.0/24\""
  2995. " } ]"
  2996. "}");
  2997. return (config);
  2998. }
  2999. // Convenience function for creating hooks library configuration with one or
  3000. // two character string constants.
  3001. std::string
  3002. buildHooksLibrariesConfig(const char* library1 = NULL,
  3003. const char* library2 = NULL) {
  3004. std::vector<std::string> libraries;
  3005. if (library1 != NULL) {
  3006. libraries.push_back(string(library1));
  3007. if (library2 != NULL) {
  3008. libraries.push_back(string(library2));
  3009. }
  3010. }
  3011. return (buildHooksLibrariesConfig(libraries));
  3012. }
  3013. // The goal of this test is to verify the configuration of hooks libraries if
  3014. // none are specified.
  3015. TEST_F(Dhcp4ParserTest, NoHooksLibraries) {
  3016. // Parse a configuration containing no names.
  3017. string config = buildHooksLibrariesConfig();
  3018. if (!executeConfiguration(config,
  3019. "set configuration with no hooks libraries")) {
  3020. FAIL() << "Unable to execute configuration";
  3021. } else {
  3022. // No libraries should be loaded at the end of the test.
  3023. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  3024. EXPECT_TRUE(libraries.empty());
  3025. }
  3026. }
  3027. // Verify parsing fails with one library that will fail validation.
  3028. TEST_F(Dhcp4ParserTest, InvalidLibrary) {
  3029. // Parse a configuration containing a failing library.
  3030. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  3031. ConstElementPtr json;
  3032. ASSERT_NO_THROW(json = parseDHCP4(config));
  3033. ConstElementPtr status;
  3034. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3035. // The status object must not be NULL
  3036. ASSERT_TRUE(status);
  3037. // Returned value should not be 0
  3038. comment_ = parseAnswer(rcode_, status);
  3039. EXPECT_NE(0, rcode_);
  3040. }
  3041. // Verify the configuration of hooks libraries with two being specified.
  3042. TEST_F(Dhcp4ParserTest, LibrariesSpecified) {
  3043. // Marker files should not be present.
  3044. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  3045. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  3046. // Set up the configuration with two libraries and load them.
  3047. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  3048. CALLOUT_LIBRARY_2);
  3049. ASSERT_TRUE(executeConfiguration(config,
  3050. "load two valid libraries"));
  3051. // Expect two libraries to be loaded in the correct order (load marker file
  3052. // is present, no unload marker file).
  3053. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  3054. ASSERT_EQ(2, libraries.size());
  3055. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  3056. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  3057. // Commit the changes so as we get the fresh configuration for the
  3058. // second part of this test.
  3059. CfgMgr::instance().commit();
  3060. // Unload the libraries. The load file should not have changed, but
  3061. // the unload one should indicate the unload() functions have been run.
  3062. config = buildHooksLibrariesConfig();
  3063. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  3064. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  3065. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  3066. // Expect the hooks system to say that none are loaded.
  3067. libraries = HooksManager::getLibraryNames();
  3068. EXPECT_TRUE(libraries.empty());
  3069. }
  3070. // This test verifies that it is possible to select subset of interfaces
  3071. // on which server should listen.
  3072. TEST_F(Dhcp4ParserTest, selectedInterfaces) {
  3073. IfaceMgrTestConfig test_config(true);
  3074. ConstElementPtr x;
  3075. string config = "{ \"interfaces-config\": {"
  3076. " \"interfaces\": [ \"eth0\", \"eth1\" ]"
  3077. "},"
  3078. "\"rebind-timer\": 2000, "
  3079. "\"renew-timer\": 1000, "
  3080. "\"valid-lifetime\": 4000 }";
  3081. ConstElementPtr json;
  3082. ASSERT_NO_THROW(json = parseDHCP4(config));
  3083. extractConfig(config);
  3084. ConstElementPtr status;
  3085. // Make sure the config manager is clean and there is no hanging
  3086. // interface configuration.
  3087. EXPECT_FALSE(test_config.socketOpen("eth0", AF_INET));
  3088. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET));
  3089. // Apply configuration.
  3090. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3091. ASSERT_TRUE(status);
  3092. checkResult(status, 0);
  3093. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET, 10000);
  3094. // eth0 and eth1 were explicitly selected. eth2 was not.
  3095. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET));
  3096. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET));
  3097. }
  3098. // This test verifies that it is possible to configure the server in such a way
  3099. // that it listens on all interfaces.
  3100. TEST_F(Dhcp4ParserTest, allInterfaces) {
  3101. IfaceMgrTestConfig test_config(true);
  3102. ConstElementPtr x;
  3103. // This configuration specifies two interfaces on which server should listen
  3104. // but it also includes asterisk. The asterisk switches server into the
  3105. // mode when it listens on all interfaces regardless of what interface names
  3106. // were specified in the "interfaces" parameter.
  3107. string config = "{ \"interfaces-config\": {"
  3108. " \"interfaces\": [ \"eth0\", \"*\", \"eth1\" ]"
  3109. "},"
  3110. "\"rebind-timer\": 2000, "
  3111. "\"renew-timer\": 1000, "
  3112. "\"valid-lifetime\": 4000 }";
  3113. ConstElementPtr json;
  3114. ASSERT_NO_THROW(json = parseDHCP4(config));
  3115. extractConfig(config);
  3116. ConstElementPtr status;
  3117. // Make sure there is no old configuration.
  3118. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET));
  3119. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET));
  3120. // Apply configuration.
  3121. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3122. ASSERT_TRUE(status);
  3123. checkResult(status, 0);
  3124. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET, 10000);
  3125. // All interfaces should be now active.
  3126. ASSERT_TRUE(test_config.socketOpen("eth0", AF_INET));
  3127. ASSERT_TRUE(test_config.socketOpen("eth1", AF_INET));
  3128. }
  3129. // This test verifies that it is possible to select subset of interfaces
  3130. // and addresses.
  3131. TEST_F(Dhcp4ParserTest, selectedInterfacesAndAddresses) {
  3132. IfaceMgrTestConfig test_config(true);
  3133. ConstElementPtr x;
  3134. string config = "{ \"interfaces-config\": {"
  3135. " \"interfaces\": [ \"eth0/10.0.0.1\", \"eth1/192.0.2.3\" ]"
  3136. "},"
  3137. "\"rebind-timer\": 2000, "
  3138. "\"renew-timer\": 1000, "
  3139. "\"valid-lifetime\": 4000 }";
  3140. ConstElementPtr json;
  3141. ASSERT_NO_THROW(json = parseDHCP4(config));
  3142. ConstElementPtr status;
  3143. // Make sure the config manager is clean and there is no hanging
  3144. // interface configuration.
  3145. ASSERT_FALSE(test_config.socketOpen("eth0", "10.0.0.1"));
  3146. ASSERT_FALSE(test_config.socketOpen("eth1", "192.0.2.3"));
  3147. ASSERT_FALSE(test_config.socketOpen("eth1", "192.0.2.5"));
  3148. // Apply configuration.
  3149. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3150. ASSERT_TRUE(status);
  3151. checkResult(status, 0);
  3152. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET, 10000);
  3153. // An address on eth0 was selected
  3154. EXPECT_TRUE(test_config.socketOpen("eth0", "10.0.0.1"));
  3155. // The 192.0.2.3 address on eth1 was selected.
  3156. EXPECT_TRUE(test_config.socketOpen("eth1", "192.0.2.3"));
  3157. // The 192.0.2.5 was not selected, thus the socket should not
  3158. // be bound to this address.
  3159. EXPECT_FALSE(test_config.socketOpen("eth1", "192.0.2.5"));
  3160. }
  3161. // This test checks the ability of the server to parse a configuration
  3162. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  3163. TEST_F(Dhcp4ParserTest, d2ClientConfig) {
  3164. ConstElementPtr status;
  3165. // Verify that the D2 configuration can be fetched and is set to disabled.
  3166. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  3167. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  3168. // Verify that the convenience method agrees.
  3169. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  3170. string config_str = "{ " + genIfaceConfig() + "," +
  3171. "\"rebind-timer\": 2000, "
  3172. "\"renew-timer\": 1000, "
  3173. "\"subnet4\": [ { "
  3174. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3175. " \"subnet\": \"192.0.2.0/24\" } ],"
  3176. " \"dhcp-ddns\" : {"
  3177. " \"enable-updates\" : true, "
  3178. " \"server-ip\" : \"192.168.2.1\", "
  3179. " \"server-port\" : 777, "
  3180. " \"sender-ip\" : \"192.168.2.2\", "
  3181. " \"sender-port\" : 778, "
  3182. " \"max-queue-size\" : 2048, "
  3183. " \"ncr-protocol\" : \"UDP\", "
  3184. " \"ncr-format\" : \"JSON\", "
  3185. " \"always-include-fqdn\" : true, "
  3186. " \"override-no-update\" : true, "
  3187. " \"override-client-update\" : true, "
  3188. " \"replace-client-name\" : \"when-present\", "
  3189. " \"generated-prefix\" : \"test.prefix\", "
  3190. " \"qualifying-suffix\" : \"test.suffix.\" },"
  3191. "\"valid-lifetime\": 4000 }";
  3192. // Convert the JSON string to configuration elements.
  3193. ConstElementPtr config;
  3194. ASSERT_NO_THROW(config = parseDHCP4(config_str, true));
  3195. extractConfig(config_str);
  3196. // Pass the configuration in for parsing.
  3197. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, config));
  3198. // check if returned status is OK
  3199. checkResult(status, 0);
  3200. // Verify that DHCP-DDNS updating is enabled.
  3201. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  3202. // Verify that the D2 configuration can be retrieved.
  3203. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  3204. ASSERT_TRUE(d2_client_config);
  3205. // Verify that the configuration values are correct.
  3206. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  3207. EXPECT_EQ("192.168.2.1", d2_client_config->getServerIp().toText());
  3208. EXPECT_EQ(777, d2_client_config->getServerPort());
  3209. EXPECT_EQ("192.168.2.2", d2_client_config->getSenderIp().toText());
  3210. EXPECT_EQ(778, d2_client_config->getSenderPort());
  3211. EXPECT_EQ(2048, d2_client_config->getMaxQueueSize());
  3212. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  3213. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  3214. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  3215. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  3216. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  3217. EXPECT_EQ(D2ClientConfig::RCM_WHEN_PRESENT, d2_client_config->getReplaceClientNameMode());
  3218. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  3219. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  3220. }
  3221. // This test checks the ability of the server to handle a configuration
  3222. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  3223. TEST_F(Dhcp4ParserTest, invalidD2ClientConfig) {
  3224. ConstElementPtr status;
  3225. // Configuration string with an invalid D2 client config,
  3226. // "server-ip" is invalid.
  3227. string config_str = "{ " + genIfaceConfig() + "," +
  3228. "\"rebind-timer\": 2000, "
  3229. "\"renew-timer\": 1000, "
  3230. "\"subnet4\": [ { "
  3231. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3232. " \"subnet\": \"192.0.2.0/24\" } ],"
  3233. " \"dhcp-ddns\" : {"
  3234. " \"enable-updates\" : true, "
  3235. " \"server-ip\" : \"bogus-value\", "
  3236. " \"server-port\" : 5301, "
  3237. " \"ncr-protocol\" : \"UDP\", "
  3238. " \"ncr-format\" : \"JSON\", "
  3239. " \"always-include-fqdn\" : true, "
  3240. " \"override-no-update\" : true, "
  3241. " \"override-client-update\" : true, "
  3242. " \"replace-client-name\" : \"when-present\", "
  3243. " \"generated-prefix\" : \"test.prefix\", "
  3244. " \"qualifying-suffix\" : \"test.suffix.\" },"
  3245. "\"valid-lifetime\": 4000 }";
  3246. // Convert the JSON string to configuration elements.
  3247. ConstElementPtr config;
  3248. ASSERT_NO_THROW(config = parseDHCP4(config_str));
  3249. // Configuration should not throw, but should fail.
  3250. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, config));
  3251. // check if returned status is failed.
  3252. checkResult(status, 1);
  3253. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3254. // Verify that the D2 configuration can be fetched and is set to disabled.
  3255. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  3256. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  3257. // Verify that the convenience method agrees.
  3258. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  3259. }
  3260. // This test checks if it is possible to specify relay information
  3261. TEST_F(Dhcp4ParserTest, subnetRelayInfo) {
  3262. ConstElementPtr status;
  3263. // A config with relay information.
  3264. string config = "{ " + genIfaceConfig() + "," +
  3265. "\"rebind-timer\": 2000, "
  3266. "\"renew-timer\": 1000, "
  3267. "\"subnet4\": [ { "
  3268. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3269. " \"renew-timer\": 1, "
  3270. " \"rebind-timer\": 2, "
  3271. " \"valid-lifetime\": 4,"
  3272. " \"relay\": { "
  3273. " \"ip-address\": \"192.0.2.123\""
  3274. " },"
  3275. " \"subnet\": \"192.0.2.0/24\" } ],"
  3276. "\"valid-lifetime\": 4000 }";
  3277. ConstElementPtr json;
  3278. ASSERT_NO_THROW(json = parseDHCP4(config));
  3279. extractConfig(config);
  3280. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3281. // returned value should be 0 (configuration success)
  3282. checkResult(status, 0);
  3283. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  3284. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  3285. ASSERT_TRUE(subnet);
  3286. EXPECT_EQ("192.0.2.123", subnet->getRelayInfo().addr_.toText());
  3287. }
  3288. // Goal of this test is to verify that multiple subnets can be configured
  3289. // with defined client classes.
  3290. TEST_F(Dhcp4ParserTest, classifySubnets) {
  3291. ConstElementPtr x;
  3292. string config = "{ " + genIfaceConfig() + "," +
  3293. "\"rebind-timer\": 2000, "
  3294. "\"renew-timer\": 1000, "
  3295. "\"subnet4\": [ { "
  3296. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3297. " \"subnet\": \"192.0.2.0/24\", "
  3298. " \"client-class\": \"alpha\" "
  3299. " },"
  3300. " {"
  3301. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  3302. " \"subnet\": \"192.0.3.0/24\", "
  3303. " \"client-class\": \"beta\" "
  3304. " },"
  3305. " {"
  3306. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  3307. " \"subnet\": \"192.0.4.0/24\", "
  3308. " \"client-class\": \"gamma\" "
  3309. " },"
  3310. " {"
  3311. " \"pools\": [ { \"pool\": \"192.0.5.101 - 192.0.5.150\" } ],"
  3312. " \"subnet\": \"192.0.5.0/24\" "
  3313. " } ],"
  3314. "\"valid-lifetime\": 4000 }";
  3315. ConstElementPtr json;
  3316. ASSERT_NO_THROW(json = parseDHCP4(config));
  3317. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3318. checkResult(x, 0);
  3319. const Subnet4Collection* subnets =
  3320. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->getAll();
  3321. ASSERT_TRUE(subnets);
  3322. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  3323. // Let's check if client belonging to alpha class is supported in subnet[0]
  3324. // and not supported in any other subnet (except subnet[3], which allows
  3325. // everyone).
  3326. ClientClasses classes;
  3327. classes.insert("alpha");
  3328. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  3329. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3330. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3331. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3332. // Let's check if client belonging to beta class is supported in subnet[1]
  3333. // and not supported in any other subnet (except subnet[3], which allows
  3334. // everyone).
  3335. classes.clear();
  3336. classes.insert("beta");
  3337. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3338. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  3339. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3340. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3341. // Let's check if client belonging to gamma class is supported in subnet[2]
  3342. // and not supported in any other subnet (except subnet[3], which allows
  3343. // everyone).
  3344. classes.clear();
  3345. classes.insert("gamma");
  3346. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3347. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3348. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  3349. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3350. // Let's check if client belonging to some other class (not mentioned in
  3351. // the config) is supported only in subnet[3], which allows everyone.
  3352. classes.clear();
  3353. classes.insert("delta");
  3354. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3355. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3356. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3357. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3358. // Finally, let's check class-less client. He should be allowed only in
  3359. // the last subnet, which does not have any class restrictions.
  3360. classes.clear();
  3361. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3362. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3363. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3364. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3365. }
  3366. // This test verifies that the host reservations can be specified for
  3367. // respective IPv4 subnets.
  3368. TEST_F(Dhcp4ParserTest, reservations) {
  3369. ConstElementPtr x;
  3370. string config = "{ " + genIfaceConfig() + "," +
  3371. "\"rebind-timer\": 2000, "
  3372. "\"renew-timer\": 1000, "
  3373. "\"subnet4\": [ "
  3374. " { "
  3375. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3376. " \"subnet\": \"192.0.2.0/24\", "
  3377. " \"id\": 123,"
  3378. " \"reservations\": ["
  3379. " ]"
  3380. " },"
  3381. " {"
  3382. " \"reservations\": ["
  3383. " {"
  3384. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  3385. " \"ip-address\": \"192.0.3.112\","
  3386. " \"hostname\": \"\","
  3387. " \"option-data\": ["
  3388. " {"
  3389. " \"name\": \"name-servers\","
  3390. " \"data\": \"192.0.3.15\""
  3391. " },"
  3392. " {"
  3393. " \"name\": \"default-ip-ttl\","
  3394. " \"data\": \"32\""
  3395. " }"
  3396. " ]"
  3397. " },"
  3398. " {"
  3399. " \"hw-address\": \"01:02:03:04:05:06\","
  3400. " \"ip-address\": \"192.0.3.120\","
  3401. " \"hostname\": \"\","
  3402. " \"option-data\": ["
  3403. " {"
  3404. " \"name\": \"name-servers\","
  3405. " \"data\": \"192.0.3.95\""
  3406. " },"
  3407. " {"
  3408. " \"name\": \"default-ip-ttl\","
  3409. " \"data\": \"11\""
  3410. " }"
  3411. " ]"
  3412. " }"
  3413. " ],"
  3414. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  3415. " \"subnet\": \"192.0.3.0/24\", "
  3416. " \"id\": 234"
  3417. " },"
  3418. " {"
  3419. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  3420. " \"subnet\": \"192.0.4.0/24\","
  3421. " \"id\": 542,"
  3422. " \"reservations\": ["
  3423. " {"
  3424. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3425. " \"ip-address\": \"192.0.4.101\","
  3426. " \"hostname\": \"\","
  3427. " \"option-data\": ["
  3428. " {"
  3429. " \"name\": \"name-servers\","
  3430. " \"data\": \"192.0.4.11\""
  3431. " },"
  3432. " {"
  3433. " \"name\": \"default-ip-ttl\","
  3434. " \"data\": \"95\""
  3435. " }"
  3436. " ]"
  3437. " },"
  3438. " {"
  3439. " \"circuit-id\": \"060504030201\","
  3440. " \"ip-address\": \"192.0.4.102\","
  3441. " \"hostname\": \"\""
  3442. " },"
  3443. " {"
  3444. " \"client-id\": \"05:01:02:03:04:05:06\","
  3445. " \"ip-address\": \"192.0.4.103\","
  3446. " \"hostname\": \"\""
  3447. " }"
  3448. " ]"
  3449. " } ],"
  3450. "\"valid-lifetime\": 4000 }";
  3451. ConstElementPtr json;
  3452. ASSERT_NO_THROW(json = parseDHCP4(config));
  3453. extractConfig(config);
  3454. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3455. checkResult(x, 0);
  3456. // Make sure all subnets have been successfully configured. There is no
  3457. // need to sanity check the subnet properties because it should have
  3458. // been already tested by other tests.
  3459. const Subnet4Collection* subnets =
  3460. CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->getAll();
  3461. ASSERT_TRUE(subnets);
  3462. ASSERT_EQ(3, subnets->size());
  3463. // Hosts configuration must be available.
  3464. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3465. ASSERT_TRUE(hosts_cfg);
  3466. // Let's create an object holding hardware address of the host having
  3467. // a reservation in the subnet having id of 234. For simplicity the
  3468. // address is a collection of numbers from 1 to 6.
  3469. std::vector<uint8_t> hwaddr_vec;
  3470. for (unsigned int i = 1; i < 7; ++i) {
  3471. hwaddr_vec.push_back(static_cast<uint8_t>(i));
  3472. }
  3473. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  3474. // Retrieve the reservation and sanity check the address reserved.
  3475. ConstHostPtr host = hosts_cfg->get4(234, hwaddr);
  3476. ASSERT_TRUE(host);
  3477. EXPECT_EQ("192.0.3.120", host->getIPv4Reservation().toText());
  3478. // This reservation should be solely assigned to the subnet 234,
  3479. // and not to other two.
  3480. EXPECT_FALSE(hosts_cfg->get4(123, hwaddr));
  3481. EXPECT_FALSE(hosts_cfg->get4(542, hwaddr));
  3482. // Check that options are assigned correctly.
  3483. Option4AddrLstPtr opt_dns =
  3484. retrieveOption<Option4AddrLstPtr>(*host, DHO_NAME_SERVERS);
  3485. ASSERT_TRUE(opt_dns);
  3486. Option4AddrLst::AddressContainer dns_addrs = opt_dns->getAddresses();
  3487. ASSERT_EQ(1, dns_addrs.size());
  3488. EXPECT_EQ("192.0.3.95", dns_addrs[0].toText());
  3489. OptionUint8Ptr opt_ttl =
  3490. retrieveOption<OptionUint8Ptr>(*host, DHO_DEFAULT_IP_TTL);
  3491. ASSERT_TRUE(opt_ttl);
  3492. EXPECT_EQ(11, static_cast<int>(opt_ttl->getValue()));
  3493. // Do the same test for the DUID based reservation.
  3494. std::vector<uint8_t> duid_vec;
  3495. for (unsigned int i = 1; i < 0xb; ++i) {
  3496. duid_vec.push_back(static_cast<uint8_t>(i));
  3497. }
  3498. DuidPtr duid(new DUID(duid_vec));
  3499. host = hosts_cfg->get4(234, HWAddrPtr(), duid);
  3500. ASSERT_TRUE(host);
  3501. EXPECT_EQ("192.0.3.112", host->getIPv4Reservation().toText());
  3502. EXPECT_FALSE(hosts_cfg->get4(123, HWAddrPtr(), duid));
  3503. EXPECT_FALSE(hosts_cfg->get4(542, HWAddrPtr(), duid));
  3504. // Check that options are assigned correctly.
  3505. opt_dns = retrieveOption<Option4AddrLstPtr>(*host, DHO_NAME_SERVERS);
  3506. ASSERT_TRUE(opt_dns);
  3507. dns_addrs = opt_dns->getAddresses();
  3508. ASSERT_EQ(1, dns_addrs.size());
  3509. EXPECT_EQ("192.0.3.15", dns_addrs[0].toText());
  3510. opt_ttl = retrieveOption<OptionUint8Ptr>(*host, DHO_DEFAULT_IP_TTL);
  3511. ASSERT_TRUE(opt_ttl);
  3512. EXPECT_EQ(32, static_cast<int>(opt_ttl->getValue()));
  3513. // The circuit-id used for one of the reservations in the subnet 542
  3514. // consists of numbers from 6 to 1. So, let's just reverse the order
  3515. // of the address from the previous test.
  3516. std::vector<uint8_t> circuit_id(hwaddr_vec.rbegin(), hwaddr_vec.rend());
  3517. host = hosts_cfg->get4(542, Host::IDENT_CIRCUIT_ID, &circuit_id[0],
  3518. circuit_id.size());
  3519. EXPECT_TRUE(host);
  3520. EXPECT_EQ("192.0.4.102", host->getIPv4Reservation().toText());
  3521. // This reservation must not belong to other subnets.
  3522. EXPECT_FALSE(hosts_cfg->get4(123, Host::IDENT_CIRCUIT_ID,
  3523. &circuit_id[0], circuit_id.size()));
  3524. EXPECT_FALSE(hosts_cfg->get4(234, Host::IDENT_CIRCUIT_ID,
  3525. &circuit_id[0], circuit_id.size()));
  3526. // Repeat the test for the DUID based reservation in this subnet.
  3527. duid.reset(new DUID(std::vector<uint8_t>(duid_vec.rbegin(),
  3528. duid_vec.rend())));
  3529. host = hosts_cfg->get4(542, HWAddrPtr(), duid);
  3530. ASSERT_TRUE(host);
  3531. EXPECT_EQ("192.0.4.101", host->getIPv4Reservation().toText());
  3532. EXPECT_FALSE(hosts_cfg->get4(123, HWAddrPtr(), duid));
  3533. EXPECT_FALSE(hosts_cfg->get4(234, HWAddrPtr(), duid));
  3534. // Check that options are assigned correctly.
  3535. opt_dns = retrieveOption<Option4AddrLstPtr>(*host, DHO_NAME_SERVERS);
  3536. ASSERT_TRUE(opt_dns);
  3537. dns_addrs = opt_dns->getAddresses();
  3538. ASSERT_EQ(1, dns_addrs.size());
  3539. EXPECT_EQ("192.0.4.11", dns_addrs[0].toText());
  3540. opt_ttl = retrieveOption<OptionUint8Ptr>(*host, DHO_DEFAULT_IP_TTL);
  3541. ASSERT_TRUE(opt_ttl);
  3542. EXPECT_EQ(95, static_cast<int>(opt_ttl->getValue()));
  3543. // Check that we can find reservation using client identifier.
  3544. ClientIdPtr client_id = ClientId::fromText("05:01:02:03:04:05:06");
  3545. host = hosts_cfg->get4(542, Host::IDENT_CLIENT_ID, &client_id->getDuid()[0],
  3546. client_id->getDuid().size());
  3547. ASSERT_TRUE(host);
  3548. EXPECT_EQ("192.0.4.103", host->getIPv4Reservation().toText());
  3549. // But this reservation should not be returned for other subnet.
  3550. host = hosts_cfg->get4(234, Host::IDENT_CLIENT_ID, &client_id->getDuid()[0],
  3551. client_id->getDuid().size());
  3552. EXPECT_FALSE(host);
  3553. }
  3554. // This test checks that it is possible to configure option data for a
  3555. // host using a user defined option format.
  3556. TEST_F(Dhcp4ParserTest, reservationWithOptionDefinition) {
  3557. ConstElementPtr x;
  3558. // The following configuration contains host declaration in which
  3559. // a non-standard option is used. This option has option definition
  3560. // specified in the configuration.
  3561. string config = "{ " + genIfaceConfig() + "," +
  3562. "\"rebind-timer\": 2000, "
  3563. "\"renew-timer\": 1000, "
  3564. "\"option-def\": [ {"
  3565. " \"name\": \"foo\","
  3566. " \"code\": 100,"
  3567. " \"type\": \"uint32\","
  3568. " \"space\": \"isc\""
  3569. "} ],"
  3570. "\"subnet4\": [ "
  3571. " {"
  3572. " \"reservations\": ["
  3573. " {"
  3574. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  3575. " \"ip-address\": \"192.0.3.112\","
  3576. " \"option-data\": ["
  3577. " {"
  3578. " \"name\": \"foo\","
  3579. " \"data\": \"123\","
  3580. " \"space\": \"isc\""
  3581. " }"
  3582. " ]"
  3583. " }"
  3584. " ],"
  3585. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  3586. " \"subnet\": \"192.0.3.0/24\", "
  3587. " \"id\": 234"
  3588. " } ],"
  3589. "\"valid-lifetime\": 4000"
  3590. "}";
  3591. ConstElementPtr json;
  3592. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  3593. extractConfig(config);
  3594. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3595. checkResult(x, 0);
  3596. // Hosts configuration must be available.
  3597. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3598. ASSERT_TRUE(hosts_cfg);
  3599. // Let's create an object holding DUID of the host. For simplicity the
  3600. // address is a collection of numbers from 1 to A.
  3601. std::vector<uint8_t> duid_vec;
  3602. for (unsigned int i = 1; i < 0xB; ++i) {
  3603. duid_vec.push_back(static_cast<uint8_t>(i));
  3604. }
  3605. DuidPtr duid(new DUID(duid_vec));
  3606. // Retrieve the reservation and sanity check the address reserved.
  3607. ConstHostPtr host = hosts_cfg->get4(234, HWAddrPtr(), duid);
  3608. ASSERT_TRUE(host);
  3609. EXPECT_EQ("192.0.3.112", host->getIPv4Reservation().toText());
  3610. // Check if the option has been parsed.
  3611. OptionUint32Ptr opt_foo = retrieveOption<OptionUint32Ptr>(*host, "isc",
  3612. 100);
  3613. ASSERT_TRUE(opt_foo);
  3614. EXPECT_EQ(100, opt_foo->getType());
  3615. EXPECT_EQ(123, opt_foo->getValue());
  3616. }
  3617. // This test verifies that the bogus host reservation would trigger a
  3618. // server configuration error.
  3619. TEST_F(Dhcp4ParserTest, reservationBogus) {
  3620. // Case 1: misspelled hw-address parameter.
  3621. ConstElementPtr x;
  3622. string config = "{ " + genIfaceConfig() + "," +
  3623. "\"rebind-timer\": 2000, "
  3624. "\"renew-timer\": 1000, "
  3625. "\"subnet4\": [ "
  3626. " { "
  3627. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  3628. " \"subnet\": \"192.0.4.0/24\","
  3629. " \"id\": 542,"
  3630. " \"reservations\": ["
  3631. " {"
  3632. " \"hw-addre\": \"06:05:04:03:02:01\","
  3633. " \"ip-address\": \"192.0.4.102\","
  3634. " \"hostname\": \"\""
  3635. " }"
  3636. " ]"
  3637. " } ],"
  3638. "\"valid-lifetime\": 4000 }";
  3639. ConstElementPtr json;
  3640. ASSERT_NO_THROW(json = parseJSON(config));
  3641. CfgMgr::instance().clear();
  3642. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3643. checkResult(x, 1);
  3644. EXPECT_THROW(parseDHCP4(config), Dhcp4ParseError);
  3645. // Case 2: DUID and HW Address both specified.
  3646. config = "{ " + genIfaceConfig() + "," +
  3647. "\"rebind-timer\": 2000, "
  3648. "\"renew-timer\": 1000, "
  3649. "\"subnet4\": [ "
  3650. " { "
  3651. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  3652. " \"subnet\": \"192.0.4.0/24\","
  3653. " \"id\": 542,"
  3654. " \"reservations\": ["
  3655. " {"
  3656. " \"duid\": \"01:02:03:04:05:06\","
  3657. " \"hw-address\": \"06:05:04:03:02:01\","
  3658. " \"ip-address\": \"192.0.4.102\","
  3659. " \"hostname\": \"\""
  3660. " }"
  3661. " ]"
  3662. " } ],"
  3663. "\"valid-lifetime\": 4000 }";
  3664. ASSERT_NO_THROW(json = parseDHCP4(config));
  3665. // Remove existing configuration, if any.
  3666. CfgMgr::instance().clear();
  3667. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3668. checkResult(x, 1);
  3669. // Case 3: Neither ip address nor hostname specified.
  3670. config = "{ " + genIfaceConfig() + "," +
  3671. "\"rebind-timer\": 2000, "
  3672. "\"renew-timer\": 1000, "
  3673. "\"subnet4\": [ "
  3674. " { "
  3675. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  3676. " \"subnet\": \"192.0.4.0/24\","
  3677. " \"id\": 542,"
  3678. " \"reservations\": ["
  3679. " {"
  3680. " \"hw-address\": \"06:05:04:03:02:01\""
  3681. " }"
  3682. " ]"
  3683. " } ],"
  3684. "\"valid-lifetime\": 4000 }";
  3685. ASSERT_NO_THROW(json = parseDHCP4(config));
  3686. // Remove existing configuration, if any.
  3687. CfgMgr::instance().clear();
  3688. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3689. checkResult(x, 1);
  3690. // Case 4: Broken specification of option data.
  3691. config = "{ " + genIfaceConfig() + "," +
  3692. "\"rebind-timer\": 2000, "
  3693. "\"renew-timer\": 1000, "
  3694. "\"subnet4\": [ "
  3695. " { "
  3696. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  3697. " \"subnet\": \"192.0.4.0/24\","
  3698. " \"id\": 542,"
  3699. " \"reservations\": ["
  3700. " {"
  3701. " \"hw-address\": \"06:05:04:03:02:01\","
  3702. " \"option-data\": ["
  3703. " {"
  3704. " \"name\": \"name-servers\","
  3705. " \"data\": \"bogus-ip-address\""
  3706. " }"
  3707. " ]"
  3708. " }"
  3709. " ]"
  3710. " } ],"
  3711. "\"valid-lifetime\": 4000 }";
  3712. ASSERT_NO_THROW(json = parseDHCP4(config));
  3713. // Remove existing configuration, if any.
  3714. CfgMgr::instance().clear();
  3715. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  3716. checkResult(x, 1);
  3717. }
  3718. /// The goal of this test is to verify that Host Reservation modes can be
  3719. /// specified on a per-subnet basis.
  3720. TEST_F(Dhcp4ParserTest, hostReservationPerSubnet) {
  3721. /// - Configuration:
  3722. /// - only addresses (no prefixes)
  3723. /// - 4 subnets with:
  3724. /// - 192.0.2.0/24 (all reservations enabled)
  3725. /// - 192.0.3.0/24 (out-of-pool reservations)
  3726. /// - 192.0.4.0/24 (reservations disabled)
  3727. /// - 192.0.5.0/24 (reservations not specified)
  3728. const char* hr_config =
  3729. "{ "
  3730. "\"rebind-timer\": 2000, "
  3731. "\"renew-timer\": 1000, "
  3732. "\"subnet4\": [ { "
  3733. " \"pools\": [ { \"pool\": \"192.0.2.0/24\" } ],"
  3734. " \"subnet\": \"192.0.2.0/24\", "
  3735. " \"reservation-mode\": \"all\""
  3736. " },"
  3737. " {"
  3738. " \"pools\": [ { \"pool\": \"192.0.3.0/24\" } ],"
  3739. " \"subnet\": \"192.0.3.0/24\", "
  3740. " \"reservation-mode\": \"out-of-pool\""
  3741. " },"
  3742. " {"
  3743. " \"pools\": [ { \"pool\": \"192.0.4.0/24\" } ],"
  3744. " \"subnet\": \"192.0.4.0/24\", "
  3745. " \"reservation-mode\": \"disabled\""
  3746. " },"
  3747. " {"
  3748. " \"pools\": [ { \"pool\": \"192.0.5.0/24\" } ],"
  3749. " \"subnet\": \"192.0.5.0/24\""
  3750. " } ],"
  3751. "\"valid-lifetime\": 4000 }";
  3752. ConstElementPtr json;
  3753. ASSERT_NO_THROW(json = parseDHCP4(hr_config));
  3754. extractConfig(hr_config);
  3755. ConstElementPtr result;
  3756. EXPECT_NO_THROW(result = configureDhcp4Server(*srv_, json));
  3757. // returned value should be 0 (success)
  3758. checkResult(result, 0);
  3759. // Let's get all subnets and check that there are 4 of them.
  3760. ConstCfgSubnets4Ptr subnets = CfgMgr::instance().getStagingCfg()->getCfgSubnets4();
  3761. ASSERT_TRUE(subnets);
  3762. const Subnet4Collection* subnet_col = subnets->getAll();
  3763. ASSERT_EQ(4, subnet_col->size()); // We expect 4 subnets
  3764. // Let's check if the parsed subnets have correct HR modes.
  3765. // Subnet 1
  3766. Subnet4Ptr subnet;
  3767. subnet = subnets->selectSubnet(IOAddress("192.0.2.1"));
  3768. ASSERT_TRUE(subnet);
  3769. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3770. // Subnet 2
  3771. subnet = subnets->selectSubnet(IOAddress("192.0.3.1"));
  3772. ASSERT_TRUE(subnet);
  3773. EXPECT_EQ(Subnet::HR_OUT_OF_POOL, subnet->getHostReservationMode());
  3774. // Subnet 3
  3775. subnet = subnets->selectSubnet(IOAddress("192.0.4.1"));
  3776. ASSERT_TRUE(subnet);
  3777. EXPECT_EQ(Subnet::HR_DISABLED, subnet->getHostReservationMode());
  3778. // Subnet 4
  3779. subnet = subnets->selectSubnet(IOAddress("192.0.5.1"));
  3780. ASSERT_TRUE(subnet);
  3781. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3782. }
  3783. /// Check that the decline-probation-period has a default value when not
  3784. /// specified.
  3785. TEST_F(Dhcp4ParserTest, declineTimerDefault) {
  3786. ConstElementPtr status;
  3787. string config = "{ " + genIfaceConfig() + "," +
  3788. "\"subnet4\": [ ]"
  3789. "}";
  3790. ConstElementPtr json;
  3791. ASSERT_NO_THROW(json = parseDHCP4(config));
  3792. extractConfig(config);
  3793. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3794. // returned value should be 0 (success)
  3795. checkResult(status, 0);
  3796. // The value of decline-probation-period must be equal to the
  3797. // default value (86400). The default value is defined in GLOBAL6_DEFAULTS in
  3798. // simple_parser6.cc.
  3799. EXPECT_EQ(86400, CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  3800. }
  3801. /// Check that the dhcp4o6-port default value has a default value if not
  3802. /// specified explicitly.
  3803. TEST_F(Dhcp4ParserTest, dhcp4o6portDefault) {
  3804. string config_txt = "{ " + genIfaceConfig() + ","
  3805. "\"subnet4\": [ ] "
  3806. "}";
  3807. ConstElementPtr config;
  3808. ASSERT_NO_THROW(config = parseDHCP4(config_txt));
  3809. extractConfig(config_txt);
  3810. ConstElementPtr status;
  3811. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, config));
  3812. // returned value should be 0 (success)
  3813. checkResult(status, 0);
  3814. // The value of decline-probation-period must be equal to the
  3815. // default value (0). The default value is defined in GLOBAL4_DEFAULTS in
  3816. // simple_parser4.cc.
  3817. EXPECT_EQ(0, CfgMgr::instance().getStagingCfg()->getDhcp4o6Port());
  3818. }
  3819. /// Check that the decline-probation-period value can be set properly.
  3820. TEST_F(Dhcp4ParserTest, declineTimer) {
  3821. ConstElementPtr status;
  3822. string config = "{ " + genIfaceConfig() + "," +
  3823. "\"decline-probation-period\": 12345,"
  3824. "\"subnet4\": [ ]"
  3825. "}";
  3826. ConstElementPtr json;
  3827. ASSERT_NO_THROW(json = parseDHCP4(config));
  3828. extractConfig(config);
  3829. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3830. // returned value should be 0 (success)
  3831. checkResult(status, 0);
  3832. // The value of decline-probation-period must be equal to the
  3833. // value specified.
  3834. EXPECT_EQ(12345,
  3835. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  3836. }
  3837. /// Check that an incorrect decline-probation-period value will be caught.
  3838. TEST_F(Dhcp4ParserTest, declineTimerError) {
  3839. ConstElementPtr status;
  3840. string config = "{ " + genIfaceConfig() + "," +
  3841. "\"decline-probation-period\": \"soon\","
  3842. "\"subnet4\": [ ]"
  3843. "}";
  3844. ConstElementPtr json;
  3845. ASSERT_NO_THROW(json = parseJSON(config));
  3846. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3847. // returned value should be 1 (error)
  3848. checkResult(status, 1);
  3849. // Check that the error contains error position.
  3850. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3851. // Check that the Dhcp4 parser catches the type error
  3852. EXPECT_THROW(parseDHCP4(config), Dhcp4ParseError);
  3853. }
  3854. // Check that configuration for the expired leases processing may be
  3855. // specified.
  3856. TEST_F(Dhcp4ParserTest, expiredLeasesProcessing) {
  3857. // Create basic configuration with the expiration specific parameters.
  3858. string config = "{ " + genIfaceConfig() + "," +
  3859. "\"expired-leases-processing\": "
  3860. "{"
  3861. " \"reclaim-timer-wait-time\": 20,"
  3862. " \"flush-reclaimed-timer-wait-time\": 35,"
  3863. " \"hold-reclaimed-time\": 1800,"
  3864. " \"max-reclaim-leases\": 50,"
  3865. " \"max-reclaim-time\": 100,"
  3866. " \"unwarned-reclaim-cycles\": 10"
  3867. "},"
  3868. "\"subnet4\": [ ]"
  3869. "}";
  3870. ConstElementPtr json;
  3871. ASSERT_NO_THROW(json = parseDHCP4(config));
  3872. extractConfig(config);
  3873. ConstElementPtr status;
  3874. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3875. // Returned value should be 0 (success)
  3876. checkResult(status, 0);
  3877. // The value of decline-probation-period must be equal to the
  3878. // value specified.
  3879. CfgExpirationPtr cfg = CfgMgr::instance().getStagingCfg()->getCfgExpiration();
  3880. ASSERT_TRUE(cfg);
  3881. // Verify that parameters are correct.
  3882. EXPECT_EQ(20, cfg->getReclaimTimerWaitTime());
  3883. EXPECT_EQ(35, cfg->getFlushReclaimedTimerWaitTime());
  3884. EXPECT_EQ(1800, cfg->getHoldReclaimedTime());
  3885. EXPECT_EQ(50, cfg->getMaxReclaimLeases());
  3886. EXPECT_EQ(100, cfg->getMaxReclaimTime());
  3887. EXPECT_EQ(10, cfg->getUnwarnedReclaimCycles());
  3888. }
  3889. // Check that invalid configuration for the expired leases processing is
  3890. // causing an error.
  3891. TEST_F(Dhcp4ParserTest, expiredLeasesProcessingError) {
  3892. // Create basic configuration with the expiration specific parameters.
  3893. // One of the parameters holds invalid value.
  3894. string config = "{ " + genIfaceConfig() + "," +
  3895. "\"expired-leases-processing\": "
  3896. "{"
  3897. " \"reclaim-timer-wait-time\": -5,"
  3898. " \"flush-reclaimed-timer-wait-time\": 35,"
  3899. " \"hold-reclaimed-time\": 1800,"
  3900. " \"max-reclaim-leases\": 50,"
  3901. " \"max-reclaim-time\": 100,"
  3902. " \"unwarned-reclaim-cycles\": 10"
  3903. "},"
  3904. "\"subnet4\": [ ]"
  3905. "}";
  3906. ConstElementPtr json;
  3907. ASSERT_NO_THROW(json = parseDHCP4(config));
  3908. ConstElementPtr status;
  3909. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3910. // Returned value should be 0 (error)
  3911. checkResult(status, 1);
  3912. // Check that the error contains error position.
  3913. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3914. }
  3915. // Checks if the DHCPv4 is able to parse the configuration without 4o6 parameters
  3916. // and does not set 4o6 fields at all.
  3917. TEST_F(Dhcp4ParserTest, 4o6default) {
  3918. ConstElementPtr status;
  3919. // Just a plain v4 config (no 4o6 parameters)
  3920. string config = "{ " + genIfaceConfig() + "," +
  3921. "\"rebind-timer\": 2000, "
  3922. "\"renew-timer\": 1000, "
  3923. "\"subnet4\": [ { "
  3924. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3925. " \"subnet\": \"192.0.2.0/24\" } ],"
  3926. "\"valid-lifetime\": 4000 }";
  3927. ConstElementPtr json;
  3928. ASSERT_NO_THROW(json = parseDHCP4(config));
  3929. extractConfig(config);
  3930. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3931. // check if returned status is OK
  3932. checkResult(status, 0);
  3933. // Now check if the configuration was indeed handled and we have
  3934. // expected pool configured.
  3935. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  3936. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  3937. ASSERT_TRUE(subnet);
  3938. Cfg4o6& dhcp4o6 = subnet->get4o6();
  3939. EXPECT_FALSE(dhcp4o6.enabled());
  3940. }
  3941. // Checks if the DHCPv4 is able to parse the configuration with 4o6 subnet
  3942. // defined.
  3943. TEST_F(Dhcp4ParserTest, 4o6subnet) {
  3944. ConstElementPtr status;
  3945. // Just a plain v4 config (no 4o6 parameters)
  3946. string config = "{ " + genIfaceConfig() + "," +
  3947. "\"rebind-timer\": 2000, "
  3948. "\"renew-timer\": 1000, "
  3949. "\"subnet4\": [ { "
  3950. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3951. " \"subnet\": \"192.0.2.0/24\","
  3952. " \"4o6-subnet\": \"2001:db8::123/45\" } ],"
  3953. "\"valid-lifetime\": 4000 }";
  3954. ConstElementPtr json;
  3955. ASSERT_NO_THROW(json = parseDHCP4(config));
  3956. extractConfig(config);
  3957. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  3958. // check if returned status is OK
  3959. checkResult(status, 0);
  3960. // Now check if the configuration was indeed handled and we have
  3961. // expected pool configured.
  3962. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  3963. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  3964. ASSERT_TRUE(subnet);
  3965. Cfg4o6& dhcp4o6 = subnet->get4o6();
  3966. EXPECT_TRUE(dhcp4o6.enabled());
  3967. EXPECT_EQ(IOAddress("2001:db8::123"), dhcp4o6.getSubnet4o6().first);
  3968. EXPECT_EQ(45, dhcp4o6.getSubnet4o6().second);
  3969. }
  3970. // Checks if the DHCPv4 is able to parse the configuration with 4o6 subnet
  3971. // defined.
  3972. TEST_F(Dhcp4ParserTest, 4o6subnetBogus) {
  3973. ConstElementPtr status;
  3974. // Just a plain v4 config (no 4o6 parameters)
  3975. string config[] = {
  3976. // Bogus configuration 1: missing / in subnet
  3977. "{ " + genIfaceConfig() + "," +
  3978. "\"rebind-timer\": 2000, "
  3979. "\"renew-timer\": 1000, "
  3980. "\"subnet4\": [ { "
  3981. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3982. " \"subnet\": \"192.0.2.0/24\","
  3983. " \"4o6-subnet\": \"2001:db8::123\" } ],"
  3984. "\"valid-lifetime\": 4000 }",
  3985. // Bogus configuration 2: incorrect address
  3986. "{ " + genIfaceConfig() + "," +
  3987. "\"rebind-timer\": 2000, "
  3988. "\"renew-timer\": 1000, "
  3989. "\"subnet4\": [ { "
  3990. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  3991. " \"subnet\": \"192.0.2.0/24\","
  3992. " \"4o6-subnet\": \"2001:db8:bogus/45\" } ],"
  3993. "\"valid-lifetime\": 4000 }",
  3994. // Bogus configuration 3: incorrect prefix length
  3995. "{ " + genIfaceConfig() + "," +
  3996. "\"rebind-timer\": 2000, "
  3997. "\"renew-timer\": 1000, "
  3998. "\"subnet4\": [ { "
  3999. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  4000. " \"subnet\": \"192.0.2.0/24\","
  4001. " \"4o6-subnet\": \"2001:db8::123/200\" } ],"
  4002. "\"valid-lifetime\": 4000 }"
  4003. };
  4004. ConstElementPtr json1;
  4005. ASSERT_NO_THROW(json1 = parseDHCP4(config[0]));
  4006. ConstElementPtr json2;
  4007. ASSERT_NO_THROW(json2 = parseDHCP4(config[0]));
  4008. ConstElementPtr json3;
  4009. ASSERT_NO_THROW(json3 = parseDHCP4(config[0]));
  4010. // Check that the first config is rejected.
  4011. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json1));
  4012. checkResult(status, 1);
  4013. // Check that the second config is rejected.
  4014. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json2));
  4015. checkResult(status, 1);
  4016. // Check that the third config is rejected.
  4017. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json3));
  4018. checkResult(status, 1);
  4019. }
  4020. // Checks if the DHCPv4 is able to parse the configuration with 4o6 network
  4021. // interface defined.
  4022. TEST_F(Dhcp4ParserTest, 4o6iface) {
  4023. ConstElementPtr status;
  4024. // Just a plain v4 config (no 4o6 parameters)
  4025. string config = "{ " + genIfaceConfig() + "," +
  4026. "\"rebind-timer\": 2000, "
  4027. "\"renew-timer\": 1000, "
  4028. "\"subnet4\": [ { "
  4029. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  4030. " \"subnet\": \"192.0.2.0/24\","
  4031. " \"4o6-interface\": \"ethX\" } ],"
  4032. "\"valid-lifetime\": 4000 }";
  4033. ConstElementPtr json;
  4034. ASSERT_NO_THROW(json = parseDHCP4(config));
  4035. extractConfig(config);
  4036. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  4037. // check if returned status is OK
  4038. checkResult(status, 0);
  4039. // Now check if the configuration was indeed handled and we have
  4040. // expected pool configured.
  4041. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  4042. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  4043. ASSERT_TRUE(subnet);
  4044. Cfg4o6& dhcp4o6 = subnet->get4o6();
  4045. EXPECT_TRUE(dhcp4o6.enabled());
  4046. EXPECT_EQ("ethX", dhcp4o6.getIface4o6());
  4047. }
  4048. // Checks if the DHCPv4 is able to parse the configuration with both 4o6 network
  4049. // interface and v6 subnet defined.
  4050. TEST_F(Dhcp4ParserTest, 4o6subnetIface) {
  4051. ConstElementPtr status;
  4052. // Just a plain v4 config (no 4o6 parameters)
  4053. string config = "{ " + genIfaceConfig() + "," +
  4054. "\"rebind-timer\": 2000, "
  4055. "\"renew-timer\": 1000, "
  4056. "\"subnet4\": [ { "
  4057. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  4058. " \"subnet\": \"192.0.2.0/24\","
  4059. " \"4o6-subnet\": \"2001:db8::543/21\","
  4060. " \"4o6-interface\": \"ethX\" } ],"
  4061. "\"valid-lifetime\": 4000 }";
  4062. ConstElementPtr json;
  4063. ASSERT_NO_THROW(json = parseDHCP4(config));
  4064. extractConfig(config);
  4065. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  4066. // check if returned status is OK
  4067. checkResult(status, 0);
  4068. // Now check if the configuration was indeed handled and we have
  4069. // expected subnet configured...
  4070. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  4071. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  4072. ASSERT_TRUE(subnet);
  4073. // ... and that subnet has 4o6 network interface specified.
  4074. Cfg4o6& dhcp4o6 = subnet->get4o6();
  4075. EXPECT_TRUE(dhcp4o6.enabled());
  4076. EXPECT_EQ(IOAddress("2001:db8::543"), dhcp4o6.getSubnet4o6().first);
  4077. EXPECT_EQ(21, dhcp4o6.getSubnet4o6().second);
  4078. EXPECT_EQ("ethX", dhcp4o6.getIface4o6());
  4079. }
  4080. // Checks if the DHCPv4 is able to parse the configuration with 4o6 network
  4081. // interface-id.
  4082. TEST_F(Dhcp4ParserTest, 4o6subnetInterfaceId) {
  4083. ConstElementPtr status;
  4084. // Just a plain v4 config (no 4o6 parameters)
  4085. string config = "{ " + genIfaceConfig() + "," +
  4086. "\"rebind-timer\": 2000, "
  4087. "\"renew-timer\": 1000, "
  4088. "\"subnet4\": [ { "
  4089. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  4090. " \"subnet\": \"192.0.2.0/24\","
  4091. " \"4o6-interface-id\": \"vlan123\" } ],"
  4092. "\"valid-lifetime\": 4000 }";
  4093. ConstElementPtr json;
  4094. ASSERT_NO_THROW(json = parseDHCP4(config));
  4095. extractConfig(config);
  4096. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  4097. // check if returned status is OK
  4098. checkResult(status, 0);
  4099. // Now check if the configuration was indeed handled and we have
  4100. // expected 4o6-interface-id configured.
  4101. Subnet4Ptr subnet = CfgMgr::instance().getStagingCfg()->
  4102. getCfgSubnets4()->selectSubnet(IOAddress("192.0.2.200"));
  4103. ASSERT_TRUE(subnet);
  4104. Cfg4o6& dhcp4o6 = subnet->get4o6();
  4105. EXPECT_TRUE(dhcp4o6.enabled());
  4106. OptionPtr ifaceid = dhcp4o6.getInterfaceId();
  4107. ASSERT_TRUE(ifaceid);
  4108. vector<uint8_t> data = ifaceid->getData();
  4109. EXPECT_EQ(7, data.size());
  4110. const char *exp = "vlan123";
  4111. EXPECT_EQ(0, memcmp(&data[0], exp, data.size()));
  4112. }
  4113. // Verifies that simple list of valid classes parses and
  4114. // is staged for commit.
  4115. TEST_F(Dhcp4ParserTest, validClientClassDictionary) {
  4116. string config = "{ " + genIfaceConfig() + "," +
  4117. "\"valid-lifetime\": 4000, \n"
  4118. "\"rebind-timer\": 2000, \n"
  4119. "\"renew-timer\": 1000, \n"
  4120. "\"client-classes\" : [ \n"
  4121. " { \n"
  4122. " \"name\": \"one\" \n"
  4123. " }, \n"
  4124. " { \n"
  4125. " \"name\": \"two\" \n"
  4126. " }, \n"
  4127. " { \n"
  4128. " \"name\": \"three\" \n"
  4129. " } \n"
  4130. "], \n"
  4131. "\"subnet4\": [ { \n"
  4132. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], \n"
  4133. " \"subnet\": \"192.0.2.0/24\" \n"
  4134. " } ] \n"
  4135. "} \n";
  4136. ConstElementPtr json;
  4137. ASSERT_NO_THROW(json = parseDHCP4(config));
  4138. extractConfig(config);
  4139. ConstElementPtr status;
  4140. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  4141. ASSERT_TRUE(status);
  4142. checkResult(status, 0);
  4143. // We check staging config because CfgMgr::commit hasn't been executed.
  4144. ClientClassDictionaryPtr dictionary;
  4145. dictionary = CfgMgr::instance().getStagingCfg()->getClientClassDictionary();
  4146. ASSERT_TRUE(dictionary);
  4147. EXPECT_EQ(3, dictionary->getClasses()->size());
  4148. // Execute the commit
  4149. ASSERT_NO_THROW(CfgMgr::instance().commit());
  4150. // Verify that after commit, the current config has the correct dictionary
  4151. dictionary = CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
  4152. ASSERT_TRUE(dictionary);
  4153. EXPECT_EQ(3, dictionary->getClasses()->size());
  4154. }
  4155. // Verifies that a class list containing an invalid
  4156. // class definition causes a configuration error.
  4157. TEST_F(Dhcp4ParserTest, invalidClientClassDictionary) {
  4158. string config = "{ " + genIfaceConfig() + "," +
  4159. "\"valid-lifetime\": 4000, \n"
  4160. "\"rebind-timer\": 2000, \n"
  4161. "\"renew-timer\": 1000, \n"
  4162. "\"client-classes\" : [ \n"
  4163. " { \n"
  4164. " \"name\": \"one\", \n"
  4165. " \"bogus\": \"bad\" \n"
  4166. " } \n"
  4167. "], \n"
  4168. "\"subnet4\": [ { \n"
  4169. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], \n"
  4170. " \"subnet\": \"192.0.2.0/24\" \n"
  4171. " } ] \n"
  4172. "} \n";
  4173. EXPECT_THROW(parseDHCP4(config), Dhcp4ParseError);
  4174. }
  4175. // Test verifies that regular configuration does not provide any user context
  4176. // in the address pool.
  4177. TEST_F(Dhcp4ParserTest, poolUserContextMissing) {
  4178. extractConfig(PARSER_CONFIGS[0]);
  4179. PoolPtr pool;
  4180. getPool(string(PARSER_CONFIGS[0]), 0, 0, pool);
  4181. ASSERT_TRUE(pool);
  4182. EXPECT_FALSE(pool->getContext());
  4183. }
  4184. // Test verifies that it's possible to specify empty user context in the
  4185. // address pool.
  4186. TEST_F(Dhcp4ParserTest, poolUserContextEmpty) {
  4187. extractConfig(PARSER_CONFIGS[1]);
  4188. PoolPtr pool;
  4189. getPool(string(PARSER_CONFIGS[1]), 0, 0, pool);
  4190. ASSERT_TRUE(pool);
  4191. ConstElementPtr ctx = pool->getContext();
  4192. ASSERT_TRUE(ctx);
  4193. // The context should be of type map and not contain any parameters.
  4194. EXPECT_EQ(Element::map, ctx->getType());
  4195. EXPECT_EQ(0, ctx->size());
  4196. }
  4197. // Test verifies that it's possible to specify parameters in the user context
  4198. // in the address pool.
  4199. TEST_F(Dhcp4ParserTest, poolUserContextData) {
  4200. extractConfig(PARSER_CONFIGS[2]);
  4201. PoolPtr pool;
  4202. getPool(string(PARSER_CONFIGS[2]), 0, 0, pool);
  4203. ASSERT_TRUE(pool);
  4204. ConstElementPtr ctx = pool->getContext();
  4205. ASSERT_TRUE(ctx);
  4206. // The context should be of type map and contain 4 parameters.
  4207. EXPECT_EQ(Element::map, ctx->getType());
  4208. EXPECT_EQ(3, ctx->size());
  4209. ConstElementPtr int_param = ctx->get("integer-param");
  4210. ConstElementPtr str_param = ctx->get("string-param");
  4211. ConstElementPtr bool_param = ctx->get("bool-param");
  4212. ASSERT_TRUE(int_param);
  4213. ASSERT_EQ(Element::integer, int_param->getType());
  4214. int64_t int_value;
  4215. EXPECT_NO_THROW(int_param->getValue(int_value));
  4216. EXPECT_EQ(42L, int_value);
  4217. ASSERT_TRUE(str_param);
  4218. ASSERT_EQ(Element::string, str_param->getType());
  4219. EXPECT_EQ("Sagittarius", str_param->stringValue());
  4220. ASSERT_TRUE(bool_param);
  4221. bool bool_value;
  4222. ASSERT_EQ(Element::boolean, bool_param->getType());
  4223. EXPECT_NO_THROW(bool_param->getValue(bool_value));
  4224. EXPECT_EQ(true, bool_value);
  4225. }
  4226. // Test verifies that it's possible to specify parameters in the user context
  4227. // in the min-max address pool.
  4228. TEST_F(Dhcp4ParserTest, pooMinMaxlUserContext) {
  4229. extractConfig(PARSER_CONFIGS[3]);
  4230. PoolPtr pool;
  4231. getPool(string(PARSER_CONFIGS[3]), 0, 0, pool);
  4232. ASSERT_TRUE(pool);
  4233. ConstElementPtr ctx = pool->getContext();
  4234. ASSERT_TRUE(ctx);
  4235. // The context should be of type map and contain 4 parameters.
  4236. EXPECT_EQ(Element::map, ctx->getType());
  4237. EXPECT_EQ(3, ctx->size());
  4238. ConstElementPtr int_param = ctx->get("integer-param");
  4239. ConstElementPtr str_param = ctx->get("string-param");
  4240. ConstElementPtr bool_param = ctx->get("bool-param");
  4241. ASSERT_TRUE(int_param);
  4242. ASSERT_EQ(Element::integer, int_param->getType());
  4243. int64_t int_value;
  4244. EXPECT_NO_THROW(int_param->getValue(int_value));
  4245. EXPECT_EQ(42L, int_value);
  4246. ASSERT_TRUE(str_param);
  4247. ASSERT_EQ(Element::string, str_param->getType());
  4248. EXPECT_EQ("Sagittarius", str_param->stringValue());
  4249. ASSERT_TRUE(bool_param);
  4250. bool bool_value;
  4251. ASSERT_EQ(Element::boolean, bool_param->getType());
  4252. EXPECT_NO_THROW(bool_param->getValue(bool_value));
  4253. EXPECT_EQ(true, bool_value);
  4254. }
  4255. // Test verifies the error message for an incorrect pool range
  4256. // is what we expect.
  4257. TEST_F(Dhcp4ParserTest, invalidPoolRange) {
  4258. string config = "{ " + genIfaceConfig() + ", \n" +
  4259. "\"valid-lifetime\": 4000, \n"
  4260. "\"rebind-timer\": 2000, \n"
  4261. "\"renew-timer\": 1000, \n"
  4262. "\"subnet4\": [ { \n"
  4263. " \"pools\": [ { \"pool\": \"192.0.2.1 - 19.2.0.200\" } ], \n"
  4264. " \"subnet\": \"192.0.2.0/24\" \n"
  4265. " } ] \n"
  4266. "} \n";
  4267. ConstElementPtr json;
  4268. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  4269. ConstElementPtr status;
  4270. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  4271. ASSERT_TRUE(status);
  4272. int rcode;
  4273. ConstElementPtr comment = parseAnswer(rcode, status);
  4274. string text;
  4275. ASSERT_NO_THROW(text = comment->stringValue());
  4276. EXPECT_EQ(1, rcode);
  4277. string expected = "Failed to create pool defined by: "
  4278. "192.0.2.1-19.2.0.200 (<string>:6:26)";
  4279. EXPECT_EQ(expected, text);
  4280. }
  4281. // Test verifies the error message for an outside subnet pool range
  4282. // is what we expect.
  4283. TEST_F(Dhcp4ParserTest, outsideSubnetPool) {
  4284. string config = "{ " + genIfaceConfig() + ", \n" +
  4285. "\"valid-lifetime\": 4000, \n"
  4286. "\"rebind-timer\": 2000, \n"
  4287. "\"renew-timer\": 1000, \n"
  4288. "\"subnet4\": [ { \n"
  4289. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], \n"
  4290. " \"subnet\": \"10.0.2.0/24\" \n"
  4291. " } ] \n"
  4292. "} \n";
  4293. ConstElementPtr json;
  4294. ASSERT_NO_THROW(json = parseDHCP4(config, true));
  4295. ConstElementPtr status;
  4296. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  4297. ASSERT_TRUE(status);
  4298. int rcode;
  4299. ConstElementPtr comment = parseAnswer(rcode, status);
  4300. string text;
  4301. ASSERT_NO_THROW(text = comment->stringValue());
  4302. EXPECT_EQ(1, rcode);
  4303. string expected = "subnet configuration failed: "
  4304. "a pool of type V4, with the following address range: "
  4305. "192.0.2.1-192.0.2.100 does not match the prefix of a subnet: "
  4306. "10.0.2.0/24 to which it is being added (<string>:5:14)";
  4307. EXPECT_EQ(expected, text);
  4308. }
  4309. }