config_parser_unittest.cc 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084
  1. // Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <arpa/inet.h>
  16. #include <gtest/gtest.h>
  17. #include <config/ccsession.h>
  18. #include <dhcp4/dhcp4_srv.h>
  19. #include <dhcp4/config_parser.h>
  20. #include <dhcp/option4_addrlst.h>
  21. #include <dhcp/option_custom.h>
  22. #include <dhcp/option_int.h>
  23. #include <dhcp/docsis3_option_defs.h>
  24. #include <dhcp/classify.h>
  25. #include <dhcpsrv/subnet.h>
  26. #include <dhcpsrv/cfgmgr.h>
  27. #include <hooks/hooks_manager.h>
  28. #include "marker_file.h"
  29. #include "test_libraries.h"
  30. #include "test_data_files_config.h"
  31. #include <boost/foreach.hpp>
  32. #include <boost/scoped_ptr.hpp>
  33. #include <iostream>
  34. #include <fstream>
  35. #include <sstream>
  36. #include <limits.h>
  37. using namespace isc;
  38. using namespace isc::asiolink;
  39. using namespace isc::config;
  40. using namespace isc::data;
  41. using namespace isc::dhcp;
  42. using namespace isc::dhcp::test;
  43. using namespace isc::hooks;
  44. using namespace std;
  45. namespace {
  46. /// @brief Prepends the given name with the DHCP4 source directory
  47. ///
  48. /// @param name file name of the desired file
  49. /// @return string containing the absolute path of the file in the DHCP source
  50. /// directory.
  51. std::string specfile(const std::string& name) {
  52. return (std::string(DHCP4_SRC_DIR) + "/" + name);
  53. }
  54. /// @brief Tests that the spec file is valid.
  55. /// Verifies that the BIND10 DHCP-DDNS configuration specification file
  56. // is valid.
  57. TEST(Dhcp4SpecTest, basicSpec) {
  58. (isc::config::moduleSpecFromFile(specfile("dhcp4.spec")));
  59. ASSERT_NO_THROW(isc::config::moduleSpecFromFile(specfile("dhcp4.spec")));
  60. }
  61. class Dhcp4ParserTest : public ::testing::Test {
  62. public:
  63. Dhcp4ParserTest()
  64. :rcode_(-1) {
  65. // Open port 0 means to not do anything at all. We don't want to
  66. // deal with sockets here, just check if configuration handling
  67. // is sane.
  68. srv_.reset(new Dhcpv4Srv(0));
  69. CfgMgr::instance().deleteActiveIfaces();
  70. }
  71. // Check that no hooks libraries are loaded. This is a pre-condition for
  72. // a number of tests, so is checked in one place. As this uses an
  73. // ASSERT call - and it is not clear from the documentation that Gtest
  74. // predicates can be used in a constructor - the check is placed in SetUp.
  75. void SetUp() {
  76. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  77. ASSERT_TRUE(libraries.empty());
  78. }
  79. // Checks if global parameter of name have expected_value
  80. void checkGlobalUint32(string name, uint32_t expected_value) {
  81. const Uint32StoragePtr uint32_defaults =
  82. globalContext()->uint32_values_;
  83. try {
  84. uint32_t actual_value = uint32_defaults->getParam(name);
  85. EXPECT_EQ(expected_value, actual_value);
  86. } catch (DhcpConfigError) {
  87. ADD_FAILURE() << "Expected uint32 with name " << name
  88. << " not found";
  89. }
  90. }
  91. // Checks if the result of DHCP server configuration has
  92. // expected code (0 for success, other for failures).
  93. // Also stores result in rcode_ and comment_.
  94. void checkResult(ConstElementPtr status, int expected_code) {
  95. ASSERT_TRUE(status);
  96. comment_ = parseAnswer(rcode_, status);
  97. EXPECT_EQ(expected_code, rcode_);
  98. }
  99. ~Dhcp4ParserTest() {
  100. resetConfiguration();
  101. // ... and delete the hooks library marker files if present
  102. unlink(LOAD_MARKER_FILE);
  103. unlink(UNLOAD_MARKER_FILE);
  104. };
  105. /// @brief Create the simple configuration with single option.
  106. ///
  107. /// This function allows to set one of the parameters that configure
  108. /// option value. These parameters are: "name", "code", "data",
  109. /// "csv-format" and "space".
  110. ///
  111. /// @param param_value string holding option parameter value to be
  112. /// injected into the configuration string.
  113. /// @param parameter name of the parameter to be configured with
  114. /// param value.
  115. /// @return configuration string containing custom values of parameters
  116. /// describing an option.
  117. std::string createConfigWithOption(const std::string& param_value,
  118. const std::string& parameter) {
  119. std::map<std::string, std::string> params;
  120. if (parameter == "name") {
  121. params["name"] = param_value;
  122. params["space"] = "dhcp4";
  123. params["code"] = "56";
  124. params["data"] = "ABCDEF0105";
  125. params["csv-format"] = "False";
  126. } else if (parameter == "space") {
  127. params["name"] = "dhcp-message";
  128. params["space"] = param_value;
  129. params["code"] = "56";
  130. params["data"] = "ABCDEF0105";
  131. params["csv-format"] = "False";
  132. } else if (parameter == "code") {
  133. params["name"] = "dhcp-message";
  134. params["space"] = "dhcp4";
  135. params["code"] = param_value;
  136. params["data"] = "ABCDEF0105";
  137. params["csv-format"] = "False";
  138. } else if (parameter == "data") {
  139. params["name"] = "dhcp-message";
  140. params["space"] = "dhcp4";
  141. params["code"] = "56";
  142. params["data"] = param_value;
  143. params["csv-format"] = "False";
  144. } else if (parameter == "csv-format") {
  145. params["name"] = "dhcp-message";
  146. params["space"] = "dhcp4";
  147. params["code"] = "56";
  148. params["data"] = "ABCDEF0105";
  149. params["csv-format"] = param_value;
  150. }
  151. return (createConfigWithOption(params));
  152. }
  153. /// @brief Create simple configuration with single option.
  154. ///
  155. /// This function creates a configuration for a single option with
  156. /// custom values for all parameters that describe the option.
  157. ///
  158. /// @params params map holding parameters and their values.
  159. /// @return configuration string containing custom values of parameters
  160. /// describing an option.
  161. std::string createConfigWithOption(const std::map<std::string, std::string>& params) {
  162. std::ostringstream stream;
  163. stream << "{ \"interfaces\": [ \"*\" ],"
  164. "\"rebind-timer\": 2000, "
  165. "\"renew-timer\": 1000, "
  166. "\"subnet4\": [ { "
  167. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  168. " \"subnet\": \"192.0.2.0/24\", "
  169. " \"option-data\": [ {";
  170. bool first = true;
  171. typedef std::pair<std::string, std::string> ParamPair;
  172. BOOST_FOREACH(ParamPair param, params) {
  173. if (!first) {
  174. stream << ", ";
  175. } else {
  176. // cppcheck-suppress unreadVariable
  177. first = false;
  178. }
  179. if (param.first == "name") {
  180. stream << "\"name\": \"" << param.second << "\"";
  181. } else if (param.first == "space") {
  182. stream << "\"space\": \"" << param.second << "\"";
  183. } else if (param.first == "code") {
  184. stream << "\"code\": " << param.second << "";
  185. } else if (param.first == "data") {
  186. stream << "\"data\": \"" << param.second << "\"";
  187. } else if (param.first == "csv-format") {
  188. stream << "\"csv-format\": " << param.second;
  189. }
  190. }
  191. stream <<
  192. " } ]"
  193. " } ],"
  194. "\"valid-lifetime\": 4000 }";
  195. return (stream.str());
  196. }
  197. /// @brief Returns an option from the subnet.
  198. ///
  199. /// This function returns an option from a subnet to which the
  200. /// specified subnet address belongs. The option is identified
  201. /// by its code.
  202. ///
  203. /// @param subnet_address Address which belongs to the subnet from
  204. /// which the option is to be returned.
  205. /// @param option_code Code of the option to be returned.
  206. /// @param expected_options_count Expected number of options in
  207. /// the particular subnet.
  208. ///
  209. /// @return Descriptor of the option. If the descriptor holds a
  210. /// NULL option pointer, it means that there was no such option
  211. /// in the subnet.
  212. Subnet::OptionDescriptor
  213. getOptionFromSubnet(const IOAddress& subnet_address,
  214. const uint16_t option_code,
  215. const uint16_t expected_options_count = 1) {
  216. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(subnet_address,
  217. classify_);
  218. if (!subnet) {
  219. /// @todo replace toText() with the use of operator <<.
  220. ADD_FAILURE() << "A subnet for the specified address "
  221. << subnet_address.toText()
  222. << "does not exist in Config Manager";
  223. }
  224. Subnet::OptionContainerPtr options =
  225. subnet->getOptionDescriptors("dhcp4");
  226. if (expected_options_count != options->size()) {
  227. ADD_FAILURE() << "The number of options in the subnet '"
  228. << subnet_address.toText() << "' is different "
  229. " than expected number of options '"
  230. << expected_options_count << "'";
  231. }
  232. // Get the search index. Index #1 is to search using option code.
  233. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  234. // Get the options for specified index. Expecting one option to be
  235. // returned but in theory we may have multiple options with the same
  236. // code so we get the range.
  237. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  238. Subnet::OptionContainerTypeIndex::const_iterator> range =
  239. idx.equal_range(option_code);
  240. if (std::distance(range.first, range.second) > 1) {
  241. ADD_FAILURE() << "There is more than one option having the"
  242. " option code '" << option_code << "' in a subnet '"
  243. << subnet_address.toText() << "'. Expected "
  244. " at most one option";
  245. } else if (std::distance(range.first, range.second) == 0) {
  246. return (Subnet::OptionDescriptor(OptionPtr(), false));
  247. }
  248. return (*range.first);
  249. }
  250. /// @brief Test invalid option parameter value.
  251. ///
  252. /// This test function constructs the simple configuration
  253. /// string and injects invalid option configuration into it.
  254. /// It expects that parser will fail with provided option code.
  255. ///
  256. /// @param param_value string holding invalid option parameter value
  257. /// to be injected into configuration string.
  258. /// @param parameter name of the parameter to be configured with
  259. /// param_value (can be any of "name", "code", "data")
  260. void testInvalidOptionParam(const std::string& param_value,
  261. const std::string& parameter) {
  262. ConstElementPtr x;
  263. std::string config = createConfigWithOption(param_value, parameter);
  264. ElementPtr json = Element::fromJSON(config);
  265. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  266. ASSERT_TRUE(x);
  267. comment_ = parseAnswer(rcode_, x);
  268. ASSERT_EQ(1, rcode_);
  269. }
  270. /// @brief Test invalid option paramater value.
  271. ///
  272. /// This test function constructs the simple configuration
  273. /// string and injects invalid option configuration into it.
  274. /// It expects that parser will fail with provided option code.
  275. ///
  276. /// @param params Map of parameters defining an option.
  277. void
  278. testInvalidOptionParam(const std::map<std::string, std::string>& params) {
  279. ConstElementPtr x;
  280. std::string config = createConfigWithOption(params);
  281. ElementPtr json = Element::fromJSON(config);
  282. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  283. ASSERT_TRUE(x);
  284. comment_ = parseAnswer(rcode_, x);
  285. ASSERT_EQ(1, rcode_);
  286. }
  287. /// @brief Test option against given code and data.
  288. ///
  289. /// @param option_desc option descriptor that carries the option to
  290. /// be tested.
  291. /// @param expected_code expected code of the option.
  292. /// @param expected_data expected data in the option.
  293. /// @param expected_data_len length of the reference data.
  294. /// @param extra_data if true extra data is allowed in an option
  295. /// after tested data.
  296. void testOption(const Subnet::OptionDescriptor& option_desc,
  297. uint16_t expected_code, const uint8_t* expected_data,
  298. size_t expected_data_len,
  299. bool extra_data = false) {
  300. // Check if option descriptor contains valid option pointer.
  301. ASSERT_TRUE(option_desc.option);
  302. // Verify option type.
  303. EXPECT_EQ(expected_code, option_desc.option->getType());
  304. // We may have many different option types being created. Some of them
  305. // have dedicated classes derived from Option class. In such case if
  306. // we want to verify the option contents against expected_data we have
  307. // to prepare raw buffer with the contents of the option. The easiest
  308. // way is to call pack() which will prepare on-wire data.
  309. util::OutputBuffer buf(option_desc.option->getData().size());
  310. option_desc.option->pack(buf);
  311. if (extra_data) {
  312. // The length of the buffer must be at least equal to size of the
  313. // reference data but it can sometimes be greater than that. This is
  314. // because some options carry suboptions that increase the overall
  315. // length.
  316. ASSERT_GE(buf.getLength() - option_desc.option->getHeaderLen(),
  317. expected_data_len);
  318. } else {
  319. ASSERT_EQ(buf.getLength() - option_desc.option->getHeaderLen(),
  320. expected_data_len);
  321. }
  322. // Verify that the data is correct. Do not verify suboptions and a header.
  323. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  324. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option->getHeaderLen(),
  325. expected_data_len));
  326. }
  327. /// @brief Test option configuration.
  328. ///
  329. /// This function creates a configuration for a specified option using
  330. /// a map of parameters specified as the argument. The map holds
  331. /// name/value pairs which identifies option's configuration parameters:
  332. /// - name
  333. /// - space
  334. /// - code
  335. /// - data
  336. /// - csv-format.
  337. /// This function applies a new server configuration and checks that the
  338. /// option being configured is inserted into CfgMgr. The raw contents of
  339. /// this option are compared with the binary data specified as expected
  340. /// data passed to this function.
  341. ///
  342. /// @param params Map of parameters defining an option.
  343. /// @param option_code Option code.
  344. /// @param expected_data Array containing binary data expected to be stored
  345. /// in the configured option.
  346. /// @param expected_data_len Length of the array holding reference data.
  347. void testConfiguration(const std::map<std::string, std::string>& params,
  348. const uint16_t option_code,
  349. const uint8_t* expected_data,
  350. const size_t expected_data_len) {
  351. std::string config = createConfigWithOption(params);
  352. ASSERT_TRUE(executeConfiguration(config, "parse option configuration"));
  353. // The subnet should now hold one option with the specified option code.
  354. Subnet::OptionDescriptor desc =
  355. getOptionFromSubnet(IOAddress("192.0.2.24"), option_code);
  356. ASSERT_TRUE(desc.option);
  357. testOption(desc, option_code, expected_data, expected_data_len);
  358. }
  359. /// @brief Parse and Execute configuration
  360. ///
  361. /// Parses a configuration and executes a configuration of the server.
  362. /// If the operation fails, the current test will register a failure.
  363. ///
  364. /// @param config Configuration to parse
  365. /// @param operation Operation being performed. In the case of an error,
  366. /// the error text will include the string "unable to <operation>.".
  367. ///
  368. /// @return true if the configuration succeeded, false if not. In the
  369. /// latter case, a failure will have been added to the current test.
  370. bool
  371. executeConfiguration(const std::string& config, const char* operation) {
  372. ConstElementPtr status;
  373. try {
  374. ElementPtr json = Element::fromJSON(config);
  375. status = configureDhcp4Server(*srv_, json);
  376. } catch (const std::exception& ex) {
  377. ADD_FAILURE() << "Unable to " << operation << ". "
  378. << "The following configuration was used: " << std::endl
  379. << config << std::endl
  380. << " and the following error message was returned:"
  381. << ex.what() << std::endl;
  382. return (false);
  383. }
  384. // The status object must not be NULL
  385. if (!status) {
  386. ADD_FAILURE() << "Unable to " << operation << ". "
  387. << "The configuration function returned a null pointer.";
  388. return (false);
  389. }
  390. // Store the answer if we need it.
  391. // Returned value should be 0 (configuration success)
  392. comment_ = parseAnswer(rcode_, status);
  393. if (rcode_ != 0) {
  394. string reason = "";
  395. if (comment_) {
  396. reason = string(" (") + comment_->stringValue() + string(")");
  397. }
  398. ADD_FAILURE() << "Unable to " << operation << ". "
  399. << "The configuration function returned error code "
  400. << rcode_ << reason;
  401. return (false);
  402. }
  403. return (true);
  404. }
  405. /// @brief Reset configuration database.
  406. ///
  407. /// This function resets configuration data base by
  408. /// removing all subnets and option-data. Reset must
  409. /// be performed after each test to make sure that
  410. /// contents of the database do not affect result of
  411. /// subsequent tests.
  412. void resetConfiguration() {
  413. string config = "{ \"interfaces\": [ \"*\" ],"
  414. "\"hooks-libraries\": [ ], "
  415. "\"rebind-timer\": 2000, "
  416. "\"renew-timer\": 1000, "
  417. "\"valid-lifetime\": 4000, "
  418. "\"subnet4\": [ ], "
  419. "\"dhcp-ddns\": { \"enable-updates\" : false }, "
  420. "\"option-def\": [ ], "
  421. "\"option-data\": [ ] }";
  422. static_cast<void>(executeConfiguration(config,
  423. "reset configuration database"));
  424. }
  425. boost::scoped_ptr<Dhcpv4Srv> srv_; ///< DHCP4 server under test
  426. int rcode_; ///< Return code from element parsing
  427. ConstElementPtr comment_; ///< Reason for parse fail
  428. isc::dhcp::ClientClasses classify_; ///< used in client classification
  429. };
  430. // Goal of this test is a verification if a very simple config update
  431. // with just a bumped version number. That's the simplest possible
  432. // config update.
  433. TEST_F(Dhcp4ParserTest, version) {
  434. ConstElementPtr x;
  435. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  436. Element::fromJSON("{\"version\": 0}")));
  437. // returned value must be 0 (configuration accepted)
  438. checkResult(x, 0);
  439. }
  440. /// The goal of this test is to verify that the code accepts only
  441. /// valid commands and malformed or unsupported parameters are rejected.
  442. TEST_F(Dhcp4ParserTest, bogusCommand) {
  443. ConstElementPtr x;
  444. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  445. Element::fromJSON("{\"bogus\": 5}")));
  446. // returned value must be 1 (configuration parse error)
  447. checkResult(x, 1);
  448. }
  449. /// The goal of this test is to verify if wrongly defined subnet will
  450. /// be rejected. Properly defined subnet must include at least one
  451. /// pool definition.
  452. TEST_F(Dhcp4ParserTest, emptySubnet) {
  453. ConstElementPtr status;
  454. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  455. Element::fromJSON("{ \"interfaces\": [ \"*\" ],"
  456. "\"rebind-timer\": 2000, "
  457. "\"renew-timer\": 1000, "
  458. "\"subnet4\": [ ], "
  459. "\"valid-lifetime\": 4000 }")));
  460. // returned value should be 0 (success)
  461. checkResult(status, 0);
  462. checkGlobalUint32("rebind-timer", 2000);
  463. checkGlobalUint32("renew-timer", 1000);
  464. checkGlobalUint32("valid-lifetime", 4000);
  465. }
  466. /// The goal of this test is to verify if defined subnet uses global
  467. /// parameter timer definitions.
  468. TEST_F(Dhcp4ParserTest, subnetGlobalDefaults) {
  469. ConstElementPtr status;
  470. string config = "{ \"interfaces\": [ \"*\" ],"
  471. "\"rebind-timer\": 2000, "
  472. "\"renew-timer\": 1000, "
  473. "\"subnet4\": [ { "
  474. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  475. " \"subnet\": \"192.0.2.0/24\" } ],"
  476. "\"valid-lifetime\": 4000 }";
  477. ElementPtr json = Element::fromJSON(config);
  478. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  479. // check if returned status is OK
  480. checkResult(status, 0);
  481. // Now check if the configuration was indeed handled and we have
  482. // expected pool configured.
  483. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  484. classify_);
  485. ASSERT_TRUE(subnet);
  486. EXPECT_EQ(1000, subnet->getT1());
  487. EXPECT_EQ(2000, subnet->getT2());
  488. EXPECT_EQ(4000, subnet->getValid());
  489. // Check that subnet-id is 1
  490. EXPECT_EQ(1, subnet->getID());
  491. }
  492. // Goal of this test is to verify that multiple subnets get unique
  493. // subnet-ids. Also, test checks that it's possible to do reconfiguration
  494. // multiple times.
  495. TEST_F(Dhcp4ParserTest, multipleSubnets) {
  496. ConstElementPtr x;
  497. // Collection of four subnets for which subnet ids should be
  498. // autogenerated - ids are unspecified or set to 0.
  499. string config = "{ \"interfaces\": [ \"*\" ],"
  500. "\"rebind-timer\": 2000, "
  501. "\"renew-timer\": 1000, "
  502. "\"subnet4\": [ { "
  503. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  504. " \"subnet\": \"192.0.2.0/24\" "
  505. " },"
  506. " {"
  507. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  508. " \"subnet\": \"192.0.3.0/24\", "
  509. " \"id\": 0 "
  510. " },"
  511. " {"
  512. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  513. " \"subnet\": \"192.0.4.0/24\" "
  514. " },"
  515. " {"
  516. " \"pool\": [ \"192.0.5.101 - 192.0.5.150\" ],"
  517. " \"subnet\": \"192.0.5.0/24\" "
  518. " } ],"
  519. "\"valid-lifetime\": 4000 }";
  520. ElementPtr json = Element::fromJSON(config);
  521. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  522. ASSERT_TRUE(x);
  523. comment_ = parseAnswer(rcode_, x);
  524. ASSERT_EQ(0, rcode_);
  525. int cnt = 0; // Number of reconfigurations
  526. do {
  527. ElementPtr json = Element::fromJSON(config);
  528. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  529. ASSERT_TRUE(x);
  530. comment_ = parseAnswer(rcode_, x);
  531. ASSERT_EQ(0, rcode_);
  532. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  533. ASSERT_TRUE(subnets);
  534. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  535. // Check subnet-ids of each subnet (it should be monotonously increasing)
  536. EXPECT_EQ(1, subnets->at(0)->getID());
  537. EXPECT_EQ(2, subnets->at(1)->getID());
  538. EXPECT_EQ(3, subnets->at(2)->getID());
  539. EXPECT_EQ(4, subnets->at(3)->getID());
  540. // Repeat reconfiguration process 10 times and check that the subnet-id
  541. // is set to the same value. Technically, just two iterations would be
  542. // sufficient, but it's nice to have a test that exercises reconfiguration
  543. // a bit.
  544. } while (++cnt < 10);
  545. }
  546. // This test checks that it is possible to assign arbitrary ids for subnets.
  547. TEST_F(Dhcp4ParserTest, multipleSubnetsExplicitIDs) {
  548. ConstElementPtr x;
  549. // Collection of four subnets for which subnet ids should be
  550. // autogenerated - ids are unspecified or set to 0.
  551. string config = "{ \"interfaces\": [ \"*\" ],"
  552. "\"rebind-timer\": 2000, "
  553. "\"renew-timer\": 1000, "
  554. "\"subnet4\": [ { "
  555. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  556. " \"subnet\": \"192.0.2.0/24\", "
  557. " \"id\": 1024 "
  558. " },"
  559. " {"
  560. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  561. " \"subnet\": \"192.0.3.0/24\", "
  562. " \"id\": 100 "
  563. " },"
  564. " {"
  565. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  566. " \"subnet\": \"192.0.4.0/24\", "
  567. " \"id\": 1 "
  568. " },"
  569. " {"
  570. " \"pool\": [ \"192.0.5.101 - 192.0.5.150\" ],"
  571. " \"subnet\": \"192.0.5.0/24\", "
  572. " \"id\": 34 "
  573. " } ],"
  574. "\"valid-lifetime\": 4000 }";
  575. ElementPtr json = Element::fromJSON(config);
  576. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  577. ASSERT_TRUE(x);
  578. comment_ = parseAnswer(rcode_, x);
  579. ASSERT_EQ(0, rcode_);
  580. int cnt = 0; // Number of reconfigurations
  581. do {
  582. ElementPtr json = Element::fromJSON(config);
  583. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  584. ASSERT_TRUE(x);
  585. comment_ = parseAnswer(rcode_, x);
  586. ASSERT_EQ(0, rcode_);
  587. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  588. ASSERT_TRUE(subnets);
  589. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  590. // Verify that subnet ids are as expected.
  591. EXPECT_EQ(1024, subnets->at(0)->getID());
  592. EXPECT_EQ(100, subnets->at(1)->getID());
  593. EXPECT_EQ(1, subnets->at(2)->getID());
  594. EXPECT_EQ(34, subnets->at(3)->getID());
  595. // Repeat reconfiguration process 10 times and check that the subnet-id
  596. // is set to the same value.
  597. } while (++cnt < 10);
  598. }
  599. // Check that the configuration with two subnets having the same id is rejected.
  600. TEST_F(Dhcp4ParserTest, multipleSubnetsOverlapingIDs) {
  601. ConstElementPtr x;
  602. // Collection of four subnets for which subnet ids should be
  603. // autogenerated - ids are unspecified or set to 0.
  604. string config = "{ \"interfaces\": [ \"*\" ],"
  605. "\"rebind-timer\": 2000, "
  606. "\"renew-timer\": 1000, "
  607. "\"subnet4\": [ { "
  608. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  609. " \"subnet\": \"192.0.2.0/24\", "
  610. " \"id\": 1024 "
  611. " },"
  612. " {"
  613. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  614. " \"subnet\": \"192.0.3.0/24\", "
  615. " \"id\": 100 "
  616. " },"
  617. " {"
  618. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  619. " \"subnet\": \"192.0.4.0/24\", "
  620. " \"id\": 1024 "
  621. " },"
  622. " {"
  623. " \"pool\": [ \"192.0.5.101 - 192.0.5.150\" ],"
  624. " \"subnet\": \"192.0.5.0/24\", "
  625. " \"id\": 34 "
  626. " } ],"
  627. "\"valid-lifetime\": 4000 }";
  628. ElementPtr json = Element::fromJSON(config);
  629. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  630. ASSERT_TRUE(x);
  631. comment_ = parseAnswer(rcode_, x);
  632. EXPECT_NE(rcode_, 0);
  633. }
  634. // Goal of this test is to verify that a previously configured subnet can be
  635. // deleted in subsequent reconfiguration.
  636. TEST_F(Dhcp4ParserTest, reconfigureRemoveSubnet) {
  637. ConstElementPtr x;
  638. // All four subnets
  639. string config4 = "{ \"interfaces\": [ \"*\" ],"
  640. "\"rebind-timer\": 2000, "
  641. "\"renew-timer\": 1000, "
  642. "\"subnet4\": [ { "
  643. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  644. " \"subnet\": \"192.0.2.0/24\", "
  645. " \"id\": 1 "
  646. " },"
  647. " {"
  648. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  649. " \"subnet\": \"192.0.3.0/24\", "
  650. " \"id\": 2 "
  651. " },"
  652. " {"
  653. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  654. " \"subnet\": \"192.0.4.0/24\", "
  655. " \"id\": 3 "
  656. " },"
  657. " {"
  658. " \"pool\": [ \"192.0.5.101 - 192.0.5.150\" ],"
  659. " \"subnet\": \"192.0.5.0/24\", "
  660. " \"id\": 4 "
  661. " } ],"
  662. "\"valid-lifetime\": 4000 }";
  663. // Three subnets (the last one removed)
  664. string config_first3 = "{ \"interfaces\": [ \"*\" ],"
  665. "\"rebind-timer\": 2000, "
  666. "\"renew-timer\": 1000, "
  667. "\"subnet4\": [ { "
  668. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  669. " \"subnet\": \"192.0.2.0/24\", "
  670. " \"id\": 1 "
  671. " },"
  672. " {"
  673. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  674. " \"subnet\": \"192.0.3.0/24\", "
  675. " \"id\": 2 "
  676. " },"
  677. " {"
  678. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  679. " \"subnet\": \"192.0.4.0/24\", "
  680. " \"id\": 3 "
  681. " } ],"
  682. "\"valid-lifetime\": 4000 }";
  683. // Second subnet removed
  684. string config_second_removed = "{ \"interfaces\": [ \"*\" ],"
  685. "\"rebind-timer\": 2000, "
  686. "\"renew-timer\": 1000, "
  687. "\"subnet4\": [ { "
  688. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  689. " \"subnet\": \"192.0.2.0/24\", "
  690. " \"id\": 1 "
  691. " },"
  692. " {"
  693. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  694. " \"subnet\": \"192.0.4.0/24\", "
  695. " \"id\": 3 "
  696. " },"
  697. " {"
  698. " \"pool\": [ \"192.0.5.101 - 192.0.5.150\" ],"
  699. " \"subnet\": \"192.0.5.0/24\", "
  700. " \"id\": 4 "
  701. " } ],"
  702. "\"valid-lifetime\": 4000 }";
  703. // CASE 1: Configure 4 subnets, then reconfigure and remove the
  704. // last one.
  705. ElementPtr json = Element::fromJSON(config4);
  706. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  707. ASSERT_TRUE(x);
  708. comment_ = parseAnswer(rcode_, x);
  709. ASSERT_EQ(0, rcode_);
  710. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  711. ASSERT_TRUE(subnets);
  712. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  713. // Do the reconfiguration (the last subnet is removed)
  714. json = Element::fromJSON(config_first3);
  715. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  716. ASSERT_TRUE(x);
  717. comment_ = parseAnswer(rcode_, x);
  718. ASSERT_EQ(0, rcode_);
  719. subnets = CfgMgr::instance().getSubnets4();
  720. ASSERT_TRUE(subnets);
  721. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets now (4th is removed)
  722. // Check subnet-ids of each subnet (it should be monotonously increasing)
  723. EXPECT_EQ(1, subnets->at(0)->getID());
  724. EXPECT_EQ(2, subnets->at(1)->getID());
  725. EXPECT_EQ(3, subnets->at(2)->getID());
  726. /// CASE 2: Configure 4 subnets, then reconfigure and remove one
  727. /// from in between (not first, not last)
  728. /// @todo: Uncomment subnet removal test as part of #3281.
  729. json = Element::fromJSON(config4);
  730. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  731. ASSERT_TRUE(x);
  732. comment_ = parseAnswer(rcode_, x);
  733. ASSERT_EQ(0, rcode_);
  734. // Do reconfiguration
  735. json = Element::fromJSON(config_second_removed);
  736. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  737. ASSERT_TRUE(x);
  738. comment_ = parseAnswer(rcode_, x);
  739. ASSERT_EQ(0, rcode_);
  740. subnets = CfgMgr::instance().getSubnets4();
  741. ASSERT_TRUE(subnets);
  742. ASSERT_EQ(3, subnets->size()); // We expect 4 subnets
  743. EXPECT_EQ(1, subnets->at(0)->getID());
  744. // The second subnet (with subnet-id = 2) is no longer there
  745. EXPECT_EQ(3, subnets->at(1)->getID());
  746. EXPECT_EQ(4, subnets->at(2)->getID());
  747. }
  748. /// @todo: implement subnet removal test as part of #3281.
  749. // Checks if the next-server defined as global parameter is taken into
  750. // consideration.
  751. TEST_F(Dhcp4ParserTest, nextServerGlobal) {
  752. ConstElementPtr status;
  753. string config = "{ \"interfaces\": [ \"*\" ],"
  754. "\"rebind-timer\": 2000, "
  755. "\"renew-timer\": 1000, "
  756. "\"next-server\": \"1.2.3.4\", "
  757. "\"subnet4\": [ { "
  758. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  759. " \"subnet\": \"192.0.2.0/24\" } ],"
  760. "\"valid-lifetime\": 4000 }";
  761. ElementPtr json = Element::fromJSON(config);
  762. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  763. // check if returned status is OK
  764. checkResult(status, 0);
  765. // Now check if the configuration was indeed handled and we have
  766. // expected pool configured.
  767. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  768. classify_);
  769. ASSERT_TRUE(subnet);
  770. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  771. }
  772. // Checks if the next-server defined as subnet parameter is taken into
  773. // consideration.
  774. TEST_F(Dhcp4ParserTest, nextServerSubnet) {
  775. ConstElementPtr status;
  776. string config = "{ \"interfaces\": [ \"*\" ],"
  777. "\"rebind-timer\": 2000, "
  778. "\"renew-timer\": 1000, "
  779. "\"subnet4\": [ { "
  780. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  781. " \"next-server\": \"1.2.3.4\", "
  782. " \"subnet\": \"192.0.2.0/24\" } ],"
  783. "\"valid-lifetime\": 4000 }";
  784. ElementPtr json = Element::fromJSON(config);
  785. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  786. // check if returned status is OK
  787. checkResult(status, 0);
  788. // Now check if the configuration was indeed handled and we have
  789. // expected pool configured.
  790. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  791. classify_);
  792. ASSERT_TRUE(subnet);
  793. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  794. }
  795. // Test checks several negative scenarios for next-server configuration: bogus
  796. // address, IPv6 adddress and empty string.
  797. TEST_F(Dhcp4ParserTest, nextServerNegative) {
  798. ConstElementPtr status;
  799. // Config with junk instead of next-server address
  800. string config_bogus1 = "{ \"interfaces\": [ \"*\" ],"
  801. "\"rebind-timer\": 2000, "
  802. "\"renew-timer\": 1000, "
  803. "\"subnet4\": [ { "
  804. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  805. " \"rebind-timer\": 2000, "
  806. " \"renew-timer\": 1000, "
  807. " \"next-server\": \"a.b.c.d\", "
  808. " \"subnet\": \"192.0.2.0/24\" } ],"
  809. "\"valid-lifetime\": 4000 }";
  810. // Config with IPv6 next server address
  811. string config_bogus2 = "{ \"interfaces\": [ \"*\" ],"
  812. "\"rebind-timer\": 2000, "
  813. "\"renew-timer\": 1000, "
  814. "\"subnet4\": [ { "
  815. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  816. " \"rebind-timer\": 2000, "
  817. " \"renew-timer\": 1000, "
  818. " \"next-server\": \"2001:db8::1\", "
  819. " \"subnet\": \"192.0.2.0/24\" } ],"
  820. "\"valid-lifetime\": 4000 }";
  821. // Config with empty next server address
  822. string config_bogus3 = "{ \"interfaces\": [ \"*\" ],"
  823. "\"rebind-timer\": 2000, "
  824. "\"renew-timer\": 1000, "
  825. "\"subnet4\": [ { "
  826. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  827. " \"rebind-timer\": 2000, "
  828. " \"renew-timer\": 1000, "
  829. " \"next-server\": \"\", "
  830. " \"subnet\": \"192.0.2.0/24\" } ],"
  831. "\"valid-lifetime\": 4000 }";
  832. ElementPtr json1 = Element::fromJSON(config_bogus1);
  833. ElementPtr json2 = Element::fromJSON(config_bogus2);
  834. ElementPtr json3 = Element::fromJSON(config_bogus3);
  835. // check if returned status is always a failure
  836. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json1));
  837. checkResult(status, 1);
  838. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json2));
  839. checkResult(status, 1);
  840. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json3));
  841. checkResult(status, 0);
  842. }
  843. // Checks if the next-server defined as global value is overridden by subnet
  844. // specific value.
  845. TEST_F(Dhcp4ParserTest, nextServerOverride) {
  846. ConstElementPtr status;
  847. string config = "{ \"interfaces\": [ \"*\" ],"
  848. "\"rebind-timer\": 2000, "
  849. "\"renew-timer\": 1000, "
  850. "\"next-server\": \"192.0.0.1\", "
  851. "\"subnet4\": [ { "
  852. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  853. " \"next-server\": \"1.2.3.4\", "
  854. " \"subnet\": \"192.0.2.0/24\" } ],"
  855. "\"valid-lifetime\": 4000 }";
  856. ElementPtr json = Element::fromJSON(config);
  857. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  858. // check if returned status is OK
  859. checkResult(status, 0);
  860. // Now check if the configuration was indeed handled and we have
  861. // expected pool configured.
  862. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  863. classify_);
  864. ASSERT_TRUE(subnet);
  865. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  866. }
  867. // Check whether it is possible to configure echo-client-id
  868. TEST_F(Dhcp4ParserTest, echoClientId) {
  869. ConstElementPtr status;
  870. string config_false = "{ \"interfaces\": [ \"*\" ],"
  871. "\"rebind-timer\": 2000, "
  872. "\"renew-timer\": 1000, "
  873. "\"echo-client-id\": false,"
  874. "\"subnet4\": [ { "
  875. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  876. " \"subnet\": \"192.0.2.0/24\" } ],"
  877. "\"valid-lifetime\": 4000 }";
  878. string config_true = "{ \"interfaces\": [ \"*\" ],"
  879. "\"rebind-timer\": 2000, "
  880. "\"renew-timer\": 1000, "
  881. "\"echo-client-id\": true,"
  882. "\"subnet4\": [ { "
  883. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  884. " \"subnet\": \"192.0.2.0/24\" } ],"
  885. "\"valid-lifetime\": 4000 }";
  886. ElementPtr json_false = Element::fromJSON(config_false);
  887. ElementPtr json_true = Element::fromJSON(config_true);
  888. // Let's check the default. It should be true
  889. ASSERT_TRUE(CfgMgr::instance().echoClientId());
  890. // Now check that "false" configuration is really applied.
  891. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json_false));
  892. ASSERT_FALSE(CfgMgr::instance().echoClientId());
  893. // Now check that "true" configuration is really applied.
  894. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json_true));
  895. ASSERT_TRUE(CfgMgr::instance().echoClientId());
  896. // In any case revert back to the default value (true)
  897. CfgMgr::instance().echoClientId(true);
  898. }
  899. // This test checks if it is possible to override global values
  900. // on a per subnet basis.
  901. TEST_F(Dhcp4ParserTest, subnetLocal) {
  902. ConstElementPtr status;
  903. string config = "{ \"interfaces\": [ \"*\" ],"
  904. "\"rebind-timer\": 2000, "
  905. "\"renew-timer\": 1000, "
  906. "\"subnet4\": [ { "
  907. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  908. " \"renew-timer\": 1, "
  909. " \"rebind-timer\": 2, "
  910. " \"valid-lifetime\": 4,"
  911. " \"subnet\": \"192.0.2.0/24\" } ],"
  912. "\"valid-lifetime\": 4000 }";
  913. ElementPtr json = Element::fromJSON(config);
  914. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  915. // returned value should be 0 (configuration success)
  916. checkResult(status, 0);
  917. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  918. classify_);
  919. ASSERT_TRUE(subnet);
  920. EXPECT_EQ(1, subnet->getT1());
  921. EXPECT_EQ(2, subnet->getT2());
  922. EXPECT_EQ(4, subnet->getValid());
  923. }
  924. // Test verifies that a subnet with pool values that do not belong to that
  925. // pool are rejected.
  926. TEST_F(Dhcp4ParserTest, poolOutOfSubnet) {
  927. ConstElementPtr status;
  928. string config = "{ \"interfaces\": [ \"*\" ],"
  929. "\"rebind-timer\": 2000, "
  930. "\"renew-timer\": 1000, "
  931. "\"subnet4\": [ { "
  932. " \"pool\": [ \"192.0.4.0/28\" ],"
  933. " \"subnet\": \"192.0.2.0/24\" } ],"
  934. "\"valid-lifetime\": 4000 }";
  935. ElementPtr json = Element::fromJSON(config);
  936. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  937. // returned value must be 1 (values error)
  938. // as the pool does not belong to that subnet
  939. checkResult(status, 1);
  940. }
  941. // Goal of this test is to verify if pools can be defined
  942. // using prefix/length notation. There is no separate test for min-max
  943. // notation as it was tested in several previous tests.
  944. TEST_F(Dhcp4ParserTest, poolPrefixLen) {
  945. ConstElementPtr status;
  946. string config = "{ \"interfaces\": [ \"*\" ],"
  947. "\"rebind-timer\": 2000, "
  948. "\"renew-timer\": 1000, "
  949. "\"subnet4\": [ { "
  950. " \"pool\": [ \"192.0.2.128/28\" ],"
  951. " \"subnet\": \"192.0.2.0/24\" } ],"
  952. "\"valid-lifetime\": 4000 }";
  953. ElementPtr json = Element::fromJSON(config);
  954. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  955. // returned value must be 0 (configuration accepted)
  956. checkResult(status, 0);
  957. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  958. classify_);
  959. ASSERT_TRUE(subnet);
  960. EXPECT_EQ(1000, subnet->getT1());
  961. EXPECT_EQ(2000, subnet->getT2());
  962. EXPECT_EQ(4000, subnet->getValid());
  963. }
  964. // The goal of this test is to check whether an option definition
  965. // that defines an option carrying an IPv4 address can be created.
  966. TEST_F(Dhcp4ParserTest, optionDefIpv4Address) {
  967. // Configuration string.
  968. std::string config =
  969. "{ \"option-def\": [ {"
  970. " \"name\": \"foo\","
  971. " \"code\": 100,"
  972. " \"type\": \"ipv4-address\","
  973. " \"array\": False,"
  974. " \"record-types\": \"\","
  975. " \"space\": \"isc\","
  976. " \"encapsulate\": \"\""
  977. " } ]"
  978. "}";
  979. ElementPtr json = Element::fromJSON(config);
  980. // Make sure that the particular option definition does not exist.
  981. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  982. ASSERT_FALSE(def);
  983. // Use the configuration string to create new option definition.
  984. ConstElementPtr status;
  985. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  986. ASSERT_TRUE(status);
  987. checkResult(status, 0);
  988. // The option definition should now be available in the CfgMgr.
  989. def = CfgMgr::instance().getOptionDef("isc", 100);
  990. ASSERT_TRUE(def);
  991. // Verify that the option definition data is valid.
  992. EXPECT_EQ("foo", def->getName());
  993. EXPECT_EQ(100, def->getCode());
  994. EXPECT_FALSE(def->getArrayType());
  995. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  996. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  997. }
  998. // The goal of this test is to check whether an option definition
  999. // that defines an option carrying a record of data fields can
  1000. // be created.
  1001. TEST_F(Dhcp4ParserTest, optionDefRecord) {
  1002. // Configuration string.
  1003. std::string config =
  1004. "{ \"option-def\": [ {"
  1005. " \"name\": \"foo\","
  1006. " \"code\": 100,"
  1007. " \"type\": \"record\","
  1008. " \"array\": False,"
  1009. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  1010. " \"space\": \"isc\","
  1011. " \"encapsulate\": \"\""
  1012. " } ]"
  1013. "}";
  1014. ElementPtr json = Element::fromJSON(config);
  1015. // Make sure that the particular option definition does not exist.
  1016. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  1017. ASSERT_FALSE(def);
  1018. // Use the configuration string to create new option definition.
  1019. ConstElementPtr status;
  1020. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1021. ASSERT_TRUE(status);
  1022. checkResult(status, 0);
  1023. // The option definition should now be available in the CfgMgr.
  1024. def = CfgMgr::instance().getOptionDef("isc", 100);
  1025. ASSERT_TRUE(def);
  1026. // Check the option data.
  1027. EXPECT_EQ("foo", def->getName());
  1028. EXPECT_EQ(100, def->getCode());
  1029. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  1030. EXPECT_FALSE(def->getArrayType());
  1031. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  1032. // The option comprises the record of data fields. Verify that all
  1033. // fields are present and they are of the expected types.
  1034. const OptionDefinition::RecordFieldsCollection& record_fields =
  1035. def->getRecordFields();
  1036. ASSERT_EQ(4, record_fields.size());
  1037. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  1038. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  1039. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  1040. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  1041. }
  1042. // The goal of this test is to verify that multiple option definitions
  1043. // can be created.
  1044. TEST_F(Dhcp4ParserTest, optionDefMultiple) {
  1045. // Configuration string.
  1046. std::string config =
  1047. "{ \"option-def\": [ {"
  1048. " \"name\": \"foo\","
  1049. " \"code\": 100,"
  1050. " \"type\": \"uint32\","
  1051. " \"array\": False,"
  1052. " \"record-types\": \"\","
  1053. " \"space\": \"isc\","
  1054. " \"encapsulate\": \"\""
  1055. " },"
  1056. " {"
  1057. " \"name\": \"foo-2\","
  1058. " \"code\": 101,"
  1059. " \"type\": \"ipv4-address\","
  1060. " \"array\": False,"
  1061. " \"record-types\": \"\","
  1062. " \"space\": \"isc\","
  1063. " \"encapsulate\": \"\""
  1064. " } ]"
  1065. "}";
  1066. ElementPtr json = Element::fromJSON(config);
  1067. // Make sure that the option definitions do not exist yet.
  1068. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  1069. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 101));
  1070. // Use the configuration string to create new option definitions.
  1071. ConstElementPtr status;
  1072. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1073. ASSERT_TRUE(status);
  1074. checkResult(status, 0);
  1075. // Check the first definition we have created.
  1076. OptionDefinitionPtr def1 = CfgMgr::instance().getOptionDef("isc", 100);
  1077. ASSERT_TRUE(def1);
  1078. // Check the option data.
  1079. EXPECT_EQ("foo", def1->getName());
  1080. EXPECT_EQ(100, def1->getCode());
  1081. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  1082. EXPECT_FALSE(def1->getArrayType());
  1083. EXPECT_TRUE(def1->getEncapsulatedSpace().empty());
  1084. // Check the second option definition we have created.
  1085. OptionDefinitionPtr def2 = CfgMgr::instance().getOptionDef("isc", 101);
  1086. ASSERT_TRUE(def2);
  1087. // Check the option data.
  1088. EXPECT_EQ("foo-2", def2->getName());
  1089. EXPECT_EQ(101, def2->getCode());
  1090. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  1091. EXPECT_FALSE(def2->getArrayType());
  1092. EXPECT_TRUE(def2->getEncapsulatedSpace().empty());
  1093. }
  1094. // The goal of this test is to verify that the duplicated option
  1095. // definition is not accepted.
  1096. TEST_F(Dhcp4ParserTest, optionDefDuplicate) {
  1097. // Configuration string. Both option definitions have
  1098. // the same code and belong to the same option space.
  1099. // This configuration should not be accepted.
  1100. std::string config =
  1101. "{ \"option-def\": [ {"
  1102. " \"name\": \"foo\","
  1103. " \"code\": 100,"
  1104. " \"type\": \"uint32\","
  1105. " \"array\": False,"
  1106. " \"record-types\": \"\","
  1107. " \"space\": \"isc\","
  1108. " \"encapsulate\": \"\""
  1109. " },"
  1110. " {"
  1111. " \"name\": \"foo-2\","
  1112. " \"code\": 100,"
  1113. " \"type\": \"ipv4-address\","
  1114. " \"array\": False,"
  1115. " \"record-types\": \"\","
  1116. " \"space\": \"isc\","
  1117. " \"encapsulate\": \"\""
  1118. " } ]"
  1119. "}";
  1120. ElementPtr json = Element::fromJSON(config);
  1121. // Make sure that the option definition does not exist yet.
  1122. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  1123. // Use the configuration string to create new option definitions.
  1124. ConstElementPtr status;
  1125. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1126. ASSERT_TRUE(status);
  1127. checkResult(status, 1);
  1128. }
  1129. // The goal of this test is to verify that the option definition
  1130. // comprising an array of uint32 values can be created.
  1131. TEST_F(Dhcp4ParserTest, optionDefArray) {
  1132. // Configuration string. Created option definition should
  1133. // comprise an array of uint32 values.
  1134. std::string config =
  1135. "{ \"option-def\": [ {"
  1136. " \"name\": \"foo\","
  1137. " \"code\": 100,"
  1138. " \"type\": \"uint32\","
  1139. " \"array\": True,"
  1140. " \"record-types\": \"\","
  1141. " \"space\": \"isc\","
  1142. " \"encapsulate\": \"\""
  1143. " } ]"
  1144. "}";
  1145. ElementPtr json = Element::fromJSON(config);
  1146. // Make sure that the particular option definition does not exist.
  1147. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  1148. ASSERT_FALSE(def);
  1149. // Use the configuration string to create new option definition.
  1150. ConstElementPtr status;
  1151. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1152. ASSERT_TRUE(status);
  1153. checkResult(status, 0);
  1154. // The option definition should now be available in the CfgMgr.
  1155. def = CfgMgr::instance().getOptionDef("isc", 100);
  1156. ASSERT_TRUE(def);
  1157. // Check the option data.
  1158. EXPECT_EQ("foo", def->getName());
  1159. EXPECT_EQ(100, def->getCode());
  1160. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1161. EXPECT_TRUE(def->getArrayType());
  1162. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  1163. }
  1164. // The purpose of this test to verify that encapsulated option
  1165. // space name may be specified.
  1166. TEST_F(Dhcp4ParserTest, optionDefEncapsulate) {
  1167. // Configuration string. Included the encapsulated
  1168. // option space name.
  1169. std::string config =
  1170. "{ \"option-def\": [ {"
  1171. " \"name\": \"foo\","
  1172. " \"code\": 100,"
  1173. " \"type\": \"uint32\","
  1174. " \"array\": False,"
  1175. " \"record-types\": \"\","
  1176. " \"space\": \"isc\","
  1177. " \"encapsulate\": \"sub-opts-space\""
  1178. " } ]"
  1179. "}";
  1180. ElementPtr json = Element::fromJSON(config);
  1181. // Make sure that the particular option definition does not exist.
  1182. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  1183. ASSERT_FALSE(def);
  1184. // Use the configuration string to create new option definition.
  1185. ConstElementPtr status;
  1186. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1187. ASSERT_TRUE(status);
  1188. checkResult(status, 0);
  1189. // The option definition should now be available in the CfgMgr.
  1190. def = CfgMgr::instance().getOptionDef("isc", 100);
  1191. ASSERT_TRUE(def);
  1192. // Check the option data.
  1193. EXPECT_EQ("foo", def->getName());
  1194. EXPECT_EQ(100, def->getCode());
  1195. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1196. EXPECT_FALSE(def->getArrayType());
  1197. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  1198. }
  1199. /// The purpose of this test is to verify that the option definition
  1200. /// with invalid name is not accepted.
  1201. TEST_F(Dhcp4ParserTest, optionDefInvalidName) {
  1202. // Configuration string. The option name is invalid as it
  1203. // contains the % character.
  1204. std::string config =
  1205. "{ \"option-def\": [ {"
  1206. " \"name\": \"invalid%name\","
  1207. " \"code\": 100,"
  1208. " \"type\": \"string\","
  1209. " \"array\": False,"
  1210. " \"record-types\": \"\","
  1211. " \"space\": \"isc\","
  1212. " \"encapsulate\": \"\""
  1213. " } ]"
  1214. "}";
  1215. ElementPtr json = Element::fromJSON(config);
  1216. // Use the configuration string to create new option definition.
  1217. ConstElementPtr status;
  1218. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1219. ASSERT_TRUE(status);
  1220. // Expecting parsing error (error code 1).
  1221. checkResult(status, 1);
  1222. }
  1223. /// The purpose of this test is to verify that the option definition
  1224. /// with invalid type is not accepted.
  1225. TEST_F(Dhcp4ParserTest, optionDefInvalidType) {
  1226. // Configuration string. The option type is invalid. It is
  1227. // "sting" instead of "string".
  1228. std::string config =
  1229. "{ \"option-def\": [ {"
  1230. " \"name\": \"foo\","
  1231. " \"code\": 100,"
  1232. " \"type\": \"sting\","
  1233. " \"array\": False,"
  1234. " \"record-types\": \"\","
  1235. " \"space\": \"isc\","
  1236. " \"encapsulate\": \"\""
  1237. " } ]"
  1238. "}";
  1239. ElementPtr json = Element::fromJSON(config);
  1240. // Use the configuration string to create new option definition.
  1241. ConstElementPtr status;
  1242. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1243. ASSERT_TRUE(status);
  1244. // Expecting parsing error (error code 1).
  1245. checkResult(status, 1);
  1246. }
  1247. /// The purpose of this test is to verify that the option definition
  1248. /// with invalid type is not accepted.
  1249. TEST_F(Dhcp4ParserTest, optionDefInvalidRecordType) {
  1250. // Configuration string. The third of the record fields
  1251. // is invalid. It is "sting" instead of "string".
  1252. std::string config =
  1253. "{ \"option-def\": [ {"
  1254. " \"name\": \"foo\","
  1255. " \"code\": 100,"
  1256. " \"type\": \"record\","
  1257. " \"array\": False,"
  1258. " \"record-types\": \"uint32,uint8,sting\","
  1259. " \"space\": \"isc\","
  1260. " \"encapsulate\": \"\""
  1261. " } ]"
  1262. "}";
  1263. ElementPtr json = Element::fromJSON(config);
  1264. // Use the configuration string to create new option definition.
  1265. ConstElementPtr status;
  1266. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1267. ASSERT_TRUE(status);
  1268. // Expecting parsing error (error code 1).
  1269. checkResult(status, 1);
  1270. }
  1271. /// The goal of this test is to verify that the invalid encapsulated
  1272. /// option space name is not accepted.
  1273. TEST_F(Dhcp4ParserTest, optionDefInvalidEncapsulatedSpace) {
  1274. // Configuration string. The encapsulated option space
  1275. // name is invalid (% character is not allowed).
  1276. std::string config =
  1277. "{ \"option-def\": [ {"
  1278. " \"name\": \"foo\","
  1279. " \"code\": 100,"
  1280. " \"type\": \"uint32\","
  1281. " \"array\": False,"
  1282. " \"record-types\": \"\","
  1283. " \"space\": \"isc\","
  1284. " \"encapsulate\": \"invalid%space%name\""
  1285. " } ]"
  1286. "}";
  1287. ElementPtr json = Element::fromJSON(config);
  1288. // Use the configuration string to create new option definition.
  1289. ConstElementPtr status;
  1290. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1291. ASSERT_TRUE(status);
  1292. // Expecting parsing error (error code 1).
  1293. checkResult(status, 1);
  1294. }
  1295. /// The goal of this test is to verify that the encapsulated
  1296. /// option space name can't be specified for the option that
  1297. /// comprises an array of data fields.
  1298. TEST_F(Dhcp4ParserTest, optionDefEncapsulatedSpaceAndArray) {
  1299. // Configuration string. The encapsulated option space
  1300. // name is set to non-empty value and the array flag
  1301. // is set.
  1302. std::string config =
  1303. "{ \"option-def\": [ {"
  1304. " \"name\": \"foo\","
  1305. " \"code\": 100,"
  1306. " \"type\": \"uint32\","
  1307. " \"array\": True,"
  1308. " \"record-types\": \"\","
  1309. " \"space\": \"isc\","
  1310. " \"encapsulate\": \"valid-space-name\""
  1311. " } ]"
  1312. "}";
  1313. ElementPtr json = Element::fromJSON(config);
  1314. // Use the configuration string to create new option definition.
  1315. ConstElementPtr status;
  1316. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1317. ASSERT_TRUE(status);
  1318. // Expecting parsing error (error code 1).
  1319. checkResult(status, 1);
  1320. }
  1321. /// The goal of this test is to verify that the option may not
  1322. /// encapsulate option space it belongs to.
  1323. TEST_F(Dhcp4ParserTest, optionDefEncapsulateOwnSpace) {
  1324. // Configuration string. Option is set to encapsulate
  1325. // option space it belongs to.
  1326. std::string config =
  1327. "{ \"option-def\": [ {"
  1328. " \"name\": \"foo\","
  1329. " \"code\": 100,"
  1330. " \"type\": \"uint32\","
  1331. " \"array\": False,"
  1332. " \"record-types\": \"\","
  1333. " \"space\": \"isc\","
  1334. " \"encapsulate\": \"isc\""
  1335. " } ]"
  1336. "}";
  1337. ElementPtr json = Element::fromJSON(config);
  1338. // Use the configuration string to create new option definition.
  1339. ConstElementPtr status;
  1340. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1341. ASSERT_TRUE(status);
  1342. // Expecting parsing error (error code 1).
  1343. checkResult(status, 1);
  1344. }
  1345. /// The purpose of this test is to verify that it is not allowed
  1346. /// to override the standard option (that belongs to dhcp4 option
  1347. /// space and has its definition) and that it is allowed to define
  1348. /// option in the dhcp4 option space that has a code which is not
  1349. /// used by any of the standard options.
  1350. TEST_F(Dhcp4ParserTest, optionStandardDefOverride) {
  1351. // Configuration string. The option code 109 is unassigned so it
  1352. // can be used for a custom option definition in dhcp4 option space.
  1353. std::string config =
  1354. "{ \"option-def\": [ {"
  1355. " \"name\": \"foo\","
  1356. " \"code\": 109,"
  1357. " \"type\": \"string\","
  1358. " \"array\": False,"
  1359. " \"record-types\": \"\","
  1360. " \"space\": \"dhcp4\","
  1361. " \"encapsulate\": \"\""
  1362. " } ]"
  1363. "}";
  1364. ElementPtr json = Element::fromJSON(config);
  1365. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("dhcp4", 109);
  1366. ASSERT_FALSE(def);
  1367. // Use the configuration string to create new option definition.
  1368. ConstElementPtr status;
  1369. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1370. ASSERT_TRUE(status);
  1371. checkResult(status, 0);
  1372. // The option definition should now be available in the CfgMgr.
  1373. def = CfgMgr::instance().getOptionDef("dhcp4", 109);
  1374. ASSERT_TRUE(def);
  1375. // Check the option data.
  1376. EXPECT_EQ("foo", def->getName());
  1377. EXPECT_EQ(109, def->getCode());
  1378. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1379. EXPECT_FALSE(def->getArrayType());
  1380. // The combination of option space and code is invalid. The 'dhcp4' option
  1381. // space groups standard options and the code 3 is reserved for one of
  1382. // them.
  1383. config =
  1384. "{ \"option-def\": [ {"
  1385. " \"name\": \"routers\","
  1386. " \"code\": 3,"
  1387. " \"type\": \"ipv4-address\","
  1388. " \"array\": False,"
  1389. " \"record-types\": \"\","
  1390. " \"space\": \"dhcp4\","
  1391. " \"encapsulate\": \"\""
  1392. " } ]"
  1393. "}";
  1394. json = Element::fromJSON(config);
  1395. // Use the configuration string to create new option definition.
  1396. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1397. ASSERT_TRUE(status);
  1398. // Expecting parsing error (error code 1).
  1399. checkResult(status, 1);
  1400. /// @todo The option 65 is a standard DHCPv4 option. However, at this point
  1401. /// there is no definition for this option in libdhcp++, so it should be
  1402. /// allowed to define it from the configuration interface. This test will
  1403. /// have to be removed once definitions for remaining standard options are
  1404. /// created.
  1405. config =
  1406. "{ \"option-def\": [ {"
  1407. " \"name\": \"nis-server-addr\","
  1408. " \"code\": 65,"
  1409. " \"type\": \"ipv4-address\","
  1410. " \"array\": False,"
  1411. " \"record-types\": \"\","
  1412. " \"space\": \"dhcp4\","
  1413. " \"encapsulate\": \"\""
  1414. " } ]"
  1415. "}";
  1416. json = Element::fromJSON(config);
  1417. // Use the configuration string to create new option definition.
  1418. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1419. ASSERT_TRUE(status);
  1420. // Expecting success.
  1421. checkResult(status, 0);
  1422. def = CfgMgr::instance().getOptionDef("dhcp4", 65);
  1423. ASSERT_TRUE(def);
  1424. // Check the option data.
  1425. EXPECT_EQ("nis-server-addr", def->getName());
  1426. EXPECT_EQ(65, def->getCode());
  1427. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  1428. EXPECT_FALSE(def->getArrayType());
  1429. }
  1430. // Goal of this test is to verify that global option
  1431. // data is configured for the subnet if the subnet
  1432. // configuration does not include options configuration.
  1433. TEST_F(Dhcp4ParserTest, optionDataDefaults) {
  1434. ConstElementPtr x;
  1435. string config = "{ \"interfaces\": [ \"*\" ],"
  1436. "\"rebind-timer\": 2000,"
  1437. "\"renew-timer\": 1000,"
  1438. "\"option-data\": [ {"
  1439. " \"name\": \"dhcp-message\","
  1440. " \"space\": \"dhcp4\","
  1441. " \"code\": 56,"
  1442. " \"data\": \"ABCDEF0105\","
  1443. " \"csv-format\": False"
  1444. " },"
  1445. " {"
  1446. " \"name\": \"default-ip-ttl\","
  1447. " \"space\": \"dhcp4\","
  1448. " \"code\": 23,"
  1449. " \"data\": \"01\","
  1450. " \"csv-format\": False"
  1451. " } ],"
  1452. "\"subnet4\": [ { "
  1453. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1454. " \"subnet\": \"192.0.2.0/24\""
  1455. " } ],"
  1456. "\"valid-lifetime\": 4000 }";
  1457. ElementPtr json = Element::fromJSON(config);
  1458. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1459. ASSERT_TRUE(x);
  1460. comment_ = parseAnswer(rcode_, x);
  1461. ASSERT_EQ(0, rcode_);
  1462. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  1463. classify_);
  1464. ASSERT_TRUE(subnet);
  1465. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1466. ASSERT_EQ(2, options->size());
  1467. // Get the search index. Index #1 is to search using option code.
  1468. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1469. // Get the options for specified index. Expecting one option to be
  1470. // returned but in theory we may have multiple options with the same
  1471. // code so we get the range.
  1472. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1473. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1474. idx.equal_range(56);
  1475. // Expect single option with the code equal to 56.
  1476. ASSERT_EQ(1, std::distance(range.first, range.second));
  1477. const uint8_t foo_expected[] = {
  1478. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1479. };
  1480. // Check if option is valid in terms of code and carried data.
  1481. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1482. range = idx.equal_range(23);
  1483. ASSERT_EQ(1, std::distance(range.first, range.second));
  1484. // Do another round of testing with second option.
  1485. const uint8_t foo2_expected[] = {
  1486. 0x01
  1487. };
  1488. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  1489. }
  1490. /// The goal of this test is to verify that two options having the same
  1491. /// option code can be added to different option spaces.
  1492. TEST_F(Dhcp4ParserTest, optionDataTwoSpaces) {
  1493. // This configuration string is to configure two options
  1494. // sharing the code 56 and having different definitions
  1495. // and belonging to the different option spaces.
  1496. // The option definition must be provided for the
  1497. // option that belongs to the 'isc' option space.
  1498. // The definition is not required for the option that
  1499. // belongs to the 'dhcp4' option space as it is the
  1500. // standard option.
  1501. string config = "{ \"interfaces\": [ \"*\" ],"
  1502. "\"rebind-timer\": 2000,"
  1503. "\"renew-timer\": 1000,"
  1504. "\"option-data\": [ {"
  1505. " \"name\": \"dhcp-message\","
  1506. " \"space\": \"dhcp4\","
  1507. " \"code\": 56,"
  1508. " \"data\": \"ABCDEF0105\","
  1509. " \"csv-format\": False"
  1510. " },"
  1511. " {"
  1512. " \"name\": \"foo\","
  1513. " \"space\": \"isc\","
  1514. " \"code\": 56,"
  1515. " \"data\": \"1234\","
  1516. " \"csv-format\": True"
  1517. " } ],"
  1518. "\"option-def\": [ {"
  1519. " \"name\": \"foo\","
  1520. " \"code\": 56,"
  1521. " \"type\": \"uint32\","
  1522. " \"array\": False,"
  1523. " \"record-types\": \"\","
  1524. " \"space\": \"isc\","
  1525. " \"encapsulate\": \"\""
  1526. " } ],"
  1527. "\"subnet4\": [ { "
  1528. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1529. " \"subnet\": \"192.0.2.0/24\""
  1530. " } ]"
  1531. "}";
  1532. ConstElementPtr status;
  1533. ElementPtr json = Element::fromJSON(config);
  1534. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1535. ASSERT_TRUE(status);
  1536. checkResult(status, 0);
  1537. // Options should be now available for the subnet.
  1538. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  1539. classify_);
  1540. ASSERT_TRUE(subnet);
  1541. // Try to get the option from the space dhcp4.
  1542. Subnet::OptionDescriptor desc1 = subnet->getOptionDescriptor("dhcp4", 56);
  1543. ASSERT_TRUE(desc1.option);
  1544. EXPECT_EQ(56, desc1.option->getType());
  1545. // Try to get the option from the space isc.
  1546. Subnet::OptionDescriptor desc2 = subnet->getOptionDescriptor("isc", 56);
  1547. ASSERT_TRUE(desc2.option);
  1548. EXPECT_EQ(56, desc1.option->getType());
  1549. // Try to get the non-existing option from the non-existing
  1550. // option space and expect that option is not returned.
  1551. Subnet::OptionDescriptor desc3 = subnet->getOptionDescriptor("non-existing", 56);
  1552. ASSERT_FALSE(desc3.option);
  1553. }
  1554. // The goal of this test is to verify that it is possible to
  1555. // encapsulate option space containing some options with
  1556. // another option. In this test we create base option that
  1557. // encapsulates option space 'isc' that comprises two other
  1558. // options. Also, for all options their definitions are
  1559. // created.
  1560. TEST_F(Dhcp4ParserTest, optionDataEncapsulate) {
  1561. // @todo DHCP configurations has many dependencies between
  1562. // parameters. First of all, configuration for subnet is
  1563. // inherited from the global values. Thus subnet has to be
  1564. // configured when all global values have been configured.
  1565. // Also, an option can encapsulate another option only
  1566. // if the latter has been configured. For this reason in this
  1567. // test we created two-stage configuration where first we
  1568. // created options that belong to encapsulated option space.
  1569. // In the second stage we add the base option. Also, the Subnet
  1570. // object is configured in the second stage so it is created
  1571. // at the very end (when all other parameters are configured).
  1572. // Starting stage 1. Configure sub-options and their definitions.
  1573. string config = "{ \"interfaces\": [ \"*\" ],"
  1574. "\"rebind-timer\": 2000,"
  1575. "\"renew-timer\": 1000,"
  1576. "\"option-data\": [ {"
  1577. " \"name\": \"foo\","
  1578. " \"space\": \"isc\","
  1579. " \"code\": 1,"
  1580. " \"data\": \"1234\","
  1581. " \"csv-format\": True"
  1582. " },"
  1583. " {"
  1584. " \"name\": \"foo2\","
  1585. " \"space\": \"isc\","
  1586. " \"code\": 2,"
  1587. " \"data\": \"192.168.2.1\","
  1588. " \"csv-format\": True"
  1589. " } ],"
  1590. "\"option-def\": [ {"
  1591. " \"name\": \"foo\","
  1592. " \"code\": 1,"
  1593. " \"type\": \"uint32\","
  1594. " \"array\": False,"
  1595. " \"record-types\": \"\","
  1596. " \"space\": \"isc\","
  1597. " \"encapsulate\": \"\""
  1598. " },"
  1599. " {"
  1600. " \"name\": \"foo2\","
  1601. " \"code\": 2,"
  1602. " \"type\": \"ipv4-address\","
  1603. " \"array\": False,"
  1604. " \"record-types\": \"\","
  1605. " \"space\": \"isc\","
  1606. " \"encapsulate\": \"\""
  1607. " } ]"
  1608. "}";
  1609. ConstElementPtr status;
  1610. ElementPtr json = Element::fromJSON(config);
  1611. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1612. ASSERT_TRUE(status);
  1613. checkResult(status, 0);
  1614. // Stage 2. Configure base option and a subnet. Please note that
  1615. // the configuration from the stage 2 is repeated because BIND
  1616. // configuration manager sends whole configuration for the lists
  1617. // where at least one element is being modified or added.
  1618. config = "{ \"interfaces\": [ \"*\" ],"
  1619. "\"rebind-timer\": 2000,"
  1620. "\"renew-timer\": 1000,"
  1621. "\"option-data\": [ {"
  1622. " \"name\": \"base-option\","
  1623. " \"space\": \"dhcp4\","
  1624. " \"code\": 222,"
  1625. " \"data\": \"11\","
  1626. " \"csv-format\": True"
  1627. " },"
  1628. " {"
  1629. " \"name\": \"foo\","
  1630. " \"space\": \"isc\","
  1631. " \"code\": 1,"
  1632. " \"data\": \"1234\","
  1633. " \"csv-format\": True"
  1634. " },"
  1635. " {"
  1636. " \"name\": \"foo2\","
  1637. " \"space\": \"isc\","
  1638. " \"code\": 2,"
  1639. " \"data\": \"192.168.2.1\","
  1640. " \"csv-format\": True"
  1641. " } ],"
  1642. "\"option-def\": [ {"
  1643. " \"name\": \"base-option\","
  1644. " \"code\": 222,"
  1645. " \"type\": \"uint8\","
  1646. " \"array\": False,"
  1647. " \"record-types\": \"\","
  1648. " \"space\": \"dhcp4\","
  1649. " \"encapsulate\": \"isc\""
  1650. "},"
  1651. "{"
  1652. " \"name\": \"foo\","
  1653. " \"code\": 1,"
  1654. " \"type\": \"uint32\","
  1655. " \"array\": False,"
  1656. " \"record-types\": \"\","
  1657. " \"space\": \"isc\","
  1658. " \"encapsulate\": \"\""
  1659. " },"
  1660. " {"
  1661. " \"name\": \"foo2\","
  1662. " \"code\": 2,"
  1663. " \"type\": \"ipv4-address\","
  1664. " \"array\": False,"
  1665. " \"record-types\": \"\","
  1666. " \"space\": \"isc\","
  1667. " \"encapsulate\": \"\""
  1668. " } ],"
  1669. "\"subnet4\": [ { "
  1670. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1671. " \"subnet\": \"192.0.2.0/24\""
  1672. " } ]"
  1673. "}";
  1674. json = Element::fromJSON(config);
  1675. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1676. ASSERT_TRUE(status);
  1677. checkResult(status, 0);
  1678. // Get the subnet.
  1679. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"),
  1680. classify_);
  1681. ASSERT_TRUE(subnet);
  1682. // We should have one option available.
  1683. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1684. ASSERT_TRUE(options);
  1685. ASSERT_EQ(1, options->size());
  1686. // Get the option.
  1687. Subnet::OptionDescriptor desc = subnet->getOptionDescriptor("dhcp4", 222);
  1688. EXPECT_TRUE(desc.option);
  1689. EXPECT_EQ(222, desc.option->getType());
  1690. // This opton should comprise two sub-options.
  1691. // One of them is 'foo' with code 1.
  1692. OptionPtr option_foo = desc.option->getOption(1);
  1693. ASSERT_TRUE(option_foo);
  1694. EXPECT_EQ(1, option_foo->getType());
  1695. // ...another one 'foo2' with code 2.
  1696. OptionPtr option_foo2 = desc.option->getOption(2);
  1697. ASSERT_TRUE(option_foo2);
  1698. EXPECT_EQ(2, option_foo2->getType());
  1699. }
  1700. // Goal of this test is to verify options configuration
  1701. // for a single subnet. In particular this test checks
  1702. // that local options configuration overrides global
  1703. // option setting.
  1704. TEST_F(Dhcp4ParserTest, optionDataInSingleSubnet) {
  1705. ConstElementPtr x;
  1706. string config = "{ \"interfaces\": [ \"*\" ],"
  1707. "\"rebind-timer\": 2000, "
  1708. "\"renew-timer\": 1000, "
  1709. "\"option-data\": [ {"
  1710. " \"name\": \"dhcp-message\","
  1711. " \"space\": \"dhcp4\","
  1712. " \"code\": 56,"
  1713. " \"data\": \"AB\","
  1714. " \"csv-format\": False"
  1715. " } ],"
  1716. "\"subnet4\": [ { "
  1717. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1718. " \"subnet\": \"192.0.2.0/24\", "
  1719. " \"option-data\": [ {"
  1720. " \"name\": \"dhcp-message\","
  1721. " \"space\": \"dhcp4\","
  1722. " \"code\": 56,"
  1723. " \"data\": \"ABCDEF0105\","
  1724. " \"csv-format\": False"
  1725. " },"
  1726. " {"
  1727. " \"name\": \"default-ip-ttl\","
  1728. " \"space\": \"dhcp4\","
  1729. " \"code\": 23,"
  1730. " \"data\": \"01\","
  1731. " \"csv-format\": False"
  1732. " } ]"
  1733. " } ],"
  1734. "\"valid-lifetime\": 4000 }";
  1735. ElementPtr json = Element::fromJSON(config);
  1736. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1737. ASSERT_TRUE(x);
  1738. comment_ = parseAnswer(rcode_, x);
  1739. ASSERT_EQ(0, rcode_);
  1740. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.24"),
  1741. classify_);
  1742. ASSERT_TRUE(subnet);
  1743. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1744. ASSERT_EQ(2, options->size());
  1745. // Get the search index. Index #1 is to search using option code.
  1746. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1747. // Get the options for specified index. Expecting one option to be
  1748. // returned but in theory we may have multiple options with the same
  1749. // code so we get the range.
  1750. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1751. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1752. idx.equal_range(56);
  1753. // Expect single option with the code equal to 100.
  1754. ASSERT_EQ(1, std::distance(range.first, range.second));
  1755. const uint8_t foo_expected[] = {
  1756. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1757. };
  1758. // Check if option is valid in terms of code and carried data.
  1759. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1760. range = idx.equal_range(23);
  1761. ASSERT_EQ(1, std::distance(range.first, range.second));
  1762. // Do another round of testing with second option.
  1763. const uint8_t foo2_expected[] = {
  1764. 0x01
  1765. };
  1766. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  1767. }
  1768. // The goal of this test is to check that the option carrying a boolean
  1769. // value can be configured using one of the values: "true", "false", "0"
  1770. // or "1".
  1771. TEST_F(Dhcp4ParserTest, optionDataBoolean) {
  1772. // Create configuration. Use standard option 19 (ip-forwarding).
  1773. std::map<std::string, std::string> params;
  1774. params["name"] = "ip-forwarding";
  1775. params["space"] = "dhcp4";
  1776. params["code"] = "19";
  1777. params["data"] = "true";
  1778. params["csv-format"] = "true";
  1779. std::string config = createConfigWithOption(params);
  1780. ASSERT_TRUE(executeConfiguration(config, "parse configuration with a"
  1781. " boolean value"));
  1782. // The subnet should now hold one option with the code 19.
  1783. Subnet::OptionDescriptor desc = getOptionFromSubnet(IOAddress("192.0.2.24"),
  1784. 19);
  1785. ASSERT_TRUE(desc.option);
  1786. // This option should be set to "true", represented as 0x1 in the option
  1787. // buffer.
  1788. uint8_t expected_option_data[] = {
  1789. 0x1
  1790. };
  1791. testConfiguration(params, 19, expected_option_data,
  1792. sizeof(expected_option_data));
  1793. // Configure the option with the "1" value. This should have the same
  1794. // effect as if "true" was specified.
  1795. params["data"] = "1";
  1796. testConfiguration(params, 19, expected_option_data,
  1797. sizeof(expected_option_data));
  1798. // The value of "1" with a few leading zeros should work too.
  1799. params["data"] = "00001";
  1800. testConfiguration(params, 19, expected_option_data,
  1801. sizeof(expected_option_data));
  1802. // Configure the option with the "false" value.
  1803. params["data"] = "false";
  1804. // The option buffer should now hold the value of 0.
  1805. expected_option_data[0] = 0;
  1806. testConfiguration(params, 19, expected_option_data,
  1807. sizeof(expected_option_data));
  1808. // Specifying "0" should have the same effect as "false".
  1809. params["data"] = "0";
  1810. testConfiguration(params, 19, expected_option_data,
  1811. sizeof(expected_option_data));
  1812. // The same effect should be for multiple 0 chars.
  1813. params["data"] = "00000";
  1814. testConfiguration(params, 19, expected_option_data,
  1815. sizeof(expected_option_data));
  1816. // Bogus values should not be accepted.
  1817. params["data"] = "bugus";
  1818. testInvalidOptionParam(params);
  1819. params["data"] = "2";
  1820. testInvalidOptionParam(params);
  1821. // Now let's test that it is possible to use binary format.
  1822. params["data"] = "0";
  1823. params["csv-format"] = "false";
  1824. testConfiguration(params, 19, expected_option_data,
  1825. sizeof(expected_option_data));
  1826. // The binary 1 should work as well.
  1827. params["data"] = "1";
  1828. expected_option_data[0] = 1;
  1829. testConfiguration(params, 19, expected_option_data,
  1830. sizeof(expected_option_data));
  1831. // As well as an even number of digits.
  1832. params["data"] = "01";
  1833. testConfiguration(params, 19, expected_option_data,
  1834. sizeof(expected_option_data));
  1835. }
  1836. // Goal of this test is to verify options configuration
  1837. // for multiple subnets.
  1838. TEST_F(Dhcp4ParserTest, optionDataInMultipleSubnets) {
  1839. ConstElementPtr x;
  1840. string config = "{ \"interfaces\": [ \"*\" ],"
  1841. "\"rebind-timer\": 2000, "
  1842. "\"renew-timer\": 1000, "
  1843. "\"subnet4\": [ { "
  1844. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1845. " \"subnet\": \"192.0.2.0/24\", "
  1846. " \"option-data\": [ {"
  1847. " \"name\": \"dhcp-message\","
  1848. " \"space\": \"dhcp4\","
  1849. " \"code\": 56,"
  1850. " \"data\": \"0102030405060708090A\","
  1851. " \"csv-format\": False"
  1852. " } ]"
  1853. " },"
  1854. " {"
  1855. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  1856. " \"subnet\": \"192.0.3.0/24\", "
  1857. " \"option-data\": [ {"
  1858. " \"name\": \"default-ip-ttl\","
  1859. " \"space\": \"dhcp4\","
  1860. " \"code\": 23,"
  1861. " \"data\": \"FF\","
  1862. " \"csv-format\": False"
  1863. " } ]"
  1864. " } ],"
  1865. "\"valid-lifetime\": 4000 }";
  1866. ElementPtr json = Element::fromJSON(config);
  1867. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1868. ASSERT_TRUE(x);
  1869. comment_ = parseAnswer(rcode_, x);
  1870. ASSERT_EQ(0, rcode_);
  1871. Subnet4Ptr subnet1 = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.100"),
  1872. classify_);
  1873. ASSERT_TRUE(subnet1);
  1874. Subnet::OptionContainerPtr options1 = subnet1->getOptionDescriptors("dhcp4");
  1875. ASSERT_EQ(1, options1->size());
  1876. // Get the search index. Index #1 is to search using option code.
  1877. const Subnet::OptionContainerTypeIndex& idx1 = options1->get<1>();
  1878. // Get the options for specified index. Expecting one option to be
  1879. // returned but in theory we may have multiple options with the same
  1880. // code so we get the range.
  1881. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1882. Subnet::OptionContainerTypeIndex::const_iterator> range1 =
  1883. idx1.equal_range(56);
  1884. // Expect single option with the code equal to 56.
  1885. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  1886. const uint8_t foo_expected[] = {
  1887. 0x01, 0x02, 0x03, 0x04, 0x05,
  1888. 0x06, 0x07, 0x08, 0x09, 0x0A
  1889. };
  1890. // Check if option is valid in terms of code and carried data.
  1891. testOption(*range1.first, 56, foo_expected, sizeof(foo_expected));
  1892. // Test another subnet in the same way.
  1893. Subnet4Ptr subnet2 = CfgMgr::instance().getSubnet4(IOAddress("192.0.3.102"),
  1894. classify_);
  1895. ASSERT_TRUE(subnet2);
  1896. Subnet::OptionContainerPtr options2 = subnet2->getOptionDescriptors("dhcp4");
  1897. ASSERT_EQ(1, options2->size());
  1898. const Subnet::OptionContainerTypeIndex& idx2 = options2->get<1>();
  1899. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1900. Subnet::OptionContainerTypeIndex::const_iterator> range2 =
  1901. idx2.equal_range(23);
  1902. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  1903. const uint8_t foo2_expected[] = { 0xFF };
  1904. testOption(*range2.first, 23, foo2_expected, sizeof(foo2_expected));
  1905. }
  1906. // Verify that empty option name is rejected in the configuration.
  1907. TEST_F(Dhcp4ParserTest, optionNameEmpty) {
  1908. // Empty option names not allowed.
  1909. testInvalidOptionParam("", "name");
  1910. }
  1911. // Verify that empty option name with spaces is rejected
  1912. // in the configuration.
  1913. TEST_F(Dhcp4ParserTest, optionNameSpaces) {
  1914. // Spaces in option names not allowed.
  1915. testInvalidOptionParam("option foo", "name");
  1916. }
  1917. // Verify that negative option code is rejected in the configuration.
  1918. TEST_F(Dhcp4ParserTest, optionCodeNegative) {
  1919. // Check negative option code -4. This should fail too.
  1920. testInvalidOptionParam("-4", "code");
  1921. }
  1922. // Verify that out of bounds option code is rejected in the configuration.
  1923. TEST_F(Dhcp4ParserTest, optionCodeNonUint8) {
  1924. // The valid option codes are uint16_t values so passing
  1925. // uint16_t maximum value incremented by 1 should result
  1926. // in failure.
  1927. testInvalidOptionParam("257", "code");
  1928. }
  1929. // Verify that zero option code is rejected in the configuration.
  1930. TEST_F(Dhcp4ParserTest, optionCodeZero) {
  1931. // Option code 0 is reserved and should not be accepted
  1932. // by configuration parser.
  1933. testInvalidOptionParam("0", "code");
  1934. }
  1935. // Verify that option data which contains non hexadecimal characters
  1936. // is rejected by the configuration.
  1937. TEST_F(Dhcp4ParserTest, optionDataInvalidChar) {
  1938. // Option code 0 is reserved and should not be accepted
  1939. // by configuration parser.
  1940. testInvalidOptionParam("01020R", "data");
  1941. }
  1942. // Verify that option data containing '0x' prefix is rejected
  1943. // by the configuration.
  1944. TEST_F(Dhcp4ParserTest, optionDataUnexpectedPrefix) {
  1945. // Option code 0 is reserved and should not be accepted
  1946. // by configuration parser.
  1947. testInvalidOptionParam("0x0102", "data");
  1948. }
  1949. // Verify that either lower or upper case characters are allowed
  1950. // to specify the option data.
  1951. TEST_F(Dhcp4ParserTest, optionDataLowerCase) {
  1952. ConstElementPtr x;
  1953. std::string config = createConfigWithOption("0a0b0C0D", "data");
  1954. ElementPtr json = Element::fromJSON(config);
  1955. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1956. ASSERT_TRUE(x);
  1957. comment_ = parseAnswer(rcode_, x);
  1958. ASSERT_EQ(0, rcode_);
  1959. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"),
  1960. classify_);
  1961. ASSERT_TRUE(subnet);
  1962. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1963. ASSERT_EQ(1, options->size());
  1964. // Get the search index. Index #1 is to search using option code.
  1965. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1966. // Get the options for specified index. Expecting one option to be
  1967. // returned but in theory we may have multiple options with the same
  1968. // code so we get the range.
  1969. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1970. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1971. idx.equal_range(56);
  1972. // Expect single option with the code equal to 100.
  1973. ASSERT_EQ(1, std::distance(range.first, range.second));
  1974. const uint8_t foo_expected[] = {
  1975. 0x0A, 0x0B, 0x0C, 0x0D
  1976. };
  1977. // Check if option is valid in terms of code and carried data.
  1978. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1979. }
  1980. // Verify that specific option object is returned for standard
  1981. // option which has dedicated option class derived from Option.
  1982. TEST_F(Dhcp4ParserTest, stdOptionData) {
  1983. ConstElementPtr x;
  1984. std::map<std::string, std::string> params;
  1985. params["name"] = "nis-servers";
  1986. params["space"] = "dhcp4";
  1987. // Option code 41 means nis-servers.
  1988. params["code"] = "41";
  1989. // Specify option values in a CSV (user friendly) format.
  1990. params["data"] = "192.0.2.10, 192.0.2.1, 192.0.2.3";
  1991. params["csv-format"] = "True";
  1992. std::string config = createConfigWithOption(params);
  1993. ElementPtr json = Element::fromJSON(config);
  1994. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1995. ASSERT_TRUE(x);
  1996. comment_ = parseAnswer(rcode_, x);
  1997. ASSERT_EQ(0, rcode_);
  1998. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"),
  1999. classify_);
  2000. ASSERT_TRUE(subnet);
  2001. Subnet::OptionContainerPtr options =
  2002. subnet->getOptionDescriptors("dhcp4");
  2003. ASSERT_TRUE(options);
  2004. ASSERT_EQ(1, options->size());
  2005. // Get the search index. Index #1 is to search using option code.
  2006. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  2007. // Get the options for specified index. Expecting one option to be
  2008. // returned but in theory we may have multiple options with the same
  2009. // code so we get the range.
  2010. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  2011. Subnet::OptionContainerTypeIndex::const_iterator> range =
  2012. idx.equal_range(DHO_NIS_SERVERS);
  2013. // Expect single option with the code equal to NIS_SERVERS option code.
  2014. ASSERT_EQ(1, std::distance(range.first, range.second));
  2015. // The actual pointer to the option is held in the option field
  2016. // in the structure returned.
  2017. OptionPtr option = range.first->option;
  2018. ASSERT_TRUE(option);
  2019. // Option object returned for here is expected to be Option6IA
  2020. // which is derived from Option. This class is dedicated to
  2021. // represent standard option IA_NA.
  2022. boost::shared_ptr<Option4AddrLst> option_addrs =
  2023. boost::dynamic_pointer_cast<Option4AddrLst>(option);
  2024. // If cast is unsuccessful than option returned was of a
  2025. // different type than Option6IA. This is wrong.
  2026. ASSERT_TRUE(option_addrs);
  2027. // Get addresses from the option.
  2028. Option4AddrLst::AddressContainer addrs = option_addrs->getAddresses();
  2029. // Verify that the addresses have been configured correctly.
  2030. ASSERT_EQ(3, addrs.size());
  2031. EXPECT_EQ("192.0.2.10", addrs[0].toText());
  2032. EXPECT_EQ("192.0.2.1", addrs[1].toText());
  2033. EXPECT_EQ("192.0.2.3", addrs[2].toText());
  2034. }
  2035. /// This test checks if Uint32Parser can really parse the whole range
  2036. /// and properly err of out of range values. As we can't call Uint32Parser
  2037. /// directly, we are exploiting the fact that it is used to parse global
  2038. /// parameter renew-timer and the results are stored in uint32_defaults.
  2039. /// We get the uint32_defaults using a getUint32Defaults functions which
  2040. /// is defined only to access the values from this test.
  2041. TEST_F(Dhcp4ParserTest, DISABLED_Uint32Parser) {
  2042. ConstElementPtr status;
  2043. // CASE 1: 0 - minimum value, should work
  2044. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2045. Element::fromJSON("{\"version\": 0,"
  2046. "\"renew-timer\": 0}")));
  2047. // returned value must be ok (0 is a proper value)
  2048. checkResult(status, 0);
  2049. checkGlobalUint32("renew-timer", 0);
  2050. // CASE 2: 4294967295U (UINT_MAX) should work as well
  2051. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2052. Element::fromJSON("{\"version\": 0,"
  2053. "\"renew-timer\": 4294967295}")));
  2054. // returned value must be ok (0 is a proper value)
  2055. checkResult(status, 0);
  2056. checkGlobalUint32("renew-timer", 4294967295U);
  2057. // CASE 3: 4294967296U (UINT_MAX + 1) should not work
  2058. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2059. Element::fromJSON("{\"version\": 0,"
  2060. "\"renew-timer\": 4294967296}")));
  2061. // returned value must be rejected (1 configuration error)
  2062. checkResult(status, 1);
  2063. // CASE 4: -1 (UINT_MIN -1 ) should not work
  2064. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  2065. Element::fromJSON("{\"version\": 0,"
  2066. "\"renew-timer\": -1}")));
  2067. // returned value must be rejected (1 configuration error)
  2068. checkResult(status, 1);
  2069. }
  2070. // The goal of this test is to verify that the standard option can
  2071. // be configured to encapsulate multiple other options.
  2072. TEST_F(Dhcp4ParserTest, stdOptionDataEncapsulate) {
  2073. // The configuration is two stage process in this test.
  2074. // In the first stahe we create definitions of suboptions
  2075. // that we will add to the base option.
  2076. // Let's create some dummy options: foo and foo2.
  2077. string config = "{ \"interfaces\": [ \"*\" ],"
  2078. "\"rebind-timer\": 2000,"
  2079. "\"renew-timer\": 1000,"
  2080. "\"option-data\": [ {"
  2081. " \"name\": \"foo\","
  2082. " \"space\": \"vendor-encapsulated-options-space\","
  2083. " \"code\": 1,"
  2084. " \"data\": \"1234\","
  2085. " \"csv-format\": True"
  2086. " },"
  2087. " {"
  2088. " \"name\": \"foo2\","
  2089. " \"space\": \"vendor-encapsulated-options-space\","
  2090. " \"code\": 2,"
  2091. " \"data\": \"192.168.2.1\","
  2092. " \"csv-format\": True"
  2093. " } ],"
  2094. "\"option-def\": [ {"
  2095. " \"name\": \"foo\","
  2096. " \"code\": 1,"
  2097. " \"type\": \"uint32\","
  2098. " \"array\": False,"
  2099. " \"record-types\": \"\","
  2100. " \"space\": \"vendor-encapsulated-options-space\","
  2101. " \"encapsulate\": \"\""
  2102. " },"
  2103. " {"
  2104. " \"name\": \"foo2\","
  2105. " \"code\": 2,"
  2106. " \"type\": \"ipv4-address\","
  2107. " \"array\": False,"
  2108. " \"record-types\": \"\","
  2109. " \"space\": \"vendor-encapsulated-options-space\","
  2110. " \"encapsulate\": \"\""
  2111. " } ]"
  2112. "}";
  2113. ConstElementPtr status;
  2114. ElementPtr json = Element::fromJSON(config);
  2115. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2116. ASSERT_TRUE(status);
  2117. checkResult(status, 0);
  2118. // Once the definitions have been added we can configure the
  2119. // standard option #17. This option comprises an enterprise
  2120. // number and sub options. By convention (introduced in
  2121. // std_option_defs.h) option named 'vendor-opts'
  2122. // encapsulates the option space named 'vendor-opts-space'.
  2123. // We add our dummy options to this option space and thus
  2124. // they should be included as sub-options in the 'vendor-opts'
  2125. // option.
  2126. config = "{ \"interfaces\": [ \"*\" ],"
  2127. "\"rebind-timer\": 2000,"
  2128. "\"renew-timer\": 1000,"
  2129. "\"option-data\": [ {"
  2130. " \"name\": \"vendor-encapsulated-options\","
  2131. " \"space\": \"dhcp4\","
  2132. " \"code\": 43,"
  2133. " \"data\": \"\","
  2134. " \"csv-format\": False"
  2135. " },"
  2136. " {"
  2137. " \"name\": \"foo\","
  2138. " \"space\": \"vendor-encapsulated-options-space\","
  2139. " \"code\": 1,"
  2140. " \"data\": \"1234\","
  2141. " \"csv-format\": True"
  2142. " },"
  2143. " {"
  2144. " \"name\": \"foo2\","
  2145. " \"space\": \"vendor-encapsulated-options-space\","
  2146. " \"code\": 2,"
  2147. " \"data\": \"192.168.2.1\","
  2148. " \"csv-format\": True"
  2149. " } ],"
  2150. "\"option-def\": [ {"
  2151. " \"name\": \"foo\","
  2152. " \"code\": 1,"
  2153. " \"type\": \"uint32\","
  2154. " \"array\": False,"
  2155. " \"record-types\": \"\","
  2156. " \"space\": \"vendor-encapsulated-options-space\","
  2157. " \"encapsulate\": \"\""
  2158. " },"
  2159. " {"
  2160. " \"name\": \"foo2\","
  2161. " \"code\": 2,"
  2162. " \"type\": \"ipv4-address\","
  2163. " \"array\": False,"
  2164. " \"record-types\": \"\","
  2165. " \"space\": \"vendor-encapsulated-options-space\","
  2166. " \"encapsulate\": \"\""
  2167. " } ],"
  2168. "\"subnet4\": [ { "
  2169. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2170. " \"subnet\": \"192.0.2.0/24\""
  2171. " } ]"
  2172. "}";
  2173. json = Element::fromJSON(config);
  2174. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2175. ASSERT_TRUE(status);
  2176. checkResult(status, 0);
  2177. // Get the subnet.
  2178. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"),
  2179. classify_);
  2180. ASSERT_TRUE(subnet);
  2181. // We should have one option available.
  2182. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  2183. ASSERT_TRUE(options);
  2184. ASSERT_EQ(1, options->size());
  2185. // Get the option.
  2186. Subnet::OptionDescriptor desc =
  2187. subnet->getOptionDescriptor("dhcp4", DHO_VENDOR_ENCAPSULATED_OPTIONS);
  2188. EXPECT_TRUE(desc.option);
  2189. EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, desc.option->getType());
  2190. // Option with the code 1 should be added as a sub-option.
  2191. OptionPtr option_foo = desc.option->getOption(1);
  2192. ASSERT_TRUE(option_foo);
  2193. EXPECT_EQ(1, option_foo->getType());
  2194. // This option comprises a single uint32_t value thus it is
  2195. // represented by OptionInt<uint32_t> class. Let's get the
  2196. // object of this type.
  2197. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  2198. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  2199. ASSERT_TRUE(option_foo_uint32);
  2200. // Validate the value according to the configuration.
  2201. EXPECT_EQ(1234, option_foo_uint32->getValue());
  2202. // Option with the code 2 should be added as a sub-option.
  2203. OptionPtr option_foo2 = desc.option->getOption(2);
  2204. ASSERT_TRUE(option_foo2);
  2205. EXPECT_EQ(2, option_foo2->getType());
  2206. // This option comprises the IPV4 address. Such option is
  2207. // represented by OptionCustom object.
  2208. OptionCustomPtr option_foo2_v4 =
  2209. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  2210. ASSERT_TRUE(option_foo2_v4);
  2211. // Get the IP address carried by this option and validate it.
  2212. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  2213. // Option with the code 3 should not be added.
  2214. EXPECT_FALSE(desc.option->getOption(3));
  2215. }
  2216. // This test checks if vendor options can be specified in the config file
  2217. // (in hex format), and later retrieved from configured subnet
  2218. TEST_F(Dhcp4ParserTest, vendorOptionsHex) {
  2219. // This configuration string is to configure two options
  2220. // sharing the code 1 and belonging to the different vendor spaces.
  2221. // (different vendor-id values).
  2222. string config = "{ \"interfaces\": [ \"*\" ],"
  2223. "\"rebind-timer\": 2000,"
  2224. "\"renew-timer\": 1000,"
  2225. "\"option-data\": [ {"
  2226. " \"name\": \"option-one\","
  2227. " \"space\": \"vendor-4491\"," // VENDOR_ID_CABLE_LABS = 4491
  2228. " \"code\": 100," // just a random code
  2229. " \"data\": \"ABCDEF0105\","
  2230. " \"csv-format\": False"
  2231. " },"
  2232. " {"
  2233. " \"name\": \"option-two\","
  2234. " \"space\": \"vendor-1234\","
  2235. " \"code\": 100,"
  2236. " \"data\": \"1234\","
  2237. " \"csv-format\": False"
  2238. " } ],"
  2239. "\"subnet4\": [ { "
  2240. " \"pool\": [ \"192.0.2.1-192.0.2.10\" ],"
  2241. " \"subnet\": \"192.0.2.0/24\""
  2242. " } ]"
  2243. "}";
  2244. ConstElementPtr status;
  2245. ElementPtr json = Element::fromJSON(config);
  2246. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2247. ASSERT_TRUE(status);
  2248. checkResult(status, 0);
  2249. // Options should be now available for the subnet.
  2250. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"),
  2251. classify_);
  2252. ASSERT_TRUE(subnet);
  2253. // Try to get the option from the vendor space 4491
  2254. Subnet::OptionDescriptor desc1 = subnet->getVendorOptionDescriptor(VENDOR_ID_CABLE_LABS, 100);
  2255. ASSERT_TRUE(desc1.option);
  2256. EXPECT_EQ(100, desc1.option->getType());
  2257. // Try to get the option from the vendor space 1234
  2258. Subnet::OptionDescriptor desc2 = subnet->getVendorOptionDescriptor(1234, 100);
  2259. ASSERT_TRUE(desc2.option);
  2260. EXPECT_EQ(100, desc1.option->getType());
  2261. // Try to get the non-existing option from the non-existing
  2262. // option space and expect that option is not returned.
  2263. Subnet::OptionDescriptor desc3 = subnet->getVendorOptionDescriptor(5678, 100);
  2264. ASSERT_FALSE(desc3.option);
  2265. }
  2266. // This test checks if vendor options can be specified in the config file,
  2267. // (in csv format), and later retrieved from configured subnet
  2268. TEST_F(Dhcp4ParserTest, vendorOptionsCsv) {
  2269. // This configuration string is to configure two options
  2270. // sharing the code 1 and belonging to the different vendor spaces.
  2271. // (different vendor-id values).
  2272. string config = "{ \"interfaces\": [ \"*\" ],"
  2273. "\"rebind-timer\": 2000,"
  2274. "\"renew-timer\": 1000,"
  2275. "\"option-data\": [ {"
  2276. " \"name\": \"foo\","
  2277. " \"space\": \"vendor-4491\","
  2278. " \"code\": 100,"
  2279. " \"data\": \"this is a string vendor-opt\","
  2280. " \"csv-format\": True"
  2281. " } ],"
  2282. "\"option-def\": [ {"
  2283. " \"name\": \"foo\","
  2284. " \"code\": 100,"
  2285. " \"type\": \"string\","
  2286. " \"array\": False,"
  2287. " \"record-types\": \"\","
  2288. " \"space\": \"vendor-4491\","
  2289. " \"encapsulate\": \"\""
  2290. " } ],"
  2291. "\"subnet4\": [ { "
  2292. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2293. " \"subnet\": \"192.0.2.0/24\" "
  2294. " } ]"
  2295. "}";
  2296. ConstElementPtr status;
  2297. ElementPtr json = Element::fromJSON(config);
  2298. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2299. ASSERT_TRUE(status);
  2300. checkResult(status, 0);
  2301. // Options should be now available for the subnet.
  2302. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"),
  2303. classify_);
  2304. ASSERT_TRUE(subnet);
  2305. // Try to get the option from the vendor space 4491
  2306. Subnet::OptionDescriptor desc1 = subnet->getVendorOptionDescriptor(VENDOR_ID_CABLE_LABS, 100);
  2307. ASSERT_TRUE(desc1.option);
  2308. EXPECT_EQ(100, desc1.option->getType());
  2309. // Try to get the non-existing option from the non-existing
  2310. // option space and expect that option is not returned.
  2311. Subnet::OptionDescriptor desc2 = subnet->getVendorOptionDescriptor(5678, 100);
  2312. ASSERT_FALSE(desc2.option);
  2313. }
  2314. // Tests of the hooks libraries configuration. All tests have the pre-
  2315. // condition (checked in the test fixture's SetUp() method) that no hooks
  2316. // libraries are loaded at the start of the tests.
  2317. // Helper function to return a configuration containing an arbitrary number
  2318. // of hooks libraries.
  2319. std::string
  2320. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  2321. const string quote("\"");
  2322. // Create the first part of the configuration string.
  2323. string config =
  2324. "{ \"interfaces\": [ \"*\" ],"
  2325. "\"hooks-libraries\": [";
  2326. // Append the libraries (separated by commas if needed)
  2327. for (int i = 0; i < libraries.size(); ++i) {
  2328. if (i > 0) {
  2329. config += string(", ");
  2330. }
  2331. config += (quote + libraries[i] + quote);
  2332. }
  2333. // Append the remainder of the configuration.
  2334. config += string(
  2335. "],"
  2336. "\"rebind-timer\": 2000,"
  2337. "\"renew-timer\": 1000,"
  2338. "\"option-data\": [ {"
  2339. " \"name\": \"dhcp-message\","
  2340. " \"space\": \"dhcp4\","
  2341. " \"code\": 56,"
  2342. " \"data\": \"ABCDEF0105\","
  2343. " \"csv-format\": False"
  2344. " },"
  2345. " {"
  2346. " \"name\": \"foo\","
  2347. " \"space\": \"isc\","
  2348. " \"code\": 56,"
  2349. " \"data\": \"1234\","
  2350. " \"csv-format\": True"
  2351. " } ],"
  2352. "\"option-def\": [ {"
  2353. " \"name\": \"foo\","
  2354. " \"code\": 56,"
  2355. " \"type\": \"uint32\","
  2356. " \"array\": False,"
  2357. " \"record-types\": \"\","
  2358. " \"space\": \"isc\","
  2359. " \"encapsulate\": \"\""
  2360. " } ],"
  2361. "\"subnet4\": [ { "
  2362. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2363. " \"subnet\": \"192.0.2.0/24\""
  2364. " } ]"
  2365. "}");
  2366. return (config);
  2367. }
  2368. // Convenience function for creating hooks library configuration with one or
  2369. // two character string constants.
  2370. std::string
  2371. buildHooksLibrariesConfig(const char* library1 = NULL,
  2372. const char* library2 = NULL) {
  2373. std::vector<std::string> libraries;
  2374. if (library1 != NULL) {
  2375. libraries.push_back(string(library1));
  2376. if (library2 != NULL) {
  2377. libraries.push_back(string(library2));
  2378. }
  2379. }
  2380. return (buildHooksLibrariesConfig(libraries));
  2381. }
  2382. // The goal of this test is to verify the configuration of hooks libraries if
  2383. // none are specified.
  2384. TEST_F(Dhcp4ParserTest, NoHooksLibraries) {
  2385. // Parse a configuration containing no names.
  2386. string config = buildHooksLibrariesConfig();
  2387. if (!executeConfiguration(config,
  2388. "set configuration with no hooks libraries")) {
  2389. FAIL() << "Unable to execute configuration";
  2390. } else {
  2391. // No libraries should be loaded at the end of the test.
  2392. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2393. EXPECT_TRUE(libraries.empty());
  2394. }
  2395. }
  2396. // Verify parsing fails with one library that will fail validation.
  2397. TEST_F(Dhcp4ParserTest, InvalidLibrary) {
  2398. // Parse a configuration containing a failing library.
  2399. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  2400. ConstElementPtr status;
  2401. ElementPtr json = Element::fromJSON(config);
  2402. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2403. // The status object must not be NULL
  2404. ASSERT_TRUE(status);
  2405. // Returned value should not be 0
  2406. comment_ = parseAnswer(rcode_, status);
  2407. EXPECT_NE(0, rcode_);
  2408. }
  2409. // Verify the configuration of hooks libraries with two being specified.
  2410. TEST_F(Dhcp4ParserTest, LibrariesSpecified) {
  2411. // Marker files should not be present.
  2412. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  2413. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2414. // Set up the configuration with two libraries and load them.
  2415. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  2416. CALLOUT_LIBRARY_2);
  2417. ASSERT_TRUE(executeConfiguration(config,
  2418. "load two valid libraries"));
  2419. // Expect two libraries to be loaded in the correct order (load marker file
  2420. // is present, no unload marker file).
  2421. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2422. ASSERT_EQ(2, libraries.size());
  2423. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2424. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2425. // Unload the libraries. The load file should not have changed, but
  2426. // the unload one should indicate the unload() functions have been run.
  2427. config = buildHooksLibrariesConfig();
  2428. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  2429. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2430. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  2431. // Expect the hooks system to say that none are loaded.
  2432. libraries = HooksManager::getLibraryNames();
  2433. EXPECT_TRUE(libraries.empty());
  2434. }
  2435. // This test verifies that it is possible to select subset of interfaces
  2436. // on which server should listen.
  2437. TEST_F(Dhcp4ParserTest, selectedInterfaces) {
  2438. ConstElementPtr x;
  2439. string config = "{ \"interfaces\": [ \"eth0\", \"eth1\" ],"
  2440. "\"rebind-timer\": 2000, "
  2441. "\"renew-timer\": 1000, "
  2442. "\"valid-lifetime\": 4000 }";
  2443. ElementPtr json = Element::fromJSON(config);
  2444. ConstElementPtr status;
  2445. // Make sure the config manager is clean and there is no hanging
  2446. // interface configuration.
  2447. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth0"));
  2448. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth1"));
  2449. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth2"));
  2450. // Apply configuration.
  2451. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2452. ASSERT_TRUE(status);
  2453. checkResult(status, 0);
  2454. // eth0 and eth1 were explicitly selected. eth2 was not.
  2455. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth0"));
  2456. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth1"));
  2457. EXPECT_FALSE(CfgMgr::instance().isActiveIface("eth2"));
  2458. }
  2459. // This test verifies that it is possible to configure the server in such a way
  2460. // that it listens on all interfaces.
  2461. TEST_F(Dhcp4ParserTest, allInterfaces) {
  2462. ConstElementPtr x;
  2463. // This configuration specifies two interfaces on which server should listen
  2464. // but it also includes asterisk. The asterisk switches server into the
  2465. // mode when it listens on all interfaces regardless of what interface names
  2466. // were specified in the "interfaces" parameter.
  2467. string config = "{ \"interfaces\": [ \"eth0\", \"*\", \"eth1\" ],"
  2468. "\"rebind-timer\": 2000, "
  2469. "\"renew-timer\": 1000, "
  2470. "\"valid-lifetime\": 4000 }";
  2471. ElementPtr json = Element::fromJSON(config);
  2472. ConstElementPtr status;
  2473. // Make sure there is no old configuration.
  2474. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth0"));
  2475. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth1"));
  2476. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth2"));
  2477. // Apply configuration.
  2478. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2479. ASSERT_TRUE(status);
  2480. checkResult(status, 0);
  2481. // All interfaces should be now active.
  2482. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth0"));
  2483. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth1"));
  2484. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth2"));
  2485. }
  2486. // This test checks the ability of the server to parse a configuration
  2487. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  2488. TEST_F(Dhcp4ParserTest, d2ClientConfig) {
  2489. ConstElementPtr status;
  2490. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2491. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2492. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2493. // Verify that the convenience method agrees.
  2494. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2495. string config_str = "{ \"interfaces\": [ \"*\" ],"
  2496. "\"rebind-timer\": 2000, "
  2497. "\"renew-timer\": 1000, "
  2498. "\"subnet4\": [ { "
  2499. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2500. " \"subnet\": \"192.0.2.0/24\" } ],"
  2501. " \"dhcp-ddns\" : {"
  2502. " \"enable-updates\" : true, "
  2503. " \"server-ip\" : \"192.168.2.1\", "
  2504. " \"server-port\" : 777, "
  2505. " \"ncr-protocol\" : \"UDP\", "
  2506. " \"ncr-format\" : \"JSON\", "
  2507. " \"always-include-fqdn\" : true, "
  2508. " \"allow-client-update\" : true, "
  2509. " \"override-no-update\" : true, "
  2510. " \"override-client-update\" : true, "
  2511. " \"replace-client-name\" : true, "
  2512. " \"generated-prefix\" : \"test.prefix\", "
  2513. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2514. "\"valid-lifetime\": 4000 }";
  2515. // Convert the JSON string to configuration elements.
  2516. ElementPtr config;
  2517. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2518. // Pass the configuration in for parsing.
  2519. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, config));
  2520. // check if returned status is OK
  2521. checkResult(status, 0);
  2522. // Verify that DHCP-DDNS updating is enabled.
  2523. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  2524. // Verify that the D2 configuration can be retrieved.
  2525. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2526. ASSERT_TRUE(d2_client_config);
  2527. // Verify that the configuration values are correct.
  2528. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  2529. EXPECT_EQ("192.168.2.1", d2_client_config->getServerIp().toText());
  2530. EXPECT_EQ(777, d2_client_config->getServerPort());
  2531. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  2532. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  2533. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  2534. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  2535. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  2536. EXPECT_TRUE(d2_client_config->getReplaceClientName());
  2537. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  2538. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  2539. }
  2540. // This test checks the ability of the server to handle a configuration
  2541. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  2542. TEST_F(Dhcp4ParserTest, invalidD2ClientConfig) {
  2543. ConstElementPtr status;
  2544. // Configuration string with an invalid D2 client config,
  2545. // "server-ip" is invalid.
  2546. string config_str = "{ \"interfaces\": [ \"*\" ],"
  2547. "\"rebind-timer\": 2000, "
  2548. "\"renew-timer\": 1000, "
  2549. "\"subnet4\": [ { "
  2550. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2551. " \"subnet\": \"192.0.2.0/24\" } ],"
  2552. " \"dhcp-ddns\" : {"
  2553. " \"enable-updates\" : true, "
  2554. " \"server-ip\" : \"bogus-value\", "
  2555. " \"server-port\" : 5301, "
  2556. " \"ncr-protocol\" : \"UDP\", "
  2557. " \"ncr-format\" : \"JSON\", "
  2558. " \"always-include-fqdn\" : true, "
  2559. " \"allow-client-update\" : true, "
  2560. " \"override-no-update\" : true, "
  2561. " \"override-client-update\" : true, "
  2562. " \"replace-client-name\" : true, "
  2563. " \"generated-prefix\" : \"test.prefix\", "
  2564. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2565. "\"valid-lifetime\": 4000 }";
  2566. // Convert the JSON string to configuration elements.
  2567. ElementPtr config;
  2568. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2569. // Configuration should not throw, but should fail.
  2570. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, config));
  2571. // check if returned status is failed.
  2572. checkResult(status, 1);
  2573. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2574. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2575. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2576. // Verify that the convenience method agrees.
  2577. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2578. }
  2579. // This test checks if it is possible to specify relay information
  2580. TEST_F(Dhcp4ParserTest, subnetRelayInfo) {
  2581. ConstElementPtr status;
  2582. // A config with relay information.
  2583. string config = "{ \"interfaces\": [ \"*\" ],"
  2584. "\"rebind-timer\": 2000, "
  2585. "\"renew-timer\": 1000, "
  2586. "\"subnet4\": [ { "
  2587. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2588. " \"renew-timer\": 1, "
  2589. " \"rebind-timer\": 2, "
  2590. " \"valid-lifetime\": 4,"
  2591. " \"relay\": { "
  2592. " \"ip-address\": \"192.0.2.123\""
  2593. " },"
  2594. " \"subnet\": \"192.0.2.0/24\" } ],"
  2595. "\"valid-lifetime\": 4000 }";
  2596. ElementPtr json = Element::fromJSON(config);
  2597. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2598. // returned value should be 0 (configuration success)
  2599. checkResult(status, 0);
  2600. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"),
  2601. classify_);
  2602. ASSERT_TRUE(subnet);
  2603. EXPECT_EQ("192.0.2.123", subnet->getRelayInfo().addr_.toText());
  2604. }
  2605. // Goal of this test is to verify that multiple subnets can be configured
  2606. // with defined client classes.
  2607. TEST_F(Dhcp4ParserTest, classifySubnets) {
  2608. ConstElementPtr x;
  2609. string config = "{ \"interfaces\": [ \"*\" ],"
  2610. "\"rebind-timer\": 2000, "
  2611. "\"renew-timer\": 1000, "
  2612. "\"subnet4\": [ { "
  2613. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2614. " \"subnet\": \"192.0.2.0/24\", "
  2615. " \"client-class\": \"alpha\" "
  2616. " },"
  2617. " {"
  2618. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  2619. " \"subnet\": \"192.0.3.0/24\", "
  2620. " \"client-class\": \"beta\" "
  2621. " },"
  2622. " {"
  2623. " \"pool\": [ \"192.0.4.101 - 192.0.4.150\" ],"
  2624. " \"subnet\": \"192.0.4.0/24\", "
  2625. " \"client-class\": \"gamma\" "
  2626. " },"
  2627. " {"
  2628. " \"pool\": [ \"192.0.5.101 - 192.0.5.150\" ],"
  2629. " \"subnet\": \"192.0.5.0/24\" "
  2630. " } ],"
  2631. "\"valid-lifetime\": 4000 }";
  2632. ElementPtr json = Element::fromJSON(config);
  2633. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  2634. ASSERT_TRUE(x);
  2635. comment_ = parseAnswer(rcode_, x);
  2636. ASSERT_EQ(0, rcode_);
  2637. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  2638. ASSERT_TRUE(subnets);
  2639. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  2640. // Let's check if client belonging to alpha class is supported in subnet[0]
  2641. // and not supported in any other subnet (except subnet[3], which allows
  2642. // everyone).
  2643. ClientClasses classes;
  2644. classes.insert("alpha");
  2645. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  2646. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2647. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2648. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2649. // Let's check if client belonging to beta class is supported in subnet[1]
  2650. // and not supported in any other subnet (except subnet[3], which allows
  2651. // everyone).
  2652. classes.clear();
  2653. classes.insert("beta");
  2654. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2655. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  2656. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2657. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2658. // Let's check if client belonging to gamma class is supported in subnet[2]
  2659. // and not supported in any other subnet (except subnet[3], which allows
  2660. // everyone).
  2661. classes.clear();
  2662. classes.insert("gamma");
  2663. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2664. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2665. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  2666. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2667. // Let's check if client belonging to some other class (not mentioned in
  2668. // the config) is supported only in subnet[3], which allows everyone.
  2669. classes.clear();
  2670. classes.insert("delta");
  2671. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2672. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2673. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2674. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2675. // Finally, let's check class-less client. He should be allowed only in
  2676. // the last subnet, which does not have any class restrictions.
  2677. classes.clear();
  2678. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2679. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2680. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2681. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2682. }
  2683. }