mockups.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2010 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 ISC_TESTUTILS_MOCKUPS_H
  15. #define ISC_TESTUTILS_MOCKUPS_H 1
  16. #include <config.h>
  17. #include <exceptions/exceptions.h>
  18. #include <cc/data.h>
  19. #include <cc/session.h>
  20. #include <xfr/xfrout_client.h>
  21. #include <asiodns/asiodns.h>
  22. #include <utility>
  23. #include <vector>
  24. namespace isc {
  25. namespace testutils {
  26. // A minimal mock configuration session. Most the methods are
  27. // stubbed out, except for a very basic group_sendmsg() and
  28. // group_recvmsg(). hasQueuedMessages() always returns false.
  29. class MockSession : public isc::cc::AbstractSession {
  30. public:
  31. MockSession() :
  32. // by default we return a simple "success" message.
  33. msg_(isc::data::Element::fromJSON("{\"result\": [0, \"SUCCESS\"]}")),
  34. send_ok_(true), receive_ok_(true)
  35. {}
  36. virtual void establish(const char*) {}
  37. virtual void disconnect() {}
  38. virtual int group_sendmsg(isc::data::ConstElementPtr msg, std::string group,
  39. std::string, std::string)
  40. {
  41. if (!send_ok_) {
  42. isc_throw(isc::cc::SessionError,
  43. "mock session send is disabled for test");
  44. }
  45. sent_msg_ = msg;
  46. msg_dest_ = group;
  47. return (0);
  48. }
  49. virtual bool group_recvmsg(isc::data::ConstElementPtr&,
  50. isc::data::ConstElementPtr& msg, bool, int)
  51. {
  52. if (!receive_ok_) {
  53. isc_throw(isc::cc::SessionError,
  54. "mock session receive is disabled for test");
  55. }
  56. msg = msg_;
  57. return (true);
  58. }
  59. virtual void subscribe(std::string, std::string) {}
  60. virtual void unsubscribe(std::string, std::string) {}
  61. virtual void startRead(boost::function<void()>) {}
  62. virtual int reply(isc::data::ConstElementPtr, isc::data::ConstElementPtr) {
  63. return (-1);
  64. }
  65. virtual bool hasQueuedMsgs() const {
  66. return (false);
  67. }
  68. virtual void setTimeout(size_t) {};
  69. virtual size_t getTimeout() const { return 0; };
  70. // The following methods extent AbstractSession to allow testing:
  71. void setMessage(isc::data::ConstElementPtr msg) { msg_ = msg; }
  72. void disableSend() { send_ok_ = false; }
  73. void disableReceive() { receive_ok_ = false; }
  74. isc::data::ConstElementPtr getSentMessage() { return (sent_msg_); }
  75. std::string getMessageDest() { return (msg_dest_); }
  76. private:
  77. isc::data::ConstElementPtr sent_msg_;
  78. std::string msg_dest_;
  79. isc::data::ConstElementPtr msg_;
  80. bool send_ok_;
  81. bool receive_ok_;
  82. };
  83. // This mock object does nothing except for recording passed parameters
  84. // to addServerXXX methods so the test code subsequently checks the parameters.
  85. class MockDNSService : public isc::asiodns::DNSServiceBase {
  86. public:
  87. // A helper tuple of parameters passed to addServerUDPFromFD().
  88. struct UDPFdParams {
  89. int fd;
  90. int af;
  91. ServerFlag options;
  92. };
  93. virtual void addServerTCPFromFD(int fd, int af) {
  94. tcp_fd_params_.push_back(std::pair<int, int>(fd, af));
  95. }
  96. virtual void addServerUDPFromFD(int fd, int af, ServerFlag options) {
  97. UDPFdParams params = { fd, af, options };
  98. udp_fd_params_.push_back(params);
  99. }
  100. virtual void clearServers() {}
  101. virtual asiolink::IOService& getIOService() {
  102. isc_throw(isc::Unexpected,
  103. "MockDNSService::getIOService() shouldn't be called");
  104. }
  105. // These two allow the tests to check how the servers have been created
  106. // through this object.
  107. const std::vector<std::pair<int, int> >& getTCPFdParams() const {
  108. return (tcp_fd_params_);
  109. }
  110. const std::vector<UDPFdParams>& getUDPFdParams() const {
  111. return (udp_fd_params_);
  112. }
  113. virtual void setTCPRecvTimeout(size_t timeout) {
  114. tcp_recv_timeout_ = timeout;
  115. }
  116. size_t getTCPRecvTimeout() {
  117. return tcp_recv_timeout_;
  118. }
  119. private:
  120. std::vector<std::pair<int, int> > tcp_fd_params_;
  121. std::vector<UDPFdParams> udp_fd_params_;
  122. size_t tcp_recv_timeout_;
  123. };
  124. // A nonoperative DNSServer object to be used in calls to processMessage().
  125. class MockServer : public isc::asiodns::DNSServer {
  126. public:
  127. MockServer() : done_(false) {}
  128. void operator()(asio::error_code, size_t) {}
  129. virtual void resume(const bool done) { done_ = done; }
  130. virtual bool hasAnswer() { return (done_); }
  131. virtual int value() { return (0); }
  132. private:
  133. bool done_;
  134. };
  135. // Mock Xfrout client
  136. class MockXfroutClient : public isc::xfr::AbstractXfroutClient {
  137. public:
  138. MockXfroutClient() :
  139. is_connected_(false), connect_ok_(true), send_ok_(true),
  140. disconnect_ok_(true)
  141. {}
  142. virtual void connect() {
  143. if (!connect_ok_) {
  144. isc_throw(isc::xfr::XfroutError,
  145. "xfrout connection disabled for test");
  146. }
  147. is_connected_ = true;
  148. }
  149. virtual void disconnect() {
  150. if (!disconnect_ok_) {
  151. isc_throw(isc::xfr::XfroutError,
  152. "closing xfrout connection is disabled for test");
  153. }
  154. is_connected_ = false;
  155. }
  156. virtual int sendXfroutRequestInfo(int, const void*, uint16_t) {
  157. if (!send_ok_) {
  158. isc_throw(isc::xfr::XfroutError,
  159. "xfrout connection send is disabled for test");
  160. }
  161. return (0);
  162. }
  163. bool isConnected() const { return (is_connected_); }
  164. void disableConnect() { connect_ok_ = false; }
  165. void disableDisconnect() { disconnect_ok_ = false; }
  166. void enableDisconnect() { disconnect_ok_ = true; }
  167. void disableSend() { send_ok_ = false; }
  168. private:
  169. bool is_connected_;
  170. bool connect_ok_;
  171. bool send_ok_;
  172. bool disconnect_ok_;
  173. };
  174. } // end of testutils
  175. } // end of isc
  176. #endif // ISC_TESTUTILS_MOCKUPS_H
  177. // Local Variables:
  178. // mode: c++
  179. // End: