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