config_parser_unittest.cc 96 KB

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