fqdn_unittest.cc 58 KB

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