32 #include <gtest/gtest.h>
34 #include <system_error>
56 using std::stringstream;
65 std::string server_addrname(
"/tmp/server.sock");
66 std::string client_addrname(
"/tmp/client.sock");
68 struct unix_networking :
public testing::Test
70 std::string snd, got, testgot;
75 unix_networking() : snd(
"this is a test line\nthis is the second\nanother\n\nlast one")
81 std::system_error err;
83 server_addr.
host(server_addrname);
85 client_addr.
host(client_addrname);
87 virtual ~unix_networking()
89 std::system_error err;
98 log.
id(
"unix tcp stream client");
101 stringstream testst(snd);
105 log <<
"sending " << snd.size() << endl;
106 ASSERT_TRUE(stream << snd << endl);
109 while (std::getline(testst, testgot))
111 ASSERT_TRUE(std::getline(stream, got));
112 log <<
"got reply " << got.size() << endl;
113 ASSERT_EQ(got, testgot);
118 ASSERT_TRUE(stream << snd << endl);
121 stringstream testst2(snd);
122 while (testst2 >> testgot)
124 ASSERT_TRUE(stream >> got);
125 log <<
"got reply " << got.size() << endl;
126 ASSERT_EQ(got, testgot);
129 log <<
"tcp_stream_test: end" << endl;
136 log.
id(
"unix tcp readwrite client");
138 log <<
"sending " << snd.size() << endl;
139 ASSERT_EQ(
sock.send(snd.data(), snd.size()), snd.size());
141 char buf[snd.size()];
145 got =
sock.recv(buf+sz, snd.size()-sz);
146 log <<
"got reply " << got << endl;
150 while (sz < snd.size());
151 ASSERT_EQ(sz, snd.size());
152 ASSERT_EQ(memcmp(buf, snd.data(), sz), 0);
154 log <<
"end" << endl;
161 log.
id(
"unix udp client");
163 log <<
"sending " << snd.size() <<
" to " << addr.
str() << endl;
164 ASSERT_EQ(
sock.send(snd.data(), snd.size(), addr), snd.size());
166 char buf[snd.size()];
169 got =
sock.recv(buf, snd.size(), from);
170 log <<
"got reply " << got <<
" from " << from.
host() << endl;
171 ASSERT_EQ(got, snd.size());
172 ASSERT_EQ(memcmp(buf, snd.data(), snd.size()), 0);
174 log <<
"end" << endl;
181 log.
id(
"unix udp server");
185 log <<
"reuse_addr" << endl;
186 sock.reuse_addr(
true);
187 log <<
"bind to " << addr.
host() << endl;
194 pin.
set(
sock.fd(), Poller::input);
195 pin.
set(quitev.
fd(), Poller::input);
207 auto sz =
sock.recv_next();
209 sock.recv(buf, sz, from);
210 log <<
"got " << sz <<
" from " << from.
host() << endl;
211 sock.send(buf, sz, from);
215 catch (
const std::exception& e)
217 log <<
"exception: " << e.what() << endl;
229 log.
id(
"unix tcp stream server");
233 log <<
"reuse_addr" << endl;
234 sock.reuse_addr(
true);
235 log <<
"bind to " << addr.
host() << endl;
237 log <<
"listen" << endl;
242 auto conn =
sock.accept(from);
243 log <<
"connect from " << from << endl;
247 for (
string got; std::getline(stream, got);)
249 log <<
"got " << got.size() << endl;
250 stream << got << endl;
254 catch (
const std::exception& e)
256 log <<
"exception: " << e.what() << endl;
268 log.
id(
"unix tcp readwrite server");
272 log <<
"reuse_addr" << endl;
273 sock.reuse_addr(
true);
274 log <<
"bind to " << addr.
host() << endl;
276 log <<
"listen" << endl;
281 auto conn =
sock.accept(from);
282 log <<
"connect from " << from << endl;
286 while ((sz = conn.recv(buf, 16)) > 0)
288 log <<
"got " << sz << endl;
292 catch (
const std::exception& e)
294 log <<
"exception: " << e.what() << endl;
303 TEST_F(unix_networking, addrnames)
305 ASSERT_EQ(server_addr.
host(), server_addrname);
306 ASSERT_EQ(client_addr.
host(), client_addrname);
309 TEST_F(unix_networking, udp_readwrite)
314 auto fut = async(udp_readwrite_server, std::ref(startev), std::ref(servsock), std::ref(server_addr), std::ref(from), std::ref(quitev));
321 log <<
"binding to " << client_addr << endl;
322 sock.bind(client_addr);
324 udp_writeread_test(sock, server_addr);
329 log <<
"return code: " << res << endl;
332 ASSERT_EQ(from.
host(), client_addr.
host());
335 TEST_F(unix_networking, tcp_iostream)
340 auto fut = async(tcp_iostream_server, std::ref(startev), std::ref(servsock), std::ref(server_addr), std::ref(from));
347 log <<
"binding to " << client_addr << endl;
348 sock.bind(client_addr);
350 log <<
"connect" << endl;
351 sock.connect(server_addr);
353 log <<
"connected to addr name=" << server_addr.
host() << endl;
355 tcp_stream_test(sock);
360 log <<
"return code: " << res << endl;
363 ASSERT_EQ(from.
host(), client_addr.
host());
366 TEST_F(unix_networking, tcp_readwrite)
371 auto fut = async(tcp_readwrite_server, std::ref(startev), std::ref(servsock), std::ref(server_addr), std::ref(from));
377 log <<
"binding to " << client_addr << endl;
378 sock.bind(client_addr);
380 log <<
"connect" << endl;
381 sock.connect(server_addr);
383 log <<
"connected to addr name=" << server_addr.
host() << endl;
385 tcp_writeread_test(sock);
389 log <<
"return code: " << res << endl;
391 ASSERT_EQ(from.
host(), client_addr.
host());
A unix domain address, which is a file of type "socket.".
virtual std::string host() const
Get host name.
virtual std::string str() const
Descriptive string for socket address.
Unix domain tcp (stream) socket.
Unix domain udp (datagram) socket.
Signaling kernel event counter.
uint64_t read()
Read from (decrement) the event counter.
int fd() const
Return file descriptor.
void write(uint64_t)
Write to (increment) the event counter.
static void remove(const std::string &, std::system_error *=nullptr)
Remove the file or directory.
Input/output stream wrapper for reader/writer.
Thread-safe stream logger.
void id(const std::string &id="")
Set the id.
void add_cout()
Add std::cout console stream.
Poller which allows polling of generic file descriptors for various events.
void set(int, int)
Add a file desriptor to poller.
int event(int)
Return flags which were polled for this file descriptor.
Signaling kernel event counter.
Common file system utilities.
Internet tcp and udp networking.
Base input/output stream classes.
Linux kernel i/o event notification (poller).
Unix domain tcp and udp networking.