-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathsocket_tests.cpp
More file actions
474 lines (386 loc) · 10.5 KB
/
socket_tests.cpp
File metadata and controls
474 lines (386 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/io_service.hpp>
#include <cppcoro/net/socket.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/when_all.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <cppcoro/cancellation_source.hpp>
#include <cppcoro/cancellation_token.hpp>
#include <cppcoro/async_scope.hpp>
#include "doctest/cppcoro_doctest.h"
using namespace cppcoro;
using namespace cppcoro::net;
TEST_SUITE_BEGIN("socket");
TEST_CASE("create TCP/IPv4")
{
io_service ioSvc;
auto socket = socket::create_tcpv4(ioSvc);
}
TEST_CASE("create TCP/IPv6")
{
io_service ioSvc;
auto socket = socket::create_tcpv6(ioSvc);
}
TEST_CASE("create UDP/IPv4")
{
io_service ioSvc;
auto socket = socket::create_udpv4(ioSvc);
}
TEST_CASE("create UDP/IPv6")
{
io_service ioSvc;
auto socket = socket::create_udpv6(ioSvc);
}
TEST_CASE("TCP/IPv4 connect/disconnect")
{
io_service ioSvc;
ip_endpoint serverAddress;
task<int> serverTask;
auto server = [&](socket listeningSocket) -> task<int>
{
auto s = socket::create_tcpv4(ioSvc);
co_await listeningSocket.accept(s);
co_await s.disconnect();
co_return 0;
};
{
auto serverSocket = socket::create_tcpv4(ioSvc);
serverSocket.bind(ipv4_endpoint{ ipv4_address::loopback(), 0 });
serverSocket.listen(3);
serverAddress = serverSocket.local_endpoint();
serverTask = server(std::move(serverSocket));
}
auto client = [&]() -> task<int>
{
auto s = socket::create_tcpv4(ioSvc);
s.bind(ipv4_endpoint{ ipv4_address::loopback(), 0 });
co_await s.connect(serverAddress);
co_await s.disconnect();
co_return 0;
};
task<int> clientTask = client();
(void)sync_wait(when_all(
[&]() -> task<int>
{
auto stopOnExit = on_scope_exit([&] { ioSvc.stop(); });
(void)co_await when_all(std::move(serverTask), std::move(clientTask));
co_return 0;
}(),
[&]() -> task<int>
{
ioSvc.process_events();
co_return 0;
}()));
}
TEST_CASE("send/recv TCP/IPv4")
{
io_service ioSvc;
auto listeningSocket = socket::create_tcpv4(ioSvc);
listeningSocket.bind(ipv4_endpoint{ ipv4_address::loopback(), 0 });
listeningSocket.listen(3);
auto echoServer = [&]() -> task<int>
{
auto acceptingSocket = socket::create_tcpv4(ioSvc);
co_await listeningSocket.accept(acceptingSocket);
std::uint8_t buffer[64];
std::size_t bytesReceived;
do
{
bytesReceived = co_await acceptingSocket.recv(buffer, sizeof(buffer));
if (bytesReceived > 0)
{
std::size_t bytesSent = 0;
do
{
bytesSent += co_await acceptingSocket.send(
buffer + bytesSent,
bytesReceived - bytesSent);
} while (bytesSent < bytesReceived);
}
} while (bytesReceived > 0);
acceptingSocket.close_send();
co_await acceptingSocket.disconnect();
co_return 0;
};
auto echoClient = [&]() -> task<int>
{
auto connectingSocket = socket::create_tcpv4(ioSvc);
connectingSocket.bind(ipv4_endpoint{});
co_await connectingSocket.connect(listeningSocket.local_endpoint());
auto receive = [&]() -> task<int>
{
std::uint8_t buffer[100];
std::uint64_t totalBytesReceived = 0;
std::size_t bytesReceived;
do
{
bytesReceived = co_await connectingSocket.recv(buffer, sizeof(buffer));
for (std::size_t i = 0; i < bytesReceived; ++i)
{
std::uint64_t byteIndex = totalBytesReceived + i;
std::uint8_t expectedByte = 'a' + (byteIndex % 26);
CHECK(buffer[i] == expectedByte);
}
totalBytesReceived += bytesReceived;
} while (bytesReceived > 0);
CHECK(totalBytesReceived == 1000);
co_return 0;
};
auto send = [&]() -> task<int>
{
std::uint8_t buffer[100];
for (std::uint64_t i = 0; i < 1000; i += sizeof(buffer))
{
for (std::size_t j = 0; j < sizeof(buffer); ++j)
{
buffer[j] = 'a' + ((i + j) % 26);
}
std::size_t bytesSent = 0;
do
{
bytesSent += co_await connectingSocket.send(buffer + bytesSent, sizeof(buffer) - bytesSent);
} while (bytesSent < sizeof(buffer));
}
connectingSocket.close_send();
co_return 0;
};
co_await when_all(send(), receive());
co_await connectingSocket.disconnect();
co_return 0;
};
(void)sync_wait(when_all(
[&]() -> task<int>
{
auto stopOnExit = on_scope_exit([&] { ioSvc.stop(); });
(void)co_await when_all(echoClient(), echoServer());
co_return 0;
}(),
[&]() -> task<int>
{
ioSvc.process_events();
co_return 0;
}()));
}
#if !CPPCORO_COMPILER_MSVC || CPPCORO_COMPILER_MSVC > 191627032 || !CPPCORO_CPU_X86
// HACK: Don't compile this function under MSVC x86.
// It results in an ICE under VS 2017.15 and earlier.
TEST_CASE("send/recv TCP/IPv4 many connections")
{
io_service ioSvc;
auto listeningSocket = socket::create_tcpv4(ioSvc);
listeningSocket.bind(ipv4_endpoint{ ipv4_address::loopback(), 0 });
listeningSocket.listen(20);
cancellation_source canceller;
auto handleConnection = [](socket s) -> task<void>
{
std::uint8_t buffer[64];
std::size_t bytesReceived;
do
{
bytesReceived = co_await s.recv(buffer, sizeof(buffer));
if (bytesReceived > 0)
{
std::size_t bytesSent = 0;
do
{
bytesSent += co_await s.send(
buffer + bytesSent,
bytesReceived - bytesSent);
} while (bytesSent < bytesReceived);
}
} while (bytesReceived > 0);
s.close_send();
co_await s.disconnect();
};
auto echoServer = [&](cancellation_token ct) -> task<>
{
async_scope connectionScope;
std::exception_ptr ex;
try
{
while (true) {
auto acceptingSocket = socket::create_tcpv4(ioSvc);
co_await listeningSocket.accept(acceptingSocket, ct);
connectionScope.spawn(
handleConnection(std::move(acceptingSocket)));
}
}
catch (const cppcoro::operation_cancelled&)
{
}
catch (...)
{
ex = std::current_exception();
}
co_await connectionScope.join();
if (ex)
{
std::rethrow_exception(ex);
}
};
auto echoClient = [&]() -> task<>
{
auto connectingSocket = socket::create_tcpv4(ioSvc);
connectingSocket.bind(ipv4_endpoint{});
co_await connectingSocket.connect(listeningSocket.local_endpoint());
auto receive = [&]() -> task<>
{
std::uint8_t buffer[100];
std::uint64_t totalBytesReceived = 0;
std::size_t bytesReceived;
do
{
bytesReceived = co_await connectingSocket.recv(buffer, sizeof(buffer));
for (std::size_t i = 0; i < bytesReceived; ++i)
{
std::uint64_t byteIndex = totalBytesReceived + i;
std::uint8_t expectedByte = 'a' + (byteIndex % 26);
CHECK(buffer[i] == expectedByte);
}
totalBytesReceived += bytesReceived;
} while (bytesReceived > 0);
CHECK(totalBytesReceived == 1000);
};
auto send = [&]() -> task<>
{
std::uint8_t buffer[100];
for (std::uint64_t i = 0; i < 1000; i += sizeof(buffer))
{
for (std::size_t j = 0; j < sizeof(buffer); ++j)
{
buffer[j] = 'a' + ((i + j) % 26);
}
std::size_t bytesSent = 0;
do
{
bytesSent += co_await connectingSocket.send(buffer + bytesSent, sizeof(buffer) - bytesSent);
} while (bytesSent < sizeof(buffer));
}
connectingSocket.close_send();
};
co_await when_all(send(), receive());
co_await connectingSocket.disconnect();
};
auto manyEchoClients = [&](int count) -> task<void>
{
auto shutdownServerOnExit = on_scope_exit([&]
{
canceller.request_cancellation();
});
std::vector<task<>> clientTasks;
clientTasks.reserve(count);
for (int i = 0; i < count; ++i)
{
clientTasks.emplace_back(echoClient());
}
co_await when_all(std::move(clientTasks));
};
(void)sync_wait(when_all(
[&]() -> task<>
{
auto stopOnExit = on_scope_exit([&] { ioSvc.stop(); });
(void)co_await when_all(
manyEchoClients(20),
echoServer(canceller.token()));
}(),
[&]() -> task<>
{
ioSvc.process_events();
co_return;
}()));
}
#endif
TEST_CASE("udp send_to/recv_from")
{
io_service ioSvc;
auto server = [&](socket serverSocket) -> task<int>
{
std::uint8_t buffer[100];
auto[bytesReceived, remoteEndPoint] = co_await serverSocket.recv_from(buffer, 100);
CHECK(bytesReceived == 50);
// Send an ACK response.
{
const std::uint8_t response[1] = { 0 };
co_await serverSocket.send_to(remoteEndPoint, &response, 1);
}
// Second message received won't fit within buffer.
try
{
std::tie(bytesReceived, remoteEndPoint) = co_await serverSocket.recv_from(buffer, 100);
FAIL("Should have thrown");
}
catch (const std::system_error&)
{
// TODO: Map this situation to some kind of error_condition value.
// The win32 ERROR_MORE_DATA error code doesn't seem to map to any of the standard std::errc values.
//
// CHECK(ex.code() == ???);
//
// Possibly also need to switch to returning a std::error_code directly rather than
// throwing a std::system_error for this case.
}
// Send an NACK response.
{
const std::uint8_t response[1] = { 1 };
co_await serverSocket.send_to(remoteEndPoint, response, 1);
}
co_return 0;
};
ip_endpoint serverAddress;
task<int> serverTask;
{
auto serverSocket = socket::create_udpv4(ioSvc);
serverSocket.bind(ipv4_endpoint{ ipv4_address::loopback(), 0 });
serverAddress = serverSocket.local_endpoint();
serverTask = server(std::move(serverSocket));
}
auto client = [&]() -> task<int>
{
auto socket = socket::create_udpv4(ioSvc);
// don't need to bind(), should be implicitly bound on first send_to().
// Send first message of 50 bytes
{
std::uint8_t buffer[50] = { 0 };
co_await socket.send_to(serverAddress, buffer, 50);
}
// Receive ACK message
{
std::uint8_t buffer[1];
auto[bytesReceived, ackAddress] = co_await socket.recv_from(buffer, 1);
CHECK(bytesReceived == 1);
CHECK(buffer[0] == 0);
CHECK(ackAddress == serverAddress);
}
// Send second message of 128 bytes
{
std::uint8_t buffer[128] = { 0 };
co_await socket.send_to(serverAddress, buffer, 128);
}
// Receive NACK message
{
std::uint8_t buffer[1];
auto[bytesReceived, ackAddress] = co_await socket.recv_from(buffer, 1);
CHECK(bytesReceived == 1);
CHECK(buffer[0] == 1);
CHECK(ackAddress == serverAddress);
}
co_return 0;
};
(void)sync_wait(when_all(
[&]() -> task<int>
{
auto stopOnExit = on_scope_exit([&] { ioSvc.stop(); });
(void)co_await when_all(std::move(serverTask), client());
co_return 0;
}(),
[&]() -> task<int>
{
ioSvc.process_events();
co_return 0;
}()));
}
TEST_SUITE_END();