test_server_unix_socket.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef TEST_SERVER_UNIX_SOCKET_H
  7. #define TEST_SERVER_UNIX_SOCKET_H
  8. #include <config.h>
  9. #include <asiolink/interval_timer.h>
  10. #include <asiolink/io_service.h>
  11. #include <boost/shared_ptr.hpp>
  12. #include <gtest/gtest.h>
  13. #include <list>
  14. #include <string>
  15. namespace isc {
  16. namespace asiolink {
  17. namespace test {
  18. class ConnectionPool;
  19. /// @brief Provides unix domain socket functionality for unit tests.
  20. ///
  21. /// This class represents a server side socket. It can be used to
  22. /// test client's transmission over the unix domain socket. By default,
  23. /// the server side socket echoes the client's message so the client's
  24. /// message (prefixed with the word "received").
  25. ///
  26. /// It is also possible to specify a custom response from the server
  27. /// instead of eachoing back the request.
  28. ///
  29. /// It is possible to make multiple connections to the server side
  30. /// socket simultaneously.
  31. ///
  32. /// The test should perform IOService::run_one until it finds that
  33. /// the number of responses sent by the server is greater than
  34. /// expected. The number of responses sent so far can be retrieved
  35. /// using @ref TestServerUnixSocket::getResponseNum.
  36. class TestServerUnixSocket {
  37. public:
  38. /// @brief Constructor.
  39. ///
  40. /// @param io_service IO service.
  41. /// @param socket_file_path Socket file path.
  42. /// @param test_timeout Test timeout in milliseconds.
  43. /// @param custom_response Custom response to be sent to the client.
  44. TestServerUnixSocket(IOService& io_service,
  45. const std::string& socket_file_path,
  46. const long test_timeout,
  47. const std::string& custom_response = "");
  48. /// @brief Destructor.
  49. ///
  50. /// Closes active connections.
  51. ~TestServerUnixSocket();
  52. /// @brief Creates and binds server socket.
  53. void bindServerSocket();
  54. /// @brief Server acceptor handler.
  55. ///
  56. /// @param ec Error code.
  57. void acceptHandler(const boost::system::error_code& ec);
  58. /// @brief Callback function invoke upon test timeout.
  59. ///
  60. /// It stops the IO service and reports test timeout.
  61. void timeoutHandler();
  62. /// @brief Return number of responses sent so far to the clients.
  63. size_t getResponseNum() const;
  64. private:
  65. /// @brief Asynchronously accept new connections.
  66. void accept();
  67. /// @brief IO service used by the tests.
  68. IOService& io_service_;
  69. /// @brief Server endpoint.
  70. boost::asio::local::stream_protocol::endpoint server_endpoint_;
  71. /// @brief Server acceptor.
  72. boost::asio::local::stream_protocol::acceptor server_acceptor_;
  73. /// @brief Asynchronous timer service to detect timeouts.
  74. IntervalTimer test_timer_;
  75. /// @brief Holds custom response to be sent to the client.
  76. std::string custom_response_;
  77. /// @brief Pool of connections.
  78. boost::shared_ptr<ConnectionPool> connection_pool_;
  79. };
  80. /// @brief Pointer to the @ref TestServerUnixSocket.
  81. typedef boost::shared_ptr<TestServerUnixSocket> TestServerUnixSocketPtr;
  82. } // end of namespace isc::asiolink::test
  83. } // end of namespace isc::asiolink
  84. } // end of namespace isc
  85. #endif // TEST_SERVER_UNIX_SOCKET_H