dhcp_parsers_unittest.cc 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <cc/command_interpreter.h>
  8. #include <cc/data.h>
  9. #include <cc/simple_parser.h>
  10. #include <dhcp/option.h>
  11. #include <dhcp/option_custom.h>
  12. #include <dhcp/option_int.h>
  13. #include <dhcp/option_string.h>
  14. #include <dhcp/option6_addrlst.h>
  15. #include <dhcp/tests/iface_mgr_test_config.h>
  16. #include <dhcpsrv/cfgmgr.h>
  17. #include <dhcpsrv/subnet.h>
  18. #include <dhcpsrv/cfg_mac_source.h>
  19. #include <dhcpsrv/parsers/dhcp_parsers.h>
  20. #include <dhcpsrv/parsers/option_data_parser.h>
  21. #include <dhcpsrv/tests/test_libraries.h>
  22. #include <dhcpsrv/testutils/config_result_check.h>
  23. #include <exceptions/exceptions.h>
  24. #include <hooks/hooks_parser.h>
  25. #include <hooks/hooks_manager.h>
  26. #include <testutils/test_to_element.h>
  27. #include <gtest/gtest.h>
  28. #include <boost/foreach.hpp>
  29. #include <boost/pointer_cast.hpp>
  30. #include <boost/scoped_ptr.hpp>
  31. #include <map>
  32. #include <string>
  33. using namespace std;
  34. using namespace isc;
  35. using namespace isc::asiolink;
  36. using namespace isc::config;
  37. using namespace isc::data;
  38. using namespace isc::dhcp;
  39. using namespace isc::dhcp::test;
  40. using namespace isc::hooks;
  41. using namespace isc::test;
  42. namespace {
  43. /// @brief DHCP Parser test fixture class
  44. class DhcpParserTest : public ::testing::Test {
  45. public:
  46. /// @brief Constructor
  47. DhcpParserTest() {
  48. resetIfaceCfg();
  49. }
  50. /// @brief Destructor.
  51. virtual ~DhcpParserTest() {
  52. resetIfaceCfg();
  53. }
  54. /// @brief Resets selection of the interfaces from previous tests.
  55. void resetIfaceCfg() {
  56. CfgMgr::instance().clear();
  57. }
  58. };
  59. /// @brief Check BooleanParser basic functionality.
  60. ///
  61. /// Verifies that the parser:
  62. /// 1. Does not allow empty for storage.
  63. /// 2. Rejects a non-boolean element.
  64. /// 3. Builds with a valid true value.
  65. /// 4. Bbuils with a valid false value.
  66. /// 5. Updates storage upon commit.
  67. TEST_F(DhcpParserTest, booleanParserTest) {
  68. const std::string name = "boolParm";
  69. // Verify that parser does not allow empty for storage.
  70. BooleanStoragePtr bs;
  71. EXPECT_THROW(BooleanParser(name, bs), isc::dhcp::DhcpConfigError);
  72. // Construct parser for testing.
  73. BooleanStoragePtr storage(new BooleanStorage());
  74. BooleanParser parser(name, storage);
  75. // Verify that parser with rejects a non-boolean element.
  76. ElementPtr wrong_element = Element::create("I am a string");
  77. EXPECT_THROW(parser.build(wrong_element), isc::BadValue);
  78. // Verify that parser will build with a valid true value.
  79. bool test_value = true;
  80. ElementPtr element = Element::create(test_value);
  81. ASSERT_NO_THROW(parser.build(element));
  82. // Verify that commit updates storage.
  83. bool actual_value = !test_value;
  84. parser.commit();
  85. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  86. EXPECT_EQ(test_value, actual_value);
  87. // Verify that parser will build with a valid false value.
  88. test_value = false;
  89. element->setValue(test_value);
  90. EXPECT_NO_THROW(parser.build(element));
  91. // Verify that commit updates storage.
  92. actual_value = !test_value;
  93. parser.commit();
  94. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  95. EXPECT_EQ(test_value, actual_value);
  96. }
  97. /// @brief Check StringParser basic functionality
  98. ///
  99. /// Verifies that the parser:
  100. /// 1. Does not allow empty for storage.
  101. /// 2. Builds with a nont string value.
  102. /// 3. Builds with a string value.
  103. /// 4. Updates storage upon commit.
  104. TEST_F(DhcpParserTest, stringParserTest) {
  105. const std::string name = "strParm";
  106. // Verify that parser does not allow empty for storage.
  107. StringStoragePtr bs;
  108. EXPECT_THROW(StringParser(name, bs), isc::dhcp::DhcpConfigError);
  109. // Construct parser for testing.
  110. StringStoragePtr storage(new StringStorage());
  111. StringParser parser(name, storage);
  112. // Verify that parser with accepts a non-string element.
  113. ElementPtr element = Element::create(9999);
  114. EXPECT_NO_THROW(parser.build(element));
  115. // Verify that commit updates storage.
  116. parser.commit();
  117. std::string actual_value;
  118. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  119. EXPECT_EQ("9999", actual_value);
  120. // Verify that parser will build with a string value.
  121. const std::string test_value = "test value";
  122. element = Element::create(test_value);
  123. ASSERT_NO_THROW(parser.build(element));
  124. // Verify that commit updates storage.
  125. parser.commit();
  126. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  127. EXPECT_EQ(test_value, actual_value);
  128. // Verify that parser with accepts a boolean true element.
  129. element = Element::create(true);
  130. EXPECT_NO_THROW(parser.build(element));
  131. // Verify that commit updates storage.
  132. parser.commit();
  133. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  134. EXPECT_EQ("true", actual_value);
  135. // Verify that parser with accepts a boolean true element.
  136. element = Element::create(false);
  137. EXPECT_NO_THROW(parser.build(element));
  138. // Verify that commit updates storage.
  139. parser.commit();
  140. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  141. EXPECT_EQ("false", actual_value);
  142. }
  143. /// @brief Check Uint32Parser basic functionality
  144. ///
  145. /// Verifies that the parser:
  146. /// 1. Does not allow empty for storage.
  147. /// 2. Rejects a non-integer element.
  148. /// 3. Rejects a negative value.
  149. /// 4. Rejects too large a value.
  150. /// 5. Builds with value of zero.
  151. /// 6. Builds with a value greater than zero.
  152. /// 7. Updates storage upon commit.
  153. TEST_F(DhcpParserTest, uint32ParserTest) {
  154. const std::string name = "intParm";
  155. // Verify that parser does not allow empty for storage.
  156. Uint32StoragePtr bs;
  157. EXPECT_THROW(Uint32Parser(name, bs), isc::dhcp::DhcpConfigError);
  158. // Construct parser for testing.
  159. Uint32StoragePtr storage(new Uint32Storage());
  160. Uint32Parser parser(name, storage);
  161. // Verify that parser with rejects a non-integer element.
  162. ElementPtr wrong_element = Element::create("I am a string");
  163. EXPECT_THROW(parser.build(wrong_element), isc::BadValue);
  164. // Verify that parser with rejects a negative value.
  165. ElementPtr int_element = Element::create(-1);
  166. EXPECT_THROW(parser.build(int_element), isc::BadValue);
  167. // Verify that parser with rejects too large a value provided we are on
  168. // 64-bit platform.
  169. if (sizeof(long) > sizeof(uint32_t)) {
  170. long max = (long)(std::numeric_limits<uint32_t>::max()) + 1;
  171. int_element->setValue(max);
  172. EXPECT_THROW(parser.build(int_element), isc::BadValue);
  173. }
  174. // Verify that parser will build with value of zero.
  175. int test_value = 0;
  176. int_element->setValue((long)test_value);
  177. ASSERT_NO_THROW(parser.build(int_element));
  178. // Verify that commit updates storage.
  179. parser.commit();
  180. uint32_t actual_value = 0;
  181. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  182. EXPECT_EQ(test_value, actual_value);
  183. // Verify that parser will build with a valid positive value.
  184. test_value = 77;
  185. int_element->setValue((long)test_value);
  186. ASSERT_NO_THROW(parser.build(int_element));
  187. // Verify that commit updates storage.
  188. parser.commit();
  189. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  190. EXPECT_EQ(test_value, actual_value);
  191. }
  192. /// Verifies the code that parses mac sources and adds them to CfgMgr
  193. TEST_F(DhcpParserTest, MacSources) {
  194. // That's an equivalent of the following snippet:
  195. // "mac-sources: [ \"duid\", \"ipv6\" ]";
  196. ElementPtr values = Element::createList();
  197. values->add(Element::create("duid"));
  198. values->add(Element::create("ipv6-link-local"));
  199. // Let's grab server configuration from CfgMgr
  200. SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
  201. ASSERT_TRUE(cfg);
  202. CfgMACSource& sources = cfg->getMACSources();
  203. // This should parse the configuration and check that it doesn't throw.
  204. MACSourcesListConfigParser parser;
  205. EXPECT_NO_THROW(parser.parse(sources, values));
  206. // Finally, check the sources that were configured
  207. CfgMACSources configured_sources = cfg->getMACSources().get();
  208. ASSERT_EQ(2, configured_sources.size());
  209. EXPECT_EQ(HWAddr::HWADDR_SOURCE_DUID, configured_sources[0]);
  210. EXPECT_EQ(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL, configured_sources[1]);
  211. }
  212. /// @brief Check MACSourcesListConfigParser rejecting empty list
  213. ///
  214. /// Verifies that the code rejects an empty mac-sources list.
  215. TEST_F(DhcpParserTest, MacSourcesEmpty) {
  216. // That's an equivalent of the following snippet:
  217. // "mac-sources: [ \"duid\", \"ipv6\" ]";
  218. ElementPtr values = Element::createList();
  219. // Let's grab server configuration from CfgMgr
  220. SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
  221. ASSERT_TRUE(cfg);
  222. CfgMACSource& sources = cfg->getMACSources();
  223. // This should throw, because if specified, at least one MAC source
  224. // has to be specified.
  225. MACSourcesListConfigParser parser;
  226. EXPECT_THROW(parser.parse(sources, values), DhcpConfigError);
  227. }
  228. /// @brief Check MACSourcesListConfigParser rejecting empty list
  229. ///
  230. /// Verifies that the code rejects fake mac source.
  231. TEST_F(DhcpParserTest, MacSourcesBogus) {
  232. // That's an equivalent of the following snippet:
  233. // "mac-sources: [ \"duid\", \"ipv6\" ]";
  234. ElementPtr values = Element::createList();
  235. values->add(Element::create("from-ebay"));
  236. values->add(Element::create("just-guess-it"));
  237. // Let's grab server configuration from CfgMgr
  238. SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
  239. ASSERT_TRUE(cfg);
  240. CfgMACSource& sources = cfg->getMACSources();
  241. // This should throw, because these are not valid sources.
  242. MACSourcesListConfigParser parser;
  243. EXPECT_THROW(parser.parse(sources, values), DhcpConfigError);
  244. }
  245. /// Verifies the code that properly catches duplicate entries
  246. /// in mac-sources definition.
  247. TEST_F(DhcpParserTest, MacSourcesDuplicate) {
  248. // That's an equivalent of the following snippet:
  249. // "mac-sources: [ \"duid\", \"ipv6\" ]";
  250. ElementPtr values = Element::createList();
  251. values->add(Element::create("ipv6-link-local"));
  252. values->add(Element::create("duid"));
  253. values->add(Element::create("duid"));
  254. values->add(Element::create("duid"));
  255. // Let's grab server configuration from CfgMgr
  256. SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
  257. ASSERT_TRUE(cfg);
  258. CfgMACSource& sources = cfg->getMACSources();
  259. // This should parse the configuration and check that it throws.
  260. MACSourcesListConfigParser parser;
  261. EXPECT_THROW(parser.parse(sources, values), DhcpConfigError);
  262. }
  263. /// @brief Test Fixture class which provides basic structure for testing
  264. /// configuration parsing. This is essentially the same structure provided
  265. /// by dhcp servers.
  266. class ParseConfigTest : public ::testing::Test {
  267. public:
  268. /// @brief Constructor
  269. ParseConfigTest()
  270. :family_(AF_INET6) {
  271. reset_context();
  272. CfgMgr::instance().clear();
  273. }
  274. ~ParseConfigTest() {
  275. reset_context();
  276. CfgMgr::instance().clear();
  277. }
  278. /// @brief Parses a configuration.
  279. ///
  280. /// Parse the given configuration, populating the context storage with
  281. /// the parsed elements.
  282. ///
  283. /// @param config_set is the set of elements to parse.
  284. /// @return returns an ConstElementPtr containing the numeric result
  285. /// code and outcome comment.
  286. isc::data::ConstElementPtr parseElementSet(isc::data::ConstElementPtr
  287. config_set) {
  288. // Answer will hold the result.
  289. ConstElementPtr answer;
  290. if (!config_set) {
  291. answer = isc::config::createAnswer(1,
  292. string("Can't parse NULL config"));
  293. return (answer);
  294. }
  295. ConfigPair config_pair;
  296. try {
  297. // Iterate over the config elements.
  298. const std::map<std::string, ConstElementPtr>& values_map =
  299. config_set->mapValue();
  300. BOOST_FOREACH(config_pair, values_map) {
  301. // These are the simple parsers. No need to go through
  302. // the ParserPtr hooplas with them.
  303. if ((config_pair.first == "option-data") ||
  304. (config_pair.first == "option-def") ||
  305. (config_pair.first == "dhcp-ddns")) {
  306. continue;
  307. }
  308. // We also don't care about the default values that may be been
  309. // inserted
  310. if ((config_pair.first == "preferred-lifetime") ||
  311. (config_pair.first == "valid-lifetime") ||
  312. (config_pair.first == "renew-timer") ||
  313. (config_pair.first == "rebind-timer")) {
  314. continue;
  315. }
  316. if (config_pair.first == "hooks-libraries") {
  317. HooksLibrariesParser hook_parser;
  318. HooksConfig& libraries =
  319. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  320. hook_parser.parse(libraries, config_pair.second);
  321. libraries.verifyLibraries(config_pair.second->getPosition());
  322. libraries.loadLibraries();
  323. continue;
  324. }
  325. }
  326. // The option definition parser is the next one to be run.
  327. std::map<std::string, ConstElementPtr>::const_iterator
  328. def_config = values_map.find("option-def");
  329. if (def_config != values_map.end()) {
  330. CfgOptionDefPtr cfg_def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef();
  331. OptionDefListParser def_list_parser;
  332. def_list_parser.parse(cfg_def, def_config->second);
  333. }
  334. // The option values parser is the next one to be run.
  335. std::map<std::string, ConstElementPtr>::const_iterator
  336. option_config = values_map.find("option-data");
  337. if (option_config != values_map.end()) {
  338. CfgOptionPtr cfg_option = CfgMgr::instance().getStagingCfg()->getCfgOption();
  339. OptionDataListParser option_list_parser(family_);
  340. option_list_parser.parse(cfg_option, option_config->second);
  341. }
  342. // The dhcp-ddns parser is the next one to be run.
  343. std::map<std::string, ConstElementPtr>::const_iterator
  344. d2_client_config = values_map.find("dhcp-ddns");
  345. if (d2_client_config != values_map.end()) {
  346. // Used to be done by parser commit
  347. D2ClientConfigParser parser;
  348. D2ClientConfigPtr cfg = parser.parse(d2_client_config->second);
  349. CfgMgr::instance().setD2ClientConfig(cfg);
  350. }
  351. // Everything was fine. Configuration is successful.
  352. answer = isc::config::createAnswer(0, "Configuration committed.");
  353. } catch (const isc::Exception& ex) {
  354. answer = isc::config::createAnswer(1,
  355. string("Configuration parsing failed: ") + ex.what());
  356. } catch (...) {
  357. answer = isc::config::createAnswer(1,
  358. string("Configuration parsing failed"));
  359. }
  360. return (answer);
  361. }
  362. /// @brief DHCP-specific method that sets global, and option specific defaults
  363. ///
  364. /// This method sets the defaults in the global scope, in option definitions,
  365. /// and in option data.
  366. ///
  367. /// @param global pointer to the Element tree that holds configuration
  368. /// @param global_defaults array with global default values
  369. /// @param option_defaults array with option-data default values
  370. /// @param option_def_defaults array with default values for option definitions
  371. /// @return number of default values inserted.
  372. size_t setAllDefaults(isc::data::ElementPtr global,
  373. const SimpleDefaults& global_defaults,
  374. const SimpleDefaults& option_defaults,
  375. const SimpleDefaults& option_def_defaults) {
  376. size_t cnt = 0;
  377. // Set global defaults first.
  378. cnt = SimpleParser::setDefaults(global, global_defaults);
  379. // Now set option definition defaults for each specified option definition
  380. ConstElementPtr option_defs = global->get("option-def");
  381. if (option_defs) {
  382. BOOST_FOREACH(ElementPtr single_def, option_defs->listValue()) {
  383. cnt += SimpleParser::setDefaults(single_def, option_def_defaults);
  384. }
  385. }
  386. ConstElementPtr options = global->get("option-data");
  387. if (options) {
  388. BOOST_FOREACH(ElementPtr single_option, options->listValue()) {
  389. cnt += SimpleParser::setDefaults(single_option, option_defaults);
  390. }
  391. }
  392. return (cnt);
  393. }
  394. /// This table defines default values for option definitions in DHCPv6
  395. static const SimpleDefaults OPTION6_DEF_DEFAULTS;
  396. /// This table defines default values for option definitions in DHCPv4
  397. static const SimpleDefaults OPTION4_DEF_DEFAULTS;
  398. /// This table defines default values for options in DHCPv6
  399. static const SimpleDefaults OPTION6_DEFAULTS;
  400. /// This table defines default values for options in DHCPv4
  401. static const SimpleDefaults OPTION4_DEFAULTS;
  402. /// This table defines default values for both DHCPv4 and DHCPv6
  403. static const SimpleDefaults GLOBAL6_DEFAULTS;
  404. /// @brief sets all default values for DHCPv4 and DHCPv6
  405. ///
  406. /// This function largely duplicates what SimpleParser4 and SimpleParser6 classes
  407. /// provide. However, since there are tons of unit-tests in dhcpsrv that need
  408. /// this functionality and there are good reasons to keep those classes in
  409. /// src/bin/dhcp{4,6}, the most straightforward way is to simply copy the
  410. /// minimum code here. Hence this method.
  411. ///
  412. /// @todo - TKM, I think this is fairly hideous and we should figure out a
  413. /// a way to not have to replicate in this fashion. It may be minimum code
  414. /// now, but it won't be fairly soon.
  415. ///
  416. /// @param config configuration structure to be filled with default values
  417. /// @param v6 true = DHCPv6, false = DHCPv4
  418. void setAllDefaults(ElementPtr config, bool v6) {
  419. if (v6) {
  420. setAllDefaults(config, GLOBAL6_DEFAULTS, OPTION6_DEFAULTS,
  421. OPTION6_DEF_DEFAULTS);
  422. } else {
  423. setAllDefaults(config, GLOBAL6_DEFAULTS, OPTION4_DEFAULTS,
  424. OPTION4_DEF_DEFAULTS);
  425. }
  426. /// D2 client configuration code is in this library
  427. ConstElementPtr d2_client = config->get("dhcp-ddns");
  428. if (d2_client) {
  429. D2ClientConfigParser::setAllDefaults(d2_client);
  430. }
  431. }
  432. /// @brief Convenience method for parsing a configuration
  433. ///
  434. /// Given a configuration string, convert it into Elements
  435. /// and parse them.
  436. /// @param config is the configuration string to parse
  437. ///
  438. /// @return returns 0 if the configuration parsed successfully,
  439. /// non-zero otherwise failure.
  440. int parseConfiguration(const std::string& config, bool v6 = false) {
  441. int rcode_ = 1;
  442. // Turn config into elements.
  443. // Test json just to make sure its valid.
  444. ElementPtr json = Element::fromJSON(config);
  445. EXPECT_TRUE(json);
  446. if (json) {
  447. setAllDefaults(json, v6);
  448. ConstElementPtr status = parseElementSet(json);
  449. ConstElementPtr comment = parseAnswer(rcode_, status);
  450. error_text_ = comment->stringValue();
  451. // If error was reported, the error string should contain
  452. // position of the data element which caused failure.
  453. if (rcode_ != 0) {
  454. std::cout << "Error text:" << error_text_ << std::endl;
  455. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  456. }
  457. }
  458. return (rcode_);
  459. }
  460. /// @brief Find an option for a given space and code within the parser
  461. /// context.
  462. /// @param space is the space name of the desired option.
  463. /// @param code is the numeric "type" of the desired option.
  464. /// @return returns an OptionPtr which points to the found
  465. /// option or is empty.
  466. /// ASSERT_ tests don't work inside functions that return values
  467. OptionPtr getOptionPtr(std::string space, uint32_t code)
  468. {
  469. OptionPtr option_ptr;
  470. OptionContainerPtr options = CfgMgr::instance().getStagingCfg()->
  471. getCfgOption()->getAll(space);
  472. // Should always be able to get options list even if it is empty.
  473. EXPECT_TRUE(options);
  474. if (options) {
  475. // Attempt to find desired option.
  476. const OptionContainerTypeIndex& idx = options->get<1>();
  477. const OptionContainerTypeRange& range = idx.equal_range(code);
  478. int cnt = std::distance(range.first, range.second);
  479. EXPECT_EQ(1, cnt);
  480. if (cnt == 1) {
  481. OptionDescriptor desc = *(idx.begin());
  482. option_ptr = desc.option_;
  483. EXPECT_TRUE(option_ptr);
  484. }
  485. }
  486. return (option_ptr);
  487. }
  488. /// @brief Wipes the contents of the context to allowing another parsing
  489. /// during a given test if needed.
  490. void reset_context(){
  491. // Note set context universe to V6 as it has to be something.
  492. CfgMgr::instance().clear();
  493. family_ = AF_INET6;
  494. // Ensure no hooks libraries are loaded.
  495. HooksManager::unloadLibraries();
  496. // Set it to minimal, disabled config
  497. D2ClientConfigPtr tmp(new D2ClientConfig());
  498. CfgMgr::instance().setD2ClientConfig(tmp);
  499. }
  500. /// Allows the tests to interrogate the state of the libraries (if required).
  501. const isc::hooks::HookLibsCollection& getLibraries() {
  502. return (CfgMgr::instance().getStagingCfg()->getHooksConfig().get());
  503. }
  504. /// @brief specifies IP protocol family (AF_INET or AF_INET6)
  505. uint16_t family_;
  506. /// @brief Error string if the parsing failed
  507. std::string error_text_;
  508. };
  509. /// This table defines default values for option definitions in DHCPv6
  510. const SimpleDefaults ParseConfigTest::OPTION6_DEF_DEFAULTS = {
  511. { "record-types", Element::string, ""},
  512. { "space", Element::string, "dhcp6"},
  513. { "array", Element::boolean, "false"},
  514. { "encapsulate", Element::string, "" }
  515. };
  516. /// This table defines default values for option definitions in DHCPv4
  517. const SimpleDefaults ParseConfigTest::OPTION4_DEF_DEFAULTS = {
  518. { "record-types", Element::string, ""},
  519. { "space", Element::string, "dhcp4"},
  520. { "array", Element::boolean, "false"},
  521. { "encapsulate", Element::string, "" }
  522. };
  523. /// This table defines default values for options in DHCPv6
  524. const SimpleDefaults ParseConfigTest::OPTION6_DEFAULTS = {
  525. { "space", Element::string, "dhcp6"},
  526. { "csv-format", Element::boolean, "true"},
  527. { "always-send", Element::boolean,"false"}
  528. };
  529. /// This table defines default values for options in DHCPv4
  530. const SimpleDefaults ParseConfigTest::OPTION4_DEFAULTS = {
  531. { "space", Element::string, "dhcp4"},
  532. { "csv-format", Element::boolean, "true"},
  533. { "always-send", Element::boolean, "false"}
  534. };
  535. /// This table defines default values for both DHCPv4 and DHCPv6
  536. const SimpleDefaults ParseConfigTest::GLOBAL6_DEFAULTS = {
  537. { "renew-timer", Element::integer, "900" },
  538. { "rebind-timer", Element::integer, "1800" },
  539. { "preferred-lifetime", Element::integer, "3600" },
  540. { "valid-lifetime", Element::integer, "7200" }
  541. };
  542. /// @brief Option configuration class
  543. ///
  544. /// This class handles option-def and option-data which can be recovered
  545. /// using the toElement() method
  546. class CfgOptionsTest : public CfgToElement {
  547. public:
  548. /// @brief Constructor
  549. ///
  550. /// @param cfg the server configuration where to get option-{def,data}
  551. CfgOptionsTest(SrvConfigPtr cfg) :
  552. cfg_option_def_(cfg->getCfgOptionDef()),
  553. cfg_option_(cfg->getCfgOption()) { }
  554. /// @brief Unparse a configuration object
  555. ///
  556. /// @return a pointer to unparsed configuration (a map with
  557. /// not empty option-def and option-data lists)
  558. ElementPtr toElement() const {
  559. ElementPtr result = Element::createMap();
  560. // Set option-def
  561. ConstElementPtr option_def = cfg_option_def_->toElement();
  562. if (!option_def->empty()) {
  563. result->set("option-def", option_def);
  564. }
  565. // Set option-data
  566. ConstElementPtr option_data = cfg_option_->toElement();
  567. if (!option_data->empty()) {
  568. result->set("option-data", option_data);
  569. }
  570. return (result);
  571. }
  572. /// @brief Run a toElement test (Element version)
  573. ///
  574. /// Use the runToElementTest template but add defaults to the config
  575. ///
  576. /// @param family the address family
  577. /// @param config the expected result without defaults
  578. void runCfgOptionsTest(uint16_t family, ConstElementPtr expected) {
  579. ConstElementPtr option_def = expected->get("option-def");
  580. if (option_def) {
  581. SimpleParser::setListDefaults(option_def,
  582. family == AF_INET ?
  583. ParseConfigTest::OPTION4_DEF_DEFAULTS :
  584. ParseConfigTest::OPTION6_DEF_DEFAULTS);
  585. }
  586. ConstElementPtr option_data = expected->get("option-data");
  587. if (option_data) {
  588. SimpleParser::setListDefaults(option_data,
  589. family == AF_INET ?
  590. ParseConfigTest::OPTION4_DEFAULTS :
  591. ParseConfigTest::OPTION6_DEFAULTS);
  592. }
  593. runToElementTest<CfgOptionsTest>(expected, *this);
  594. }
  595. /// @brief Run a toElement test
  596. ///
  597. /// Use the runToElementTest template but add defaults to the config
  598. ///
  599. /// @param family the address family
  600. /// @param expected the expected result without defaults
  601. void runCfgOptionsTest(uint16_t family, std::string config) {
  602. ConstElementPtr json;
  603. ASSERT_NO_THROW(json = Element::fromJSON(config)) << config;
  604. runCfgOptionsTest(family, json);
  605. }
  606. private:
  607. /// @brief Pointer to option definitions configuration.
  608. CfgOptionDefPtr cfg_option_def_;
  609. /// @brief Reference to options (data) configuration.
  610. CfgOptionPtr cfg_option_;
  611. };
  612. /// @brief Check basic parsing of option definitions.
  613. ///
  614. /// Note that this tests basic operation of the OptionDefinitionListParser and
  615. /// OptionDefinitionParser. It uses a simple configuration consisting of
  616. /// one definition and verifies that it is parsed and committed to storage
  617. /// correctly.
  618. TEST_F(ParseConfigTest, basicOptionDefTest) {
  619. // Configuration string.
  620. std::string config =
  621. "{ \"option-def\": [ {"
  622. " \"name\": \"foo\","
  623. " \"code\": 100,"
  624. " \"type\": \"ipv4-address\","
  625. " \"array\": false,"
  626. " \"record-types\": \"\","
  627. " \"space\": \"isc\","
  628. " \"encapsulate\": \"\""
  629. " } ]"
  630. "}";
  631. // Verify that the configuration string parses.
  632. int rcode = parseConfiguration(config);
  633. ASSERT_EQ(0, rcode);
  634. // Verify that the option definition can be retrieved.
  635. OptionDefinitionPtr def =
  636. CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  637. ASSERT_TRUE(def);
  638. // Verify that the option definition is correct.
  639. EXPECT_EQ("foo", def->getName());
  640. EXPECT_EQ(100, def->getCode());
  641. EXPECT_FALSE(def->getArrayType());
  642. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  643. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  644. // Check if libdhcp++ runtime options have been updated.
  645. OptionDefinitionPtr def_libdhcp = LibDHCP::getRuntimeOptionDef("isc", 100);
  646. ASSERT_TRUE(def_libdhcp);
  647. // The LibDHCP should return a separate instance of the option definition
  648. // but the values should be equal.
  649. EXPECT_TRUE(def_libdhcp != def);
  650. EXPECT_TRUE(*def_libdhcp == *def);
  651. // Check if it can be unparsed.
  652. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  653. cfg.runCfgOptionsTest(family_, config);
  654. }
  655. /// @brief Check minimal parsing of option definitions.
  656. ///
  657. /// Same than basic but without optional parameters set to their default.
  658. TEST_F(ParseConfigTest, minimalOptionDefTest) {
  659. // Configuration string.
  660. std::string config =
  661. "{ \"option-def\": [ {"
  662. " \"name\": \"foo\","
  663. " \"code\": 100,"
  664. " \"type\": \"ipv4-address\","
  665. " \"space\": \"isc\""
  666. " } ]"
  667. "}";
  668. // Verify that the configuration string parses.
  669. int rcode = parseConfiguration(config);
  670. ASSERT_EQ(0, rcode);
  671. // Verify that the option definition can be retrieved.
  672. OptionDefinitionPtr def =
  673. CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  674. ASSERT_TRUE(def);
  675. // Verify that the option definition is correct.
  676. EXPECT_EQ("foo", def->getName());
  677. EXPECT_EQ(100, def->getCode());
  678. EXPECT_FALSE(def->getArrayType());
  679. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  680. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  681. // Check if it can be unparsed.
  682. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  683. cfg.runCfgOptionsTest(family_, config);
  684. }
  685. /// @brief Check parsing of option definitions using default dhcp6 space.
  686. ///
  687. /// Same than minimal but using the fact the default universe is V6
  688. /// so the default space is dhcp6
  689. TEST_F(ParseConfigTest, defaultSpaceOptionDefTest) {
  690. // Configuration string.
  691. std::string config =
  692. "{ \"option-def\": [ {"
  693. " \"name\": \"foo\","
  694. " \"code\": 10000,"
  695. " \"type\": \"ipv6-address\""
  696. " } ]"
  697. "}";
  698. // Verify that the configuration string parses.
  699. int rcode = parseConfiguration(config, true);
  700. ASSERT_EQ(0, rcode);
  701. // Verify that the option definition can be retrieved.
  702. OptionDefinitionPtr def =
  703. CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get(DHCP6_OPTION_SPACE, 10000);
  704. ASSERT_TRUE(def);
  705. // Verify that the option definition is correct.
  706. EXPECT_EQ("foo", def->getName());
  707. EXPECT_EQ(10000, def->getCode());
  708. EXPECT_FALSE(def->getArrayType());
  709. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, def->getType());
  710. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  711. // Check if it can be unparsed.
  712. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  713. cfg.runCfgOptionsTest(family_, config);
  714. }
  715. /// @brief Check basic parsing of options.
  716. ///
  717. /// Note that this tests basic operation of the OptionDataListParser and
  718. /// OptionDataParser. It uses a simple configuration consisting of one
  719. /// one definition and matching option data. It verifies that the option
  720. /// is parsed and committed to storage correctly.
  721. TEST_F(ParseConfigTest, basicOptionDataTest) {
  722. // Configuration string.
  723. std::string config =
  724. "{ \"option-def\": [ {"
  725. " \"name\": \"foo\","
  726. " \"code\": 100,"
  727. " \"type\": \"ipv4-address\","
  728. " \"space\": \"isc\""
  729. " } ], "
  730. " \"option-data\": [ {"
  731. " \"name\": \"foo\","
  732. " \"space\": \"isc\","
  733. " \"code\": 100,"
  734. " \"data\": \"192.0.2.0\","
  735. " \"csv-format\": true,"
  736. " \"always-send\": false"
  737. " } ]"
  738. "}";
  739. // Verify that the configuration string parses.
  740. int rcode = parseConfiguration(config);
  741. ASSERT_EQ(0, rcode);
  742. // Verify that the option can be retrieved.
  743. OptionPtr opt_ptr = getOptionPtr("isc", 100);
  744. ASSERT_TRUE(opt_ptr);
  745. // Verify that the option data is correct.
  746. std::string val = "type=00100, len=00004: 192.0.2.0 (ipv4-address)";
  747. EXPECT_EQ(val, opt_ptr->toText());
  748. // Check if it can be unparsed.
  749. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  750. cfg.runCfgOptionsTest(family_, config);
  751. }
  752. /// @brief Check minimal parsing of options.
  753. ///
  754. /// Same than basic but without optional parameters set to their default.
  755. TEST_F(ParseConfigTest, minimalOptionDataTest) {
  756. // Configuration string.
  757. std::string config =
  758. "{ \"option-def\": [ {"
  759. " \"name\": \"foo\","
  760. " \"code\": 100,"
  761. " \"type\": \"ipv4-address\","
  762. " \"space\": \"isc\""
  763. " } ], "
  764. " \"option-data\": [ {"
  765. " \"name\": \"foo\","
  766. " \"space\": \"isc\","
  767. " \"data\": \"192.0.2.0\""
  768. " } ]"
  769. "}";
  770. // Verify that the configuration string parses.
  771. int rcode = parseConfiguration(config);
  772. ASSERT_EQ(0, rcode);
  773. // Verify that the option can be retrieved.
  774. OptionPtr opt_ptr = getOptionPtr("isc", 100);
  775. ASSERT_TRUE(opt_ptr);
  776. // Verify that the option data is correct.
  777. std::string val = "type=00100, len=00004: 192.0.2.0 (ipv4-address)";
  778. EXPECT_EQ(val, opt_ptr->toText());
  779. ElementPtr expected = Element::fromJSON(config);
  780. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  781. opt_data->set("code", Element::create(100));
  782. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  783. cfg.runCfgOptionsTest(family_, expected);
  784. }
  785. /// @brief Check parsing of options with escape characters.
  786. ///
  787. /// Note that this tests basic operation of the OptionDataListParser and
  788. /// OptionDataParser. It uses a simple configuration consisting of one
  789. /// one definition and matching option data. It verifies that the option
  790. /// is parsed and committed to storage correctly and that its content
  791. /// has the actual character (e.g. an actual backslash, not double backslash).
  792. TEST_F(ParseConfigTest, escapedOptionDataTest) {
  793. family_ = AF_INET;
  794. // We need to use double escapes here. The first backslash will
  795. // be consumed by C++ preprocessor, so the actual string will
  796. // have two backslash characters: \\SMSBoot\\x64\\wdsnbp.com.
  797. //
  798. std::string config =
  799. "{\"option-data\": [ {"
  800. " \"name\": \"boot-file-name\","
  801. " \"data\": \"\\\\SMSBoot\\\\x64\\\\wdsnbp.com\""
  802. " } ]"
  803. "}";
  804. // Verify that the configuration string parses.
  805. int rcode = parseConfiguration(config);
  806. ASSERT_EQ(0, rcode);
  807. // Verify that the option can be retrieved.
  808. OptionPtr opt = getOptionPtr(DHCP4_OPTION_SPACE, DHO_BOOT_FILE_NAME);
  809. ASSERT_TRUE(opt);
  810. util::OutputBuffer buf(100);
  811. uint8_t exp[] = { DHO_BOOT_FILE_NAME, 23, '\\', 'S', 'M', 'S', 'B', 'o', 'o',
  812. 't', '\\', 'x', '6', '4', '\\', 'w', 'd', 's', 'n', 'b',
  813. 'p', '.', 'c', 'o', 'm' };
  814. ASSERT_EQ(25, sizeof(exp));
  815. opt->pack(buf);
  816. EXPECT_EQ(Option::OPTION4_HDR_LEN + 23, buf.getLength());
  817. EXPECT_TRUE(0 == memcmp(buf.getData(), exp, 25));
  818. ElementPtr expected = Element::fromJSON(config);
  819. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  820. opt_data->set("code", Element::create(DHO_BOOT_FILE_NAME));
  821. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  822. cfg.runCfgOptionsTest(family_, expected);
  823. }
  824. // This test checks behavior of the configuration parser for option data
  825. // for different values of csv-format parameter and when there is an option
  826. // definition present.
  827. TEST_F(ParseConfigTest, optionDataCSVFormatWithOptionDef) {
  828. std::string config =
  829. "{ \"option-data\": [ {"
  830. " \"name\": \"swap-server\","
  831. " \"space\": \"dhcp4\","
  832. " \"code\": 16,"
  833. " \"data\": \"192.0.2.0\""
  834. " } ]"
  835. "}";
  836. // The default universe is V6. We need to change it to use dhcp4 option
  837. // space.
  838. family_ = AF_INET;
  839. int rcode = 0;
  840. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  841. ASSERT_EQ(0, rcode);
  842. // Verify that the option data is correct.
  843. OptionCustomPtr addr_opt = boost::dynamic_pointer_cast<
  844. OptionCustom>(getOptionPtr(DHCP4_OPTION_SPACE, 16));
  845. ASSERT_TRUE(addr_opt);
  846. EXPECT_EQ("192.0.2.0", addr_opt->readAddress().toText());
  847. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  848. cfg.runCfgOptionsTest(family_, config);
  849. // Explicitly enable csv-format.
  850. CfgMgr::instance().clear();
  851. config =
  852. "{ \"option-data\": [ {"
  853. " \"name\": \"swap-server\","
  854. " \"space\": \"dhcp4\","
  855. " \"code\": 16,"
  856. " \"csv-format\": true,"
  857. " \"data\": \"192.0.2.0\""
  858. " } ]"
  859. "}";
  860. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  861. ASSERT_EQ(0, rcode);
  862. // Verify that the option data is correct.
  863. addr_opt = boost::dynamic_pointer_cast<
  864. OptionCustom>(getOptionPtr(DHCP4_OPTION_SPACE, 16));
  865. ASSERT_TRUE(addr_opt);
  866. EXPECT_EQ("192.0.2.0", addr_opt->readAddress().toText());
  867. // To make runToElementTest to work the csv-format must be removed...
  868. // Explicitly disable csv-format and use hex instead.
  869. CfgMgr::instance().clear();
  870. config =
  871. "{ \"option-data\": [ {"
  872. " \"name\": \"swap-server\","
  873. " \"space\": \"dhcp4\","
  874. " \"code\": 16,"
  875. " \"csv-format\": false,"
  876. " \"data\": \"C0000200\""
  877. " } ]"
  878. "}";
  879. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  880. ASSERT_EQ(0, rcode);
  881. // Verify that the option data is correct.
  882. addr_opt = boost::dynamic_pointer_cast<
  883. OptionCustom>(getOptionPtr(DHCP4_OPTION_SPACE, 16));
  884. ASSERT_TRUE(addr_opt);
  885. EXPECT_EQ("192.0.2.0", addr_opt->readAddress().toText());
  886. CfgOptionsTest cfg2(CfgMgr::instance().getStagingCfg());
  887. cfg2.runCfgOptionsTest(family_, config);
  888. }
  889. // This test checks behavior of the configuration parser for option data
  890. // for different values of csv-format parameter and when there is no
  891. // option definition.
  892. TEST_F(ParseConfigTest, optionDataCSVFormatNoOptionDef) {
  893. // This option doesn't have any definition. It is ok to use such
  894. // an option but the data should be specified in hex, not as CSV.
  895. // Note that the parser will by default use the CSV format for the
  896. // data but only in case there is a suitable option definition.
  897. std::string config =
  898. "{ \"option-data\": [ {"
  899. " \"name\": \"foo-name\","
  900. " \"space\": \"dhcp6\","
  901. " \"code\": 25000,"
  902. " \"data\": \"1, 2, 5\""
  903. " } ]"
  904. "}";
  905. int rcode = 0;
  906. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  907. EXPECT_NE(0, rcode);
  908. CfgMgr::instance().clear();
  909. // The data specified here will work both for CSV format and hex format.
  910. // What we want to test here is that when the csv-format is enforced, the
  911. // parser will fail because of lack of an option definition.
  912. config =
  913. "{ \"option-data\": [ {"
  914. " \"name\": \"foo-name\","
  915. " \"space\": \"dhcp6\","
  916. " \"code\": 25000,"
  917. " \"csv-format\": true,"
  918. " \"data\": \"0\""
  919. " } ]"
  920. "}";
  921. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  922. EXPECT_NE(0, rcode);
  923. CfgMgr::instance().clear();
  924. // The same test case as above, but for the data specified in hex should
  925. // be successful.
  926. config =
  927. "{ \"option-data\": [ {"
  928. " \"name\": \"foo-name\","
  929. " \"space\": \"dhcp6\","
  930. " \"code\": 25000,"
  931. " \"csv-format\": false,"
  932. " \"data\": \"0\""
  933. " } ]"
  934. "}";
  935. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  936. ASSERT_EQ(0, rcode);
  937. OptionPtr opt = getOptionPtr(DHCP6_OPTION_SPACE, 25000);
  938. ASSERT_TRUE(opt);
  939. ASSERT_EQ(1, opt->getData().size());
  940. EXPECT_EQ(0, opt->getData()[0]);
  941. ElementPtr expected = Element::fromJSON(config);
  942. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  943. opt_data->remove("name");
  944. opt_data->set("data", Element::create(std::string("00")));
  945. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  946. cfg.runCfgOptionsTest(family_, expected);
  947. CfgMgr::instance().clear();
  948. // When csv-format is not specified, the parser will check if the definition
  949. // exists or not. Since there is no definition, the parser will accept the
  950. // data in hex.
  951. config =
  952. "{ \"option-data\": [ {"
  953. " \"name\": \"foo-name\","
  954. " \"space\": \"dhcp6\","
  955. " \"code\": 25000,"
  956. " \"csv-format\": false,"
  957. " \"data\": \"123456\""
  958. " } ]"
  959. "}";
  960. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  961. EXPECT_EQ(0, rcode);
  962. opt = getOptionPtr(DHCP6_OPTION_SPACE, 25000);
  963. ASSERT_TRUE(opt);
  964. ASSERT_EQ(3, opt->getData().size());
  965. EXPECT_EQ(0x12, opt->getData()[0]);
  966. EXPECT_EQ(0x34, opt->getData()[1]);
  967. EXPECT_EQ(0x56, opt->getData()[2]);
  968. expected = Element::fromJSON(config);
  969. opt_data = expected->get("option-data")->getNonConst(0);
  970. opt_data->remove("name");
  971. CfgOptionsTest cfg2(CfgMgr::instance().getStagingCfg());
  972. cfg2.runCfgOptionsTest(family_, expected);
  973. }
  974. // This test verifies that the option name is not mandatory, if the option
  975. // code has been specified.
  976. TEST_F(ParseConfigTest, optionDataNoName) {
  977. std::string config =
  978. "{ \"option-data\": [ {"
  979. " \"space\": \"dhcp6\","
  980. " \"code\": 23,"
  981. " \"data\": \"2001:db8:1::1\""
  982. " } ]"
  983. "}";
  984. int rcode = 0;
  985. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  986. EXPECT_EQ(0, rcode);
  987. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  988. Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE, 23));
  989. ASSERT_TRUE(opt);
  990. ASSERT_EQ(1, opt->getAddresses().size());
  991. EXPECT_EQ( "2001:db8:1::1", opt->getAddresses()[0].toText());
  992. ElementPtr expected = Element::fromJSON(config);
  993. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  994. opt_data->set("name", Element::create(std::string("dns-servers")));
  995. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  996. cfg.runCfgOptionsTest(family_, expected);
  997. }
  998. // This test verifies that the option code is not mandatory, if the option
  999. // name has been specified.
  1000. TEST_F(ParseConfigTest, optionDataNoCode) {
  1001. std::string config =
  1002. "{ \"option-data\": [ {"
  1003. " \"space\": \"dhcp6\","
  1004. " \"name\": \"dns-servers\","
  1005. " \"data\": \"2001:db8:1::1\""
  1006. " } ]"
  1007. "}";
  1008. int rcode = 0;
  1009. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  1010. EXPECT_EQ(0, rcode);
  1011. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  1012. Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE, 23));
  1013. ASSERT_TRUE(opt);
  1014. ASSERT_EQ(1, opt->getAddresses().size());
  1015. EXPECT_EQ( "2001:db8:1::1", opt->getAddresses()[0].toText());
  1016. ElementPtr expected = Element::fromJSON(config);
  1017. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  1018. opt_data->set("code", Element::create(D6O_NAME_SERVERS));
  1019. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  1020. cfg.runCfgOptionsTest(family_, expected);
  1021. }
  1022. // This test verifies that the option data configuration with a minimal
  1023. // set of parameters works as expected.
  1024. TEST_F(ParseConfigTest, optionDataMinimal) {
  1025. std::string config =
  1026. "{ \"option-data\": [ {"
  1027. " \"name\": \"dns-servers\","
  1028. " \"data\": \"2001:db8:1::10\""
  1029. " } ]"
  1030. "}";
  1031. int rcode = 0;
  1032. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  1033. EXPECT_EQ(0, rcode);
  1034. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  1035. Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE, 23));
  1036. ASSERT_TRUE(opt);
  1037. ASSERT_EQ(1, opt->getAddresses().size());
  1038. EXPECT_EQ( "2001:db8:1::10", opt->getAddresses()[0].toText());
  1039. ElementPtr expected = Element::fromJSON(config);
  1040. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  1041. opt_data->set("code", Element::create(D6O_NAME_SERVERS));
  1042. opt_data->set("space", Element::create(std::string(DHCP6_OPTION_SPACE)));
  1043. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  1044. cfg.runCfgOptionsTest(family_, expected);
  1045. CfgMgr::instance().clear();
  1046. // This time using an option code.
  1047. config =
  1048. "{ \"option-data\": [ {"
  1049. " \"code\": 23,"
  1050. " \"data\": \"2001:db8:1::20\""
  1051. " } ]"
  1052. "}";
  1053. rcode = 0;
  1054. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  1055. EXPECT_EQ(0, rcode);
  1056. opt = boost::dynamic_pointer_cast<Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE,
  1057. 23));
  1058. ASSERT_TRUE(opt);
  1059. ASSERT_EQ(1, opt->getAddresses().size());
  1060. EXPECT_EQ( "2001:db8:1::20", opt->getAddresses()[0].toText());
  1061. expected = Element::fromJSON(config);
  1062. opt_data = expected->get("option-data")->getNonConst(0);
  1063. opt_data->set("name", Element::create(std::string("dns-servers")));
  1064. opt_data->set("space", Element::create(std::string(DHCP6_OPTION_SPACE)));
  1065. CfgOptionsTest cfg2(CfgMgr::instance().getStagingCfg());
  1066. cfg2.runCfgOptionsTest(family_, expected);
  1067. }
  1068. // This test verifies that the option data configuration with a minimal
  1069. // set of parameters works as expected when option definition is
  1070. // created in the configuration file.
  1071. TEST_F(ParseConfigTest, optionDataMinimalWithOptionDef) {
  1072. // Configuration string.
  1073. std::string config =
  1074. "{ \"option-def\": [ {"
  1075. " \"name\": \"foo-name\","
  1076. " \"code\": 2345,"
  1077. " \"type\": \"ipv6-address\","
  1078. " \"array\": true,"
  1079. " \"space\": \"dhcp6\""
  1080. " } ],"
  1081. " \"option-data\": [ {"
  1082. " \"name\": \"foo-name\","
  1083. " \"data\": \"2001:db8:1::10, 2001:db8:1::123\""
  1084. " } ]"
  1085. "}";
  1086. int rcode = 0;
  1087. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  1088. EXPECT_EQ(0, rcode);
  1089. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  1090. Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE, 2345));
  1091. ASSERT_TRUE(opt);
  1092. ASSERT_EQ(2, opt->getAddresses().size());
  1093. EXPECT_EQ("2001:db8:1::10", opt->getAddresses()[0].toText());
  1094. EXPECT_EQ("2001:db8:1::123", opt->getAddresses()[1].toText());
  1095. ElementPtr expected = Element::fromJSON(config);
  1096. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  1097. opt_data->set("code", Element::create(2345));
  1098. opt_data->set("space", Element::create(std::string(DHCP6_OPTION_SPACE)));
  1099. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  1100. cfg.runCfgOptionsTest(family_, expected);
  1101. CfgMgr::instance().clear();
  1102. // Do the same test but now use an option code.
  1103. config =
  1104. "{ \"option-def\": [ {"
  1105. " \"name\": \"foo-name\","
  1106. " \"code\": 2345,"
  1107. " \"type\": \"ipv6-address\","
  1108. " \"array\": true,"
  1109. " \"space\": \"dhcp6\""
  1110. " } ],"
  1111. " \"option-data\": [ {"
  1112. " \"code\": 2345,"
  1113. " \"data\": \"2001:db8:1::10, 2001:db8:1::123\""
  1114. " } ]"
  1115. "}";
  1116. rcode = 0;
  1117. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  1118. EXPECT_EQ(0, rcode);
  1119. opt = boost::dynamic_pointer_cast<Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE,
  1120. 2345));
  1121. ASSERT_TRUE(opt);
  1122. ASSERT_EQ(2, opt->getAddresses().size());
  1123. EXPECT_EQ("2001:db8:1::10", opt->getAddresses()[0].toText());
  1124. EXPECT_EQ("2001:db8:1::123", opt->getAddresses()[1].toText());
  1125. expected = Element::fromJSON(config);
  1126. opt_data = expected->get("option-data")->getNonConst(0);
  1127. opt_data->set("name", Element::create(std::string("foo-name")));
  1128. opt_data->set("space", Element::create(std::string(DHCP6_OPTION_SPACE)));
  1129. CfgOptionsTest cfg2(CfgMgr::instance().getStagingCfg());
  1130. cfg2.runCfgOptionsTest(family_, expected);
  1131. }
  1132. // This test verifies an empty option data configuration is supported.
  1133. TEST_F(ParseConfigTest, emptyOptionData) {
  1134. // Configuration string.
  1135. const std::string config =
  1136. "{ \"option-data\": [ {"
  1137. " \"name\": \"dhcp4o6-server-addr\""
  1138. " } ]"
  1139. "}";
  1140. int rcode = 0;
  1141. ASSERT_NO_THROW(rcode = parseConfiguration(config, true));
  1142. EXPECT_EQ(0, rcode);
  1143. const Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  1144. Option6AddrLst>(getOptionPtr(DHCP6_OPTION_SPACE, D6O_DHCPV4_O_DHCPV6_SERVER));
  1145. ASSERT_TRUE(opt);
  1146. ASSERT_EQ(0, opt->getAddresses().size());
  1147. ElementPtr expected = Element::fromJSON(config);
  1148. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  1149. opt_data->set("code", Element::create(D6O_DHCPV4_O_DHCPV6_SERVER));
  1150. opt_data->set("space", Element::create(std::string(DHCP6_OPTION_SPACE)));
  1151. opt_data->set("csv-format", Element::create(false));
  1152. opt_data->set("data", Element::create(std::string("")));
  1153. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  1154. cfg.runCfgOptionsTest(family_, expected);
  1155. }
  1156. // This test verifies an option data without suboptions is supported
  1157. TEST_F(ParseConfigTest, optionDataNoSubOption) {
  1158. // Configuration string.
  1159. const std::string config =
  1160. "{ \"option-data\": [ {"
  1161. " \"name\": \"vendor-encapsulated-options\""
  1162. " } ]"
  1163. "}";
  1164. // The default universe is V6. We need to change it to use dhcp4 option
  1165. // space.
  1166. family_ = AF_INET;
  1167. int rcode = 0;
  1168. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  1169. EXPECT_EQ(0, rcode);
  1170. const OptionPtr opt = getOptionPtr(DHCP4_OPTION_SPACE, DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1171. ASSERT_TRUE(opt);
  1172. ASSERT_EQ(0, opt->getOptions().size());
  1173. ElementPtr expected = Element::fromJSON(config);
  1174. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  1175. opt_data->set("code", Element::create(DHO_VENDOR_ENCAPSULATED_OPTIONS));
  1176. opt_data->set("space", Element::create(std::string(DHCP4_OPTION_SPACE)));
  1177. opt_data->set("csv-format", Element::create(false));
  1178. opt_data->set("data", Element::create(std::string("")));
  1179. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  1180. cfg.runCfgOptionsTest(family_, expected);
  1181. }
  1182. // This tests option-data in CSV format and embedded commas.
  1183. TEST_F(ParseConfigTest, commaCSVFormatOptionData) {
  1184. // Configuration string.
  1185. std::string config =
  1186. "{ \"option-data\": [ {"
  1187. " \"csv-format\": true,"
  1188. " \"code\": 41,"
  1189. " \"data\": \"EST5EDT4\\\\,M3.2.0/02:00\\\\,M11.1.0/02:00\","
  1190. " \"space\": \"dhcp6\""
  1191. " } ]"
  1192. "}";
  1193. // Verify that the configuration string parses.
  1194. int rcode = parseConfiguration(config, true);
  1195. ASSERT_EQ(0, rcode);
  1196. // Verify that the option can be retrieved.
  1197. OptionPtr opt = getOptionPtr(DHCP6_OPTION_SPACE, 41);
  1198. ASSERT_TRUE(opt);
  1199. // Get the option as an option string.
  1200. OptionStringPtr opt_str = boost::dynamic_pointer_cast<OptionString>(opt);
  1201. ASSERT_TRUE(opt_str);
  1202. // Verify that the option data is correct.
  1203. string val = "EST5EDT4,M3.2.0/02:00,M11.1.0/02:00";
  1204. EXPECT_EQ(val, opt_str->getValue());
  1205. ElementPtr expected = Element::fromJSON(config);
  1206. ElementPtr opt_data = expected->get("option-data")->getNonConst(0);
  1207. opt_data->remove("csv-format");
  1208. opt_data->set("name", Element::create(std::string("new-posix-timezone")));
  1209. CfgOptionsTest cfg(CfgMgr::instance().getStagingCfg());
  1210. cfg.runCfgOptionsTest(family_, expected);
  1211. }
  1212. /// The next set of tests check basic operation of the HooksLibrariesParser.
  1213. //
  1214. // Convenience function to set a configuration of zero or more hooks
  1215. // libraries:
  1216. //
  1217. // lib1 - No parameters
  1218. // lib2 - Empty parameters statement
  1219. // lib3 - Valid parameters
  1220. std::string
  1221. setHooksLibrariesConfig(const char* lib1 = NULL, const char* lib2 = NULL,
  1222. const char* lib3 = NULL) {
  1223. const string lbrace("{");
  1224. const string rbrace("}");
  1225. const string quote("\"");
  1226. const string comma_space(", ");
  1227. const string library("\"library\": ");
  1228. string config = string("{ \"hooks-libraries\": [");
  1229. if (lib1 != NULL) {
  1230. // Library 1 has no parameters
  1231. config += lbrace;
  1232. config += library + quote + std::string(lib1) + quote;
  1233. config += rbrace;
  1234. if (lib2 != NULL) {
  1235. // Library 2 has an empty parameters statement
  1236. config += comma_space + lbrace;
  1237. config += library + quote + std::string(lib2) + quote + comma_space;
  1238. config += string("\"parameters\": {}");
  1239. config += rbrace;
  1240. if (lib3 != NULL) {
  1241. // Library 3 has valid parameters
  1242. config += comma_space + lbrace;
  1243. config += library + quote + std::string(lib3) + quote + comma_space;
  1244. config += string("\"parameters\": {");
  1245. config += string(" \"svalue\": \"string value\", ");
  1246. config += string(" \"ivalue\": 42, "); // Integer value
  1247. config += string(" \"bvalue\": true"); // Boolean value
  1248. config += string("}");
  1249. config += rbrace;
  1250. }
  1251. }
  1252. }
  1253. config += std::string("] }");
  1254. return (config);
  1255. }
  1256. // hooks-libraries element that does not contain anything.
  1257. TEST_F(ParseConfigTest, noHooksLibraries) {
  1258. // Check that no libraries are currently loaded
  1259. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1260. EXPECT_TRUE(hooks_libraries.empty());
  1261. // Create an empty hooks-libraries configuration element.
  1262. const string config = setHooksLibrariesConfig();
  1263. // Verify that the configuration string parses.
  1264. const int rcode = parseConfiguration(config);
  1265. ASSERT_TRUE(rcode == 0) << error_text_;
  1266. // Verify that the configuration object unparses.
  1267. ConstElementPtr expected;
  1268. ASSERT_NO_THROW(expected =
  1269. Element::fromJSON(config)->get("hooks-libraries"));
  1270. ASSERT_TRUE(expected);
  1271. const HooksConfig& cfg =
  1272. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1273. runToElementTest<HooksConfig>(expected, cfg);
  1274. // Check that the parser recorded nothing.
  1275. isc::hooks::HookLibsCollection libraries = getLibraries();
  1276. EXPECT_TRUE(libraries.empty());
  1277. // Check that there are still no libraries loaded.
  1278. hooks_libraries = HooksManager::getLibraryNames();
  1279. EXPECT_TRUE(hooks_libraries.empty());
  1280. }
  1281. // hooks-libraries element that contains a single library.
  1282. TEST_F(ParseConfigTest, oneHooksLibrary) {
  1283. // Check that no libraries are currently loaded
  1284. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1285. EXPECT_TRUE(hooks_libraries.empty());
  1286. // Configuration with hooks-libraries set to a single library.
  1287. const string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1);
  1288. // Verify that the configuration string parses.
  1289. const int rcode = parseConfiguration(config);
  1290. ASSERT_TRUE(rcode == 0) << error_text_;
  1291. // Verify that the configuration object unparses.
  1292. ConstElementPtr expected;
  1293. ASSERT_NO_THROW(expected =
  1294. Element::fromJSON(config)->get("hooks-libraries"));
  1295. ASSERT_TRUE(expected);
  1296. const HooksConfig& cfg =
  1297. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1298. runToElementTest<HooksConfig>(expected, cfg);
  1299. // Check that the parser recorded a single library.
  1300. isc::hooks::HookLibsCollection libraries = getLibraries();
  1301. ASSERT_EQ(1, libraries.size());
  1302. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
  1303. // Check that the change was propagated to the hooks manager.
  1304. hooks_libraries = HooksManager::getLibraryNames();
  1305. ASSERT_EQ(1, hooks_libraries.size());
  1306. EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
  1307. }
  1308. // hooks-libraries element that contains two libraries
  1309. TEST_F(ParseConfigTest, twoHooksLibraries) {
  1310. // Check that no libraries are currently loaded
  1311. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1312. EXPECT_TRUE(hooks_libraries.empty());
  1313. // Configuration with hooks-libraries set to two libraries.
  1314. const string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1315. CALLOUT_LIBRARY_2);
  1316. // Verify that the configuration string parses.
  1317. const int rcode = parseConfiguration(config);
  1318. ASSERT_TRUE(rcode == 0) << error_text_;
  1319. // Verify that the configuration object unparses.
  1320. ConstElementPtr expected;
  1321. ASSERT_NO_THROW(expected =
  1322. Element::fromJSON(config)->get("hooks-libraries"));
  1323. ASSERT_TRUE(expected);
  1324. const HooksConfig& cfg =
  1325. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1326. runToElementTest<HooksConfig>(expected, cfg);
  1327. // Check that the parser recorded two libraries in the expected order.
  1328. isc::hooks::HookLibsCollection libraries = getLibraries();
  1329. ASSERT_EQ(2, libraries.size());
  1330. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
  1331. EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1].first);
  1332. // Verify that the change was propagated to the hooks manager.
  1333. hooks_libraries = HooksManager::getLibraryNames();
  1334. ASSERT_EQ(2, hooks_libraries.size());
  1335. EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
  1336. EXPECT_EQ(CALLOUT_LIBRARY_2, hooks_libraries[1]);
  1337. }
  1338. // Configure with two libraries, then reconfigure with the same libraries.
  1339. TEST_F(ParseConfigTest, reconfigureSameHooksLibraries) {
  1340. // Check that no libraries are currently loaded
  1341. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1342. EXPECT_TRUE(hooks_libraries.empty());
  1343. // Configuration with hooks-libraries set to two libraries.
  1344. const std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1345. CALLOUT_LIBRARY_2);
  1346. // Verify that the configuration string parses. The twoHooksLibraries
  1347. // test shows that the list will be as expected.
  1348. int rcode = parseConfiguration(config);
  1349. ASSERT_TRUE(rcode == 0) << error_text_;
  1350. // Verify that the configuration object unparses.
  1351. ConstElementPtr expected;
  1352. ASSERT_NO_THROW(expected =
  1353. Element::fromJSON(config)->get("hooks-libraries"));
  1354. ASSERT_TRUE(expected);
  1355. const HooksConfig& cfg =
  1356. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1357. runToElementTest<HooksConfig>(expected, cfg);
  1358. // The previous test shows that the parser correctly recorded the two
  1359. // libraries and that they loaded correctly.
  1360. // Parse the string again.
  1361. rcode = parseConfiguration(config);
  1362. ASSERT_TRUE(rcode == 0) << error_text_;
  1363. // The list has not changed between the two parse operations. However,
  1364. // the parameters (or the files they could point to) could have
  1365. // changed, so the libraries are reloaded anyway.
  1366. const HooksConfig& cfg2 =
  1367. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1368. runToElementTest<HooksConfig>(expected, cfg2);
  1369. isc::hooks::HookLibsCollection libraries = getLibraries();
  1370. ASSERT_EQ(2, libraries.size());
  1371. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
  1372. EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1].first);
  1373. // ... and check that the same two libraries are still loaded in the
  1374. // HooksManager.
  1375. hooks_libraries = HooksManager::getLibraryNames();
  1376. ASSERT_EQ(2, hooks_libraries.size());
  1377. EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
  1378. EXPECT_EQ(CALLOUT_LIBRARY_2, hooks_libraries[1]);
  1379. }
  1380. // Configure the hooks with two libraries, then reconfigure with the same
  1381. // libraries, but in reverse order.
  1382. TEST_F(ParseConfigTest, reconfigureReverseHooksLibraries) {
  1383. // Check that no libraries are currently loaded
  1384. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1385. EXPECT_TRUE(hooks_libraries.empty());
  1386. // Configuration with hooks-libraries set to two libraries.
  1387. std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1388. CALLOUT_LIBRARY_2);
  1389. // Verify that the configuration string parses. The twoHooksLibraries
  1390. // test shows that the list will be as expected.
  1391. int rcode = parseConfiguration(config);
  1392. ASSERT_TRUE(rcode == 0) << error_text_;
  1393. // A previous test shows that the parser correctly recorded the two
  1394. // libraries and that they loaded correctly.
  1395. // Parse the reversed set of libraries.
  1396. config = setHooksLibrariesConfig(CALLOUT_LIBRARY_2, CALLOUT_LIBRARY_1);
  1397. rcode = parseConfiguration(config);
  1398. ASSERT_TRUE(rcode == 0) << error_text_;
  1399. // The list has changed, and this is what we should see.
  1400. isc::hooks::HookLibsCollection libraries = getLibraries();
  1401. ASSERT_EQ(2, libraries.size());
  1402. EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[0].first);
  1403. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[1].first);
  1404. // ... and check that this was propagated to the HooksManager.
  1405. hooks_libraries = HooksManager::getLibraryNames();
  1406. ASSERT_EQ(2, hooks_libraries.size());
  1407. EXPECT_EQ(CALLOUT_LIBRARY_2, hooks_libraries[0]);
  1408. EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[1]);
  1409. }
  1410. // Configure the hooks with two libraries, then reconfigure with
  1411. // no libraries.
  1412. TEST_F(ParseConfigTest, reconfigureZeroHooksLibraries) {
  1413. // Check that no libraries are currently loaded
  1414. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1415. EXPECT_TRUE(hooks_libraries.empty());
  1416. // Configuration with hooks-libraries set to two libraries.
  1417. std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1418. CALLOUT_LIBRARY_2);
  1419. // Verify that the configuration string parses.
  1420. int rcode = parseConfiguration(config);
  1421. ASSERT_TRUE(rcode == 0) << error_text_;
  1422. // A previous test shows that the parser correctly recorded the two
  1423. // libraries and that they loaded correctly.
  1424. // Parse the string again, this time without any libraries.
  1425. config = setHooksLibrariesConfig();
  1426. rcode = parseConfiguration(config);
  1427. ASSERT_TRUE(rcode == 0) << error_text_;
  1428. // Verify that the configuration object unparses.
  1429. ConstElementPtr expected;
  1430. ASSERT_NO_THROW(expected =
  1431. Element::fromJSON(config)->get("hooks-libraries"));
  1432. ASSERT_TRUE(expected);
  1433. const HooksConfig& cfg =
  1434. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1435. runToElementTest<HooksConfig>(expected, cfg);
  1436. // The list has changed, and this is what we should see.
  1437. isc::hooks::HookLibsCollection libraries = getLibraries();
  1438. EXPECT_TRUE(libraries.empty());
  1439. // Check that no libraries are currently loaded
  1440. hooks_libraries = HooksManager::getLibraryNames();
  1441. EXPECT_TRUE(hooks_libraries.empty());
  1442. }
  1443. // Check with a set of libraries, some of which are invalid.
  1444. TEST_F(ParseConfigTest, invalidHooksLibraries) {
  1445. // Check that no libraries are currently loaded
  1446. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1447. EXPECT_TRUE(hooks_libraries.empty());
  1448. // Configuration string. This contains an invalid library which should
  1449. // trigger an error in the "build" stage.
  1450. const std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1451. NOT_PRESENT_LIBRARY,
  1452. CALLOUT_LIBRARY_2);
  1453. // Verify that the configuration fails to parse. (Syntactically it's OK,
  1454. // but the library is invalid).
  1455. const int rcode = parseConfiguration(config);
  1456. ASSERT_FALSE(rcode == 0) << error_text_;
  1457. // Check that the message contains the library in error.
  1458. EXPECT_FALSE(error_text_.find(NOT_PRESENT_LIBRARY) == string::npos) <<
  1459. "Error text returned from parse failure is " << error_text_;
  1460. // Check that the parser recorded the names but, as they were in error,
  1461. // does not flag them as changed.
  1462. isc::hooks::HookLibsCollection libraries = getLibraries();
  1463. ASSERT_EQ(3, libraries.size());
  1464. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
  1465. EXPECT_EQ(NOT_PRESENT_LIBRARY, libraries[1].first);
  1466. EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[2].first);
  1467. // ...and check it did not alter the libraries in the hooks manager.
  1468. hooks_libraries = HooksManager::getLibraryNames();
  1469. EXPECT_TRUE(hooks_libraries.empty());
  1470. }
  1471. // Check that trying to reconfigure with an invalid set of libraries fails.
  1472. TEST_F(ParseConfigTest, reconfigureInvalidHooksLibraries) {
  1473. // Check that no libraries are currently loaded
  1474. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1475. EXPECT_TRUE(hooks_libraries.empty());
  1476. // Configure with a single library.
  1477. std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1);
  1478. int rcode = parseConfiguration(config);
  1479. ASSERT_TRUE(rcode == 0) << error_text_;
  1480. // A previous test shows that the parser correctly recorded the two
  1481. // libraries and that they loaded correctly.
  1482. // Configuration string. This contains an invalid library which should
  1483. // trigger an error in the "build" stage.
  1484. config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1, NOT_PRESENT_LIBRARY,
  1485. CALLOUT_LIBRARY_2);
  1486. // Verify that the configuration fails to parse. (Syntactically it's OK,
  1487. // but the library is invalid).
  1488. rcode = parseConfiguration(config);
  1489. EXPECT_FALSE(rcode == 0) << error_text_;
  1490. // Check that the message contains the library in error.
  1491. EXPECT_FALSE(error_text_.find(NOT_PRESENT_LIBRARY) == string::npos) <<
  1492. "Error text returned from parse failure is " << error_text_;
  1493. // Check that the parser recorded the names but, as the library set was
  1494. // incorrect, did not mark the configuration as changed.
  1495. isc::hooks::HookLibsCollection libraries = getLibraries();
  1496. ASSERT_EQ(3, libraries.size());
  1497. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
  1498. EXPECT_EQ(NOT_PRESENT_LIBRARY, libraries[1].first);
  1499. EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[2].first);
  1500. // ... but check that the hooks manager was not updated with the incorrect
  1501. // names.
  1502. hooks_libraries.clear();
  1503. hooks_libraries = HooksManager::getLibraryNames();
  1504. ASSERT_EQ(1, hooks_libraries.size());
  1505. EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
  1506. }
  1507. // Check that if hooks-libraries contains invalid syntax, it is detected.
  1508. TEST_F(ParseConfigTest, invalidSyntaxHooksLibraries) {
  1509. // Element holds a mixture of (valid) maps and non-maps.
  1510. string config1 = "{ \"hooks-libraries\": [ "
  1511. "{ \"library\": \"/opt/lib/lib1\" }, "
  1512. "\"/opt/lib/lib2\" "
  1513. "] }";
  1514. string error1 = "one or more entries in the hooks-libraries list is not"
  1515. " a map";
  1516. int rcode = parseConfiguration(config1);
  1517. ASSERT_NE(0, rcode);
  1518. EXPECT_TRUE(error_text_.find(error1) != string::npos) <<
  1519. "Error text returned from parse failure is " << error_text_;
  1520. // Element holds valid maps, except one where the library element is not
  1521. // a string.
  1522. string config2 = "{ \"hooks-libraries\": [ "
  1523. "{ \"library\": \"/opt/lib/lib1\" }, "
  1524. "{ \"library\": 123 } "
  1525. "] }";
  1526. string error2 = "value of 'library' element is not a string giving"
  1527. " the path to a hooks library";
  1528. rcode = parseConfiguration(config2);
  1529. ASSERT_NE(0, rcode);
  1530. EXPECT_TRUE(error_text_.find(error2) != string::npos) <<
  1531. "Error text returned from parse failure is " << error_text_;
  1532. // Element holds valid maps, except one where the library element is the
  1533. // empty string.
  1534. string config3 = "{ \"hooks-libraries\": [ "
  1535. "{ \"library\": \"/opt/lib/lib1\" }, "
  1536. "{ \"library\": \"\" } "
  1537. "] }";
  1538. string error3 = "value of 'library' element must not be blank";
  1539. rcode = parseConfiguration(config3);
  1540. ASSERT_NE(0, rcode);
  1541. EXPECT_TRUE(error_text_.find(error3) != string::npos) <<
  1542. "Error text returned from parse failure is " << error_text_;
  1543. // Element holds valid maps, except one where the library element is all
  1544. // spaces.
  1545. string config4 = "{ \"hooks-libraries\": [ "
  1546. "{ \"library\": \"/opt/lib/lib1\" }, "
  1547. "{ \"library\": \" \" } "
  1548. "] }";
  1549. string error4 = "value of 'library' element must not be blank";
  1550. rcode = parseConfiguration(config4);
  1551. ASSERT_NE(0, rcode);
  1552. EXPECT_TRUE(error_text_.find(error3) != string::npos) <<
  1553. "Error text returned from parse failure is " << error_text_;
  1554. // Element holds valid maps, except one that does not contain a
  1555. // 'library' element.
  1556. string config5 = "{ \"hooks-libraries\": [ "
  1557. "{ \"library\": \"/opt/lib/lib1\" }, "
  1558. "{ \"parameters\": { \"alpha\": 123 } }, "
  1559. "{ \"library\": \"/opt/lib/lib2\" } "
  1560. "] }";
  1561. string error5 = "one or more hooks-libraries elements are missing the"
  1562. " name of the library";
  1563. rcode = parseConfiguration(config5);
  1564. ASSERT_NE(0, rcode);
  1565. EXPECT_TRUE(error_text_.find(error5) != string::npos) <<
  1566. "Error text returned from parse failure is " << error_text_;
  1567. }
  1568. // Check that some parameters may have configuration parameters configured.
  1569. TEST_F(ParseConfigTest, HooksLibrariesParameters) {
  1570. // Check that no libraries are currently loaded
  1571. vector<string> hooks_libraries = HooksManager::getLibraryNames();
  1572. EXPECT_TRUE(hooks_libraries.empty());
  1573. // Configuration string. This contains an invalid library which should
  1574. // trigger an error in the "build" stage.
  1575. const std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1576. CALLOUT_LIBRARY_2,
  1577. CALLOUT_PARAMS_LIBRARY);
  1578. // Verify that the configuration fails to parse. (Syntactically it's OK,
  1579. // but the library is invalid).
  1580. const int rcode = parseConfiguration(config);
  1581. ASSERT_EQ(0, rcode);
  1582. // Verify that the configuration object unparses.
  1583. ConstElementPtr expected;
  1584. ASSERT_NO_THROW(expected =
  1585. Element::fromJSON(config)->get("hooks-libraries"));
  1586. ASSERT_TRUE(expected);
  1587. const HooksConfig& cfg =
  1588. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  1589. runToElementTest<HooksConfig>(expected, cfg);
  1590. // Check that the parser recorded the names.
  1591. isc::hooks::HookLibsCollection libraries = getLibraries();
  1592. ASSERT_EQ(3, libraries.size());
  1593. EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
  1594. EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1].first);
  1595. EXPECT_EQ(CALLOUT_PARAMS_LIBRARY, libraries[2].first);
  1596. // Also, check that the third library has its parameters specified.
  1597. // They were set by setHooksLibrariesConfig. The first has no
  1598. // parameters, the second one has an empty map and the third
  1599. // one has actual parameters.
  1600. EXPECT_FALSE(libraries[0].second);
  1601. EXPECT_TRUE(libraries[1].second);
  1602. ASSERT_TRUE(libraries[2].second);
  1603. // Ok, get the parameter for the third library.
  1604. ConstElementPtr params = libraries[2].second;
  1605. // It must be a map.
  1606. ASSERT_EQ(Element::map, params->getType());
  1607. // This map should have 3 parameters:
  1608. // - svalue (and will expect its value to be "string value")
  1609. // - ivalue (and will expect its value to be 42)
  1610. // - bvalue (and will expect its value to be true)
  1611. ConstElementPtr svalue = params->get("svalue");
  1612. ConstElementPtr ivalue = params->get("ivalue");
  1613. ConstElementPtr bvalue = params->get("bvalue");
  1614. // There should be no extra parameters.
  1615. EXPECT_FALSE(params->get("nonexistent"));
  1616. ASSERT_TRUE(svalue);
  1617. ASSERT_TRUE(ivalue);
  1618. ASSERT_TRUE(bvalue);
  1619. ASSERT_EQ(Element::string, svalue->getType());
  1620. ASSERT_EQ(Element::integer, ivalue->getType());
  1621. ASSERT_EQ(Element::boolean, bvalue->getType());
  1622. EXPECT_EQ("string value", svalue->stringValue());
  1623. EXPECT_EQ(42, ivalue->intValue());
  1624. EXPECT_EQ(true, bvalue->boolValue());
  1625. }
  1626. /// @brief Checks that a valid, enabled D2 client configuration works correctly.
  1627. TEST_F(ParseConfigTest, validD2Config) {
  1628. // Configuration string containing valid values.
  1629. std::string config_str =
  1630. "{ \"dhcp-ddns\" :"
  1631. " {"
  1632. " \"enable-updates\" : true, "
  1633. " \"server-ip\" : \"192.0.2.0\", "
  1634. " \"server-port\" : 3432, "
  1635. " \"sender-ip\" : \"192.0.2.1\", "
  1636. " \"sender-port\" : 3433, "
  1637. " \"max-queue-size\" : 2048, "
  1638. " \"ncr-protocol\" : \"UDP\", "
  1639. " \"ncr-format\" : \"JSON\", "
  1640. " \"always-include-fqdn\" : true, "
  1641. " \"override-no-update\" : true, "
  1642. " \"override-client-update\" : true, "
  1643. " \"replace-client-name\" : \"when-present\", "
  1644. " \"generated-prefix\" : \"test.prefix\", "
  1645. " \"qualifying-suffix\" : \"test.suffix.\" "
  1646. " }"
  1647. "}";
  1648. // Verify that the configuration string parses.
  1649. int rcode = parseConfiguration(config_str);
  1650. ASSERT_TRUE(rcode == 0) << error_text_;
  1651. // Verify that DHCP-DDNS is enabled and we can fetch the configuration.
  1652. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  1653. D2ClientConfigPtr d2_client_config;
  1654. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  1655. ASSERT_TRUE(d2_client_config);
  1656. // Verify that the configuration values are as expected.
  1657. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  1658. EXPECT_EQ("192.0.2.0", d2_client_config->getServerIp().toText());
  1659. EXPECT_EQ(3432, d2_client_config->getServerPort());
  1660. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  1661. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  1662. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  1663. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  1664. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  1665. EXPECT_EQ(D2ClientConfig::RCM_WHEN_PRESENT, d2_client_config->getReplaceClientNameMode());
  1666. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  1667. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  1668. // Verify that the configuration object unparses.
  1669. ConstElementPtr expected;
  1670. ASSERT_NO_THROW(expected = Element::fromJSON(config_str)->get("dhcp-ddns"));
  1671. ASSERT_TRUE(expected);
  1672. runToElementTest<D2ClientConfig>(expected, *d2_client_config);
  1673. // Another valid Configuration string.
  1674. // This one is disabled, has IPV6 server ip, control flags false,
  1675. // empty prefix/suffix
  1676. std::string config_str2 =
  1677. "{ \"dhcp-ddns\" :"
  1678. " {"
  1679. " \"enable-updates\" : false, "
  1680. " \"server-ip\" : \"2001:db8::\", "
  1681. " \"server-port\" : 43567, "
  1682. " \"sender-ip\" : \"2001:db8::1\", "
  1683. " \"sender-port\" : 3433, "
  1684. " \"max-queue-size\" : 2048, "
  1685. " \"ncr-protocol\" : \"UDP\", "
  1686. " \"ncr-format\" : \"JSON\", "
  1687. " \"always-include-fqdn\" : false, "
  1688. " \"override-no-update\" : false, "
  1689. " \"override-client-update\" : false, "
  1690. " \"replace-client-name\" : \"never\", "
  1691. " \"generated-prefix\" : \"\", "
  1692. " \"qualifying-suffix\" : \"\" "
  1693. " }"
  1694. "}";
  1695. // Verify that the configuration string parses.
  1696. rcode = parseConfiguration(config_str2);
  1697. ASSERT_TRUE(rcode == 0) << error_text_;
  1698. // Verify that DHCP-DDNS is disabled and we can fetch the configuration.
  1699. EXPECT_FALSE(CfgMgr::instance().ddnsEnabled());
  1700. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  1701. ASSERT_TRUE(d2_client_config);
  1702. // Verify that the configuration values are as expected.
  1703. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  1704. EXPECT_EQ("2001:db8::", d2_client_config->getServerIp().toText());
  1705. EXPECT_EQ(43567, d2_client_config->getServerPort());
  1706. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  1707. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  1708. EXPECT_FALSE(d2_client_config->getAlwaysIncludeFqdn());
  1709. EXPECT_FALSE(d2_client_config->getOverrideNoUpdate());
  1710. EXPECT_FALSE(d2_client_config->getOverrideClientUpdate());
  1711. EXPECT_EQ(D2ClientConfig::RCM_NEVER, d2_client_config->getReplaceClientNameMode());
  1712. EXPECT_EQ("", d2_client_config->getGeneratedPrefix());
  1713. EXPECT_EQ("", d2_client_config->getQualifyingSuffix());
  1714. ASSERT_NO_THROW(expected = Element::fromJSON(config_str2)->get("dhcp-ddns"));
  1715. ASSERT_TRUE(expected);
  1716. runToElementTest<D2ClientConfig>(expected, *d2_client_config);
  1717. }
  1718. /// @brief Checks that D2 client can be configured with enable flag of
  1719. /// false only.
  1720. TEST_F(ParseConfigTest, validDisabledD2Config) {
  1721. // Configuration string. This defines a disabled D2 client config.
  1722. std::string config_str =
  1723. "{ \"dhcp-ddns\" :"
  1724. " {"
  1725. " \"enable-updates\" : false"
  1726. " }"
  1727. "}";
  1728. // Verify that the configuration string parses.
  1729. int rcode = parseConfiguration(config_str);
  1730. ASSERT_TRUE(rcode == 0) << error_text_;
  1731. // Verify that DHCP-DDNS is disabled.
  1732. EXPECT_FALSE(CfgMgr::instance().ddnsEnabled());
  1733. // Make sure fetched config agrees.
  1734. D2ClientConfigPtr d2_client_config;
  1735. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  1736. EXPECT_TRUE(d2_client_config);
  1737. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  1738. }
  1739. /// @brief Checks that given a partial configuration, parser supplies
  1740. /// default values
  1741. TEST_F(ParseConfigTest, parserDefaultsD2Config) {
  1742. // Configuration string. This defines an enabled D2 client config
  1743. // with the mandatory parameter in such a case, all other parameters
  1744. // are optional and their default values will be used.
  1745. std::string config_str =
  1746. "{ \"dhcp-ddns\" :"
  1747. " {"
  1748. " \"enable-updates\" : true, "
  1749. " \"qualifying-suffix\" : \"test.suffix.\" "
  1750. " }"
  1751. "}";
  1752. // Verify that the configuration string parses.
  1753. int rcode = parseConfiguration(config_str);
  1754. ASSERT_TRUE(rcode == 0) << error_text_;
  1755. // Verify that DHCP-DDNS is enabled.
  1756. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  1757. // Make sure fetched config is correct.
  1758. D2ClientConfigPtr d2_client_config;
  1759. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  1760. EXPECT_TRUE(d2_client_config);
  1761. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  1762. EXPECT_EQ(D2ClientConfig::DFT_SERVER_IP,
  1763. d2_client_config->getServerIp().toText());
  1764. EXPECT_EQ(D2ClientConfig::DFT_SERVER_PORT,
  1765. d2_client_config->getServerPort());
  1766. EXPECT_EQ(dhcp_ddns::stringToNcrProtocol(D2ClientConfig::DFT_NCR_PROTOCOL),
  1767. d2_client_config->getNcrProtocol());
  1768. EXPECT_EQ(dhcp_ddns::stringToNcrFormat(D2ClientConfig::DFT_NCR_FORMAT),
  1769. d2_client_config->getNcrFormat());
  1770. EXPECT_EQ(D2ClientConfig::DFT_ALWAYS_INCLUDE_FQDN,
  1771. d2_client_config->getAlwaysIncludeFqdn());
  1772. EXPECT_EQ(D2ClientConfig::DFT_OVERRIDE_NO_UPDATE,
  1773. d2_client_config->getOverrideNoUpdate());
  1774. EXPECT_EQ(D2ClientConfig::DFT_OVERRIDE_CLIENT_UPDATE,
  1775. d2_client_config->getOverrideClientUpdate());
  1776. EXPECT_EQ(D2ClientConfig::
  1777. stringToReplaceClientNameMode(D2ClientConfig::
  1778. DFT_REPLACE_CLIENT_NAME_MODE),
  1779. d2_client_config->getReplaceClientNameMode());
  1780. EXPECT_EQ(D2ClientConfig::DFT_GENERATED_PREFIX,
  1781. d2_client_config->getGeneratedPrefix());
  1782. EXPECT_EQ("test.suffix.",
  1783. d2_client_config->getQualifyingSuffix());
  1784. }
  1785. /// @brief Check various invalid D2 client configurations.
  1786. TEST_F(ParseConfigTest, invalidD2Config) {
  1787. std::string invalid_configs[] = {
  1788. // Must supply qualifying-suffix when updates are enabled
  1789. "{ \"dhcp-ddns\" :"
  1790. " {"
  1791. " \"enable-updates\" : true"
  1792. " }"
  1793. "}",
  1794. // Invalid server ip value
  1795. "{ \"dhcp-ddns\" :"
  1796. " {"
  1797. " \"enable-updates\" : true, "
  1798. " \"server-ip\" : \"x192.0.2.0\", "
  1799. " \"server-port\" : 53001, "
  1800. " \"ncr-protocol\" : \"UDP\", "
  1801. " \"ncr-format\" : \"JSON\", "
  1802. " \"always-include-fqdn\" : true, "
  1803. " \"override-no-update\" : true, "
  1804. " \"override-client-update\" : true, "
  1805. " \"replace-client-name\" : true, "
  1806. " \"generated-prefix\" : \"test.prefix\", "
  1807. " \"qualifying-suffix\" : \"test.suffix.\" "
  1808. " }"
  1809. "}",
  1810. // Unknown protocol
  1811. "{ \"dhcp-ddns\" :"
  1812. " {"
  1813. " \"enable-updates\" : true, "
  1814. " \"server-ip\" : \"192.0.2.0\", "
  1815. " \"server-port\" : 53001, "
  1816. " \"ncr-protocol\" : \"Bogus\", "
  1817. " \"ncr-format\" : \"JSON\", "
  1818. " \"always-include-fqdn\" : true, "
  1819. " \"override-no-update\" : true, "
  1820. " \"override-client-update\" : true, "
  1821. " \"replace-client-name\" : true, "
  1822. " \"generated-prefix\" : \"test.prefix\", "
  1823. " \"qualifying-suffix\" : \"test.suffix.\" "
  1824. " }"
  1825. "}",
  1826. // Unsupported protocol
  1827. "{ \"dhcp-ddns\" :"
  1828. " {"
  1829. " \"enable-updates\" : true, "
  1830. " \"server-ip\" : \"192.0.2.0\", "
  1831. " \"server-port\" : 53001, "
  1832. " \"ncr-protocol\" : \"TCP\", "
  1833. " \"ncr-format\" : \"JSON\", "
  1834. " \"always-include-fqdn\" : true, "
  1835. " \"override-no-update\" : true, "
  1836. " \"override-client-update\" : true, "
  1837. " \"replace-client-name\" : true, "
  1838. " \"generated-prefix\" : \"test.prefix\", "
  1839. " \"qualifying-suffix\" : \"test.suffix.\" "
  1840. " }"
  1841. "}",
  1842. // Unknown format
  1843. "{ \"dhcp-ddns\" :"
  1844. " {"
  1845. " \"enable-updates\" : true, "
  1846. " \"server-ip\" : \"192.0.2.0\", "
  1847. " \"server-port\" : 53001, "
  1848. " \"ncr-protocol\" : \"UDP\", "
  1849. " \"ncr-format\" : \"Bogus\", "
  1850. " \"always-include-fqdn\" : true, "
  1851. " \"override-no-update\" : true, "
  1852. " \"override-client-update\" : true, "
  1853. " \"replace-client-name\" : true, "
  1854. " \"generated-prefix\" : \"test.prefix\", "
  1855. " \"qualifying-suffix\" : \"test.suffix.\" "
  1856. " }"
  1857. "}",
  1858. // Invalid Port
  1859. "{ \"dhcp-ddns\" :"
  1860. " {"
  1861. " \"enable-updates\" : true, "
  1862. " \"server-ip\" : \"192.0.2.0\", "
  1863. " \"server-port\" : \"bogus\", "
  1864. " \"ncr-protocol\" : \"UDP\", "
  1865. " \"ncr-format\" : \"JSON\", "
  1866. " \"always-include-fqdn\" : true, "
  1867. " \"override-no-update\" : true, "
  1868. " \"override-client-update\" : true, "
  1869. " \"replace-client-name\" : true, "
  1870. " \"generated-prefix\" : \"test.prefix\", "
  1871. " \"qualifying-suffix\" : \"test.suffix.\" "
  1872. " }"
  1873. "}",
  1874. // Mismatched server and sender IPs
  1875. "{ \"dhcp-ddns\" :"
  1876. " {"
  1877. " \"enable-updates\" : true, "
  1878. " \"server-ip\" : \"192.0.2.0\", "
  1879. " \"server-port\" : 3432, "
  1880. " \"sender-ip\" : \"3001::5\", "
  1881. " \"sender-port\" : 3433, "
  1882. " \"max-queue-size\" : 2048, "
  1883. " \"ncr-protocol\" : \"UDP\", "
  1884. " \"ncr-format\" : \"JSON\", "
  1885. " \"always-include-fqdn\" : true, "
  1886. " \"override-no-update\" : true, "
  1887. " \"override-client-update\" : true, "
  1888. " \"replace-client-name\" : true, "
  1889. " \"generated-prefix\" : \"test.prefix\", "
  1890. " \"qualifying-suffix\" : \"test.suffix.\" "
  1891. " }"
  1892. "}",
  1893. // Identical server and sender IP/port
  1894. "{ \"dhcp-ddns\" :"
  1895. " {"
  1896. " \"enable-updates\" : true, "
  1897. " \"server-ip\" : \"3001::5\", "
  1898. " \"server-port\" : 3433, "
  1899. " \"sender-ip\" : \"3001::5\", "
  1900. " \"sender-port\" : 3433, "
  1901. " \"max-queue-size\" : 2048, "
  1902. " \"ncr-protocol\" : \"UDP\", "
  1903. " \"ncr-format\" : \"JSON\", "
  1904. " \"always-include-fqdn\" : true, "
  1905. " \"override-no-update\" : true, "
  1906. " \"override-client-update\" : true, "
  1907. " \"replace-client-name\" : true, "
  1908. " \"generated-prefix\" : \"test.prefix\", "
  1909. " \"qualifying-suffix\" : \"test.suffix.\" "
  1910. " }"
  1911. "}",
  1912. // Invalid replace-client-name value
  1913. "{ \"dhcp-ddns\" :"
  1914. " {"
  1915. " \"enable-updates\" : true, "
  1916. " \"server-ip\" : \"3001::5\", "
  1917. " \"server-port\" : 3433, "
  1918. " \"sender-ip\" : \"3001::5\", "
  1919. " \"sender-port\" : 3434, "
  1920. " \"max-queue-size\" : 2048, "
  1921. " \"ncr-protocol\" : \"UDP\", "
  1922. " \"ncr-format\" : \"JSON\", "
  1923. " \"always-include-fqdn\" : true, "
  1924. " \"override-no-update\" : true, "
  1925. " \"override-client-update\" : true, "
  1926. " \"replace-client-name\" : \"BOGUS\", "
  1927. " \"generated-prefix\" : \"test.prefix\", "
  1928. " \"qualifying-suffix\" : \"test.suffix.\" "
  1929. " }"
  1930. "}",
  1931. // stop
  1932. ""
  1933. };
  1934. // Fetch the original config.
  1935. D2ClientConfigPtr original_config;
  1936. ASSERT_NO_THROW(original_config = CfgMgr::instance().getD2ClientConfig());
  1937. // Iterate through the invalid configuration strings, attempting to
  1938. // parse each one. They should fail to parse, but fail gracefully.
  1939. D2ClientConfigPtr current_config;
  1940. int i = 0;
  1941. while (!invalid_configs[i].empty()) {
  1942. // Verify that the configuration string parses without throwing.
  1943. int rcode = parseConfiguration(invalid_configs[i]);
  1944. // Verify that parse result indicates a parsing error.
  1945. ASSERT_TRUE(rcode != 0) << "Invalid config #: " << i
  1946. << " should not have passed!";
  1947. // Verify that the "official" config still matches the original config.
  1948. ASSERT_NO_THROW(current_config =
  1949. CfgMgr::instance().getD2ClientConfig());
  1950. EXPECT_EQ(*original_config, *current_config);
  1951. ++i;
  1952. }
  1953. }
  1954. /// @brief Checks that a valid relay info structure for IPv4 can be handled
  1955. TEST_F(ParseConfigTest, validRelayInfo4) {
  1956. // Relay information structure. Very simple for now.
  1957. std::string config_str =
  1958. " {"
  1959. " \"ip-address\" : \"192.0.2.1\""
  1960. " }";
  1961. ElementPtr json = Element::fromJSON(config_str);
  1962. // We need to set the default ip-address to something.
  1963. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("0.0.0.0")));
  1964. RelayInfoParser parser(Option::V4);
  1965. // Subnet4 parser will pass 0.0.0.0 to the RelayInfoParser
  1966. EXPECT_NO_THROW(parser.parse(result, json));
  1967. EXPECT_EQ("192.0.2.1", result->addr_.toText());
  1968. }
  1969. /// @brief Checks that a bogus relay info structure for IPv4 is rejected.
  1970. TEST_F(ParseConfigTest, bogusRelayInfo4) {
  1971. // Invalid config (wrong family type of the ip-address field)
  1972. std::string config_str_bogus1 =
  1973. " {"
  1974. " \"ip-address\" : \"2001:db8::1\""
  1975. " }";
  1976. ElementPtr json_bogus1 = Element::fromJSON(config_str_bogus1);
  1977. // Invalid config (that thing is not an IPv4 address)
  1978. std::string config_str_bogus2 =
  1979. " {"
  1980. " \"ip-address\" : \"256.345.123.456\""
  1981. " }";
  1982. ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2);
  1983. // Invalid config (ip-address is mandatory)
  1984. std::string config_str_bogus3 =
  1985. " {"
  1986. " }";
  1987. ElementPtr json_bogus3 = Element::fromJSON(config_str_bogus3);
  1988. // We need to set the default ip-address to something.
  1989. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(IOAddress::IPV4_ZERO_ADDRESS()));
  1990. RelayInfoParser parser(Option::V4);
  1991. // wrong family type
  1992. EXPECT_THROW(parser.parse(result, json_bogus1), DhcpConfigError);
  1993. // Too large byte values in pseudo-IPv4 addr
  1994. EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError);
  1995. // Mandatory ip-address is missing. What a pity.
  1996. EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError);
  1997. }
  1998. /// @brief Checks that a valid relay info structure for IPv6 can be handled
  1999. TEST_F(ParseConfigTest, validRelayInfo6) {
  2000. // Relay information structure. Very simple for now.
  2001. std::string config_str =
  2002. " {"
  2003. " \"ip-address\" : \"2001:db8::1\""
  2004. " }";
  2005. ElementPtr json = Element::fromJSON(config_str);
  2006. // We need to set the default ip-address to something.
  2007. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::")));
  2008. RelayInfoParser parser(Option::V6);
  2009. // Subnet4 parser will pass :: to the RelayInfoParser
  2010. EXPECT_NO_THROW(parser.parse(result, json));
  2011. EXPECT_EQ("2001:db8::1", result->addr_.toText());
  2012. }
  2013. /// @brief Checks that a valid relay info structure for IPv6 can be handled
  2014. TEST_F(ParseConfigTest, bogusRelayInfo6) {
  2015. // Invalid config (wrong family type of the ip-address field
  2016. std::string config_str_bogus1 =
  2017. " {"
  2018. " \"ip-address\" : \"192.0.2.1\""
  2019. " }";
  2020. ElementPtr json_bogus1 = Element::fromJSON(config_str_bogus1);
  2021. // That IPv6 address doesn't look right
  2022. std::string config_str_bogus2 =
  2023. " {"
  2024. " \"ip-address\" : \"2001:db8:::4\""
  2025. " }";
  2026. ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2);
  2027. // Missing mandatory ip-address field.
  2028. std::string config_str_bogus3 =
  2029. " {"
  2030. " }";
  2031. ElementPtr json_bogus3 = Element::fromJSON(config_str_bogus3);
  2032. // We need to set the default ip-address to something.
  2033. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::")));
  2034. RelayInfoParser parser(Option::V6);
  2035. // Negative scenario (wrong family type)
  2036. EXPECT_THROW(parser.parse(result, json_bogus1), DhcpConfigError);
  2037. // Looks like IPv6 address, but has too many colons
  2038. EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError);
  2039. // Mandatory ip-address is missing. What a pity.
  2040. EXPECT_THROW(parser.parse(result, json_bogus3), DhcpConfigError);
  2041. }
  2042. // There's no test for ControlSocketParser, as it is tested in the DHCPv4 code
  2043. // (see CtrlDhcpv4SrvTest.commandSocketBasic in
  2044. // src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc).
  2045. }; // Anonymous namespace