nc_test_utils.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // Copyright (C) 2013-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <d2/d2_cfg_mgr.h>
  15. #include <dns/opcode.h>
  16. #include <dns/messagerenderer.h>
  17. #include <nc_test_utils.h>
  18. #include <asio.hpp>
  19. #include <asiolink/udp_endpoint.h>
  20. #include <gtest/gtest.h>
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::d2;
  24. namespace isc {
  25. namespace d2 {
  26. const char* TEST_DNS_SERVER_IP = "127.0.0.1";
  27. size_t TEST_DNS_SERVER_PORT = 5301;
  28. const bool HAS_RDATA = true;
  29. const bool NO_RDATA = false;
  30. //*************************** FauxServer class ***********************
  31. FauxServer::FauxServer(asiolink::IOService& io_service,
  32. asiolink::IOAddress& address, size_t port)
  33. :io_service_(io_service), address_(address), port_(port),
  34. server_socket_(), receive_pending_(false), perpetual_receive_(true) {
  35. server_socket_.reset(new asio::ip::udp::socket(io_service_.get_io_service(),
  36. asio::ip::udp::v4()));
  37. server_socket_->set_option(asio::socket_base::reuse_address(true));
  38. isc::asiolink::UDPEndpoint endpoint(address_, port_);
  39. server_socket_->bind(endpoint.getASIOEndpoint());
  40. }
  41. FauxServer::FauxServer(asiolink::IOService& io_service,
  42. DnsServerInfo& server)
  43. :io_service_(io_service), address_(server.getIpAddress()),
  44. port_(server.getPort()), server_socket_(), receive_pending_(false),
  45. perpetual_receive_(true) {
  46. server_socket_.reset(new asio::ip::udp::socket(io_service_.get_io_service(),
  47. asio::ip::udp::v4()));
  48. server_socket_->set_option(asio::socket_base::reuse_address(true));
  49. isc::asiolink::UDPEndpoint endpoint(address_, port_);
  50. server_socket_->bind(endpoint.getASIOEndpoint());
  51. }
  52. FauxServer::~FauxServer() {
  53. }
  54. void
  55. FauxServer::receive (const ResponseMode& response_mode,
  56. const dns::Rcode& response_rcode) {
  57. if (receive_pending_) {
  58. return;
  59. }
  60. receive_pending_ = true;
  61. server_socket_->async_receive_from(asio::buffer(receive_buffer_,
  62. sizeof(receive_buffer_)),
  63. remote_,
  64. boost::bind(&FauxServer::requestHandler,
  65. this, _1, _2,
  66. response_mode,
  67. response_rcode));
  68. }
  69. void
  70. FauxServer::requestHandler(const asio::error_code& error,
  71. std::size_t bytes_recvd,
  72. const ResponseMode& response_mode,
  73. const dns::Rcode& response_rcode) {
  74. // If we encountered an error or received no data then fail.
  75. // We expect the client to send good requests.
  76. if (error.value() != 0 || bytes_recvd < 1) {
  77. ADD_FAILURE() << "FauxServer receive failed" << error.message();
  78. receive_pending_ = false;
  79. return;
  80. }
  81. // We have a successfully received data. We need to turn it into
  82. // a request in order to build a proper response.
  83. // Note D2UpdateMessage is geared towards making requests and
  84. // reading responses. This is the opposite perspective so we have
  85. // to a bit of roll-your-own here.
  86. dns::Message request(dns::Message::PARSE);
  87. util::InputBuffer request_buf(receive_buffer_, bytes_recvd);
  88. try {
  89. request.fromWire(request_buf);
  90. } catch (const std::exception& ex) {
  91. // If the request cannot be parsed, then fail the test.
  92. // We expect the client to send good requests.
  93. ADD_FAILURE() << "FauxServer request is corrupt:" << ex.what();
  94. receive_pending_ = false;
  95. return;
  96. }
  97. // The request parsed OK, so let's build a response.
  98. // We must use the QID we received in the response or IOFetch will
  99. // toss the response out, resulting in eventual timeout.
  100. // We fill in the zone with data we know is from the "server".
  101. dns::Message response(dns::Message::RENDER);
  102. response.setQid(request.getQid());
  103. dns::Question question(dns::Name("response.example.com"),
  104. dns::RRClass::ANY(), dns::RRType::SOA());
  105. response.addQuestion(question);
  106. response.setOpcode(dns::Opcode(dns::Opcode::UPDATE_CODE));
  107. response.setHeaderFlag(dns::Message::HEADERFLAG_QR, true);
  108. // Set the response Rcode to value passed in, default is NOERROR.
  109. response.setRcode(response_rcode);
  110. // Render the response to a buffer.
  111. dns::MessageRenderer renderer;
  112. util::OutputBuffer response_buf(TEST_MSG_MAX);
  113. renderer.setBuffer(&response_buf);
  114. response.toWire(renderer);
  115. // If mode is to ship garbage, then stomp on part of the rendered
  116. // message.
  117. if (response_mode == CORRUPT_RESP) {
  118. response_buf.writeUint16At(0xFFFF, 2);
  119. }
  120. // Ship the response via synchronous send.
  121. try {
  122. int cnt = server_socket_->send_to(asio::
  123. buffer(response_buf.getData(),
  124. response_buf.getLength()),
  125. remote_);
  126. // Make sure we sent what we expect to send.
  127. if (cnt != response_buf.getLength()) {
  128. ADD_FAILURE() << "FauxServer sent: " << cnt << " expected: "
  129. << response_buf.getLength();
  130. }
  131. } catch (const std::exception& ex) {
  132. ADD_FAILURE() << "FauxServer send failed: " << ex.what();
  133. }
  134. receive_pending_ = false;
  135. if (perpetual_receive_) {
  136. // Schedule the next receive
  137. receive (response_mode, response_rcode);
  138. }
  139. }
  140. //********************** TimedIO class ***********************
  141. TimedIO::TimedIO()
  142. : io_service_(new isc::asiolink::IOService()),
  143. timer_(*io_service_), run_time_(0) {
  144. }
  145. TimedIO::~TimedIO() {
  146. }
  147. int
  148. TimedIO::runTimedIO(int run_time) {
  149. run_time_ = run_time;
  150. int cnt = io_service_->get_io_service().poll();
  151. if (cnt == 0) {
  152. timer_.setup(boost::bind(&TimedIO::timesUp, this), run_time_);
  153. cnt = io_service_->get_io_service().run_one();
  154. timer_.cancel();
  155. }
  156. return (cnt);
  157. }
  158. void
  159. TimedIO::timesUp() {
  160. io_service_->stop();
  161. FAIL() << "Test Time: " << run_time_ << " expired";
  162. }
  163. //********************** TransactionTest class ***********************
  164. const unsigned int TransactionTest::FORWARD_CHG = 0x01;
  165. const unsigned int TransactionTest::REVERSE_CHG = 0x02;
  166. const unsigned int TransactionTest::FWD_AND_REV_CHG = REVERSE_CHG | FORWARD_CHG;
  167. TransactionTest::TransactionTest() : ncr_() {
  168. }
  169. TransactionTest::~TransactionTest() {
  170. }
  171. void
  172. TransactionTest::setupForIPv4Transaction(dhcp_ddns::NameChangeType chg_type,
  173. int change_mask) {
  174. const char* msg_str =
  175. "{"
  176. " \"change_type\" : 0 , "
  177. " \"forward_change\" : true , "
  178. " \"reverse_change\" : true , "
  179. " \"fqdn\" : \"my.forward.example.com.\" , "
  180. " \"ip_address\" : \"192.168.2.1\" , "
  181. " \"dhcid\" : \"0102030405060708\" , "
  182. " \"lease_expires_on\" : \"20130121132405\" , "
  183. " \"lease_length\" : 1300 "
  184. "}";
  185. // Create NameChangeRequest from JSON string.
  186. ncr_ = dhcp_ddns::NameChangeRequest::fromJSON(msg_str);
  187. // Set the change type.
  188. ncr_->setChangeType(chg_type);
  189. // If the change mask does not include a forward change clear the
  190. // forward domain; otherwise create the domain and its servers.
  191. if (!(change_mask & FORWARD_CHG)) {
  192. ncr_->setForwardChange(false);
  193. forward_domain_.reset();
  194. } else {
  195. // Create the forward domain and then its servers.
  196. forward_domain_ = makeDomain("example.com.");
  197. addDomainServer(forward_domain_, "forward.example.com",
  198. "127.0.0.1", 5301);
  199. addDomainServer(forward_domain_, "forward2.example.com",
  200. "127.0.0.1", 5302);
  201. }
  202. // If the change mask does not include a reverse change clear the
  203. // reverse domain; otherwise create the domain and its servers.
  204. if (!(change_mask & REVERSE_CHG)) {
  205. ncr_->setReverseChange(false);
  206. reverse_domain_.reset();
  207. } else {
  208. // Create the reverse domain and its server.
  209. reverse_domain_ = makeDomain("2.168.192.in.addr.arpa.");
  210. addDomainServer(reverse_domain_, "reverse.example.com",
  211. "127.0.0.1", 5301);
  212. addDomainServer(reverse_domain_, "reverse2.example.com",
  213. "127.0.0.1", 5302);
  214. }
  215. }
  216. void
  217. TransactionTest::setupForIPv6Transaction(dhcp_ddns::NameChangeType chg_type,
  218. int change_mask) {
  219. const char* msg_str =
  220. "{"
  221. " \"change_type\" : 0 , "
  222. " \"forward_change\" : true , "
  223. " \"reverse_change\" : true , "
  224. " \"fqdn\" : \"my6.forward.example.com.\" , "
  225. " \"ip_address\" : \"2001:1::100\" , "
  226. " \"dhcid\" : \"0102030405060708\" , "
  227. " \"lease_expires_on\" : \"20130121132405\" , "
  228. " \"lease_length\" : 1300 "
  229. "}";
  230. // Create NameChangeRequest from JSON string.
  231. ncr_ = makeNcrFromString(msg_str);
  232. // Set the change type.
  233. ncr_->setChangeType(chg_type);
  234. // If the change mask does not include a forward change clear the
  235. // forward domain; otherwise create the domain and its servers.
  236. if (!(change_mask & FORWARD_CHG)) {
  237. ncr_->setForwardChange(false);
  238. forward_domain_.reset();
  239. } else {
  240. // Create the forward domain and then its servers.
  241. forward_domain_ = makeDomain("example.com.");
  242. addDomainServer(forward_domain_, "fwd6-server.example.com",
  243. "::1", 5301);
  244. addDomainServer(forward_domain_, "fwd6-server2.example.com",
  245. "::1", 5302);
  246. }
  247. // If the change mask does not include a reverse change clear the
  248. // reverse domain; otherwise create the domain and its servers.
  249. if (!(change_mask & REVERSE_CHG)) {
  250. ncr_->setReverseChange(false);
  251. reverse_domain_.reset();
  252. } else {
  253. // Create the reverse domain and its server.
  254. reverse_domain_ = makeDomain("1.2001.ip6.arpa.");
  255. addDomainServer(reverse_domain_, "rev6-server.example.com",
  256. "::1", 5301);
  257. addDomainServer(reverse_domain_, "rev6-server2.example.com",
  258. "::1", 5302);
  259. }
  260. }
  261. //********************** Functions ****************************
  262. void
  263. checkRRCount(const D2UpdateMessagePtr& request,
  264. D2UpdateMessage::UpdateMsgSection section, int count) {
  265. dns::RRsetIterator rrset_it = request->beginSection(section);
  266. dns::RRsetIterator rrset_end = request->endSection(section);
  267. ASSERT_EQ(count, std::distance(rrset_it, rrset_end));
  268. }
  269. void
  270. checkZone(const D2UpdateMessagePtr& request, const std::string& exp_zone_name) {
  271. // Verify the zone section info.
  272. D2ZonePtr zone = request->getZone();
  273. EXPECT_TRUE(zone);
  274. EXPECT_EQ(exp_zone_name, zone->getName().toText());
  275. EXPECT_EQ(dns::RRClass::IN().getCode(), zone->getClass().getCode());
  276. }
  277. void
  278. checkRR(dns::RRsetPtr rrset, const std::string& exp_name,
  279. const dns::RRClass& exp_class, const dns::RRType& exp_type,
  280. unsigned int exp_ttl, dhcp_ddns::NameChangeRequestPtr ncr,
  281. bool has_rdata) {
  282. // Verify the FQDN/DHCID RR fields.
  283. EXPECT_EQ(exp_name, rrset->getName().toText());
  284. EXPECT_EQ(exp_class.getCode(), rrset->getClass().getCode());
  285. EXPECT_EQ(exp_type.getCode(), rrset->getType().getCode());
  286. EXPECT_EQ(exp_ttl, rrset->getTTL().getValue());
  287. if ((!has_rdata) ||
  288. (exp_type == dns::RRType::ANY() || exp_class == dns::RRClass::ANY())) {
  289. // ANY types do not have RData
  290. ASSERT_EQ(0, rrset->getRdataCount());
  291. return;
  292. }
  293. ASSERT_EQ(1, rrset->getRdataCount());
  294. dns::RdataIteratorPtr rdata_it = rrset->getRdataIterator();
  295. ASSERT_TRUE(rdata_it);
  296. if ((exp_type == dns::RRType::A()) ||
  297. (exp_type == dns::RRType::AAAA())) {
  298. // should have lease rdata
  299. EXPECT_EQ(ncr->getIpAddress(), rdata_it->getCurrent().toText());
  300. } else if (exp_type == dns::RRType::PTR()) {
  301. // should have PTR rdata
  302. EXPECT_EQ(ncr->getFqdn(), rdata_it->getCurrent().toText());
  303. } else if (exp_type == dns::RRType::DHCID()) {
  304. // should have DHCID rdata
  305. const std::vector<uint8_t>& ncr_dhcid = ncr->getDhcid().getBytes();
  306. util::InputBuffer buffer(ncr_dhcid.data(), ncr_dhcid.size());
  307. dns::rdata::in::DHCID rdata_ref(buffer, ncr_dhcid.size());
  308. EXPECT_EQ(0, rdata_ref.compare(rdata_it->getCurrent()));
  309. } else {
  310. // we got a problem
  311. FAIL();
  312. }
  313. }
  314. dns::RRsetPtr
  315. getRRFromSection(const D2UpdateMessagePtr& request,
  316. D2UpdateMessage::UpdateMsgSection section, int index) {
  317. dns::RRsetIterator rrset_it = request->beginSection(section);
  318. dns::RRsetIterator rrset_end = request->endSection(section);
  319. if (std::distance(rrset_it, rrset_end) <= index) {
  320. // Return an empty pointer if index is out of range.
  321. return (dns::RRsetPtr());
  322. }
  323. std::advance(rrset_it, index);
  324. return (*rrset_it);
  325. }
  326. dhcp_ddns::NameChangeRequestPtr makeNcrFromString(const std::string& ncr_str) {
  327. return (dhcp_ddns::NameChangeRequest::fromJSON(ncr_str));
  328. }
  329. DdnsDomainPtr makeDomain(const std::string& zone_name,
  330. const std::string& key_name) {
  331. DdnsDomainPtr domain;
  332. DnsServerInfoStoragePtr servers(new DnsServerInfoStorage());
  333. domain.reset(new DdnsDomain(zone_name, key_name, servers));
  334. return (domain);
  335. }
  336. void addDomainServer(DdnsDomainPtr& domain, const std::string& name,
  337. const std::string& ip, const size_t port) {
  338. DnsServerInfoPtr server(new DnsServerInfo(name, asiolink::IOAddress(ip),
  339. port));
  340. domain->getServers()->push_back(server);
  341. }
  342. // Verifies that the contents of the given transaction's DNS update request
  343. // is correct for adding a forward DNS entry
  344. void checkAddFwdAddressRequest(NameChangeTransaction& tran) {
  345. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  346. ASSERT_TRUE(request);
  347. // Safety check.
  348. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  349. ASSERT_TRUE(ncr);
  350. std::string exp_zone_name = tran.getForwardDomain()->getName();
  351. std::string exp_fqdn = ncr->getFqdn();
  352. const dns::RRType& exp_ip_rr_type = tran.getAddressRRType();
  353. // Verify the zone section.
  354. checkZone(request, exp_zone_name);
  355. // Verify the PREREQUISITE SECTION
  356. // Should be 1 which tests for FQDN does not exist.
  357. dns::RRsetPtr rrset;
  358. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 1);
  359. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  360. SECTION_PREREQUISITE, 0));
  361. checkRR(rrset, exp_fqdn, dns::RRClass::NONE(), dns::RRType::ANY(), 0, ncr);
  362. // Verify the UPDATE SECTION
  363. // Should be 2 RRs: 1 to add the FQDN/IP and one to add the DHCID RR
  364. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 2);
  365. // Fetch ttl.
  366. uint32_t ttl = ncr->getLeaseLength();
  367. // First, Verify the FQDN/IP add RR.
  368. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  369. SECTION_UPDATE, 0));
  370. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), exp_ip_rr_type, ttl, ncr);
  371. // Now, verify the DHCID add RR.
  372. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  373. SECTION_UPDATE, 1));
  374. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), dns::RRType::DHCID(),
  375. ttl, ncr);
  376. // Verify there are no RRs in the ADDITIONAL Section.
  377. checkRRCount(request, D2UpdateMessage::SECTION_ADDITIONAL, 0);
  378. // Verify that it will render toWire without throwing.
  379. dns::MessageRenderer renderer;
  380. ASSERT_NO_THROW(request->toWire(renderer));
  381. }
  382. // Verifies that the contents of the given transaction's DNS update request
  383. // is correct for replacing a forward DNS entry
  384. void checkReplaceFwdAddressRequest(NameChangeTransaction& tran) {
  385. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  386. ASSERT_TRUE(request);
  387. // Safety check.
  388. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  389. ASSERT_TRUE(ncr);
  390. std::string exp_zone_name = tran.getForwardDomain()->getName();
  391. std::string exp_fqdn = ncr->getFqdn();
  392. const dns::RRType& exp_ip_rr_type = tran.getAddressRRType();
  393. // Verify the zone section.
  394. checkZone(request, exp_zone_name);
  395. // Verify the PREREQUISITE SECTION
  396. // Should be 2, 1 which tests for FQDN does exist and the other
  397. // checks for a matching DHCID.
  398. dns::RRsetPtr rrset;
  399. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 2);
  400. // Verify the FQDN test RR.
  401. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  402. SECTION_PREREQUISITE, 0));
  403. checkRR(rrset, exp_fqdn, dns::RRClass::ANY(), dns::RRType::ANY(), 0, ncr);
  404. // Verify the DHCID test RR.
  405. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  406. SECTION_PREREQUISITE, 1));
  407. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), dns::RRType::DHCID(), 0, ncr);
  408. // Verify the UPDATE SECTION
  409. // Should be 2, 1 which deletes the existing FQDN/IP and one that
  410. // adds the new one.
  411. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 2);
  412. // Fetch ttl.
  413. uint32_t ttl = ncr->getLeaseLength();
  414. // Verify the FQDN delete RR.
  415. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  416. SECTION_UPDATE, 0));
  417. checkRR(rrset, exp_fqdn, dns::RRClass::ANY(), exp_ip_rr_type, 0, ncr);
  418. // Verify the FQDN/IP add RR.
  419. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  420. SECTION_UPDATE, 1));
  421. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), exp_ip_rr_type, ttl, ncr);
  422. // Verify there are no RRs in the ADDITIONAL Section.
  423. checkRRCount(request, D2UpdateMessage::SECTION_ADDITIONAL, 0);
  424. // Verify that it will render toWire without throwing.
  425. dns::MessageRenderer renderer;
  426. ASSERT_NO_THROW(request->toWire(renderer));
  427. }
  428. // Verifies that the contents of the given transaction's DNS update request
  429. // is correct for replacing a reverse DNS entry
  430. void checkReplaceRevPtrsRequest(NameChangeTransaction& tran) {
  431. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  432. ASSERT_TRUE(request);
  433. // Safety check.
  434. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  435. ASSERT_TRUE(ncr);
  436. std::string exp_zone_name = tran.getReverseDomain()->getName();
  437. std::string exp_rev_addr = D2CfgMgr::reverseIpAddress(ncr->getIpAddress());
  438. // Verify the zone section.
  439. checkZone(request, exp_zone_name);
  440. // Verify there are no RRs in the PREREQUISITE Section.
  441. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 0);
  442. // Fetch ttl.
  443. uint32_t ttl = ncr->getLeaseLength();
  444. // Verify the UPDATE Section.
  445. // It should contain 4 RRs:
  446. // 1. A delete all PTR RRs for the given IP
  447. // 2. A delete of all DHCID RRs for the given IP
  448. // 3. An add of the new PTR RR
  449. // 4. An add of the new DHCID RR
  450. dns::RRsetPtr rrset;
  451. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 4);
  452. // Verify the PTR delete RR.
  453. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  454. SECTION_UPDATE, 0));
  455. checkRR(rrset, exp_rev_addr, dns::RRClass::ANY(), dns::RRType::PTR(),
  456. 0, ncr);
  457. // Verify the DHCID delete RR.
  458. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  459. SECTION_UPDATE, 1));
  460. checkRR(rrset, exp_rev_addr, dns::RRClass::ANY(), dns::RRType::DHCID(),
  461. 0, ncr);
  462. // Verify the PTR add RR.
  463. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  464. SECTION_UPDATE, 2));
  465. checkRR(rrset, exp_rev_addr, dns::RRClass::IN(), dns::RRType::PTR(),
  466. ttl, ncr);
  467. // Verify the DHCID add RR.
  468. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  469. SECTION_UPDATE, 3));
  470. checkRR(rrset, exp_rev_addr, dns::RRClass::IN(), dns::RRType::DHCID(),
  471. ttl, ncr);
  472. // Verify there are no RRs in the ADDITIONAL Section.
  473. checkRRCount(request, D2UpdateMessage::SECTION_ADDITIONAL, 0);
  474. // Verify that it will render toWire without throwing.
  475. dns::MessageRenderer renderer;
  476. ASSERT_NO_THROW(request->toWire(renderer));
  477. }
  478. void checkRemoveFwdAddressRequest(NameChangeTransaction& tran) {
  479. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  480. ASSERT_TRUE(request);
  481. // Safety check.
  482. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  483. ASSERT_TRUE(ncr);
  484. std::string exp_zone_name = tran.getForwardDomain()->getName();
  485. std::string exp_fqdn = ncr->getFqdn();
  486. // Verify the zone section.
  487. checkZone(request, exp_zone_name);
  488. // Verify there is 1 RR in the PREREQUISITE Section.
  489. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 1);
  490. // Verify the DHCID matching assertion RR.
  491. dns::RRsetPtr rrset;
  492. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  493. SECTION_PREREQUISITE, 0));
  494. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), dns::RRType::DHCID(),
  495. 0, ncr);
  496. // Verify there is 1 RR in the UPDATE Section.
  497. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 1);
  498. // Verify the FQDN/IP delete RR.
  499. const dns::RRType& exp_ip_rr_type = tran.getAddressRRType();
  500. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  501. SECTION_UPDATE, 0));
  502. checkRR(rrset, exp_fqdn, dns::RRClass::NONE(), exp_ip_rr_type,
  503. 0, ncr);
  504. // Verify that it will render toWire without throwing.
  505. dns::MessageRenderer renderer;
  506. ASSERT_NO_THROW(request->toWire(renderer));
  507. }
  508. void checkRemoveFwdRRsRequest(NameChangeTransaction& tran) {
  509. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  510. ASSERT_TRUE(request);
  511. // Safety check.
  512. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  513. ASSERT_TRUE(ncr);
  514. std::string exp_zone_name = tran.getForwardDomain()->getName();
  515. std::string exp_fqdn = ncr->getFqdn();
  516. // Verify the zone section.
  517. checkZone(request, exp_zone_name);
  518. // Verify there is 1 RR in the PREREQUISITE Section.
  519. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 3);
  520. // Verify the DHCID matches assertion.
  521. dns::RRsetPtr rrset;
  522. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  523. SECTION_PREREQUISITE, 0));
  524. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), dns::RRType::DHCID(),
  525. 0, ncr);
  526. // Verify the NO A RRs assertion.
  527. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  528. SECTION_PREREQUISITE, 1));
  529. checkRR(rrset, exp_fqdn, dns::RRClass::NONE(), dns::RRType::A(),
  530. 0, ncr, NO_RDATA);
  531. // Verify the NO AAAA RRs assertion.
  532. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  533. SECTION_PREREQUISITE, 2));
  534. checkRR(rrset, exp_fqdn, dns::RRClass::NONE(), dns::RRType::AAAA(),
  535. 0, ncr, NO_RDATA);
  536. // Verify there is 1 RR in the UPDATE Section.
  537. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 1);
  538. // Verify the delete all for the FQDN RR.
  539. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  540. SECTION_UPDATE, 0));
  541. checkRR(rrset, exp_fqdn, dns::RRClass::ANY(), dns::RRType::ANY(),
  542. 0, ncr);
  543. // Verify that it will render toWire without throwing.
  544. dns::MessageRenderer renderer;
  545. ASSERT_NO_THROW(request->toWire(renderer));
  546. }
  547. void checkRemoveRevPtrsRequest(NameChangeTransaction& tran) {
  548. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  549. ASSERT_TRUE(request);
  550. // Safety check.
  551. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  552. ASSERT_TRUE(ncr);
  553. std::string exp_zone_name = tran.getReverseDomain()->getName();
  554. std::string exp_rev_addr = D2CfgMgr::reverseIpAddress(ncr->getIpAddress());
  555. // Verify the zone section.
  556. checkZone(request, exp_zone_name);
  557. // Verify there is 1 RR in the PREREQUISITE Section.
  558. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 1);
  559. // Verify the FQDN-PTRNAME assertion RR.
  560. dns::RRsetPtr rrset;
  561. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  562. SECTION_PREREQUISITE, 0));
  563. checkRR(rrset, exp_rev_addr, dns::RRClass::IN(), dns::RRType::PTR(),
  564. 0, ncr);
  565. // Verify there is 1 RR in the UPDATE Section.
  566. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 1);
  567. // Verify the delete all for the FQDN RR.
  568. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  569. SECTION_UPDATE, 0));
  570. checkRR(rrset, exp_rev_addr, dns::RRClass::ANY(), dns::RRType::ANY(),
  571. 0, ncr);
  572. // Verify that it will render toWire without throwing.
  573. dns::MessageRenderer renderer;
  574. ASSERT_NO_THROW(request->toWire(renderer));
  575. }
  576. std::string toHexText(const uint8_t* data, size_t len) {
  577. std::ostringstream stream;
  578. stream << "Data length is: " << len << std::endl;
  579. for (int i = 0; i < len; ++i) {
  580. if (i > 0 && ((i % 16) == 0)) {
  581. stream << std::endl;
  582. }
  583. stream << setfill('0') << setw(2) << setbase(16)
  584. << static_cast<unsigned int>(data[i]) << " ";
  585. }
  586. return (stream.str());
  587. }
  588. }; // namespace isc::d2
  589. }; // namespace isc