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
|
/*
* QTest testcase for netdev stream and dgram
*
* Copyright (c) 2022 Red Hat, Inc.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qemu/sockets.h"
#include <glib/gstdio.h>
#include "../unit/socket-helpers.h"
#include "libqtest.h"
#define CONNECTION_TIMEOUT 60
#define EXPECT_STATE(q, e, t) \
do { \
char *resp = NULL; \
g_test_timer_start(); \
do { \
g_free(resp); \
resp = qtest_hmp(q, "info network"); \
if (t) { \
strrchr(resp, t)[0] = 0; \
} \
if (g_str_equal(resp, e)) { \
break; \
} \
} while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
g_assert_cmpstr(resp, ==, e); \
g_free(resp); \
} while (0)
static gchar *tmpdir;
static int inet_get_free_port_socket_ipv4(int sock)
{
struct sockaddr_in addr;
socklen_t len;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = 0;
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
return -1;
}
len = sizeof(addr);
if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) {
return -1;
}
return ntohs(addr.sin_port);
}
static int inet_get_free_port_socket_ipv6(int sock)
{
struct sockaddr_in6 addr;
socklen_t len;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_addr = in6addr_any;
addr.sin6_port = 0;
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
return -1;
}
len = sizeof(addr);
if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) {
return -1;
}
return ntohs(addr.sin6_port);
}
static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
{
int sock[nb];
int i;
for (i = 0; i < nb; i++) {
sock[i] = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
if (sock[i] < 0) {
break;
}
port[i] = ipv6 ? inet_get_free_port_socket_ipv6(sock[i]) :
inet_get_free_port_socket_ipv4(sock[i]);
if (port[i] == -1) {
break;
}
}
nb = i;
for (i = 0; i < nb; i++) {
closesocket(sock[i]);
}
return nb;
}
static int inet_get_free_port(bool ipv6)
{
int nb, port;
nb = inet_get_free_port_multiple(1, &port, ipv6);
g_assert_cmpint(nb, ==, 1);
return port;
}
static void test_stream_inet_ipv4(void)
{
QTestState *qts0, *qts1;
char *expect;
int port;
port = inet_get_free_port(false);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=true,addr.type=inet,"
"addr.ipv4=on,addr.ipv6=off,"
"addr.host=127.0.0.1,addr.port=%d", port);
EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,server=false,id=st0,addr.type=inet,"
"addr.ipv4=on,addr.ipv6=off,"
"addr.host=127.0.0.1,addr.port=%d", port);
expect = g_strdup_printf("st0: index=0,type=stream,tcp:127.0.0.1:%d\r\n",
port);
EXPECT_STATE(qts1, expect, 0);
g_free(expect);
/* the port is unknown, check only the address */
EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:127.0.0.1", ':');
qtest_quit(qts1);
qtest_quit(qts0);
}
static void test_stream_inet_ipv6(void)
{
QTestState *qts0, *qts1;
char *expect;
int port;
port = inet_get_free_port(true);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=true,addr.type=inet,"
"addr.ipv4=off,addr.ipv6=on,"
"addr.host=::1,addr.port=%d", port);
EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,server=false,id=st0,addr.type=inet,"
"addr.ipv4=off,addr.ipv6=on,"
"addr.host=::1,addr.port=%d", port);
expect = g_strdup_printf("st0: index=0,type=stream,tcp:::1:%d\r\n",
port);
EXPECT_STATE(qts1, expect, 0);
g_free(expect);
/* the port is unknown, check only the address */
EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':');
qtest_quit(qts1);
qtest_quit(qts0);
}
static void test_stream_unix(void)
{
QTestState *qts0, *qts1;
char *expect;
gchar *path;
path = g_strconcat(tmpdir, "/stream_unix", NULL);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=true,"
"addr.type=unix,addr.path=%s,",
path);
EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=false,"
"addr.type=unix,addr.path=%s",
path);
expect = g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path);
EXPECT_STATE(qts1, expect, 0);
EXPECT_STATE(qts0, expect, 0);
g_free(expect);
g_free(path);
qtest_quit(qts1);
qtest_quit(qts0);
}
#ifdef CONFIG_LINUX
static void test_stream_unix_abstract(void)
{
QTestState *qts0, *qts1;
char *expect;
gchar *path;
path = g_strconcat(tmpdir, "/stream_unix_abstract", NULL);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=true,"
"addr.type=unix,addr.path=%s,"
"addr.abstract=on",
path);
EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=false,"
"addr.type=unix,addr.path=%s,addr.abstract=on",
path);
expect = g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path);
EXPECT_STATE(qts1, expect, 0);
EXPECT_STATE(qts0, expect, 0);
g_free(expect);
g_free(path);
qtest_quit(qts1);
qtest_quit(qts0);
}
#endif
#ifndef _WIN32
static void test_stream_fd(void)
{
QTestState *qts0, *qts1;
int sock[2];
int ret;
ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, sock);
g_assert_true(ret == 0);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,addr.type=fd,addr.str=%d",
sock[0]);
EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,addr.type=fd,addr.str=%d",
sock[1]);
EXPECT_STATE(qts1, "st0: index=0,type=stream,unix:\r\n", 0);
EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0);
qtest_quit(qts1);
qtest_quit(qts0);
closesocket(sock[0]);
closesocket(sock[1]);
}
#endif
static void test_dgram_inet(void)
{
QTestState *qts0, *qts1;
char *expect;
int port[2];
int nb;
nb = inet_get_free_port_multiple(2, port, false);
g_assert_cmpint(nb, ==, 2);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,"
"local.type=inet,local.host=127.0.0.1,local.port=%d,"
"remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
port[0], port[1]);
expect = g_strdup_printf("st0: index=0,type=dgram,"
"udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
port[0], port[1]);
EXPECT_STATE(qts0, expect, 0);
g_free(expect);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,"
"local.type=inet,local.host=127.0.0.1,local.port=%d,"
"remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
port[1], port[0]);
expect = g_strdup_printf("st0: index=0,type=dgram,"
"udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
port[1], port[0]);
EXPECT_STATE(qts1, expect, 0);
g_free(expect);
qtest_quit(qts1);
qtest_quit(qts0);
}
#ifndef _WIN32
static void test_dgram_mcast(void)
{
QTestState *qts;
qts = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,"
"remote.type=inet,remote.host=230.0.0.1,remote.port=1234");
EXPECT_STATE(qts, "st0: index=0,type=dgram,mcast=230.0.0.1:1234\r\n", 0);
qtest_quit(qts);
}
static void test_dgram_unix(void)
{
QTestState *qts0, *qts1;
char *expect;
gchar *path0, *path1;
path0 = g_strconcat(tmpdir, "/dgram_unix0", NULL);
path1 = g_strconcat(tmpdir, "/dgram_unix1", NULL);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,local.type=unix,local.path=%s,"
"remote.type=unix,remote.path=%s",
path0, path1);
expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
path0, path1);
EXPECT_STATE(qts0, expect, 0);
g_free(expect);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,local.type=unix,local.path=%s,"
"remote.type=unix,remote.path=%s",
path1, path0);
expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
path1, path0);
EXPECT_STATE(qts1, expect, 0);
g_free(expect);
unlink(path0);
g_free(path0);
unlink(path1);
g_free(path1);
qtest_quit(qts1);
qtest_quit(qts0);
}
static void test_dgram_fd(void)
{
QTestState *qts0, *qts1;
char *expect;
int ret;
int sv[2];
ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, sv);
g_assert_cmpint(ret, !=, -1);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,local.type=fd,local.str=%d",
sv[0]);
expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[0]);
EXPECT_STATE(qts0, expect, 0);
g_free(expect);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev dgram,id=st0,local.type=fd,local.str=%d",
sv[1]);
expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[1]);
EXPECT_STATE(qts1, expect, 0);
g_free(expect);
qtest_quit(qts1);
qtest_quit(qts0);
closesocket(sv[0]);
closesocket(sv[1]);
}
#endif
int main(int argc, char **argv)
{
int ret;
bool has_ipv4, has_ipv6, has_afunix;
g_autoptr(GError) err = NULL;
socket_init();
g_test_init(&argc, &argv, NULL);
if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
g_error("socket_check_protocol_support() failed\n");
}
tmpdir = g_dir_make_tmp("netdev-socket.XXXXXX", &err);
if (tmpdir == NULL) {
g_error("Can't create temporary directory in %s: %s",
g_get_tmp_dir(), err->message);
}
if (has_ipv4) {
qtest_add_func("/netdev/stream/inet/ipv4", test_stream_inet_ipv4);
qtest_add_func("/netdev/dgram/inet", test_dgram_inet);
#ifndef _WIN32
qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast);
#endif
}
if (has_ipv6) {
qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6);
}
socket_check_afunix_support(&has_afunix);
if (has_afunix) {
#ifndef _WIN32
qtest_add_func("/netdev/dgram/unix", test_dgram_unix);
#endif
qtest_add_func("/netdev/stream/unix", test_stream_unix);
#ifdef CONFIG_LINUX
qtest_add_func("/netdev/stream/unix/abstract",
test_stream_unix_abstract);
#endif
#ifndef _WIN32
qtest_add_func("/netdev/stream/fd", test_stream_fd);
qtest_add_func("/netdev/dgram/fd", test_dgram_fd);
#endif
}
ret = g_test_run();
g_rmdir(tmpdir);
g_free(tmpdir);
return ret;
}
|