fqdn_unittest.cc 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. // Copyright (C) 2013-2015 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 <asiolink/io_address.h>
  16. #include <dhcp/option4_client_fqdn.h>
  17. #include <dhcp/option_int_array.h>
  18. #include <dhcp/tests/iface_mgr_test_config.h>
  19. #include <dhcp4/tests/dhcp4_client.h>
  20. #include <dhcp4/tests/dhcp4_test_utils.h>
  21. #include <dhcp_ddns/ncr_msg.h>
  22. #include <dhcpsrv/cfgmgr.h>
  23. #include <dhcpsrv/lease_mgr.h>
  24. #include <dhcpsrv/lease_mgr_factory.h>
  25. #include <gtest/gtest.h>
  26. #include <boost/scoped_ptr.hpp>
  27. using namespace isc;
  28. using namespace isc::asiolink;
  29. using namespace isc::dhcp;
  30. using namespace isc::dhcp::test;
  31. using namespace isc::dhcp_ddns;
  32. namespace {
  33. /// @brief Set of JSON configurations used by the FQDN tests.
  34. const char* CONFIGS[] = {
  35. "{ \"interfaces-config\": {"
  36. " \"interfaces\": [ \"*\" ]"
  37. "},"
  38. "\"valid-lifetime\": 3000,"
  39. "\"subnet4\": [ { "
  40. " \"subnet\": \"10.0.0.0/24\", "
  41. " \"id\": 1,"
  42. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  43. " \"option-data\": [ {"
  44. " \"name\": \"routers\","
  45. " \"code\": 3,"
  46. " \"data\": \"10.0.0.200,10.0.0.201\","
  47. " \"csv-format\": true,"
  48. " \"space\": \"dhcp4\""
  49. " } ],"
  50. " \"reservations\": ["
  51. " {"
  52. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  53. " \"hostname\": \"unique-host.example.org\""
  54. " }"
  55. " ]"
  56. " }],"
  57. "\"dhcp-ddns\": {"
  58. "\"enable-updates\": true,"
  59. "\"qualifying-suffix\": \"\""
  60. "}"
  61. "}",
  62. "{ \"interfaces-config\": {"
  63. " \"interfaces\": [ \"*\" ]"
  64. "},"
  65. "\"valid-lifetime\": 3000,"
  66. "\"subnet4\": [ { "
  67. " \"subnet\": \"10.0.0.0/24\", "
  68. " \"id\": 1,"
  69. " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
  70. " \"option-data\": [ {"
  71. " \"name\": \"routers\","
  72. " \"code\": 3,"
  73. " \"data\": \"10.0.0.200,10.0.0.201\","
  74. " \"csv-format\": true,"
  75. " \"space\": \"dhcp4\""
  76. " } ],"
  77. " \"reservations\": ["
  78. " {"
  79. " \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
  80. " \"hostname\": \"foobar\""
  81. " }"
  82. " ]"
  83. " }],"
  84. "\"dhcp-ddns\": {"
  85. "\"enable-updates\": true,"
  86. "\"qualifying-suffix\": \"fake-suffix.isc.org.\""
  87. "}"
  88. "}"
  89. };
  90. class NameDhcpv4SrvTest : public Dhcpv4SrvTest {
  91. public:
  92. // Reference to D2ClientMgr singleton
  93. D2ClientMgr& d2_mgr_;
  94. /// @brief Pointer to the DHCP server instance.
  95. NakedDhcpv4Srv* srv_;
  96. /// @brief Interface Manager's fake configuration control.
  97. IfaceMgrTestConfig iface_mgr_test_config_;
  98. // Bit Constants for turning on and off DDNS configuration options.
  99. static const uint16_t ALWAYS_INCLUDE_FQDN = 1;
  100. static const uint16_t OVERRIDE_NO_UPDATE = 2;
  101. static const uint16_t OVERRIDE_CLIENT_UPDATE = 4;
  102. static const uint16_t REPLACE_CLIENT_NAME = 8;
  103. NameDhcpv4SrvTest()
  104. : Dhcpv4SrvTest(),
  105. d2_mgr_(CfgMgr::instance().getD2ClientMgr()),
  106. srv_(NULL),
  107. iface_mgr_test_config_(true)
  108. {
  109. srv_ = new NakedDhcpv4Srv(0);
  110. IfaceMgr::instance().openSockets4();
  111. // Config DDNS to be enabled, all controls off
  112. enableD2();
  113. }
  114. virtual ~NameDhcpv4SrvTest() {
  115. delete srv_;
  116. // CfgMgr singleton doesn't get wiped between tests, so we'll
  117. // disable D2 explicitly between tests.
  118. disableD2();
  119. }
  120. /// @brief Sets the server's DDNS configuration to ddns updates disabled.
  121. void disableD2() {
  122. // Default constructor creates a config with DHCP-DDNS updates
  123. // disabled.
  124. D2ClientConfigPtr cfg(new D2ClientConfig());
  125. CfgMgr::instance().setD2ClientConfig(cfg);
  126. }
  127. /// @brief Enables DHCP-DDNS updates with the given options enabled.
  128. ///
  129. /// Replaces the current D2ClientConfiguration with a configuration
  130. /// which as updates enabled and the control options set based upon
  131. /// the bit mask of options.
  132. ///
  133. /// @param mask Bit mask of configuration options that should be enabled.
  134. void enableD2(const uint16_t mask = 0) {
  135. D2ClientConfigPtr cfg;
  136. ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
  137. isc::asiolink::IOAddress("127.0.0.1"), 53001,
  138. isc::asiolink::IOAddress("0.0.0.0"), 0, 1024,
  139. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  140. (mask & ALWAYS_INCLUDE_FQDN),
  141. (mask & OVERRIDE_NO_UPDATE),
  142. (mask & OVERRIDE_CLIENT_UPDATE),
  143. (mask & REPLACE_CLIENT_NAME),
  144. "myhost", "example.com")));
  145. ASSERT_NO_THROW(CfgMgr::instance().setD2ClientConfig(cfg));
  146. ASSERT_NO_THROW(srv_->startD2());
  147. }
  148. // Create a lease to be used by various tests.
  149. Lease4Ptr createLease(const isc::asiolink::IOAddress& addr,
  150. const std::string& hostname,
  151. const bool fqdn_fwd,
  152. const bool fqdn_rev) {
  153. const uint8_t hwaddr_data[] = { 0, 1, 2, 3, 4, 5, 6 };
  154. HWAddrPtr hwaddr(new HWAddr(hwaddr_data, sizeof(hwaddr_data), HTYPE_ETHER));
  155. Lease4Ptr lease(new Lease4(addr, hwaddr,
  156. &generateClientId()->getData()[0],
  157. generateClientId()->getData().size(),
  158. 100, 50, 75, time(NULL), subnet_->getID()));
  159. // @todo Set this through the Lease4 constructor.
  160. lease->hostname_ = hostname;
  161. lease->fqdn_fwd_ = fqdn_fwd;
  162. lease->fqdn_rev_ = fqdn_rev;
  163. return (lease);
  164. }
  165. // Create an instance of the DHCPv4 Client FQDN Option.
  166. Option4ClientFqdnPtr
  167. createClientFqdn(const uint8_t flags,
  168. const std::string& fqdn_name,
  169. Option4ClientFqdn::DomainNameType fqdn_type) {
  170. return (Option4ClientFqdnPtr(new Option4ClientFqdn(flags,
  171. Option4ClientFqdn::
  172. RCODE_CLIENT(),
  173. fqdn_name,
  174. fqdn_type)));
  175. }
  176. // Create an instance of the Hostname option.
  177. OptionStringPtr
  178. createHostname(const std::string& hostname) {
  179. OptionStringPtr opt_hostname(new OptionString(Option::V4,
  180. DHO_HOST_NAME,
  181. hostname));
  182. return (opt_hostname);
  183. }
  184. /// @brief Convenience method for generating an FQDN from an IP address.
  185. ///
  186. /// This is just a wrapper method around the D2ClientMgr's method for
  187. /// generating domain names from the configured prefix, suffix, and a
  188. /// given IP address. This is useful for verifying that fully generated
  189. /// names are correct.
  190. ///
  191. /// @param addr IP address used in the lease.
  192. /// @param trailing_dot A boolean flag which indicates whether the
  193. /// trailing dot should be appended to the end of the hostname.
  194. /// The default value is "true" which means that it should.
  195. ///
  196. /// @return An std::string contained the generated FQDN.
  197. std::string generatedNameFromAddress(const IOAddress& addr,
  198. const bool trailing_dot = true) {
  199. return(CfgMgr::instance().getD2ClientMgr()
  200. .generateFqdn(addr, trailing_dot));
  201. }
  202. // Get the Client FQDN Option from the given message.
  203. Option4ClientFqdnPtr getClientFqdnOption(const Pkt4Ptr& pkt) {
  204. return (boost::dynamic_pointer_cast<
  205. Option4ClientFqdn>(pkt->getOption(DHO_FQDN)));
  206. }
  207. // get the Hostname option from the given message.
  208. OptionStringPtr getHostnameOption(const Pkt4Ptr& pkt) {
  209. return (boost::dynamic_pointer_cast<
  210. OptionString>(pkt->getOption(DHO_HOST_NAME)));
  211. }
  212. // Create a message holding DHCPv4 Client FQDN Option.
  213. Pkt4Ptr generatePktWithFqdn(const uint8_t msg_type,
  214. const uint8_t fqdn_flags,
  215. const std::string& fqdn_domain_name,
  216. Option4ClientFqdn::DomainNameType fqdn_type,
  217. const bool include_prl,
  218. const bool include_clientid = true) {
  219. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(msg_type, 1234));
  220. pkt->setRemoteAddr(IOAddress("192.0.2.3"));
  221. pkt->setIface("eth1");
  222. // For DISCOVER we don't include server id, because client broadcasts
  223. // the message to all servers.
  224. if (msg_type != DHCPDISCOVER) {
  225. pkt->addOption(srv_->getServerID());
  226. }
  227. if (include_clientid) {
  228. pkt->addOption(generateClientId());
  229. }
  230. // Create Client FQDN Option with the specified flags and
  231. // domain-name.
  232. pkt->addOption(createClientFqdn(fqdn_flags, fqdn_domain_name,
  233. fqdn_type));
  234. // Control whether or not to request that server returns the FQDN
  235. // option. Server may be configured to always return it or return
  236. // only in case client requested it.
  237. if (include_prl) {
  238. OptionUint8ArrayPtr option_prl =
  239. OptionUint8ArrayPtr(new OptionUint8Array(Option::V4,
  240. DHO_DHCP_PARAMETER_REQUEST_LIST));
  241. option_prl->addValue(DHO_FQDN);
  242. }
  243. return (pkt);
  244. }
  245. // Create a message holding a Hostname option.
  246. Pkt4Ptr generatePktWithHostname(const uint8_t msg_type,
  247. const std::string& hostname) {
  248. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(msg_type, 1234));
  249. pkt->setRemoteAddr(IOAddress("192.0.2.3"));
  250. // For DISCOVER we don't include server id, because client broadcasts
  251. // the message to all servers.
  252. if (msg_type != DHCPDISCOVER) {
  253. pkt->addOption(srv_->getServerID());
  254. }
  255. pkt->addOption(generateClientId());
  256. // Create Client FQDN Option with the specified flags and
  257. // domain-name.
  258. pkt->addOption(createHostname(hostname));
  259. return (pkt);
  260. }
  261. // Test that server generates the appropriate FQDN option in response to
  262. // client's FQDN option.
  263. void testProcessFqdn(const Pkt4Ptr& query, const uint8_t exp_flags,
  264. const std::string& exp_domain_name,
  265. Option4ClientFqdn::DomainNameType
  266. exp_domain_type = Option4ClientFqdn::FULL) {
  267. ASSERT_TRUE(getClientFqdnOption(query));
  268. Pkt4Ptr answer;
  269. if (query->getType() == DHCPDISCOVER) {
  270. answer.reset(new Pkt4(DHCPOFFER, 1234));
  271. } else {
  272. answer.reset(new Pkt4(DHCPACK, 1234));
  273. }
  274. Dhcpv4Exchange ex = createExchange(query);
  275. ASSERT_NO_THROW(srv_->processClientName(ex));
  276. Option4ClientFqdnPtr fqdn = getClientFqdnOption(ex.getResponse());
  277. ASSERT_TRUE(fqdn);
  278. checkFqdnFlags(ex.getResponse(), exp_flags);
  279. EXPECT_EQ(exp_domain_name, fqdn->getDomainName());
  280. EXPECT_EQ(exp_domain_type, fqdn->getDomainNameType());
  281. }
  282. /// @brief Checks the packet's FQDN option flags against a given mask
  283. ///
  284. /// @param pkt IPv4 packet whose FQDN flags should be checked.
  285. /// @param exp_flags Bit mask of flags that are expected to be true.
  286. void checkFqdnFlags(const Pkt4Ptr& pkt, const uint8_t exp_flags) {
  287. Option4ClientFqdnPtr fqdn = getClientFqdnOption(pkt);
  288. ASSERT_TRUE(fqdn);
  289. const bool flag_n = (exp_flags & Option4ClientFqdn::FLAG_N) != 0;
  290. const bool flag_s = (exp_flags & Option4ClientFqdn::FLAG_S) != 0;
  291. const bool flag_o = (exp_flags & Option4ClientFqdn::FLAG_O) != 0;
  292. const bool flag_e = (exp_flags & Option4ClientFqdn::FLAG_E) != 0;
  293. EXPECT_EQ(flag_n, fqdn->getFlag(Option4ClientFqdn::FLAG_N));
  294. EXPECT_EQ(flag_s, fqdn->getFlag(Option4ClientFqdn::FLAG_S));
  295. EXPECT_EQ(flag_o, fqdn->getFlag(Option4ClientFqdn::FLAG_O));
  296. EXPECT_EQ(flag_e, fqdn->getFlag(Option4ClientFqdn::FLAG_E));
  297. }
  298. // Processes the Hostname option in the client's message and returns
  299. // the hostname option which would be sent to the client. It will
  300. // throw NULL pointer if the hostname option is not to be included
  301. // in the response.
  302. OptionStringPtr processHostname(const Pkt4Ptr& query) {
  303. if (!getHostnameOption(query)) {
  304. ADD_FAILURE() << "Hostname option not carried in the query";
  305. }
  306. Pkt4Ptr answer;
  307. if (query->getType() == DHCPDISCOVER) {
  308. answer.reset(new Pkt4(DHCPOFFER, 1234));
  309. } else {
  310. answer.reset(new Pkt4(DHCPACK, 1234));
  311. }
  312. Dhcpv4Exchange ex = createExchange(query);
  313. srv_->processClientName(ex);
  314. OptionStringPtr hostname = getHostnameOption(ex.getResponse());
  315. return (hostname);
  316. }
  317. // Verify that NameChangeRequest holds valid values.
  318. void verifyNameChangeRequest(const isc::dhcp_ddns::NameChangeType type,
  319. const bool reverse, const bool forward,
  320. const std::string& addr,
  321. const std::string& fqdn,
  322. const std::string& dhcid,
  323. const time_t cltt,
  324. const uint16_t len,
  325. const bool not_strict_expire_check = false) {
  326. NameChangeRequestPtr ncr;
  327. ASSERT_NO_THROW(ncr = d2_mgr_.peekAt(0));
  328. ASSERT_TRUE(ncr);
  329. EXPECT_EQ(type, ncr->getChangeType());
  330. EXPECT_EQ(forward, ncr->isForwardChange());
  331. EXPECT_EQ(reverse, ncr->isReverseChange());
  332. EXPECT_EQ(addr, ncr->getIpAddress());
  333. EXPECT_EQ(fqdn, ncr->getFqdn());
  334. // Compare dhcid if it is not empty. In some cases, the DHCID is
  335. // not known in advance and can't be compared.
  336. if (!dhcid.empty()) {
  337. EXPECT_EQ(dhcid, ncr->getDhcid().toStr());
  338. }
  339. // In some cases, the test doesn't have access to the last transmission
  340. // time for the particular client. In such cases, the test can use the
  341. // current time as cltt but the it may not check the lease expiration time
  342. // for equality but rather check that the lease expiration time is not
  343. // greater than the current time + lease lifetime.
  344. if (not_strict_expire_check) {
  345. EXPECT_GE(cltt + len, ncr->getLeaseExpiresOn());
  346. } else {
  347. EXPECT_EQ(cltt + len, ncr->getLeaseExpiresOn());
  348. }
  349. EXPECT_EQ(len, ncr->getLeaseLength());
  350. EXPECT_EQ(isc::dhcp_ddns::ST_NEW, ncr->getStatus());
  351. // Process the message off the queue
  352. ASSERT_NO_THROW(d2_mgr_.runReadyIO());
  353. }
  354. /// @brief Tests processing a request with the given client flags
  355. ///
  356. /// This method creates a request with its FQDN flags set to the given
  357. /// value and submits it to the server for processing. It then checks
  358. /// the following:
  359. /// 1. Did the server generate an ACK with the correct FQDN flags
  360. /// 2. If the server should have generated an NCR, did it? and If
  361. /// so was it correct?
  362. ///
  363. /// @param client_flags Mask of client FQDN flags which are true
  364. /// @param response_flags Mask of expected FQDN flags in the response
  365. void flagVsConfigScenario(const uint8_t client_flags,
  366. const uint8_t response_flags) {
  367. // Create fake interfaces and open fake sockets.
  368. IfaceMgrTestConfig iface_config(true);
  369. IfaceMgr::instance().openSockets4();
  370. Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, client_flags,
  371. "myhost.example.com.",
  372. Option4ClientFqdn::FULL, true);
  373. // Process the request.
  374. Pkt4Ptr reply;
  375. ASSERT_NO_THROW(reply = srv_->processRequest(req));
  376. // Verify the response and flags.
  377. checkResponse(reply, DHCPACK, 1234);
  378. checkFqdnFlags(reply, response_flags);
  379. // NCRs cannot be sent to the d2_mgr unless updates are enabled.
  380. if (d2_mgr_.ddnsEnabled()) {
  381. // There should be an NCR if response S flag is 1 or N flag is 0.
  382. bool exp_fwd = (response_flags & Option4ClientFqdn::FLAG_S);
  383. bool exp_rev = (!(response_flags & Option4ClientFqdn::FLAG_N));
  384. if (!exp_fwd && !exp_rev) {
  385. ASSERT_EQ(0, d2_mgr_.getQueueSize());
  386. } else {
  387. // Verify that there is one NameChangeRequest as expected.
  388. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  389. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD,
  390. exp_rev, exp_fwd,
  391. reply->getYiaddr().toText(),
  392. "myhost.example.com.",
  393. "", // empty DHCID means don't check it
  394. time(NULL) + subnet_->getValid(),
  395. subnet_->getValid(), true);
  396. }
  397. }
  398. }
  399. };
  400. // Test that the exception is thrown if lease pointer specified as the argument
  401. // of computeDhcid function is NULL.
  402. TEST_F(NameDhcpv4SrvTest, dhcidNullLease) {
  403. Lease4Ptr lease;
  404. EXPECT_THROW(srv_->computeDhcid(lease), isc::dhcp::DhcidComputeError);
  405. }
  406. // Test that the appropriate exception is thrown if the lease object used
  407. // to compute DHCID comprises wrong hostname.
  408. TEST_F(NameDhcpv4SrvTest, dhcidWrongHostname) {
  409. // First, make sure that the lease with the correct hostname is accepted.
  410. Lease4Ptr lease = createLease(IOAddress("192.0.2.3"),
  411. "myhost.example.com.", true, true);
  412. ASSERT_NO_THROW(srv_->computeDhcid(lease));
  413. // Now, use the wrong hostname. It should result in the exception.
  414. lease->hostname_ = "myhost...example.com.";
  415. EXPECT_THROW(srv_->computeDhcid(lease), isc::dhcp::DhcidComputeError);
  416. // Also, empty hostname is wrong.
  417. lease->hostname_ = "";
  418. EXPECT_THROW(srv_->computeDhcid(lease), isc::dhcp::DhcidComputeError);
  419. }
  420. // Test that the DHCID is computed correctly, when the lease holds
  421. // correct hostname and non-NULL client id.
  422. TEST_F(NameDhcpv4SrvTest, dhcidComputeFromClientId) {
  423. Lease4Ptr lease = createLease(IOAddress("192.0.2.3"),
  424. "myhost.example.com.",
  425. true, true);
  426. isc::dhcp_ddns::D2Dhcid dhcid;
  427. ASSERT_NO_THROW(dhcid = srv_->computeDhcid(lease));
  428. // Make sure that the computed DHCID is valid.
  429. std::string dhcid_ref = "00010132E91AA355CFBB753C0F0497A5A9404"
  430. "36965B68B6D438D98E680BF10B09F3BCF";
  431. EXPECT_EQ(dhcid_ref, dhcid.toStr());
  432. }
  433. // Test that the DHCID is computed correctly, when the lease holds correct
  434. // hostname and NULL client id.
  435. TEST_F(NameDhcpv4SrvTest, dhcidComputeFromHWAddr) {
  436. Lease4Ptr lease = createLease(IOAddress("192.0.2.3"),
  437. "myhost.example.com.",
  438. true, true);
  439. lease->client_id_.reset();
  440. isc::dhcp_ddns::D2Dhcid dhcid;
  441. ASSERT_NO_THROW(dhcid = srv_->computeDhcid(lease));
  442. // Make sure that the computed DHCID is valid.
  443. std::string dhcid_ref = "0000012247F6DC4423C3E8627434A9D6868609"
  444. "D88948F78018B215EDCAA30C0C135035";
  445. EXPECT_EQ(dhcid_ref, dhcid.toStr());
  446. }
  447. // Tests the following scenario:
  448. // - Updates are enabled
  449. // - All overrides are off
  450. // - Client requests forward update (N = 0, S = 1)
  451. //
  452. // Server should perform the update:
  453. // - Response flags should N = 0, S = 1, O = 0
  454. // - Should queue an NCR
  455. TEST_F(NameDhcpv4SrvTest, updatesEnabled) {
  456. flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
  457. Option4ClientFqdn::FLAG_S),
  458. (Option4ClientFqdn::FLAG_E |
  459. Option4ClientFqdn::FLAG_S));
  460. }
  461. // Tests the following scenario
  462. // - Updates are disabled
  463. // - Client requests forward update (N = 0, S = 1)
  464. //
  465. // Server should NOT perform updates:
  466. // - Response flags should N = 1, S = 0, O = 1
  467. // - Should not queue any NCRs
  468. TEST_F(NameDhcpv4SrvTest, updatesDisabled) {
  469. disableD2();
  470. flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
  471. Option4ClientFqdn::FLAG_S),
  472. (Option4ClientFqdn::FLAG_E |
  473. Option4ClientFqdn::FLAG_N |
  474. Option4ClientFqdn::FLAG_O));
  475. }
  476. // Tests the following scenario:
  477. // - Updates are enabled
  478. // - All overrides are off.
  479. // - Client requests no updates (N = 1, S = 0)
  480. //
  481. // Server should NOT perform updates:
  482. // - Response flags should N = 1, S = 0, O = 0
  483. // - Should not queue any NCRs
  484. TEST_F(NameDhcpv4SrvTest, respectNoUpdate) {
  485. flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
  486. Option4ClientFqdn::FLAG_N),
  487. (Option4ClientFqdn::FLAG_E |
  488. Option4ClientFqdn::FLAG_N));
  489. }
  490. // Tests the following scenario:
  491. // - Updates are enabled
  492. // - override-no-update is on
  493. // - Client requests no updates (N = 1, S = 0)
  494. //
  495. // Server should override "no update" request and perform updates:
  496. // - Response flags should be N = 0, S = 1, O = 1
  497. // - Should queue an NCR
  498. TEST_F(NameDhcpv4SrvTest, overrideNoUpdate) {
  499. enableD2(OVERRIDE_NO_UPDATE);
  500. flagVsConfigScenario((Option4ClientFqdn::FLAG_E |
  501. Option4ClientFqdn::FLAG_N),
  502. (Option4ClientFqdn::FLAG_E |
  503. Option4ClientFqdn::FLAG_S |
  504. Option4ClientFqdn::FLAG_O));
  505. }
  506. // Tests the following scenario:
  507. // - Updates are enabled
  508. // - All overrides are off.
  509. // - Client requests delegation (N = 0, S = 0)
  510. //
  511. // Server should respect client's delegation request and NOT do updates:
  512. // - Response flags should be N = 0, S = 0, O = 0
  513. // - Should not queue any NCRs
  514. TEST_F(NameDhcpv4SrvTest, respectClientDelegation) {
  515. flagVsConfigScenario(Option4ClientFqdn::FLAG_E,
  516. Option4ClientFqdn::FLAG_E);
  517. }
  518. // Tests the following scenario:
  519. // - Updates are enabled
  520. // - override-client-update is on.
  521. // - Client requests delegation (N = 0, S = 0)
  522. //
  523. // Server should override client's delegation request and do updates:
  524. // - Response flags should be N = 0, S = 1, O = 1
  525. // - Should queue an NCR
  526. TEST_F(NameDhcpv4SrvTest, overrideClientDelegation) {
  527. // Turn on override-client-update.
  528. enableD2(OVERRIDE_CLIENT_UPDATE);
  529. flagVsConfigScenario(Option4ClientFqdn::FLAG_E,
  530. (Option4ClientFqdn::FLAG_E |
  531. Option4ClientFqdn::FLAG_S |
  532. Option4ClientFqdn::FLAG_O));
  533. }
  534. // Test that server processes the Hostname option sent by a client and
  535. // responds with the Hostname option to confirm that the server has
  536. // taken responsibility for the update.
  537. TEST_F(NameDhcpv4SrvTest, serverUpdateHostname) {
  538. Pkt4Ptr query;
  539. ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST,
  540. "myhost.example.com."));
  541. OptionStringPtr hostname;
  542. ASSERT_NO_THROW(hostname = processHostname(query));
  543. ASSERT_TRUE(hostname);
  544. EXPECT_EQ("myhost.example.com.", hostname->getValue());
  545. }
  546. // Test that the server skips processing of a wrong Hostname option.
  547. TEST_F(NameDhcpv4SrvTest, serverUpdateWrongHostname) {
  548. Pkt4Ptr query;
  549. ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST,
  550. "abc..example.com"));
  551. OptionStringPtr hostname;
  552. ASSERT_NO_THROW(hostname = processHostname(query));
  553. EXPECT_FALSE(hostname);
  554. }
  555. // Test that server generates the fully qualified domain name for the client
  556. // if client supplies the partial name.
  557. TEST_F(NameDhcpv4SrvTest, serverUpdateForwardPartialNameFqdn) {
  558. Pkt4Ptr query = generatePktWithFqdn(DHCPREQUEST,
  559. Option4ClientFqdn::FLAG_E |
  560. Option4ClientFqdn::FLAG_S,
  561. "myhost",
  562. Option4ClientFqdn::PARTIAL,
  563. true);
  564. testProcessFqdn(query,
  565. Option4ClientFqdn::FLAG_E | Option4ClientFqdn::FLAG_S,
  566. "myhost.example.com.");
  567. }
  568. // Test that server generates the fully qualified domain name for the client
  569. // if client supplies the unqualified name in the Hostname option.
  570. TEST_F(NameDhcpv4SrvTest, serverUpdateUnqualifiedHostname) {
  571. Pkt4Ptr query;
  572. ASSERT_NO_THROW(query = generatePktWithHostname(DHCPREQUEST, "myhost"));
  573. OptionStringPtr hostname;
  574. ASSERT_NO_THROW(hostname = processHostname(query));
  575. ASSERT_TRUE(hostname);
  576. EXPECT_EQ("myhost.example.com", hostname->getValue());
  577. }
  578. // Test that server sets empty domain-name in the FQDN option when client
  579. // supplied no domain-name. The domain-name is supposed to be set after the
  580. // lease is acquired. The domain-name is then generated from the IP address
  581. // assigned to a client.
  582. TEST_F(NameDhcpv4SrvTest, serverUpdateForwardNoNameFqdn) {
  583. Pkt4Ptr query = generatePktWithFqdn(DHCPREQUEST,
  584. Option4ClientFqdn::FLAG_E |
  585. Option4ClientFqdn::FLAG_S,
  586. "",
  587. Option4ClientFqdn::PARTIAL,
  588. true);
  589. testProcessFqdn(query,
  590. Option4ClientFqdn::FLAG_E | Option4ClientFqdn::FLAG_S,
  591. "", Option4ClientFqdn::PARTIAL);
  592. }
  593. // Test that exactly one NameChangeRequest is generated when the new lease
  594. // has been acquired (old lease is NULL).
  595. TEST_F(NameDhcpv4SrvTest, createNameChangeRequestsNewLease) {
  596. Lease4Ptr lease = createLease(IOAddress("192.0.2.3"), "myhost.example.com.",
  597. true, true);
  598. Lease4Ptr old_lease;
  599. ASSERT_NO_THROW(srv_->createNameChangeRequests(lease, old_lease));
  600. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  601. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  602. "192.0.2.3", "myhost.example.com.",
  603. "00010132E91AA355CFBB753C0F0497A5A940436965"
  604. "B68B6D438D98E680BF10B09F3BCF",
  605. lease->cltt_, 100);
  606. }
  607. // Test that no NameChangeRequest is generated when a lease is renewed and
  608. // the FQDN data hasn't changed.
  609. TEST_F(NameDhcpv4SrvTest, createNameChangeRequestsRenewNoChange) {
  610. Lease4Ptr lease = createLease(IOAddress("192.0.2.3"), "myhost.example.com.",
  611. true, true);
  612. Lease4Ptr old_lease = createLease(IOAddress("192.0.2.3"),
  613. "myhost.example.com.", true, true);
  614. old_lease->valid_lft_ += 100;
  615. ASSERT_NO_THROW(srv_->createNameChangeRequests(lease, old_lease));
  616. ASSERT_EQ(0, d2_mgr_.getQueueSize());
  617. }
  618. // Test that no NameChangeRequest is generated when forward and reverse
  619. // DNS update flags are not set in the lease.
  620. TEST_F(NameDhcpv4SrvTest, createNameChangeRequestsNoUpdate) {
  621. Lease4Ptr lease1 = createLease(IOAddress("192.0.2.3"),
  622. "lease1.example.com.",
  623. true, true);
  624. Lease4Ptr lease2 = createLease(IOAddress("192.0.2.3"),
  625. "lease2.example.com.",
  626. false, false);
  627. ASSERT_NO_THROW(srv_->createNameChangeRequests(lease2, lease1));
  628. EXPECT_EQ(1, d2_mgr_.getQueueSize());
  629. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  630. "192.0.2.3", "lease1.example.com.",
  631. "0001013A5B311F5B9FB10DDF8E53689B874F25D"
  632. "62CC147C2FF237A64C90E5A597C9B7A",
  633. lease1->cltt_, 100);
  634. lease2->hostname_ = "";
  635. lease2->fqdn_rev_ = true;
  636. lease2->fqdn_fwd_ = true;
  637. ASSERT_NO_THROW(srv_->createNameChangeRequests(lease2, lease1));
  638. EXPECT_EQ(1, d2_mgr_.getQueueSize());
  639. }
  640. // Test that two NameChangeRequests are generated when the lease is being
  641. // renewed and the new lease has updated FQDN data.
  642. TEST_F(NameDhcpv4SrvTest, createNameChangeRequestsRenew) {
  643. Lease4Ptr lease1 = createLease(IOAddress("192.0.2.3"),
  644. "lease1.example.com.",
  645. true, true);
  646. Lease4Ptr lease2 = createLease(IOAddress("192.0.2.3"),
  647. "lease2.example.com.",
  648. true, true);
  649. ASSERT_NO_THROW(srv_->createNameChangeRequests(lease2, lease1));
  650. ASSERT_EQ(2, d2_mgr_.getQueueSize());
  651. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  652. "192.0.2.3", "lease1.example.com.",
  653. "0001013A5B311F5B9FB10DDF8E53689B874F25D"
  654. "62CC147C2FF237A64C90E5A597C9B7A",
  655. lease1->cltt_, 100);
  656. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  657. "192.0.2.3", "lease2.example.com.",
  658. "000101F906D2BB752E1B2EECC5FF2BF434C0B2D"
  659. "D6D7F7BD873F4F280165DB8C9DBA7CB",
  660. lease2->cltt_, 100);
  661. }
  662. // Test that the OFFER message generated as a result of the DISCOVER message
  663. // processing will not result in generation of the NameChangeRequests.
  664. TEST_F(NameDhcpv4SrvTest, processDiscover) {
  665. IfaceMgrTestConfig test_config(true);
  666. IfaceMgr::instance().openSockets4();
  667. Pkt4Ptr req = generatePktWithFqdn(DHCPDISCOVER, Option4ClientFqdn::FLAG_S |
  668. Option4ClientFqdn::FLAG_E,
  669. "myhost.example.com.",
  670. Option4ClientFqdn::FULL, true);
  671. Pkt4Ptr reply;
  672. ASSERT_NO_THROW(reply = srv_->processDiscover(req));
  673. checkResponse(reply, DHCPOFFER, 1234);
  674. EXPECT_EQ(0, d2_mgr_.getQueueSize());
  675. }
  676. // Test that server generates client's hostname from the IP address assigned
  677. // to it when DHCPv4 Client FQDN option specifies an empty domain-name.
  678. TEST_F(NameDhcpv4SrvTest, processRequestFqdnEmptyDomainName) {
  679. IfaceMgrTestConfig test_config(true);
  680. IfaceMgr::instance().openSockets4();
  681. Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
  682. Option4ClientFqdn::FLAG_E,
  683. "", Option4ClientFqdn::PARTIAL, true);
  684. Pkt4Ptr reply;
  685. ASSERT_NO_THROW(reply = srv_->processRequest(req));
  686. checkResponse(reply, DHCPACK, 1234);
  687. // Verify that there is one NameChangeRequest generated.
  688. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  689. // The hostname is generated from the IP address acquired (yiaddr).
  690. std::string hostname = generatedNameFromAddress(reply->getYiaddr());
  691. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  692. reply->getYiaddr().toText(), hostname,
  693. "", // empty DHCID forces that it is not checked
  694. time(NULL) + subnet_->getValid(),
  695. subnet_->getValid(), true);
  696. }
  697. // Test that server generates client's hostname from the IP address assigned
  698. // to it when DHCPv4 Client FQDN option specifies an empty domain-name AND
  699. // ddns updates are disabled.
  700. TEST_F(NameDhcpv4SrvTest, processRequestEmptyDomainNameDisabled) {
  701. // Create fake interfaces and open fake sockets.
  702. IfaceMgrTestConfig test_config(true);
  703. IfaceMgr::instance().openSockets4();
  704. disableD2();
  705. Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
  706. Option4ClientFqdn::FLAG_E,
  707. "", Option4ClientFqdn::PARTIAL, true);
  708. Pkt4Ptr reply;
  709. ASSERT_NO_THROW(reply = srv_->processRequest(req));
  710. checkResponse(reply, DHCPACK, 1234);
  711. Option4ClientFqdnPtr fqdn = getClientFqdnOption(reply);
  712. ASSERT_TRUE(fqdn);
  713. // The hostname is generated from the IP address acquired (yiaddr).
  714. std::string hostname = generatedNameFromAddress(reply->getYiaddr());
  715. EXPECT_EQ(hostname, fqdn->getDomainName());
  716. EXPECT_EQ(Option4ClientFqdn::FULL, fqdn->getDomainNameType());
  717. }
  718. // Test that server generates client's hostname from the IP address assigned
  719. // to it when Hostname option carries the top level domain-name.
  720. TEST_F(NameDhcpv4SrvTest, processRequestTopLevelHostname) {
  721. IfaceMgrTestConfig test_config(true);
  722. IfaceMgr::instance().openSockets4();
  723. Pkt4Ptr req = generatePktWithHostname(DHCPREQUEST, ".");
  724. // Set interface for the incoming packet. The server requires it to
  725. // generate client id.
  726. req->setIface("eth1");
  727. Pkt4Ptr reply;
  728. ASSERT_NO_THROW(reply = srv_->processRequest(req));
  729. checkResponse(reply, DHCPACK, 1234);
  730. // Verify that there is one NameChangeRequest generated.
  731. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  732. // The hostname is generated from the IP address acquired (yiaddr).
  733. std::string hostname = generatedNameFromAddress(reply->getYiaddr());
  734. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  735. reply->getYiaddr().toText(), hostname,
  736. "", // empty DHCID forces that it is not checked
  737. time(NULL), subnet_->getValid(), true);
  738. }
  739. // Test that client may send two requests, each carrying FQDN option with
  740. // a different domain-name. Server should use existing lease for the second
  741. // request but modify the DNS entries for the lease according to the contents
  742. // of the FQDN sent in the second request.
  743. TEST_F(NameDhcpv4SrvTest, processTwoRequestsFqdn) {
  744. IfaceMgrTestConfig test_config(true);
  745. IfaceMgr::instance().openSockets4();
  746. Pkt4Ptr req1 = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
  747. Option4ClientFqdn::FLAG_E,
  748. "myhost.example.com.",
  749. Option4ClientFqdn::FULL, true);
  750. Pkt4Ptr reply;
  751. ASSERT_NO_THROW(reply = srv_->processRequest(req1));
  752. checkResponse(reply, DHCPACK, 1234);
  753. // Verify that there is one NameChangeRequest generated.
  754. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  755. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  756. reply->getYiaddr().toText(), "myhost.example.com.",
  757. "00010132E91AA355CFBB753C0F0497A5A940436"
  758. "965B68B6D438D98E680BF10B09F3BCF",
  759. time(NULL), subnet_->getValid(), true);
  760. // Create another Request message but with a different FQDN. Server
  761. // should generate two NameChangeRequests: one to remove existing entry,
  762. // another one to add new entry with updated domain-name.
  763. Pkt4Ptr req2 = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
  764. Option4ClientFqdn::FLAG_E,
  765. "otherhost.example.com.",
  766. Option4ClientFqdn::FULL, true);
  767. ASSERT_NO_THROW(reply = srv_->processRequest(req2));
  768. checkResponse(reply, DHCPACK, 1234);
  769. // There should be two NameChangeRequests. Verify that they are valid.
  770. ASSERT_EQ(2, d2_mgr_.getQueueSize());
  771. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  772. reply->getYiaddr().toText(),
  773. "myhost.example.com.",
  774. "00010132E91AA355CFBB753C0F0497A5A940436"
  775. "965B68B6D438D98E680BF10B09F3BCF",
  776. time(NULL), subnet_->getValid(), true);
  777. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  778. reply->getYiaddr().toText(),
  779. "otherhost.example.com.",
  780. "000101A5AEEA7498BD5AD9D3BF600E49FF39A7E3"
  781. "AFDCE8C3D0E53F35CC584DD63C89CA",
  782. time(NULL), subnet_->getValid(), true);
  783. }
  784. // Test that client may send two requests, each carrying Hostname option with
  785. // a different name. Server should use existing lease for the second request
  786. // but modify the DNS entries for the lease according to the contents of the
  787. // Hostname sent in the second request.
  788. TEST_F(NameDhcpv4SrvTest, processTwoRequestsHostname) {
  789. IfaceMgrTestConfig test_config(true);
  790. IfaceMgr::instance().openSockets4();
  791. Pkt4Ptr req1 = generatePktWithHostname(DHCPREQUEST, "myhost.example.com.");
  792. // Set interface for the incoming packet. The server requires it to
  793. // generate client id.
  794. req1->setIface("eth1");
  795. Pkt4Ptr reply;
  796. ASSERT_NO_THROW(reply = srv_->processRequest(req1));
  797. checkResponse(reply, DHCPACK, 1234);
  798. // Verify that there is one NameChangeRequest generated.
  799. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  800. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  801. reply->getYiaddr().toText(), "myhost.example.com.",
  802. "00010132E91AA355CFBB753C0F0497A5A940436"
  803. "965B68B6D438D98E680BF10B09F3BCF",
  804. time(NULL), subnet_->getValid(), true);
  805. // Create another Request message but with a different Hostname. Server
  806. // should generate two NameChangeRequests: one to remove existing entry,
  807. // another one to add new entry with updated domain-name.
  808. Pkt4Ptr req2 = generatePktWithHostname(DHCPREQUEST, "otherhost");
  809. // Set interface for the incoming packet. The server requires it to
  810. // generate client id.
  811. req2->setIface("eth1");
  812. ASSERT_NO_THROW(reply = srv_->processRequest(req2));
  813. checkResponse(reply, DHCPACK, 1234);
  814. // There should be two NameChangeRequests. Verify that they are valid.
  815. ASSERT_EQ(2, d2_mgr_.getQueueSize());
  816. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  817. reply->getYiaddr().toText(),
  818. "myhost.example.com.",
  819. "00010132E91AA355CFBB753C0F0497A5A940436"
  820. "965B68B6D438D98E680BF10B09F3BCF",
  821. time(NULL), subnet_->getValid(), true);
  822. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  823. reply->getYiaddr().toText(),
  824. "otherhost.example.com.",
  825. "000101A5AEEA7498BD5AD9D3BF600E49FF39A7E3"
  826. "AFDCE8C3D0E53F35CC584DD63C89CA",
  827. time(NULL), subnet_->getValid(), true);
  828. }
  829. // Test that when a release message is sent for a previously acquired lease,
  830. // DDNS updates are enabled that the server generates a NameChangeRequest
  831. // to remove entries corresponding to the released lease.
  832. TEST_F(NameDhcpv4SrvTest, processRequestRelease) {
  833. IfaceMgrTestConfig test_config(true);
  834. IfaceMgr::instance().openSockets4();
  835. // Verify the updates are enabled.
  836. ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
  837. // Create and process a lease request so we have a lease to release.
  838. Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
  839. Option4ClientFqdn::FLAG_E,
  840. "myhost.example.com.",
  841. Option4ClientFqdn::FULL, true);
  842. Pkt4Ptr reply;
  843. ASSERT_NO_THROW(reply = srv_->processRequest(req));
  844. checkResponse(reply, DHCPACK, 1234);
  845. // Verify that there is one NameChangeRequest generated for lease.
  846. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  847. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  848. reply->getYiaddr().toText(), "myhost.example.com.",
  849. "00010132E91AA355CFBB753C0F0497A5A940436"
  850. "965B68B6D438D98E680BF10B09F3BCF",
  851. time(NULL), subnet_->getValid(), true);
  852. // Create and process the Release message.
  853. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  854. rel->setCiaddr(reply->getYiaddr());
  855. rel->setRemoteAddr(IOAddress("192.0.2.3"));
  856. rel->addOption(generateClientId());
  857. rel->addOption(srv_->getServerID());
  858. ASSERT_NO_THROW(srv_->processRelease(rel));
  859. // The lease has been removed, so there should be a NameChangeRequest to
  860. // remove corresponding DNS entries.
  861. ASSERT_EQ(1, d2_mgr_.getQueueSize());
  862. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  863. reply->getYiaddr().toText(), "myhost.example.com.",
  864. "00010132E91AA355CFBB753C0F0497A5A940436"
  865. "965B68B6D438D98E680BF10B09F3BCF",
  866. time(NULL), subnet_->getValid(), true);
  867. }
  868. // Test that when the Release message is sent for a previously acquired lease
  869. // and DDNS updates are disabled that server does NOT generate a
  870. // NameChangeRequest to remove entries corresponding to the released lease.
  871. // Queue size is not available when updates are not enabled, however,
  872. // attempting to send a NCR when updates disabled will result in a throw.
  873. // If no throws are experienced then no attempt was made to send a NCR.
  874. TEST_F(NameDhcpv4SrvTest, processRequestReleaseUpdatesDisabled) {
  875. // Create fake interfaces and open fake sockets.
  876. IfaceMgrTestConfig test_config(true);
  877. IfaceMgr::instance().openSockets4();
  878. // Disable DDNS.
  879. disableD2();
  880. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  881. // Create and process a lease request so we have a lease to release.
  882. Pkt4Ptr req = generatePktWithFqdn(DHCPREQUEST, Option4ClientFqdn::FLAG_S |
  883. Option4ClientFqdn::FLAG_E,
  884. "myhost.example.com.",
  885. Option4ClientFqdn::FULL, true);
  886. Pkt4Ptr reply;
  887. ASSERT_NO_THROW(reply = srv_->processRequest(req));
  888. checkResponse(reply, DHCPACK, 1234);
  889. // Create and process the Release message.
  890. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  891. rel->setCiaddr(reply->getYiaddr());
  892. rel->setRemoteAddr(IOAddress("192.0.2.3"));
  893. rel->addOption(generateClientId());
  894. rel->addOption(srv_->getServerID());
  895. ASSERT_NO_THROW(srv_->processRelease(rel));
  896. }
  897. // This test verifies that the server sends the FQDN option to the client
  898. // with the reserved hostname.
  899. TEST_F(NameDhcpv4SrvTest, fqdnReservation) {
  900. Dhcp4Client client(Dhcp4Client::SELECTING);
  901. // Use HW address that matches the reservation entry in the configuration.
  902. client.setHWAddress("aa:bb:cc:dd:ee:ff");
  903. // Configure DHCP server.
  904. configure(CONFIGS[0], *client.getServer());
  905. // Make sure that DDNS is enabled.
  906. ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
  907. ASSERT_NO_THROW(client.getServer()->startD2());
  908. // Include the Client FQDN option.
  909. ASSERT_NO_THROW(client.includeFQDN(Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E,
  910. "client-name", Option4ClientFqdn::PARTIAL));
  911. // Send the DHCPDISCOVER.
  912. ASSERT_NO_THROW(client.doDiscover());
  913. // Make sure that the server responded.
  914. Pkt4Ptr resp = client.getContext().response_;
  915. ASSERT_TRUE(resp);
  916. ASSERT_EQ(DHCPOFFER, static_cast<int>(resp->getType()));
  917. // Obtain the FQDN option sent in the response and make sure that the server
  918. // has used the hostname reserved for this client.
  919. Option4ClientFqdnPtr fqdn;
  920. fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>(resp->getOption(DHO_FQDN));
  921. ASSERT_TRUE(fqdn);
  922. EXPECT_EQ("unique-host.example.org.", fqdn->getDomainName());
  923. // When receiving DHCPDISCOVER, no NCRs should be generated.
  924. EXPECT_EQ(0, d2_mgr_.getQueueSize());
  925. // Now send the DHCPREQUEST with including the FQDN option.
  926. ASSERT_NO_THROW(client.doRequest());
  927. resp = client.getContext().response_;
  928. ASSERT_TRUE(resp);
  929. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  930. // Once again check that the FQDN is as expected.
  931. fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>(resp->getOption(DHO_FQDN));
  932. ASSERT_TRUE(fqdn);
  933. EXPECT_EQ("unique-host.example.org.", fqdn->getDomainName());
  934. {
  935. SCOPED_TRACE("Verify the correctness of the NCR for the"
  936. "unique-host.example.org");
  937. // Because this is a new lease, there should be one NCR which adds the
  938. // new DNS entry.
  939. ASSERT_EQ(1, CfgMgr::instance().getD2ClientMgr().getQueueSize());
  940. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  941. resp->getYiaddr().toText(),
  942. "unique-host.example.org.",
  943. "000001ACB52196C8F3BCC1DF3BA1F40BAC39BF23"
  944. "0D280858B1ED7696E174C4479E3372",
  945. time(NULL), subnet_->getValid(), true);
  946. }
  947. // And that this FQDN has been stored in the lease database.
  948. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(client.config_.lease_.addr_);
  949. ASSERT_TRUE(lease);
  950. EXPECT_EQ("unique-host.example.org.", lease->hostname_);
  951. // Reconfigure DHCP server to use a different hostname for the client.
  952. configure(CONFIGS[1], *client.getServer());
  953. // Make sure that DDNS is enabled.
  954. ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
  955. ASSERT_NO_THROW(client.getServer()->startD2());
  956. // Client is in the renewing state.
  957. client.setState(Dhcp4Client::RENEWING);
  958. client.doRequest();
  959. resp = client.getContext().response_;
  960. ASSERT_TRUE(resp);
  961. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  962. // The new FQDN should contain a different name this time.
  963. fqdn = boost::dynamic_pointer_cast<Option4ClientFqdn>(resp->getOption(DHO_FQDN));
  964. ASSERT_TRUE(fqdn);
  965. EXPECT_EQ("foobar.fake-suffix.isc.org.", fqdn->getDomainName());
  966. // And the lease in the lease database should also contain this new FQDN.
  967. lease = LeaseMgrFactory::instance().getLease4(client.config_.lease_.addr_);
  968. ASSERT_TRUE(lease);
  969. EXPECT_EQ("foobar.fake-suffix.isc.org.", lease->hostname_);
  970. // Now there should be two name NCRs. One that removes the previous entry
  971. // and the one that adds a new entry for the new hostname.
  972. ASSERT_EQ(2, CfgMgr::instance().getD2ClientMgr().getQueueSize());
  973. {
  974. SCOPED_TRACE("Verify the correctness of the CHG_REMOVE NCR for the "
  975. "unique-host.example.org");
  976. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  977. resp->getYiaddr().toText(),
  978. "unique-host.example.org.",
  979. "000001ACB52196C8F3BCC1DF3BA1F40BAC39BF23"
  980. "0D280858B1ED7696E174C4479E3372",
  981. time(NULL), subnet_->getValid(), true);
  982. }
  983. {
  984. SCOPED_TRACE("Verify the correctness of the CHG_ADD NCR for the "
  985. "foobar.fake-suffix.isc.org");
  986. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  987. resp->getYiaddr().toText(),
  988. "foobar.fake-suffix.isc.org.",
  989. "0000017C29B3C236344924E448E247F3FD56C7E9"
  990. "167B3397B1305FB664C160B967CE1F",
  991. time(NULL), subnet_->getValid(), true);
  992. }
  993. }
  994. // This test verifies that the server sends the Hostname option to the client
  995. // with the reserved hostname.
  996. TEST_F(NameDhcpv4SrvTest, hostnameReservation) {
  997. Dhcp4Client client(Dhcp4Client::SELECTING);
  998. // Use HW address that matches the reservation entry in the configuration.
  999. client.setHWAddress("aa:bb:cc:dd:ee:ff");
  1000. // Configure DHCP server.
  1001. configure(CONFIGS[0], *client.getServer());
  1002. // Make sure that DDNS is enabled.
  1003. ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
  1004. ASSERT_NO_THROW(client.getServer()->startD2());
  1005. // Include the Hostname option.
  1006. ASSERT_NO_THROW(client.includeHostname("client-name"));
  1007. // Send the DHCPDISCOVER
  1008. ASSERT_NO_THROW(client.doDiscover());
  1009. // Make sure that the server responded.
  1010. Pkt4Ptr resp = client.getContext().response_;
  1011. ASSERT_TRUE(resp);
  1012. ASSERT_EQ(DHCPOFFER, static_cast<int>(resp->getType()));
  1013. // Obtain the Hostname option sent in the response and make sure that the server
  1014. // has used the hostname reserved for this client.
  1015. OptionStringPtr hostname;
  1016. hostname = boost::dynamic_pointer_cast<OptionString>(resp->getOption(DHO_HOST_NAME));
  1017. ASSERT_TRUE(hostname);
  1018. EXPECT_EQ("unique-host.example.org", hostname->getValue());
  1019. // Now send the DHCPREQUEST with including the Hostname option.
  1020. ASSERT_NO_THROW(client.doRequest());
  1021. resp = client.getContext().response_;
  1022. ASSERT_TRUE(resp);
  1023. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1024. // Once again check that the Hostname is as expected.
  1025. hostname = boost::dynamic_pointer_cast<OptionString>(resp->getOption(DHO_HOST_NAME));
  1026. ASSERT_TRUE(hostname);
  1027. EXPECT_EQ("unique-host.example.org", hostname->getValue());
  1028. // And that this hostname has been stored in the lease database.
  1029. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(client.config_.lease_.addr_);
  1030. ASSERT_TRUE(lease);
  1031. EXPECT_EQ("unique-host.example.org", lease->hostname_);
  1032. // Because this is a new lease, there should be one NCR which adds the
  1033. // new DNS entry.
  1034. ASSERT_EQ(1, CfgMgr::instance().getD2ClientMgr().getQueueSize());
  1035. {
  1036. SCOPED_TRACE("Verify the correctness of the NCR for the"
  1037. "unique-host.example.org");
  1038. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  1039. resp->getYiaddr().toText(),
  1040. "unique-host.example.org.",
  1041. "000001ACB52196C8F3BCC1DF3BA1F40BAC39BF23"
  1042. "0D280858B1ED7696E174C4479E3372",
  1043. time(NULL), subnet_->getValid(), true);
  1044. }
  1045. // Reconfigure DHCP server to use a different hostname for the client.
  1046. configure(CONFIGS[1], *client.getServer());
  1047. // Make sure that DDNS is enabled.
  1048. ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
  1049. ASSERT_NO_THROW(client.getServer()->startD2());
  1050. // Client is in the renewing state.
  1051. client.setState(Dhcp4Client::RENEWING);
  1052. client.doRequest();
  1053. resp = client.getContext().response_;
  1054. ASSERT_TRUE(resp);
  1055. ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
  1056. // The new hostname should be different than previously.
  1057. hostname = boost::dynamic_pointer_cast<OptionString>(resp->getOption(DHO_HOST_NAME));
  1058. ASSERT_TRUE(hostname);
  1059. EXPECT_EQ("foobar.fake-suffix.isc.org", hostname->getValue());
  1060. // And the lease in the lease database should also contain this new FQDN.
  1061. lease = LeaseMgrFactory::instance().getLease4(client.config_.lease_.addr_);
  1062. ASSERT_TRUE(lease);
  1063. EXPECT_EQ("foobar.fake-suffix.isc.org", lease->hostname_);
  1064. // Now there should be two name NCRs. One that removes the previous entry
  1065. // and the one that adds a new entry for the new hostname.
  1066. ASSERT_EQ(2, CfgMgr::instance().getD2ClientMgr().getQueueSize());
  1067. {
  1068. SCOPED_TRACE("Verify the correctness of the CHG_REMOVE NCR for the "
  1069. "unique-host.example.org");
  1070. verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
  1071. resp->getYiaddr().toText(),
  1072. "unique-host.example.org.",
  1073. "000001ACB52196C8F3BCC1DF3BA1F40BAC39BF23"
  1074. "0D280858B1ED7696E174C4479E3372",
  1075. time(NULL), subnet_->getValid(), true);
  1076. }
  1077. {
  1078. SCOPED_TRACE("Verify the correctness of the CHG_ADD NCR for the "
  1079. "foobar.fake-suffix.isc.org");
  1080. verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
  1081. resp->getYiaddr().toText(),
  1082. "foobar.fake-suffix.isc.org.",
  1083. "0000017C29B3C236344924E448E247F3FD56C7E9"
  1084. "167B3397B1305FB664C160B967CE1F",
  1085. time(NULL), subnet_->getValid(), true);
  1086. }
  1087. }
  1088. } // end of anonymous namespace