config_parser_unittest.cc 77 KB

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