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 #include "xmlrpcpp/XmlRpcSocket.h"
25 #include "xmlrpcpp/XmlRpcUtil.h"
26 #include "mock_socket.h"
27 
28 #include <deque>
29 
30 #include <string.h>
31 #include <errno.h>
32 
33 #include <gtest/gtest.h>
34 
35 using namespace XmlRpc;
36 
37 bool XmlRpcSocket::s_use_ipv6_ = false;
38 
39 // Returns message corresponding to last errno.
40 // NOTE(austin): this matches the default implementation.
41 std::string XmlRpcSocket::getErrorMsg() {
42  return getErrorMsg(getError());
43 }
44 
45 // Returns message corresponding to errno
46 // NOTE(austin): this matches the default implementation.
47 std::string XmlRpcSocket::getErrorMsg(int error) {
48  char err[60];
49 #ifdef _MSC_VER
50  strerror_s(err, 60, error);
51 #else
52  snprintf(err, sizeof(err), "%s", strerror(error));
53 #endif
54  return std::string(err);
55 }
56 
57 #define EXPECT_PROLOGUE(name) \
58  EXPECT_EQ(0, name##_calls) \
59  << "Test error; cannont expect " #name " more than once";
60 
61 std::deque<int> close_calls;
62 void XmlRpcSocket::close(int fd) {
63  EXPECT_LE(1u, close_calls.size());
64  if (close_calls.size() > 0) {
65  int close_fd = close_calls.front();
66  close_calls.pop_front();
67  EXPECT_EQ(close_fd, fd);
68  }
69 }
70 
72  close_calls.push_back(fd);
73 }
74 
75 int socket_ret = 0;
76 int socket_calls = 0;
78  EXPECT_EQ(1, socket_calls);
79  socket_calls--;
80  return socket_ret;
81 }
82 
85  socket_calls = 1;
86  socket_ret = ret;
87 }
88 
89 bool setNonBlocking_ret = true;
92 bool XmlRpcSocket::setNonBlocking(int fd) {
93  EXPECT_EQ(1, setNonBlocking_calls);
94  setNonBlocking_calls--;
95  EXPECT_EQ(setNonBlocking_fd, fd);
96  return setNonBlocking_ret;
97 }
98 void MockSocketTest::Expect_setNonBlocking(int fd, bool ret) {
100  setNonBlocking_calls = 1;
101  setNonBlocking_fd = fd;
102  setNonBlocking_ret = ret;
103 }
104 
105 bool setReuseAddr_ret = true;
108 bool XmlRpcSocket::setReuseAddr(int fd) {
109  EXPECT_EQ(1, setReuseAddr_calls);
110  setReuseAddr_calls--;
111  EXPECT_EQ(setReuseAddr_fd, fd);
112  return setReuseAddr_ret;
113 }
114 
115 void MockSocketTest::Expect_setReuseAddr(int fd, bool ret) {
117  setReuseAddr_calls = 1;
118  setReuseAddr_fd = fd;
119  setReuseAddr_ret = ret;
120 }
121 
122 bool bind_ret = true;
123 int bind_fd = 0;
124 int bind_port = 0;
125 int bind_calls = 0;
126 bool XmlRpcSocket::bind(int fd, int port) {
127  bind_calls--;
128  EXPECT_EQ(bind_fd, fd);
129  EXPECT_EQ(bind_port, port);
130  return bind_ret;
131 }
132 
133 void MockSocketTest::Expect_bind(int fd, int port, bool ret) {
135  bind_calls = 1;
136  bind_fd = fd;
137  bind_port = port;
138  bind_ret = ret;
139 }
140 
141 bool listen_ret = true;
142 int listen_fd = 0;
144 int listen_calls = 0;
145 bool XmlRpcSocket::listen(int fd, int backlog) {
146  listen_calls--;
147  EXPECT_EQ(listen_fd, fd);
148  EXPECT_EQ(listen_backlog, backlog);
149  return listen_ret;
150 }
151 
152 void MockSocketTest::Expect_listen(int fd, int backlog, bool ret) {
154  listen_calls = 1;
155  listen_fd = fd;
156  listen_backlog = backlog;
157  listen_ret = ret;
158 }
159 
160 int accept_ret = 0;
161 int accept_fd = 0;
162 int accept_calls = 0;
163 int XmlRpcSocket::accept(int fd) {
164  accept_calls--;
165  EXPECT_EQ(accept_fd, fd);
166  return accept_ret;
167 }
168 
169 void MockSocketTest::Expect_accept(int fd, int ret) {
171  accept_calls = 1;
172  accept_fd = fd;
173  accept_ret = ret;
174 }
175 
176 bool connect_ret = true;
177 int connect_fd = 0;
178 std::string connect_host = "";
179 int connect_port = 0;
181 bool XmlRpcSocket::connect(int fd, const std::string& host, int port) {
182  connect_calls--;
183  EXPECT_EQ(connect_fd, fd);
184  EXPECT_EQ(connect_host, host);
185  EXPECT_EQ(connect_port, port);
186  return connect_ret;
187 }
188 
190  const std::string& host,
191  int port,
192  bool ret) {
194  connect_calls = 1;
195  connect_fd = fd;
196  connect_host = host;
197  connect_port = port;
198  connect_ret = ret;
199 }
200 
201 bool nbRead_ret = true;
202 int nbRead_fd = 0;
203 std::string nbRead_s = "";
204 bool nbRead_eof = false;
205 int nbRead_calls = 0;
206 bool XmlRpcSocket::nbRead(int fd, std::string& s, bool* eof) {
207  nbRead_calls--;
208  EXPECT_EQ(nbRead_fd, fd);
209  s = nbRead_s;
210  *eof = nbRead_eof;
211  return nbRead_ret;
212 }
213 
215  const std::string& s,
216  bool eof,
217  bool ret) {
219  nbRead_calls = 1;
220  nbRead_fd = fd;
221  nbRead_s = s;
222  nbRead_eof = eof;
223  nbRead_ret = ret;
224 }
225 
226 bool nbWrite_ret = true;
227 int nbWrite_fd = 0;
228 std::string nbWrite_s = "";
231 bool XmlRpcSocket::nbWrite(int fd, const std::string& s, int* bytesSoFar) {
232  nbWrite_calls--;
233  EXPECT_EQ(nbWrite_fd, fd);
234  EXPECT_EQ(nbWrite_s, s);
235  *bytesSoFar = nbWrite_bytes;
236  return nbWrite_ret;
237 }
238 
240  const std::string& s,
241  int bytes,
242  bool ret) {
244  nbWrite_calls = 1;
245  nbWrite_fd = fd;
246  nbWrite_s = s;
247  nbWrite_bytes = bytes;
248  nbWrite_ret = ret;
249 }
250 
251 int getError_ret = 0;
254  getError_calls--;
255  return getError_ret;
256 }
257 
260  getError_calls = 1;
261  getError_ret = ret;
262 }
263 
264 int get_port_ret = 0;
268  get_port_calls--;
269  EXPECT_EQ(get_port_socket, socket);
270  return get_port_ret;
271 }
272 
275  get_port_calls = 1;
276  get_port_socket = socket;
277  get_port_ret = ret;
278 }
279 
281  socket_calls = 0;
282  close_calls.clear();
283  setNonBlocking_calls = 0;
284  setReuseAddr_calls = 0;
285  bind_calls = 0;
286  listen_calls = 0;
287  accept_calls = 0;
288  connect_calls = 0;
289  nbRead_calls = 0;
290  nbWrite_calls = 0;
291  getError_calls = 0;
292  get_port_calls = 0;
293 }
294 
296  CheckCalls();
297 }
298 
300  // Check that call counters and queues are empty.
301  EXPECT_EQ(0, socket_calls);
302  EXPECT_EQ(0u, close_calls.size());
303  EXPECT_EQ(0, setNonBlocking_calls);
304  EXPECT_EQ(0, setReuseAddr_calls);
305  EXPECT_EQ(0, bind_calls);
306  EXPECT_EQ(0, listen_calls);
307  EXPECT_EQ(0, accept_calls);
308  EXPECT_EQ(0, connect_calls);
309  EXPECT_EQ(0, nbRead_calls);
310  EXPECT_EQ(0, nbWrite_calls);
311  EXPECT_EQ(0, getError_calls);
312  EXPECT_EQ(0, get_port_calls);
313 
314  // Reset call counters and queues so we don't get leakage between different
315  // parts of the test.
316  socket_calls = 0;
317  close_calls.clear();
318  setNonBlocking_calls = 0;
319  setReuseAddr_calls = 0;
320  bind_calls = 0;
321  listen_calls = 0;
322  accept_calls = 0;
323  connect_calls = 0;
324  nbRead_calls = 0;
325  nbWrite_calls = 0;
326  getError_calls = 0;
327  get_port_calls = 0;
328 }
int socket_calls
Definition: mock_socket.cpp:76
int accept_ret
void Expect_listen(int fd, int backlog, bool ret)
bool nbRead_ret
static bool setReuseAddr(int socket)
int close_fd
void Expect_setNonBlocking(int fd, bool ret)
Definition: mock_socket.cpp:98
static int get_port(int socket)
int nbRead_calls
int setReuseAddr_fd
static int accept(int socket)
Accept a client connection request.
int get_port_ret
void Expect_setReuseAddr(int fd, bool ret)
int get_port_socket
int nbWrite_bytes
int setNonBlocking_calls
Definition: mock_socket.cpp:91
XmlRpcServer s
Definition: HelloServer.cpp:11
int bind_fd
void Expect_bind(int fd, int port, bool ret)
int bind_port
static bool nbRead(int socket, std::string &s, bool *eof)
Read text from the specified socket. Returns false on error.
int connect_calls
bool setReuseAddr_ret
int connect_port
static std::string getErrorMsg()
Returns message corresponding to last error.
void Expect_close(int fd)
Definition: mock_socket.cpp:71
static bool nbWrite(int socket, const std::string &s, int *bytesSoFar)
Write text to the specified socket. Returns false on error.
int bind_calls
void Expect_connect(int fd, const std::string &host, int port, bool ret)
int setNonBlocking_fd
Definition: mock_socket.cpp:90
int getError_calls
bool nbRead_eof
int nbWrite_fd
int accept_fd
std::string connect_host
int nbRead_fd
static bool bind(int socket, int port)
Bind to a specified port.
static int getError()
Returns last errno.
void Expect_getError(int ret)
static bool connect(int socket, const std::string &host, int port)
Connect a socket to a server (from a client)
std::deque< int > close_calls
Definition: mock_socket.cpp:61
static bool s_use_ipv6_
Definition: XmlRpcSocket.h:26
int listen_backlog
std::string nbRead_s
int connect_fd
static bool listen(int socket, int backlog)
Set socket in listen mode.
int accept_calls
#define EXPECT_PROLOGUE(name)
Definition: mock_socket.cpp:57
void Expect_get_port(int socket, int ret)
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
int setReuseAddr_calls
static void close(int socket)
Closes a socket.
bool listen_ret
int get_port_calls
int getError_ret
int listen_calls
bool bind_ret
bool connect_ret
int listen_fd
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
std::string nbWrite_s
void Expect_nbRead(int fd, const std::string &s, bool eof, bool ret)
void Expect_socket(int ret)
Definition: mock_socket.cpp:83
bool nbWrite_ret
void Expect_accept(int fd, int ret)
void Expect_nbWrite(int fd, const std::string &s, int bytes, bool ret)
int socket_ret
Definition: mock_socket.cpp:75
bool setNonBlocking_ret
Definition: mock_socket.cpp:89
int nbWrite_calls


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix
autogenerated on Sun Feb 3 2019 03:29:51