config_parser_unittest.cc 197 KB

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