mock_socketsession.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2012 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. #ifndef __UTIL_UNITTESTS_MOCKSOCKETSESSION_H
  15. #define __UTIL_UNITTESTS_MOCKSOCKETSESSION_H 1
  16. #include <exceptions/exceptions.h>
  17. #include <util/io/socketsession.h>
  18. #include <util/io/sockaddr_util.h>
  19. #include <cassert>
  20. #include <cstring>
  21. #include <vector>
  22. #include <sys/socket.h>
  23. namespace isc {
  24. namespace util {
  25. namespace unittests {
  26. // Mock socket session forwarder
  27. class MockSocketSessionForwarder :
  28. public isc::util::io::BaseSocketSessionForwarder
  29. {
  30. public:
  31. MockSocketSessionForwarder() :
  32. is_connected_(false), connect_ok_(true), push_ok_(true),
  33. close_ok_(true)
  34. {}
  35. virtual void connectToReceiver() {
  36. if (!connect_ok_) {
  37. isc_throw(isc::util::io::SocketSessionError, "socket session "
  38. "forwarding connection disabled for test");
  39. }
  40. if (is_connected_) {
  41. isc_throw(isc::util::io::SocketSessionError, "duplicate connect");
  42. }
  43. is_connected_ = true;
  44. }
  45. virtual void close() {
  46. is_connected_ = false;
  47. }
  48. virtual void push(int sock, int family, int type, int protocol,
  49. const struct sockaddr& local_end,
  50. const struct sockaddr& remote_end,
  51. const void* data, size_t data_len)
  52. {
  53. if (!push_ok_) {
  54. isc_throw(isc::util::io::SocketSessionError,
  55. "socket session forwarding is disabled for test");
  56. }
  57. // Copy parameters for later checks
  58. pushed_sock_ = sock;
  59. pushed_family_ = family;
  60. pushed_type_ = type;
  61. pushed_protocol_ = protocol;
  62. assert(remote_end.sa_family == AF_INET);
  63. assert(io::internal::getSALength(local_end) <=
  64. sizeof(pushed_local_end_ss_));
  65. std::memcpy(&pushed_local_end_ss_, &local_end,
  66. io::internal::getSALength(local_end));
  67. assert(io::internal::getSALength(remote_end) <=
  68. sizeof(pushed_remote_end_ss_));
  69. std::memcpy(&pushed_remote_end_ss_, &remote_end,
  70. io::internal::getSALength(remote_end));
  71. pushed_data_.resize(data_len);
  72. std::memcpy(&pushed_data_[0], data, data_len);
  73. }
  74. bool isConnected() const { return (is_connected_); }
  75. void disableConnect() { connect_ok_ = false; }
  76. void enableConnect() { connect_ok_ = true; }
  77. void disableClose() { close_ok_ = false; }
  78. void enableClose() { close_ok_ = true; }
  79. void disablePush() { push_ok_ = false; }
  80. // Read-only accessors to recorded parameters to the previous successful
  81. // call to push(). Return values are undefined if there has been no
  82. // successful call to push().
  83. int getPushedSock() const { return (pushed_sock_); }
  84. int getPushedFamily() const { return (pushed_family_); }
  85. int getPushedType() const { return (pushed_type_); }
  86. int getPushedProtocol() const { return (pushed_protocol_); }
  87. const struct sockaddr& getPushedLocalend() const {
  88. return (*io::internal::convertSockAddr(&pushed_local_end_ss_));
  89. }
  90. const struct sockaddr& getPushedRemoteend() const {
  91. return (*io::internal::convertSockAddr(&pushed_remote_end_ss_));
  92. }
  93. const std::vector<uint8_t> getPushedData() const { return (pushed_data_); }
  94. private:
  95. bool is_connected_;
  96. bool connect_ok_;
  97. bool push_ok_;
  98. bool close_ok_;
  99. int pushed_sock_;
  100. int pushed_family_;
  101. int pushed_type_;
  102. int pushed_protocol_;
  103. struct sockaddr_storage pushed_local_end_ss_;
  104. struct sockaddr_storage pushed_remote_end_ss_;
  105. std::vector<uint8_t> pushed_data_;
  106. };
  107. } // end of unittests
  108. } // end of util
  109. } // end of isc
  110. #endif // __UTIL_UNITTESTS_MOCKSOCKETSESSION_H
  111. // Local Variables:
  112. // mode: c++
  113. // End: