recursive_query_unittest.cc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. int
  605. setSocketTimeout(int sock, size_t tv_sec, size_t tv_usec) {
  606. const struct timeval timeo = { tv_sec, tv_usec };
  607. int recv_options = 0;
  608. if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo))) {
  609. if (errno == ENOPROTOOPT) { // see RecursiveQueryTest::recvUDP()
  610. recv_options = MSG_DONTWAIT;
  611. } else {
  612. isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
  613. }
  614. }
  615. return (recv_options);
  616. }
  617. // try to read from the socket max time
  618. // *num is incremented for every succesfull read
  619. // returns true if it can read max times, false otherwise
  620. bool tryRead(int sock, int recv_options, size_t max, int* num) {
  621. size_t i = 0;
  622. do {
  623. char inbuff[512];
  624. if (recv(sock, inbuff, sizeof(inbuff), recv_options) < 0) {
  625. return false;
  626. } else {
  627. ++i;
  628. ++*num;
  629. }
  630. } while (i < max);
  631. return true;
  632. }
  633. // Mock resolver callback for testing forward query.
  634. class MockResolverCallback : public isc::resolve::ResolverInterface::Callback {
  635. public:
  636. enum ResultValue {
  637. DEFAULT = 0,
  638. SUCCESS = 1,
  639. FAILURE = 2
  640. };
  641. MockResolverCallback(DNSServer* server):
  642. result(DEFAULT),
  643. server_(server->clone())
  644. {}
  645. ~MockResolverCallback() {
  646. delete server_;
  647. }
  648. void success(const isc::dns::MessagePtr response) {
  649. result = SUCCESS;
  650. server_->resume(true);
  651. }
  652. void failure() {
  653. result = FAILURE;
  654. server_->resume(false);
  655. }
  656. uint32_t result;
  657. private:
  658. DNSServer* server_;
  659. };
  660. // Test query timeout, set query timeout is lower than client timeout
  661. // and lookup timeout.
  662. TEST_F(RecursiveQueryTest, forwardQueryTimeout) {
  663. // Prepare the service (we do not use the common setup, we do not answer
  664. setDNSService();
  665. // Prepare the socket
  666. sock_.reset(createTestSocket());
  667. // Prepare the server
  668. bool done(true);
  669. MockServerStop server(io_service_, &done);
  670. // Do the answer
  671. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  672. RecursiveQuery query(*dns_service_,
  673. *nsas_, cache_,
  674. singleAddress(TEST_IPV4_ADDR, port),
  675. singleAddress(TEST_IPV4_ADDR, port),
  676. 10, 4000, 3000, 2);
  677. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  678. OutputBufferPtr buffer(new OutputBuffer(0));
  679. MessagePtr answer(new Message(Message::RENDER));
  680. Message query_message(Message::RENDER);
  681. isc::resolve::initResponseMessage(question, 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 client timeout to lower than querytimeout, we should
  689. // get a failure answer
  690. // (no actual answer is given here yet. TODO the returned error message
  691. // should be tested)
  692. TEST_F(RecursiveQueryTest, forwardClientTimeout) {
  693. // Prepare the service (we do not use the common setup, we do not answer
  694. setDNSService();
  695. sock_.reset(createTestSocket());
  696. // Prepare the server
  697. bool done1(true);
  698. MockServerStop server(io_service_, &done1);
  699. MessagePtr answer(new Message(Message::RENDER));
  700. // Do the answer
  701. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  702. RecursiveQuery query(*dns_service_,
  703. *nsas_, cache_,
  704. singleAddress(TEST_IPV4_ADDR, port),
  705. singleAddress(TEST_IPV4_ADDR, port),
  706. 1000, 10, 4000, 4);
  707. Question q(Name("example.net"), RRClass::IN(), RRType::A());
  708. OutputBufferPtr buffer(new OutputBuffer(0));
  709. Message query_message(Message::RENDER);
  710. isc::resolve::initResponseMessage(q, query_message);
  711. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  712. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  713. // Run the test
  714. io_service_.run();
  715. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  716. }
  717. // If we set lookup timeout to lower than querytimeout, the lookup
  718. // will fail.
  719. TEST_F(RecursiveQueryTest, forwardLookupTimeout) {
  720. // Prepare the service (we do not use the common setup, we do not answer
  721. setDNSService();
  722. // Prepare the socket
  723. sock_.reset(createTestSocket());
  724. // Prepare the server
  725. bool done(true);
  726. MockServerStop server(io_service_, &done);
  727. MessagePtr answer(new Message(Message::RENDER));
  728. // Do the answer
  729. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  730. RecursiveQuery query(*dns_service_,
  731. *nsas_, cache_,
  732. singleAddress(TEST_IPV4_ADDR, port),
  733. singleAddress(TEST_IPV4_ADDR, port),
  734. 1000, 4000, 10, 5);
  735. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  736. OutputBufferPtr buffer(new OutputBuffer(0));
  737. Message query_message(Message::RENDER);
  738. isc::resolve::initResponseMessage(question, query_message);
  739. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  740. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  741. // Run the test
  742. io_service_.run();
  743. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  744. }
  745. // Set everything very low and see if this doesn't cause weird
  746. // behaviour
  747. TEST_F(RecursiveQueryTest, lowtimeouts) {
  748. // Prepare the service (we do not use the common setup, we do not answer
  749. setDNSService();
  750. // Prepare the socket
  751. sock_.reset(createTestSocket());
  752. // Prepare the server
  753. bool done(true);
  754. MockServerStop server(io_service_, &done);
  755. MessagePtr answer(new Message(Message::RENDER));
  756. // Do the answer
  757. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  758. RecursiveQuery query(*dns_service_,
  759. *nsas_, cache_,
  760. singleAddress(TEST_IPV4_ADDR, port),
  761. singleAddress(TEST_IPV4_ADDR, port),
  762. 1, 1, 1, 1);
  763. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  764. OutputBufferPtr buffer(new OutputBuffer(0));
  765. Message query_message(Message::RENDER);
  766. isc::resolve::initResponseMessage(question, query_message);
  767. boost::shared_ptr<MockResolverCallback> callback(new MockResolverCallback(&server));
  768. query.forward(ConstMessagePtr(&query_message), answer, buffer, &server, callback);
  769. // Run the test
  770. io_service_.run();
  771. EXPECT_EQ(callback->result, MockResolverCallback::FAILURE);
  772. }
  773. // as mentioned above, we need a more better framework for this,
  774. // in addition to that, this sends out queries into the world
  775. // (which we should catch somehow and fake replies for)
  776. // for the skeleton code, it shouldn't be too much of a problem
  777. // Ok so even we don't all have access to the DNS world right now,
  778. // so disabling these tests too.
  779. TEST_F(RecursiveQueryTest, DISABLED_recursiveSendOk) {
  780. setDNSService(true, false);
  781. bool done;
  782. MockServerStop server(io_service_, &done);
  783. vector<pair<string, uint16_t> > empty_vector;
  784. RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
  785. empty_vector, 10000, 0);
  786. Question q(Name("www.isc.org"), RRClass::IN(), RRType::A());
  787. OutputBufferPtr buffer(new OutputBuffer(0));
  788. MessagePtr answer(new Message(Message::RENDER));
  789. rq.resolve(q, answer, buffer, &server);
  790. io_service_.run();
  791. // Check that the answer we got matches the one we wanted
  792. EXPECT_EQ(Rcode::NOERROR(), answer->getRcode());
  793. ASSERT_EQ(1, answer->getRRCount(Message::SECTION_ANSWER));
  794. RRsetPtr a = *answer->beginSection(Message::SECTION_ANSWER);
  795. EXPECT_EQ(q.getName(), a->getName());
  796. EXPECT_EQ(q.getType(), a->getType());
  797. EXPECT_EQ(q.getClass(), a->getClass());
  798. EXPECT_EQ(1, a->getRdataCount());
  799. }
  800. // see comments at previous test
  801. TEST_F(RecursiveQueryTest, DISABLED_recursiveSendNXDOMAIN) {
  802. setDNSService(true, false);
  803. bool done;
  804. MockServerStop server(io_service_, &done);
  805. vector<pair<string, uint16_t> > empty_vector;
  806. RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
  807. empty_vector, 10000, 0);
  808. Question q(Name("wwwdoesnotexist.isc.org"), RRClass::IN(), RRType::A());
  809. OutputBufferPtr buffer(new OutputBuffer(0));
  810. MessagePtr answer(new Message(Message::RENDER));
  811. rq.resolve(q, answer, buffer, &server);
  812. io_service_.run();
  813. // Check that the answer we got matches the one we wanted
  814. EXPECT_EQ(Rcode::NXDOMAIN(), answer->getRcode());
  815. EXPECT_EQ(0, answer->getRRCount(Message::SECTION_ANSWER));
  816. }
  817. // Test that we don't start at root when we have a lower NS cached.
  818. TEST_F(RecursiveQueryTest, CachedNS) {
  819. setDNSService(true, true);
  820. // Check we have a reasonable fallback - if there's nothing of interest
  821. // in the cache, start at root.
  822. EXPECT_EQ(".", deepestDelegation(Name("www.somewhere.deep.example.org"),
  823. RRClass::IN(), cache_));
  824. // Prefill the cache. There's a zone with a NS and IP address for one
  825. // of them (to see that one is enough) and another deeper one, with NS,
  826. // but without IP.
  827. RRsetPtr nsUpper(new RRset(Name("example.org"), RRClass::IN(),
  828. RRType::NS(), RRTTL(300)));
  829. nsUpper->addRdata(rdata::generic::NS(Name("ns.example.org")));
  830. nsUpper->addRdata(rdata::generic::NS(Name("ns2.example.org")));
  831. RRsetPtr nsLower(new RRset(Name("somewhere.deep.example.org"),
  832. RRClass::IN(), RRType::NS(), RRTTL(300)));
  833. nsLower->addRdata(rdata::generic::NS(Name("ns.somewhere.deep.example.org"))
  834. );
  835. RRsetPtr nsIp(new RRset(Name("ns2.example.org"), RRClass::IN(),
  836. RRType::A(), RRTTL(300)));
  837. nsIp->addRdata(rdata::in::A("192.0.2.1"));
  838. // Make sure the test runs in the correct environment (we don't test
  839. // the cache, but we need it to unswer this way for the test, so we
  840. // just make sure)
  841. ASSERT_TRUE(cache_.update(nsUpper));
  842. ASSERT_TRUE(cache_.update(nsLower));
  843. ASSERT_TRUE(cache_.update(nsIp));
  844. RRsetPtr deepest(cache_.lookupDeepestNS(Name(
  845. "www.somewhere.deep.example.org"), RRClass::IN()));
  846. ASSERT_NE(RRsetPtr(), deepest);
  847. ASSERT_EQ(nsLower->getName(), deepest->getName());
  848. // Direct check of the function that chooses the delegation point
  849. // It should not use nsLower, because we don't have IP address for
  850. // that one. But it can choose nsUpper.
  851. EXPECT_EQ("example.org.",
  852. deepestDelegation(Name("www.somewhere.deep.example.org"),
  853. RRClass::IN(), cache_));
  854. // Now more complex and indirect test:
  855. // We ask it to resolve the name for us. It will pick up a delegation
  856. // point and ask NSAS for it. NSAS will in turn ask resolver for NS record
  857. // of the delegation point. We then pick it up from the fake resolver
  858. // and check it is the correct one. This checks the delegation point
  859. // travels safely trough the whole path there (it would be enough to check
  860. // it up to NSAS, but replacing NSAS is more complicated, so we just
  861. // include in the test as well for simplicity).
  862. // Prepare the recursive query
  863. vector<pair<string, uint16_t> > roots;
  864. roots.push_back(pair<string, uint16_t>("192.0.2.2", 53));
  865. RecursiveQuery rq(*dns_service_, *nsas_, cache_,
  866. vector<pair<string, uint16_t> >(), roots);
  867. // Ask a question at the bottom. It should not use the lower NS, because
  868. // it would lead to a loop in NS. But it can use the nsUpper one, it has
  869. // an IP address and we can avoid asking root.
  870. Question q(Name("www.somewhere.deep.example.org"), RRClass::IN(),
  871. RRType::A());
  872. OutputBufferPtr buffer(new OutputBuffer(0));
  873. MessagePtr answer(new Message(Message::RENDER));
  874. // The server is here so we have something to pass there
  875. MockServer server(io_service_);
  876. rq.resolve(q, answer, buffer, &server);
  877. // We don't need to run the service in this test. We are interested only
  878. // in the place it starts resolving at
  879. // Look what is asked by NSAS - it should be our delegation point.
  880. EXPECT_NO_THROW(EXPECT_EQ(nsUpper->getName(),
  881. (*resolver_)[0]->getName()) <<
  882. "It starts resolving at the wrong place") <<
  883. "It does not ask NSAS anything, how does it know where to send?";
  884. }
  885. // TODO: add tests that check whether the cache is updated on succesfull
  886. // responses, and not updated on failures.
  887. }