mock_socket.cpp
Go to the documentation of this file.
1 /*
2  * Unit tests for XmlRpc++
3  *
4  * Copyright (C) 2017, Zoox Inc
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Author: Austin Hendrix <austin@zoox.com>
21  *
22  */
23 
24 #define xmlrpcpp_EXPORTS // we are mocking XmlRpcSocket, define the symbol in order to export XmlRpcSocket class
25 # include "xmlrpcpp/XmlRpcSocket.h"
26 #undef xmlrpcpp_EXPORTS
27 
28 #include "xmlrpcpp/XmlRpcUtil.h"
29 #include "mock_socket.h"
30 
31 #include <deque>
32 
33 #include <string.h>
34 #include <errno.h>
35 
36 #include <gtest/gtest.h>
37 
38 using namespace XmlRpc;
39 
40 bool XmlRpcSocket::s_use_ipv6_ = false;
41 
42 // Returns message corresponding to last errno.
43 // NOTE(austin): this matches the default implementation.
44 std::string XmlRpcSocket::getErrorMsg() {
45  return getErrorMsg(getError());
46 }
47 
48 // Returns message corresponding to errno
49 // NOTE(austin): this matches the default implementation.
50 std::string XmlRpcSocket::getErrorMsg(int error) {
51  char err[60];
52  std::snprintf(err, sizeof(err), "%s", strerror(error));
53  return std::string(err);
54 }
55 
56 #define EXPECT_PROLOGUE(name) \
57  EXPECT_EQ(0, name##_calls) \
58  << "Test error; cannont expect " #name " more than once";
59 
60 std::deque<int> close_calls;
61 void XmlRpcSocket::close(int fd) {
62  EXPECT_LE(1u, close_calls.size());
63  if (close_calls.size() > 0) {
64  int close_fd = close_calls.front();
65  close_calls.pop_front();
66  EXPECT_EQ(close_fd, fd);
67  }
68 }
69 
71  close_calls.push_back(fd);
72 }
73 
74 int socket_ret = 0;
75 int socket_calls = 0;
77  EXPECT_EQ(1, socket_calls);
78  socket_calls--;
79  return socket_ret;
80 }
81 
83  EXPECT_PROLOGUE(socket);
84  socket_calls = 1;
85  socket_ret = ret;
86 }
87 
88 bool setNonBlocking_ret = true;
91 bool XmlRpcSocket::setNonBlocking(int fd) {
92  EXPECT_EQ(1, setNonBlocking_calls);
94  EXPECT_EQ(setNonBlocking_fd, fd);
95  return setNonBlocking_ret;
96 }
97 void MockSocketTest::Expect_setNonBlocking(int fd, bool ret) {
98  EXPECT_PROLOGUE(setNonBlocking);
100  setNonBlocking_fd = fd;
101  setNonBlocking_ret = ret;
102 }
103 
104 bool setReuseAddr_ret = true;
107 bool XmlRpcSocket::setReuseAddr(int fd) {
108  EXPECT_EQ(1, setReuseAddr_calls);
110  EXPECT_EQ(setReuseAddr_fd, fd);
111  return setReuseAddr_ret;
112 }
113 
114 void MockSocketTest::Expect_setReuseAddr(int fd, bool ret) {
115  EXPECT_PROLOGUE(setReuseAddr);
116  setReuseAddr_calls = 1;
117  setReuseAddr_fd = fd;
118  setReuseAddr_ret = ret;
119 }
120 
121 bool bind_ret = true;
122 int bind_fd = 0;
123 int bind_port = 0;
124 int bind_calls = 0;
125 bool XmlRpcSocket::bind(int fd, int port) {
126  bind_calls--;
127  EXPECT_EQ(bind_fd, fd);
128  EXPECT_EQ(bind_port, port);
129  return bind_ret;
130 }
131 
132 void MockSocketTest::Expect_bind(int fd, int port, bool ret) {
133  EXPECT_PROLOGUE(bind);
134  bind_calls = 1;
135  bind_fd = fd;
136  bind_port = port;
137  bind_ret = ret;
138 }
139 
140 bool listen_ret = true;
141 int listen_fd = 0;
143 int listen_calls = 0;
144 bool XmlRpcSocket::listen(int fd, int backlog) {
145  listen_calls--;
146  EXPECT_EQ(listen_fd, fd);
147  EXPECT_EQ(listen_backlog, backlog);
148  return listen_ret;
149 }
150 
151 void MockSocketTest::Expect_listen(int fd, int backlog, bool ret) {
152  EXPECT_PROLOGUE(listen);
153  listen_calls = 1;
154  listen_fd = fd;
155  listen_backlog = backlog;
156  listen_ret = ret;
157 }
158 
159 int accept_ret = 0;
160 int accept_fd = 0;
161 int accept_calls = 0;
162 int XmlRpcSocket::accept(int fd) {
163  accept_calls--;
164  EXPECT_EQ(accept_fd, fd);
165  return accept_ret;
166 }
167 
168 void MockSocketTest::Expect_accept(int fd, int ret) {
169  EXPECT_PROLOGUE(accept);
170  accept_calls = 1;
171  accept_fd = fd;
172  accept_ret = ret;
173 }
174 
175 bool connect_ret = true;
176 int connect_fd = 0;
177 std::string connect_host = "";
178 int connect_port = 0;
180 bool XmlRpcSocket::connect(int fd, const std::string& host, int port) {
181  connect_calls--;
182  EXPECT_EQ(connect_fd, fd);
183  EXPECT_EQ(connect_host, host);
184  EXPECT_EQ(connect_port, port);
185  return connect_ret;
186 }
187 
189  const std::string& host,
190  int port,
191  bool ret) {
192  EXPECT_PROLOGUE(connect);
193  connect_calls = 1;
194  connect_fd = fd;
195  connect_host = host;
196  connect_port = port;
197  connect_ret = ret;
198 }
199 
200 bool nbRead_ret = true;
201 int nbRead_fd = 0;
202 std::string nbRead_s = "";
203 bool nbRead_eof = false;
204 int nbRead_calls = 0;
205 bool XmlRpcSocket::nbRead(int fd, std::string& s, bool* eof) {
206  nbRead_calls--;
207  EXPECT_EQ(nbRead_fd, fd);
208  s = nbRead_s;
209  *eof = nbRead_eof;
210  return nbRead_ret;
211 }
212 
214  const std::string& s,
215  bool eof,
216  bool ret) {
217  EXPECT_PROLOGUE(nbRead);
218  nbRead_calls = 1;
219  nbRead_fd = fd;
220  nbRead_s = s;
221  nbRead_eof = eof;
222  nbRead_ret = ret;
223 }
224 
225 bool nbWrite_ret = true;
226 int nbWrite_fd = 0;
227 std::string nbWrite_s = "";
230 bool XmlRpcSocket::nbWrite(int fd, const std::string& s, int* bytesSoFar) {
231  nbWrite_calls--;
232  EXPECT_EQ(nbWrite_fd, fd);
233  EXPECT_EQ(nbWrite_s, s);
234  *bytesSoFar = nbWrite_bytes;
235  return nbWrite_ret;
236 }
237 
239  const std::string& s,
240  int bytes,
241  bool ret) {
242  EXPECT_PROLOGUE(nbWrite);
243  nbWrite_calls = 1;
244  nbWrite_fd = fd;
245  nbWrite_s = s;
246  nbWrite_bytes = bytes;
247  nbWrite_ret = ret;
248 }
249 
250 int getError_ret = 0;
253  getError_calls--;
254  return getError_ret;
255 }
256 
258  EXPECT_PROLOGUE(getError);
259  getError_calls = 1;
260  getError_ret = ret;
261 }
262 
263 int get_port_ret = 0;
266 int XmlRpcSocket::get_port(int socket) {
267  get_port_calls--;
268  EXPECT_EQ(get_port_socket, socket);
269  return get_port_ret;
270 }
271 
272 void MockSocketTest::Expect_get_port(int socket, int ret) {
273  EXPECT_PROLOGUE(get_port);
274  get_port_calls = 1;
275  get_port_socket = socket;
276  get_port_ret = ret;
277 }
278 
280  socket_calls = 0;
281  close_calls.clear();
283  setReuseAddr_calls = 0;
284  bind_calls = 0;
285  listen_calls = 0;
286  accept_calls = 0;
287  connect_calls = 0;
288  nbRead_calls = 0;
289  nbWrite_calls = 0;
290  getError_calls = 0;
291  get_port_calls = 0;
292 }
293 
295  CheckCalls();
296 }
297 
299  // Check that call counters and queues are empty.
300  EXPECT_EQ(0, socket_calls);
301  EXPECT_EQ(0u, close_calls.size());
302  EXPECT_EQ(0, setNonBlocking_calls);
303  EXPECT_EQ(0, setReuseAddr_calls);
304  EXPECT_EQ(0, bind_calls);
305  EXPECT_EQ(0, listen_calls);
306  EXPECT_EQ(0, accept_calls);
307  EXPECT_EQ(0, connect_calls);
308  EXPECT_EQ(0, nbRead_calls);
309  EXPECT_EQ(0, nbWrite_calls);
310  EXPECT_EQ(0, getError_calls);
311  EXPECT_EQ(0, get_port_calls);
312 
313  // Reset call counters and queues so we don't get leakage between different
314  // parts of the test.
315  socket_calls = 0;
316  close_calls.clear();
318  setReuseAddr_calls = 0;
319  bind_calls = 0;
320  listen_calls = 0;
321  accept_calls = 0;
322  connect_calls = 0;
323  nbRead_calls = 0;
324  nbWrite_calls = 0;
325  getError_calls = 0;
326  get_port_calls = 0;
327 }
get_port_ret
int get_port_ret
Definition: mock_socket.cpp:263
close_calls
std::deque< int > close_calls
Definition: mock_socket.cpp:60
XmlRpc::XmlRpcSocket::getError
static int getError()
Returns last errno.
Definition: XmlRpcSocket.cpp:371
XmlRpc::XmlRpcSocket::socket
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
Definition: XmlRpcSocket.cpp:101
bind_fd
int bind_fd
Definition: mock_socket.cpp:122
nbRead_s
std::string nbRead_s
Definition: mock_socket.cpp:202
bind_port
int bind_port
Definition: mock_socket.cpp:123
XmlRpc::XmlRpcSocket::s_use_ipv6_
static bool s_use_ipv6_
Definition: XmlRpcSocket.h:26
setReuseAddr_calls
int setReuseAddr_calls
Definition: mock_socket.cpp:106
s
XmlRpcServer s
Definition: HelloServer.cpp:11
socket_calls
int socket_calls
Definition: mock_socket.cpp:75
MockSocketTest::Expect_nbWrite
void Expect_nbWrite(int fd, const std::string &s, int bytes, bool ret)
Definition: mock_socket.cpp:238
XmlRpc::XmlRpcSocket::close
static void close(int socket)
Closes a socket.
Definition: XmlRpcSocket.cpp:109
nbWrite_calls
int nbWrite_calls
Definition: mock_socket.cpp:229
setNonBlocking_fd
int setNonBlocking_fd
Definition: mock_socket.cpp:89
EXPECT_PROLOGUE
#define EXPECT_PROLOGUE(name)
Definition: mock_socket.cpp:56
nbRead_ret
bool nbRead_ret
Definition: mock_socket.cpp:200
connect_host
std::string connect_host
Definition: mock_socket.cpp:177
close_fd
int close_fd
Definition: test_socket.cpp:221
MockSocketTest::Expect_nbRead
void Expect_nbRead(int fd, const std::string &s, bool eof, bool ret)
Definition: mock_socket.cpp:213
MockSocketTest::Expect_socket
void Expect_socket(int ret)
Definition: mock_socket.cpp:82
XmlRpc
Definition: XmlRpcClient.h:20
listen_ret
bool listen_ret
Definition: mock_socket.cpp:140
bind_ret
bool bind_ret
Definition: mock_socket.cpp:121
MockSocketTest::Expect_getError
void Expect_getError(int ret)
Definition: mock_socket.cpp:257
getError_ret
int getError_ret
Definition: mock_socket.cpp:250
setReuseAddr_fd
int setReuseAddr_fd
Definition: mock_socket.cpp:105
nbWrite_s
std::string nbWrite_s
Definition: mock_socket.cpp:227
connect_ret
bool connect_ret
Definition: mock_socket.cpp:175
get_port_socket
int get_port_socket
Definition: mock_socket.cpp:264
mock_socket.h
MockSocketTest::Expect_setReuseAddr
void Expect_setReuseAddr(int fd, bool ret)
Definition: mock_socket.cpp:114
socket_ret
int socket_ret
Definition: mock_socket.cpp:74
MockSocketTest::Expect_listen
void Expect_listen(int fd, int backlog, bool ret)
Definition: mock_socket.cpp:151
XmlRpc::XmlRpcSocket::setNonBlocking
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
Definition: XmlRpcSocket.cpp:123
listen_backlog
int listen_backlog
Definition: mock_socket.cpp:142
accept_ret
int accept_ret
Definition: mock_socket.cpp:159
MockSocketTest::Expect_setNonBlocking
void Expect_setNonBlocking(int fd, bool ret)
Definition: mock_socket.cpp:97
accept_calls
int accept_calls
Definition: mock_socket.cpp:161
setNonBlocking_ret
bool setNonBlocking_ret
Definition: mock_socket.cpp:88
XmlRpc::XmlRpcSocket::get_port
static int get_port(int socket)
Definition: XmlRpcSocket.cpp:397
nbWrite_ret
bool nbWrite_ret
Definition: mock_socket.cpp:225
connect_port
int connect_port
Definition: mock_socket.cpp:178
MockSocketTest::Expect_connect
void Expect_connect(int fd, const std::string &host, int port, bool ret)
Definition: mock_socket.cpp:188
XmlRpcSocket.h
MockSocketTest::SetUp
void SetUp()
Definition: mock_socket.cpp:279
XmlRpc::XmlRpcSocket::accept
static int accept(int socket)
Accept a client connection request.
Definition: XmlRpcSocket.cpp:183
nbRead_calls
int nbRead_calls
Definition: mock_socket.cpp:204
MockSocketTest::Expect_accept
void Expect_accept(int fd, int ret)
Definition: mock_socket.cpp:168
accept_fd
int accept_fd
Definition: mock_socket.cpp:160
nbRead_fd
int nbRead_fd
Definition: mock_socket.cpp:201
MockSocketTest::Expect_bind
void Expect_bind(int fd, int port, bool ret)
Definition: mock_socket.cpp:132
listen_calls
int listen_calls
Definition: mock_socket.cpp:143
listen_fd
int listen_fd
Definition: mock_socket.cpp:141
nbWrite_bytes
int nbWrite_bytes
Definition: mock_socket.cpp:228
XmlRpc::XmlRpcSocket::setReuseAddr
static bool setReuseAddr(int socket)
Definition: XmlRpcSocket.cpp:135
XmlRpc::XmlRpcSocket::nbWrite
static bool nbWrite(int socket, const std::string &s, int *bytesSoFar)
Write text to the specified socket. Returns false on error.
Definition: XmlRpcSocket.cpp:335
connect_fd
int connect_fd
Definition: mock_socket.cpp:176
setNonBlocking_calls
int setNonBlocking_calls
Definition: mock_socket.cpp:90
MockSocketTest::Expect_get_port
void Expect_get_port(int socket, int ret)
Definition: mock_socket.cpp:272
XmlRpc::XmlRpcSocket::getErrorMsg
static std::string getErrorMsg()
Returns message corresponding to last error.
Definition: XmlRpcSocket.cpp:383
XmlRpc::XmlRpcSocket::nbRead
static bool nbRead(int socket, std::string &s, bool *eof)
Read text from the specified socket. Returns false on error.
Definition: XmlRpcSocket.cpp:295
connect_calls
int connect_calls
Definition: mock_socket.cpp:179
XmlRpc::XmlRpcSocket::bind
static bool bind(int socket, int port)
Bind to a specified port.
Definition: XmlRpcSocket.cpp:145
setReuseAddr_ret
bool setReuseAddr_ret
Definition: mock_socket.cpp:104
MockSocketTest::CheckCalls
void CheckCalls()
Definition: mock_socket.cpp:298
getError_calls
int getError_calls
Definition: mock_socket.cpp:251
bind_calls
int bind_calls
Definition: mock_socket.cpp:124
XmlRpcUtil.h
MockSocketTest::Expect_close
void Expect_close(int fd)
Definition: mock_socket.cpp:70
nbWrite_fd
int nbWrite_fd
Definition: mock_socket.cpp:226
XmlRpc::XmlRpcSocket::listen
static bool listen(int socket, int backlog)
Set socket in listen mode.
Definition: XmlRpcSocket.cpp:176
XmlRpc::XmlRpcSocket::connect
static bool connect(int socket, const std::string &host, int port)
Connect a socket to a server (from a client)
Definition: XmlRpcSocket.cpp:202
MockSocketTest::TearDown
void TearDown()
Definition: mock_socket.cpp:294
get_port_calls
int get_port_calls
Definition: mock_socket.cpp:265
nbRead_eof
bool nbRead_eof
Definition: mock_socket.cpp:203


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:32