recursive_query_unittest.cc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. // Copyright (C) 2011 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 <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <sys/time.h>
  18. #include <cstring>
  19. #include <boost/lexical_cast.hpp>
  20. #include <boost/bind.hpp>
  21. #include <boost/scoped_ptr.hpp>
  22. #include <boost/date_time/posix_time/posix_time_types.hpp>
  23. #include <gtest/gtest.h>
  24. #include <exceptions/exceptions.h>
  25. #include <dns/tests/unittest_util.h>
  26. #include <dns/rcode.h>
  27. #include <util/buffer.h>
  28. #include <util/unittests/resolver.h>
  29. #include <dns/message.h>
  30. #include <dns/rdataclass.h>
  31. #include <nsas/nameserver_address_store.h>
  32. #include <cache/resolver_cache.h>
  33. #include <resolve/resolve.h>
  34. // IMPORTANT: We shouldn't directly use ASIO definitions in this test.
  35. // In particular, we must not include asio.hpp in this file.
  36. // The asiolink module is primarily intended to be a wrapper that hide the
  37. // details of the underlying implementations. We need to test the wrapper
  38. // level behaviors. In addition, some compilers reject to compile this file
  39. // if we include asio.hpp unless we specify a special compiler option.
  40. // If we need to test something at the level of underlying ASIO and need
  41. // their definition, that test should go to asiolink/internal/tests.
  42. #include <resolve/recursive_query.h>
  43. #include <asiodns/dns_lookup.h>
  44. #include <asiolink/io_socket.h>
  45. #include <asiolink/io_service.h>
  46. #include <asiolink/io_message.h>
  47. #include <asiolink/io_error.h>
  48. #include <asiolink/simple_callback.h>
  49. using isc::UnitTestUtil;
  50. using namespace std;
  51. using namespace isc::asiodns;
  52. using namespace isc::asiolink;
  53. using namespace isc::dns;
  54. using namespace isc::util;
  55. using boost::scoped_ptr;
  56. namespace isc {
  57. namespace asiodns {
  58. // This is defined in recursive_query.cc, but not in header (it's not public
  59. // function). So bring it in to be tested.
  60. std::string
  61. deepestDelegation(Name name, RRClass rrclass,
  62. isc::cache::ResolverCache& cache);
  63. }
  64. }
  65. namespace {
  66. const char* const TEST_SERVER_PORT = "53535";
  67. const char* const TEST_CLIENT_PORT = "53536";
  68. const char* const TEST_IPV6_ADDR = "::1";
  69. const char* const TEST_IPV4_ADDR = "127.0.0.1";
  70. // This data is intended to be valid as a DNS/TCP-like message: the first
  71. // two octets encode the length of the rest of the data. This is crucial
  72. // for the tests below.
  73. const uint8_t test_data[] = {0, 4, 1, 2, 3, 4};
  74. // This function returns an addrinfo structure for use by tests.
  75. struct addrinfo*
  76. resolveAddress(const int protocol, const char* const addr,
  77. const char* const port)
  78. {
  79. struct addrinfo hints;
  80. memset(&hints, 0, sizeof(hints));
  81. hints.ai_family = AF_UNSPEC; // let the address decide it.
  82. hints.ai_socktype = (protocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  83. hints.ai_protocol = protocol;
  84. hints.ai_flags = AI_NUMERICSERV;
  85. struct addrinfo* res;
  86. const int error = getaddrinfo(addr, port, &hints, &res);
  87. if (error != 0) {
  88. isc_throw(IOError, "getaddrinfo failed: " << gai_strerror(error));
  89. }
  90. return (res);
  91. }
  92. // convenience shortcut of the other version using different addresses and
  93. // ports depending on whether we're testing IPv4 or v6, TCP or UDP, and
  94. // client or server operation.
  95. struct addrinfo*
  96. resolveAddress(const int family, const int protocol, const bool client) {
  97. return (resolveAddress(protocol,
  98. (family == AF_INET6) ? TEST_IPV6_ADDR :
  99. TEST_IPV4_ADDR,
  100. client ? TEST_CLIENT_PORT : TEST_SERVER_PORT));
  101. }
  102. // A helper holder of addrinfo so we can safely release the resource
  103. // either when leaving the defined scope either normally or due to exception.
  104. struct ScopedAddrInfo {
  105. ScopedAddrInfo(struct addrinfo* res) : res_(res) {}
  106. ~ScopedAddrInfo() { freeaddrinfo(res_); }
  107. struct addrinfo* res_;
  108. };
  109. // Similar to ScopedAddrInfo but for socket FD. It also supports the "release"
  110. // operation so it can release the ownership of the FD.
  111. struct ScopedSocket {
  112. ScopedSocket(int s) : s_(s) {}
  113. ~ScopedSocket() {
  114. if (s_ >= 0) {
  115. close(s_);
  116. }
  117. }
  118. int release() {
  119. int s = s_;
  120. s_ = -1;
  121. return (s);
  122. }
  123. int s_;
  124. };
  125. // This fixture is a framework for various types of network operations
  126. // using the ASIO interfaces. Each test case creates an IOService object,
  127. // opens a local "client" socket for testing, sends data via the local socket
  128. // to the service that would run in the IOService object.
  129. // A mock callback function (an ASIOCallBack object) is registered with the
  130. // IOService object, so the test code should be able to examine the data
  131. // received on the server side. It then checks the received data matches
  132. // expected parameters.
  133. // If initialization parameters of the IOService should be modified, the test
  134. // case can do it using the setDNSService() method.
  135. // Note: the set of tests in RecursiveQueryTest use actual network services and may
  136. // involve undesirable side effects such as blocking.
  137. class RecursiveQueryTest : public ::testing::Test {
  138. protected:
  139. RecursiveQueryTest();
  140. ~RecursiveQueryTest() {
  141. // It would delete itself, but after the io_service_, which could
  142. // segfailt in case there were unhandled requests
  143. resolver_.reset();
  144. if (sock_ != -1) {
  145. close(sock_);
  146. }
  147. }
  148. // Send a test UDP packet to a mock server
  149. void sendUDP(const int family) {
  150. ScopedAddrInfo sai(resolveAddress(family, IPPROTO_UDP, false));
  151. struct addrinfo* res = sai.res_;
  152. sock_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  153. if (sock_ < 0) {
  154. isc_throw(IOError, "failed to open test socket");
  155. }
  156. const int cc = sendto(sock_, test_data, sizeof(test_data), 0,
  157. res->ai_addr, res->ai_addrlen);
  158. if (cc != sizeof(test_data)) {
  159. isc_throw(IOError, "unexpected sendto result: " << cc);
  160. }
  161. io_service_->run();
  162. }
  163. // Send a test TCP packet to a mock server
  164. void sendTCP(const int family) {
  165. ScopedAddrInfo sai(resolveAddress(family, IPPROTO_TCP, false));
  166. struct addrinfo* res = sai.res_;
  167. sock_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  168. if (sock_ < 0) {
  169. isc_throw(IOError, "failed to open test socket");
  170. }
  171. if (connect(sock_, res->ai_addr, res->ai_addrlen) < 0) {
  172. isc_throw(IOError, "failed to connect to the test server");
  173. }
  174. const int cc = send(sock_, test_data, sizeof(test_data), 0);
  175. if (cc != sizeof(test_data)) {
  176. isc_throw(IOError, "unexpected send result: " << cc);
  177. }
  178. io_service_->run();
  179. }
  180. // Receive a UDP packet from a mock server; used for testing
  181. // recursive lookup. The caller must place a RecursiveQuery
  182. // on the IO Service queue before running this routine.
  183. void recvUDP(const int family, void* buffer, size_t& size) {
  184. ScopedAddrInfo sai(resolveAddress(family, IPPROTO_UDP, true));
  185. struct addrinfo* res = sai.res_;
  186. sock_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  187. if (sock_ < 0) {
  188. isc_throw(IOError, "failed to open test socket");
  189. }
  190. if (bind(sock_, res->ai_addr, res->ai_addrlen) < 0) {
  191. isc_throw(IOError, "bind failed: " << strerror(errno));
  192. }
  193. // The IO service queue should have a RecursiveQuery object scheduled
  194. // to run at this point. This call will cause it to begin an
  195. // async send, then return.
  196. io_service_->run_one();
  197. // ... and this one will block until the send has completed
  198. io_service_->run_one();
  199. // Now we attempt to recv() whatever was sent.
  200. // XXX: there's no guarantee the receiving socket can immediately get
  201. // the packet. Normally we can perform blocking recv to wait for it,
  202. // but in theory it's even possible that the packet is lost.
  203. // In order to prevent the test from hanging in such a worst case
  204. // we add an ad hoc timeout.
  205. const struct timeval timeo = { 10, 0 };
  206. int recv_options = 0;
  207. if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo,
  208. sizeof(timeo))) {
  209. if (errno == ENOPROTOOPT) {
  210. // Workaround for Solaris: it doesn't accept SO_RCVTIMEO
  211. // with the error of ENOPROTOOPT. Since this is a workaround
  212. // for rare error cases anyway, we simply switch to the
  213. // "don't wait" mode. If we still find an error in recv()
  214. // can happen often we'll consider a more complete solution.
  215. recv_options = MSG_DONTWAIT;
  216. } else {
  217. isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
  218. }
  219. }
  220. const int ret = recv(sock_, buffer, size, recv_options);
  221. if (ret < 0) {
  222. isc_throw(IOError, "recvfrom failed: " << strerror(errno));
  223. }
  224. // Pass the message size back via the size parameter
  225. size = ret;
  226. }
  227. void
  228. addServer(const string& address, const char* const port, int protocol) {
  229. ScopedAddrInfo sai(resolveAddress(protocol, address.c_str(), port));
  230. struct addrinfo* res = sai.res_;
  231. const int family = res->ai_family;
  232. ScopedSocket sock(socket(res->ai_family, res->ai_socktype,
  233. res->ai_protocol));
  234. const int s = sock.s_;
  235. if (s < 0) {
  236. isc_throw(isc::Unexpected, "failed to open a test socket");
  237. }
  238. const int on = 1;
  239. if (family == AF_INET6) {
  240. if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) ==
  241. -1) {
  242. isc_throw(isc::Unexpected,
  243. "failed to set socket option(IPV6_V6ONLY)");
  244. }
  245. }
  246. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  247. isc_throw(isc::Unexpected,
  248. "failed to set socket option(SO_REUSEADDR)");
  249. }
  250. if (bind(s, res->ai_addr, res->ai_addrlen) != 0) {
  251. isc_throw(isc::Unexpected, "failed to bind a test socket");
  252. }
  253. if (protocol == IPPROTO_TCP) {
  254. dns_service_->addServerTCPFromFD(sock.release(), family);
  255. } else {
  256. dns_service_->addServerUDPFromFD(sock.release(), family);
  257. }
  258. }
  259. // Set up an IO Service queue using the specified address
  260. void setDNSService(const string& address) {
  261. setDNSService();
  262. addServer(address, TEST_SERVER_PORT, IPPROTO_TCP);
  263. addServer(address, TEST_SERVER_PORT, IPPROTO_UDP);
  264. }
  265. // Set up an IO Service queue using the "any" address, on IPv4 if
  266. // 'use_ipv4' is true and on IPv6 if 'use_ipv6' is true.
  267. void setDNSService(const bool use_ipv4, const bool use_ipv6) {
  268. setDNSService();
  269. if (use_ipv6) {
  270. addServer("::", TEST_SERVER_PORT, IPPROTO_TCP);
  271. addServer("::", TEST_SERVER_PORT, IPPROTO_UDP);
  272. }
  273. if (use_ipv4) {
  274. addServer("0.0.0.0", TEST_SERVER_PORT, IPPROTO_TCP);
  275. addServer("0.0.0.0", TEST_SERVER_PORT, IPPROTO_UDP);
  276. }
  277. }
  278. // Set up empty DNS Service
  279. // Set up an IO Service queue without any addresses
  280. void setDNSService() {
  281. io_service_.reset(new IOService());
  282. callback_.reset(new ASIOCallBack(this));
  283. dns_service_.reset(new DNSService(*io_service_, callback_.get(), NULL,
  284. NULL));
  285. }
  286. // Run a simple server test, on either IPv4 or IPv6, and over either
  287. // UDP or TCP. Calls the sendUDP() or sendTCP() methods, which will
  288. // start the IO Service queue. The UDPServer or TCPServer that was
  289. // created by setIOService() will receive the test packet and issue a
  290. // callback, which enables us to check that the data it received
  291. // matches what we sent.
  292. void doTest(const int family, const int protocol) {
  293. if (protocol == IPPROTO_UDP) {
  294. sendUDP(family);
  295. } else {
  296. sendTCP(family);
  297. }
  298. // There doesn't seem to be an effective test for the validity of
  299. // 'native'.
  300. // One thing we are sure is it must be different from our local socket.
  301. EXPECT_NE(sock_, callback_native_);
  302. EXPECT_EQ(protocol, callback_protocol_);
  303. EXPECT_EQ(family == AF_INET6 ? TEST_IPV6_ADDR : TEST_IPV4_ADDR,
  304. callback_address_);
  305. const uint8_t* expected_data =
  306. protocol == IPPROTO_UDP ? test_data : test_data + 2;
  307. const size_t expected_datasize =
  308. protocol == IPPROTO_UDP ? sizeof(test_data) :
  309. sizeof(test_data) - 2;
  310. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &callback_data_[0],
  311. callback_data_.size(),
  312. expected_data, expected_datasize);
  313. }
  314. protected:
  315. // This is a nonfunctional mockup of a DNSServer object. Its purpose
  316. // is to resume after a recursive query or other asynchronous call
  317. // has completed.
  318. class MockServer : public DNSServer {
  319. public:
  320. explicit MockServer(IOService& io_service,
  321. SimpleCallback* checkin = NULL,
  322. DNSLookup* lookup = NULL,
  323. DNSAnswer* answer = NULL) :
  324. io_(io_service),
  325. done_(false),
  326. message_(new Message(Message::PARSE)),
  327. answer_message_(new Message(Message::RENDER)),
  328. respbuf_(new OutputBuffer(0)),
  329. checkin_(checkin), lookup_(lookup), answer_(answer)
  330. {}
  331. void operator()(asio::error_code ec = asio::error_code(),
  332. size_t length = 0)
  333. {}
  334. void resume(const bool) {
  335. // should never be called in our tests
  336. }
  337. DNSServer* clone() {
  338. MockServer* s = new MockServer(*this);
  339. return (s);
  340. }
  341. inline void asyncLookup() {
  342. if (lookup_) {
  343. (*lookup_)(*io_message_, message_, answer_message_,
  344. respbuf_, this);
  345. }
  346. }
  347. protected:
  348. IOService& io_;
  349. bool done_;
  350. private:
  351. // Currently unused; these will be used for testing
  352. // asynchronous lookup calls via the asyncLookup() method
  353. boost::shared_ptr<isc::asiolink::IOMessage> io_message_;
  354. isc::dns::MessagePtr message_;
  355. isc::dns::MessagePtr answer_message_;
  356. isc::util::OutputBufferPtr respbuf_;
  357. // Callback functions provided by the caller
  358. const SimpleCallback* checkin_;
  359. const DNSLookup* lookup_;
  360. const DNSAnswer* answer_;
  361. };
  362. // This version of mock server just stops the io_service when it is resumed
  363. class MockServerStop : public MockServer {
  364. public:
  365. explicit MockServerStop(IOService& io_service, bool* done) :
  366. MockServer(io_service),
  367. done_(done)
  368. {}
  369. void resume(const bool done) {
  370. *done_ = done;
  371. io_.stop();
  372. }
  373. DNSServer* clone() {
  374. return (new MockServerStop(*this));
  375. }
  376. private:
  377. bool* done_;
  378. };
  379. // This version of mock server just stops the io_service when it is resumed
  380. // the second time. (Used in the clientTimeout test, where resume
  381. // is called initially with the error answer, and later when the
  382. // lookup times out, it is called without an answer to send back)
  383. class MockServerStop2 : public MockServer {
  384. public:
  385. explicit MockServerStop2(IOService& io_service,
  386. bool* done1, bool* done2) :
  387. MockServer(io_service),
  388. done1_(done1),
  389. done2_(done2),
  390. stopped_once_(false)
  391. {}
  392. void resume(const bool done) {
  393. if (stopped_once_) {
  394. *done2_ = done;
  395. io_.stop();
  396. } else {
  397. *done1_ = done;
  398. stopped_once_ = true;
  399. }
  400. }
  401. DNSServer* clone() {
  402. return (new MockServerStop2(*this));
  403. }
  404. private:
  405. bool* done1_;
  406. bool* done2_;
  407. bool stopped_once_;
  408. };
  409. private:
  410. class ASIOCallBack : public SimpleCallback {
  411. public:
  412. ASIOCallBack(RecursiveQueryTest* test_obj) : test_obj_(test_obj) {}
  413. void operator()(const IOMessage& io_message) const {
  414. test_obj_->callBack(io_message);
  415. }
  416. private:
  417. RecursiveQueryTest* test_obj_;
  418. };
  419. void callBack(const IOMessage& io_message) {
  420. callback_protocol_ = io_message.getSocket().getProtocol();
  421. callback_native_ = io_message.getSocket().getNative();
  422. callback_address_ =
  423. io_message.getRemoteEndpoint().getAddress().toText();
  424. callback_data_.assign(
  425. static_cast<const uint8_t*>(io_message.getData()),
  426. static_cast<const uint8_t*>(io_message.getData()) +
  427. io_message.getDataSize());
  428. io_service_->stop();
  429. }
  430. protected:
  431. // We use a pointer for io_service_, because for some tests we
  432. // need to recreate a new one within one onstance of this class
  433. scoped_ptr<IOService> io_service_;
  434. scoped_ptr<DNSService> dns_service_;
  435. scoped_ptr<isc::nsas::NameserverAddressStore> nsas_;
  436. isc::cache::ResolverCache cache_;
  437. scoped_ptr<ASIOCallBack> callback_;
  438. int callback_protocol_;
  439. int callback_native_;
  440. string callback_address_;
  441. vector<uint8_t> callback_data_;
  442. int sock_;
  443. boost::shared_ptr<isc::util::unittests::TestResolver> resolver_;
  444. };
  445. RecursiveQueryTest::RecursiveQueryTest() :
  446. dns_service_(NULL), callback_(NULL), callback_protocol_(0),
  447. callback_native_(-1), sock_(-1),
  448. resolver_(new isc::util::unittests::TestResolver())
  449. {
  450. io_service_.reset(new IOService());
  451. setDNSService(true, true);
  452. nsas_.reset(new isc::nsas::NameserverAddressStore(resolver_));
  453. }
  454. TEST_F(RecursiveQueryTest, v6UDPSend) {
  455. doTest(AF_INET6, IPPROTO_UDP);
  456. }
  457. TEST_F(RecursiveQueryTest, v6TCPSend) {
  458. doTest(AF_INET6, IPPROTO_TCP);
  459. }
  460. TEST_F(RecursiveQueryTest, v4UDPSend) {
  461. doTest(AF_INET, IPPROTO_UDP);
  462. }
  463. TEST_F(RecursiveQueryTest, v4TCPSend) {
  464. doTest(AF_INET, IPPROTO_TCP);
  465. }
  466. TEST_F(RecursiveQueryTest, v6UDPSendSpecific) {
  467. // Explicitly set a specific address to be bound to the socket.
  468. // The subsequent test does not directly ensures the underlying socket
  469. // is bound to the expected address, but the success of the tests should
  470. // reasonably suggest it works as intended.
  471. // Specifying an address also implicitly means the service runs in a
  472. // single address-family mode. In tests using TCP we can confirm that
  473. // by trying to make a connection and seeing a failure. In UDP, it'd be
  474. // more complicated because we need to use a connected socket and catch
  475. // an error on a subsequent read operation. We could do it, but for
  476. // simplicity we only tests the easier cases for now.
  477. setDNSService(TEST_IPV6_ADDR);
  478. doTest(AF_INET6, IPPROTO_UDP);
  479. }
  480. TEST_F(RecursiveQueryTest, v6TCPSendSpecific) {
  481. setDNSService(TEST_IPV6_ADDR);
  482. doTest(AF_INET6, IPPROTO_TCP);
  483. EXPECT_THROW(sendTCP(AF_INET), IOError);
  484. }
  485. TEST_F(RecursiveQueryTest, v4UDPSendSpecific) {
  486. setDNSService(TEST_IPV4_ADDR);
  487. doTest(AF_INET, IPPROTO_UDP);
  488. }
  489. TEST_F(RecursiveQueryTest, v4TCPSendSpecific) {
  490. setDNSService(TEST_IPV4_ADDR);
  491. doTest(AF_INET, IPPROTO_TCP);
  492. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  493. }
  494. TEST_F(RecursiveQueryTest, v6AddServer) {
  495. setDNSService();
  496. addServer(TEST_IPV6_ADDR, TEST_SERVER_PORT, IPPROTO_TCP);
  497. doTest(AF_INET6, IPPROTO_TCP);
  498. EXPECT_THROW(sendTCP(AF_INET), IOError);
  499. }
  500. TEST_F(RecursiveQueryTest, v4AddServer) {
  501. setDNSService();
  502. addServer(TEST_IPV4_ADDR, TEST_SERVER_PORT, IPPROTO_TCP);
  503. doTest(AF_INET, IPPROTO_TCP);
  504. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  505. }
  506. TEST_F(RecursiveQueryTest, clearServers) {
  507. setDNSService();
  508. dns_service_->clearServers();
  509. EXPECT_THROW(sendTCP(AF_INET), IOError);
  510. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  511. }
  512. TEST_F(RecursiveQueryTest, v6TCPOnly) {
  513. // Open only IPv6 TCP socket. A subsequent attempt of establishing an
  514. // IPv4/TCP connection should fail. See above for why we only test this
  515. // for TCP.
  516. setDNSService(false, true);
  517. EXPECT_THROW(sendTCP(AF_INET), IOError);
  518. }
  519. TEST_F(RecursiveQueryTest, v4TCPOnly) {
  520. setDNSService(true, false);
  521. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  522. }
  523. vector<pair<string, uint16_t> >
  524. singleAddress(const string &address, uint16_t port) {
  525. vector<pair<string, uint16_t> > result;
  526. result.push_back(pair<string, uint16_t>(address, port));
  527. return (result);
  528. }
  529. TEST_F(RecursiveQueryTest, recursiveSetupV4) {
  530. setDNSService(true, false);
  531. uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  532. EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
  533. *nsas_, cache_,
  534. singleAddress(TEST_IPV4_ADDR, port),
  535. singleAddress(TEST_IPV4_ADDR, port)));
  536. }
  537. TEST_F(RecursiveQueryTest, recursiveSetupV6) {
  538. setDNSService(false, true);
  539. uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  540. EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
  541. *nsas_, cache_,
  542. singleAddress(TEST_IPV6_ADDR, port),
  543. singleAddress(TEST_IPV6_ADDR,port)));
  544. }
  545. // XXX:
  546. // This is very inadequate unit testing. It should be generalized into
  547. // a routine that can do this with variable address family, address, and
  548. // port, and with the various callbacks defined in such a way as to ensure
  549. // full code coverage including error cases.
  550. TEST_F(RecursiveQueryTest, forwarderSend) {
  551. setDNSService(true, false);
  552. // Note: We use the test prot plus one to ensure we aren't binding
  553. // to the same port as the actual server
  554. uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  555. MockServer server(*io_service_);
  556. RecursiveQuery rq(*dns_service_,
  557. *nsas_, cache_,
  558. singleAddress(TEST_IPV4_ADDR, port),
  559. singleAddress(TEST_IPV4_ADDR, port));
  560. Question q(Name("example.com"), RRClass::IN(), RRType::TXT());
  561. Message query_message(Message::RENDER);
  562. isc::resolve::initResponseMessage(q, query_message);
  563. OutputBufferPtr buffer(new OutputBuffer(0));
  564. MessagePtr answer(new Message(Message::RENDER));
  565. rq.forward(ConstMessagePtr(&query_message), answer, buffer, &server);
  566. char data[4096];
  567. size_t size = sizeof(data);
  568. ASSERT_NO_THROW(recvUDP(AF_INET, data, size));
  569. Message m(Message::PARSE);
  570. InputBuffer ibuf(data, size);
  571. // Make sure we can parse the message that was sent
  572. EXPECT_NO_THROW(m.parseHeader(ibuf));
  573. EXPECT_NO_THROW(m.fromWire(ibuf));
  574. // Check that the question sent matches the one we wanted
  575. QuestionPtr q2 = *m.beginQuestion();
  576. EXPECT_EQ(q.getName(), q2->getName());
  577. EXPECT_EQ(q.getType(), q2->getType());
  578. EXPECT_EQ(q.getClass(), q2->getClass());
  579. }
  580. int
  581. createTestSocket() {
  582. ScopedAddrInfo sai(resolveAddress(AF_INET, IPPROTO_UDP, true));
  583. struct addrinfo* res = sai.res_;
  584. int sock_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  585. if (sock_ < 0) {
  586. isc_throw(IOError, "failed to open test socket");
  587. }
  588. if (bind(sock_, res->ai_addr, res->ai_addrlen) < 0) {
  589. isc_throw(IOError, "failed to bind test socket");
  590. }
  591. return (sock_);
  592. }
  593. int
  594. setSocketTimeout(int sock_, size_t tv_sec, size_t tv_usec) {
  595. const struct timeval timeo = { tv_sec, tv_usec };
  596. int recv_options = 0;
  597. if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo))) {
  598. if (errno == ENOPROTOOPT) { // see RecursiveQueryTest::recvUDP()
  599. recv_options = MSG_DONTWAIT;
  600. } else {
  601. isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
  602. }
  603. }
  604. return recv_options;
  605. }
  606. // try to read from the socket max time
  607. // *num is incremented for every succesfull read
  608. // returns true if it can read max times, false otherwise
  609. bool tryRead(int sock_, int recv_options, size_t max, int* num) {
  610. size_t i = 0;
  611. do {
  612. char inbuff[512];
  613. if (recv(sock_, inbuff, sizeof(inbuff), recv_options) < 0) {
  614. return false;
  615. } else {
  616. ++i;
  617. ++*num;
  618. }
  619. } while (i < max);
  620. return true;
  621. }
  622. // Mock resolver callback for testing forward query.
  623. class MockResolverCallback : public isc::resolve::ResolverInterface::Callback {
  624. public:
  625. enum ResultValue {
  626. DEFAULT = 0,
  627. SUCCESS = 1,
  628. FAILURE = 2
  629. };
  630. MockResolverCallback(DNSServer* server):
  631. result(DEFAULT),
  632. server_(server->clone())
  633. {}
  634. ~MockResolverCallback() {
  635. delete server_;
  636. }
  637. void success(const isc::dns::MessagePtr response) {
  638. result = SUCCESS;
  639. server_->resume(true);
  640. }
  641. void failure() {
  642. result = FAILURE;
  643. server_->resume(false);
  644. }
  645. uint32_t result;
  646. private:
  647. DNSServer* server_;
  648. };
  649. // Test query timeout, set query timeout is lower than client timeout
  650. // and lookup timeout.
  651. TEST_F(RecursiveQueryTest, forwardQueryTimeout) {
  652. // Prepare the service (we do not use the common setup, we do not answer
  653. setDNSService();
  654. // Prepare the socket
  655. sock_ = createTestSocket();
  656. // Prepare the server
  657. bool done(true);
  658. MockServerStop server(*io_service_, &done);
  659. // Do the answer
  660. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  661. RecursiveQuery query(*dns_service_,
  662. *nsas_, cache_,
  663. singleAddress(TEST_IPV4_ADDR, port),
  664. singleAddress(TEST_IPV4_ADDR, port),
  665. 10, 4000, 3000, 2);
  666. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  667. OutputBufferPtr buffer(new OutputBuffer(0));
  668. MessagePtr answer(new Message(Message::RENDER));
  669. Message query_message(Message::RENDER);
  670. isc::resolve::initResponseMessage(question, query_message);
  671. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  672. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  673. // Run the test
  674. io_service_->run();
  675. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  676. }
  677. // If we set client timeout to lower than querytimeout, we should
  678. // get a failure answer
  679. // (no actual answer is given here yet. TODO the returned error message
  680. // should be tested)
  681. TEST_F(RecursiveQueryTest, forwardClientTimeout) {
  682. // Prepare the service (we do not use the common setup, we do not answer
  683. setDNSService();
  684. sock_ = createTestSocket();
  685. // Prepare the server
  686. bool done1(true);
  687. MockServerStop server(*io_service_, &done1);
  688. MessagePtr answer(new Message(Message::RENDER));
  689. // Do the answer
  690. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  691. RecursiveQuery query(*dns_service_,
  692. *nsas_, cache_,
  693. singleAddress(TEST_IPV4_ADDR, port),
  694. singleAddress(TEST_IPV4_ADDR, port),
  695. 1000, 10, 4000, 4);
  696. Question q(Name("example.net"), RRClass::IN(), RRType::A());
  697. OutputBufferPtr buffer(new OutputBuffer(0));
  698. Message query_message(Message::RENDER);
  699. isc::resolve::initResponseMessage(q, query_message);
  700. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  701. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  702. // Run the test
  703. io_service_->run();
  704. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  705. }
  706. // If we set lookup timeout to lower than querytimeout, the lookup
  707. // will fail.
  708. TEST_F(RecursiveQueryTest, forwardLookupTimeout) {
  709. // Prepare the service (we do not use the common setup, we do not answer
  710. setDNSService();
  711. // Prepare the socket
  712. sock_ = createTestSocket();
  713. // Prepare the server
  714. bool done(true);
  715. MockServerStop server(*io_service_, &done);
  716. MessagePtr answer(new Message(Message::RENDER));
  717. // Do the answer
  718. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  719. RecursiveQuery query(*dns_service_,
  720. *nsas_, cache_,
  721. singleAddress(TEST_IPV4_ADDR, port),
  722. singleAddress(TEST_IPV4_ADDR, port),
  723. 1000, 4000, 10, 5);
  724. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  725. OutputBufferPtr buffer(new OutputBuffer(0));
  726. Message query_message(Message::RENDER);
  727. isc::resolve::initResponseMessage(question, query_message);
  728. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  729. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  730. // Run the test
  731. io_service_->run();
  732. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  733. }
  734. // Set everything very low and see if this doesn't cause weird
  735. // behaviour
  736. TEST_F(RecursiveQueryTest, lowtimeouts) {
  737. // Prepare the service (we do not use the common setup, we do not answer
  738. setDNSService();
  739. // Prepare the socket
  740. sock_ = createTestSocket();
  741. // Prepare the server
  742. bool done(true);
  743. MockServerStop server(*io_service_, &done);
  744. MessagePtr answer(new Message(Message::RENDER));
  745. // Do the answer
  746. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  747. RecursiveQuery query(*dns_service_,
  748. *nsas_, cache_,
  749. singleAddress(TEST_IPV4_ADDR, port),
  750. singleAddress(TEST_IPV4_ADDR, port),
  751. 1, 1, 1, 1);
  752. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  753. OutputBufferPtr buffer(new OutputBuffer(0));
  754. Message query_message(Message::RENDER);
  755. isc::resolve::initResponseMessage(question, query_message);
  756. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  757. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  758. // Run the test
  759. io_service_->run();
  760. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  761. }
  762. // as mentioned above, we need a more better framework for this,
  763. // in addition to that, this sends out queries into the world
  764. // (which we should catch somehow and fake replies for)
  765. // for the skeleton code, it shouldn't be too much of a problem
  766. // Ok so even we don't all have access to the DNS world right now,
  767. // so disabling these tests too.
  768. TEST_F(RecursiveQueryTest, DISABLED_recursiveSendOk) {
  769. setDNSService(true, false);
  770. bool done;
  771. MockServerStop server(*io_service_, &done);
  772. vector<pair<string, uint16_t> > empty_vector;
  773. RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
  774. empty_vector, 10000, 0);
  775. Question q(Name("www.isc.org"), RRClass::IN(), RRType::A());
  776. OutputBufferPtr buffer(new OutputBuffer(0));
  777. MessagePtr answer(new Message(Message::RENDER));
  778. rq.resolve(q, answer, buffer, &server);
  779. io_service_->run();
  780. // Check that the answer we got matches the one we wanted
  781. EXPECT_EQ(Rcode::NOERROR(), answer->getRcode());
  782. ASSERT_EQ(1, answer->getRRCount(Message::SECTION_ANSWER));
  783. RRsetPtr a = *answer->beginSection(Message::SECTION_ANSWER);
  784. EXPECT_EQ(q.getName(), a->getName());
  785. EXPECT_EQ(q.getType(), a->getType());
  786. EXPECT_EQ(q.getClass(), a->getClass());
  787. EXPECT_EQ(1, a->getRdataCount());
  788. }
  789. // see comments at previous test
  790. TEST_F(RecursiveQueryTest, DISABLED_recursiveSendNXDOMAIN) {
  791. setDNSService(true, false);
  792. bool done;
  793. MockServerStop server(*io_service_, &done);
  794. vector<pair<string, uint16_t> > empty_vector;
  795. RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
  796. empty_vector, 10000, 0);
  797. Question q(Name("wwwdoesnotexist.isc.org"), RRClass::IN(), RRType::A());
  798. OutputBufferPtr buffer(new OutputBuffer(0));
  799. MessagePtr answer(new Message(Message::RENDER));
  800. rq.resolve(q, answer, buffer, &server);
  801. io_service_->run();
  802. // Check that the answer we got matches the one we wanted
  803. EXPECT_EQ(Rcode::NXDOMAIN(), answer->getRcode());
  804. EXPECT_EQ(0, answer->getRRCount(Message::SECTION_ANSWER));
  805. }
  806. // Test that we don't start at root when we have a lower NS cached.
  807. TEST_F(RecursiveQueryTest, CachedNS) {
  808. setDNSService(true, true);
  809. // Check we have a reasonable fallback - if there's nothing of interest
  810. // in the cache, start at root.
  811. EXPECT_EQ(".", deepestDelegation(Name("www.somewhere.deep.example.org"),
  812. RRClass::IN(), cache_));
  813. // Prefill the cache. There's a zone with a NS and IP address for one
  814. // of them (to see that one is enough) and another deeper one, with NS,
  815. // but without IP.
  816. RRsetPtr nsUpper(new RRset(Name("example.org"), RRClass::IN(),
  817. RRType::NS(), RRTTL(300)));
  818. nsUpper->addRdata(rdata::generic::NS(Name("ns.example.org")));
  819. nsUpper->addRdata(rdata::generic::NS(Name("ns2.example.org")));
  820. RRsetPtr nsLower(new RRset(Name("somewhere.deep.example.org"),
  821. RRClass::IN(), RRType::NS(), RRTTL(300)));
  822. nsLower->addRdata(rdata::generic::NS(Name("ns.somewhere.deep.example.org"))
  823. );
  824. RRsetPtr nsIp(new RRset(Name("ns2.example.org"), RRClass::IN(),
  825. RRType::A(), RRTTL(300)));
  826. nsIp->addRdata(rdata::in::A("192.0.2.1"));
  827. // Make sure the test runs in the correct environment (we don't test
  828. // the cache, but we need it to unswer this way for the test, so we
  829. // just make sure)
  830. ASSERT_TRUE(cache_.update(nsUpper));
  831. ASSERT_TRUE(cache_.update(nsLower));
  832. ASSERT_TRUE(cache_.update(nsIp));
  833. RRsetPtr deepest(cache_.lookupDeepestNS(Name(
  834. "www.somewhere.deep.example.org"), RRClass::IN()));
  835. ASSERT_NE(RRsetPtr(), deepest);
  836. ASSERT_EQ(nsLower->getName(), deepest->getName());
  837. // Direct check of the function that chooses the delegation point
  838. // It should not use nsLower, because we don't have IP address for
  839. // that one. But it can choose nsUpper.
  840. EXPECT_EQ("example.org.",
  841. deepestDelegation(Name("www.somewhere.deep.example.org"),
  842. RRClass::IN(), cache_));
  843. // Now more complex and indirect test:
  844. // We ask it to resolve the name for us. It will pick up a delegation
  845. // point and ask NSAS for it. NSAS will in turn ask resolver for NS record
  846. // of the delegation point. We then pick it up from the fake resolver
  847. // and check it is the correct one. This checks the delegation point
  848. // travels safely trough the whole path there (it would be enough to check
  849. // it up to NSAS, but replacing NSAS is more complicated, so we just
  850. // include in the test as well for simplicity).
  851. // Prepare the recursive query
  852. vector<pair<string, uint16_t> > roots;
  853. roots.push_back(pair<string, uint16_t>("192.0.2.2", 53));
  854. RecursiveQuery rq(*dns_service_, *nsas_, cache_,
  855. vector<pair<string, uint16_t> >(), roots);
  856. // Ask a question at the bottom. It should not use the lower NS, because
  857. // it would lead to a loop in NS. But it can use the nsUpper one, it has
  858. // an IP address and we can avoid asking root.
  859. Question q(Name("www.somewhere.deep.example.org"), RRClass::IN(),
  860. RRType::A());
  861. OutputBufferPtr buffer(new OutputBuffer(0));
  862. MessagePtr answer(new Message(Message::RENDER));
  863. // The server is here so we have something to pass there
  864. MockServer server(*io_service_);
  865. rq.resolve(q, answer, buffer, &server);
  866. // We don't need to run the service in this test. We are interested only
  867. // in the place it starts resolving at
  868. // Look what is asked by NSAS - it should be our delegation point.
  869. EXPECT_NO_THROW(EXPECT_EQ(nsUpper->getName(),
  870. (*resolver_)[0]->getName()) <<
  871. "It starts resolving at the wrong place") <<
  872. "It does not ask NSAS anything, how does it know where to send?";
  873. }
  874. // TODO: add tests that check whether the cache is updated on succesfull
  875. // responses, and not updated on failures.
  876. }