2 * Tests for util/qemu-sockets.c
4 * Copyright 2018 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "qemu/sockets.h"
24 #include "qapi/error.h"
25 #include "socket-helpers.h"
26 #include "monitor/monitor.h"
28 static void test_fd_is_socket_bad(void)
30 char *tmp
= g_strdup("qemu-test-util-sockets-XXXXXX");
31 int fd
= mkstemp(tmp
);
39 g_assert(!fd_is_socket(fd
));
43 static void test_fd_is_socket_good(void)
45 int fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
49 g_assert(fd_is_socket(fd
));
53 static int mon_fd
= -1;
54 static const char *mon_fdname
;
55 __thread Monitor
*cur_mon
;
57 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
60 g_assert(mon
== cur_mon
);
61 if (mon_fd
== -1 || !g_str_equal(mon_fdname
, fdname
)) {
62 error_setg(errp
, "No fd named %s", fdname
);
69 * Syms of stubs in libqemuutil.a are discarded at .o file
70 * granularity. To replace monitor_get_fd() and monitor_cur(), we
71 * must ensure that we also replace any other symbol that is used in
72 * the binary and would be taken from the same stub object file,
73 * otherwise we get duplicate syms at link time.
75 Monitor
*monitor_cur(void) { return cur_mon
; }
76 Monitor
*monitor_set_cur(Coroutine
*co
, Monitor
*mon
) { abort(); }
77 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
) { abort(); }
80 static void test_socket_fd_pass_name_good(void)
85 cur_mon
= g_malloc(1); /* Fake a monitor */
87 mon_fd
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
88 g_assert_cmpint(mon_fd
, >, STDERR_FILENO
);
90 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
91 addr
.u
.fd
.str
= g_strdup(mon_fdname
);
93 fd
= socket_connect(&addr
, &error_abort
);
94 g_assert_cmpint(fd
, !=, -1);
95 g_assert_cmpint(fd
, !=, mon_fd
);
98 fd
= socket_listen(&addr
, 1, &error_abort
);
99 g_assert_cmpint(fd
, !=, -1);
100 g_assert_cmpint(fd
, !=, mon_fd
);
103 g_free(addr
.u
.fd
.str
);
111 static void test_socket_fd_pass_name_bad(void)
117 cur_mon
= g_malloc(1); /* Fake a monitor */
119 mon_fd
= dup(STDOUT_FILENO
);
120 g_assert_cmpint(mon_fd
, >, STDERR_FILENO
);
122 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
123 addr
.u
.fd
.str
= g_strdup(mon_fdname
);
125 fd
= socket_connect(&addr
, &err
);
126 g_assert_cmpint(fd
, ==, -1);
127 error_free_or_abort(&err
);
129 fd
= socket_listen(&addr
, 1, &err
);
130 g_assert_cmpint(fd
, ==, -1);
131 error_free_or_abort(&err
);
133 g_free(addr
.u
.fd
.str
);
141 static void test_socket_fd_pass_name_nomon(void)
147 g_assert(cur_mon
== NULL
);
149 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
150 addr
.u
.fd
.str
= g_strdup("myfd");
152 fd
= socket_connect(&addr
, &err
);
153 g_assert_cmpint(fd
, ==, -1);
154 error_free_or_abort(&err
);
156 fd
= socket_listen(&addr
, 1, &err
);
157 g_assert_cmpint(fd
, ==, -1);
158 error_free_or_abort(&err
);
160 g_free(addr
.u
.fd
.str
);
164 static void test_socket_fd_pass_num_good(void)
169 g_assert(cur_mon
== NULL
);
170 sfd
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
171 g_assert_cmpint(sfd
, >, STDERR_FILENO
);
173 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
174 addr
.u
.fd
.str
= g_strdup_printf("%d", sfd
);
176 fd
= socket_connect(&addr
, &error_abort
);
177 g_assert_cmpint(fd
, ==, sfd
);
179 fd
= socket_listen(&addr
, 1, &error_abort
);
180 g_assert_cmpint(fd
, ==, sfd
);
182 g_free(addr
.u
.fd
.str
);
186 static void test_socket_fd_pass_num_bad(void)
192 g_assert(cur_mon
== NULL
);
193 sfd
= dup(STDOUT_FILENO
);
195 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
196 addr
.u
.fd
.str
= g_strdup_printf("%d", sfd
);
198 fd
= socket_connect(&addr
, &err
);
199 g_assert_cmpint(fd
, ==, -1);
200 error_free_or_abort(&err
);
202 fd
= socket_listen(&addr
, 1, &err
);
203 g_assert_cmpint(fd
, ==, -1);
204 error_free_or_abort(&err
);
206 g_free(addr
.u
.fd
.str
);
210 static void test_socket_fd_pass_num_nocli(void)
216 cur_mon
= g_malloc(1); /* Fake a monitor */
218 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
219 addr
.u
.fd
.str
= g_strdup_printf("%d", STDOUT_FILENO
);
221 fd
= socket_connect(&addr
, &err
);
222 g_assert_cmpint(fd
, ==, -1);
223 error_free_or_abort(&err
);
225 fd
= socket_listen(&addr
, 1, &err
);
226 g_assert_cmpint(fd
, ==, -1);
227 error_free_or_abort(&err
);
229 g_free(addr
.u
.fd
.str
);
235 #define ABSTRACT_SOCKET_VARIANTS 3
238 SocketAddress
*server
, *client
[ABSTRACT_SOCKET_VARIANTS
];
239 bool expect_connect
[ABSTRACT_SOCKET_VARIANTS
];
240 } abstract_socket_matrix_row
;
242 static gpointer
unix_client_thread_func(gpointer user_data
)
244 abstract_socket_matrix_row
*row
= user_data
;
248 for (i
= 0; i
< ABSTRACT_SOCKET_VARIANTS
; i
++) {
249 if (row
->expect_connect
[i
]) {
250 fd
= socket_connect(row
->client
[i
], &error_abort
);
251 g_assert_cmpint(fd
, >=, 0);
253 fd
= socket_connect(row
->client
[i
], &err
);
254 g_assert_cmpint(fd
, ==, -1);
255 error_free_or_abort(&err
);
262 static void test_socket_unix_abstract_row(abstract_socket_matrix_row
*test
)
266 struct sockaddr_un un
;
267 socklen_t len
= sizeof(un
);
269 /* Last one must connect, or else accept() below hangs */
270 assert(test
->expect_connect
[ABSTRACT_SOCKET_VARIANTS
- 1]);
272 fd
= socket_listen(test
->server
, 1, &error_abort
);
273 g_assert_cmpint(fd
, >=, 0);
274 g_assert(fd_is_socket(fd
));
276 cli
= g_thread_new("abstract_unix_client",
277 unix_client_thread_func
,
280 for (i
= 0; i
< ABSTRACT_SOCKET_VARIANTS
; i
++) {
281 if (test
->expect_connect
[i
]) {
282 connfd
= accept(fd
, (struct sockaddr
*)&un
, &len
);
283 g_assert_cmpint(connfd
, !=, -1);
292 static void test_socket_unix_abstract(void)
294 SocketAddress addr
, addr_tight
, addr_padded
;
295 abstract_socket_matrix_row matrix
[ABSTRACT_SOCKET_VARIANTS
] = {
297 { &addr_tight
, &addr_padded
, &addr
},
298 { true, false, true } },
300 { &addr_padded
, &addr
, &addr_tight
},
301 { false, true, true } },
303 { &addr
, &addr_tight
, &addr_padded
},
304 { false, false, true } }
308 addr
.type
= SOCKET_ADDRESS_TYPE_UNIX
;
309 addr
.u
.q_unix
.path
= g_strdup_printf("unix-%d-%u",
310 getpid(), g_random_int());
311 addr
.u
.q_unix
.has_abstract
= true;
312 addr
.u
.q_unix
.abstract
= true;
313 addr
.u
.q_unix
.has_tight
= false;
314 addr
.u
.q_unix
.tight
= false;
317 addr_tight
.u
.q_unix
.has_tight
= true;
318 addr_tight
.u
.q_unix
.tight
= true;
321 addr_padded
.u
.q_unix
.has_tight
= true;
322 addr_padded
.u
.q_unix
.tight
= false;
324 for (i
= 0; i
< ABSTRACT_SOCKET_VARIANTS
; i
++) {
325 test_socket_unix_abstract_row(&matrix
[i
]);
328 g_free(addr
.u
.q_unix
.path
);
331 #endif /* CONFIG_LINUX */
333 int main(int argc
, char **argv
)
335 bool has_ipv4
, has_ipv6
;
337 qemu_init_main_loop(&error_abort
);
340 g_test_init(&argc
, &argv
, NULL
);
342 /* We're creating actual IPv4/6 sockets, so we should
343 * check if the host running tests actually supports
344 * each protocol to avoid breaking tests on machines
345 * with either IPv4 or IPv6 disabled.
347 if (socket_check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
348 g_printerr("socket_check_protocol_support() failed\n");
353 g_test_add_func("/util/socket/is-socket/bad",
354 test_fd_is_socket_bad
);
355 g_test_add_func("/util/socket/is-socket/good",
356 test_fd_is_socket_good
);
358 g_test_add_func("/socket/fd-pass/name/good",
359 test_socket_fd_pass_name_good
);
360 g_test_add_func("/socket/fd-pass/name/bad",
361 test_socket_fd_pass_name_bad
);
362 g_test_add_func("/socket/fd-pass/name/nomon",
363 test_socket_fd_pass_name_nomon
);
364 g_test_add_func("/socket/fd-pass/num/good",
365 test_socket_fd_pass_num_good
);
366 g_test_add_func("/socket/fd-pass/num/bad",
367 test_socket_fd_pass_num_bad
);
368 g_test_add_func("/socket/fd-pass/num/nocli",
369 test_socket_fd_pass_num_nocli
);
374 g_test_add_func("/util/socket/unix-abstract",
375 test_socket_unix_abstract
);