33 #include <gtest/gtest.h> 50 strerror_s(err, 60, error);
52 snprintf(err,
sizeof(err),
"%s", strerror(error));
54 return std::string(err);
57 #define EXPECT_PROLOGUE(name) \ 58 EXPECT_EQ(0, name##_calls) \ 59 << "Test error; cannont expect " #name " more than once"; 63 EXPECT_LE(1u, close_calls.size());
64 if (close_calls.size() > 0) {
66 close_calls.pop_front();
67 EXPECT_EQ(close_fd, fd);
72 close_calls.push_back(fd);
78 EXPECT_EQ(1, socket_calls);
93 EXPECT_EQ(1, setNonBlocking_calls);
94 setNonBlocking_calls--;
95 EXPECT_EQ(setNonBlocking_fd, fd);
100 setNonBlocking_calls = 1;
101 setNonBlocking_fd = fd;
102 setNonBlocking_ret = ret;
109 EXPECT_EQ(1, setReuseAddr_calls);
110 setReuseAddr_calls--;
111 EXPECT_EQ(setReuseAddr_fd, fd);
117 setReuseAddr_calls = 1;
118 setReuseAddr_fd = fd;
119 setReuseAddr_ret = ret;
128 EXPECT_EQ(bind_fd, fd);
129 EXPECT_EQ(bind_port, port);
147 EXPECT_EQ(listen_fd, fd);
148 EXPECT_EQ(listen_backlog, backlog);
156 listen_backlog = backlog;
165 EXPECT_EQ(accept_fd, fd);
183 EXPECT_EQ(connect_fd, fd);
184 EXPECT_EQ(connect_host, host);
185 EXPECT_EQ(connect_port, port);
190 const std::string& host,
208 EXPECT_EQ(nbRead_fd, fd);
215 const std::string&
s,
233 EXPECT_EQ(nbWrite_fd, fd);
234 EXPECT_EQ(nbWrite_s, s);
240 const std::string&
s,
247 nbWrite_bytes = bytes;
269 EXPECT_EQ(get_port_socket, socket);
283 setNonBlocking_calls = 0;
284 setReuseAddr_calls = 0;
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);
318 setNonBlocking_calls = 0;
319 setReuseAddr_calls = 0;
void Expect_listen(int fd, int backlog, bool ret)
static bool setReuseAddr(int socket)
void Expect_setNonBlocking(int fd, bool ret)
static int get_port(int socket)
static int accept(int socket)
Accept a client connection request.
void Expect_setReuseAddr(int fd, bool ret)
void Expect_bind(int fd, int port, bool ret)
static bool nbRead(int socket, std::string &s, bool *eof)
Read text from the specified socket. Returns false on error.
static std::string getErrorMsg()
Returns message corresponding to last error.
void Expect_close(int fd)
static bool nbWrite(int socket, const std::string &s, int *bytesSoFar)
Write text to the specified socket. Returns false on error.
void Expect_connect(int fd, const std::string &host, int port, bool ret)
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
static bool listen(int socket, int backlog)
Set socket in listen mode.
#define EXPECT_PROLOGUE(name)
void Expect_get_port(int socket, int ret)
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
static void close(int socket)
Closes a socket.
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
void Expect_nbRead(int fd, const std::string &s, bool eof, bool ret)
void Expect_socket(int ret)
void Expect_accept(int fd, int ret)
void Expect_nbWrite(int fd, const std::string &s, int bytes, bool ret)