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 
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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 
259  getError_calls = 1;
260  getError_ret = ret;
261 }
262 
263 int get_port_ret = 0;
267  get_port_calls--;
268  EXPECT_EQ(get_port_socket, socket);
269  return get_port_ret;
270 }
271 
274  get_port_calls = 1;
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 }
int socket_calls
Definition: mock_socket.cpp:75
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:97
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:90
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:70
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:89
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:60
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:56
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:82
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:74
bool setNonBlocking_ret
Definition: mock_socket.cpp:88
int nbWrite_calls


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:22