config_parser_unittest.cc 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. // Copyright (C) 2012-2013 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 <arpa/inet.h>
  16. #include <gtest/gtest.h>
  17. #include <config/ccsession.h>
  18. #include <dhcp4/dhcp4_srv.h>
  19. #include <dhcp4/config_parser.h>
  20. #include <dhcp/option4_addrlst.h>
  21. #include <dhcp/option_custom.h>
  22. #include <dhcp/option_int.h>
  23. #include <dhcpsrv/subnet.h>
  24. #include <dhcpsrv/cfgmgr.h>
  25. #include <boost/foreach.hpp>
  26. #include <iostream>
  27. #include <fstream>
  28. #include <sstream>
  29. #include <limits.h>
  30. using namespace std;
  31. using namespace isc;
  32. using namespace isc::dhcp;
  33. using namespace isc::asiolink;
  34. using namespace isc::data;
  35. using namespace isc::config;
  36. namespace {
  37. class Dhcp4ParserTest : public ::testing::Test {
  38. public:
  39. Dhcp4ParserTest()
  40. :rcode_(-1) {
  41. // Open port 0 means to not do anything at all. We don't want to
  42. // deal with sockets here, just check if configuration handling
  43. // is sane.
  44. srv_ = new Dhcpv4Srv(0);
  45. }
  46. // Checks if global parameter of name have expected_value
  47. void checkGlobalUint32(string name, uint32_t expected_value) {
  48. const std::map<std::string, uint32_t>& uint32_defaults = getUint32Defaults();
  49. std::map<std::string, uint32_t>::const_iterator it =
  50. uint32_defaults.find(name);
  51. if (it == uint32_defaults.end()) {
  52. ADD_FAILURE() << "Expected uint32 with name " << name
  53. << " not found";
  54. return;
  55. }
  56. EXPECT_EQ(expected_value, it->second);
  57. }
  58. // Checks if the result of DHCP server configuration has
  59. // expected code (0 for success, other for failures).
  60. // Also stores result in rcode_ and comment_.
  61. void checkResult(ConstElementPtr status, int expected_code) {
  62. ASSERT_TRUE(status);
  63. comment_ = parseAnswer(rcode_, status);
  64. EXPECT_EQ(expected_code, rcode_);
  65. }
  66. ~Dhcp4ParserTest() {
  67. resetConfiguration();
  68. delete srv_;
  69. };
  70. /// @brief Create the simple configuration with single option.
  71. ///
  72. /// This function allows to set one of the parameters that configure
  73. /// option value. These parameters are: "name", "code", "data",
  74. /// "csv-format" and "space".
  75. ///
  76. /// @param param_value string holiding option parameter value to be
  77. /// injected into the configuration string.
  78. /// @param parameter name of the parameter to be configured with
  79. /// param value.
  80. /// @return configuration string containing custom values of parameters
  81. /// describing an option.
  82. std::string createConfigWithOption(const std::string& param_value,
  83. const std::string& parameter) {
  84. std::map<std::string, std::string> params;
  85. if (parameter == "name") {
  86. params["name"] = param_value;
  87. params["space"] = "dhcp4";
  88. params["code"] = "56";
  89. params["data"] = "AB CDEF0105";
  90. params["csv-format"] = "False";
  91. } else if (parameter == "space") {
  92. params["name"] = "dhcp-message";
  93. params["space"] = param_value;
  94. params["code"] = "56";
  95. params["data"] = "AB CDEF0105";
  96. params["csv-format"] = "False";
  97. } else if (parameter == "code") {
  98. params["name"] = "dhcp-message";
  99. params["space"] = "dhcp4";
  100. params["code"] = param_value;
  101. params["data"] = "AB CDEF0105";
  102. params["csv-format"] = "False";
  103. } else if (parameter == "data") {
  104. params["name"] = "dhcp-message";
  105. params["space"] = "dhcp4";
  106. params["code"] = "56";
  107. params["data"] = param_value;
  108. params["csv-format"] = "False";
  109. } else if (parameter == "csv-format") {
  110. params["name"] = "dhcp-message";
  111. params["space"] = "dhcp4";
  112. params["code"] = "56";
  113. params["data"] = "AB CDEF0105";
  114. params["csv-format"] = param_value;
  115. }
  116. return (createConfigWithOption(params));
  117. }
  118. /// @brief Create simple configuration with single option.
  119. ///
  120. /// This function creates a configuration for a single option with
  121. /// custom values for all parameters that describe the option.
  122. ///
  123. /// @params params map holding parameters and their values.
  124. /// @return configuration string containing custom values of parameters
  125. /// describing an option.
  126. std::string createConfigWithOption(const std::map<std::string, std::string>& params) {
  127. std::ostringstream stream;
  128. stream << "{ \"interface\": [ \"all\" ],"
  129. "\"rebind-timer\": 2000, "
  130. "\"renew-timer\": 1000, "
  131. "\"subnet4\": [ { "
  132. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  133. " \"subnet\": \"192.0.2.0/24\", "
  134. " \"option-data\": [ {";
  135. bool first = true;
  136. typedef std::pair<std::string, std::string> ParamPair;
  137. BOOST_FOREACH(ParamPair param, params) {
  138. if (!first) {
  139. stream << ", ";
  140. } else {
  141. // cppcheck-suppress unreadVariable
  142. first = false;
  143. }
  144. if (param.first == "name") {
  145. stream << "\"name\": \"" << param.second << "\"";
  146. } else if (param.first == "space") {
  147. stream << "\"space\": \"" << param.second << "\"";
  148. } else if (param.first == "code") {
  149. stream << "\"code\": " << param.second << "";
  150. } else if (param.first == "data") {
  151. stream << "\"data\": \"" << param.second << "\"";
  152. } else if (param.first == "csv-format") {
  153. stream << "\"csv-format\": " << param.second;
  154. }
  155. }
  156. stream <<
  157. " } ]"
  158. " } ],"
  159. "\"valid-lifetime\": 4000 }";
  160. return (stream.str());
  161. }
  162. /// @brief Test invalid option parameter value.
  163. ///
  164. /// This test function constructs the simple configuration
  165. /// string and injects invalid option configuration into it.
  166. /// It expects that parser will fail with provided option code.
  167. ///
  168. /// @param param_value string holding invalid option parameter value
  169. /// to be injected into configuration string.
  170. /// @param parameter name of the parameter to be configured with
  171. /// param_value (can be any of "name", "code", "data")
  172. void testInvalidOptionParam(const std::string& param_value,
  173. const std::string& parameter) {
  174. ConstElementPtr x;
  175. std::string config = createConfigWithOption(param_value, parameter);
  176. ElementPtr json = Element::fromJSON(config);
  177. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  178. ASSERT_TRUE(x);
  179. comment_ = parseAnswer(rcode_, x);
  180. ASSERT_EQ(1, rcode_);
  181. }
  182. /// @brief Test option against given code and data.
  183. ///
  184. /// @param option_desc option descriptor that carries the option to
  185. /// be tested.
  186. /// @param expected_code expected code of the option.
  187. /// @param expected_data expected data in the option.
  188. /// @param expected_data_len length of the reference data.
  189. /// @param extra_data if true extra data is allowed in an option
  190. /// after tested data.
  191. void testOption(const Subnet::OptionDescriptor& option_desc,
  192. uint16_t expected_code, const uint8_t* expected_data,
  193. size_t expected_data_len,
  194. bool extra_data = false) {
  195. // Check if option descriptor contains valid option pointer.
  196. ASSERT_TRUE(option_desc.option);
  197. // Verify option type.
  198. EXPECT_EQ(expected_code, option_desc.option->getType());
  199. // We may have many different option types being created. Some of them
  200. // have dedicated classes derived from Option class. In such case if
  201. // we want to verify the option contents against expected_data we have
  202. // to prepare raw buffer with the contents of the option. The easiest
  203. // way is to call pack() which will prepare on-wire data.
  204. util::OutputBuffer buf(option_desc.option->getData().size());
  205. option_desc.option->pack(buf);
  206. if (extra_data) {
  207. // The length of the buffer must be at least equal to size of the
  208. // reference data but it can sometimes be greater than that. This is
  209. // because some options carry suboptions that increase the overall
  210. // length.
  211. ASSERT_GE(buf.getLength() - option_desc.option->getHeaderLen(),
  212. expected_data_len);
  213. } else {
  214. ASSERT_EQ(buf.getLength() - option_desc.option->getHeaderLen(),
  215. expected_data_len);
  216. }
  217. // Verify that the data is correct. Do not verify suboptions and a header.
  218. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  219. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option->getHeaderLen(),
  220. expected_data_len));
  221. }
  222. /// @brief Reset configuration database.
  223. ///
  224. /// This function resets configuration data base by
  225. /// removing all subnets and option-data. Reset must
  226. /// be performed after each test to make sure that
  227. /// contents of the database do not affect result of
  228. /// subsequent tests.
  229. void resetConfiguration() {
  230. ConstElementPtr status;
  231. string config = "{ \"interface\": [ \"all\" ],"
  232. "\"rebind-timer\": 2000, "
  233. "\"renew-timer\": 1000, "
  234. "\"valid-lifetime\": 4000, "
  235. "\"subnet4\": [ ], "
  236. "\"option-def\": [ ], "
  237. "\"option-data\": [ ] }";
  238. try {
  239. ElementPtr json = Element::fromJSON(config);
  240. status = configureDhcp4Server(*srv_, json);
  241. } catch (const std::exception& ex) {
  242. FAIL() << "Fatal error: unable to reset configuration database"
  243. << " after the test. The following configuration was used"
  244. << " to reset database: " << std::endl
  245. << config << std::endl
  246. << " and the following error message was returned:"
  247. << ex.what() << std::endl;
  248. }
  249. // status object must not be NULL
  250. if (!status) {
  251. FAIL() << "Fatal error: unable to reset configuration database"
  252. << " after the test. Configuration function returned"
  253. << " NULL pointer" << std::endl;
  254. }
  255. comment_ = parseAnswer(rcode_, status);
  256. // returned value should be 0 (configuration success)
  257. if (rcode_ != 0) {
  258. FAIL() << "Fatal error: unable to reset configuration database"
  259. << " after the test. Configuration function returned"
  260. << " error code " << rcode_ << std::endl;
  261. }
  262. }
  263. Dhcpv4Srv* srv_;
  264. int rcode_;
  265. ConstElementPtr comment_;
  266. };
  267. // Goal of this test is a verification if a very simple config update
  268. // with just a bumped version number. That's the simplest possible
  269. // config update.
  270. TEST_F(Dhcp4ParserTest, version) {
  271. ConstElementPtr x;
  272. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  273. Element::fromJSON("{\"version\": 0}")));
  274. // returned value must be 0 (configuration accepted)
  275. checkResult(x, 0);
  276. }
  277. /// The goal of this test is to verify that the code accepts only
  278. /// valid commands and malformed or unsupported parameters are rejected.
  279. TEST_F(Dhcp4ParserTest, bogusCommand) {
  280. ConstElementPtr x;
  281. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  282. Element::fromJSON("{\"bogus\": 5}")));
  283. // returned value must be 1 (configuration parse error)
  284. checkResult(x, 1);
  285. }
  286. /// The goal of this test is to verify if wrongly defined subnet will
  287. /// be rejected. Properly defined subnet must include at least one
  288. /// pool definition.
  289. TEST_F(Dhcp4ParserTest, emptySubnet) {
  290. ConstElementPtr status;
  291. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  292. Element::fromJSON("{ \"interface\": [ \"all\" ],"
  293. "\"rebind-timer\": 2000, "
  294. "\"renew-timer\": 1000, "
  295. "\"subnet4\": [ ], "
  296. "\"valid-lifetime\": 4000 }")));
  297. // returned value should be 0 (success)
  298. checkResult(status, 0);
  299. checkGlobalUint32("rebind-timer", 2000);
  300. checkGlobalUint32("renew-timer", 1000);
  301. checkGlobalUint32("valid-lifetime", 4000);
  302. }
  303. /// The goal of this test is to verify if defined subnet uses global
  304. /// parameter timer definitions.
  305. TEST_F(Dhcp4ParserTest, subnetGlobalDefaults) {
  306. ConstElementPtr status;
  307. string config = "{ \"interface\": [ \"all\" ],"
  308. "\"rebind-timer\": 2000, "
  309. "\"renew-timer\": 1000, "
  310. "\"subnet4\": [ { "
  311. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  312. " \"subnet\": \"192.0.2.0/24\" } ],"
  313. "\"valid-lifetime\": 4000 }";
  314. ElementPtr json = Element::fromJSON(config);
  315. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  316. // check if returned status is OK
  317. checkResult(status, 0);
  318. // Now check if the configuration was indeed handled and we have
  319. // expected pool configured.
  320. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  321. ASSERT_TRUE(subnet);
  322. EXPECT_EQ(1000, subnet->getT1());
  323. EXPECT_EQ(2000, subnet->getT2());
  324. EXPECT_EQ(4000, subnet->getValid());
  325. }
  326. // This test checks if it is possible to override global values
  327. // on a per subnet basis.
  328. TEST_F(Dhcp4ParserTest, subnetLocal) {
  329. ConstElementPtr status;
  330. string config = "{ \"interface\": [ \"all\" ],"
  331. "\"rebind-timer\": 2000, "
  332. "\"renew-timer\": 1000, "
  333. "\"subnet4\": [ { "
  334. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  335. " \"renew-timer\": 1, "
  336. " \"rebind-timer\": 2, "
  337. " \"valid-lifetime\": 4,"
  338. " \"subnet\": \"192.0.2.0/24\" } ],"
  339. "\"valid-lifetime\": 4000 }";
  340. ElementPtr json = Element::fromJSON(config);
  341. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  342. // returned value should be 0 (configuration success)
  343. checkResult(status, 0);
  344. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  345. ASSERT_TRUE(subnet);
  346. EXPECT_EQ(1, subnet->getT1());
  347. EXPECT_EQ(2, subnet->getT2());
  348. EXPECT_EQ(4, subnet->getValid());
  349. }
  350. // Test verifies that a subnet with pool values that do not belong to that
  351. // pool are rejected.
  352. TEST_F(Dhcp4ParserTest, poolOutOfSubnet) {
  353. ConstElementPtr status;
  354. string config = "{ \"interface\": [ \"all\" ],"
  355. "\"rebind-timer\": 2000, "
  356. "\"renew-timer\": 1000, "
  357. "\"subnet4\": [ { "
  358. " \"pool\": [ \"192.0.4.0/28\" ],"
  359. " \"subnet\": \"192.0.2.0/24\" } ],"
  360. "\"valid-lifetime\": 4000 }";
  361. ElementPtr json = Element::fromJSON(config);
  362. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  363. // returned value must be 1 (values error)
  364. // as the pool does not belong to that subnet
  365. checkResult(status, 1);
  366. }
  367. // Goal of this test is to verify if pools can be defined
  368. // using prefix/length notation. There is no separate test for min-max
  369. // notation as it was tested in several previous tests.
  370. TEST_F(Dhcp4ParserTest, poolPrefixLen) {
  371. ConstElementPtr status;
  372. string config = "{ \"interface\": [ \"all\" ],"
  373. "\"rebind-timer\": 2000, "
  374. "\"renew-timer\": 1000, "
  375. "\"subnet4\": [ { "
  376. " \"pool\": [ \"192.0.2.128/28\" ],"
  377. " \"subnet\": \"192.0.2.0/24\" } ],"
  378. "\"valid-lifetime\": 4000 }";
  379. ElementPtr json = Element::fromJSON(config);
  380. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  381. // returned value must be 0 (configuration accepted)
  382. checkResult(status, 0);
  383. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  384. ASSERT_TRUE(subnet);
  385. EXPECT_EQ(1000, subnet->getT1());
  386. EXPECT_EQ(2000, subnet->getT2());
  387. EXPECT_EQ(4000, subnet->getValid());
  388. }
  389. // The goal of this test is to check whether an option definition
  390. // that defines an option carrying an IPv4 address can be created.
  391. TEST_F(Dhcp4ParserTest, optionDefIpv4Address) {
  392. // Configuration string.
  393. std::string config =
  394. "{ \"option-def\": [ {"
  395. " \"name\": \"foo\","
  396. " \"code\": 100,"
  397. " \"type\": \"ipv4-address\","
  398. " \"array\": False,"
  399. " \"record-types\": \"\","
  400. " \"space\": \"isc\","
  401. " \"encapsulate\": \"\""
  402. " } ]"
  403. "}";
  404. ElementPtr json = Element::fromJSON(config);
  405. // Make sure that the particular option definition does not exist.
  406. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  407. ASSERT_FALSE(def);
  408. // Use the configuration string to create new option definition.
  409. ConstElementPtr status;
  410. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  411. ASSERT_TRUE(status);
  412. checkResult(status, 0);
  413. // The option definition should now be available in the CfgMgr.
  414. def = CfgMgr::instance().getOptionDef("isc", 100);
  415. ASSERT_TRUE(def);
  416. // Verify that the option definition data is valid.
  417. EXPECT_EQ("foo", def->getName());
  418. EXPECT_EQ(100, def->getCode());
  419. EXPECT_FALSE(def->getArrayType());
  420. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  421. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  422. }
  423. // The goal of this test is to check whether an option definition
  424. // that defines an option carrying a record of data fields can
  425. // be created.
  426. TEST_F(Dhcp4ParserTest, optionDefRecord) {
  427. // Configuration string.
  428. std::string config =
  429. "{ \"option-def\": [ {"
  430. " \"name\": \"foo\","
  431. " \"code\": 100,"
  432. " \"type\": \"record\","
  433. " \"array\": False,"
  434. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  435. " \"space\": \"isc\","
  436. " \"encapsulate\": \"\""
  437. " } ]"
  438. "}";
  439. ElementPtr json = Element::fromJSON(config);
  440. // Make sure that the particular option definition does not exist.
  441. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  442. ASSERT_FALSE(def);
  443. // Use the configuration string to create new option definition.
  444. ConstElementPtr status;
  445. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  446. ASSERT_TRUE(status);
  447. checkResult(status, 0);
  448. // The option definition should now be available in the CfgMgr.
  449. def = CfgMgr::instance().getOptionDef("isc", 100);
  450. ASSERT_TRUE(def);
  451. // Check the option data.
  452. EXPECT_EQ("foo", def->getName());
  453. EXPECT_EQ(100, def->getCode());
  454. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  455. EXPECT_FALSE(def->getArrayType());
  456. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  457. // The option comprises the record of data fields. Verify that all
  458. // fields are present and they are of the expected types.
  459. const OptionDefinition::RecordFieldsCollection& record_fields =
  460. def->getRecordFields();
  461. ASSERT_EQ(4, record_fields.size());
  462. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  463. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  464. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  465. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  466. }
  467. // The goal of this test is to verify that multiple option definitions
  468. // can be created.
  469. TEST_F(Dhcp4ParserTest, optionDefMultiple) {
  470. // Configuration string.
  471. std::string config =
  472. "{ \"option-def\": [ {"
  473. " \"name\": \"foo\","
  474. " \"code\": 100,"
  475. " \"type\": \"uint32\","
  476. " \"array\": False,"
  477. " \"record-types\": \"\","
  478. " \"space\": \"isc\","
  479. " \"encapsulate\": \"\""
  480. " },"
  481. " {"
  482. " \"name\": \"foo-2\","
  483. " \"code\": 101,"
  484. " \"type\": \"ipv4-address\","
  485. " \"array\": False,"
  486. " \"record-types\": \"\","
  487. " \"space\": \"isc\","
  488. " \"encapsulate\": \"\""
  489. " } ]"
  490. "}";
  491. ElementPtr json = Element::fromJSON(config);
  492. // Make sure that the option definitions do not exist yet.
  493. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  494. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 101));
  495. // Use the configuration string to create new option definitions.
  496. ConstElementPtr status;
  497. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  498. ASSERT_TRUE(status);
  499. checkResult(status, 0);
  500. // Check the first definition we have created.
  501. OptionDefinitionPtr def1 = CfgMgr::instance().getOptionDef("isc", 100);
  502. ASSERT_TRUE(def1);
  503. // Check the option data.
  504. EXPECT_EQ("foo", def1->getName());
  505. EXPECT_EQ(100, def1->getCode());
  506. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  507. EXPECT_FALSE(def1->getArrayType());
  508. EXPECT_TRUE(def1->getEncapsulatedSpace().empty());
  509. // Check the second option definition we have created.
  510. OptionDefinitionPtr def2 = CfgMgr::instance().getOptionDef("isc", 101);
  511. ASSERT_TRUE(def2);
  512. // Check the option data.
  513. EXPECT_EQ("foo-2", def2->getName());
  514. EXPECT_EQ(101, def2->getCode());
  515. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  516. EXPECT_FALSE(def2->getArrayType());
  517. EXPECT_TRUE(def2->getEncapsulatedSpace().empty());
  518. }
  519. // The goal of this test is to verify that the duplicated option
  520. // definition is not accepted.
  521. TEST_F(Dhcp4ParserTest, optionDefDuplicate) {
  522. // Configuration string. Both option definitions have
  523. // the same code and belong to the same option space.
  524. // This configuration should not be accepted.
  525. std::string config =
  526. "{ \"option-def\": [ {"
  527. " \"name\": \"foo\","
  528. " \"code\": 100,"
  529. " \"type\": \"uint32\","
  530. " \"array\": False,"
  531. " \"record-types\": \"\","
  532. " \"space\": \"isc\","
  533. " \"encapsulate\": \"\""
  534. " },"
  535. " {"
  536. " \"name\": \"foo-2\","
  537. " \"code\": 100,"
  538. " \"type\": \"ipv4-address\","
  539. " \"array\": False,"
  540. " \"record-types\": \"\","
  541. " \"space\": \"isc\","
  542. " \"encapsulate\": \"\""
  543. " } ]"
  544. "}";
  545. ElementPtr json = Element::fromJSON(config);
  546. // Make sure that the option definition does not exist yet.
  547. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  548. // Use the configuration string to create new option definitions.
  549. ConstElementPtr status;
  550. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  551. ASSERT_TRUE(status);
  552. checkResult(status, 1);
  553. }
  554. // The goal of this test is to verify that the option definition
  555. // comprising an array of uint32 values can be created.
  556. TEST_F(Dhcp4ParserTest, optionDefArray) {
  557. // Configuration string. Created option definition should
  558. // comprise an array of uint32 values.
  559. std::string config =
  560. "{ \"option-def\": [ {"
  561. " \"name\": \"foo\","
  562. " \"code\": 100,"
  563. " \"type\": \"uint32\","
  564. " \"array\": True,"
  565. " \"record-types\": \"\","
  566. " \"space\": \"isc\","
  567. " \"encapsulate\": \"\""
  568. " } ]"
  569. "}";
  570. ElementPtr json = Element::fromJSON(config);
  571. // Make sure that the particular option definition does not exist.
  572. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  573. ASSERT_FALSE(def);
  574. // Use the configuration string to create new option definition.
  575. ConstElementPtr status;
  576. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  577. ASSERT_TRUE(status);
  578. checkResult(status, 0);
  579. // The option definition should now be available in the CfgMgr.
  580. def = CfgMgr::instance().getOptionDef("isc", 100);
  581. ASSERT_TRUE(def);
  582. // Check the option data.
  583. EXPECT_EQ("foo", def->getName());
  584. EXPECT_EQ(100, def->getCode());
  585. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  586. EXPECT_TRUE(def->getArrayType());
  587. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  588. }
  589. // The purpose of this test to verify that encapsulated option
  590. // space name may be specified.
  591. TEST_F(Dhcp4ParserTest, optionDefEncapsulate) {
  592. // Configuration string. Included the encapsulated
  593. // option space name.
  594. std::string config =
  595. "{ \"option-def\": [ {"
  596. " \"name\": \"foo\","
  597. " \"code\": 100,"
  598. " \"type\": \"uint32\","
  599. " \"array\": False,"
  600. " \"record-types\": \"\","
  601. " \"space\": \"isc\","
  602. " \"encapsulate\": \"sub-opts-space\""
  603. " } ]"
  604. "}";
  605. ElementPtr json = Element::fromJSON(config);
  606. // Make sure that the particular option definition does not exist.
  607. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  608. ASSERT_FALSE(def);
  609. // Use the configuration string to create new option definition.
  610. ConstElementPtr status;
  611. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  612. ASSERT_TRUE(status);
  613. checkResult(status, 0);
  614. // The option definition should now be available in the CfgMgr.
  615. def = CfgMgr::instance().getOptionDef("isc", 100);
  616. ASSERT_TRUE(def);
  617. // Check the option data.
  618. EXPECT_EQ("foo", def->getName());
  619. EXPECT_EQ(100, def->getCode());
  620. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  621. EXPECT_FALSE(def->getArrayType());
  622. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  623. }
  624. /// The purpose of this test is to verify that the option definition
  625. /// with invalid name is not accepted.
  626. TEST_F(Dhcp4ParserTest, optionDefInvalidName) {
  627. // Configuration string. The option name is invalid as it
  628. // contains the % character.
  629. std::string config =
  630. "{ \"option-def\": [ {"
  631. " \"name\": \"invalid%name\","
  632. " \"code\": 100,"
  633. " \"type\": \"string\","
  634. " \"array\": False,"
  635. " \"record-types\": \"\","
  636. " \"space\": \"isc\","
  637. " \"encapsulate\": \"\""
  638. " } ]"
  639. "}";
  640. ElementPtr json = Element::fromJSON(config);
  641. // Use the configuration string to create new option definition.
  642. ConstElementPtr status;
  643. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  644. ASSERT_TRUE(status);
  645. // Expecting parsing error (error code 1).
  646. checkResult(status, 1);
  647. }
  648. /// The purpose of this test is to verify that the option definition
  649. /// with invalid type is not accepted.
  650. TEST_F(Dhcp4ParserTest, optionDefInvalidType) {
  651. // Configuration string. The option type is invalid. It is
  652. // "sting" instead of "string".
  653. std::string config =
  654. "{ \"option-def\": [ {"
  655. " \"name\": \"foo\","
  656. " \"code\": 100,"
  657. " \"type\": \"sting\","
  658. " \"array\": False,"
  659. " \"record-types\": \"\","
  660. " \"space\": \"isc\","
  661. " \"encapsulate\": \"\""
  662. " } ]"
  663. "}";
  664. ElementPtr json = Element::fromJSON(config);
  665. // Use the configuration string to create new option definition.
  666. ConstElementPtr status;
  667. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  668. ASSERT_TRUE(status);
  669. // Expecting parsing error (error code 1).
  670. checkResult(status, 1);
  671. }
  672. /// The purpose of this test is to verify that the option definition
  673. /// with invalid type is not accepted.
  674. TEST_F(Dhcp4ParserTest, optionDefInvalidRecordType) {
  675. // Configuration string. The third of the record fields
  676. // is invalid. It is "sting" instead of "string".
  677. std::string config =
  678. "{ \"option-def\": [ {"
  679. " \"name\": \"foo\","
  680. " \"code\": 100,"
  681. " \"type\": \"record\","
  682. " \"array\": False,"
  683. " \"record-types\": \"uint32,uint8,sting\","
  684. " \"space\": \"isc\","
  685. " \"encapsulate\": \"\""
  686. " } ]"
  687. "}";
  688. ElementPtr json = Element::fromJSON(config);
  689. // Use the configuration string to create new option definition.
  690. ConstElementPtr status;
  691. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  692. ASSERT_TRUE(status);
  693. // Expecting parsing error (error code 1).
  694. checkResult(status, 1);
  695. }
  696. /// The goal of this test is to verify that the invalid encapsulated
  697. /// option space name is not accepted.
  698. TEST_F(Dhcp4ParserTest, optionDefInvalidEncapsulatedSpace) {
  699. // Configuration string. The encapsulated option space
  700. // name is invalid (% character is not allowed).
  701. std::string config =
  702. "{ \"option-def\": [ {"
  703. " \"name\": \"foo\","
  704. " \"code\": 100,"
  705. " \"type\": \"uint32\","
  706. " \"array\": False,"
  707. " \"record-types\": \"\","
  708. " \"space\": \"isc\","
  709. " \"encapsulate\": \"invalid%space%name\""
  710. " } ]"
  711. "}";
  712. ElementPtr json = Element::fromJSON(config);
  713. // Use the configuration string to create new option definition.
  714. ConstElementPtr status;
  715. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  716. ASSERT_TRUE(status);
  717. // Expecting parsing error (error code 1).
  718. checkResult(status, 1);
  719. }
  720. /// The goal of this test is to verify that the encapsulated
  721. /// option space name can't be specified for the option that
  722. /// comprises an array of data fields.
  723. TEST_F(Dhcp4ParserTest, optionDefEncapsulatedSpaceAndArray) {
  724. // Configuration string. The encapsulated option space
  725. // name is set to non-empty value and the array flag
  726. // is set.
  727. std::string config =
  728. "{ \"option-def\": [ {"
  729. " \"name\": \"foo\","
  730. " \"code\": 100,"
  731. " \"type\": \"uint32\","
  732. " \"array\": True,"
  733. " \"record-types\": \"\","
  734. " \"space\": \"isc\","
  735. " \"encapsulate\": \"valid-space-name\""
  736. " } ]"
  737. "}";
  738. ElementPtr json = Element::fromJSON(config);
  739. // Use the configuration string to create new option definition.
  740. ConstElementPtr status;
  741. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  742. ASSERT_TRUE(status);
  743. // Expecting parsing error (error code 1).
  744. checkResult(status, 1);
  745. }
  746. /// The goal of this test is to verify that the option may not
  747. /// encapsulate option space it belongs to.
  748. TEST_F(Dhcp4ParserTest, optionDefEncapsulateOwnSpace) {
  749. // Configuration string. Option is set to encapsulate
  750. // option space it belongs to.
  751. std::string config =
  752. "{ \"option-def\": [ {"
  753. " \"name\": \"foo\","
  754. " \"code\": 100,"
  755. " \"type\": \"uint32\","
  756. " \"array\": False,"
  757. " \"record-types\": \"\","
  758. " \"space\": \"isc\","
  759. " \"encapsulate\": \"isc\""
  760. " } ]"
  761. "}";
  762. ElementPtr json = Element::fromJSON(config);
  763. // Use the configuration string to create new option definition.
  764. ConstElementPtr status;
  765. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  766. ASSERT_TRUE(status);
  767. // Expecting parsing error (error code 1).
  768. checkResult(status, 1);
  769. }
  770. /// The purpose of this test is to verify that it is not allowed
  771. /// to override the standard option (that belongs to dhcp4 option
  772. /// space) and that it is allowed to define option in the dhcp4
  773. /// option space that has a code which is not used by any of the
  774. /// standard options.
  775. TEST_F(Dhcp4ParserTest, optionStandardDefOverride) {
  776. // Configuration string. The option code 109 is unassigned
  777. // so it can be used for a custom option definition in
  778. // dhcp4 option space.
  779. std::string config =
  780. "{ \"option-def\": [ {"
  781. " \"name\": \"foo\","
  782. " \"code\": 109,"
  783. " \"type\": \"string\","
  784. " \"array\": False,"
  785. " \"record-types\": \"\","
  786. " \"space\": \"dhcp4\","
  787. " \"encapsulate\": \"\""
  788. " } ]"
  789. "}";
  790. ElementPtr json = Element::fromJSON(config);
  791. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("dhcp4", 109);
  792. ASSERT_FALSE(def);
  793. // Use the configuration string to create new option definition.
  794. ConstElementPtr status;
  795. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  796. ASSERT_TRUE(status);
  797. checkResult(status, 0);
  798. // The option definition should now be available in the CfgMgr.
  799. def = CfgMgr::instance().getOptionDef("dhcp4", 109);
  800. ASSERT_TRUE(def);
  801. // Check the option data.
  802. EXPECT_EQ("foo", def->getName());
  803. EXPECT_EQ(109, def->getCode());
  804. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  805. EXPECT_FALSE(def->getArrayType());
  806. // The combination of option space and code is
  807. // invalid. The 'dhcp4' option space groups
  808. // standard options and the code 100 is reserved
  809. // for one of them.
  810. config =
  811. "{ \"option-def\": [ {"
  812. " \"name\": \"foo\","
  813. " \"code\": 100,"
  814. " \"type\": \"string\","
  815. " \"array\": False,"
  816. " \"record-types\": \"\","
  817. " \"space\": \"dhcp4\","
  818. " \"encapsulate\": \"\""
  819. " } ]"
  820. "}";
  821. json = Element::fromJSON(config);
  822. // Use the configuration string to create new option definition.
  823. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  824. ASSERT_TRUE(status);
  825. // Expecting parsing error (error code 1).
  826. checkResult(status, 1);
  827. }
  828. // Goal of this test is to verify that global option
  829. // data is configured for the subnet if the subnet
  830. // configuration does not include options configuration.
  831. TEST_F(Dhcp4ParserTest, optionDataDefaults) {
  832. ConstElementPtr x;
  833. string config = "{ \"interface\": [ \"all\" ],"
  834. "\"rebind-timer\": 2000,"
  835. "\"renew-timer\": 1000,"
  836. "\"option-data\": [ {"
  837. " \"name\": \"dhcp-message\","
  838. " \"space\": \"dhcp4\","
  839. " \"code\": 56,"
  840. " \"data\": \"AB CDEF0105\","
  841. " \"csv-format\": False"
  842. " },"
  843. " {"
  844. " \"name\": \"default-ip-ttl\","
  845. " \"space\": \"dhcp4\","
  846. " \"code\": 23,"
  847. " \"data\": \"01\","
  848. " \"csv-format\": False"
  849. " } ],"
  850. "\"subnet4\": [ { "
  851. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  852. " \"subnet\": \"192.0.2.0/24\""
  853. " } ],"
  854. "\"valid-lifetime\": 4000 }";
  855. ElementPtr json = Element::fromJSON(config);
  856. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  857. ASSERT_TRUE(x);
  858. comment_ = parseAnswer(rcode_, x);
  859. ASSERT_EQ(0, rcode_);
  860. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  861. ASSERT_TRUE(subnet);
  862. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  863. ASSERT_EQ(2, options->size());
  864. // Get the search index. Index #1 is to search using option code.
  865. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  866. // Get the options for specified index. Expecting one option to be
  867. // returned but in theory we may have multiple options with the same
  868. // code so we get the range.
  869. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  870. Subnet::OptionContainerTypeIndex::const_iterator> range =
  871. idx.equal_range(56);
  872. // Expect single option with the code equal to 56.
  873. ASSERT_EQ(1, std::distance(range.first, range.second));
  874. const uint8_t foo_expected[] = {
  875. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  876. };
  877. // Check if option is valid in terms of code and carried data.
  878. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  879. range = idx.equal_range(23);
  880. ASSERT_EQ(1, std::distance(range.first, range.second));
  881. // Do another round of testing with second option.
  882. const uint8_t foo2_expected[] = {
  883. 0x01
  884. };
  885. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  886. }
  887. /// The goal of this test is to verify that two options having the same
  888. /// option code can be added to different option spaces.
  889. TEST_F(Dhcp4ParserTest, optionDataTwoSpaces) {
  890. // This configuration string is to configure two options
  891. // sharing the code 56 and having different definitions
  892. // and belonging to the different option spaces.
  893. // The option definition must be provided for the
  894. // option that belongs to the 'isc' option space.
  895. // The definition is not required for the option that
  896. // belongs to the 'dhcp4' option space as it is the
  897. // standard option.
  898. string config = "{ \"interface\": [ \"all\" ],"
  899. "\"rebind-timer\": 2000,"
  900. "\"renew-timer\": 1000,"
  901. "\"option-data\": [ {"
  902. " \"name\": \"dhcp-message\","
  903. " \"space\": \"dhcp4\","
  904. " \"code\": 56,"
  905. " \"data\": \"AB CDEF0105\","
  906. " \"csv-format\": False"
  907. " },"
  908. " {"
  909. " \"name\": \"foo\","
  910. " \"space\": \"isc\","
  911. " \"code\": 56,"
  912. " \"data\": \"1234\","
  913. " \"csv-format\": True"
  914. " } ],"
  915. "\"option-def\": [ {"
  916. " \"name\": \"foo\","
  917. " \"code\": 56,"
  918. " \"type\": \"uint32\","
  919. " \"array\": False,"
  920. " \"record-types\": \"\","
  921. " \"space\": \"isc\","
  922. " \"encapsulate\": \"\""
  923. " } ],"
  924. "\"subnet4\": [ { "
  925. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  926. " \"subnet\": \"192.0.2.0/24\""
  927. " } ]"
  928. "}";
  929. ConstElementPtr status;
  930. ElementPtr json = Element::fromJSON(config);
  931. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  932. ASSERT_TRUE(status);
  933. checkResult(status, 0);
  934. // Options should be now available for the subnet.
  935. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  936. ASSERT_TRUE(subnet);
  937. // Try to get the option from the space dhcp4.
  938. Subnet::OptionDescriptor desc1 = subnet->getOptionDescriptor("dhcp4", 56);
  939. ASSERT_TRUE(desc1.option);
  940. EXPECT_EQ(56, desc1.option->getType());
  941. // Try to get the option from the space isc.
  942. Subnet::OptionDescriptor desc2 = subnet->getOptionDescriptor("isc", 56);
  943. ASSERT_TRUE(desc2.option);
  944. EXPECT_EQ(56, desc1.option->getType());
  945. // Try to get the non-existing option from the non-existing
  946. // option space and expect that option is not returned.
  947. Subnet::OptionDescriptor desc3 = subnet->getOptionDescriptor("non-existing", 56);
  948. ASSERT_FALSE(desc3.option);
  949. }
  950. // The goal of this test is to verify that it is possible to
  951. // encapsulate option space containing some options with
  952. // another option. In this test we create base option that
  953. // encapsulates option space 'isc' that comprises two other
  954. // options. Also, for all options their definitions are
  955. // created.
  956. TEST_F(Dhcp4ParserTest, optionDataEncapsulate) {
  957. // @todo DHCP configurations has many dependencies between
  958. // parameters. First of all, configuration for subnet is
  959. // inherited from the global values. Thus subnet has to be
  960. // configured when all global values have been configured.
  961. // Also, an option can encapsulate another option only
  962. // if the latter has been configured. For this reason in this
  963. // test we created two-stage configuration where first we
  964. // created options that belong to encapsulated option space.
  965. // In the second stage we add the base option. Also, the Subnet
  966. // object is configured in the second stage so it is created
  967. // at the very end (when all other parameters are configured).
  968. // Starting stage 1. Configure sub-options and their definitions.
  969. string config = "{ \"interface\": [ \"all\" ],"
  970. "\"rebind-timer\": 2000,"
  971. "\"renew-timer\": 1000,"
  972. "\"option-data\": [ {"
  973. " \"name\": \"foo\","
  974. " \"space\": \"isc\","
  975. " \"code\": 1,"
  976. " \"data\": \"1234\","
  977. " \"csv-format\": True"
  978. " },"
  979. " {"
  980. " \"name\": \"foo2\","
  981. " \"space\": \"isc\","
  982. " \"code\": 2,"
  983. " \"data\": \"192.168.2.1\","
  984. " \"csv-format\": True"
  985. " } ],"
  986. "\"option-def\": [ {"
  987. " \"name\": \"foo\","
  988. " \"code\": 1,"
  989. " \"type\": \"uint32\","
  990. " \"array\": False,"
  991. " \"record-types\": \"\","
  992. " \"space\": \"isc\","
  993. " \"encapsulate\": \"\""
  994. " },"
  995. " {"
  996. " \"name\": \"foo2\","
  997. " \"code\": 2,"
  998. " \"type\": \"ipv4-address\","
  999. " \"array\": False,"
  1000. " \"record-types\": \"\","
  1001. " \"space\": \"isc\","
  1002. " \"encapsulate\": \"\""
  1003. " } ]"
  1004. "}";
  1005. ConstElementPtr status;
  1006. ElementPtr json = Element::fromJSON(config);
  1007. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1008. ASSERT_TRUE(status);
  1009. checkResult(status, 0);
  1010. // Stage 2. Configure base option and a subnet. Please note that
  1011. // the configuration from the stage 2 is repeated because BIND
  1012. // configuration manager sends whole configuration for the lists
  1013. // where at least one element is being modified or added.
  1014. config = "{ \"interface\": [ \"all\" ],"
  1015. "\"rebind-timer\": 2000,"
  1016. "\"renew-timer\": 1000,"
  1017. "\"option-data\": [ {"
  1018. " \"name\": \"base-option\","
  1019. " \"space\": \"dhcp4\","
  1020. " \"code\": 222,"
  1021. " \"data\": \"11\","
  1022. " \"csv-format\": True"
  1023. " },"
  1024. " {"
  1025. " \"name\": \"foo\","
  1026. " \"space\": \"isc\","
  1027. " \"code\": 1,"
  1028. " \"data\": \"1234\","
  1029. " \"csv-format\": True"
  1030. " },"
  1031. " {"
  1032. " \"name\": \"foo2\","
  1033. " \"space\": \"isc\","
  1034. " \"code\": 2,"
  1035. " \"data\": \"192.168.2.1\","
  1036. " \"csv-format\": True"
  1037. " } ],"
  1038. "\"option-def\": [ {"
  1039. " \"name\": \"base-option\","
  1040. " \"code\": 222,"
  1041. " \"type\": \"uint8\","
  1042. " \"array\": False,"
  1043. " \"record-types\": \"\","
  1044. " \"space\": \"dhcp4\","
  1045. " \"encapsulate\": \"isc\""
  1046. "},"
  1047. "{"
  1048. " \"name\": \"foo\","
  1049. " \"code\": 1,"
  1050. " \"type\": \"uint32\","
  1051. " \"array\": False,"
  1052. " \"record-types\": \"\","
  1053. " \"space\": \"isc\","
  1054. " \"encapsulate\": \"\""
  1055. " },"
  1056. " {"
  1057. " \"name\": \"foo2\","
  1058. " \"code\": 2,"
  1059. " \"type\": \"ipv4-address\","
  1060. " \"array\": False,"
  1061. " \"record-types\": \"\","
  1062. " \"space\": \"isc\","
  1063. " \"encapsulate\": \"\""
  1064. " } ],"
  1065. "\"subnet4\": [ { "
  1066. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1067. " \"subnet\": \"192.0.2.0/24\""
  1068. " } ]"
  1069. "}";
  1070. json = Element::fromJSON(config);
  1071. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1072. ASSERT_TRUE(status);
  1073. checkResult(status, 0);
  1074. // Get the subnet.
  1075. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1076. ASSERT_TRUE(subnet);
  1077. // We should have one option available.
  1078. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1079. ASSERT_TRUE(options);
  1080. ASSERT_EQ(1, options->size());
  1081. // Get the option.
  1082. Subnet::OptionDescriptor desc = subnet->getOptionDescriptor("dhcp4", 222);
  1083. EXPECT_TRUE(desc.option);
  1084. EXPECT_EQ(222, desc.option->getType());
  1085. // This opton should comprise two sub-options.
  1086. // One of them is 'foo' with code 1.
  1087. OptionPtr option_foo = desc.option->getOption(1);
  1088. ASSERT_TRUE(option_foo);
  1089. EXPECT_EQ(1, option_foo->getType());
  1090. // ...another one 'foo2' with code 2.
  1091. OptionPtr option_foo2 = desc.option->getOption(2);
  1092. ASSERT_TRUE(option_foo2);
  1093. EXPECT_EQ(2, option_foo2->getType());
  1094. }
  1095. // Goal of this test is to verify options configuration
  1096. // for a single subnet. In particular this test checks
  1097. // that local options configuration overrides global
  1098. // option setting.
  1099. TEST_F(Dhcp4ParserTest, optionDataInSingleSubnet) {
  1100. ConstElementPtr x;
  1101. string config = "{ \"interface\": [ \"all\" ],"
  1102. "\"rebind-timer\": 2000, "
  1103. "\"renew-timer\": 1000, "
  1104. "\"option-data\": [ {"
  1105. " \"name\": \"dhcp-message\","
  1106. " \"space\": \"dhcp4\","
  1107. " \"code\": 56,"
  1108. " \"data\": \"AB\","
  1109. " \"csv-format\": False"
  1110. " } ],"
  1111. "\"subnet4\": [ { "
  1112. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1113. " \"subnet\": \"192.0.2.0/24\", "
  1114. " \"option-data\": [ {"
  1115. " \"name\": \"dhcp-message\","
  1116. " \"space\": \"dhcp4\","
  1117. " \"code\": 56,"
  1118. " \"data\": \"AB CDEF0105\","
  1119. " \"csv-format\": False"
  1120. " },"
  1121. " {"
  1122. " \"name\": \"default-ip-ttl\","
  1123. " \"space\": \"dhcp4\","
  1124. " \"code\": 23,"
  1125. " \"data\": \"01\","
  1126. " \"csv-format\": False"
  1127. " } ]"
  1128. " } ],"
  1129. "\"valid-lifetime\": 4000 }";
  1130. ElementPtr json = Element::fromJSON(config);
  1131. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1132. ASSERT_TRUE(x);
  1133. comment_ = parseAnswer(rcode_, x);
  1134. ASSERT_EQ(0, rcode_);
  1135. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.24"));
  1136. ASSERT_TRUE(subnet);
  1137. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1138. ASSERT_EQ(2, options->size());
  1139. // Get the search index. Index #1 is to search using option code.
  1140. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1141. // Get the options for specified index. Expecting one option to be
  1142. // returned but in theory we may have multiple options with the same
  1143. // code so we get the range.
  1144. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1145. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1146. idx.equal_range(56);
  1147. // Expect single option with the code equal to 100.
  1148. ASSERT_EQ(1, std::distance(range.first, range.second));
  1149. const uint8_t foo_expected[] = {
  1150. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1151. };
  1152. // Check if option is valid in terms of code and carried data.
  1153. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1154. range = idx.equal_range(23);
  1155. ASSERT_EQ(1, std::distance(range.first, range.second));
  1156. // Do another round of testing with second option.
  1157. const uint8_t foo2_expected[] = {
  1158. 0x01
  1159. };
  1160. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  1161. }
  1162. // Goal of this test is to verify options configuration
  1163. // for multiple subnets.
  1164. TEST_F(Dhcp4ParserTest, optionDataInMultipleSubnets) {
  1165. ConstElementPtr x;
  1166. string config = "{ \"interface\": [ \"all\" ],"
  1167. "\"rebind-timer\": 2000, "
  1168. "\"renew-timer\": 1000, "
  1169. "\"subnet4\": [ { "
  1170. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1171. " \"subnet\": \"192.0.2.0/24\", "
  1172. " \"option-data\": [ {"
  1173. " \"name\": \"dhcp-message\","
  1174. " \"space\": \"dhcp4\","
  1175. " \"code\": 56,"
  1176. " \"data\": \"0102030405060708090A\","
  1177. " \"csv-format\": False"
  1178. " } ]"
  1179. " },"
  1180. " {"
  1181. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  1182. " \"subnet\": \"192.0.3.0/24\", "
  1183. " \"option-data\": [ {"
  1184. " \"name\": \"default-ip-ttl\","
  1185. " \"space\": \"dhcp4\","
  1186. " \"code\": 23,"
  1187. " \"data\": \"FF\","
  1188. " \"csv-format\": False"
  1189. " } ]"
  1190. " } ],"
  1191. "\"valid-lifetime\": 4000 }";
  1192. ElementPtr json = Element::fromJSON(config);
  1193. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1194. ASSERT_TRUE(x);
  1195. comment_ = parseAnswer(rcode_, x);
  1196. ASSERT_EQ(0, rcode_);
  1197. Subnet4Ptr subnet1 = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.100"));
  1198. ASSERT_TRUE(subnet1);
  1199. Subnet::OptionContainerPtr options1 = subnet1->getOptionDescriptors("dhcp4");
  1200. ASSERT_EQ(1, options1->size());
  1201. // Get the search index. Index #1 is to search using option code.
  1202. const Subnet::OptionContainerTypeIndex& idx1 = options1->get<1>();
  1203. // Get the options for specified index. Expecting one option to be
  1204. // returned but in theory we may have multiple options with the same
  1205. // code so we get the range.
  1206. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1207. Subnet::OptionContainerTypeIndex::const_iterator> range1 =
  1208. idx1.equal_range(56);
  1209. // Expect single option with the code equal to 56.
  1210. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  1211. const uint8_t foo_expected[] = {
  1212. 0x01, 0x02, 0x03, 0x04, 0x05,
  1213. 0x06, 0x07, 0x08, 0x09, 0x0A
  1214. };
  1215. // Check if option is valid in terms of code and carried data.
  1216. testOption(*range1.first, 56, foo_expected, sizeof(foo_expected));
  1217. // Test another subnet in the same way.
  1218. Subnet4Ptr subnet2 = CfgMgr::instance().getSubnet4(IOAddress("192.0.3.102"));
  1219. ASSERT_TRUE(subnet2);
  1220. Subnet::OptionContainerPtr options2 = subnet2->getOptionDescriptors("dhcp4");
  1221. ASSERT_EQ(1, options2->size());
  1222. const Subnet::OptionContainerTypeIndex& idx2 = options2->get<1>();
  1223. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1224. Subnet::OptionContainerTypeIndex::const_iterator> range2 =
  1225. idx2.equal_range(23);
  1226. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  1227. const uint8_t foo2_expected[] = { 0xFF };
  1228. testOption(*range2.first, 23, foo2_expected, sizeof(foo2_expected));
  1229. }
  1230. // Verify that empty option name is rejected in the configuration.
  1231. TEST_F(Dhcp4ParserTest, optionNameEmpty) {
  1232. // Empty option names not allowed.
  1233. testInvalidOptionParam("", "name");
  1234. }
  1235. // Verify that empty option name with spaces is rejected
  1236. // in the configuration.
  1237. TEST_F(Dhcp4ParserTest, optionNameSpaces) {
  1238. // Spaces in option names not allowed.
  1239. testInvalidOptionParam("option foo", "name");
  1240. }
  1241. // Verify that negative option code is rejected in the configuration.
  1242. TEST_F(Dhcp4ParserTest, optionCodeNegative) {
  1243. // Check negative option code -4. This should fail too.
  1244. testInvalidOptionParam("-4", "code");
  1245. }
  1246. // Verify that out of bounds option code is rejected in the configuration.
  1247. TEST_F(Dhcp4ParserTest, optionCodeNonUint8) {
  1248. // The valid option codes are uint16_t values so passing
  1249. // uint16_t maximum value incremented by 1 should result
  1250. // in failure.
  1251. testInvalidOptionParam("257", "code");
  1252. }
  1253. // Verify that zero option code is rejected in the configuration.
  1254. TEST_F(Dhcp4ParserTest, optionCodeZero) {
  1255. // Option code 0 is reserved and should not be accepted
  1256. // by configuration parser.
  1257. testInvalidOptionParam("0", "code");
  1258. }
  1259. // Verify that option data which contains non hexadecimal characters
  1260. // is rejected by the configuration.
  1261. TEST_F(Dhcp4ParserTest, optionDataInvalidChar) {
  1262. // Option code 0 is reserved and should not be accepted
  1263. // by configuration parser.
  1264. testInvalidOptionParam("01020R", "data");
  1265. }
  1266. // Verify that option data containing '0x' prefix is rejected
  1267. // by the configuration.
  1268. TEST_F(Dhcp4ParserTest, optionDataUnexpectedPrefix) {
  1269. // Option code 0 is reserved and should not be accepted
  1270. // by configuration parser.
  1271. testInvalidOptionParam("0x0102", "data");
  1272. }
  1273. // Verify that option data consisting od an odd number of
  1274. // hexadecimal digits is rejected in the configuration.
  1275. TEST_F(Dhcp4ParserTest, optionDataOddLength) {
  1276. // Option code 0 is reserved and should not be accepted
  1277. // by configuration parser.
  1278. testInvalidOptionParam("123", "data");
  1279. }
  1280. // Verify that either lower or upper case characters are allowed
  1281. // to specify the option data.
  1282. TEST_F(Dhcp4ParserTest, optionDataLowerCase) {
  1283. ConstElementPtr x;
  1284. std::string config = createConfigWithOption("0a0b0C0D", "data");
  1285. ElementPtr json = Element::fromJSON(config);
  1286. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1287. ASSERT_TRUE(x);
  1288. comment_ = parseAnswer(rcode_, x);
  1289. ASSERT_EQ(0, rcode_);
  1290. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1291. ASSERT_TRUE(subnet);
  1292. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1293. ASSERT_EQ(1, options->size());
  1294. // Get the search index. Index #1 is to search using option code.
  1295. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1296. // Get the options for specified index. Expecting one option to be
  1297. // returned but in theory we may have multiple options with the same
  1298. // code so we get the range.
  1299. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1300. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1301. idx.equal_range(56);
  1302. // Expect single option with the code equal to 100.
  1303. ASSERT_EQ(1, std::distance(range.first, range.second));
  1304. const uint8_t foo_expected[] = {
  1305. 0x0A, 0x0B, 0x0C, 0x0D
  1306. };
  1307. // Check if option is valid in terms of code and carried data.
  1308. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1309. }
  1310. // Verify that specific option object is returned for standard
  1311. // option which has dedicated option class derived from Option.
  1312. TEST_F(Dhcp4ParserTest, stdOptionData) {
  1313. ConstElementPtr x;
  1314. std::map<std::string, std::string> params;
  1315. params["name"] = "nis-servers";
  1316. params["space"] = "dhcp4";
  1317. // Option code 41 means nis-servers.
  1318. params["code"] = "41";
  1319. // Specify option values in a CSV (user friendly) format.
  1320. params["data"] = "192.0.2.10, 192.0.2.1, 192.0.2.3";
  1321. params["csv-format"] = "True";
  1322. std::string config = createConfigWithOption(params);
  1323. ElementPtr json = Element::fromJSON(config);
  1324. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1325. ASSERT_TRUE(x);
  1326. comment_ = parseAnswer(rcode_, x);
  1327. ASSERT_EQ(0, rcode_);
  1328. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1329. ASSERT_TRUE(subnet);
  1330. Subnet::OptionContainerPtr options =
  1331. subnet->getOptionDescriptors("dhcp4");
  1332. ASSERT_TRUE(options);
  1333. ASSERT_EQ(1, options->size());
  1334. // Get the search index. Index #1 is to search using option code.
  1335. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1336. // Get the options for specified index. Expecting one option to be
  1337. // returned but in theory we may have multiple options with the same
  1338. // code so we get the range.
  1339. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1340. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1341. idx.equal_range(DHO_NIS_SERVERS);
  1342. // Expect single option with the code equal to NIS_SERVERS option code.
  1343. ASSERT_EQ(1, std::distance(range.first, range.second));
  1344. // The actual pointer to the option is held in the option field
  1345. // in the structure returned.
  1346. OptionPtr option = range.first->option;
  1347. ASSERT_TRUE(option);
  1348. // Option object returned for here is expected to be Option6IA
  1349. // which is derived from Option. This class is dedicated to
  1350. // represent standard option IA_NA.
  1351. boost::shared_ptr<Option4AddrLst> option_addrs =
  1352. boost::dynamic_pointer_cast<Option4AddrLst>(option);
  1353. // If cast is unsuccessful than option returned was of a
  1354. // different type than Option6IA. This is wrong.
  1355. ASSERT_TRUE(option_addrs);
  1356. // Get addresses from the option.
  1357. Option4AddrLst::AddressContainer addrs = option_addrs->getAddresses();
  1358. // Verify that the addresses have been configured correctly.
  1359. ASSERT_EQ(3, addrs.size());
  1360. EXPECT_EQ("192.0.2.10", addrs[0].toText());
  1361. EXPECT_EQ("192.0.2.1", addrs[1].toText());
  1362. EXPECT_EQ("192.0.2.3", addrs[2].toText());
  1363. }
  1364. /// This test checks if Uint32Parser can really parse the whole range
  1365. /// and properly err of out of range values. As we can't call Uint32Parser
  1366. /// directly, we are exploiting the fact that it is used to parse global
  1367. /// parameter renew-timer and the results are stored in uint32_defaults.
  1368. /// We get the uint32_defaults using a getUint32Defaults functions which
  1369. /// is defined only to access the values from this test.
  1370. TEST_F(Dhcp4ParserTest, DISABLED_Uint32Parser) {
  1371. ConstElementPtr status;
  1372. // CASE 1: 0 - minimum value, should work
  1373. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1374. Element::fromJSON("{\"version\": 0,"
  1375. "\"renew-timer\": 0}")));
  1376. // returned value must be ok (0 is a proper value)
  1377. checkResult(status, 0);
  1378. checkGlobalUint32("renew-timer", 0);
  1379. // CASE 2: 4294967295U (UINT_MAX) should work as well
  1380. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1381. Element::fromJSON("{\"version\": 0,"
  1382. "\"renew-timer\": 4294967295}")));
  1383. // returned value must be ok (0 is a proper value)
  1384. checkResult(status, 0);
  1385. checkGlobalUint32("renew-timer", 4294967295U);
  1386. // CASE 3: 4294967296U (UINT_MAX + 1) should not work
  1387. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1388. Element::fromJSON("{\"version\": 0,"
  1389. "\"renew-timer\": 4294967296}")));
  1390. // returned value must be rejected (1 configuration error)
  1391. checkResult(status, 1);
  1392. // CASE 4: -1 (UINT_MIN -1 ) should not work
  1393. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1394. Element::fromJSON("{\"version\": 0,"
  1395. "\"renew-timer\": -1}")));
  1396. // returned value must be rejected (1 configuration error)
  1397. checkResult(status, 1);
  1398. }
  1399. // The goal of this test is to verify that the standard option can
  1400. // be configured to encapsulate multiple other options.
  1401. TEST_F(Dhcp4ParserTest, stdOptionDataEncapsulate) {
  1402. // The configuration is two stage process in this test.
  1403. // In the first stahe we create definitions of suboptions
  1404. // that we will add to the base option.
  1405. // Let's create some dummy options: foo and foo2.
  1406. string config = "{ \"interface\": [ \"all\" ],"
  1407. "\"rebind-timer\": 2000,"
  1408. "\"renew-timer\": 1000,"
  1409. "\"option-data\": [ {"
  1410. " \"name\": \"foo\","
  1411. " \"space\": \"vendor-encapsulated-options-space\","
  1412. " \"code\": 1,"
  1413. " \"data\": \"1234\","
  1414. " \"csv-format\": True"
  1415. " },"
  1416. " {"
  1417. " \"name\": \"foo2\","
  1418. " \"space\": \"vendor-encapsulated-options-space\","
  1419. " \"code\": 2,"
  1420. " \"data\": \"192.168.2.1\","
  1421. " \"csv-format\": True"
  1422. " } ],"
  1423. "\"option-def\": [ {"
  1424. " \"name\": \"foo\","
  1425. " \"code\": 1,"
  1426. " \"type\": \"uint32\","
  1427. " \"array\": False,"
  1428. " \"record-types\": \"\","
  1429. " \"space\": \"vendor-encapsulated-options-space\","
  1430. " \"encapsulate\": \"\""
  1431. " },"
  1432. " {"
  1433. " \"name\": \"foo2\","
  1434. " \"code\": 2,"
  1435. " \"type\": \"ipv4-address\","
  1436. " \"array\": False,"
  1437. " \"record-types\": \"\","
  1438. " \"space\": \"vendor-encapsulated-options-space\","
  1439. " \"encapsulate\": \"\""
  1440. " } ]"
  1441. "}";
  1442. ConstElementPtr status;
  1443. ElementPtr json = Element::fromJSON(config);
  1444. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1445. ASSERT_TRUE(status);
  1446. checkResult(status, 0);
  1447. // Once the definitions have been added we can configure the
  1448. // standard option #17. This option comprises an enterprise
  1449. // number and sub options. By convention (introduced in
  1450. // std_option_defs.h) option named 'vendor-opts'
  1451. // encapsulates the option space named 'vendor-opts-space'.
  1452. // We add our dummy options to this option space and thus
  1453. // they should be included as sub-options in the 'vendor-opts'
  1454. // option.
  1455. config = "{ \"interface\": [ \"all\" ],"
  1456. "\"rebind-timer\": 2000,"
  1457. "\"renew-timer\": 1000,"
  1458. "\"option-data\": [ {"
  1459. " \"name\": \"vendor-encapsulated-options\","
  1460. " \"space\": \"dhcp4\","
  1461. " \"code\": 43,"
  1462. " \"data\": \"\","
  1463. " \"csv-format\": False"
  1464. " },"
  1465. " {"
  1466. " \"name\": \"foo\","
  1467. " \"space\": \"vendor-encapsulated-options-space\","
  1468. " \"code\": 1,"
  1469. " \"data\": \"1234\","
  1470. " \"csv-format\": True"
  1471. " },"
  1472. " {"
  1473. " \"name\": \"foo2\","
  1474. " \"space\": \"vendor-encapsulated-options-space\","
  1475. " \"code\": 2,"
  1476. " \"data\": \"192.168.2.1\","
  1477. " \"csv-format\": True"
  1478. " } ],"
  1479. "\"option-def\": [ {"
  1480. " \"name\": \"foo\","
  1481. " \"code\": 1,"
  1482. " \"type\": \"uint32\","
  1483. " \"array\": False,"
  1484. " \"record-types\": \"\","
  1485. " \"space\": \"vendor-encapsulated-options-space\","
  1486. " \"encapsulate\": \"\""
  1487. " },"
  1488. " {"
  1489. " \"name\": \"foo2\","
  1490. " \"code\": 2,"
  1491. " \"type\": \"ipv4-address\","
  1492. " \"array\": False,"
  1493. " \"record-types\": \"\","
  1494. " \"space\": \"vendor-encapsulated-options-space\","
  1495. " \"encapsulate\": \"\""
  1496. " } ],"
  1497. "\"subnet4\": [ { "
  1498. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1499. " \"subnet\": \"192.0.2.0/24\""
  1500. " } ]"
  1501. "}";
  1502. json = Element::fromJSON(config);
  1503. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1504. ASSERT_TRUE(status);
  1505. checkResult(status, 0);
  1506. // Get the subnet.
  1507. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1508. ASSERT_TRUE(subnet);
  1509. // We should have one option available.
  1510. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1511. ASSERT_TRUE(options);
  1512. ASSERT_EQ(1, options->size());
  1513. // Get the option.
  1514. Subnet::OptionDescriptor desc =
  1515. subnet->getOptionDescriptor("dhcp4", DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1516. EXPECT_TRUE(desc.option);
  1517. EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, desc.option->getType());
  1518. // Option with the code 1 should be added as a sub-option.
  1519. OptionPtr option_foo = desc.option->getOption(1);
  1520. ASSERT_TRUE(option_foo);
  1521. EXPECT_EQ(1, option_foo->getType());
  1522. // This option comprises a single uint32_t value thus it is
  1523. // represented by OptionInt<uint32_t> class. Let's get the
  1524. // object of this type.
  1525. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  1526. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  1527. ASSERT_TRUE(option_foo_uint32);
  1528. // Validate the value according to the configuration.
  1529. EXPECT_EQ(1234, option_foo_uint32->getValue());
  1530. // Option with the code 2 should be added as a sub-option.
  1531. OptionPtr option_foo2 = desc.option->getOption(2);
  1532. ASSERT_TRUE(option_foo2);
  1533. EXPECT_EQ(2, option_foo2->getType());
  1534. // This option comprises the IPV4 address. Such option is
  1535. // represented by OptionCustom object.
  1536. OptionCustomPtr option_foo2_v4 =
  1537. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  1538. ASSERT_TRUE(option_foo2_v4);
  1539. // Get the IP address carried by this option and validate it.
  1540. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  1541. // Option with the code 3 should not be added.
  1542. EXPECT_FALSE(desc.option->getOption(3));
  1543. }
  1544. };