config_parser_unittest.cc 122 KB

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