nc_test_utils.cc 28 KB

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