dhcp_parsers_unittest.cc 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. // Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <config/ccsession.h>
  16. #include <cc/data.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_custom.h>
  19. #include <dhcp/option_int.h>
  20. #include <dhcp/option6_addrlst.h>
  21. #include <dhcp/tests/iface_mgr_test_config.h>
  22. #include <dhcpsrv/cfgmgr.h>
  23. #include <dhcpsrv/subnet.h>
  24. #include <dhcpsrv/dhcp_parsers.h>
  25. #include <dhcpsrv/tests/test_libraries.h>
  26. #include <dhcpsrv/testutils/config_result_check.h>
  27. #include <exceptions/exceptions.h>
  28. #include <hooks/hooks_manager.h>
  29. #include <gtest/gtest.h>
  30. #include <boost/foreach.hpp>
  31. #include <boost/pointer_cast.hpp>
  32. #include <map>
  33. #include <string>
  34. using namespace std;
  35. using namespace isc;
  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. namespace {
  42. /// @brief DHCP Parser test fixture class
  43. class DhcpParserTest : public ::testing::Test {
  44. public:
  45. /// @brief Constructor
  46. DhcpParserTest() {
  47. resetIfaceCfg();
  48. }
  49. /// @brief Destructor.
  50. virtual ~DhcpParserTest() {
  51. resetIfaceCfg();
  52. }
  53. /// @brief Resets selection of the interfaces from previous tests.
  54. void resetIfaceCfg() {
  55. CfgMgr::instance().clear();
  56. }
  57. };
  58. /// @brief Check BooleanParser basic functionality.
  59. ///
  60. /// Verifies that the parser:
  61. /// 1. Does not allow empty for storage.
  62. /// 2. Rejects a non-boolean element.
  63. /// 3. Builds with a valid true value.
  64. /// 4. Bbuils with a valid false value.
  65. /// 5. Updates storage upon commit.
  66. TEST_F(DhcpParserTest, booleanParserTest) {
  67. const std::string name = "boolParm";
  68. // Verify that parser does not allow empty for storage.
  69. BooleanStoragePtr bs;
  70. EXPECT_THROW(BooleanParser(name, bs), isc::dhcp::DhcpConfigError);
  71. // Construct parser for testing.
  72. BooleanStoragePtr storage(new BooleanStorage());
  73. BooleanParser parser(name, storage);
  74. // Verify that parser with rejects a non-boolean element.
  75. ElementPtr wrong_element = Element::create("I am a string");
  76. EXPECT_THROW(parser.build(wrong_element), isc::BadValue);
  77. // Verify that parser will build with a valid true value.
  78. bool test_value = true;
  79. ElementPtr element = Element::create(test_value);
  80. ASSERT_NO_THROW(parser.build(element));
  81. // Verify that commit updates storage.
  82. bool actual_value = !test_value;
  83. parser.commit();
  84. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  85. EXPECT_EQ(test_value, actual_value);
  86. // Verify that parser will build with a valid false value.
  87. test_value = false;
  88. element->setValue(test_value);
  89. EXPECT_NO_THROW(parser.build(element));
  90. // Verify that commit updates storage.
  91. actual_value = ~test_value;
  92. parser.commit();
  93. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  94. EXPECT_EQ(test_value, actual_value);
  95. }
  96. /// @brief Check StringParser basic functionality
  97. ///
  98. /// Verifies that the parser:
  99. /// 1. Does not allow empty for storage.
  100. /// 2. Builds with a nont string value.
  101. /// 3. Builds with a string value.
  102. /// 4. Updates storage upon commit.
  103. TEST_F(DhcpParserTest, stringParserTest) {
  104. const std::string name = "strParm";
  105. // Verify that parser does not allow empty for storage.
  106. StringStoragePtr bs;
  107. EXPECT_THROW(StringParser(name, bs), isc::dhcp::DhcpConfigError);
  108. // Construct parser for testing.
  109. StringStoragePtr storage(new StringStorage());
  110. StringParser parser(name, storage);
  111. // Verify that parser with accepts a non-string element.
  112. ElementPtr element = Element::create(9999);
  113. EXPECT_NO_THROW(parser.build(element));
  114. // Verify that commit updates storage.
  115. parser.commit();
  116. std::string actual_value;
  117. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  118. EXPECT_EQ("9999", actual_value);
  119. // Verify that parser will build with a string value.
  120. const std::string test_value = "test value";
  121. element = Element::create(test_value);
  122. ASSERT_NO_THROW(parser.build(element));
  123. // Verify that commit updates storage.
  124. parser.commit();
  125. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  126. EXPECT_EQ(test_value, actual_value);
  127. }
  128. /// @brief Check Uint32Parser basic functionality
  129. ///
  130. /// Verifies that the parser:
  131. /// 1. Does not allow empty for storage.
  132. /// 2. Rejects a non-integer element.
  133. /// 3. Rejects a negative value.
  134. /// 4. Rejects too large a value.
  135. /// 5. Builds with value of zero.
  136. /// 6. Builds with a value greater than zero.
  137. /// 7. Updates storage upon commit.
  138. TEST_F(DhcpParserTest, uint32ParserTest) {
  139. const std::string name = "intParm";
  140. // Verify that parser does not allow empty for storage.
  141. Uint32StoragePtr bs;
  142. EXPECT_THROW(Uint32Parser(name, bs), isc::dhcp::DhcpConfigError);
  143. // Construct parser for testing.
  144. Uint32StoragePtr storage(new Uint32Storage());
  145. Uint32Parser parser(name, storage);
  146. // Verify that parser with rejects a non-interger element.
  147. ElementPtr wrong_element = Element::create("I am a string");
  148. EXPECT_THROW(parser.build(wrong_element), isc::BadValue);
  149. // Verify that parser with rejects a negative value.
  150. ElementPtr int_element = Element::create(-1);
  151. EXPECT_THROW(parser.build(int_element), isc::BadValue);
  152. // Verify that parser with rejects too large a value provided we are on
  153. // 64-bit platform.
  154. if (sizeof(long) > sizeof(uint32_t)) {
  155. long max = (long)(std::numeric_limits<uint32_t>::max()) + 1;
  156. int_element->setValue(max);
  157. EXPECT_THROW(parser.build(int_element), isc::BadValue);
  158. }
  159. // Verify that parser will build with value of zero.
  160. int test_value = 0;
  161. int_element->setValue((long)test_value);
  162. ASSERT_NO_THROW(parser.build(int_element));
  163. // Verify that commit updates storage.
  164. parser.commit();
  165. uint32_t actual_value = 0;
  166. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  167. EXPECT_EQ(test_value, actual_value);
  168. // Verify that parser will build with a valid positive value.
  169. test_value = 77;
  170. int_element->setValue((long)test_value);
  171. ASSERT_NO_THROW(parser.build(int_element));
  172. // Verify that commit updates storage.
  173. parser.commit();
  174. EXPECT_NO_THROW((actual_value = storage->getParam(name)));
  175. EXPECT_EQ(test_value, actual_value);
  176. }
  177. /// @brief Check InterfaceListParser basic functionality
  178. ///
  179. /// Verifies that the parser:
  180. /// 1. Does not allow empty for storage.
  181. /// 2. Does not allow name other than "interfaces"
  182. /// 3. Parses list of interfaces and adds them to CfgMgr
  183. /// 4. Parses wildcard interface name and sets a CfgMgr flag which indicates
  184. /// that server will listen on all interfaces.
  185. TEST_F(DhcpParserTest, interfaceListParserTest) {
  186. IfaceMgrTestConfig test_config(true);
  187. const std::string name = "interfaces";
  188. ParserContextPtr parser_context(new ParserContext(Option::V4));
  189. // Verify that parser constructor fails if parameter name isn't "interface"
  190. EXPECT_THROW(InterfaceListConfigParser("bogus_name", parser_context),
  191. isc::BadValue);
  192. boost::scoped_ptr<InterfaceListConfigParser>
  193. parser(new InterfaceListConfigParser(name, parser_context));
  194. ElementPtr list_element = Element::createList();
  195. list_element->add(Element::create("eth0"));
  196. // This should parse the configuration and add eth0 and eth1 to the list
  197. // of interfaces that server should listen on.
  198. parser->build(list_element);
  199. parser->commit();
  200. // Use CfgMgr instance to check if eth0 and eth1 was added, and that
  201. // eth2 was not added.
  202. SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
  203. ASSERT_TRUE(cfg);
  204. ASSERT_NO_THROW(cfg->getCfgIface().openSockets(AF_INET, 10000));
  205. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET));
  206. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET));
  207. // Add keyword all to the configuration. This should activate all
  208. // interfaces, including eth2, even though it has not been explicitly
  209. // added.
  210. list_element->add(Element::create("*"));
  211. // Reset parser and configuration.
  212. parser.reset(new InterfaceListConfigParser(name, parser_context));
  213. cfg->getCfgIface().closeSockets();
  214. CfgMgr::instance().clear();
  215. parser->build(list_element);
  216. parser->commit();
  217. cfg = CfgMgr::instance().getStagingCfg();
  218. ASSERT_NO_THROW(cfg->getCfgIface().openSockets(AF_INET, 10000));
  219. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET));
  220. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET));
  221. }
  222. /// @brief Test Fixture class which provides basic structure for testing
  223. /// configuration parsing. This is essentially the same structure provided
  224. /// by dhcp servers.
  225. class ParseConfigTest : public ::testing::Test {
  226. public:
  227. /// @brief Constructor
  228. ParseConfigTest() {
  229. reset_context();
  230. CfgMgr::instance().clear();
  231. }
  232. ~ParseConfigTest() {
  233. reset_context();
  234. CfgMgr::instance().clear();
  235. }
  236. /// @brief Parses a configuration.
  237. ///
  238. /// Parse the given configuration, populating the context storage with
  239. /// the parsed elements.
  240. ///
  241. /// @param config_set is the set of elements to parse.
  242. /// @return returns an ConstElementPtr containing the numeric result
  243. /// code and outcome comment.
  244. isc::data::ConstElementPtr parseElementSet(isc::data::ConstElementPtr
  245. config_set) {
  246. // Answer will hold the result.
  247. ConstElementPtr answer;
  248. if (!config_set) {
  249. answer = isc::config::createAnswer(1,
  250. string("Can't parse NULL config"));
  251. return (answer);
  252. }
  253. // option parsing must be done last, so save it if we hit if first
  254. ParserPtr option_parser;
  255. ConfigPair config_pair;
  256. try {
  257. // Iterate over the config elements.
  258. const std::map<std::string, ConstElementPtr>& values_map =
  259. config_set->mapValue();
  260. BOOST_FOREACH(config_pair, values_map) {
  261. // Create the parser based on element name.
  262. ParserPtr parser(createConfigParser(config_pair.first));
  263. // Options must be parsed last
  264. if (config_pair.first == "option-data") {
  265. option_parser = parser;
  266. } else {
  267. // Anything else we can call build straight away.
  268. parser->build(config_pair.second);
  269. parser->commit();
  270. }
  271. }
  272. // The option values parser is the next one to be run.
  273. std::map<std::string, ConstElementPtr>::const_iterator
  274. option_config = values_map.find("option-data");
  275. if (option_config != values_map.end()) {
  276. option_parser->build(option_config->second);
  277. option_parser->commit();
  278. }
  279. // Everything was fine. Configuration is successful.
  280. answer = isc::config::createAnswer(0, "Configuration committed.");
  281. } catch (const isc::Exception& ex) {
  282. answer = isc::config::createAnswer(1,
  283. string("Configuration parsing failed: ") + ex.what());
  284. } catch (...) {
  285. answer = isc::config::createAnswer(1,
  286. string("Configuration parsing failed"));
  287. }
  288. return (answer);
  289. }
  290. /// @brief Create an element parser based on the element name.
  291. ///
  292. /// Creates a parser for the appropriate element and stores a pointer to it
  293. /// in the appropriate class variable.
  294. ///
  295. /// Note that the method currently it only supports option-defs, option-data
  296. /// and hooks-libraries.
  297. ///
  298. /// @param config_id is the name of the configuration element.
  299. ///
  300. /// @return returns a shared pointer to DhcpConfigParser.
  301. ///
  302. /// @throw throws NotImplemented if element name isn't supported.
  303. ParserPtr createConfigParser(const std::string& config_id) {
  304. ParserPtr parser;
  305. int family = parser_context_->universe_ == Option::V4 ?
  306. AF_INET : AF_INET6;
  307. if (config_id.compare("option-data") == 0) {
  308. parser.reset(new OptionDataListParser(config_id, CfgOptionPtr(),
  309. family));
  310. } else if (config_id.compare("option-def") == 0) {
  311. parser.reset(new OptionDefListParser(config_id,
  312. parser_context_));
  313. } else if (config_id.compare("hooks-libraries") == 0) {
  314. parser.reset(new HooksLibrariesParser(config_id));
  315. hooks_libraries_parser_ =
  316. boost::dynamic_pointer_cast<HooksLibrariesParser>(parser);
  317. } else if (config_id.compare("dhcp-ddns") == 0) {
  318. parser.reset(new D2ClientConfigParser(config_id));
  319. } else {
  320. isc_throw(NotImplemented,
  321. "Parser error: configuration parameter not supported: "
  322. << config_id);
  323. }
  324. return (parser);
  325. }
  326. /// @brief Convenience method for parsing a configuration
  327. ///
  328. /// Given a configuration string, convert it into Elements
  329. /// and parse them.
  330. /// @param config is the configuration string to parse
  331. ///
  332. /// @return retuns 0 if the configuration parsed successfully,
  333. /// non-zero otherwise failure.
  334. int parseConfiguration(const std::string& config) {
  335. int rcode_ = 1;
  336. // Turn config into elements.
  337. // Test json just to make sure its valid.
  338. ElementPtr json = Element::fromJSON(config);
  339. EXPECT_TRUE(json);
  340. if (json) {
  341. ConstElementPtr status = parseElementSet(json);
  342. ConstElementPtr comment = parseAnswer(rcode_, status);
  343. error_text_ = comment->stringValue();
  344. // If error was reported, the error string should contain
  345. // position of the data element which caused failure.
  346. if (rcode_ != 0) {
  347. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  348. }
  349. }
  350. return (rcode_);
  351. }
  352. /// @brief Find an option for a given space and code within the parser
  353. /// context.
  354. /// @param space is the space name of the desired option.
  355. /// @param code is the numeric "type" of the desired option.
  356. /// @return returns an OptionPtr which points to the found
  357. /// option or is empty.
  358. /// ASSERT_ tests don't work inside functions that return values
  359. OptionPtr getOptionPtr(std::string space, uint32_t code)
  360. {
  361. OptionPtr option_ptr;
  362. OptionContainerPtr options = CfgMgr::instance().getStagingCfg()->
  363. getCfgOption()->getAll(space);
  364. // Should always be able to get options list even if it is empty.
  365. EXPECT_TRUE(options);
  366. if (options) {
  367. // Attempt to find desired option.
  368. const OptionContainerTypeIndex& idx = options->get<1>();
  369. const OptionContainerTypeRange& range = idx.equal_range(code);
  370. int cnt = std::distance(range.first, range.second);
  371. EXPECT_EQ(1, cnt);
  372. if (cnt == 1) {
  373. OptionDescriptor desc = *(idx.begin());
  374. option_ptr = desc.option_;
  375. EXPECT_TRUE(option_ptr);
  376. }
  377. }
  378. return (option_ptr);
  379. }
  380. /// @brief Wipes the contents of the context to allowing another parsing
  381. /// during a given test if needed.
  382. void reset_context(){
  383. // Note set context universe to V6 as it has to be something.
  384. CfgMgr::instance().clear();
  385. parser_context_.reset(new ParserContext(Option::V6));
  386. // Ensure no hooks libraries are loaded.
  387. HooksManager::unloadLibraries();
  388. // Set it to minimal, disabled config
  389. D2ClientConfigPtr tmp(new D2ClientConfig());
  390. CfgMgr::instance().setD2ClientConfig(tmp);
  391. }
  392. /// @brief Parsers used in the parsing of the configuration
  393. ///
  394. /// Allows the tests to interrogate the state of the parsers (if required).
  395. boost::shared_ptr<HooksLibrariesParser> hooks_libraries_parser_;
  396. /// @brief Parser context - provides storage for options and definitions
  397. ParserContextPtr parser_context_;
  398. /// @brief Error string if the parsing failed
  399. std::string error_text_;
  400. };
  401. /// @brief Check Basic parsing of option definitions.
  402. ///
  403. /// Note that this tests basic operation of the OptionDefinitionListParser and
  404. /// OptionDefinitionParser. It uses a simple configuration consisting of one
  405. /// one definition and verifies that it is parsed and committed to storage
  406. /// correctly.
  407. TEST_F(ParseConfigTest, basicOptionDefTest) {
  408. // Configuration string.
  409. std::string config =
  410. "{ \"option-def\": [ {"
  411. " \"name\": \"foo\","
  412. " \"code\": 100,"
  413. " \"type\": \"ipv4-address\","
  414. " \"array\": False,"
  415. " \"record-types\": \"\","
  416. " \"space\": \"isc\","
  417. " \"encapsulate\": \"\""
  418. " } ]"
  419. "}";
  420. // Verify that the configuration string parses.
  421. int rcode = parseConfiguration(config);
  422. ASSERT_TRUE(rcode == 0);
  423. // Verify that the option definition can be retrieved.
  424. OptionDefinitionPtr def =
  425. CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  426. ASSERT_TRUE(def);
  427. // Verify that the option definition is correct.
  428. EXPECT_EQ("foo", def->getName());
  429. EXPECT_EQ(100, def->getCode());
  430. EXPECT_FALSE(def->getArrayType());
  431. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  432. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  433. }
  434. /// @brief Check Basic parsing of options.
  435. ///
  436. /// Note that this tests basic operation of the OptionDataListParser and
  437. /// OptionDataParser. It uses a simple configuration consisting of one
  438. /// one definition and matching option data. It verifies that the option
  439. /// is parsed and committed to storage correctly.
  440. TEST_F(ParseConfigTest, basicOptionDataTest) {
  441. // Configuration string.
  442. std::string config =
  443. "{ \"option-def\": [ {"
  444. " \"name\": \"foo\","
  445. " \"code\": 100,"
  446. " \"type\": \"ipv4-address\","
  447. " \"array\": False,"
  448. " \"record-types\": \"\","
  449. " \"space\": \"isc\","
  450. " \"encapsulate\": \"\""
  451. " } ], "
  452. " \"option-data\": [ {"
  453. " \"name\": \"foo\","
  454. " \"space\": \"isc\","
  455. " \"code\": 100,"
  456. " \"data\": \"192.0.2.0\","
  457. " \"csv-format\": True"
  458. " } ]"
  459. "}";
  460. // Verify that the configuration string parses.
  461. int rcode = parseConfiguration(config);
  462. ASSERT_TRUE(rcode == 0);
  463. // Verify that the option can be retrieved.
  464. OptionPtr opt_ptr = getOptionPtr("isc", 100);
  465. ASSERT_TRUE(opt_ptr);
  466. // Verify that the option definition is correct.
  467. std::string val = "type=100, len=4, data fields:\n "
  468. " #0 192.0.2.0 ( ipv4-address ) \n";
  469. EXPECT_EQ(val, opt_ptr->toText());
  470. }
  471. // This test checks behavior of the configuration parser for option data
  472. // for different values of csv-format parameter and when there is an option
  473. // definition present.
  474. TEST_F(ParseConfigTest, optionDataCSVFormatWithOptionDef) {
  475. std::string config =
  476. "{ \"option-data\": [ {"
  477. " \"name\": \"swap-server\","
  478. " \"space\": \"dhcp4\","
  479. " \"code\": 16,"
  480. " \"data\": \"192.0.2.0\""
  481. " } ]"
  482. "}";
  483. // The default universe is V6. We need to change it to use dhcp4 option
  484. // space.
  485. parser_context_->universe_ = Option::V4;
  486. int rcode = 0;
  487. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  488. ASSERT_EQ(0, rcode);
  489. // Verify that the option data is correct.
  490. OptionCustomPtr addr_opt = boost::dynamic_pointer_cast<
  491. OptionCustom>(getOptionPtr("dhcp4", 16));
  492. ASSERT_TRUE(addr_opt);
  493. EXPECT_EQ("192.0.2.0", addr_opt->readAddress().toText());
  494. // Explicitly enable csv-format.
  495. CfgMgr::instance().clear();
  496. config =
  497. "{ \"option-data\": [ {"
  498. " \"name\": \"swap-server\","
  499. " \"space\": \"dhcp4\","
  500. " \"code\": 16,"
  501. " \"csv-format\": True,"
  502. " \"data\": \"192.0.2.0\""
  503. " } ]"
  504. "}";
  505. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  506. ASSERT_EQ(0, rcode);
  507. // Verify that the option data is correct.
  508. addr_opt = boost::dynamic_pointer_cast<
  509. OptionCustom>(getOptionPtr("dhcp4", 16));
  510. ASSERT_TRUE(addr_opt);
  511. EXPECT_EQ("192.0.2.0", addr_opt->readAddress().toText());
  512. // Explicitly disable csv-format and use hex instead.
  513. CfgMgr::instance().clear();
  514. config =
  515. "{ \"option-data\": [ {"
  516. " \"name\": \"swap-server\","
  517. " \"space\": \"dhcp4\","
  518. " \"code\": 16,"
  519. " \"csv-format\": False,"
  520. " \"data\": \"C0000200\""
  521. " } ]"
  522. "}";
  523. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  524. ASSERT_EQ(0, rcode);
  525. // Verify that the option data is correct.
  526. addr_opt = boost::dynamic_pointer_cast<
  527. OptionCustom>(getOptionPtr("dhcp4", 16));
  528. ASSERT_TRUE(addr_opt);
  529. EXPECT_EQ("192.0.2.0", addr_opt->readAddress().toText());
  530. }
  531. // This test checks behavior of the configuration parser for option data
  532. // for different values of csv-format parameter and when there is no
  533. // option definition.
  534. TEST_F(ParseConfigTest, optionDataCSVFormatNoOptionDef) {
  535. // This option doesn't have any definition. It is ok to use such
  536. // an option but the data should be specified in hex, not as CSV.
  537. // Note that the parser will by default use the CSV format for the
  538. // data but only in case there is a suitable option definition.
  539. std::string config =
  540. "{ \"option-data\": [ {"
  541. " \"name\": \"foo-name\","
  542. " \"space\": \"dhcp6\","
  543. " \"code\": 25000,"
  544. " \"data\": \"1, 2, 5\""
  545. " } ]"
  546. "}";
  547. int rcode = 0;
  548. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  549. EXPECT_NE(0, rcode);
  550. CfgMgr::instance().clear();
  551. // The data specified here will work both for CSV format and hex format.
  552. // What we want to test here is that when the csv-format is enforced, the
  553. // parser will fail because of lack of an option definition.
  554. config =
  555. "{ \"option-data\": [ {"
  556. " \"name\": \"foo-name\","
  557. " \"space\": \"dhcp6\","
  558. " \"code\": 25000,"
  559. " \"csv-format\": True,"
  560. " \"data\": \"0\""
  561. " } ]"
  562. "}";
  563. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  564. EXPECT_NE(0, rcode);
  565. CfgMgr::instance().clear();
  566. // The same test case as above, but for the data specified in hex should
  567. // be successful.
  568. config =
  569. "{ \"option-data\": [ {"
  570. " \"name\": \"foo-name\","
  571. " \"space\": \"dhcp6\","
  572. " \"code\": 25000,"
  573. " \"csv-format\": False,"
  574. " \"data\": \"0\""
  575. " } ]"
  576. "}";
  577. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  578. ASSERT_EQ(0, rcode);
  579. OptionPtr opt = getOptionPtr("dhcp6", 25000);
  580. ASSERT_TRUE(opt);
  581. ASSERT_EQ(1, opt->getData().size());
  582. EXPECT_EQ(0, opt->getData()[0]);
  583. CfgMgr::instance().clear();
  584. // When csv-format is not specified, the parser will check if the definition
  585. // exists or not. Since there is no definition, the parser will accept the
  586. // data in hex.
  587. config =
  588. "{ \"option-data\": [ {"
  589. " \"name\": \"foo-name\","
  590. " \"space\": \"dhcp6\","
  591. " \"code\": 25000,"
  592. " \"data\": \"123456\""
  593. " } ]"
  594. "}";
  595. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  596. EXPECT_EQ(0, rcode);
  597. opt = getOptionPtr("dhcp6", 25000);
  598. ASSERT_TRUE(opt);
  599. ASSERT_EQ(3, opt->getData().size());
  600. EXPECT_EQ(0x12, opt->getData()[0]);
  601. EXPECT_EQ(0x34, opt->getData()[1]);
  602. EXPECT_EQ(0x56, opt->getData()[2]);
  603. }
  604. // This test verifies that the option name is not mandatory, if the option
  605. // code has been specified.
  606. TEST_F(ParseConfigTest, optionDataNoName) {
  607. std::string config =
  608. "{ \"option-data\": [ {"
  609. " \"space\": \"dhcp6\","
  610. " \"code\": 23,"
  611. " \"csv-format\": True,"
  612. " \"data\": \"2001:db8:1::1\""
  613. " } ]"
  614. "}";
  615. int rcode = 0;
  616. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  617. EXPECT_EQ(0, rcode);
  618. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  619. Option6AddrLst>(getOptionPtr("dhcp6", 23));
  620. ASSERT_TRUE(opt);
  621. ASSERT_EQ(1, opt->getAddresses().size());
  622. EXPECT_EQ( "2001:db8:1::1", opt->getAddresses()[0].toText());
  623. }
  624. // This test verifies that the option code is not mandatory, if the option
  625. // name has been specified.
  626. TEST_F(ParseConfigTest, optionDataNoCode) {
  627. std::string config =
  628. "{ \"option-data\": [ {"
  629. " \"space\": \"dhcp6\","
  630. " \"name\": \"dns-servers\","
  631. " \"csv-format\": True,"
  632. " \"data\": \"2001:db8:1::1\""
  633. " } ]"
  634. "}";
  635. int rcode = 0;
  636. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  637. EXPECT_EQ(0, rcode);
  638. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  639. Option6AddrLst>(getOptionPtr("dhcp6", 23));
  640. ASSERT_TRUE(opt);
  641. ASSERT_EQ(1, opt->getAddresses().size());
  642. EXPECT_EQ( "2001:db8:1::1", opt->getAddresses()[0].toText());
  643. }
  644. // This test verifies that the option data configuration with a minimal
  645. // set of parameters works as expected.
  646. TEST_F(ParseConfigTest, optionDataMinimal) {
  647. std::string config =
  648. "{ \"option-data\": [ {"
  649. " \"name\": \"dns-servers\","
  650. " \"data\": \"2001:db8:1::10\""
  651. " } ]"
  652. "}";
  653. int rcode = 0;
  654. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  655. EXPECT_EQ(0, rcode);
  656. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  657. Option6AddrLst>(getOptionPtr("dhcp6", 23));
  658. ASSERT_TRUE(opt);
  659. ASSERT_EQ(1, opt->getAddresses().size());
  660. EXPECT_EQ( "2001:db8:1::10", opt->getAddresses()[0].toText());
  661. CfgMgr::instance().clear();
  662. // This time using an option code.
  663. config =
  664. "{ \"option-data\": [ {"
  665. " \"code\": 23,"
  666. " \"data\": \"2001:db8:1::20\""
  667. " } ]"
  668. "}";
  669. rcode = 0;
  670. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  671. EXPECT_EQ(0, rcode);
  672. opt = boost::dynamic_pointer_cast<Option6AddrLst>(getOptionPtr("dhcp6",
  673. 23));
  674. ASSERT_TRUE(opt);
  675. ASSERT_EQ(1, opt->getAddresses().size());
  676. EXPECT_EQ( "2001:db8:1::20", opt->getAddresses()[0].toText());
  677. }
  678. // This test verifies that the option data configuration with a minimal
  679. // set of parameters works as expected when option definition is
  680. // created in the configruation file.
  681. TEST_F(ParseConfigTest, optionDataMinimalWithOptionDef) {
  682. // Configuration string.
  683. std::string config =
  684. "{ \"option-def\": [ {"
  685. " \"name\": \"foo-name\","
  686. " \"code\": 2345,"
  687. " \"type\": \"ipv6-address\","
  688. " \"array\": True,"
  689. " \"record-types\": \"\","
  690. " \"space\": \"dhcp6\","
  691. " \"encapsulate\": \"\""
  692. " } ],"
  693. " \"option-data\": [ {"
  694. " \"name\": \"foo-name\","
  695. " \"data\": \"2001:db8:1::10, 2001:db8:1::123\""
  696. " } ]"
  697. "}";
  698. int rcode = 0;
  699. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  700. EXPECT_EQ(0, rcode);
  701. Option6AddrLstPtr opt = boost::dynamic_pointer_cast<
  702. Option6AddrLst>(getOptionPtr("dhcp6", 2345));
  703. ASSERT_TRUE(opt);
  704. ASSERT_EQ(2, opt->getAddresses().size());
  705. EXPECT_EQ("2001:db8:1::10", opt->getAddresses()[0].toText());
  706. EXPECT_EQ("2001:db8:1::123", opt->getAddresses()[1].toText());
  707. CfgMgr::instance().clear();
  708. // Do the same test but now use an option code.
  709. config =
  710. "{ \"option-def\": [ {"
  711. " \"name\": \"foo-name\","
  712. " \"code\": 2345,"
  713. " \"type\": \"ipv6-address\","
  714. " \"array\": True,"
  715. " \"record-types\": \"\","
  716. " \"space\": \"dhcp6\","
  717. " \"encapsulate\": \"\""
  718. " } ],"
  719. " \"option-data\": [ {"
  720. " \"code\": 2345,"
  721. " \"data\": \"2001:db8:1::10, 2001:db8:1::123\""
  722. " } ]"
  723. "}";
  724. rcode = 0;
  725. ASSERT_NO_THROW(rcode = parseConfiguration(config));
  726. EXPECT_EQ(0, rcode);
  727. opt = boost::dynamic_pointer_cast<Option6AddrLst>(getOptionPtr("dhcp6",
  728. 2345));
  729. ASSERT_TRUE(opt);
  730. ASSERT_EQ(2, opt->getAddresses().size());
  731. EXPECT_EQ("2001:db8:1::10", opt->getAddresses()[0].toText());
  732. EXPECT_EQ("2001:db8:1::123", opt->getAddresses()[1].toText());
  733. }
  734. }; // Anonymous namespace
  735. /// These tests check basic operation of the HooksLibrariesParser.
  736. // hooks-libraries that do not contain anything.
  737. TEST_F(ParseConfigTest, noHooksLibrariesTest) {
  738. // Configuration with hooks-libraries not present.
  739. string config = "{ \"hooks-libraries\": [] }";
  740. // Verify that the configuration string parses.
  741. int rcode = parseConfiguration(config);
  742. ASSERT_TRUE(rcode == 0) << error_text_;
  743. // Check that the parser recorded no change to the current state
  744. // (as the test starts with no hooks libraries loaded).
  745. std::vector<std::string> libraries;
  746. bool changed;
  747. hooks_libraries_parser_->getLibraries(libraries, changed);
  748. EXPECT_TRUE(libraries.empty());
  749. EXPECT_FALSE(changed);
  750. // Load a single library and repeat the parse.
  751. vector<string> basic_library;
  752. basic_library.push_back(string(CALLOUT_LIBRARY_1));
  753. HooksManager::loadLibraries(basic_library);
  754. rcode = parseConfiguration(config);
  755. ASSERT_TRUE(rcode == 0) << error_text_;
  756. // This time the change should have been recorded.
  757. hooks_libraries_parser_->getLibraries(libraries, changed);
  758. EXPECT_TRUE(libraries.empty());
  759. EXPECT_TRUE(changed);
  760. // But repeating it again and we are back to no change.
  761. rcode = parseConfiguration(config);
  762. ASSERT_TRUE(rcode == 0) << error_text_;
  763. hooks_libraries_parser_->getLibraries(libraries, changed);
  764. EXPECT_TRUE(libraries.empty());
  765. EXPECT_FALSE(changed);
  766. }
  767. TEST_F(ParseConfigTest, validHooksLibrariesTest) {
  768. // Configuration string. This contains a set of valid libraries.
  769. const std::string quote("\"");
  770. const std::string comma(", ");
  771. const std::string config =
  772. std::string("{ ") +
  773. std::string("\"hooks-libraries\": [") +
  774. quote + std::string(CALLOUT_LIBRARY_1) + quote + comma +
  775. quote + std::string(CALLOUT_LIBRARY_2) + quote +
  776. std::string("]") +
  777. std::string("}");
  778. // Verify that the configuration string parses.
  779. int rcode = parseConfiguration(config);
  780. ASSERT_TRUE(rcode == 0) << error_text_;
  781. // Check that the parser holds two libraries and the configuration is
  782. // recorded as having changed.
  783. std::vector<std::string> libraries;
  784. bool changed;
  785. hooks_libraries_parser_->getLibraries(libraries, changed);
  786. EXPECT_EQ(2, libraries.size());
  787. EXPECT_TRUE(changed);
  788. // The expected libraries should be the list of libraries specified
  789. // in the given order.
  790. std::vector<std::string> expected;
  791. expected.push_back(CALLOUT_LIBRARY_1);
  792. expected.push_back(CALLOUT_LIBRARY_2);
  793. EXPECT_TRUE(expected == libraries);
  794. // Parse the string again.
  795. rcode = parseConfiguration(config);
  796. ASSERT_TRUE(rcode == 0) << error_text_;
  797. // The list has not changed, and this is what we should see.
  798. hooks_libraries_parser_->getLibraries(libraries, changed);
  799. EXPECT_EQ(2, libraries.size());
  800. EXPECT_FALSE(changed);
  801. }
  802. // Check with a set of libraries, some of which are invalid.
  803. TEST_F(ParseConfigTest, invalidHooksLibrariesTest) {
  804. /// @todo Initialize global library context to null
  805. // Configuration string. This contains an invalid library which should
  806. // trigger an error in the "build" stage.
  807. const std::string quote("\"");
  808. const std::string comma(", ");
  809. const std::string config =
  810. std::string("{ ") +
  811. std::string("\"hooks-libraries\": [") +
  812. quote + std::string(CALLOUT_LIBRARY_1) + quote + comma +
  813. quote + std::string(NOT_PRESENT_LIBRARY) + quote + comma +
  814. quote + std::string(CALLOUT_LIBRARY_2) + quote +
  815. std::string("]") +
  816. std::string("}");
  817. // Verify that the configuration fails to parse. (Syntactically it's OK,
  818. // but the library is invalid).
  819. int rcode = parseConfiguration(config);
  820. ASSERT_FALSE(rcode == 0) << error_text_;
  821. // Check that the message contains the library in error.
  822. EXPECT_FALSE(error_text_.find(NOT_PRESENT_LIBRARY) == string::npos) <<
  823. "Error text returned from parse failure is " << error_text_;
  824. }
  825. /// @brief Checks that a valid, enabled D2 client configuration works correctly.
  826. TEST_F(ParseConfigTest, validD2Config) {
  827. // Configuration string containing valid values.
  828. std::string config_str =
  829. "{ \"dhcp-ddns\" :"
  830. " {"
  831. " \"enable-updates\" : true, "
  832. " \"server-ip\" : \"192.0.2.0\", "
  833. " \"server-port\" : 3432, "
  834. " \"sender-ip\" : \"192.0.2.1\", "
  835. " \"sender-port\" : 3433, "
  836. " \"max-queue-size\" : 2048, "
  837. " \"ncr-protocol\" : \"UDP\", "
  838. " \"ncr-format\" : \"JSON\", "
  839. " \"always-include-fqdn\" : true, "
  840. " \"override-no-update\" : true, "
  841. " \"override-client-update\" : true, "
  842. " \"replace-client-name\" : true, "
  843. " \"generated-prefix\" : \"test.prefix\", "
  844. " \"qualifying-suffix\" : \"test.suffix.\" "
  845. " }"
  846. "}";
  847. // Verify that the configuration string parses.
  848. int rcode = parseConfiguration(config_str);
  849. ASSERT_TRUE(rcode == 0) << error_text_;
  850. // Verify that DHCP-DDNS is enabled and we can fetch the configuration.
  851. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  852. D2ClientConfigPtr d2_client_config;
  853. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  854. ASSERT_TRUE(d2_client_config);
  855. // Verify that the configuration values are as expected.
  856. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  857. EXPECT_EQ("192.0.2.0", d2_client_config->getServerIp().toText());
  858. EXPECT_EQ(3432, d2_client_config->getServerPort());
  859. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  860. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  861. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  862. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  863. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  864. EXPECT_TRUE(d2_client_config->getReplaceClientName());
  865. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  866. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  867. // Another valid Configuration string.
  868. // This one is disabled, has IPV6 server ip, control flags false,
  869. // empty prefix/suffix
  870. std::string config_str2 =
  871. "{ \"dhcp-ddns\" :"
  872. " {"
  873. " \"enable-updates\" : false, "
  874. " \"server-ip\" : \"2001:db8::\", "
  875. " \"server-port\" : 43567, "
  876. " \"sender-ip\" : \"2001:db8::1\", "
  877. " \"sender-port\" : 3433, "
  878. " \"max-queue-size\" : 2048, "
  879. " \"ncr-protocol\" : \"UDP\", "
  880. " \"ncr-format\" : \"JSON\", "
  881. " \"always-include-fqdn\" : false, "
  882. " \"override-no-update\" : false, "
  883. " \"override-client-update\" : false, "
  884. " \"replace-client-name\" : false, "
  885. " \"generated-prefix\" : \"\", "
  886. " \"qualifying-suffix\" : \"\" "
  887. " }"
  888. "}";
  889. // Verify that the configuration string parses.
  890. rcode = parseConfiguration(config_str2);
  891. ASSERT_TRUE(rcode == 0) << error_text_;
  892. // Verify that DHCP-DDNS is disabled and we can fetch the configuration.
  893. EXPECT_FALSE(CfgMgr::instance().ddnsEnabled());
  894. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  895. ASSERT_TRUE(d2_client_config);
  896. // Verify that the configuration values are as expected.
  897. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  898. EXPECT_EQ("2001:db8::", d2_client_config->getServerIp().toText());
  899. EXPECT_EQ(43567, d2_client_config->getServerPort());
  900. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  901. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  902. EXPECT_FALSE(d2_client_config->getAlwaysIncludeFqdn());
  903. EXPECT_FALSE(d2_client_config->getOverrideNoUpdate());
  904. EXPECT_FALSE(d2_client_config->getOverrideClientUpdate());
  905. EXPECT_FALSE(d2_client_config->getReplaceClientName());
  906. EXPECT_EQ("", d2_client_config->getGeneratedPrefix());
  907. EXPECT_EQ("", d2_client_config->getQualifyingSuffix());
  908. }
  909. /// @brief Checks that D2 client can be configured with enable flag of
  910. /// false only.
  911. TEST_F(ParseConfigTest, validDisabledD2Config) {
  912. // Configuration string. This contains a set of valid libraries.
  913. std::string config_str =
  914. "{ \"dhcp-ddns\" :"
  915. " {"
  916. " \"enable-updates\" : false"
  917. " }"
  918. "}";
  919. // Verify that the configuration string parses.
  920. int rcode = parseConfiguration(config_str);
  921. ASSERT_TRUE(rcode == 0) << error_text_;
  922. // Verify that DHCP-DDNS is disabled.
  923. EXPECT_FALSE(CfgMgr::instance().ddnsEnabled());
  924. // Make sure fetched config agrees.
  925. D2ClientConfigPtr d2_client_config;
  926. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  927. EXPECT_TRUE(d2_client_config);
  928. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  929. }
  930. /// @brief Checks that given a partial configuration, parser supplies
  931. /// default values
  932. TEST_F(ParseConfigTest, parserDefaultsD2Config) {
  933. // Configuration string. This contains a set of valid libraries.
  934. std::string config_str =
  935. "{ \"dhcp-ddns\" :"
  936. " {"
  937. " \"enable-updates\" : true"
  938. " }"
  939. "}";
  940. // Verify that the configuration string parses.
  941. int rcode = parseConfiguration(config_str);
  942. ASSERT_TRUE(rcode == 0) << error_text_;
  943. // Verify that DHCP-DDNS is enabled.
  944. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  945. // Make sure fetched config is correct.
  946. D2ClientConfigPtr d2_client_config;
  947. ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
  948. EXPECT_TRUE(d2_client_config);
  949. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  950. EXPECT_EQ(D2ClientConfig::DFT_SERVER_IP,
  951. d2_client_config->getServerIp().toText());
  952. EXPECT_EQ(D2ClientConfig::DFT_SERVER_PORT,
  953. d2_client_config->getServerPort());
  954. EXPECT_EQ(dhcp_ddns::stringToNcrProtocol(D2ClientConfig::DFT_NCR_PROTOCOL),
  955. d2_client_config->getNcrProtocol());
  956. EXPECT_EQ(dhcp_ddns::stringToNcrFormat(D2ClientConfig::DFT_NCR_FORMAT),
  957. d2_client_config->getNcrFormat());
  958. EXPECT_EQ(D2ClientConfig::DFT_ALWAYS_INCLUDE_FQDN,
  959. d2_client_config->getAlwaysIncludeFqdn());
  960. EXPECT_EQ(D2ClientConfig::DFT_OVERRIDE_NO_UPDATE,
  961. d2_client_config->getOverrideNoUpdate());
  962. EXPECT_EQ(D2ClientConfig::DFT_OVERRIDE_CLIENT_UPDATE,
  963. d2_client_config->getOverrideClientUpdate());
  964. EXPECT_EQ(D2ClientConfig::DFT_REPLACE_CLIENT_NAME,
  965. d2_client_config->getReplaceClientName());
  966. EXPECT_EQ(D2ClientConfig::DFT_GENERATED_PREFIX,
  967. d2_client_config->getGeneratedPrefix());
  968. EXPECT_EQ(D2ClientConfig::DFT_QUALIFYING_SUFFIX,
  969. d2_client_config->getQualifyingSuffix());
  970. }
  971. /// @brief Check various invalid D2 client configurations.
  972. TEST_F(ParseConfigTest, invalidD2Config) {
  973. std::string invalid_configs[] = {
  974. // Must supply at lease enable-updates
  975. "{ \"dhcp-ddns\" :"
  976. " {"
  977. " }"
  978. "}",
  979. // Invalid server ip value
  980. "{ \"dhcp-ddns\" :"
  981. " {"
  982. " \"enable-updates\" : true, "
  983. " \"server-ip\" : \"x192.0.2.0\", "
  984. " \"server-port\" : 53001, "
  985. " \"ncr-protocol\" : \"UDP\", "
  986. " \"ncr-format\" : \"JSON\", "
  987. " \"always-include-fqdn\" : true, "
  988. " \"override-no-update\" : true, "
  989. " \"override-client-update\" : true, "
  990. " \"replace-client-name\" : true, "
  991. " \"generated-prefix\" : \"test.prefix\", "
  992. " \"qualifying-suffix\" : \"test.suffix.\" "
  993. " }"
  994. "}",
  995. // Unknown protocol
  996. "{ \"dhcp-ddns\" :"
  997. " {"
  998. " \"enable-updates\" : true, "
  999. " \"server-ip\" : \"192.0.2.0\", "
  1000. " \"server-port\" : 53001, "
  1001. " \"ncr-protocol\" : \"Bogus\", "
  1002. " \"ncr-format\" : \"JSON\", "
  1003. " \"always-include-fqdn\" : true, "
  1004. " \"override-no-update\" : true, "
  1005. " \"override-client-update\" : true, "
  1006. " \"replace-client-name\" : true, "
  1007. " \"generated-prefix\" : \"test.prefix\", "
  1008. " \"qualifying-suffix\" : \"test.suffix.\" "
  1009. " }"
  1010. "}",
  1011. // Unsupported protocol
  1012. "{ \"dhcp-ddns\" :"
  1013. " {"
  1014. " \"enable-updates\" : true, "
  1015. " \"server-ip\" : \"192.0.2.0\", "
  1016. " \"server-port\" : 53001, "
  1017. " \"ncr-protocol\" : \"TCP\", "
  1018. " \"ncr-format\" : \"JSON\", "
  1019. " \"always-include-fqdn\" : true, "
  1020. " \"override-no-update\" : true, "
  1021. " \"override-client-update\" : true, "
  1022. " \"replace-client-name\" : true, "
  1023. " \"generated-prefix\" : \"test.prefix\", "
  1024. " \"qualifying-suffix\" : \"test.suffix.\" "
  1025. " }"
  1026. "}",
  1027. // Unknown format
  1028. "{ \"dhcp-ddns\" :"
  1029. " {"
  1030. " \"enable-updates\" : true, "
  1031. " \"server-ip\" : \"192.0.2.0\", "
  1032. " \"server-port\" : 53001, "
  1033. " \"ncr-protocol\" : \"UDP\", "
  1034. " \"ncr-format\" : \"Bogus\", "
  1035. " \"always-include-fqdn\" : true, "
  1036. " \"override-no-update\" : true, "
  1037. " \"override-client-update\" : true, "
  1038. " \"replace-client-name\" : true, "
  1039. " \"generated-prefix\" : \"test.prefix\", "
  1040. " \"qualifying-suffix\" : \"test.suffix.\" "
  1041. " }"
  1042. "}",
  1043. // Invalid Port
  1044. "{ \"dhcp-ddns\" :"
  1045. " {"
  1046. " \"enable-updates\" : true, "
  1047. " \"server-ip\" : \"192.0.2.0\", "
  1048. " \"server-port\" : \"bogus\", "
  1049. " \"ncr-protocol\" : \"UDP\", "
  1050. " \"ncr-format\" : \"JSON\", "
  1051. " \"always-include-fqdn\" : true, "
  1052. " \"override-no-update\" : true, "
  1053. " \"override-client-update\" : true, "
  1054. " \"replace-client-name\" : true, "
  1055. " \"generated-prefix\" : \"test.prefix\", "
  1056. " \"qualifying-suffix\" : \"test.suffix.\" "
  1057. " }"
  1058. "}",
  1059. // Mismatched server and sender IPs
  1060. "{ \"dhcp-ddns\" :"
  1061. " {"
  1062. " \"enable-updates\" : true, "
  1063. " \"server-ip\" : \"192.0.2.0\", "
  1064. " \"server-port\" : 3432, "
  1065. " \"sender-ip\" : \"3001::5\", "
  1066. " \"sender-port\" : 3433, "
  1067. " \"max-queue-size\" : 2048, "
  1068. " \"ncr-protocol\" : \"UDP\", "
  1069. " \"ncr-format\" : \"JSON\", "
  1070. " \"always-include-fqdn\" : true, "
  1071. " \"override-no-update\" : true, "
  1072. " \"override-client-update\" : true, "
  1073. " \"replace-client-name\" : true, "
  1074. " \"generated-prefix\" : \"test.prefix\", "
  1075. " \"qualifying-suffix\" : \"test.suffix.\" "
  1076. " }"
  1077. "}",
  1078. // Identical server and sender IP/port
  1079. "{ \"dhcp-ddns\" :"
  1080. " {"
  1081. " \"enable-updates\" : true, "
  1082. " \"server-ip\" : \"3001::5\", "
  1083. " \"server-port\" : 3433, "
  1084. " \"sender-ip\" : \"3001::5\", "
  1085. " \"sender-port\" : 3433, "
  1086. " \"max-queue-size\" : 2048, "
  1087. " \"ncr-protocol\" : \"UDP\", "
  1088. " \"ncr-format\" : \"JSON\", "
  1089. " \"always-include-fqdn\" : true, "
  1090. " \"override-no-update\" : true, "
  1091. " \"override-client-update\" : true, "
  1092. " \"replace-client-name\" : true, "
  1093. " \"generated-prefix\" : \"test.prefix\", "
  1094. " \"qualifying-suffix\" : \"test.suffix.\" "
  1095. " }"
  1096. "}",
  1097. // stop
  1098. ""
  1099. };
  1100. // Fetch the original config.
  1101. D2ClientConfigPtr original_config;
  1102. ASSERT_NO_THROW(original_config = CfgMgr::instance().getD2ClientConfig());
  1103. // Iterate through the invalid configuration strings, attempting to
  1104. // parse each one. They should fail to parse, but fail gracefully.
  1105. D2ClientConfigPtr current_config;
  1106. int i = 0;
  1107. while (!invalid_configs[i].empty()) {
  1108. // Verify that the configuration string parses without throwing.
  1109. int rcode = parseConfiguration(invalid_configs[i]);
  1110. // Verify that parse result indicates a parsing error.
  1111. ASSERT_TRUE(rcode != 0) << "Invalid config #: " << i
  1112. << " should not have passed!";
  1113. // Verify that the "official" config still matches the original config.
  1114. ASSERT_NO_THROW(current_config =
  1115. CfgMgr::instance().getD2ClientConfig());
  1116. EXPECT_EQ(*original_config, *current_config);
  1117. ++i;
  1118. }
  1119. }
  1120. /// @brief DHCP Configuration Parser Context test fixture.
  1121. class ParserContextTest : public ::testing::Test {
  1122. public:
  1123. /// @brief Constructor
  1124. ParserContextTest() { }
  1125. /// @brief Check that the storages of the specific type hold the
  1126. /// same value.
  1127. ///
  1128. /// This function assumes that the ref_values storage holds parameter
  1129. /// called 'foo'.
  1130. ///
  1131. /// @param ref_values A storage holding reference value. In the typical
  1132. /// case it is a storage held in the original context, which is assigned
  1133. /// to another context.
  1134. /// @param values A storage holding value to be checked.
  1135. /// @tparam ContainerType A type of the storage.
  1136. template<typename ContainerType>
  1137. void checkValueEq(const boost::shared_ptr<ContainerType>& ref_values,
  1138. const boost::shared_ptr<ContainerType>& values) {
  1139. ASSERT_NO_THROW(values->getParam("foo"));
  1140. EXPECT_EQ(ref_values->getParam("foo"), values->getParam("foo"));
  1141. }
  1142. /// @brief Check that the storages of the specific type hold the same
  1143. /// position of the parameter.
  1144. ///
  1145. /// @param name A name of the parameter to check.
  1146. /// @param ref_values A storage holding reference position. In the typical
  1147. /// case it is a storage held in the original context, which is assigned
  1148. /// to another context.
  1149. /// @param values A storage holding position to be checked.
  1150. /// @tparam ContainerType A type of the storage.
  1151. template<typename ContainerType>
  1152. void checkPositionEq(const std::string& name,
  1153. const boost::shared_ptr<ContainerType>& ref_values,
  1154. const boost::shared_ptr<ContainerType>& values) {
  1155. // Verify that the position is correct.
  1156. EXPECT_EQ(ref_values->getPosition(name).line_,
  1157. values->getPosition(name).line_);
  1158. EXPECT_EQ(ref_values->getPosition(name).pos_,
  1159. values->getPosition(name).pos_);
  1160. EXPECT_EQ(ref_values->getPosition(name).file_,
  1161. values->getPosition(name).file_);
  1162. }
  1163. /// @brief Check that the storages of the specific type hold different
  1164. /// value.
  1165. ///
  1166. /// This function assumes that the ref_values storage holds exactly
  1167. /// one parameter called 'foo'.
  1168. ///
  1169. /// @param ref_values A storage holding reference value. In the typical
  1170. /// case it is a storage held in the original context, which is assigned
  1171. /// to another context.
  1172. /// @param values A storage holding value to be checked.
  1173. /// @tparam ContainerType A type of the storage.
  1174. /// @tparam ValueType A type of the value in the container.
  1175. template<typename ContainerType>
  1176. void checkValueNeq(const boost::shared_ptr<ContainerType>& ref_values,
  1177. const boost::shared_ptr<ContainerType>& values) {
  1178. ASSERT_NO_THROW(values->getParam("foo"));
  1179. EXPECT_NE(ref_values->getParam("foo"), values->getParam("foo"));
  1180. }
  1181. /// @brief Check that the storages of the specific type hold different
  1182. /// position.
  1183. ///
  1184. /// @param name A name of the parameter to be checked.
  1185. /// @param ref_values A storage holding reference position. In the typical
  1186. /// case it is a storage held in the original context, which is assigned
  1187. /// to another context.
  1188. /// @param values A storage holding position to be checked.
  1189. /// @tparam ContainerType A type of the storage.
  1190. template<typename ContainerType>
  1191. void checkPositionNeq(const std::string& name,
  1192. const boost::shared_ptr<ContainerType>& ref_values,
  1193. const boost::shared_ptr<ContainerType>& values) {
  1194. // At least one of the position fields must be different.
  1195. EXPECT_TRUE((ref_values->getPosition(name).line_ !=
  1196. values->getPosition(name).line_) ||
  1197. (ref_values->getPosition(name).pos_ !=
  1198. values->getPosition(name).pos_) ||
  1199. (ref_values->getPosition(name).file_ !=
  1200. values->getPosition(name).file_));
  1201. }
  1202. /// @brief Test copy constructor or assignment operator when values
  1203. /// being copied are NULL.
  1204. ///
  1205. /// @param copy Indicates that copy constructor should be tested
  1206. /// (if true), or assignment operator (if false).
  1207. void testCopyAssignmentNull(const bool copy) {
  1208. ParserContext ctx(Option::V6);
  1209. // Release all pointers in the context.
  1210. ctx.boolean_values_.reset();
  1211. ctx.uint32_values_.reset();
  1212. ctx.string_values_.reset();
  1213. ctx.hooks_libraries_.reset();
  1214. // Even if the fields of the context are NULL, it should get
  1215. // copied.
  1216. ParserContextPtr ctx_new(new ParserContext(Option::V6));
  1217. if (copy) {
  1218. ASSERT_NO_THROW(ctx_new.reset(new ParserContext(ctx)));
  1219. } else {
  1220. *ctx_new = ctx;
  1221. }
  1222. // The resulting context has its fields equal to NULL.
  1223. EXPECT_FALSE(ctx_new->boolean_values_);
  1224. EXPECT_FALSE(ctx_new->uint32_values_);
  1225. EXPECT_FALSE(ctx_new->string_values_);
  1226. EXPECT_FALSE(ctx_new->hooks_libraries_);
  1227. }
  1228. /// @brief Test copy constructor or assignment operator.
  1229. ///
  1230. /// @param copy Indicates that copy constructor should be tested (if true),
  1231. /// or assignment operator (if false).
  1232. void testCopyAssignment(const bool copy) {
  1233. // Create new context. It will be later copied/assigned to another
  1234. // context.
  1235. ParserContext ctx(Option::V6);
  1236. // Set boolean parameter 'foo'.
  1237. ASSERT_TRUE(ctx.boolean_values_);
  1238. ctx.boolean_values_->setParam("foo", true,
  1239. Element::Position("kea.conf", 123, 234));
  1240. // Set various parameters to test that position is copied between
  1241. // contexts.
  1242. ctx.boolean_values_->setParam("pos0", true,
  1243. Element::Position("kea.conf", 1, 2));
  1244. ctx.boolean_values_->setParam("pos1", true,
  1245. Element::Position("kea.conf", 10, 20));
  1246. ctx.boolean_values_->setParam("pos2", true,
  1247. Element::Position("kea.conf", 100, 200));
  1248. // Set uint32 parameter 'foo'.
  1249. ASSERT_TRUE(ctx.uint32_values_);
  1250. ctx.uint32_values_->setParam("foo", 123,
  1251. Element::Position("kea.conf", 123, 234));
  1252. // Set various parameters to test that position is copied between
  1253. // contexts.
  1254. ctx.uint32_values_->setParam("pos0", 123,
  1255. Element::Position("kea.conf", 1, 2));
  1256. ctx.uint32_values_->setParam("pos1", 123,
  1257. Element::Position("kea.conf", 10, 20));
  1258. ctx.uint32_values_->setParam("pos2", 123,
  1259. Element::Position("kea.conf", 100, 200));
  1260. // Ser string parameter 'foo'.
  1261. ASSERT_TRUE(ctx.string_values_);
  1262. ctx.string_values_->setParam("foo", "some string",
  1263. Element::Position("kea.conf", 123, 234));
  1264. // Set various parameters to test that position is copied between
  1265. // contexts.
  1266. ctx.string_values_->setParam("pos0", "some string",
  1267. Element::Position("kea.conf", 1, 2));
  1268. ctx.string_values_->setParam("pos1", "some string",
  1269. Element::Position("kea.conf", 10, 20));
  1270. ctx.string_values_->setParam("pos2", "some string",
  1271. Element::Position("kea.conf", 100, 200));
  1272. // Allocate container for hooks libraries and add one library name.
  1273. ctx.hooks_libraries_.reset(new std::vector<std::string>());
  1274. ctx.hooks_libraries_->push_back("library1");
  1275. // We will use ctx_new to assign another context to it or copy
  1276. // construct.
  1277. ParserContextPtr ctx_new(new ParserContext(Option::V4));;
  1278. if (copy) {
  1279. ctx_new.reset(new ParserContext(ctx));
  1280. } else {
  1281. *ctx_new = ctx;
  1282. }
  1283. // New context has the same boolean value.
  1284. ASSERT_TRUE(ctx_new->boolean_values_);
  1285. {
  1286. SCOPED_TRACE("Check that boolean values are equal in both"
  1287. " contexts");
  1288. checkValueEq(ctx.boolean_values_, ctx_new->boolean_values_);
  1289. }
  1290. // New context has the same boolean values' positions.
  1291. {
  1292. SCOPED_TRACE("Check that positions of boolean values are equal"
  1293. " in both contexts");
  1294. checkPositionEq("pos0", ctx.boolean_values_,
  1295. ctx_new->boolean_values_);
  1296. checkPositionEq("pos1", ctx.boolean_values_,
  1297. ctx_new->boolean_values_);
  1298. checkPositionEq("pos2", ctx.boolean_values_,
  1299. ctx_new->boolean_values_);
  1300. }
  1301. // New context has the same uint32 value.
  1302. ASSERT_TRUE(ctx_new->uint32_values_);
  1303. {
  1304. SCOPED_TRACE("Check that uint32_t values are equal in both"
  1305. " contexts");
  1306. checkValueEq(ctx.uint32_values_, ctx_new->uint32_values_);
  1307. }
  1308. // New context has the same uint32 values' positions.
  1309. {
  1310. SCOPED_TRACE("Check that positions of uint32 values are equal"
  1311. " in both contexts");
  1312. checkPositionEq("pos0", ctx.uint32_values_,
  1313. ctx_new->uint32_values_);
  1314. checkPositionEq("pos1", ctx.uint32_values_,
  1315. ctx_new->uint32_values_);
  1316. checkPositionEq("pos2", ctx.uint32_values_,
  1317. ctx_new->uint32_values_);
  1318. }
  1319. // New context has the same uint32 value position.
  1320. {
  1321. SCOPED_TRACE("Check that positions of uint32_t values are equal"
  1322. " in both contexts");
  1323. checkPositionEq("foo", ctx.uint32_values_, ctx_new->uint32_values_);
  1324. }
  1325. // New context has the same string value.
  1326. ASSERT_TRUE(ctx_new->string_values_);
  1327. {
  1328. SCOPED_TRACE("Check that string values are equal in both contexts");
  1329. checkValueEq(ctx.string_values_, ctx_new->string_values_);
  1330. }
  1331. // New context has the same string values' positions.
  1332. {
  1333. SCOPED_TRACE("Check that positions of string values are equal"
  1334. " in both contexts");
  1335. checkPositionEq("pos0", ctx.string_values_,
  1336. ctx_new->string_values_);
  1337. checkPositionEq("pos1", ctx.string_values_,
  1338. ctx_new->string_values_);
  1339. checkPositionEq("pos2", ctx.string_values_,
  1340. ctx_new->string_values_);
  1341. }
  1342. // New context has the same hooks library.
  1343. ASSERT_TRUE(ctx_new->hooks_libraries_);
  1344. {
  1345. ASSERT_EQ(1, ctx_new->hooks_libraries_->size());
  1346. EXPECT_EQ("library1", (*ctx_new->hooks_libraries_)[0]);
  1347. }
  1348. // New context has the same universe.
  1349. EXPECT_EQ(ctx.universe_, ctx_new->universe_);
  1350. // Change the value of the boolean parameter. This should not affect the
  1351. // corresponding value in the new context.
  1352. {
  1353. SCOPED_TRACE("Check that boolean value isn't changed when original"
  1354. " value and position is changed");
  1355. ctx.boolean_values_->setParam("foo", false,
  1356. Element::Position("kea.conf",
  1357. 12, 10));
  1358. checkValueNeq(ctx.boolean_values_, ctx_new->boolean_values_);
  1359. }
  1360. {
  1361. SCOPED_TRACE("Check that positions of the boolean parameters aren't"
  1362. " changed when the corresponding positions in the"
  1363. " original context are changed");
  1364. // Modify file name.
  1365. ctx.boolean_values_->setParam("pos0", false,
  1366. Element::Position("foo.conf",
  1367. 1, 2));
  1368. checkPositionNeq("pos0", ctx.boolean_values_,
  1369. ctx_new->boolean_values_);
  1370. // Modify line number.
  1371. ctx.boolean_values_->setParam("pos1", false,
  1372. Element::Position("kea.conf",
  1373. 11, 20));
  1374. checkPositionNeq("pos1", ctx.boolean_values_,
  1375. ctx_new->boolean_values_);
  1376. // Modify position within a line.
  1377. ctx.boolean_values_->setParam("pos2", false,
  1378. Element::Position("kea.conf",
  1379. 101, 201));
  1380. checkPositionNeq("pos2", ctx.boolean_values_,
  1381. ctx_new->boolean_values_);
  1382. }
  1383. // Change the value of the uint32_t parameter. This should not affect
  1384. // the corresponding value in the new context.
  1385. {
  1386. SCOPED_TRACE("Check that uint32_t value isn't changed when original"
  1387. " value and position is changed");
  1388. ctx.uint32_values_->setParam("foo", 987,
  1389. Element::Position("kea.conf", 10, 11));
  1390. checkValueNeq(ctx.uint32_values_, ctx_new->uint32_values_);
  1391. }
  1392. {
  1393. SCOPED_TRACE("Check that positions of the uint32 parameters aren't"
  1394. " changed when the corresponding positions in the"
  1395. " original context are changed");
  1396. // Modify file name.
  1397. ctx.uint32_values_->setParam("pos0", 123,
  1398. Element::Position("foo.conf", 1, 2));
  1399. checkPositionNeq("pos0", ctx.uint32_values_,
  1400. ctx_new->uint32_values_);
  1401. // Modify line number.
  1402. ctx.uint32_values_->setParam("pos1", 123,
  1403. Element::Position("kea.conf",
  1404. 11, 20));
  1405. checkPositionNeq("pos1", ctx.uint32_values_,
  1406. ctx_new->uint32_values_);
  1407. // Modify position within a line.
  1408. ctx.uint32_values_->setParam("pos2", 123,
  1409. Element::Position("kea.conf",
  1410. 101, 201));
  1411. checkPositionNeq("pos2", ctx.uint32_values_,
  1412. ctx_new->uint32_values_);
  1413. }
  1414. // Change the value of the string parameter. This should not affect the
  1415. // corresponding value in the new context.
  1416. {
  1417. SCOPED_TRACE("Check that string value isn't changed when original"
  1418. " value and position is changed");
  1419. ctx.string_values_->setParam("foo", "different string",
  1420. Element::Position("kea.conf", 10, 11));
  1421. checkValueNeq(ctx.string_values_, ctx_new->string_values_);
  1422. }
  1423. {
  1424. SCOPED_TRACE("Check that positions of the string parameters aren't"
  1425. " changed when the corresponding positions in the"
  1426. " original context are changed");
  1427. // Modify file name.
  1428. ctx.string_values_->setParam("pos0", "some string",
  1429. Element::Position("foo.conf", 1, 2));
  1430. checkPositionNeq("pos0", ctx.string_values_,
  1431. ctx_new->string_values_);
  1432. // Modify line number.
  1433. ctx.string_values_->setParam("pos1", "some string",
  1434. Element::Position("kea.conf",
  1435. 11, 20));
  1436. checkPositionNeq("pos1", ctx.string_values_,
  1437. ctx_new->string_values_);
  1438. // Modify position within a line.
  1439. ctx.string_values_->setParam("pos2", "some string",
  1440. Element::Position("kea.conf",
  1441. 101, 201));
  1442. checkPositionNeq("pos2", ctx.string_values_,
  1443. ctx_new->string_values_);
  1444. }
  1445. // Change the list of libraries. this should not affect the list in the
  1446. // new context.
  1447. ctx.hooks_libraries_->clear();
  1448. ctx.hooks_libraries_->push_back("library2");
  1449. ASSERT_EQ(1, ctx_new->hooks_libraries_->size());
  1450. EXPECT_EQ("library1", (*ctx_new->hooks_libraries_)[0]);
  1451. // Change the universe. This should not affect the universe value in the
  1452. // new context.
  1453. ctx.universe_ = Option::V4;
  1454. EXPECT_EQ(Option::V6, ctx_new->universe_);
  1455. }
  1456. };
  1457. // Check that the assignment operator of the ParserContext class copies all
  1458. // fields correctly.
  1459. TEST_F(ParserContextTest, assignment) {
  1460. testCopyAssignment(false);
  1461. }
  1462. // Check that the assignment operator of the ParserContext class copies all
  1463. // fields correctly when these fields are NULL.
  1464. TEST_F(ParserContextTest, assignmentNull) {
  1465. testCopyAssignmentNull(false);
  1466. }
  1467. // Check that the context is copy constructed correctly.
  1468. TEST_F(ParserContextTest, copyConstruct) {
  1469. testCopyAssignment(true);
  1470. }
  1471. // Check that the context is copy constructed correctly, when context fields
  1472. // are NULL.
  1473. TEST_F(ParserContextTest, copyConstructNull) {
  1474. testCopyAssignmentNull(true);
  1475. }
  1476. /// @brief Checks that a valid relay info structure for IPv4 can be handled
  1477. TEST_F(ParseConfigTest, validRelayInfo4) {
  1478. // Relay information structure. Very simple for now.
  1479. std::string config_str =
  1480. " {"
  1481. " \"ip-address\" : \"192.0.2.1\""
  1482. " }";
  1483. ElementPtr json = Element::fromJSON(config_str);
  1484. // Invalid config (wrong family type of the ip-address field)
  1485. std::string config_str_bogus1 =
  1486. " {"
  1487. " \"ip-address\" : \"2001:db8::1\""
  1488. " }";
  1489. ElementPtr json_bogus1 = Element::fromJSON(config_str_bogus1);
  1490. // Invalid config (that thing is not an IPv4 address)
  1491. std::string config_str_bogus2 =
  1492. " {"
  1493. " \"ip-address\" : \"256.345.123.456\""
  1494. " }";
  1495. ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2);
  1496. // We need to set the default ip-address to something.
  1497. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("0.0.0.0")));
  1498. boost::shared_ptr<RelayInfoParser> parser;
  1499. // Subnet4 parser will pass 0.0.0.0 to the RelayInfoParser
  1500. EXPECT_NO_THROW(parser.reset(new RelayInfoParser("ignored", result,
  1501. Option::V4)));
  1502. EXPECT_NO_THROW(parser->build(json));
  1503. EXPECT_NO_THROW(parser->commit());
  1504. EXPECT_EQ("192.0.2.1", result->addr_.toText());
  1505. // Let's check negative scenario (wrong family type)
  1506. EXPECT_THROW(parser->build(json_bogus1), DhcpConfigError);
  1507. // Let's check negative scenario (too large byte values in pseudo-IPv4 addr)
  1508. EXPECT_THROW(parser->build(json_bogus2), DhcpConfigError);
  1509. }
  1510. /// @brief Checks that a valid relay info structure for IPv6 can be handled
  1511. TEST_F(ParseConfigTest, validRelayInfo6) {
  1512. // Relay information structure. Very simple for now.
  1513. std::string config_str =
  1514. " {"
  1515. " \"ip-address\" : \"2001:db8::1\""
  1516. " }";
  1517. ElementPtr json = Element::fromJSON(config_str);
  1518. // Invalid config (wrong family type of the ip-address field
  1519. std::string config_str_bogus1 =
  1520. " {"
  1521. " \"ip-address\" : \"192.0.2.1\""
  1522. " }";
  1523. ElementPtr json_bogus1 = Element::fromJSON(config_str_bogus1);
  1524. // That IPv6 address doesn't look right
  1525. std::string config_str_bogus2 =
  1526. " {"
  1527. " \"ip-address\" : \"2001:db8:::4\""
  1528. " }";
  1529. ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2);
  1530. // We need to set the default ip-address to something.
  1531. Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::")));
  1532. boost::shared_ptr<RelayInfoParser> parser;
  1533. // Subnet4 parser will pass :: to the RelayInfoParser
  1534. EXPECT_NO_THROW(parser.reset(new RelayInfoParser("ignored", result,
  1535. Option::V6)));
  1536. EXPECT_NO_THROW(parser->build(json));
  1537. EXPECT_NO_THROW(parser->commit());
  1538. EXPECT_EQ("2001:db8::1", result->addr_.toText());
  1539. // Let's check negative scenario (wrong family type)
  1540. EXPECT_THROW(parser->build(json_bogus1), DhcpConfigError);
  1541. // Unparseable text that looks like IPv6 address, but has too many colons
  1542. EXPECT_THROW(parser->build(json_bogus2), DhcpConfigError);
  1543. }