recursive_query_unittest.cc 36 KB

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