nc_test_utils.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Copyright (C) 2013 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 <gtest/gtest.h>
  19. using namespace std;
  20. using namespace isc;
  21. using namespace isc::d2;
  22. namespace isc {
  23. namespace d2 {
  24. const char* TEST_DNS_SERVER_IP = "127.0.0.1";
  25. size_t TEST_DNS_SERVER_PORT = 5301;
  26. FauxServer::FauxServer(asiolink::IOService& io_service,
  27. asiolink::IOAddress& address, size_t port)
  28. :io_service_(io_service), address_(address), port_(port),
  29. server_socket_() {
  30. server_socket_.reset(new asio::ip::udp::socket(io_service_.get_io_service(),
  31. asio::ip::udp::v4()));
  32. server_socket_->set_option(asio::socket_base::reuse_address(true));
  33. server_socket_->bind(asio::ip::udp::endpoint(address_.getAddress(), port_));
  34. }
  35. FauxServer::FauxServer(asiolink::IOService& io_service,
  36. DnsServerInfo& server)
  37. :io_service_(io_service), address_(server.getIpAddress()),
  38. port_(server.getPort()), server_socket_() {
  39. server_socket_.reset(new asio::ip::udp::socket(io_service_.get_io_service(),
  40. asio::ip::udp::v4()));
  41. server_socket_->set_option(asio::socket_base::reuse_address(true));
  42. server_socket_->bind(asio::ip::udp::endpoint(address_.getAddress(), port_));
  43. }
  44. FauxServer::~FauxServer() {
  45. }
  46. void
  47. FauxServer::receive (const ResponseMode& response_mode,
  48. const dns::Rcode& response_rcode) {
  49. server_socket_->async_receive_from(asio::buffer(receive_buffer_,
  50. sizeof(receive_buffer_)),
  51. remote_,
  52. boost::bind(&FauxServer::requestHandler,
  53. this, _1, _2,
  54. response_mode,
  55. response_rcode));
  56. }
  57. void
  58. FauxServer::requestHandler(const asio::error_code& error,
  59. std::size_t bytes_recvd,
  60. const ResponseMode& response_mode,
  61. const dns::Rcode& response_rcode) {
  62. // If we encountered an error or received no data then fail.
  63. // We expect the client to send good requests.
  64. if (error.value() != 0 || bytes_recvd < 1) {
  65. ADD_FAILURE() << "FauxServer receive failed" << error.message();
  66. return;
  67. }
  68. // We have a successfully received data. We need to turn it into
  69. // a request in order to build a proper response.
  70. // Note D2UpdateMessage is geared towards making requests and
  71. // reading responses. This is the opposite perspective so we have
  72. // to a bit of roll-your-own here.
  73. dns::Message request(dns::Message::PARSE);
  74. util::InputBuffer request_buf(receive_buffer_, bytes_recvd);
  75. try {
  76. request.fromWire(request_buf);
  77. } catch (const std::exception& ex) {
  78. // If the request cannot be parsed, then fail the test.
  79. // We expect the client to send good requests.
  80. ADD_FAILURE() << "FauxServer request is corrupt:" << ex.what();
  81. return;
  82. }
  83. // The request parsed ok, so let's build a response.
  84. // We must use the QID we received in the response or IOFetch will
  85. // toss the response out, resulting in eventual timeout.
  86. // We fill in the zone with data we know is from the "server".
  87. dns::Message response(dns::Message::RENDER);
  88. response.setQid(request.getQid());
  89. dns::Question question(dns::Name("response.example.com"),
  90. dns::RRClass::ANY(), dns::RRType::SOA());
  91. response.addQuestion(question);
  92. response.setOpcode(dns::Opcode(dns::Opcode::UPDATE_CODE));
  93. response.setHeaderFlag(dns::Message::HEADERFLAG_QR, true);
  94. // Set the response Rcode to value passed in, default is NOERROR.
  95. response.setRcode(response_rcode);
  96. // Render the response to a buffer.
  97. dns::MessageRenderer renderer;
  98. util::OutputBuffer response_buf(TEST_MSG_MAX);
  99. renderer.setBuffer(&response_buf);
  100. response.toWire(renderer);
  101. // If mode is to ship garbage, then stomp on part of the rendered
  102. // message.
  103. if (response_mode == CORRUPT_RESP) {
  104. response_buf.writeUint16At(0xFFFF, 2);
  105. }
  106. // Ship the reponse via synchronous send.
  107. try {
  108. int cnt = server_socket_->send_to(asio::
  109. buffer(response_buf.getData(),
  110. response_buf.getLength()),
  111. remote_);
  112. // Make sure we sent what we expect to send.
  113. if (cnt != response_buf.getLength()) {
  114. ADD_FAILURE() << "FauxServer sent: " << cnt << " expected: "
  115. << response_buf.getLength();
  116. }
  117. } catch (const std::exception& ex) {
  118. ADD_FAILURE() << "FauxServer send failed: " << ex.what();
  119. }
  120. }
  121. void
  122. checkRRCount(const D2UpdateMessagePtr& request,
  123. D2UpdateMessage::UpdateMsgSection section, int count) {
  124. dns::RRsetIterator rrset_it = request->beginSection(section);
  125. dns::RRsetIterator rrset_end = request->endSection(section);
  126. ASSERT_EQ(count, std::distance(rrset_it, rrset_end));
  127. }
  128. void
  129. checkZone(const D2UpdateMessagePtr& request, const std::string& exp_zone_name) {
  130. // Verify the zone section info.
  131. D2ZonePtr zone = request->getZone();
  132. EXPECT_TRUE(zone);
  133. EXPECT_EQ(exp_zone_name, zone->getName().toText());
  134. EXPECT_EQ(dns::RRClass::IN().getCode(), zone->getClass().getCode());
  135. }
  136. void
  137. checkRR(dns::RRsetPtr rrset, const std::string& exp_name,
  138. const dns::RRClass& exp_class, const dns::RRType& exp_type,
  139. unsigned int exp_ttl, dhcp_ddns::NameChangeRequestPtr ncr) {
  140. // Verify the FQDN/DHCID RR fields.
  141. EXPECT_EQ(exp_name, rrset->getName().toText());
  142. EXPECT_EQ(exp_class.getCode(), rrset->getClass().getCode());
  143. EXPECT_EQ(exp_type.getCode(), rrset->getType().getCode());
  144. EXPECT_EQ(exp_ttl, rrset->getTTL().getValue());
  145. if (exp_type == dns::RRType::ANY() || exp_class == dns::RRClass::ANY()) {
  146. // ANY types do not have RData
  147. ASSERT_EQ(0, rrset->getRdataCount());
  148. return;
  149. }
  150. ASSERT_EQ(1, rrset->getRdataCount());
  151. dns::RdataIteratorPtr rdata_it = rrset->getRdataIterator();
  152. ASSERT_TRUE(rdata_it);
  153. if ((exp_type == dns::RRType::A()) ||
  154. (exp_type == dns::RRType::AAAA())) {
  155. // should have lease rdata
  156. EXPECT_EQ(ncr->getIpAddress(), rdata_it->getCurrent().toText());
  157. } else if (exp_type == dns::RRType::PTR()) {
  158. // should have PTR rdata
  159. EXPECT_EQ(ncr->getFqdn(), rdata_it->getCurrent().toText());
  160. } else if (exp_type == dns::RRType::DHCID()) {
  161. // should have DHCID rdata
  162. const std::vector<uint8_t>& ncr_dhcid = ncr->getDhcid().getBytes();
  163. util::InputBuffer buffer(ncr_dhcid.data(), ncr_dhcid.size());
  164. dns::rdata::in::DHCID rdata_ref(buffer, ncr_dhcid.size());
  165. EXPECT_EQ(0, rdata_ref.compare(rdata_it->getCurrent()));
  166. } else {
  167. // we got a problem
  168. FAIL();
  169. }
  170. }
  171. dns::RRsetPtr
  172. getRRFromSection(const D2UpdateMessagePtr& request,
  173. D2UpdateMessage::UpdateMsgSection section, int index) {
  174. dns::RRsetIterator rrset_it = request->beginSection(section);
  175. dns::RRsetIterator rrset_end = request->endSection(section);
  176. if (std::distance(rrset_it, rrset_end) <= index) {
  177. // Return an empty pointer if index is out of range.
  178. return (dns::RRsetPtr());
  179. }
  180. std::advance(rrset_it, index);
  181. return (*rrset_it);
  182. }
  183. dhcp_ddns::NameChangeRequestPtr makeNcrFromString(const std::string& ncr_str) {
  184. return (dhcp_ddns::NameChangeRequest::fromJSON(ncr_str));
  185. }
  186. DdnsDomainPtr makeDomain(const std::string& zone_name,
  187. const std::string& key_name) {
  188. DdnsDomainPtr domain;
  189. DnsServerInfoStoragePtr servers(new DnsServerInfoStorage());
  190. domain.reset(new DdnsDomain(zone_name, key_name, servers));
  191. return (domain);
  192. }
  193. void addDomainServer(DdnsDomainPtr& domain, const std::string& name,
  194. const std::string& ip, const size_t port) {
  195. DnsServerInfoPtr server(new DnsServerInfo(name, asiolink::IOAddress(ip),
  196. port));
  197. domain->getServers()->push_back(server);
  198. }
  199. // Verifies that the contents of the given transaction's DNS update request
  200. // is correct for adding a forward DNS entry
  201. void checkForwardAddRequest(NameChangeTransaction& tran) {
  202. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  203. ASSERT_TRUE(request);
  204. // Safety check.
  205. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  206. ASSERT_TRUE(ncr);
  207. std::string exp_zone_name = tran.getForwardDomain()->getName();
  208. std::string exp_fqdn = ncr->getFqdn();
  209. const dns::RRType& exp_ip_rr_type = tran.getAddressRRType();
  210. // Verify the zone section.
  211. checkZone(request, exp_zone_name);
  212. // Verify the PREREQUISITE SECTION
  213. // Should be 1 which tests for FQDN does not exist.
  214. dns::RRsetPtr rrset;
  215. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 1);
  216. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  217. SECTION_PREREQUISITE, 0));
  218. checkRR(rrset, exp_fqdn, dns::RRClass::NONE(), dns::RRType::ANY(), 0, ncr);
  219. // Verify the UPDATE SECTION
  220. // Should be 2 RRs: 1 to add the FQDN/IP and one to add the DHCID RR
  221. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 2);
  222. // First, Verify the FQDN/IP add RR.
  223. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  224. SECTION_UPDATE, 0));
  225. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), exp_ip_rr_type, 0, ncr);
  226. // Now, verify the DHCID add RR.
  227. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  228. SECTION_UPDATE, 1));
  229. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), dns::RRType::DHCID(), 0, ncr);
  230. // Verify there are no RRs in the ADDITIONAL Section.
  231. checkRRCount(request, D2UpdateMessage::SECTION_ADDITIONAL, 0);
  232. // Verify that it will render toWire without throwing.
  233. dns::MessageRenderer renderer;
  234. ASSERT_NO_THROW(request->toWire(renderer));
  235. }
  236. // Verifies that the contents of the given transaction's DNS update request
  237. // is correct for replacing a forward DNS entry
  238. void checkForwardReplaceRequest(NameChangeTransaction& tran) {
  239. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  240. ASSERT_TRUE(request);
  241. // Safety check.
  242. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  243. ASSERT_TRUE(ncr);
  244. std::string exp_zone_name = tran.getForwardDomain()->getName();
  245. std::string exp_fqdn = ncr->getFqdn();
  246. const dns::RRType& exp_ip_rr_type = tran.getAddressRRType();
  247. // Verify the zone section.
  248. checkZone(request, exp_zone_name);
  249. // Verify the PREREQUISITE SECTION
  250. // Should be 2, 1 which tests for FQDN does exist and the other
  251. // checks for a matching DHCID.
  252. dns::RRsetPtr rrset;
  253. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 2);
  254. // Verify the FQDN test RR.
  255. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  256. SECTION_PREREQUISITE, 0));
  257. checkRR(rrset, exp_fqdn, dns::RRClass::ANY(), dns::RRType::ANY(), 0, ncr);
  258. // Verify the DHCID test RR.
  259. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  260. SECTION_PREREQUISITE, 1));
  261. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), dns::RRType::DHCID(), 0, ncr);
  262. // Verify the UPDATE SECTION
  263. // Should be 2, 1 which deletes the existing FQDN/IP and one that
  264. // adds the new one.
  265. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 2);
  266. // Verify the FQDN delete RR.
  267. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  268. SECTION_UPDATE, 0));
  269. checkRR(rrset, exp_fqdn, dns::RRClass::ANY(), exp_ip_rr_type, 0, ncr);
  270. // Verify the FQDN/IP add RR.
  271. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  272. SECTION_UPDATE, 1));
  273. checkRR(rrset, exp_fqdn, dns::RRClass::IN(), exp_ip_rr_type, 0, ncr);
  274. // Verify there are no RRs in the ADDITIONAL Section.
  275. checkRRCount(request, D2UpdateMessage::SECTION_ADDITIONAL, 0);
  276. // Verify that it will render toWire without throwing.
  277. dns::MessageRenderer renderer;
  278. ASSERT_NO_THROW(request->toWire(renderer));
  279. }
  280. // Verifies that the contents of the given transaction's DNS update request
  281. // is correct for replacing a reverse DNS entry
  282. void checkReverseReplaceRequest(NameChangeTransaction& tran) {
  283. const D2UpdateMessagePtr& request = tran.getDnsUpdateRequest();
  284. ASSERT_TRUE(request);
  285. // Safety check.
  286. dhcp_ddns::NameChangeRequestPtr ncr = tran.getNcr();
  287. ASSERT_TRUE(ncr);
  288. std::string exp_zone_name = tran.getReverseDomain()->getName();
  289. std::string exp_rev_addr = D2CfgMgr::reverseIpAddress(ncr->getIpAddress());
  290. // Verify the zone section.
  291. checkZone(request, exp_zone_name);
  292. // Verify there are no RRs in the PREREQUISITE Section.
  293. checkRRCount(request, D2UpdateMessage::SECTION_PREREQUISITE, 0);
  294. // Verify the UPDATE Section.
  295. // It should contain 4 RRs:
  296. // 1. A delete all PTR RRs for the given IP
  297. // 2. A delete of all DHCID RRs for the given IP
  298. // 3. An add of the new PTR RR
  299. // 4. An add of the new DHCID RR
  300. dns::RRsetPtr rrset;
  301. checkRRCount(request, D2UpdateMessage::SECTION_UPDATE, 4);
  302. // Verify the PTR delete RR.
  303. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  304. SECTION_UPDATE, 0));
  305. checkRR(rrset, exp_rev_addr, dns::RRClass::ANY(), dns::RRType::PTR(),
  306. 0, ncr);
  307. // Verify the DHCID delete RR.
  308. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  309. SECTION_UPDATE, 1));
  310. checkRR(rrset, exp_rev_addr, dns::RRClass::ANY(), dns::RRType::DHCID(),
  311. 0, ncr);
  312. // Verify the PTR add RR.
  313. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  314. SECTION_UPDATE, 2));
  315. checkRR(rrset, exp_rev_addr, dns::RRClass::IN(), dns::RRType::PTR(),
  316. 0, ncr);
  317. // Verify the DHCID add RR.
  318. ASSERT_TRUE(rrset = getRRFromSection(request, D2UpdateMessage::
  319. SECTION_UPDATE, 3));
  320. checkRR(rrset, exp_rev_addr, dns::RRClass::IN(), dns::RRType::DHCID(),
  321. 0, ncr);
  322. // Verify there are no RRs in the ADDITIONAL Section.
  323. checkRRCount(request, D2UpdateMessage::SECTION_ADDITIONAL, 0);
  324. // Verify that it will render toWire without throwing.
  325. dns::MessageRenderer renderer;
  326. ASSERT_NO_THROW(request->toWire(renderer));
  327. }
  328. }; // namespace isc::d2
  329. }; // namespace isc