d2_client_unittest.cc 48 KB

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