recursive_query_unittest.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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/socket.h>
  16. #include <sys/time.h>
  17. #include <string.h>
  18. #include <boost/lexical_cast.hpp>
  19. #include <boost/bind.hpp>
  20. #include <boost/date_time/posix_time/posix_time_types.hpp>
  21. #include <gtest/gtest.h>
  22. #include <exceptions/exceptions.h>
  23. #include <dns/tests/unittest_util.h>
  24. #include <dns/rcode.h>
  25. #include <dns/buffer.h>
  26. #include <dns/message.h>
  27. #include <nsas/nameserver_address_store.h>
  28. #include <cache/resolver_cache.h>
  29. // IMPORTANT: We shouldn't directly use ASIO definitions in this test.
  30. // In particular, we must not include asio.hpp in this file.
  31. // The asiolink module is primarily intended to be a wrapper that hide the
  32. // details of the underlying implementations. We need to test the wrapper
  33. // level behaviors. In addition, some compilers reject to compile this file
  34. // if we include asio.hpp unless we specify a special compiler option.
  35. // If we need to test something at the level of underlying ASIO and need
  36. // their definition, that test should go to asiolink/internal/tests.
  37. #include <asiolink/recursive_query.h>
  38. #include <asiolink/io_socket.h>
  39. #include <asiolink/io_service.h>
  40. #include <asiolink/io_message.h>
  41. #include <asiolink/io_error.h>
  42. #include <asiolink/dns_lookup.h>
  43. #include <asiolink/simple_callback.h>
  44. using isc::UnitTestUtil;
  45. using namespace std;
  46. using namespace asiolink;
  47. using namespace isc::dns;
  48. namespace {
  49. const char* const TEST_SERVER_PORT = "53535";
  50. const char* const TEST_CLIENT_PORT = "53536";
  51. const char* const TEST_IPV6_ADDR = "::1";
  52. const char* const TEST_IPV4_ADDR = "127.0.0.1";
  53. // This data is intended to be valid as a DNS/TCP-like message: the first
  54. // two octets encode the length of the rest of the data. This is crucial
  55. // for the tests below.
  56. const uint8_t test_data[] = {0, 4, 1, 2, 3, 4};
  57. // This function returns an addrinfo structure for use by tests, using
  58. // different addresses and ports depending on whether we're testing
  59. // IPv4 or v6, TCP or UDP, and client or server operation.
  60. struct addrinfo*
  61. resolveAddress(const int family, const int protocol, const bool client) {
  62. const char* const addr = (family == AF_INET6) ?
  63. TEST_IPV6_ADDR : TEST_IPV4_ADDR;
  64. const char* const port = client ? TEST_CLIENT_PORT : TEST_SERVER_PORT;
  65. struct addrinfo hints;
  66. memset(&hints, 0, sizeof(hints));
  67. hints.ai_family = family;
  68. hints.ai_socktype = (protocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  69. hints.ai_protocol = protocol;
  70. hints.ai_flags = AI_NUMERICSERV;
  71. struct addrinfo* res;
  72. const int error = getaddrinfo(addr, port, &hints, &res);
  73. if (error != 0) {
  74. isc_throw(IOError, "getaddrinfo failed: " << gai_strerror(error));
  75. }
  76. return (res);
  77. }
  78. // This fixture is a framework for various types of network operations
  79. // using the ASIO interfaces. Each test case creates an IOService object,
  80. // opens a local "client" socket for testing, sends data via the local socket
  81. // to the service that would run in the IOService object.
  82. // A mock callback function (an ASIOCallBack object) is registered with the
  83. // IOService object, so the test code should be able to examine the data
  84. // received on the server side. It then checks the received data matches
  85. // expected parameters.
  86. // If initialization parameters of the IOService should be modified, the test
  87. // case can do it using the setDNSService() method.
  88. // Note: the set of tests in RecursiveQueryTest use actual network services and may
  89. // involve undesirable side effects such as blocking.
  90. class RecursiveQueryTest : public ::testing::Test {
  91. protected:
  92. RecursiveQueryTest();
  93. ~RecursiveQueryTest() {
  94. if (res_ != NULL) {
  95. freeaddrinfo(res_);
  96. }
  97. if (sock_ != -1) {
  98. close(sock_);
  99. }
  100. delete dns_service_;
  101. delete callback_;
  102. delete io_service_;
  103. }
  104. // Send a test UDP packet to a mock server
  105. void sendUDP(const int family) {
  106. res_ = resolveAddress(family, IPPROTO_UDP, false);
  107. sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
  108. if (sock_ < 0) {
  109. isc_throw(IOError, "failed to open test socket");
  110. }
  111. const int cc = sendto(sock_, test_data, sizeof(test_data), 0,
  112. res_->ai_addr, res_->ai_addrlen);
  113. if (cc != sizeof(test_data)) {
  114. isc_throw(IOError, "unexpected sendto result: " << cc);
  115. }
  116. io_service_->run();
  117. }
  118. // Send a test TCP packet to a mock server
  119. void sendTCP(const int family) {
  120. res_ = resolveAddress(family, IPPROTO_TCP, false);
  121. sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
  122. if (sock_ < 0) {
  123. isc_throw(IOError, "failed to open test socket");
  124. }
  125. if (connect(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
  126. isc_throw(IOError, "failed to connect to the test server");
  127. }
  128. const int cc = send(sock_, test_data, sizeof(test_data), 0);
  129. if (cc != sizeof(test_data)) {
  130. isc_throw(IOError, "unexpected send result: " << cc);
  131. }
  132. io_service_->run();
  133. }
  134. // Receive a UDP packet from a mock server; used for testing
  135. // recursive lookup. The caller must place a RecursiveQuery
  136. // on the IO Service queue before running this routine.
  137. void recvUDP(const int family, void* buffer, size_t& size) {
  138. res_ = resolveAddress(family, IPPROTO_UDP, true);
  139. sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
  140. if (sock_ < 0) {
  141. isc_throw(IOError, "failed to open test socket");
  142. }
  143. if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
  144. isc_throw(IOError, "bind failed: " << strerror(errno));
  145. }
  146. // The IO service queue should have a RecursiveQuery object scheduled
  147. // to run at this point. This call will cause it to begin an
  148. // async send, then return.
  149. io_service_->run_one();
  150. // ... and this one will block until the send has completed
  151. io_service_->run_one();
  152. // Now we attempt to recv() whatever was sent.
  153. // XXX: there's no guarantee the receiving socket can immediately get
  154. // the packet. Normally we can perform blocking recv to wait for it,
  155. // but in theory it's even possible that the packet is lost.
  156. // In order to prevent the test from hanging in such a worst case
  157. // we add an ad hoc timeout.
  158. const struct timeval timeo = { 10, 0 };
  159. int recv_options = 0;
  160. if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo,
  161. sizeof(timeo))) {
  162. if (errno == ENOPROTOOPT) {
  163. // Workaround for Solaris: it doesn't accept SO_RCVTIMEO
  164. // with the error of ENOPROTOOPT. Since this is a workaround
  165. // for rare error cases anyway, we simply switch to the
  166. // "don't wait" mode. If we still find an error in recv()
  167. // can happen often we'll consider a more complete solution.
  168. recv_options = MSG_DONTWAIT;
  169. } else {
  170. isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
  171. }
  172. }
  173. const int ret = recv(sock_, buffer, size, recv_options);
  174. if (ret < 0) {
  175. isc_throw(IOError, "recvfrom failed: " << strerror(errno));
  176. }
  177. // Pass the message size back via the size parameter
  178. size = ret;
  179. }
  180. // Set up an IO Service queue using the specified address
  181. void setDNSService(const char& address) {
  182. delete dns_service_;
  183. dns_service_ = NULL;
  184. delete io_service_;
  185. io_service_ = new IOService();
  186. callback_ = new ASIOCallBack(this);
  187. dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, address, callback_, NULL, NULL);
  188. }
  189. // Set up an IO Service queue using the "any" address, on IPv4 if
  190. // 'use_ipv4' is true and on IPv6 if 'use_ipv6' is true.
  191. void setDNSService(const bool use_ipv4, const bool use_ipv6) {
  192. delete dns_service_;
  193. dns_service_ = NULL;
  194. delete io_service_;
  195. io_service_ = new IOService();
  196. callback_ = new ASIOCallBack(this);
  197. dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, use_ipv4, use_ipv6, callback_,
  198. NULL, NULL);
  199. }
  200. // Set up empty DNS Service
  201. // Set up an IO Service queue without any addresses
  202. void setDNSService() {
  203. delete dns_service_;
  204. dns_service_ = NULL;
  205. delete io_service_;
  206. io_service_ = new IOService();
  207. callback_ = new ASIOCallBack(this);
  208. dns_service_ = new DNSService(*io_service_, callback_, NULL, NULL);
  209. }
  210. // Run a simple server test, on either IPv4 or IPv6, and over either
  211. // UDP or TCP. Calls the sendUDP() or sendTCP() methods, which will
  212. // start the IO Service queue. The UDPServer or TCPServer that was
  213. // created by setIOService() will receive the test packet and issue a
  214. // callback, which enables us to check that the data it received
  215. // matches what we sent.
  216. void doTest(const int family, const int protocol) {
  217. if (protocol == IPPROTO_UDP) {
  218. sendUDP(family);
  219. } else {
  220. sendTCP(family);
  221. }
  222. // There doesn't seem to be an effective test for the validity of
  223. // 'native'.
  224. // One thing we are sure is it must be different from our local socket.
  225. EXPECT_NE(sock_, callback_native_);
  226. EXPECT_EQ(protocol, callback_protocol_);
  227. EXPECT_EQ(family == AF_INET6 ? TEST_IPV6_ADDR : TEST_IPV4_ADDR,
  228. callback_address_);
  229. const uint8_t* expected_data =
  230. protocol == IPPROTO_UDP ? test_data : test_data + 2;
  231. const size_t expected_datasize =
  232. protocol == IPPROTO_UDP ? sizeof(test_data) :
  233. sizeof(test_data) - 2;
  234. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &callback_data_[0],
  235. callback_data_.size(),
  236. expected_data, expected_datasize);
  237. }
  238. protected:
  239. // This is a nonfunctional mockup of a DNSServer object. Its purpose
  240. // is to resume after a recursive query or other asynchronous call
  241. // has completed.
  242. class MockServer : public DNSServer {
  243. public:
  244. explicit MockServer(IOService& io_service,
  245. SimpleCallback* checkin = NULL,
  246. DNSLookup* lookup = NULL,
  247. DNSAnswer* answer = NULL) :
  248. io_(io_service),
  249. message_(new Message(Message::PARSE)),
  250. answer_message_(new Message(Message::RENDER)),
  251. respbuf_(new OutputBuffer(0)),
  252. checkin_(checkin), lookup_(lookup), answer_(answer)
  253. {}
  254. void operator()(asio::error_code ec = asio::error_code(),
  255. size_t length = 0)
  256. {}
  257. void resume(const bool) {
  258. // should never be called in our tests
  259. }
  260. DNSServer* clone() {
  261. MockServer* s = new MockServer(*this);
  262. return (s);
  263. }
  264. inline void asyncLookup() {
  265. if (lookup_) {
  266. (*lookup_)(*io_message_, message_, answer_message_,
  267. respbuf_, this);
  268. }
  269. }
  270. protected:
  271. IOService& io_;
  272. bool done_;
  273. private:
  274. // Currently unused; these will be used for testing
  275. // asynchronous lookup calls via the asyncLookup() method
  276. boost::shared_ptr<asiolink::IOMessage> io_message_;
  277. isc::dns::MessagePtr message_;
  278. isc::dns::MessagePtr answer_message_;
  279. isc::dns::OutputBufferPtr respbuf_;
  280. // Callback functions provided by the caller
  281. const SimpleCallback* checkin_;
  282. const DNSLookup* lookup_;
  283. const DNSAnswer* answer_;
  284. };
  285. // This version of mock server just stops the io_service when it is resumed
  286. class MockServerStop : public MockServer {
  287. public:
  288. explicit MockServerStop(IOService& io_service, bool* done) :
  289. MockServer(io_service),
  290. done_(done)
  291. {}
  292. void resume(const bool done) {
  293. *done_ = done;
  294. io_.stop();
  295. }
  296. DNSServer* clone() {
  297. return (new MockServerStop(*this));
  298. }
  299. private:
  300. bool* done_;
  301. };
  302. class MockResolver : public isc::resolve::ResolverInterface {
  303. void resolve(const QuestionPtr& question,
  304. const ResolverInterface::CallbackPtr& callback) {
  305. }
  306. };
  307. // This version of mock server just stops the io_service when it is resumed
  308. // the second time. (Used in the clientTimeout test, where resume
  309. // is called initially with the error answer, and later when the
  310. // lookup times out, it is called without an answer to send back)
  311. class MockServerStop2 : public MockServer {
  312. public:
  313. explicit MockServerStop2(IOService& io_service,
  314. bool* done1, bool* done2) :
  315. MockServer(io_service),
  316. done1_(done1),
  317. done2_(done2),
  318. stopped_once_(false)
  319. {}
  320. void resume(const bool done) {
  321. if (stopped_once_) {
  322. *done2_ = done;
  323. io_.stop();
  324. } else {
  325. *done1_ = done;
  326. stopped_once_ = true;
  327. }
  328. }
  329. DNSServer* clone() {
  330. return (new MockServerStop2(*this));
  331. }
  332. private:
  333. bool* done1_;
  334. bool* done2_;
  335. bool stopped_once_;
  336. };
  337. private:
  338. class ASIOCallBack : public SimpleCallback {
  339. public:
  340. ASIOCallBack(RecursiveQueryTest* test_obj) : test_obj_(test_obj) {}
  341. void operator()(const IOMessage& io_message) const {
  342. test_obj_->callBack(io_message);
  343. }
  344. private:
  345. RecursiveQueryTest* test_obj_;
  346. };
  347. void callBack(const IOMessage& io_message) {
  348. callback_protocol_ = io_message.getSocket().getProtocol();
  349. callback_native_ = io_message.getSocket().getNative();
  350. callback_address_ =
  351. io_message.getRemoteEndpoint().getAddress().toText();
  352. callback_data_.assign(
  353. static_cast<const uint8_t*>(io_message.getData()),
  354. static_cast<const uint8_t*>(io_message.getData()) +
  355. io_message.getDataSize());
  356. io_service_->stop();
  357. }
  358. protected:
  359. // We use a pointer for io_service_, because for some tests we
  360. // need to recreate a new one within one onstance of this class
  361. IOService* io_service_;
  362. DNSService* dns_service_;
  363. isc::nsas::NameserverAddressStore* nsas_;
  364. isc::cache::ResolverCache cache_;
  365. ASIOCallBack* callback_;
  366. int callback_protocol_;
  367. int callback_native_;
  368. string callback_address_;
  369. vector<uint8_t> callback_data_;
  370. int sock_;
  371. struct addrinfo* res_;
  372. };
  373. RecursiveQueryTest::RecursiveQueryTest() :
  374. dns_service_(NULL), callback_(NULL), sock_(-1), res_(NULL)
  375. {
  376. io_service_ = new IOService();
  377. setDNSService(true, true);
  378. boost::shared_ptr<MockResolver>mock_resolver(new MockResolver());
  379. nsas_ = new isc::nsas::NameserverAddressStore(mock_resolver);
  380. }
  381. TEST_F(RecursiveQueryTest, v6UDPSend) {
  382. doTest(AF_INET6, IPPROTO_UDP);
  383. }
  384. TEST_F(RecursiveQueryTest, v6TCPSend) {
  385. doTest(AF_INET6, IPPROTO_TCP);
  386. }
  387. TEST_F(RecursiveQueryTest, v4UDPSend) {
  388. doTest(AF_INET, IPPROTO_UDP);
  389. }
  390. TEST_F(RecursiveQueryTest, v4TCPSend) {
  391. doTest(AF_INET, IPPROTO_TCP);
  392. }
  393. TEST_F(RecursiveQueryTest, v6UDPSendSpecific) {
  394. // Explicitly set a specific address to be bound to the socket.
  395. // The subsequent test does not directly ensures the underlying socket
  396. // is bound to the expected address, but the success of the tests should
  397. // reasonably suggest it works as intended.
  398. // Specifying an address also implicitly means the service runs in a
  399. // single address-family mode. In tests using TCP we can confirm that
  400. // by trying to make a connection and seeing a failure. In UDP, it'd be
  401. // more complicated because we need to use a connected socket and catch
  402. // an error on a subsequent read operation. We could do it, but for
  403. // simplicity we only tests the easier cases for now.
  404. setDNSService(*TEST_IPV6_ADDR);
  405. doTest(AF_INET6, IPPROTO_UDP);
  406. }
  407. TEST_F(RecursiveQueryTest, v6TCPSendSpecific) {
  408. setDNSService(*TEST_IPV6_ADDR);
  409. doTest(AF_INET6, IPPROTO_TCP);
  410. EXPECT_THROW(sendTCP(AF_INET), IOError);
  411. }
  412. TEST_F(RecursiveQueryTest, v4UDPSendSpecific) {
  413. setDNSService(*TEST_IPV4_ADDR);
  414. doTest(AF_INET, IPPROTO_UDP);
  415. }
  416. TEST_F(RecursiveQueryTest, v4TCPSendSpecific) {
  417. setDNSService(*TEST_IPV4_ADDR);
  418. doTest(AF_INET, IPPROTO_TCP);
  419. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  420. }
  421. TEST_F(RecursiveQueryTest, v6AddServer) {
  422. setDNSService();
  423. dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV6_ADDR);
  424. doTest(AF_INET6, IPPROTO_TCP);
  425. EXPECT_THROW(sendTCP(AF_INET), IOError);
  426. }
  427. TEST_F(RecursiveQueryTest, v4AddServer) {
  428. setDNSService();
  429. dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV4_ADDR);
  430. doTest(AF_INET, IPPROTO_TCP);
  431. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  432. }
  433. TEST_F(RecursiveQueryTest, clearServers) {
  434. setDNSService();
  435. dns_service_->clearServers();
  436. EXPECT_THROW(sendTCP(AF_INET), IOError);
  437. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  438. }
  439. TEST_F(RecursiveQueryTest, v6TCPOnly) {
  440. // Open only IPv6 TCP socket. A subsequent attempt of establishing an
  441. // IPv4/TCP connection should fail. See above for why we only test this
  442. // for TCP.
  443. setDNSService(false, true);
  444. EXPECT_THROW(sendTCP(AF_INET), IOError);
  445. }
  446. TEST_F(RecursiveQueryTest, v4TCPOnly) {
  447. setDNSService(true, false);
  448. EXPECT_THROW(sendTCP(AF_INET6), IOError);
  449. }
  450. vector<pair<string, uint16_t> >
  451. singleAddress(const string &address, uint16_t port) {
  452. vector<pair<string, uint16_t> > result;
  453. result.push_back(pair<string, uint16_t>(address, port));
  454. return (result);
  455. }
  456. TEST_F(RecursiveQueryTest, recursiveSetupV4) {
  457. setDNSService(true, false);
  458. uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  459. EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
  460. *nsas_, cache_,
  461. singleAddress(TEST_IPV4_ADDR, port),
  462. singleAddress(TEST_IPV4_ADDR, port)));
  463. }
  464. TEST_F(RecursiveQueryTest, recursiveSetupV6) {
  465. setDNSService(false, true);
  466. uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  467. EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
  468. *nsas_, cache_,
  469. singleAddress(TEST_IPV6_ADDR, port),
  470. singleAddress(TEST_IPV6_ADDR,port)));
  471. }
  472. // XXX:
  473. // This is very inadequate unit testing. It should be generalized into
  474. // a routine that can do this with variable address family, address, and
  475. // port, and with the various callbacks defined in such a way as to ensure
  476. // full code coverage including error cases.
  477. TEST_F(RecursiveQueryTest, forwarderSend) {
  478. setDNSService(true, false);
  479. // Note: We use the test prot plus one to ensure we aren't binding
  480. // to the same port as the actual server
  481. uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  482. MockServer server(*io_service_);
  483. RecursiveQuery rq(*dns_service_,
  484. *nsas_, cache_,
  485. singleAddress(TEST_IPV4_ADDR, port),
  486. singleAddress(TEST_IPV4_ADDR, port));
  487. Question q(Name("example.com"), RRClass::IN(), RRType::TXT());
  488. OutputBufferPtr buffer(new OutputBuffer(0));
  489. MessagePtr answer(new Message(Message::RENDER));
  490. rq.resolve(q, answer, buffer, &server);
  491. char data[4096];
  492. size_t size = sizeof(data);
  493. ASSERT_NO_THROW(recvUDP(AF_INET, data, size));
  494. Message m(Message::PARSE);
  495. InputBuffer ibuf(data, size);
  496. // Make sure we can parse the message that was sent
  497. EXPECT_NO_THROW(m.parseHeader(ibuf));
  498. EXPECT_NO_THROW(m.fromWire(ibuf));
  499. // Check that the question sent matches the one we wanted
  500. QuestionPtr q2 = *m.beginQuestion();
  501. EXPECT_EQ(q.getName(), q2->getName());
  502. EXPECT_EQ(q.getType(), q2->getType());
  503. EXPECT_EQ(q.getClass(), q2->getClass());
  504. }
  505. int
  506. createTestSocket()
  507. {
  508. struct addrinfo* res_ = resolveAddress(AF_INET, IPPROTO_UDP, true);
  509. int sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
  510. if (sock_ < 0) {
  511. isc_throw(IOError, "failed to open test socket");
  512. }
  513. if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
  514. isc_throw(IOError, "failed to bind test socket");
  515. }
  516. return sock_;
  517. }
  518. int
  519. setSocketTimeout(int sock_, size_t tv_sec, size_t tv_usec) {
  520. const struct timeval timeo = { tv_sec, tv_usec };
  521. int recv_options = 0;
  522. if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo))) {
  523. if (errno == ENOPROTOOPT) { // see RecursiveQueryTest::recvUDP()
  524. recv_options = MSG_DONTWAIT;
  525. } else {
  526. isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
  527. }
  528. }
  529. return recv_options;
  530. }
  531. // try to read from the socket max time
  532. // *num is incremented for every succesfull read
  533. // returns true if it can read max times, false otherwise
  534. bool tryRead(int sock_, int recv_options, size_t max, int* num) {
  535. size_t i = 0;
  536. do {
  537. char inbuff[512];
  538. if (recv(sock_, inbuff, sizeof(inbuff), recv_options) < 0) {
  539. return false;
  540. } else {
  541. ++i;
  542. ++*num;
  543. }
  544. } while (i < max);
  545. return true;
  546. }
  547. // Test it tries the correct amount of times before giving up
  548. TEST_F(RecursiveQueryTest, forwardQueryTimeout) {
  549. // Prepare the service (we do not use the common setup, we do not answer
  550. setDNSService();
  551. // Prepare the socket
  552. sock_ = createTestSocket();
  553. // Prepare the server
  554. bool done(true);
  555. MockServerStop server(*io_service_, &done);
  556. // Do the answer
  557. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  558. RecursiveQuery query(*dns_service_,
  559. *nsas_, cache_,
  560. singleAddress(TEST_IPV4_ADDR, port),
  561. singleAddress(TEST_IPV4_ADDR, port),
  562. 10, 4000, 3000, 2);
  563. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  564. OutputBufferPtr buffer(new OutputBuffer(0));
  565. MessagePtr answer(new Message(Message::RENDER));
  566. query.resolve(question, answer, buffer, &server);
  567. // Run the test
  568. io_service_->run();
  569. // Read up to 3 packets. Use some ad hoc timeout to prevent an infinite
  570. // block (see also recvUDP()).
  571. int recv_options = setSocketTimeout(sock_, 10, 0);
  572. int num = 0;
  573. bool read_success = tryRead(sock_, recv_options, 3, &num);
  574. // The query should fail
  575. EXPECT_FALSE(done);
  576. EXPECT_EQ(3, num);
  577. EXPECT_TRUE(read_success);
  578. }
  579. // If we set client timeout to lower than querytimeout, we should
  580. // get a failure answer, but still see retries
  581. // (no actual answer is given here yet)
  582. TEST_F(RecursiveQueryTest, forwardClientTimeout) {
  583. // Prepare the service (we do not use the common setup, we do not answer
  584. setDNSService();
  585. sock_ = createTestSocket();
  586. // Prepare the server
  587. bool done1(true);
  588. bool done2(true);
  589. MockServerStop2 server(*io_service_, &done1, &done2);
  590. MessagePtr answer(new Message(Message::RENDER));
  591. // Do the answer
  592. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  593. // Set it up to retry twice before client timeout fires
  594. // Since the lookup timer has not fired, it should retry
  595. // four times
  596. RecursiveQuery query(*dns_service_,
  597. *nsas_, cache_,
  598. singleAddress(TEST_IPV4_ADDR, port),
  599. singleAddress(TEST_IPV4_ADDR, port),
  600. 200, 480, 4000, 4);
  601. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  602. OutputBufferPtr buffer(new OutputBuffer(0));
  603. query.resolve(question, answer, buffer, &server);
  604. // Run the test
  605. io_service_->run();
  606. // we know it'll fail, so make it a shorter timeout
  607. int recv_options = setSocketTimeout(sock_, 1, 0);
  608. // Try to read 5 times
  609. int num = 0;
  610. bool read_success = tryRead(sock_, recv_options, 5, &num);
  611. // The query should fail, but we should have kept on trying
  612. EXPECT_TRUE(done1);
  613. EXPECT_FALSE(done2);
  614. EXPECT_EQ(5, num);
  615. EXPECT_TRUE(read_success);
  616. }
  617. // If we set lookup timeout to lower than querytimeout*retries, we should
  618. // fail before the full amount of retries
  619. TEST_F(RecursiveQueryTest, forwardLookupTimeout) {
  620. // Prepare the service (we do not use the common setup, we do not answer
  621. setDNSService();
  622. // Prepare the socket
  623. sock_ = createTestSocket();
  624. // Prepare the server
  625. bool done(true);
  626. MockServerStop server(*io_service_, &done);
  627. MessagePtr answer(new Message(Message::RENDER));
  628. // Do the answer
  629. const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
  630. // Set up the test so that it will retry 5 times, but the lookup
  631. // timeout will fire after only 3 normal timeouts
  632. RecursiveQuery query(*dns_service_,
  633. *nsas_, cache_,
  634. singleAddress(TEST_IPV4_ADDR, port),
  635. singleAddress(TEST_IPV4_ADDR, port),
  636. 200, 4000, 480, 5);
  637. Question question(Name("example.net"), RRClass::IN(), RRType::A());
  638. OutputBufferPtr buffer(new OutputBuffer(0));
  639. query.resolve(question, answer, buffer, &server);
  640. // Run the test
  641. io_service_->run();
  642. int recv_options = setSocketTimeout(sock_, 1, 0);
  643. // Try to read 5 times, should stop after 3 reads
  644. int num = 0;
  645. bool read_success = tryRead(sock_, recv_options, 5, &num);
  646. // The query should fail
  647. EXPECT_FALSE(done);
  648. EXPECT_EQ(3, num);
  649. EXPECT_FALSE(read_success);
  650. }
  651. // as mentioned above, we need a more better framework for this,
  652. // in addition to that, this sends out queries into the world
  653. // (which we should catch somehow and fake replies for)
  654. // for the skeleton code, it shouldn't be too much of a problem
  655. // Ok so even we don't all have access to the DNS world right now,
  656. // so disabling these tests too.
  657. TEST_F(RecursiveQueryTest, DISABLED_recursiveSendOk) {
  658. setDNSService(true, false);
  659. bool done;
  660. MockServerStop server(*io_service_, &done);
  661. vector<pair<string, uint16_t> > empty_vector;
  662. RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
  663. empty_vector, 10000, 0);
  664. Question q(Name("www.isc.org"), RRClass::IN(), RRType::A());
  665. OutputBufferPtr buffer(new OutputBuffer(0));
  666. MessagePtr answer(new Message(Message::RENDER));
  667. rq.resolve(q, answer, buffer, &server);
  668. io_service_->run();
  669. // Check that the answer we got matches the one we wanted
  670. EXPECT_EQ(Rcode::NOERROR(), answer->getRcode());
  671. ASSERT_EQ(1, answer->getRRCount(Message::SECTION_ANSWER));
  672. RRsetPtr a = *answer->beginSection(Message::SECTION_ANSWER);
  673. EXPECT_EQ(q.getName(), a->getName());
  674. EXPECT_EQ(q.getType(), a->getType());
  675. EXPECT_EQ(q.getClass(), a->getClass());
  676. EXPECT_EQ(1, a->getRdataCount());
  677. }
  678. // see comments at previous test
  679. TEST_F(RecursiveQueryTest, DISABLED_recursiveSendNXDOMAIN) {
  680. setDNSService(true, false);
  681. bool done;
  682. MockServerStop server(*io_service_, &done);
  683. vector<pair<string, uint16_t> > empty_vector;
  684. RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
  685. empty_vector, 10000, 0);
  686. Question q(Name("wwwdoesnotexist.isc.org"), RRClass::IN(), RRType::A());
  687. OutputBufferPtr buffer(new OutputBuffer(0));
  688. MessagePtr answer(new Message(Message::RENDER));
  689. rq.resolve(q, answer, buffer, &server);
  690. io_service_->run();
  691. // Check that the answer we got matches the one we wanted
  692. EXPECT_EQ(Rcode::NXDOMAIN(), answer->getRcode());
  693. EXPECT_EQ(0, answer->getRRCount(Message::SECTION_ANSWER));
  694. }
  695. }