config_parser_unittest.cc 74 KB

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