d2_cfg_mgr_unittests.cc 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. // Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <config/module_spec.h>
  8. #include <d2/d2_config.h>
  9. #include <d2/d2_cfg_mgr.h>
  10. #include <dhcpsrv/testutils/config_result_check.h>
  11. #include <process/testutils/d_test_stubs.h>
  12. #include <test_data_files_config.h>
  13. #include <util/encode/base64.h>
  14. #include <boost/foreach.hpp>
  15. #include <boost/scoped_ptr.hpp>
  16. #include <gtest/gtest.h>
  17. using namespace std;
  18. using namespace isc;
  19. using namespace isc::d2;
  20. using namespace isc::process;
  21. namespace {
  22. /// @brief Function to create full path to the spec file
  23. ///
  24. /// The full path is dependent upon the value of D2_SRC_DIR which
  25. /// whose value is generated from test_data_files_config.h.in
  26. ///
  27. /// @param name file name to which the path should be prepended
  28. std::string specfile(const std::string& name) {
  29. return (std::string(D2_SRC_DIR) + "/" + name);
  30. }
  31. /// @brief Function to create full path to test data file
  32. ///
  33. /// The full path is dependent upon the value of D2_TEST_DATA_DIR which
  34. /// whose value is generated from test_data_files_config.h.in
  35. ///
  36. /// @param name file name to which the path should be prepended
  37. std::string testDataFile(const std::string& name) {
  38. return (std::string(D2_TEST_DATA_DIR) + "/" + name);
  39. }
  40. /// @brief Test fixture class for testing D2CfgMgr class.
  41. /// It maintains an member instance of D2CfgMgr and provides methods for
  42. /// converting JSON strings to configuration element sets, checking parse
  43. /// results, and accessing the configuration context.
  44. class D2CfgMgrTest : public ConfigParseTest {
  45. public:
  46. /// @brief Constructor
  47. D2CfgMgrTest():cfg_mgr_(new D2CfgMgr()), d2_params_() {
  48. }
  49. /// @brief Destructor
  50. ~D2CfgMgrTest() {
  51. }
  52. /// @brief Configuration manager instance.
  53. D2CfgMgrPtr cfg_mgr_;
  54. /// @brief Build JSON configuration string for a D2Params element
  55. ///
  56. /// Constructs a JSON string for "params" element using replacable
  57. /// parameters.
  58. ///
  59. /// @param ip_address string to insert as ip_address value
  60. /// @param port integer to insert as port value
  61. /// @param dns_server_timeout integer to insert as dns_server_timeout value
  62. /// @param ncr_protocol string to insert as ncr_protocol value
  63. /// @param ncr_format string to insert as ncr_format value
  64. ///
  65. /// @return std::string containing the JSON configuration text
  66. std::string makeParamsConfigString(const std::string& ip_address,
  67. const int port,
  68. const int dns_server_timeout,
  69. const std::string& ncr_protocol,
  70. const std::string& ncr_format) {
  71. std::ostringstream config;
  72. config <<
  73. "{"
  74. " \"ip-address\": \"" << ip_address << "\" , "
  75. " \"port\": " << port << " , "
  76. " \"dns-server-timeout\": " << dns_server_timeout << " , "
  77. " \"ncr-protocol\": \"" << ncr_protocol << "\" , "
  78. " \"ncr-format\": \"" << ncr_format << "\", "
  79. "\"tsig-keys\": [], "
  80. "\"forward-ddns\" : {}, "
  81. "\"reverse-ddns\" : {} "
  82. "}";
  83. return (config.str());
  84. }
  85. /// @brief Enumeration to select between expected configuration outcomes
  86. enum RunConfigMode {
  87. SHOULD_PASS,
  88. SHOULD_FAIL
  89. };
  90. /// @brief Parses a configuration string and tests against a given outcome
  91. ///
  92. /// Convenience method which accepts JSON text and an expected pass or fail
  93. /// outcome. It converts the text into an ElementPtr and passes that to
  94. /// configuration manager's parseConfig method. It then tests the
  95. /// parse result against the expected outcome If they do not match it
  96. /// the method asserts a failure. If they do match, it refreshes the
  97. /// the D2Params pointer with the newly parsed instance.
  98. ///
  99. /// @param config_str the JSON configuration text to parse
  100. /// @param mode indicator if the parsing should fail or not. It defaults
  101. /// defaults to SHOULD_PASS.
  102. ///
  103. void runConfig(std::string config_str, RunConfigMode mode=SHOULD_PASS) {
  104. // We assume the config string is valid JSON.
  105. ASSERT_TRUE(fromJSON(config_str));
  106. // Parse the configuration and verify we got the expected outcome.
  107. answer_ = cfg_mgr_->parseConfig(config_set_);
  108. ASSERT_TRUE(checkAnswer(mode == SHOULD_FAIL));
  109. // Verify that the D2 context can be retrieved and is not null.
  110. D2CfgContextPtr context;
  111. ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());
  112. // Verify that the global scalars have the proper values.
  113. d2_params_ = context->getD2Params();
  114. ASSERT_TRUE(d2_params_);
  115. }
  116. /// @brief Check parse result against expected outcome and position info
  117. ///
  118. /// This method analyzes the given parsing result against an expected outcome
  119. /// of SHOULD_PASS or SHOULD_FAIL. If it is expected to fail, the comment
  120. /// contained within the result is searched for Element::Position information
  121. /// which should contain the given file name. It does not attempt to verify
  122. /// the numerical values for line number and col.
  123. ///
  124. /// @param answer Element set containing an integer result code and string
  125. /// comment.
  126. /// @param mode indicator if the parsing should fail or not.
  127. /// @param file_name name of the file containing the configuration text
  128. /// parsed. It defaults to "<string>" which is the value present if the
  129. /// configuration text did not originate from a file. (i.e. one did not use
  130. /// isc::data::Element::fromJSONFile() to read the JSON text).
  131. void
  132. checkAnswerWithError(isc::data::ConstElementPtr answer,
  133. RunConfigMode mode, std::string file_name="<string>") {
  134. int rcode = 0;
  135. isc::data::ConstElementPtr comment;
  136. comment = isc::config::parseAnswer(rcode, answer);
  137. if (mode == SHOULD_PASS) {
  138. if (rcode == 0) {
  139. return;
  140. }
  141. FAIL() << "Parsing was expected to pass but failed : " << rcode
  142. << " comment: " << *comment;
  143. }
  144. if (rcode == 0) {
  145. FAIL() << "Parsing was expected to fail but passed : "
  146. << " comment: " << *comment;
  147. }
  148. // Parsing was expected to fail, test for position info.
  149. if (isc::dhcp::test::errorContainsPosition(answer, file_name)) {
  150. return;
  151. }
  152. FAIL() << "Parsing failed as expected but lacks position : " << *comment;
  153. }
  154. /// @brief Pointer the D2Params most recently parsed.
  155. D2ParamsPtr d2_params_;
  156. };
  157. /// @brief Tests that the spec file is valid.
  158. /// Verifies that the DHCP-DDNS configuration specification file
  159. // is valid.
  160. TEST(D2SpecTest, basicSpec) {
  161. ASSERT_NO_THROW(isc::config::
  162. moduleSpecFromFile(specfile("dhcp-ddns.spec")));
  163. }
  164. /// @brief Convenience function which compares the contents of the given
  165. /// DnsServerInfo against the given set of values.
  166. ///
  167. /// It is structured in such a way that each value is checked, and output
  168. /// is generate for all that do not match.
  169. ///
  170. /// @param server is a pointer to the server to check against.
  171. /// @param hostname is the value to compare against server's hostname_.
  172. /// @param ip_address is the string value to compare against server's
  173. /// ip_address_.
  174. /// @param port is the value to compare against server's port.
  175. ///
  176. /// @return returns true if there is a match across the board, otherwise it
  177. /// returns false.
  178. bool checkServer(DnsServerInfoPtr server, const char* hostname,
  179. const char *ip_address, uint32_t port)
  180. {
  181. // Return value, assume its a match.
  182. bool result = true;
  183. if (!server) {
  184. EXPECT_TRUE(server);
  185. return false;
  186. }
  187. // Check hostname.
  188. if (server->getHostname() != hostname) {
  189. EXPECT_EQ(hostname, server->getHostname());
  190. result = false;
  191. }
  192. // Check IP address.
  193. if (server->getIpAddress().toText() != ip_address) {
  194. EXPECT_EQ(ip_address, server->getIpAddress().toText());
  195. result = false;
  196. }
  197. // Check port.
  198. if (server->getPort() != port) {
  199. EXPECT_EQ (port, server->getPort());
  200. result = false;
  201. }
  202. return (result);
  203. }
  204. /// @brief Convenience function which compares the contents of the given
  205. /// TSIGKeyInfo against the given set of values, and that the TSIGKey
  206. /// member points to a key.
  207. ///
  208. /// @param key is a pointer to the TSIGKeyInfo instance to verify
  209. /// @param name is the value to compare against key's name_.
  210. /// @param algorithm is the string value to compare against key's algorithm.
  211. /// @param secret is the value to compare against key's secret.
  212. ///
  213. /// @return returns true if there is a match across the board, otherwise it
  214. /// returns false.
  215. bool checkKey(TSIGKeyInfoPtr key, const std::string& name,
  216. const std::string& algorithm, const std::string& secret,
  217. uint32_t digestbits = 0) {
  218. // Return value, assume its a match.
  219. return (((key) &&
  220. (key->getName() == name) &&
  221. (key->getAlgorithm() == algorithm) &&
  222. (key->getDigestbits() == digestbits) &&
  223. (key->getSecret() == secret) &&
  224. (key->getTSIGKey())));
  225. }
  226. /// @brief Test fixture class for testing DnsServerInfo parsing.
  227. class TSIGKeyInfoTest : public ConfigParseTest {
  228. public:
  229. /// @brief Constructor
  230. TSIGKeyInfoTest() {
  231. reset();
  232. }
  233. /// @brief Destructor
  234. ~TSIGKeyInfoTest() {
  235. }
  236. /// @brief Wipe out the current storage and parser and replace
  237. /// them with new ones.
  238. void reset() {
  239. keys_.reset(new TSIGKeyInfoMap());
  240. parser_.reset(new TSIGKeyInfoParser("test", keys_));
  241. }
  242. /// @brief Storage for "committing" keys.
  243. TSIGKeyInfoMapPtr keys_;
  244. /// @brief Pointer to the current parser instance.
  245. isc::dhcp::ParserPtr parser_;
  246. };
  247. /// @brief Test fixture class for testing DnsServerInfo parsing.
  248. class DnsServerInfoTest : public ConfigParseTest {
  249. public:
  250. /// @brief Constructor
  251. DnsServerInfoTest() {
  252. reset();
  253. }
  254. /// @brief Destructor
  255. ~DnsServerInfoTest() {
  256. }
  257. /// @brief Wipe out the current storage and parser and replace
  258. /// them with new ones.
  259. void reset() {
  260. servers_.reset(new DnsServerInfoStorage());
  261. parser_.reset(new DnsServerInfoParser("test", servers_));
  262. }
  263. /// @brief Storage for "committing" servers.
  264. DnsServerInfoStoragePtr servers_;
  265. /// @brief Pointer to the current parser instance.
  266. isc::dhcp::ParserPtr parser_;
  267. };
  268. /// @brief Test fixture class for testing DDnsDomain parsing.
  269. class DdnsDomainTest : public ConfigParseTest {
  270. public:
  271. /// @brief Constructor
  272. DdnsDomainTest() {
  273. reset();
  274. }
  275. /// @brief Destructor
  276. ~DdnsDomainTest() {
  277. }
  278. /// @brief Wipe out the current storage and parser and replace
  279. /// them with new ones.
  280. void reset() {
  281. keys_.reset(new TSIGKeyInfoMap());
  282. domains_.reset(new DdnsDomainMap());
  283. parser_.reset(new DdnsDomainParser("test", domains_, keys_));
  284. }
  285. /// @brief Add TSIGKeyInfos to the key map
  286. ///
  287. /// @param name the name of the key
  288. /// @param algorithm the algorithm of the key
  289. /// @param secret the secret value of the key
  290. void addKey(const std::string& name, const std::string& algorithm,
  291. const std::string& secret) {
  292. TSIGKeyInfoPtr key_info(new TSIGKeyInfo(name, algorithm, secret));
  293. (*keys_)[name]=key_info;
  294. }
  295. /// @brief Storage for "committing" domains.
  296. DdnsDomainMapPtr domains_;
  297. /// @brief Storage for TSIGKeys
  298. TSIGKeyInfoMapPtr keys_;
  299. /// @brief Pointer to the current parser instance.
  300. isc::dhcp::ParserPtr parser_;
  301. };
  302. /// @brief Tests a basic valid configuration for D2Param.
  303. TEST_F(D2CfgMgrTest, validParamsEntry) {
  304. // Verify that ip_address can be valid v4 address.
  305. std::string config = makeParamsConfigString ("192.0.0.1", 777, 333,
  306. "UDP", "JSON");
  307. runConfig(config);
  308. EXPECT_EQ(isc::asiolink::IOAddress("192.0.0.1"),
  309. d2_params_->getIpAddress());
  310. EXPECT_EQ(777, d2_params_->getPort());
  311. EXPECT_EQ(333, d2_params_->getDnsServerTimeout());
  312. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_params_->getNcrProtocol());
  313. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_params_->getNcrFormat());
  314. // Verify that ip_address can be valid v6 address.
  315. config = makeParamsConfigString ("3001::5", 777, 333, "UDP", "JSON");
  316. runConfig(config);
  317. // Verify that the global scalars have the proper values.
  318. EXPECT_EQ(isc::asiolink::IOAddress("3001::5"),
  319. d2_params_->getIpAddress());
  320. // Verify the configuration summary.
  321. EXPECT_EQ("listening on 3001::5, port 777, using UDP",
  322. d2_params_->getConfigSummary());
  323. }
  324. /// @brief Tests default values for D2Params.
  325. /// It verifies that D2Params is populated with default value for optional
  326. /// parameter if not supplied in the configuration.
  327. /// Currently they are all optional.
  328. TEST_F(D2CfgMgrTest, defaultValues) {
  329. // Check that omitting ip_address gets you its default
  330. std::string config =
  331. "{"
  332. " \"port\": 777 , "
  333. " \"dns-server-timeout\": 333 , "
  334. " \"ncr-protocol\": \"UDP\" , "
  335. " \"ncr-format\": \"JSON\", "
  336. "\"tsig-keys\": [], "
  337. "\"forward-ddns\" : {}, "
  338. "\"reverse-ddns\" : {} "
  339. "}";
  340. runConfig(config);
  341. EXPECT_EQ(isc::asiolink::IOAddress(D2Params::DFT_IP_ADDRESS),
  342. d2_params_->getIpAddress());
  343. // Check that omitting port gets you its default
  344. config =
  345. "{"
  346. " \"ip-address\": \"192.0.0.1\" , "
  347. " \"dns-server-timeout\": 333 , "
  348. " \"ncr-protocol\": \"UDP\" , "
  349. " \"ncr-format\": \"JSON\", "
  350. "\"tsig-keys\": [], "
  351. "\"forward-ddns\" : {}, "
  352. "\"reverse-ddns\" : {} "
  353. "}";
  354. runConfig(config);
  355. EXPECT_EQ(D2Params::DFT_PORT, d2_params_->getPort());
  356. // Check that omitting timeout gets you its default
  357. config =
  358. "{"
  359. " \"ip-address\": \"192.0.0.1\" , "
  360. " \"port\": 777 , "
  361. " \"ncr-protocol\": \"UDP\" , "
  362. " \"ncr-format\": \"JSON\", "
  363. "\"tsig-keys\": [], "
  364. "\"forward-ddns\" : {}, "
  365. "\"reverse-ddns\" : {} "
  366. "}";
  367. runConfig(config);
  368. EXPECT_EQ(D2Params::DFT_DNS_SERVER_TIMEOUT,
  369. d2_params_->getDnsServerTimeout());
  370. // Check that protocol timeout gets you its default
  371. config =
  372. "{"
  373. " \"ip-address\": \"192.0.0.1\" , "
  374. " \"port\": 777 , "
  375. " \"dns-server-timeout\": 333 , "
  376. " \"ncr-format\": \"JSON\", "
  377. "\"tsig-keys\": [], "
  378. "\"forward-ddns\" : {}, "
  379. "\"reverse-ddns\" : {} "
  380. "}";
  381. runConfig(config);
  382. EXPECT_EQ(dhcp_ddns::stringToNcrProtocol(D2Params::DFT_NCR_PROTOCOL),
  383. d2_params_->getNcrProtocol());
  384. // Check that format timeout gets you its default
  385. config =
  386. "{"
  387. " \"ip-address\": \"192.0.0.1\" , "
  388. " \"port\": 777 , "
  389. " \"dns-server-timeout\": 333 , "
  390. " \"ncr-protocol\": \"UDP\", "
  391. "\"tsig-keys\": [], "
  392. "\"forward-ddns\" : {}, "
  393. "\"reverse-ddns\" : {} "
  394. "}";
  395. runConfig(config);
  396. EXPECT_EQ(dhcp_ddns::stringToNcrFormat(D2Params::DFT_NCR_FORMAT),
  397. d2_params_->getNcrFormat());
  398. }
  399. /// @brief Tests the unsupported scalar parameters and objects are detected.
  400. TEST_F(D2CfgMgrTest, unsupportedTopLevelItems) {
  401. // Check that an unsupported top level parameter fails.
  402. std::string config =
  403. "{"
  404. " \"ip-address\": \"127.0.0.1\", "
  405. " \"port\": 777 , "
  406. " \"dns-server-timeout\": 333 , "
  407. " \"ncr-protocol\": \"UDP\" , "
  408. " \"ncr-format\": \"JSON\", "
  409. "\"tsig-keys\": [], "
  410. "\"forward-ddns\" : {}, "
  411. "\"reverse-ddns\" : {}, "
  412. "\"bogus-param\" : true "
  413. "}";
  414. runConfig(config, SHOULD_FAIL);
  415. // Check that unsupported top level objects fails. For
  416. // D2 these fail as they are not in the parse order.
  417. config =
  418. "{"
  419. " \"ip-address\": \"127.0.0.1\", "
  420. " \"port\": 777 , "
  421. " \"dns-server-timeout\": 333 , "
  422. " \"ncr-protocol\": \"UDP\" , "
  423. " \"ncr-format\": \"JSON\", "
  424. "\"tsig-keys\": [], "
  425. "\"bogus-object-one\" : {}, "
  426. "\"forward-ddns\" : {}, "
  427. "\"reverse-ddns\" : {}, "
  428. "\"bogus-object-two\" : {} "
  429. "}";
  430. runConfig(config, SHOULD_FAIL);
  431. }
  432. /// @brief Tests the enforcement of data validation when parsing D2Params.
  433. /// It verifies that:
  434. /// -# ip_address cannot be "0.0.0.0"
  435. /// -# ip_address cannot be "::"
  436. /// -# port cannot be 0
  437. /// -# dns_server_timeout cannat be 0
  438. /// -# ncr_protocol must be valid
  439. /// -# ncr_format must be valid
  440. TEST_F(D2CfgMgrTest, invalidEntry) {
  441. // Cannot use IPv4 ANY address
  442. std::string config = makeParamsConfigString ("0.0.0.0", 777, 333,
  443. "UDP", "JSON");
  444. runConfig(config, SHOULD_FAIL);
  445. // Cannot use IPv6 ANY address
  446. config = makeParamsConfigString ("::", 777, 333, "UDP", "JSON");
  447. runConfig(config, SHOULD_FAIL);
  448. // Cannot use port 0
  449. config = makeParamsConfigString ("127.0.0.1", 0, 333, "UDP", "JSON");
  450. runConfig(config, SHOULD_FAIL);
  451. // Cannot use dns server timeout of 0
  452. config = makeParamsConfigString ("127.0.0.1", 777, 0, "UDP", "JSON");
  453. runConfig(config, SHOULD_FAIL);
  454. // Invalid protocol
  455. config = makeParamsConfigString ("127.0.0.1", 777, 333, "BOGUS", "JSON");
  456. runConfig(config, SHOULD_FAIL);
  457. // Unsupported protocol
  458. config = makeParamsConfigString ("127.0.0.1", 777, 333, "TCP", "JSON");
  459. runConfig(config, SHOULD_FAIL);
  460. // Invalid format
  461. config = makeParamsConfigString ("127.0.0.1", 777, 333, "UDP", "BOGUS");
  462. runConfig(config, SHOULD_FAIL);
  463. }
  464. /// @brief Tests the enforcement of data validation when parsing TSIGKeyInfos.
  465. /// It verifies that:
  466. /// 1. Name cannot be blank.
  467. /// 2. Algorithm cannot be blank.
  468. /// 3. Secret cannot be blank.
  469. TEST_F(TSIGKeyInfoTest, invalidEntry) {
  470. // Config with a blank name entry.
  471. std::string config = "{"
  472. " \"name\": \"\" , "
  473. " \"algorithm\": \"HMAC-MD5\" , "
  474. " \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
  475. "}";
  476. ASSERT_TRUE(fromJSON(config));
  477. // Verify that build fails on blank name.
  478. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  479. // Config with a blank algorithm entry.
  480. config = "{"
  481. " \"name\": \"d2_key_one\" , "
  482. " \"algorithm\": \"\" , "
  483. " \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
  484. "}";
  485. ASSERT_TRUE(fromJSON(config));
  486. // Verify that build fails on blank algorithm.
  487. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  488. // Config with an invalid algorithm entry.
  489. config = "{"
  490. " \"name\": \"d2_key_one\" , "
  491. " \"algorithm\": \"bogus\" , "
  492. " \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
  493. "}";
  494. ASSERT_TRUE(fromJSON(config));
  495. // Verify that build fails on blank algorithm.
  496. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  497. // Config with a blank secret entry.
  498. config = "{"
  499. " \"name\": \"d2_key_one\" , "
  500. " \"algorithm\": \"HMAC-MD5\" , "
  501. " \"secret\": \"\" "
  502. "}";
  503. ASSERT_TRUE(fromJSON(config));
  504. // Verify that build fails blank secret
  505. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  506. // Config with an invalid secret entry.
  507. config = "{"
  508. " \"name\": \"d2_key_one\" , "
  509. " \"algorithm\": \"HMAC-MD5\" , "
  510. " \"secret\": \"bogus\" "
  511. "}";
  512. ASSERT_TRUE(fromJSON(config));
  513. // Verify that build fails an invalid secret
  514. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  515. }
  516. /// @brief Verifies that TSIGKeyInfo parsing creates a proper TSIGKeyInfo
  517. /// when given a valid combination of entries.
  518. TEST_F(TSIGKeyInfoTest, validEntry) {
  519. // Valid entries for TSIG key, all items are required.
  520. std::string config = "{"
  521. " \"name\": \"d2_key_one\" , "
  522. " \"algorithm\": \"HMAC-MD5\" , "
  523. " \"digest-bits\": 120 , "
  524. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  525. "}";
  526. ASSERT_TRUE(fromJSON(config));
  527. // Verify that it builds and commits without throwing.
  528. //ASSERT_NO_THROW(parser_->build(config_set_));
  529. (parser_->build(config_set_));
  530. ASSERT_NO_THROW(parser_->commit());
  531. // Verify the correct number of keys are present
  532. int count = keys_->size();
  533. EXPECT_EQ(1, count);
  534. // Find the key and retrieve it.
  535. TSIGKeyInfoMap::iterator gotit = keys_->find("d2_key_one");
  536. ASSERT_TRUE(gotit != keys_->end());
  537. TSIGKeyInfoPtr& key = gotit->second;
  538. // Verify the key contents.
  539. EXPECT_TRUE(checkKey(key, "d2_key_one", "HMAC-MD5",
  540. "dGhpcyBrZXkgd2lsbCBtYXRjaA==", 120));
  541. }
  542. /// @brief Verifies that attempting to parse an invalid list of TSIGKeyInfo
  543. /// entries is detected.
  544. TEST_F(TSIGKeyInfoTest, invalidTSIGKeyList) {
  545. // Construct a list of keys with an invalid key entry.
  546. std::string config = "["
  547. " { \"name\": \"key1\" , "
  548. " \"algorithm\": \"HMAC-MD5\" ,"
  549. " \"digest-bits\": 120 , "
  550. " \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
  551. " },"
  552. // this entry has an invalid algorithm
  553. " { \"name\": \"key2\" , "
  554. " \"algorithm\": \"\" ,"
  555. " \"digest-bits\": 120 , "
  556. " \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
  557. " },"
  558. " { \"name\": \"key3\" , "
  559. " \"algorithm\": \"HMAC-MD5\" ,"
  560. " \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
  561. " }"
  562. " ]";
  563. ASSERT_TRUE(fromJSON(config));
  564. // Create the list parser.
  565. isc::dhcp::ParserPtr parser;
  566. ASSERT_NO_THROW(parser.reset(new TSIGKeyInfoListParser("test", keys_)));
  567. // Verify that the list builds without errors.
  568. EXPECT_THROW(parser->build(config_set_), D2CfgError);
  569. }
  570. /// @brief Verifies that attempting to parse an invalid list of TSIGKeyInfo
  571. /// entries is detected.
  572. TEST_F(TSIGKeyInfoTest, duplicateTSIGKey) {
  573. // Construct a list of keys with an invalid key entry.
  574. std::string config = "["
  575. " { \"name\": \"key1\" , "
  576. " \"algorithm\": \"HMAC-MD5\" ,"
  577. " \"digest-bits\": 120 , "
  578. " \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
  579. " },"
  580. " { \"name\": \"key2\" , "
  581. " \"algorithm\": \"HMAC-MD5\" ,"
  582. " \"digest-bits\": 120 , "
  583. " \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
  584. " },"
  585. " { \"name\": \"key1\" , "
  586. " \"algorithm\": \"HMAC-MD5\" ,"
  587. " \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
  588. " }"
  589. " ]";
  590. ASSERT_TRUE(fromJSON(config));
  591. // Create the list parser.
  592. isc::dhcp::ParserPtr parser;
  593. ASSERT_NO_THROW(parser.reset(new TSIGKeyInfoListParser("test", keys_)));
  594. // Verify that the list builds without errors.
  595. EXPECT_THROW(parser->build(config_set_), D2CfgError);
  596. }
  597. /// @brief Verifies a valid list of TSIG Keys parses correctly.
  598. /// Also verifies that all of the supported algorithm names work.
  599. TEST_F(TSIGKeyInfoTest, validTSIGKeyList) {
  600. // Construct a valid list of keys.
  601. std::string config = "["
  602. " { \"name\": \"key1\" , "
  603. " \"algorithm\": \"HMAC-MD5\" ,"
  604. " \"digest-bits\": 80 , "
  605. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  606. " },"
  607. " { \"name\": \"key2\" , "
  608. " \"algorithm\": \"HMAC-SHA1\" ,"
  609. " \"digest-bits\": 80 , "
  610. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  611. " },"
  612. " { \"name\": \"key3\" , "
  613. " \"algorithm\": \"HMAC-SHA256\" ,"
  614. " \"digest-bits\": 128 , "
  615. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  616. " },"
  617. " { \"name\": \"key4\" , "
  618. " \"algorithm\": \"HMAC-SHA224\" ,"
  619. " \"digest-bits\": 112 , "
  620. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  621. " },"
  622. " { \"name\": \"key5\" , "
  623. " \"algorithm\": \"HMAC-SHA384\" ,"
  624. " \"digest-bits\": 192 , "
  625. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  626. " },"
  627. " { \"name\": \"key6\" , "
  628. " \"algorithm\": \"HMAC-SHA512\" ,"
  629. " \"digest-bits\": 256 , "
  630. " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
  631. " }"
  632. " ]";
  633. ASSERT_TRUE(fromJSON(config));
  634. // Verify that the list builds and commits without errors.
  635. // Create the list parser.
  636. isc::dhcp::ParserPtr parser;
  637. ASSERT_NO_THROW(parser.reset(new TSIGKeyInfoListParser("test", keys_)));
  638. ASSERT_NO_THROW(parser->build(config_set_));
  639. ASSERT_NO_THROW(parser->commit());
  640. std::string ref_secret = "dGhpcyBrZXkgd2lsbCBtYXRjaA==";
  641. // Verify the correct number of keys are present
  642. int count = keys_->size();
  643. ASSERT_EQ(6, count);
  644. // Find the 1st key and retrieve it.
  645. TSIGKeyInfoMap::iterator gotit = keys_->find("key1");
  646. ASSERT_TRUE(gotit != keys_->end());
  647. TSIGKeyInfoPtr& key = gotit->second;
  648. // Verify the key contents.
  649. EXPECT_TRUE(checkKey(key, "key1", TSIGKeyInfo::HMAC_MD5_STR,
  650. ref_secret, 80));
  651. // Find the 2nd key and retrieve it.
  652. gotit = keys_->find("key2");
  653. ASSERT_TRUE(gotit != keys_->end());
  654. key = gotit->second;
  655. // Verify the key contents.
  656. EXPECT_TRUE(checkKey(key, "key2", TSIGKeyInfo::HMAC_SHA1_STR,
  657. ref_secret, 80));
  658. // Find the 3rd key and retrieve it.
  659. gotit = keys_->find("key3");
  660. ASSERT_TRUE(gotit != keys_->end());
  661. key = gotit->second;
  662. // Verify the key contents.
  663. EXPECT_TRUE(checkKey(key, "key3", TSIGKeyInfo::HMAC_SHA256_STR,
  664. ref_secret, 128));
  665. // Find the 4th key and retrieve it.
  666. gotit = keys_->find("key4");
  667. ASSERT_TRUE(gotit != keys_->end());
  668. key = gotit->second;
  669. // Verify the key contents.
  670. EXPECT_TRUE(checkKey(key, "key4", TSIGKeyInfo::HMAC_SHA224_STR,
  671. ref_secret, 112));
  672. // Find the 5th key and retrieve it.
  673. gotit = keys_->find("key5");
  674. ASSERT_TRUE(gotit != keys_->end());
  675. key = gotit->second;
  676. // Verify the key contents.
  677. EXPECT_TRUE(checkKey(key, "key5", TSIGKeyInfo::HMAC_SHA384_STR,
  678. ref_secret, 192));
  679. // Find the 6th key and retrieve it.
  680. gotit = keys_->find("key6");
  681. ASSERT_TRUE(gotit != keys_->end());
  682. key = gotit->second;
  683. // Verify the key contents.
  684. EXPECT_TRUE(checkKey(key, "key6", TSIGKeyInfo::HMAC_SHA512_STR,
  685. ref_secret, 256));
  686. }
  687. /// @brief Tests the enforcement of data validation when parsing DnsServerInfos.
  688. /// It verifies that:
  689. /// 1. Specifying both a hostname and an ip address is not allowed.
  690. /// 2. Specifying both blank a hostname and blank ip address is not allowed.
  691. /// 3. Specifying a negative port number is not allowed.
  692. TEST_F(DnsServerInfoTest, invalidEntry) {
  693. // Create a config in which both host and ip address are supplied.
  694. // Verify that build fails.
  695. std::string config = "{ \"hostname\": \"pegasus.tmark\", "
  696. " \"ip-address\": \"127.0.0.1\" } ";
  697. ASSERT_TRUE(fromJSON(config));
  698. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  699. // Neither host nor ip address supplied
  700. // Verify that builds fails.
  701. config = "{ \"hostname\": \"\", "
  702. " \"ip-address\": \"\" } ";
  703. ASSERT_TRUE(fromJSON(config));
  704. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  705. // Create a config with a negative port number.
  706. // Verify that build fails.
  707. config = "{ \"ip-address\": \"192.168.5.6\" ,"
  708. " \"port\": -100 }";
  709. ASSERT_TRUE(fromJSON(config));
  710. EXPECT_THROW (parser_->build(config_set_), isc::BadValue);
  711. }
  712. /// @brief Verifies that DnsServerInfo parsing creates a proper DnsServerInfo
  713. /// when given a valid combination of entries.
  714. /// It verifies that:
  715. /// 1. A DnsServerInfo entry is correctly made, when given only a hostname.
  716. /// 2. A DnsServerInfo entry is correctly made, when given ip address and port.
  717. /// 3. A DnsServerInfo entry is correctly made, when given only an ip address.
  718. TEST_F(DnsServerInfoTest, validEntry) {
  719. /// @todo When resolvable hostname is supported you'll need this test.
  720. /// // Valid entries for dynamic host
  721. /// std::string config = "{ \"hostname\": \"pegasus.tmark\" }";
  722. /// ASSERT_TRUE(fromJSON(config));
  723. /// // Verify that it builds and commits without throwing.
  724. /// ASSERT_NO_THROW(parser_->build(config_set_));
  725. /// ASSERT_NO_THROW(parser_->commit());
  726. /// //Verify the correct number of servers are present
  727. /// int count = servers_->size();
  728. /// EXPECT_EQ(1, count);
  729. /// Verify the server exists and has the correct values.
  730. /// DnsServerInfoPtr server = (*servers_)[0];
  731. /// EXPECT_TRUE(checkServer(server, "pegasus.tmark",
  732. /// DnsServerInfo::EMPTY_IP_STR,
  733. /// DnsServerInfo::STANDARD_DNS_PORT));
  734. /// // Start over for a new test.
  735. /// reset();
  736. // Valid entries for static ip
  737. std::string config = " { \"ip-address\": \"127.0.0.1\" , "
  738. " \"port\": 100 }";
  739. ASSERT_TRUE(fromJSON(config));
  740. // Verify that it builds and commits without throwing.
  741. ASSERT_NO_THROW(parser_->build(config_set_));
  742. ASSERT_NO_THROW(parser_->commit());
  743. // Verify the correct number of servers are present
  744. int count = servers_->size();
  745. EXPECT_EQ(1, count);
  746. // Verify the server exists and has the correct values.
  747. DnsServerInfoPtr server = (*servers_)[0];
  748. EXPECT_TRUE(checkServer(server, "", "127.0.0.1", 100));
  749. // Start over for a new test.
  750. reset();
  751. // Valid entries for static ip, no port
  752. config = " { \"ip-address\": \"192.168.2.5\" }";
  753. ASSERT_TRUE(fromJSON(config));
  754. // Verify that it builds and commits without throwing.
  755. ASSERT_NO_THROW(parser_->build(config_set_));
  756. ASSERT_NO_THROW(parser_->commit());
  757. // Verify the correct number of servers are present
  758. count = servers_->size();
  759. EXPECT_EQ(1, count);
  760. // Verify the server exists and has the correct values.
  761. server = (*servers_)[0];
  762. EXPECT_TRUE(checkServer(server, "", "192.168.2.5",
  763. DnsServerInfo::STANDARD_DNS_PORT));
  764. }
  765. /// @brief Verifies that attempting to parse an invalid list of DnsServerInfo
  766. /// entries is detected.
  767. TEST_F(ConfigParseTest, invalidServerList) {
  768. // Construct a list of servers with an invalid server entry.
  769. std::string config = "[ { \"ip-address\": \"127.0.0.1\" }, "
  770. "{ \"ip-address\": \"\" }, "
  771. "{ \"ip-address\": \"127.0.0.2\" } ]";
  772. ASSERT_TRUE(fromJSON(config));
  773. // Create the server storage and list parser.
  774. DnsServerInfoStoragePtr servers(new DnsServerInfoStorage());
  775. isc::dhcp::ParserPtr parser;
  776. ASSERT_NO_THROW(parser.reset(new DnsServerInfoListParser("test", servers)));
  777. // Verify that build fails.
  778. EXPECT_THROW(parser->build(config_set_), D2CfgError);
  779. }
  780. /// @brief Verifies that a list of DnsServerInfo entries parses correctly given
  781. /// a valid configuration.
  782. TEST_F(ConfigParseTest, validServerList) {
  783. // Create a valid list of servers.
  784. std::string config = "[ { \"ip-address\": \"127.0.0.1\" }, "
  785. "{ \"ip-address\": \"127.0.0.2\" }, "
  786. "{ \"ip-address\": \"127.0.0.3\" } ]";
  787. ASSERT_TRUE(fromJSON(config));
  788. // Create the server storage and list parser.
  789. DnsServerInfoStoragePtr servers(new DnsServerInfoStorage());
  790. isc::dhcp::ParserPtr parser;
  791. ASSERT_NO_THROW(parser.reset(new DnsServerInfoListParser("test", servers)));
  792. // Verfiy that the list builds and commits without error.
  793. ASSERT_NO_THROW(parser->build(config_set_));
  794. ASSERT_NO_THROW(parser->commit());
  795. // Verify that the server storage contains the correct number of servers.
  796. int count = servers->size();
  797. EXPECT_EQ(3, count);
  798. // Verify the first server exists and has the correct values.
  799. DnsServerInfoPtr server = (*servers)[0];
  800. EXPECT_TRUE(checkServer(server, "", "127.0.0.1",
  801. DnsServerInfo::STANDARD_DNS_PORT));
  802. // Verify the second server exists and has the correct values.
  803. server = (*servers)[1];
  804. EXPECT_TRUE(checkServer(server, "", "127.0.0.2",
  805. DnsServerInfo::STANDARD_DNS_PORT));
  806. // Verify the third server exists and has the correct values.
  807. server = (*servers)[2];
  808. EXPECT_TRUE(checkServer(server, "", "127.0.0.3",
  809. DnsServerInfo::STANDARD_DNS_PORT));
  810. }
  811. /// @brief Tests the enforcement of data validation when parsing DdnsDomains.
  812. /// It verifies that:
  813. /// 1. Domain storage cannot be null when constructing a DdnsDomainParser.
  814. /// 2. The name entry is not optional.
  815. /// 3. The server list man not be empty.
  816. /// 4. That a mal-formed server entry is detected.
  817. /// 5. That an undefined key name is detected.
  818. TEST_F(DdnsDomainTest, invalidDdnsDomainEntry) {
  819. // Verify that attempting to construct the parser with null storage fails.
  820. DdnsDomainMapPtr domains;
  821. ASSERT_THROW(isc::dhcp::ParserPtr(
  822. new DdnsDomainParser("test", domains, keys_)), D2CfgError);
  823. // Create a domain configuration without a name
  824. std::string config = "{ \"key-name\": \"d2_key.tmark.org\" , "
  825. " \"dns-servers\" : [ "
  826. " { \"ip-address\": \"127.0.0.1\" , "
  827. " \"port\": 100 },"
  828. " { \"ip-address\": \"127.0.0.2\" , "
  829. " \"port\": 200 },"
  830. " { \"ip-address\": \"127.0.0.3\" , "
  831. " \"port\": 300 } ] } ";
  832. ASSERT_TRUE(fromJSON(config));
  833. // Verify that the domain configuration builds fails.
  834. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  835. // Create a domain configuration with an empty server list.
  836. config = "{ \"name\": \"tmark.org\" , "
  837. " \"key-name\": \"d2_key.tmark.org\" , "
  838. " \"dns-servers\" : [ "
  839. " ] } ";
  840. ASSERT_TRUE(fromJSON(config));
  841. // Verify that the domain configuration build fails.
  842. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  843. // Create a domain configuration with a mal-formed server entry.
  844. config = "{ \"name\": \"tmark.org\" , "
  845. " \"key-name\": \"d2_key.tmark.org\" , "
  846. " \"dns-servers\" : [ "
  847. " { \"ip-address\": \"127.0.0.3\" , "
  848. " \"port\": -1 } ] } ";
  849. ASSERT_TRUE(fromJSON(config));
  850. // Verify that the domain configuration build fails.
  851. EXPECT_THROW(parser_->build(config_set_), isc::BadValue);
  852. // Create a domain configuration without an defined key name
  853. config = "{ \"name\": \"tmark.org\" , "
  854. " \"key-name\": \"d2_key.tmark.org\" , "
  855. " \"dns-servers\" : [ "
  856. " { \"ip-address\": \"127.0.0.3\" , "
  857. " \"port\": 300 } ] } ";
  858. ASSERT_TRUE(fromJSON(config));
  859. // Verify that the domain configuration build fails.
  860. EXPECT_THROW(parser_->build(config_set_), D2CfgError);
  861. }
  862. /// @brief Verifies the basics of parsing DdnsDomains.
  863. /// It verifies that:
  864. /// 1. Valid construction of DdnsDomainParser functions.
  865. /// 2. Given a valid, configuration entry, DdnsDomainParser parses
  866. /// correctly.
  867. /// (It indirectly verifies the operation of DdnsDomainMap).
  868. TEST_F(DdnsDomainTest, ddnsDomainParsing) {
  869. // Create a valid domain configuration entry containing three valid
  870. // servers.
  871. std::string config =
  872. "{ \"name\": \"tmark.org\" , "
  873. " \"key-name\": \"d2_key.tmark.org\" , "
  874. " \"dns-servers\" : [ "
  875. " { \"ip-address\": \"127.0.0.1\" , "
  876. " \"port\": 100 },"
  877. " { \"ip-address\": \"127.0.0.2\" , "
  878. " \"port\": 200 },"
  879. " { \"ip-address\": \"127.0.0.3\" , "
  880. " \"port\": 300 } ] } ";
  881. ASSERT_TRUE(fromJSON(config));
  882. // Add a TSIG key to the test key map, so key validation will pass.
  883. addKey("d2_key.tmark.org", "HMAC-MD5", "GWG/Xfbju4O2iXGqkSu4PQ==");
  884. // Verify that the domain configuration builds and commits without error.
  885. ASSERT_NO_THROW(parser_->build(config_set_));
  886. ASSERT_NO_THROW(parser_->commit());
  887. // Verify that the domain storage contains the correct number of domains.
  888. int count = domains_->size();
  889. EXPECT_EQ(1, count);
  890. // Verify that the expected domain exists and can be retrieved from
  891. // the storage.
  892. DdnsDomainMap::iterator gotit = domains_->find("tmark.org");
  893. ASSERT_TRUE(gotit != domains_->end());
  894. DdnsDomainPtr& domain = gotit->second;
  895. // Verify the name and key_name values.
  896. EXPECT_EQ("tmark.org", domain->getName());
  897. EXPECT_EQ("d2_key.tmark.org", domain->getKeyName());
  898. ASSERT_TRUE(domain->getTSIGKeyInfo());
  899. ASSERT_TRUE(domain->getTSIGKeyInfo()->getTSIGKey());
  900. // Verify that the server list exists and contains the correct number of
  901. // servers.
  902. const DnsServerInfoStoragePtr& servers = domain->getServers();
  903. EXPECT_TRUE(servers);
  904. count = servers->size();
  905. EXPECT_EQ(3, count);
  906. // Fetch each server and verify its contents.
  907. DnsServerInfoPtr server = (*servers)[0];
  908. EXPECT_TRUE(server);
  909. EXPECT_TRUE(checkServer(server, "", "127.0.0.1", 100));
  910. server = (*servers)[1];
  911. EXPECT_TRUE(server);
  912. EXPECT_TRUE(checkServer(server, "", "127.0.0.2", 200));
  913. server = (*servers)[2];
  914. EXPECT_TRUE(server);
  915. EXPECT_TRUE(checkServer(server, "", "127.0.0.3", 300));
  916. }
  917. /// @brief Tests the fundamentals of parsing DdnsDomain lists.
  918. /// This test verifies that given a valid domain list configuration
  919. /// it will accurately parse and populate each domain in the list.
  920. TEST_F(DdnsDomainTest, DdnsDomainListParsing) {
  921. // Create a valid domain list configuration, with two domains
  922. // that have three servers each.
  923. std::string config =
  924. "[ "
  925. "{ \"name\": \"tmark.org\" , "
  926. " \"key-name\": \"d2_key.tmark.org\" , "
  927. " \"dns-servers\" : [ "
  928. " { \"ip-address\": \"127.0.0.1\" , "
  929. " \"port\": 100 },"
  930. " { \"ip-address\": \"127.0.0.2\" , "
  931. " \"port\": 200 },"
  932. " { \"ip-address\": \"127.0.0.3\" , "
  933. " \"port\": 300 } ] } "
  934. ", "
  935. "{ \"name\": \"billcat.net\" , "
  936. " \"key-name\": \"d2_key.billcat.net\" , "
  937. " \"dns-servers\" : [ "
  938. " { \"ip-address\": \"127.0.0.4\" , "
  939. " \"port\": 400 },"
  940. " { \"ip-address\": \"127.0.0.5\" , "
  941. " \"port\": 500 },"
  942. " { \"ip-address\": \"127.0.0.6\" , "
  943. " \"port\": 600 } ] } "
  944. "] ";
  945. ASSERT_TRUE(fromJSON(config));
  946. // Add keys to key map so key validation passes.
  947. addKey("d2_key.tmark.org", "HMAC-MD5", "GWG/Xfbju4O2iXGqkSu4PQ==");
  948. addKey("d2_key.billcat.net", "HMAC-MD5", "GWG/Xfbju4O2iXGqkSu4PQ==");
  949. // Create the list parser
  950. isc::dhcp::ParserPtr list_parser;
  951. ASSERT_NO_THROW(list_parser.reset(
  952. new DdnsDomainListParser("test", domains_, keys_)));
  953. // Verify that the domain configuration builds and commits without error.
  954. ASSERT_NO_THROW(list_parser->build(config_set_));
  955. ASSERT_NO_THROW(list_parser->commit());
  956. // Verify that the domain storage contains the correct number of domains.
  957. int count = domains_->size();
  958. EXPECT_EQ(2, count);
  959. // Verify that the first domain exists and can be retrieved.
  960. DdnsDomainMap::iterator gotit = domains_->find("tmark.org");
  961. ASSERT_TRUE(gotit != domains_->end());
  962. DdnsDomainPtr& domain = gotit->second;
  963. // Verify the name and key_name values of the first domain.
  964. EXPECT_EQ("tmark.org", domain->getName());
  965. EXPECT_EQ("d2_key.tmark.org", domain->getKeyName());
  966. ASSERT_TRUE(domain->getTSIGKeyInfo());
  967. ASSERT_TRUE(domain->getTSIGKeyInfo()->getTSIGKey());
  968. // Verify the each of the first domain's servers
  969. DnsServerInfoStoragePtr servers = domain->getServers();
  970. EXPECT_TRUE(servers);
  971. count = servers->size();
  972. EXPECT_EQ(3, count);
  973. DnsServerInfoPtr server = (*servers)[0];
  974. EXPECT_TRUE(server);
  975. EXPECT_TRUE(checkServer(server, "", "127.0.0.1", 100));
  976. server = (*servers)[1];
  977. EXPECT_TRUE(server);
  978. EXPECT_TRUE(checkServer(server, "", "127.0.0.2", 200));
  979. server = (*servers)[2];
  980. EXPECT_TRUE(server);
  981. EXPECT_TRUE(checkServer(server, "", "127.0.0.3", 300));
  982. // Verify second domain
  983. gotit = domains_->find("billcat.net");
  984. ASSERT_TRUE(gotit != domains_->end());
  985. domain = gotit->second;
  986. // Verify the name and key_name values of the second domain.
  987. EXPECT_EQ("billcat.net", domain->getName());
  988. EXPECT_EQ("d2_key.billcat.net", domain->getKeyName());
  989. ASSERT_TRUE(domain->getTSIGKeyInfo());
  990. ASSERT_TRUE(domain->getTSIGKeyInfo()->getTSIGKey());
  991. // Verify the each of second domain's servers
  992. servers = domain->getServers();
  993. EXPECT_TRUE(servers);
  994. count = servers->size();
  995. EXPECT_EQ(3, count);
  996. server = (*servers)[0];
  997. EXPECT_TRUE(server);
  998. EXPECT_TRUE(checkServer(server, "", "127.0.0.4", 400));
  999. server = (*servers)[1];
  1000. EXPECT_TRUE(server);
  1001. EXPECT_TRUE(checkServer(server, "", "127.0.0.5", 500));
  1002. server = (*servers)[2];
  1003. EXPECT_TRUE(server);
  1004. EXPECT_TRUE(checkServer(server, "", "127.0.0.6", 600));
  1005. }
  1006. /// @brief Tests that a domain list configuration cannot contain duplicates.
  1007. TEST_F(DdnsDomainTest, duplicateDomain) {
  1008. // Create a domain list configuration that contains two domains with
  1009. // the same name.
  1010. std::string config =
  1011. "[ "
  1012. "{ \"name\": \"tmark.org\" , "
  1013. " \"dns-servers\" : [ "
  1014. " { \"ip-address\": \"127.0.0.3\" , "
  1015. " \"port\": 300 } ] } "
  1016. ", "
  1017. "{ \"name\": \"tmark.org\" , "
  1018. " \"dns-servers\" : [ "
  1019. " { \"ip-address\": \"127.0.0.3\" , "
  1020. " \"port\": 300 } ] } "
  1021. "] ";
  1022. ASSERT_TRUE(fromJSON(config));
  1023. // Create the list parser
  1024. isc::dhcp::ParserPtr list_parser;
  1025. ASSERT_NO_THROW(list_parser.reset(
  1026. new DdnsDomainListParser("test", domains_, keys_)));
  1027. // Verify that the parse build fails.
  1028. EXPECT_THROW(list_parser->build(config_set_), D2CfgError);
  1029. }
  1030. /// @brief Tests construction of D2CfgMgr
  1031. /// This test verifies that a D2CfgMgr constructs properly.
  1032. TEST(D2CfgMgr, construction) {
  1033. boost::scoped_ptr<D2CfgMgr> cfg_mgr;
  1034. // Verify that configuration manager constructions without error.
  1035. ASSERT_NO_THROW(cfg_mgr.reset(new D2CfgMgr()));
  1036. // Verify that the context can be retrieved and is not null.
  1037. D2CfgContextPtr context;
  1038. ASSERT_NO_THROW(context = cfg_mgr->getD2CfgContext());
  1039. EXPECT_TRUE(context);
  1040. // Verify that the forward manager can be retrieved and is not null.
  1041. EXPECT_TRUE(context->getForwardMgr());
  1042. // Verify that the reverse manager can be retrieved and is not null.
  1043. EXPECT_TRUE(context->getReverseMgr());
  1044. // Verify that the manager can be destructed without error.
  1045. EXPECT_NO_THROW(cfg_mgr.reset());
  1046. }
  1047. /// @brief Tests the parsing of a complete, valid DHCP-DDNS configuration.
  1048. /// This tests passes the configuration into an instance of D2CfgMgr just
  1049. /// as it would be done by d2_process in response to a configuration update
  1050. /// event.
  1051. TEST_F(D2CfgMgrTest, fullConfig) {
  1052. // Create a configuration with all of application level parameters, plus
  1053. // both the forward and reverse ddns managers. Both managers have two
  1054. // domains with three servers per domain.
  1055. std::string config = "{ "
  1056. "\"ip-address\" : \"192.168.1.33\" , "
  1057. "\"port\" : 88 , "
  1058. " \"dns-server-timeout\": 333 , "
  1059. " \"ncr-protocol\": \"UDP\" , "
  1060. " \"ncr-format\": \"JSON\", "
  1061. "\"tsig-keys\": ["
  1062. "{"
  1063. " \"name\": \"d2_key.tmark.org\" , "
  1064. " \"algorithm\": \"hmac-md5\" , "
  1065. " \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
  1066. "},"
  1067. "{"
  1068. " \"name\": \"d2_key.billcat.net\" , "
  1069. " \"algorithm\": \"hmac-md5\" , "
  1070. " \"digest-bits\": 120 , "
  1071. " \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
  1072. "}"
  1073. "],"
  1074. "\"forward-ddns\" : {"
  1075. "\"ddns-domains\": [ "
  1076. "{ \"name\": \"tmark.org\" , "
  1077. " \"key-name\": \"d2_key.tmark.org\" , "
  1078. " \"dns-servers\" : [ "
  1079. " { \"ip-address\": \"127.0.0.1\" } , "
  1080. " { \"ip-address\": \"127.0.0.2\" } , "
  1081. " { \"ip-address\": \"127.0.0.3\"} "
  1082. " ] } "
  1083. ", "
  1084. "{ \"name\": \"billcat.net\" , "
  1085. " \"key-name\": \"d2_key.billcat.net\" , "
  1086. " \"dns-servers\" : [ "
  1087. " { \"ip-address\": \"127.0.0.4\" } , "
  1088. " { \"ip-address\": \"127.0.0.5\" } , "
  1089. " { \"ip-address\": \"127.0.0.6\" } "
  1090. " ] } "
  1091. "] },"
  1092. "\"reverse-ddns\" : {"
  1093. "\"ddns-domains\": [ "
  1094. "{ \"name\": \" 0.168.192.in.addr.arpa.\" , "
  1095. " \"key-name\": \"d2_key.tmark.org\" , "
  1096. " \"dns-servers\" : [ "
  1097. " { \"ip-address\": \"127.0.1.1\" } , "
  1098. " { \"ip-address\": \"127.0.2.1\" } , "
  1099. " { \"ip-address\": \"127.0.3.1\" } "
  1100. " ] } "
  1101. ", "
  1102. "{ \"name\": \" 0.247.106.in.addr.arpa.\" , "
  1103. " \"key-name\": \"d2_key.billcat.net\" , "
  1104. " \"dns-servers\" : [ "
  1105. " { \"ip-address\": \"127.0.4.1\" }, "
  1106. " { \"ip-address\": \"127.0.5.1\" } , "
  1107. " { \"ip-address\": \"127.0.6.1\" } "
  1108. " ] } "
  1109. "] } }";
  1110. ASSERT_TRUE(fromJSON(config));
  1111. // Verify that we can parse the configuration.
  1112. answer_ = cfg_mgr_->parseConfig(config_set_);
  1113. ASSERT_TRUE(checkAnswer(0));
  1114. // Verify that the D2 context can be retrieved and is not null.
  1115. D2CfgContextPtr context;
  1116. ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());
  1117. // Verify that the global scalars have the proper values.
  1118. D2ParamsPtr& d2_params = context->getD2Params();
  1119. ASSERT_TRUE(d2_params);
  1120. EXPECT_EQ(isc::asiolink::IOAddress("192.168.1.33"),
  1121. d2_params->getIpAddress());
  1122. EXPECT_EQ(88, d2_params->getPort());
  1123. EXPECT_EQ(333, d2_params->getDnsServerTimeout());
  1124. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_params->getNcrProtocol());
  1125. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_params->getNcrFormat());
  1126. // Verify that the forward manager can be retrieved.
  1127. DdnsDomainListMgrPtr mgr = context->getForwardMgr();
  1128. ASSERT_TRUE(mgr);
  1129. // Verify that the forward manager has the correct number of domains.
  1130. DdnsDomainMapPtr domains = mgr->getDomains();
  1131. ASSERT_TRUE(domains);
  1132. int count = domains->size();
  1133. EXPECT_EQ(2, count);
  1134. // Verify that the server count in each of the forward manager domains.
  1135. // NOTE that since prior tests have validated server parsing, we are are
  1136. // assuming that the servers did in fact parse correctly if the correct
  1137. // number of them are there.
  1138. DdnsDomainMapPair domain_pair;
  1139. BOOST_FOREACH(domain_pair, (*domains)) {
  1140. DdnsDomainPtr domain = domain_pair.second;
  1141. DnsServerInfoStoragePtr servers = domain->getServers();
  1142. count = servers->size();
  1143. EXPECT_TRUE(servers);
  1144. EXPECT_EQ(3, count);
  1145. }
  1146. // Verify that the reverse manager can be retrieved.
  1147. mgr = context->getReverseMgr();
  1148. ASSERT_TRUE(mgr);
  1149. // Verify that the reverse manager has the correct number of domains.
  1150. domains = mgr->getDomains();
  1151. count = domains->size();
  1152. EXPECT_EQ(2, count);
  1153. // Verify that the server count in each of the reverse manager domains.
  1154. // NOTE that since prior tests have validated server parsing, we are are
  1155. // assuming that the servers did in fact parse correctly if the correct
  1156. // number of them are there.
  1157. BOOST_FOREACH(domain_pair, (*domains)) {
  1158. DdnsDomainPtr domain = domain_pair.second;
  1159. DnsServerInfoStoragePtr servers = domain->getServers();
  1160. count = servers->size();
  1161. EXPECT_TRUE(servers);
  1162. EXPECT_EQ(3, count);
  1163. }
  1164. // Test directional update flags.
  1165. EXPECT_TRUE(cfg_mgr_->forwardUpdatesEnabled());
  1166. EXPECT_TRUE(cfg_mgr_->reverseUpdatesEnabled());
  1167. // Verify that parsing the exact same configuration a second time
  1168. // does not cause a duplicate value errors.
  1169. answer_ = cfg_mgr_->parseConfig(config_set_);
  1170. ASSERT_TRUE(checkAnswer(0));
  1171. }
  1172. /// @brief Tests the basics of the D2CfgMgr FQDN-domain matching
  1173. /// This test uses a valid configuration to exercise the D2CfgMgr
  1174. /// forward FQDN-to-domain matching.
  1175. /// It verifies that:
  1176. /// 1. Given an FQDN which exactly matches a domain's name, that domain is
  1177. /// returned as match.
  1178. /// 2. Given a FQDN for sub-domain in the list, returns the proper match.
  1179. /// 3. Given a FQDN that matches no domain name, returns the wild card domain
  1180. /// as a match.
  1181. TEST_F(D2CfgMgrTest, forwardMatch) {
  1182. // Create configuration with one domain, one sub domain, and the wild
  1183. // card.
  1184. std::string config = "{ "
  1185. "\"ip-address\" : \"192.168.1.33\" , "
  1186. "\"port\" : 88 , "
  1187. "\"tsig-keys\": [] ,"
  1188. "\"forward-ddns\" : {"
  1189. "\"ddns-domains\": [ "
  1190. "{ \"name\": \"tmark.org\" , "
  1191. " \"dns-servers\" : [ "
  1192. " { \"ip-address\": \"127.0.0.1\" } "
  1193. " ] } "
  1194. ", "
  1195. "{ \"name\": \"one.tmark.org\" , "
  1196. " \"dns-servers\" : [ "
  1197. " { \"ip-address\": \"127.0.0.2\" } "
  1198. " ] } "
  1199. ", "
  1200. "{ \"name\": \"*\" , "
  1201. " \"dns-servers\" : [ "
  1202. " { \"ip-address\": \"127.0.0.3\" } "
  1203. " ] } "
  1204. "] }, "
  1205. "\"reverse-ddns\" : {} "
  1206. "}";
  1207. ASSERT_TRUE(fromJSON(config));
  1208. // Verify that we can parse the configuration.
  1209. answer_ = cfg_mgr_->parseConfig(config_set_);
  1210. ASSERT_TRUE(checkAnswer(0));
  1211. // Verify that the D2 context can be retrieved and is not null.
  1212. D2CfgContextPtr context;
  1213. ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());
  1214. // Test directional update flags.
  1215. EXPECT_TRUE(cfg_mgr_->forwardUpdatesEnabled());
  1216. EXPECT_FALSE(cfg_mgr_->reverseUpdatesEnabled());
  1217. DdnsDomainPtr match;
  1218. // Verify that an exact match works.
  1219. EXPECT_TRUE(cfg_mgr_->matchForward("tmark.org", match));
  1220. EXPECT_EQ("tmark.org", match->getName());
  1221. // Verify that search is case insensisitive.
  1222. EXPECT_TRUE(cfg_mgr_->matchForward("TMARK.ORG", match));
  1223. EXPECT_EQ("tmark.org", match->getName());
  1224. // Verify that an exact match works.
  1225. EXPECT_TRUE(cfg_mgr_->matchForward("one.tmark.org", match));
  1226. EXPECT_EQ("one.tmark.org", match->getName());
  1227. // Verify that a FQDN for sub-domain matches.
  1228. EXPECT_TRUE(cfg_mgr_->matchForward("blue.tmark.org", match));
  1229. EXPECT_EQ("tmark.org", match->getName());
  1230. // Verify that a FQDN for sub-domain matches.
  1231. EXPECT_TRUE(cfg_mgr_->matchForward("red.one.tmark.org", match));
  1232. EXPECT_EQ("one.tmark.org", match->getName());
  1233. // Verify that an FQDN with no match, returns the wild card domain.
  1234. EXPECT_TRUE(cfg_mgr_->matchForward("shouldbe.wildcard", match));
  1235. EXPECT_EQ("*", match->getName());
  1236. // Verify that an attempt to match an empty FQDN throws.
  1237. ASSERT_THROW(cfg_mgr_->matchForward("", match), D2CfgError);
  1238. }
  1239. /// @brief Tests domain matching when there is no wild card domain.
  1240. /// This test verifies that matches are found only for FQDNs that match
  1241. /// some or all of a domain name. FQDNs without matches should not return
  1242. /// a match.
  1243. TEST_F(D2CfgMgrTest, matchNoWildcard) {
  1244. // Create a configuration with one domain, one sub-domain, and NO wild card.
  1245. std::string config = "{ "
  1246. "\"ip-address\" : \"192.168.1.33\" , "
  1247. "\"port\" : 88 , "
  1248. "\"tsig-keys\": [] ,"
  1249. "\"forward-ddns\" : {"
  1250. "\"ddns-domains\": [ "
  1251. "{ \"name\": \"tmark.org\" , "
  1252. " \"dns-servers\" : [ "
  1253. " { \"ip-address\": \"127.0.0.1\" } "
  1254. " ] } "
  1255. ", "
  1256. "{ \"name\": \"one.tmark.org\" , "
  1257. " \"dns-servers\" : [ "
  1258. " { \"ip-address\": \"127.0.0.2\" } "
  1259. " ] } "
  1260. "] }, "
  1261. "\"reverse-ddns\" : {} "
  1262. " }";
  1263. ASSERT_TRUE(fromJSON(config));
  1264. // Verify that we can parse the configuration.
  1265. answer_ = cfg_mgr_->parseConfig(config_set_);
  1266. ASSERT_TRUE(checkAnswer(0));
  1267. // Verify that the D2 context can be retrieved and is not null.
  1268. D2CfgContextPtr context;
  1269. ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());
  1270. DdnsDomainPtr match;
  1271. // Verify that full or partial matches, still match.
  1272. EXPECT_TRUE(cfg_mgr_->matchForward("tmark.org", match));
  1273. EXPECT_EQ("tmark.org", match->getName());
  1274. EXPECT_TRUE(cfg_mgr_->matchForward("blue.tmark.org", match));
  1275. EXPECT_EQ("tmark.org", match->getName());
  1276. EXPECT_TRUE(cfg_mgr_->matchForward("red.one.tmark.org", match));
  1277. EXPECT_EQ("one.tmark.org", match->getName());
  1278. // Verify that a FQDN with no match, fails to match.
  1279. EXPECT_FALSE(cfg_mgr_->matchForward("shouldbe.wildcard", match));
  1280. }
  1281. /// @brief Tests domain matching when there is ONLY a wild card domain.
  1282. /// This test verifies that any FQDN matches the wild card.
  1283. TEST_F(D2CfgMgrTest, matchAll) {
  1284. std::string config = "{ "
  1285. "\"ip-address\" : \"192.168.1.33\" , "
  1286. "\"port\" : 88 , "
  1287. "\"tsig-keys\": [] ,"
  1288. "\"forward-ddns\" : {"
  1289. "\"ddns-domains\": [ "
  1290. "{ \"name\": \"*\" , "
  1291. " \"dns-servers\" : [ "
  1292. " { \"ip-address\": \"127.0.0.1\" } "
  1293. " ] } "
  1294. "] }, "
  1295. "\"reverse-ddns\" : {} "
  1296. "}";
  1297. ASSERT_TRUE(fromJSON(config));
  1298. // Verify that we can parse the configuration.
  1299. answer_ = cfg_mgr_->parseConfig(config_set_);
  1300. ASSERT_TRUE(checkAnswer(0));
  1301. // Verify that the D2 context can be retrieved and is not null.
  1302. D2CfgContextPtr context;
  1303. ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());
  1304. // Verify that wild card domain is returned for any FQDN.
  1305. DdnsDomainPtr match;
  1306. EXPECT_TRUE(cfg_mgr_->matchForward("tmark.org", match));
  1307. EXPECT_EQ("*", match->getName());
  1308. EXPECT_TRUE(cfg_mgr_->matchForward("shouldbe.wildcard", match));
  1309. EXPECT_EQ("*", match->getName());
  1310. // Verify that an attempt to match an empty FQDN still throws.
  1311. ASSERT_THROW(cfg_mgr_->matchReverse("", match), D2CfgError);
  1312. }
  1313. /// @brief Tests the basics of the D2CfgMgr reverse FQDN-domain matching
  1314. /// This test uses a valid configuration to exercise the D2CfgMgr's
  1315. /// reverse FQDN-to-domain matching.
  1316. /// It verifies that:
  1317. /// 1. Given an FQDN which exactly matches a domain's name, that domain is
  1318. /// returned as match.
  1319. /// 2. Given a FQDN for sub-domain in the list, returns the proper match.
  1320. /// 3. Given a FQDN that matches no domain name, returns the wild card domain
  1321. /// as a match.
  1322. TEST_F(D2CfgMgrTest, matchReverse) {
  1323. std::string config = "{ "
  1324. "\"ip-address\" : \"192.168.1.33\" , "
  1325. "\"port\" : 88 , "
  1326. "\"tsig-keys\": [] ,"
  1327. "\"forward-ddns\" : {}, "
  1328. "\"reverse-ddns\" : {"
  1329. "\"ddns-domains\": [ "
  1330. "{ \"name\": \"5.100.168.192.in-addr.arpa.\" , "
  1331. " \"dns-servers\" : [ "
  1332. " { \"ip-address\": \"127.0.0.1\" } "
  1333. " ] }, "
  1334. "{ \"name\": \"100.200.192.in-addr.arpa.\" , "
  1335. " \"dns-servers\" : [ "
  1336. " { \"ip-address\": \"127.0.0.1\" } "
  1337. " ] }, "
  1338. "{ \"name\": \"170.192.in-addr.arpa.\" , "
  1339. " \"dns-servers\" : [ "
  1340. " { \"ip-address\": \"127.0.0.1\" } "
  1341. " ] }, "
  1342. // Note mixed case to test case insensitivity.
  1343. "{ \"name\": \"2.0.3.0.8.b.d.0.1.0.0.2.IP6.ARPA.\" , "
  1344. " \"dns-servers\" : [ "
  1345. " { \"ip-address\": \"127.0.0.1\" } "
  1346. " ] },"
  1347. "{ \"name\": \"*\" , "
  1348. " \"dns-servers\" : [ "
  1349. " { \"ip-address\": \"127.0.0.1\" } "
  1350. " ] } "
  1351. "] } }";
  1352. ASSERT_TRUE(fromJSON(config));
  1353. // Verify that we can parse the configuration.
  1354. answer_ = cfg_mgr_->parseConfig(config_set_);
  1355. ASSERT_TRUE(checkAnswer(0));
  1356. // Verify that the D2 context can be retrieved and is not null.
  1357. D2CfgContextPtr context;
  1358. ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());
  1359. // Test directional update flags.
  1360. EXPECT_FALSE(cfg_mgr_->forwardUpdatesEnabled());
  1361. EXPECT_TRUE(cfg_mgr_->reverseUpdatesEnabled());
  1362. DdnsDomainPtr match;
  1363. // Verify an exact match.
  1364. EXPECT_TRUE(cfg_mgr_->matchReverse("192.168.100.5", match));
  1365. EXPECT_EQ("5.100.168.192.in-addr.arpa.", match->getName());
  1366. // Verify a sub-domain match.
  1367. EXPECT_TRUE(cfg_mgr_->matchReverse("192.200.100.27", match));
  1368. EXPECT_EQ("100.200.192.in-addr.arpa.", match->getName());
  1369. // Verify a sub-domain match.
  1370. EXPECT_TRUE(cfg_mgr_->matchReverse("192.170.50.30", match));
  1371. EXPECT_EQ("170.192.in-addr.arpa.", match->getName());
  1372. // Verify a wild card match.
  1373. EXPECT_TRUE(cfg_mgr_->matchReverse("1.1.1.1", match));
  1374. EXPECT_EQ("*", match->getName());
  1375. // Verify a IPv6 match.
  1376. EXPECT_TRUE(cfg_mgr_->matchReverse("2001:db8:302:99::",match));
  1377. EXPECT_EQ("2.0.3.0.8.b.d.0.1.0.0.2.IP6.ARPA.", match->getName());
  1378. // Verify a IPv6 wild card match.
  1379. EXPECT_TRUE(cfg_mgr_->matchReverse("2001:db8:99:302::",match));
  1380. EXPECT_EQ("*", match->getName());
  1381. // Verify that an attempt to match an invalid IP address throws.
  1382. ASSERT_THROW(cfg_mgr_->matchReverse("", match), D2CfgError);
  1383. }
  1384. /// @brief Tests D2 config parsing against a wide range of config permutations.
  1385. /// It iterates over all of the test configurations described in given file.
  1386. /// The file content is JSON specialized to this test. The format of the file
  1387. /// is:
  1388. ///
  1389. /// @code
  1390. /// # The file must open with a list. It's name is arbitrary.
  1391. ///
  1392. /// { "test_list" :
  1393. /// [
  1394. ///
  1395. /// # Test one starts here:
  1396. /// {
  1397. ///
  1398. /// # Each test has:
  1399. /// # 1. description - optional text description
  1400. /// # 2. should-fail - bool indicator if parsing is expected to file
  1401. /// # (defaults to false)
  1402. /// # 3. data - configuration text to parse
  1403. /// #
  1404. /// "description" : "<text describing test>",
  1405. /// "should_fail" : <true|false> ,
  1406. /// "data" :
  1407. /// {
  1408. /// # configuration elements here
  1409. /// "bool_val" : false,
  1410. /// "some_map" : {}
  1411. /// # :
  1412. /// }
  1413. /// }
  1414. ///
  1415. /// # Next test would start here
  1416. /// ,
  1417. /// {
  1418. /// }
  1419. ///
  1420. /// ]}
  1421. ///
  1422. /// @endcode
  1423. ///
  1424. /// (The file supports comments per Element::fromJSONFile())
  1425. ///
  1426. TEST_F(D2CfgMgrTest, configPermutations) {
  1427. std::string test_file = testDataFile("d2_cfg_tests.json");
  1428. isc::data::ConstElementPtr tests;
  1429. // Read contents of the file and parse it as JSON. Note it must contain
  1430. // all valid JSON, we aren't testing JSON parsing.
  1431. try {
  1432. tests = isc::data::Element::fromJSONFile(test_file, true);
  1433. } catch (const std::exception& ex) {
  1434. FAIL() << "ERROR parsing file : " << test_file << " : " << ex.what();
  1435. }
  1436. // Read in each test For each test, read:
  1437. // 1. description - optional text description
  1438. // 2. should-fail - bool indicator if parsing is expected to file (defaults
  1439. // to false
  1440. // 3. data - configuration text to parse
  1441. //
  1442. // Next attempt to parse the configuration by passing it into
  1443. // D2CfgMgr::parseConfig(). Then check the parsing outcome against the
  1444. // expected outcome as given by should-fail.
  1445. isc::data::ConstElementPtr test;
  1446. ASSERT_TRUE(tests->get("test-list"));
  1447. BOOST_FOREACH(test, tests->get("test-list")->listValue()) {
  1448. // Grab the description.
  1449. std::string description = "<no desc>";
  1450. isc::data::ConstElementPtr elem = test->get("description");
  1451. if (elem) {
  1452. elem->getValue(description);
  1453. }
  1454. // Grab the outcome flag, should-fail, defaults to false if it's
  1455. // not specified.
  1456. bool should_fail = false;
  1457. elem = test->get("should-fail");
  1458. if (elem) {
  1459. elem->getValue(should_fail);
  1460. }
  1461. // Grab the test's configuration data.
  1462. isc::data::ConstElementPtr data = test->get("data");
  1463. ASSERT_TRUE(data) << "No data for test: "
  1464. << " : " << test->getPosition();
  1465. // Attempt to parse the configuration. We verify that we get the expected
  1466. // outcome, and if it was supposed to fail if the explanation contains
  1467. // position information.
  1468. checkAnswerWithError(cfg_mgr_->parseConfig(data),
  1469. (should_fail ? SHOULD_FAIL : SHOULD_PASS),
  1470. test_file);
  1471. }
  1472. }
  1473. } // end of anonymous namespace