test_server_unix_socket.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/enable_shared_from_this.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <gtest/gtest.h>
  14. #include <list>
  15. #include <stdint.h>
  16. #include <string>
  17. namespace isc {
  18. namespace asiolink {
  19. namespace test {
  20. class ConnectionPool;
  21. /// @brief Provides unix domain socket functionality for unit tests.
  22. ///
  23. /// This class represents a server side socket. It can be used to
  24. /// test client's transmission over the unix domain socket. By default,
  25. /// the server side socket echoes the client's message so the client's
  26. /// message (prefixed with the word "received").
  27. ///
  28. /// It is also possible to specify a custom response from the server
  29. /// instead of eachoing back the request.
  30. ///
  31. /// It is possible to make multiple connections to the server side
  32. /// socket simultaneously.
  33. ///
  34. /// The test should perform IOService::run_one until it finds that
  35. /// the number of responses sent by the server is greater than
  36. /// expected. The number of responses sent so far can be retrieved
  37. /// using @ref TestServerUnixSocket::getResponseNum.
  38. ///
  39. /// This class uses @c shared_from_this() to pass its instance to the
  40. /// @c boost::bind function, thus the caller must store shared pointer
  41. /// to this object.
  42. class TestServerUnixSocket
  43. : public boost::enable_shared_from_this<TestServerUnixSocket> {
  44. public:
  45. /// @brief Constructor.
  46. ///
  47. /// @param io_service IO service.
  48. /// @param socket_file_path Socket file path.
  49. /// @param custom_response Custom response to be sent to the client.
  50. TestServerUnixSocket(IOService& io_service,
  51. const std::string& socket_file_path,
  52. const std::string& custom_response = "");
  53. /// @brief Destructor.
  54. ///
  55. /// Closes active connections.
  56. ~TestServerUnixSocket();
  57. /// @brief Starts timer for detecting test timeout.
  58. ///
  59. /// @param test_timeout Test timeout in milliseconds.
  60. void startTimer(const long test_timeout);
  61. /// @brief Starts timer for detecting test timeout.
  62. ///
  63. /// @param test_timeout Test timeout in milliseconds.
  64. void startTimer(const long test_timeout);
  65. /// @brief Generates response of a given length.
  66. ///
  67. /// @param response_size Desired response size.
  68. void generateCustomResponse(const uint64_t response_size);
  69. /// @brief Creates and binds server socket.
  70. void bindServerSocket();
  71. /// @brief Server acceptor handler.
  72. ///
  73. /// @param ec Error code.
  74. void acceptHandler(const boost::system::error_code& ec);
  75. /// @brief Callback function invoke upon test timeout.
  76. ///
  77. /// It stops the IO service and reports test timeout.
  78. void timeoutHandler();
  79. /// @brief Return number of responses sent so far to the clients.
  80. size_t getResponseNum() const;
  81. /// @brief Checks if IO service has been stopped as a result of the
  82. /// timeout.
  83. bool isStopped() const {
  84. return (stopped_);
  85. }
  86. private:
  87. /// @brief Asynchronously accept new connections.
  88. void accept();
  89. /// @brief IO service used by the tests.
  90. IOService& io_service_;
  91. /// @brief Server endpoint.
  92. boost::asio::local::stream_protocol::endpoint server_endpoint_;
  93. /// @brief Server acceptor.
  94. boost::asio::local::stream_protocol::acceptor server_acceptor_;
  95. /// @brief Asynchronous timer service to detect timeouts.
  96. IntervalTimer test_timer_;
  97. /// @brief Holds custom response to be sent to the client.
  98. std::string custom_response_;
  99. /// @brief Pool of connections.
  100. boost::shared_ptr<ConnectionPool> connection_pool_;
  101. /// @brief Indicates if IO service has been stopped as a result of
  102. /// a timeout.
  103. bool stopped_;
  104. };
  105. /// @brief Pointer to the @ref TestServerUnixSocket.
  106. typedef boost::shared_ptr<TestServerUnixSocket> TestServerUnixSocketPtr;
  107. } // end of namespace isc::asiolink::test
  108. } // end of namespace isc::asiolink
  109. } // end of namespace isc
  110. #endif // TEST_SERVER_UNIX_SOCKET_H