d2_client_unittest.cc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 <dhcp/option4_client_fqdn.h>
  16. #include <dhcp/option6_client_fqdn.h>
  17. #include <dhcpsrv/d2_client_mgr.h>
  18. #include <exceptions/exceptions.h>
  19. #include <gtest/gtest.h>
  20. using namespace std;
  21. using namespace isc::asiolink;
  22. using namespace isc::dhcp;
  23. using namespace isc::util;
  24. using namespace isc;
  25. namespace {
  26. /// @brief Checks constructors and accessors of D2ClientConfig.
  27. TEST(D2ClientConfigTest, constructorsAndAccessors) {
  28. D2ClientConfigPtr d2_client_config;
  29. // Verify default constructor creates a disabled instance.
  30. ASSERT_NO_THROW(d2_client_config.reset(new D2ClientConfig()));
  31. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  32. // Verify the enable-updates can be toggled.
  33. d2_client_config->enableUpdates(true);
  34. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  35. d2_client_config->enableUpdates(false);
  36. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  37. d2_client_config.reset();
  38. bool enable_updates = true;
  39. isc::asiolink::IOAddress server_ip("127.0.0.1");
  40. size_t server_port = 477;
  41. dhcp_ddns::NameChangeProtocol ncr_protocol = dhcp_ddns::NCR_UDP;
  42. dhcp_ddns::NameChangeFormat ncr_format = dhcp_ddns::FMT_JSON;
  43. bool always_include_fqdn = true;
  44. bool override_no_update = true;
  45. bool override_client_update = true;
  46. bool replace_client_name = true;
  47. std::string generated_prefix = "the_prefix";
  48. std::string qualifying_suffix = "the.suffix.";
  49. // Verify that we can construct a valid, enabled instance.
  50. ASSERT_NO_THROW(d2_client_config.reset(new
  51. D2ClientConfig(enable_updates,
  52. server_ip,
  53. server_port,
  54. ncr_protocol,
  55. ncr_format,
  56. always_include_fqdn,
  57. override_no_update,
  58. override_client_update,
  59. replace_client_name,
  60. generated_prefix,
  61. qualifying_suffix)));
  62. ASSERT_TRUE(d2_client_config);
  63. // Verify that the accessors return the expected values.
  64. EXPECT_EQ(d2_client_config->getEnableUpdates(), enable_updates);
  65. EXPECT_EQ(d2_client_config->getServerIp(), server_ip);
  66. EXPECT_EQ(d2_client_config->getServerPort(), server_port);
  67. EXPECT_EQ(d2_client_config->getNcrProtocol(), ncr_protocol);
  68. EXPECT_EQ(d2_client_config->getNcrFormat(), ncr_format);
  69. EXPECT_EQ(d2_client_config->getAlwaysIncludeFqdn(), always_include_fqdn);
  70. EXPECT_EQ(d2_client_config->getOverrideNoUpdate(), override_no_update);
  71. EXPECT_EQ(d2_client_config->getOverrideClientUpdate(),
  72. override_client_update);
  73. EXPECT_EQ(d2_client_config->getReplaceClientName(), replace_client_name);
  74. EXPECT_EQ(d2_client_config->getGeneratedPrefix(), generated_prefix);
  75. EXPECT_EQ(d2_client_config->getQualifyingSuffix(), qualifying_suffix);
  76. // Verify that toText called by << operator doesn't bomb.
  77. ASSERT_NO_THROW(std::cout << "toText test:" << std::endl <<
  78. *d2_client_config << std::endl);
  79. // Verify that constructor does not allow use of NCR_TCP.
  80. /// @todo obviously this becomes invalid once TCP is supported.
  81. ASSERT_THROW(d2_client_config.reset(new
  82. D2ClientConfig(enable_updates,
  83. server_ip,
  84. server_port,
  85. dhcp_ddns::NCR_TCP,
  86. ncr_format,
  87. always_include_fqdn,
  88. override_no_update,
  89. override_client_update,
  90. replace_client_name,
  91. generated_prefix,
  92. qualifying_suffix)),
  93. D2ClientError);
  94. /// @todo if additional validation is added to ctor, this test needs to
  95. /// expand accordingly.
  96. }
  97. /// @brief Tests the equality and inequality operators of D2ClientConfig.
  98. TEST(D2ClientConfigTest, equalityOperator) {
  99. D2ClientConfigPtr ref_config;
  100. D2ClientConfigPtr test_config;
  101. isc::asiolink::IOAddress ref_address("127.0.0.1");
  102. isc::asiolink::IOAddress test_address("127.0.0.2");
  103. // Create an instance to use as a reference.
  104. ASSERT_NO_THROW(ref_config.reset(new D2ClientConfig(true,
  105. ref_address, 477,
  106. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  107. true, true, true, true,
  108. "pre-fix", "suf-fix")));
  109. ASSERT_TRUE(ref_config);
  110. // Check a configuration that is identical to reference configuration.
  111. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  112. ref_address, 477,
  113. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  114. true, true, true, true,
  115. "pre-fix", "suf-fix")));
  116. ASSERT_TRUE(test_config);
  117. EXPECT_TRUE(*ref_config == *test_config);
  118. EXPECT_FALSE(*ref_config != *test_config);
  119. // Check a configuration that differs only by enable flag.
  120. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(false,
  121. ref_address, 477,
  122. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  123. true, true, true, true,
  124. "pre-fix", "suf-fix")));
  125. ASSERT_TRUE(test_config);
  126. EXPECT_FALSE(*ref_config == *test_config);
  127. EXPECT_TRUE(*ref_config != *test_config);
  128. // Check a configuration that differs only by server ip.
  129. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  130. test_address, 477,
  131. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  132. true, true, true, true,
  133. "pre-fix", "suf-fix")));
  134. ASSERT_TRUE(test_config);
  135. EXPECT_FALSE(*ref_config == *test_config);
  136. EXPECT_TRUE(*ref_config != *test_config);
  137. // Check a configuration that differs only by server port.
  138. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  139. ref_address, 333,
  140. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  141. true, true, true, true,
  142. "pre-fix", "suf-fix")));
  143. ASSERT_TRUE(test_config);
  144. EXPECT_FALSE(*ref_config == *test_config);
  145. EXPECT_TRUE(*ref_config != *test_config);
  146. // Check a configuration that differs only by always_include_fqdn.
  147. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  148. ref_address, 477,
  149. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  150. false, true, true, true,
  151. "pre-fix", "suf-fix")));
  152. ASSERT_TRUE(test_config);
  153. EXPECT_FALSE(*ref_config == *test_config);
  154. EXPECT_TRUE(*ref_config != *test_config);
  155. // Check a configuration that differs only by override_no_update.
  156. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  157. ref_address, 477,
  158. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  159. true, false, true, true,
  160. "pre-fix", "suf-fix")));
  161. ASSERT_TRUE(test_config);
  162. EXPECT_FALSE(*ref_config == *test_config);
  163. EXPECT_TRUE(*ref_config != *test_config);
  164. // Check a configuration that differs only by override_client_update.
  165. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  166. ref_address, 477,
  167. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  168. true, true, false, true,
  169. "pre-fix", "suf-fix")));
  170. ASSERT_TRUE(test_config);
  171. EXPECT_FALSE(*ref_config == *test_config);
  172. EXPECT_TRUE(*ref_config != *test_config);
  173. // Check a configuration that differs only by replace_client_name.
  174. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  175. ref_address, 477,
  176. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  177. true, true, true, false,
  178. "pre-fix", "suf-fix")));
  179. ASSERT_TRUE(test_config);
  180. EXPECT_FALSE(*ref_config == *test_config);
  181. EXPECT_TRUE(*ref_config != *test_config);
  182. // Check a configuration that differs only by generated_prefix.
  183. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  184. ref_address, 477,
  185. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  186. true, true, true, true,
  187. "bogus", "suf-fix")));
  188. ASSERT_TRUE(test_config);
  189. EXPECT_FALSE(*ref_config == *test_config);
  190. EXPECT_TRUE(*ref_config != *test_config);
  191. // Check a configuration that differs only by qualifying_suffix.
  192. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  193. ref_address, 477,
  194. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  195. true, true, true, true,
  196. "pre-fix", "bogus")));
  197. ASSERT_TRUE(test_config);
  198. EXPECT_FALSE(*ref_config == *test_config);
  199. EXPECT_TRUE(*ref_config != *test_config);
  200. }
  201. /// @brief Checks the D2ClientMgr constructor.
  202. TEST(D2ClientMgr, constructor) {
  203. D2ClientMgrPtr d2_client_mgr;
  204. // Verify we can construct with the default constructor.
  205. ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));
  206. // After construction, D2 configuration should be disabled.
  207. // Fetch it and verify this is the case.
  208. D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
  209. ASSERT_TRUE(original_config);
  210. EXPECT_FALSE(original_config->getEnableUpdates());
  211. // Make sure convenience method agrees.
  212. EXPECT_FALSE(d2_client_mgr->ddnsEnabled());
  213. }
  214. /// @brief Checks passing the D2ClientMgr a valid D2 client configuration.
  215. /// @todo Once NameChangeSender is integrated, this test needs to expand, and
  216. /// additional scenario tests will need to be written.
  217. TEST(D2ClientMgr, validConfig) {
  218. D2ClientMgrPtr d2_client_mgr;
  219. // Construct the manager and fetch its initial configuration.
  220. ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));
  221. D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
  222. ASSERT_TRUE(original_config);
  223. // Verify that we cannot set the config to an empty pointer.
  224. D2ClientConfigPtr new_cfg;
  225. ASSERT_THROW(d2_client_mgr->setD2ClientConfig(new_cfg), D2ClientError);
  226. // Create a new, enabled config.
  227. ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig(true,
  228. isc::asiolink::IOAddress("127.0.0.1"), 477,
  229. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  230. true, true, true, true,
  231. "pre-fix", "suf-fix")));
  232. // Verify that we can assign a new, non-empty configuration.
  233. ASSERT_NO_THROW(d2_client_mgr->setD2ClientConfig(new_cfg));
  234. // Verify that we can fetch the newly assigned configuration.
  235. D2ClientConfigPtr updated_config = d2_client_mgr->getD2ClientConfig();
  236. ASSERT_TRUE(updated_config);
  237. EXPECT_TRUE(updated_config->getEnableUpdates());
  238. // Make sure convenience method agrees with the updated configuration.
  239. EXPECT_TRUE(d2_client_mgr->ddnsEnabled());
  240. // Make sure the configuration we fetched is the one we assigned,
  241. // and not the original configuration.
  242. EXPECT_EQ(*new_cfg, *updated_config);
  243. EXPECT_NE(*original_config, *updated_config);
  244. }
  245. /// @brief Tests that analyzeFqdn detects invalid combination of both the
  246. /// client S and N flags set to true.
  247. TEST(D2ClientMgr, analyzeFqdnInvalidCombination) {
  248. D2ClientMgr mgr;
  249. bool server_s = false;
  250. bool server_n = false;
  251. // Create disabled configuration.
  252. D2ClientConfigPtr cfg;
  253. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig()));
  254. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  255. ASSERT_FALSE(mgr.ddnsEnabled());
  256. // client S=1 N=1 is invalid. analyzeFqdn should throw.
  257. ASSERT_THROW(mgr.analyzeFqdn(true, true, server_s, server_n),
  258. isc::BadValue);
  259. // Create enabled configuration with all controls off (no overrides).
  260. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  261. isc::asiolink::IOAddress("127.0.0.1"), 477,
  262. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  263. false, false, false, false,
  264. "pre-fix", "suf-fix")));
  265. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  266. ASSERT_TRUE(mgr.ddnsEnabled());
  267. // client S=1 N=1 is invalid. analyzeFqdn should throw.
  268. ASSERT_THROW(mgr.analyzeFqdn(true, true, server_s, server_n),
  269. isc::BadValue);
  270. }
  271. /// @brief Tests that analyzeFqdn generates correct server S and N flags when
  272. /// updates are enabled and all overrides are off.
  273. TEST(D2ClientMgr, analyzeFqdnEnabledNoOverrides) {
  274. D2ClientMgr mgr;
  275. bool server_s = false;
  276. bool server_n = false;
  277. // Create enabled configuration with all controls off (no overrides).
  278. D2ClientConfigPtr cfg;
  279. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  280. isc::asiolink::IOAddress("127.0.0.1"), 477,
  281. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  282. false, false, false, false,
  283. "pre-fix", "suf-fix")));
  284. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  285. ASSERT_TRUE(mgr.ddnsEnabled());
  286. ASSERT_FALSE(cfg->getOverrideClientUpdate());
  287. ASSERT_FALSE(cfg->getOverrideNoUpdate());
  288. // client S=0 N=0 means client wants to do forward update.
  289. // server S should be 0 (server is not doing forward updates)
  290. // and server N should be 0 (server doing reverse updates)
  291. mgr.analyzeFqdn(false, false, server_s, server_n);
  292. EXPECT_FALSE(server_s);
  293. EXPECT_FALSE(server_n);
  294. // client S=1 N=0 means client wants server to do forward update.
  295. // server S should be 1 (server is doing forward updates)
  296. // and server N should be 0 (server doing updates)
  297. mgr.analyzeFqdn(true, false, server_s, server_n);
  298. EXPECT_TRUE(server_s);
  299. EXPECT_FALSE(server_n);
  300. // client S=0 N=1 means client wants no one to do forward updates.
  301. // server S should be 0 (server is not forward updates)
  302. // and server N should be 1 (server is not doing any updates)
  303. mgr.analyzeFqdn(false, true, server_s, server_n);
  304. EXPECT_FALSE(server_s);
  305. EXPECT_TRUE(server_n);
  306. }
  307. /// @brief Tests that analyzeFqdn generates correct server S and N flags when
  308. /// updates are enabled and override-no-update is on.
  309. TEST(D2ClientMgr, analyzeFqdnEnabledOverrideNoUpdate) {
  310. D2ClientMgr mgr;
  311. bool server_s = false;
  312. bool server_n = false;
  313. // Create enabled configuration with OVERRIDE_NO_UPDATE on.
  314. D2ClientConfigPtr cfg;
  315. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  316. isc::asiolink::IOAddress("127.0.0.1"), 477,
  317. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  318. false, true, false, false,
  319. "pre-fix", "suf-fix")));
  320. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  321. ASSERT_TRUE(mgr.ddnsEnabled());
  322. ASSERT_TRUE(cfg->getOverrideNoUpdate());
  323. ASSERT_FALSE(cfg->getOverrideClientUpdate());
  324. // client S=0 N=0 means client wants to do forward update.
  325. // server S should be 0 (server is not doing forward updates)
  326. // and server N should be 0 (server is doing reverse updates)
  327. mgr.analyzeFqdn(false, false, server_s, server_n);
  328. EXPECT_FALSE(server_s);
  329. EXPECT_FALSE(server_n);
  330. // client S=1 N=0 means client wants server to do forward update.
  331. // server S should be 1 (server is doing forward updates)
  332. // and server N should be 0 (server doing updates)
  333. mgr.analyzeFqdn(true, false, server_s, server_n);
  334. EXPECT_TRUE(server_s);
  335. EXPECT_FALSE(server_n);
  336. // client S=0 N=1 means client wants no one to do forward updates.
  337. // server S should be 1 (server is doing forward updates)
  338. // and server N should be 0 (server is doing updates)
  339. mgr.analyzeFqdn(false, true, server_s, server_n);
  340. EXPECT_TRUE(server_s);
  341. EXPECT_FALSE(server_n);
  342. }
  343. /// @brief Tests that analyzeFqdn generates correct server S and N flags when
  344. /// updates are enabled and override-client-update is on.
  345. TEST(D2ClientMgr, analyzeFqdnEnabledOverrideClientUpdate) {
  346. D2ClientMgr mgr;
  347. bool server_s = false;
  348. bool server_n = false;
  349. // Create enabled configuration with OVERRIDE_CLIENT_UPDATE on.
  350. D2ClientConfigPtr cfg;
  351. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  352. isc::asiolink::IOAddress("127.0.0.1"), 477,
  353. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  354. false, false, true, false,
  355. "pre-fix", "suf-fix")));
  356. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  357. ASSERT_TRUE(mgr.ddnsEnabled());
  358. ASSERT_FALSE(cfg->getOverrideNoUpdate());
  359. ASSERT_TRUE(cfg->getOverrideClientUpdate());
  360. // client S=0 N=0 means client wants to do forward update.
  361. // server S should be 1 (server is doing forward updates)
  362. // and server N should be 0 (server doing updates)
  363. mgr.analyzeFqdn(false, false, server_s, server_n);
  364. EXPECT_TRUE(server_s);
  365. EXPECT_FALSE(server_n);
  366. // client S=1 N=0 means client wants server to do forward update.
  367. // server S should be 1 (server is doing forward updates)
  368. // and server N should be 0 (server doing updates)
  369. mgr.analyzeFqdn(true, false, server_s, server_n);
  370. EXPECT_TRUE(server_s);
  371. EXPECT_FALSE(server_n);
  372. // client S=0 N=1 means client wants no one to do forward updates.
  373. // server S should be 0 (server is not forward updates)
  374. // and server N should be 1 (server is not doing any updates)
  375. mgr.analyzeFqdn(false, true, server_s, server_n);
  376. EXPECT_FALSE(server_s);
  377. EXPECT_TRUE(server_n);
  378. }
  379. /// @brief Verifies the adustFqdnFlags template with Option4ClientFqdn objects.
  380. /// Ensures that the method can set the N, S, and O flags properly.
  381. /// Other permutations are covered by analyzeFqdnFlag tests.
  382. TEST(D2ClientMgr, adjustFqdnFlagsV4) {
  383. D2ClientMgr mgr;
  384. Option4ClientFqdnPtr request;
  385. Option4ClientFqdnPtr response;
  386. // Create enabled configuration and override-no-update on.
  387. D2ClientConfigPtr cfg;
  388. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  389. isc::asiolink::IOAddress("127.0.0.1"), 477,
  390. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  391. false, true, false, false,
  392. "pre-fix", "suf-fix")));
  393. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  394. ASSERT_TRUE(mgr.ddnsEnabled());
  395. ASSERT_TRUE(cfg->getOverrideNoUpdate());
  396. ASSERT_FALSE(cfg->getOverrideClientUpdate());
  397. // client S=0 N=0 means client wants to do forward update.
  398. // server S should be 0 (server is not doing forward updates)
  399. // and server N should be 0 (server is doing reverse updates)
  400. // and server O should be 0
  401. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  402. "", Option4ClientFqdn::PARTIAL));
  403. response.reset(new Option4ClientFqdn(*request));
  404. response->resetFlags();
  405. mgr.adjustFqdnFlags<Option4ClientFqdn>(*request, *response);
  406. EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_S));
  407. EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
  408. EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_O));
  409. // client S=1 N=0 means client wants server to do forward update.
  410. // server S should be 1 (server is doing forward updates)
  411. // and server N should be 0 (server doing updates)
  412. // and server O should be 0
  413. request.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S,
  414. Option4ClientFqdn::RCODE_CLIENT(),
  415. "", Option4ClientFqdn::PARTIAL));
  416. response.reset(new Option4ClientFqdn(*request));
  417. response->resetFlags();
  418. mgr.adjustFqdnFlags<Option4ClientFqdn>(*request, *response);
  419. EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_S));
  420. EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
  421. EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_O));
  422. // client S=0 N=1 means client wants no one to do updates
  423. // server S should be 1 (server is doing forward updates)
  424. // and server N should be 0 (server doing updates)
  425. // and O should be 1 (overriding client S)
  426. request.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_N,
  427. Option4ClientFqdn::RCODE_CLIENT(),
  428. "", Option4ClientFqdn::PARTIAL));
  429. response.reset(new Option4ClientFqdn(*request));
  430. response->resetFlags();
  431. mgr.adjustFqdnFlags<Option4ClientFqdn>(*request, *response);
  432. EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_S));
  433. EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
  434. EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_O));
  435. }
  436. /// @brief Verified the getUpdateDirections template method with
  437. /// Option4ClientFqdn objects.
  438. TEST(D2ClientMgr, updateDirectionsV4) {
  439. D2ClientMgr mgr;
  440. Option4ClientFqdnPtr response;
  441. bool do_forward = false;
  442. bool do_reverse = false;
  443. // Response S=0, N=0 should mean do reverse only.
  444. response.reset(new Option4ClientFqdn(0,
  445. Option4ClientFqdn::RCODE_CLIENT(),
  446. "", Option4ClientFqdn::PARTIAL));
  447. mgr.getUpdateDirections(*response, do_forward, do_reverse);
  448. EXPECT_FALSE(do_forward);
  449. EXPECT_TRUE(do_reverse);
  450. // Response S=0, N=1 should mean don't do either.
  451. response.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_N,
  452. Option4ClientFqdn::RCODE_CLIENT(),
  453. "", Option4ClientFqdn::PARTIAL));
  454. mgr.getUpdateDirections(*response, do_forward, do_reverse);
  455. EXPECT_FALSE(do_forward);
  456. EXPECT_FALSE(do_reverse);
  457. // Response S=1, N=0 should mean do both.
  458. response.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S,
  459. Option4ClientFqdn::RCODE_CLIENT(),
  460. "", Option4ClientFqdn::PARTIAL));
  461. mgr.getUpdateDirections(*response, do_forward, do_reverse);
  462. EXPECT_TRUE(do_forward);
  463. EXPECT_TRUE(do_reverse);
  464. // Response S=1, N=1 isn't possible.
  465. }
  466. /// @brief Tests the qualifyName method's ability to construct FQDNs
  467. TEST(D2ClientMgr, qualifyName) {
  468. D2ClientMgr mgr;
  469. // Create enabled configuration.
  470. D2ClientConfigPtr cfg;
  471. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  472. isc::asiolink::IOAddress("127.0.0.1"), 477,
  473. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  474. false, false, true, false,
  475. "prefix", "suffix.com")));
  476. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  477. // Verify that the qualifying suffix gets appended with trailing dot added.
  478. std::string partial_name = "somehost";
  479. std::string qualified_name = mgr.qualifyName(partial_name);
  480. EXPECT_EQ("somehost.suffix.com.", qualified_name);
  481. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  482. isc::asiolink::IOAddress("127.0.0.1"), 477,
  483. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  484. false, false, true, false,
  485. "prefix", "hasdot.com.")));
  486. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  487. // Verify that the qualifying suffix gets appended without dot added.
  488. qualified_name = mgr.qualifyName(partial_name);
  489. EXPECT_EQ("somehost.hasdot.com.", qualified_name);
  490. }
  491. /// @brief Tests the generateFdqn method's ability to construct FQDNs
  492. TEST(D2ClientMgr, generateFqdn) {
  493. D2ClientMgr mgr;
  494. // Create enabled configuration.
  495. D2ClientConfigPtr cfg;
  496. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  497. isc::asiolink::IOAddress("127.0.0.1"), 477,
  498. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  499. false, false, true, false,
  500. "prefix", "suffix.com")));
  501. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  502. // Verify that it works with an IPv4 address.
  503. asiolink::IOAddress v4address("192.0.2.75");
  504. EXPECT_EQ("prefix-192-0-2-75.suffix.com.", mgr.generateFqdn(v4address));
  505. // Verify that it works with an IPv6 address.
  506. asiolink::IOAddress v6address("2001:db8::2");
  507. EXPECT_EQ("prefix-2001-db8--2.suffix.com.", mgr.generateFqdn(v6address));
  508. // Create a disabled config.
  509. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig()));
  510. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  511. // Verify names generate properly with a disabled configuration.
  512. EXPECT_EQ("myhost-192-0-2-75.example.com.", mgr.generateFqdn(v4address));
  513. EXPECT_EQ("myhost-2001-db8--2.example.com.", mgr.generateFqdn(v6address));
  514. }
  515. /// @brief Tests adjustDomainName template method with Option4ClientFqdn
  516. TEST(D2ClientMgr, adjustDomainNameV4) {
  517. D2ClientMgr mgr;
  518. Option4ClientFqdnPtr request;
  519. Option4ClientFqdnPtr response;
  520. // Create enabled configuration.
  521. D2ClientConfigPtr cfg;
  522. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  523. isc::asiolink::IOAddress("127.0.0.1"), 477,
  524. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  525. false, false, false, false,
  526. "prefix", "suffix.com")));
  527. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  528. ASSERT_FALSE(cfg->getReplaceClientName());
  529. // replace-client-name is false, client passes in empty fqdn
  530. // reponse domain should be empty/partial.
  531. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  532. "", Option4ClientFqdn::PARTIAL));
  533. response.reset(new Option4ClientFqdn(*request));
  534. mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
  535. EXPECT_EQ("", response->getDomainName());
  536. EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());
  537. // replace-client-name is false, client passes in a partial fqdn
  538. // response should contain client's name plus the qualifying suffix.
  539. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  540. "myhost", Option4ClientFqdn::PARTIAL));
  541. response.reset(new Option4ClientFqdn(*request));
  542. mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
  543. EXPECT_EQ("myhost.suffix.com.", response->getDomainName());
  544. EXPECT_EQ(Option4ClientFqdn::FULL, response->getDomainNameType());
  545. // replace-client-name is false, client passes in a full fqdn
  546. // response domain should not be altered.
  547. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  548. "myhost.example.com.",
  549. Option4ClientFqdn::FULL));
  550. response.reset(new Option4ClientFqdn(*request));
  551. mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
  552. EXPECT_EQ("myhost.example.com.", response->getDomainName());
  553. EXPECT_EQ(Option4ClientFqdn::FULL, response->getDomainNameType());
  554. // Create enabled configuration.
  555. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  556. isc::asiolink::IOAddress("127.0.0.1"), 477,
  557. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  558. false, false, false, true,
  559. "prefix", "suffix.com")));
  560. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  561. ASSERT_TRUE(cfg->getReplaceClientName());
  562. // replace-client-name is true, client passes in empty fqdn
  563. // reponse domain should be empty/partial.
  564. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  565. "", Option4ClientFqdn::PARTIAL));
  566. response.reset(new Option4ClientFqdn(*request));
  567. mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
  568. EXPECT_EQ("", response->getDomainName());
  569. EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());
  570. // replace-client-name is true, client passes in a partial fqdn
  571. // reponse domain should be empty/partial.
  572. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  573. "myhost", Option4ClientFqdn::PARTIAL));
  574. response.reset(new Option4ClientFqdn(*request));
  575. mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
  576. EXPECT_EQ("", response->getDomainName());
  577. EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());
  578. // replace-client-name is true, client passes in a full fqdn
  579. // reponse domain should be empty/partial.
  580. request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
  581. "myhost.example.com.",
  582. Option4ClientFqdn::FULL));
  583. response.reset(new Option4ClientFqdn(*request));
  584. mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
  585. EXPECT_EQ("", response->getDomainName());
  586. EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());
  587. }
  588. /// @brief Tests adjustDomainName template method with Option6ClientFqdn
  589. TEST(D2ClientMgr, adjustDomainNameV6) {
  590. D2ClientMgr mgr;
  591. Option6ClientFqdnPtr request;
  592. Option6ClientFqdnPtr response;
  593. // Create enabled configuration.
  594. D2ClientConfigPtr cfg;
  595. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  596. isc::asiolink::IOAddress("127.0.0.1"), 477,
  597. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  598. false, false, false, false,
  599. "prefix", "suffix.com")));
  600. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  601. ASSERT_FALSE(cfg->getReplaceClientName());
  602. // replace-client-name is false, client passes in empty fqdn
  603. // reponse domain should be empty/partial.
  604. request.reset(new Option6ClientFqdn(0, "", Option6ClientFqdn::PARTIAL));
  605. response.reset(new Option6ClientFqdn(*request));
  606. mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
  607. EXPECT_EQ("", response->getDomainName());
  608. EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());
  609. // replace-client-name is false, client passes in a partial fqdn
  610. // response should contain client's name plus the qualifying suffix.
  611. request.reset(new Option6ClientFqdn(0, "myhost",
  612. Option6ClientFqdn::PARTIAL));
  613. response.reset(new Option6ClientFqdn(*request));
  614. mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
  615. EXPECT_EQ("myhost.suffix.com.", response->getDomainName());
  616. EXPECT_EQ(Option6ClientFqdn::FULL, response->getDomainNameType());
  617. // replace-client-name is false, client passes in a full fqdn
  618. // response domain should not be altered.
  619. request.reset(new Option6ClientFqdn(0, "myhost.example.com.",
  620. Option6ClientFqdn::FULL));
  621. response.reset(new Option6ClientFqdn(*request));
  622. mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
  623. EXPECT_EQ("myhost.example.com.", response->getDomainName());
  624. EXPECT_EQ(Option6ClientFqdn::FULL, response->getDomainNameType());
  625. // Create enabled configuration.
  626. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  627. isc::asiolink::IOAddress("127.0.0.1"), 477,
  628. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  629. false, false, false, true,
  630. "prefix", "suffix.com")));
  631. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  632. ASSERT_TRUE(cfg->getReplaceClientName());
  633. // replace-client-name is true, client passes in empty fqdn
  634. // reponse domain should be empty/partial.
  635. request.reset(new Option6ClientFqdn(0, "", Option6ClientFqdn::PARTIAL));
  636. response.reset(new Option6ClientFqdn(*request));
  637. mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
  638. EXPECT_EQ("", response->getDomainName());
  639. EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());
  640. // replace-client-name is true, client passes in a partial fqdn
  641. // reponse domain should be empty/partial.
  642. request.reset(new Option6ClientFqdn(0, "myhost",
  643. Option6ClientFqdn::PARTIAL));
  644. response.reset(new Option6ClientFqdn(*request));
  645. mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
  646. EXPECT_EQ("", response->getDomainName());
  647. EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());
  648. // replace-client-name is true, client passes in a full fqdn
  649. // reponse domain should be empty/partial.
  650. request.reset(new Option6ClientFqdn(0, "myhost.example.com.",
  651. Option6ClientFqdn::FULL));
  652. response.reset(new Option6ClientFqdn(*request));
  653. mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
  654. EXPECT_EQ("", response->getDomainName());
  655. EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());
  656. }
  657. /// @brief Verifies the adustFqdnFlags template with Option6ClientFqdn objects.
  658. /// Ensures that the method can set the N, S, and O flags properly.
  659. /// Other permutations are covered by analyzeFqdnFlags tests.
  660. TEST(D2ClientMgr, adjustFqdnFlagsV6) {
  661. D2ClientMgr mgr;
  662. Option6ClientFqdnPtr request;
  663. Option6ClientFqdnPtr response;
  664. // Create enabled configuration and override-no-update on.
  665. D2ClientConfigPtr cfg;
  666. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  667. isc::asiolink::IOAddress("127.0.0.1"), 477,
  668. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  669. false, true, false, false,
  670. "pre-fix", "suf-fix")));
  671. ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
  672. ASSERT_TRUE(mgr.ddnsEnabled());
  673. ASSERT_TRUE(cfg->getOverrideNoUpdate());
  674. ASSERT_FALSE(cfg->getOverrideClientUpdate());
  675. // client S=0 N=0 means client wants to do forward update.
  676. // server S should be 0 (server is not doing forward updates)
  677. // and server N should be 0 (server doing reverse updates)
  678. // and server O should be 0
  679. request.reset(new Option6ClientFqdn(0, "", Option6ClientFqdn::PARTIAL));
  680. response.reset(new Option6ClientFqdn(*request));
  681. response->resetFlags();
  682. mgr.adjustFqdnFlags<Option6ClientFqdn>(*request, *response);
  683. EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_S));
  684. EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
  685. EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_O));
  686. // client S=1 N=0 means client wants server to do forward update.
  687. // server S should be 1 (server is doing forward updates)
  688. // and server N should be 0 (server doing updates)
  689. // and server O should be 0
  690. request.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_S,
  691. "", Option6ClientFqdn::PARTIAL));
  692. response.reset(new Option6ClientFqdn(*request));
  693. response->resetFlags();
  694. mgr.adjustFqdnFlags<Option6ClientFqdn>(*request, *response);
  695. EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_S));
  696. EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
  697. EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_O));
  698. // client S=0 N=1 means client wants no one to do updates
  699. // server S should be 1 (server is doing forward updates)
  700. // and server N should be 0 (server doing updates)
  701. // and O should be 1 (overriding client S)
  702. request.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_N,
  703. "", Option6ClientFqdn::PARTIAL));
  704. response.reset(new Option6ClientFqdn(*request));
  705. response->resetFlags();
  706. mgr.adjustFqdnFlags<Option6ClientFqdn>(*request, *response);
  707. EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_S));
  708. EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
  709. EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_O));
  710. }
  711. /// @brief Verified the getUpdateDirections template method with
  712. /// Option6ClientFqdn objects.
  713. TEST(D2ClientMgr, updateDirectionsV6) {
  714. D2ClientMgr mgr;
  715. Option6ClientFqdnPtr response;
  716. bool do_forward = false;
  717. bool do_reverse = false;
  718. // Response S=0, N=0 should mean do reverse only.
  719. response.reset(new Option6ClientFqdn(0,
  720. "", Option6ClientFqdn::PARTIAL));
  721. mgr.getUpdateDirections(*response, do_forward, do_reverse);
  722. EXPECT_FALSE(do_forward);
  723. EXPECT_TRUE(do_reverse);
  724. // Response S=0, N=1 should mean don't do either.
  725. response.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_N,
  726. "", Option6ClientFqdn::PARTIAL));
  727. mgr.getUpdateDirections(*response, do_forward, do_reverse);
  728. EXPECT_FALSE(do_forward);
  729. EXPECT_FALSE(do_reverse);
  730. // Response S=1, N=0 should mean do both.
  731. response.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_S,
  732. "", Option6ClientFqdn::PARTIAL));
  733. mgr.getUpdateDirections(*response, do_forward, do_reverse);
  734. EXPECT_TRUE(do_forward);
  735. EXPECT_TRUE(do_reverse);
  736. // Response S=1, N=1 isn't possible.
  737. }
  738. } // end of anonymous namespace