mockups.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include <config.h>
  15. #include <cc/data.h>
  16. #include <cc/session.h>
  17. #include <xfr/xfrout_client.h>
  18. #include <asiodns/asiodns.h>
  19. // A minimal mock configuration session. Most the methods are
  20. // stubbed out, except for a very basic group_sendmsg() and
  21. // group_recvmsg(). hasQueuedMessages() always returns false.
  22. class MockSession : public isc::cc::AbstractSession {
  23. public:
  24. MockSession() :
  25. // by default we return a simple "success" message.
  26. msg_(isc::data::Element::fromJSON("{\"result\": [0, \"SUCCESS\"]}")),
  27. send_ok_(true), receive_ok_(true)
  28. {}
  29. virtual void establish(const char*) {}
  30. virtual void disconnect() {}
  31. virtual int group_sendmsg(isc::data::ConstElementPtr msg, std::string group,
  32. std::string, std::string)
  33. {
  34. if (!send_ok_) {
  35. isc_throw(isc::cc::SessionError,
  36. "mock session send is disabled for test");
  37. }
  38. sent_msg_ = msg;
  39. msg_dest_ = group;
  40. return (0);
  41. }
  42. virtual bool group_recvmsg(isc::data::ConstElementPtr&,
  43. isc::data::ConstElementPtr& msg, bool, int)
  44. {
  45. if (!receive_ok_) {
  46. isc_throw(isc::cc::SessionError,
  47. "mock session receive is disabled for test");
  48. }
  49. msg = msg_;
  50. return (true);
  51. }
  52. virtual void subscribe(std::string, std::string) {}
  53. virtual void unsubscribe(std::string, std::string) {}
  54. virtual void startRead(boost::function<void()>) {}
  55. virtual int reply(isc::data::ConstElementPtr, isc::data::ConstElementPtr) {
  56. return (-1);
  57. }
  58. virtual bool hasQueuedMsgs() const {
  59. return (false);
  60. }
  61. virtual void setTimeout(size_t) {};
  62. virtual size_t getTimeout() const { return 0; };
  63. // The following methods extent AbstractSession to allow testing:
  64. void setMessage(isc::data::ConstElementPtr msg) { msg_ = msg; }
  65. void disableSend() { send_ok_ = false; }
  66. void disableReceive() { receive_ok_ = false; }
  67. isc::data::ConstElementPtr getSentMessage() { return (sent_msg_); }
  68. std::string getMessageDest() { return (msg_dest_); }
  69. private:
  70. isc::data::ConstElementPtr sent_msg_;
  71. std::string msg_dest_;
  72. isc::data::ConstElementPtr msg_;
  73. bool send_ok_;
  74. bool receive_ok_;
  75. };
  76. // A nonoperative DNSServer object to be used in calls to processMessage().
  77. class MockServer : public isc::asiodns::DNSServer {
  78. public:
  79. MockServer() : done_(false) {}
  80. void operator()(asio::error_code, size_t) {}
  81. virtual void resume(const bool done) { done_ = done; }
  82. virtual bool hasAnswer() { return (done_); }
  83. virtual int value() { return (0); }
  84. private:
  85. bool done_;
  86. };
  87. // Mock Xfrout client
  88. class MockXfroutClient : public isc::xfr::AbstractXfroutClient {
  89. public:
  90. MockXfroutClient() :
  91. is_connected_(false), connect_ok_(true), send_ok_(true),
  92. disconnect_ok_(true)
  93. {}
  94. virtual void connect() {
  95. if (!connect_ok_) {
  96. isc_throw(isc::xfr::XfroutError,
  97. "xfrout connection disabled for test");
  98. }
  99. is_connected_ = true;
  100. }
  101. virtual void disconnect() {
  102. if (!disconnect_ok_) {
  103. isc_throw(isc::xfr::XfroutError,
  104. "closing xfrout connection is disabled for test");
  105. }
  106. is_connected_ = false;
  107. }
  108. virtual int sendXfroutRequestInfo(int, const void*, uint16_t) {
  109. if (!send_ok_) {
  110. isc_throw(isc::xfr::XfroutError,
  111. "xfrout connection send is disabled for test");
  112. }
  113. return (0);
  114. }
  115. bool isConnected() const { return (is_connected_); }
  116. void disableConnect() { connect_ok_ = false; }
  117. void disableDisconnect() { disconnect_ok_ = false; }
  118. void enableDisconnect() { disconnect_ok_ = true; }
  119. void disableSend() { send_ok_ = false; }
  120. private:
  121. bool is_connected_;
  122. bool connect_ok_;
  123. bool send_ok_;
  124. bool disconnect_ok_;
  125. };