dns_server_unittest.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 <gtest/gtest.h>
  16. #include <asio.hpp>
  17. #include <asiolink/io_endpoint.h>
  18. #include <asiolink/io_error.h>
  19. #include <asiodns/udp_server.h>
  20. #include <asiodns/sync_udp_server.h>
  21. #include <asiodns/tcp_server.h>
  22. #include <asiodns/dns_answer.h>
  23. #include <asiodns/dns_lookup.h>
  24. #include <string>
  25. #include <cstring>
  26. #include <cerrno>
  27. #include <csignal>
  28. #include <unistd.h> //for alarm
  29. #include <boost/shared_ptr.hpp>
  30. #include <boost/bind.hpp>
  31. #include <boost/function.hpp>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. /// The following tests focus on stop interface for udp and
  35. /// tcp server, there are lots of things can be shared to test
  36. /// both tcp and udp server, so they are in the same unittest
  37. /// The general work flow for dns server, is that wait for user
  38. /// query, once get one query, we will check the data is valid or
  39. /// not, if it passed, we will try to loop up the question, then
  40. /// compose the answer and finally send it back to user. The server
  41. /// may be stopped at any point during this porcess, so the test strategy
  42. /// is that we define 5 stop point and stop the server at these
  43. /// 5 points, to check whether stop is successful
  44. /// The 5 test points are :
  45. /// Before the server start to run
  46. /// After we get the query and check whether it's valid
  47. /// After we lookup the query
  48. /// After we compoisite the answer
  49. /// After user get the final result.
  50. /// The standard about whether we stop the server successfully or not
  51. /// is based on the fact that if the server is still running, the io
  52. /// service won't quit since it will wait for some asynchronized event for
  53. /// server. So if the io service block function run returns we assume
  54. /// that the server is stopped. To avoid stop interface failure which
  55. /// will block followed tests, using alarm signal to stop the blocking
  56. /// io service
  57. ///
  58. /// The whole test context including one server and one client, and
  59. /// five stop checkpoints, we call them ServerStopper exclude the first
  60. /// stop point. Once the unittest fired, the client will send message
  61. /// to server, and the stopper may stop the server at the checkpoint, then
  62. /// we check the client get feedback or not. Since there is no DNS logic
  63. /// involved so the message sending between client and server is plain text
  64. /// And the valid checker, question lookup and answer composition are dummy.
  65. using namespace isc::asiolink;
  66. using namespace isc::asiodns;
  67. using namespace asio;
  68. namespace {
  69. const char* const server_ip = "::1";
  70. const int server_port = 5553;
  71. const char* const server_port_str = "5553";
  72. //message client send to udp server, which isn't dns package
  73. //just for simple testing
  74. const char* const query_message = "BIND10 is awesome";
  75. // \brief provide capacity to derived class the ability
  76. // to stop DNSServer at certern point
  77. class ServerStopper {
  78. public:
  79. ServerStopper() : server_to_stop_(NULL) {}
  80. virtual ~ServerStopper(){}
  81. void setServerToStop(DNSServer* server) {
  82. server_to_stop_ = server;
  83. }
  84. void stopServer() const {
  85. if (server_to_stop_) {
  86. server_to_stop_->stop();
  87. }
  88. }
  89. private:
  90. DNSServer* server_to_stop_;
  91. };
  92. // \brief no check logic at all,just provide a checkpoint to stop the server
  93. class DummyChecker : public SimpleCallback, public ServerStopper {
  94. public:
  95. virtual void operator()(const IOMessage&) const {
  96. stopServer();
  97. }
  98. };
  99. // \brief no lookup logic at all,just provide a checkpoint to stop the server
  100. class DummyLookup : public DNSLookup, public ServerStopper {
  101. public:
  102. DummyLookup() :
  103. allow_resume_(true)
  104. { }
  105. void operator()(const IOMessage& io_message,
  106. isc::dns::MessagePtr message,
  107. isc::dns::MessagePtr answer_message,
  108. isc::util::OutputBufferPtr buffer,
  109. DNSServer* server) const {
  110. stopServer();
  111. if (allow_resume_) {
  112. server->resume(true);
  113. }
  114. }
  115. // If you want it not to call resume, set this to false
  116. bool allow_resume_;
  117. };
  118. // \brief copy the data received from user to the answer part
  119. // provide checkpoint to stop server
  120. class SimpleAnswer : public DNSAnswer, public ServerStopper {
  121. public:
  122. void operator()(const IOMessage& message,
  123. isc::dns::MessagePtr query_message,
  124. isc::dns::MessagePtr answer_message,
  125. isc::util::OutputBufferPtr buffer) const
  126. {
  127. //copy what we get from user
  128. buffer->writeData(message.getData(), message.getDataSize());
  129. stopServer();
  130. }
  131. };
  132. // \brief simple client, send one string to server and wait for response
  133. // in case, server stopped and client cann't get response, there is a timer wait
  134. // for specified seconds (the value is just a estimate since server process logic is quite
  135. // simple, and all the intercommunication is local) then cancel the waiting.
  136. class SimpleClient : public ServerStopper {
  137. public:
  138. static const size_t MAX_DATA_LEN = 256;
  139. SimpleClient(asio::io_service& service,
  140. unsigned int wait_server_time_out)
  141. {
  142. wait_for_response_timer_.reset(new deadline_timer(service));
  143. received_data_ = new char[MAX_DATA_LEN];
  144. received_data_len_ = 0;
  145. wait_server_time_out_ = wait_server_time_out;
  146. }
  147. virtual ~SimpleClient() {
  148. delete [] received_data_;
  149. }
  150. void setGetFeedbackCallback(boost::function<void()>& func) {
  151. get_response_call_back_ = func;
  152. }
  153. virtual void sendDataThenWaitForFeedback(const std::string& data) = 0;
  154. virtual std::string getReceivedData() const = 0;
  155. void startTimer() {
  156. wait_for_response_timer_->cancel();
  157. wait_for_response_timer_->
  158. expires_from_now(boost::posix_time::
  159. seconds(wait_server_time_out_));
  160. wait_for_response_timer_->
  161. async_wait(boost::bind(&SimpleClient::stopWaitingforResponse,
  162. this));
  163. }
  164. void cancelTimer() { wait_for_response_timer_->cancel(); }
  165. void getResponseCallBack(const asio::error_code& error, size_t
  166. received_bytes)
  167. {
  168. cancelTimer();
  169. if (!error)
  170. received_data_len_ = received_bytes;
  171. if (!get_response_call_back_.empty()) {
  172. get_response_call_back_();
  173. }
  174. stopServer();
  175. }
  176. protected:
  177. virtual void stopWaitingforResponse() = 0;
  178. boost::shared_ptr<deadline_timer> wait_for_response_timer_;
  179. char* received_data_;
  180. size_t received_data_len_;
  181. boost::function<void()> get_response_call_back_;
  182. unsigned int wait_server_time_out_;
  183. };
  184. class UDPClient : public SimpleClient {
  185. public:
  186. //After 1 second without feedback client will stop wait
  187. static const unsigned int SERVER_TIME_OUT = 1;
  188. UDPClient(asio::io_service& service, const ip::udp::endpoint& server) :
  189. SimpleClient(service, SERVER_TIME_OUT)
  190. {
  191. server_ = server;
  192. socket_.reset(new ip::udp::socket(service));
  193. socket_->open(ip::udp::v6());
  194. }
  195. void sendDataThenWaitForFeedback(const std::string& data) {
  196. received_data_len_ = 0;
  197. socket_->send_to(buffer(data.c_str(), data.size() + 1), server_);
  198. socket_->async_receive_from(buffer(received_data_, MAX_DATA_LEN),
  199. received_from_,
  200. boost::bind(&SimpleClient::
  201. getResponseCallBack, this, _1,
  202. _2));
  203. startTimer();
  204. }
  205. virtual std::string getReceivedData() const {
  206. return (received_data_len_ == 0 ? std::string("") :
  207. std::string(received_data_));
  208. }
  209. private:
  210. void stopWaitingforResponse() {
  211. socket_->close();
  212. }
  213. boost::shared_ptr<ip::udp::socket> socket_;
  214. ip::udp::endpoint server_;
  215. ip::udp::endpoint received_from_;
  216. };
  217. class TCPClient : public SimpleClient {
  218. public:
  219. // after 2 seconds without feedback client will stop wait,
  220. // this includes connect, send message and recevice message
  221. static const unsigned int SERVER_TIME_OUT = 2;
  222. TCPClient(asio::io_service& service, const ip::tcp::endpoint& server)
  223. : SimpleClient(service, SERVER_TIME_OUT)
  224. {
  225. server_ = server;
  226. socket_.reset(new ip::tcp::socket(service));
  227. socket_->open(ip::tcp::v6());
  228. }
  229. virtual void sendDataThenWaitForFeedback(const std::string &data) {
  230. received_data_len_ = 0;
  231. data_to_send_ = data;
  232. data_to_send_len_ = data.size() + 1;
  233. socket_->async_connect(server_, boost::bind(&TCPClient::connectHandler,
  234. this, _1));
  235. startTimer();
  236. }
  237. virtual std::string getReceivedData() const {
  238. return (received_data_len_ == 0 ? std::string("") :
  239. std::string(received_data_ + 2));
  240. }
  241. private:
  242. void stopWaitingforResponse() {
  243. socket_->close();
  244. }
  245. void connectHandler(const asio::error_code& error) {
  246. if (!error) {
  247. data_to_send_len_ = htons(data_to_send_len_);
  248. socket_->async_send(buffer(&data_to_send_len_, 2),
  249. boost::bind(&TCPClient::sendMessageBodyHandler,
  250. this, _1, _2));
  251. }
  252. }
  253. void sendMessageBodyHandler(const asio::error_code& error,
  254. size_t send_bytes)
  255. {
  256. if (!error && send_bytes == 2) {
  257. socket_->async_send(buffer(data_to_send_.c_str(),
  258. data_to_send_.size() + 1),
  259. boost::bind(&TCPClient::finishSendHandler, this, _1, _2));
  260. }
  261. }
  262. void finishSendHandler(const asio::error_code& error, size_t send_bytes) {
  263. if (!error && send_bytes == data_to_send_.size() + 1) {
  264. socket_->async_receive(buffer(received_data_, MAX_DATA_LEN),
  265. boost::bind(&SimpleClient::getResponseCallBack, this, _1,
  266. _2));
  267. }
  268. }
  269. boost::shared_ptr<ip::tcp::socket> socket_;
  270. ip::tcp::endpoint server_;
  271. std::string data_to_send_;
  272. uint16_t data_to_send_len_;
  273. };
  274. // \brief provide the context which including two clients and
  275. // two servers, UDP client will only communicate with UDP server, same for TCP
  276. // client
  277. //
  278. // This is only the active part of the test. We run the test case four times, once
  279. // for each type of initialization (once when giving it the address and port,
  280. // once when giving the file descriptor) multiplied by once for each type of UDP
  281. // server (UDPServer and SyncUDPServer), to ensure it works exactly the same.
  282. template<class UDPServerClass>
  283. class DNSServerTestBase : public::testing::Test {
  284. protected:
  285. DNSServerTestBase() :
  286. server_address_(ip::address::from_string(server_ip)),
  287. checker_(new DummyChecker()),
  288. lookup_(new DummyLookup()),
  289. answer_(new SimpleAnswer()),
  290. udp_client_(new UDPClient(service,
  291. ip::udp::endpoint(server_address_,
  292. server_port))),
  293. tcp_client_(new TCPClient(service,
  294. ip::tcp::endpoint(server_address_,
  295. server_port))),
  296. udp_server_(NULL),
  297. tcp_server_(NULL)
  298. {
  299. current_service = &service;
  300. }
  301. ~ DNSServerTestBase() {
  302. if (udp_server_ != NULL) {
  303. udp_server_->stop();
  304. }
  305. if (tcp_server_ != NULL) {
  306. tcp_server_->stop();
  307. }
  308. delete checker_;
  309. delete lookup_;
  310. delete answer_;
  311. delete udp_server_;
  312. delete udp_client_;
  313. delete tcp_server_;
  314. delete tcp_client_;
  315. // No delete here. The service is not allocated by new, but as our
  316. // member. This only references it, so just cleaning the pointer.
  317. current_service = NULL;
  318. }
  319. void testStopServerByStopper(DNSServer* server, SimpleClient* client,
  320. ServerStopper* stopper)
  321. {
  322. static const unsigned int IO_SERVICE_TIME_OUT = 5;
  323. io_service_is_time_out = false;
  324. stopper->setServerToStop(server);
  325. (*server)();
  326. client->sendDataThenWaitForFeedback(query_message);
  327. // Since thread hasn't been introduced into the tool box, using
  328. // signal to make sure run function will eventually return even
  329. // server stop failed
  330. void (*prev_handler)(int) =
  331. std::signal(SIGALRM, DNSServerTestBase::stopIOService);
  332. current_service = &service;
  333. alarm(IO_SERVICE_TIME_OUT);
  334. service.run();
  335. service.reset();
  336. //cancel scheduled alarm
  337. alarm(0);
  338. std::signal(SIGALRM, prev_handler);
  339. }
  340. static void stopIOService(int _no_use_parameter) {
  341. io_service_is_time_out = true;
  342. if (current_service != NULL) {
  343. current_service->stop();
  344. }
  345. }
  346. bool serverStopSucceed() const {
  347. return (!io_service_is_time_out);
  348. }
  349. asio::io_service service;
  350. const ip::address server_address_;
  351. DummyChecker* const checker_;
  352. DummyLookup* const lookup_;
  353. SimpleAnswer* const answer_;
  354. UDPClient* const udp_client_;
  355. TCPClient* const tcp_client_;
  356. UDPServerClass* udp_server_;
  357. TCPServer* tcp_server_;
  358. // To access them in signal handle function, the following
  359. // variables have to be static.
  360. static asio::io_service* current_service;
  361. static bool io_service_is_time_out;
  362. };
  363. // Initialization with name and port
  364. template<class UDPServerClass>
  365. class AddrPortInit : public DNSServerTestBase<UDPServerClass> {
  366. protected:
  367. AddrPortInit() {
  368. this->udp_server_ = new UDPServerClass(this->service,
  369. this->server_address_,
  370. server_port, this->checker_,
  371. this->lookup_, this->answer_);
  372. this->tcp_server_ = new TCPServer(this->service, this->server_address_,
  373. server_port, this->checker_,
  374. this->lookup_, this->answer_);
  375. }
  376. };
  377. // Initialization by the file descriptor
  378. template<class UDPServerClass>
  379. class FdInit : public DNSServerTestBase<UDPServerClass> {
  380. private:
  381. // Opens the file descriptor for us
  382. // It uses the low-level C api, as it seems to be the easiest way to get
  383. // a raw file descriptor. It also is what the socket creator does and this
  384. // API is aimed to it.
  385. int getFd(int type) {
  386. struct addrinfo hints;
  387. memset(&hints, 0, sizeof(hints));
  388. hints.ai_family = AF_UNSPEC;
  389. hints.ai_socktype = type;
  390. hints.ai_protocol = (type == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP;
  391. hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
  392. struct addrinfo* res;
  393. const int error = getaddrinfo(server_ip, server_port_str,
  394. &hints, &res);
  395. if (error != 0) {
  396. isc_throw(IOError, "getaddrinfo failed: " << gai_strerror(error));
  397. }
  398. int sock;
  399. const int on(1);
  400. // Go as far as you can and stop on failure
  401. // Create the socket
  402. // set the options
  403. // and bind it
  404. const bool failed((sock = socket(res->ai_family, res->ai_socktype,
  405. res->ai_protocol)) == -1 ||
  406. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
  407. sizeof(on)) == -1 ||
  408. bind(sock, res->ai_addr, res->ai_addrlen) == -1);
  409. // No matter if it succeeded or not, free the address info
  410. freeaddrinfo(res);
  411. if (failed) {
  412. if (sock != -1) {
  413. close(sock);
  414. }
  415. return (-1);
  416. } else {
  417. return (sock);
  418. }
  419. }
  420. protected:
  421. // Using SetUp here so we can ASSERT_*
  422. void SetUp() {
  423. const int fdUDP(getFd(SOCK_DGRAM));
  424. ASSERT_NE(-1, fdUDP) << strerror(errno);
  425. this->udp_server_ = new UDPServerClass(this->service, fdUDP, AF_INET6,
  426. this->checker_, this->lookup_,
  427. this->answer_);
  428. const int fdTCP(getFd(SOCK_STREAM));
  429. ASSERT_NE(-1, fdTCP) << strerror(errno);
  430. this->tcp_server_ = new TCPServer(this->service, fdTCP, AF_INET6,
  431. this->checker_, this->lookup_,
  432. this->answer_);
  433. }
  434. };
  435. // This makes it the template as gtest wants it.
  436. template<class Parent>
  437. class DNSServerTest : public Parent { };
  438. typedef ::testing::Types<AddrPortInit<UDPServer>, AddrPortInit<SyncUDPServer>,
  439. FdInit<UDPServer>, FdInit<SyncUDPServer> >
  440. ServerTypes;
  441. TYPED_TEST_CASE(DNSServerTest, ServerTypes);
  442. typedef ::testing::Types<UDPServer, SyncUDPServer> UDPServerTypes;
  443. TYPED_TEST_CASE(DNSServerTestBase, UDPServerTypes);
  444. template<class UDPServerClass>
  445. bool DNSServerTestBase<UDPServerClass>::io_service_is_time_out = false;
  446. template<class UDPServerClass>
  447. asio::io_service* DNSServerTestBase<UDPServerClass>::current_service(NULL);
  448. typedef ::testing::Types<AddrPortInit<SyncUDPServer>, FdInit<SyncUDPServer> >
  449. SyncTypes;
  450. template<class Parent>
  451. class SyncServerTest : public Parent { };
  452. TYPED_TEST_CASE(SyncServerTest, SyncTypes);
  453. // Test whether server stopped successfully after client get response
  454. // client will send query and start to wait for response, once client
  455. // get response, udp server will be stopped, the io service won't quit
  456. // if udp server doesn't stop successfully.
  457. TYPED_TEST(DNSServerTest, stopUDPServerAfterOneQuery) {
  458. this->testStopServerByStopper(this->udp_server_, this->udp_client_,
  459. this->udp_client_);
  460. EXPECT_EQ(query_message, this->udp_client_->getReceivedData());
  461. EXPECT_TRUE(this->serverStopSucceed());
  462. }
  463. // Test whether udp server stopped successfully before server start to serve
  464. TYPED_TEST(DNSServerTest, stopUDPServerBeforeItStartServing) {
  465. this->udp_server_->stop();
  466. this->testStopServerByStopper(this->udp_server_, this->udp_client_,
  467. this->udp_client_);
  468. EXPECT_EQ(std::string(""), this->udp_client_->getReceivedData());
  469. EXPECT_TRUE(this->serverStopSucceed());
  470. }
  471. // Test whether udp server stopped successfully during message check
  472. TYPED_TEST(DNSServerTest, stopUDPServerDuringMessageCheck) {
  473. this->testStopServerByStopper(this->udp_server_, this->udp_client_,
  474. this->checker_);
  475. EXPECT_EQ(std::string(""), this->udp_client_->getReceivedData());
  476. EXPECT_TRUE(this->serverStopSucceed());
  477. }
  478. // Test whether udp server stopped successfully during query lookup
  479. TYPED_TEST(DNSServerTest, stopUDPServerDuringQueryLookup) {
  480. this->testStopServerByStopper(this->udp_server_, this->udp_client_,
  481. this->lookup_);
  482. EXPECT_EQ(std::string(""), this->udp_client_->getReceivedData());
  483. EXPECT_TRUE(this->serverStopSucceed());
  484. }
  485. // Test whether udp server stopped successfully during composing answer
  486. TYPED_TEST(DNSServerTest, stopUDPServerDuringPrepareAnswer) {
  487. this->testStopServerByStopper(this->udp_server_, this->udp_client_,
  488. this->answer_);
  489. EXPECT_EQ(std::string(""), this->udp_client_->getReceivedData());
  490. EXPECT_TRUE(this->serverStopSucceed());
  491. }
  492. static void stopServerManyTimes(DNSServer *server, unsigned int times) {
  493. for (unsigned int i = 0; i < times; ++i) {
  494. server->stop();
  495. }
  496. }
  497. // Test whether udp server stop interface can be invoked several times without
  498. // throw any exception
  499. TYPED_TEST(DNSServerTest, stopUDPServeMoreThanOnce) {
  500. ASSERT_NO_THROW({
  501. boost::function<void()> stop_server_3_times
  502. = boost::bind(stopServerManyTimes, this->udp_server_, 3);
  503. this->udp_client_->setGetFeedbackCallback(stop_server_3_times);
  504. this->testStopServerByStopper(this->udp_server_,
  505. this->udp_client_, this->udp_client_);
  506. EXPECT_EQ(query_message, this->udp_client_->getReceivedData());
  507. });
  508. EXPECT_TRUE(this->serverStopSucceed());
  509. }
  510. TYPED_TEST(DNSServerTest, stopTCPServerAfterOneQuery) {
  511. this->testStopServerByStopper(this->tcp_server_, this->tcp_client_,
  512. this->tcp_client_);
  513. EXPECT_EQ(query_message, this->tcp_client_->getReceivedData());
  514. EXPECT_TRUE(this->serverStopSucceed());
  515. }
  516. // Test whether tcp server stopped successfully before server start to serve
  517. TYPED_TEST(DNSServerTest, stopTCPServerBeforeItStartServing) {
  518. this->tcp_server_->stop();
  519. this->testStopServerByStopper(this->tcp_server_, this->tcp_client_,
  520. this->tcp_client_);
  521. EXPECT_EQ(std::string(""), this->tcp_client_->getReceivedData());
  522. EXPECT_TRUE(this->serverStopSucceed());
  523. }
  524. // Test whether tcp server stopped successfully during message check
  525. TYPED_TEST(DNSServerTest, stopTCPServerDuringMessageCheck) {
  526. this->testStopServerByStopper(this->tcp_server_, this->tcp_client_,
  527. this->checker_);
  528. EXPECT_EQ(std::string(""), this->tcp_client_->getReceivedData());
  529. EXPECT_TRUE(this->serverStopSucceed());
  530. }
  531. // Test whether tcp server stopped successfully during query lookup
  532. TYPED_TEST(DNSServerTest, stopTCPServerDuringQueryLookup) {
  533. this->testStopServerByStopper(this->tcp_server_, this->tcp_client_,
  534. this->lookup_);
  535. EXPECT_EQ(std::string(""), this->tcp_client_->getReceivedData());
  536. EXPECT_TRUE(this->serverStopSucceed());
  537. }
  538. // Test whether tcp server stopped successfully during composing answer
  539. TYPED_TEST(DNSServerTest, stopTCPServerDuringPrepareAnswer) {
  540. this->testStopServerByStopper(this->tcp_server_, this->tcp_client_,
  541. this->answer_);
  542. EXPECT_EQ(std::string(""), this->tcp_client_->getReceivedData());
  543. EXPECT_TRUE(this->serverStopSucceed());
  544. }
  545. // Test whether tcp server stop interface can be invoked several times without
  546. // throw any exception
  547. TYPED_TEST(DNSServerTest, stopTCPServeMoreThanOnce) {
  548. ASSERT_NO_THROW({
  549. boost::function<void()> stop_server_3_times
  550. = boost::bind(stopServerManyTimes, this->tcp_server_, 3);
  551. this->tcp_client_->setGetFeedbackCallback(stop_server_3_times);
  552. this->testStopServerByStopper(this->tcp_server_, this->tcp_client_,
  553. this->tcp_client_);
  554. EXPECT_EQ(query_message, this->tcp_client_->getReceivedData());
  555. });
  556. EXPECT_TRUE(this->serverStopSucceed());
  557. }
  558. // It raises an exception when invalid address family is passed
  559. // The parameter here doesn't mean anything
  560. TYPED_TEST(DNSServerTestBase, invalidFamily) {
  561. // We abuse DNSServerTestBase for this test, as we don't need the
  562. // initialization.
  563. EXPECT_THROW(TypeParam(this->service, 0, AF_UNIX, this->checker_,
  564. this->lookup_, this->answer_),
  565. isc::InvalidParameter);
  566. EXPECT_THROW(TCPServer(this->service, 0, AF_UNIX, this->checker_,
  567. this->lookup_, this->answer_),
  568. isc::InvalidParameter);
  569. }
  570. // It raises an exception when invalid address family is passed
  571. TYPED_TEST(DNSServerTestBase, invalidTCPFD) {
  572. // We abuse DNSServerTestBase for this test, as we don't need the
  573. // initialization.
  574. /*
  575. FIXME: The UDP server doesn't fail reliably with an invalid FD.
  576. We need to find a way to trigger it reliably (it seems epoll
  577. asio backend does fail as it tries to insert it right away, but
  578. not the others, maybe we could make it run this at last on epoll-based
  579. systems).
  580. EXPECT_THROW(UDPServer(service, -1, AF_INET, checker_, lookup_,
  581. answer_), isc::asiolink::IOError);
  582. */
  583. EXPECT_THROW(TCPServer(this->service, -1, AF_INET, this->checker_,
  584. this->lookup_, this->answer_),
  585. isc::asiolink::IOError);
  586. }
  587. TYPED_TEST(DNSServerTestBase, DISABLED_invalidUDPFD) {
  588. /*
  589. FIXME: The UDP server doesn't fail reliably with an invalid FD.
  590. We need to find a way to trigger it reliably (it seems epoll
  591. asio backend does fail as it tries to insert it right away, but
  592. not the others, maybe we could make it run this at least on epoll-based
  593. systems).
  594. */
  595. EXPECT_THROW(TypeParam(this->service, -1, AF_INET, this->checker_,
  596. this->lookup_, this->answer_),
  597. isc::asiolink::IOError);
  598. }
  599. // Check it rejects some of the unsupported operatirons
  600. TYPED_TEST(SyncServerTest, unsupportedOps) {
  601. EXPECT_THROW(this->udp_server_->clone(), isc::Unexpected);
  602. EXPECT_THROW(this->udp_server_->asyncLookup(), isc::Unexpected);
  603. }
  604. // Check it rejects forgotten resume (eg. insists that it is synchronous)
  605. TYPED_TEST(SyncServerTest, mustResume) {
  606. this->lookup_->allow_resume_ = false;
  607. ASSERT_THROW(this->testStopServerByStopper(this->udp_server_,
  608. this->udp_client_,
  609. this->lookup_),
  610. isc::Unexpected);
  611. }
  612. }