hw/display/xlnx_dp: Free FIFOs adding xlnx_dp_finalize()
[qemu/ar7.git] / tests / unit / test-util-sockets.c
blob72b92465298218927da6ab240d1173e2e984cef5
1 /*
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);
32 if (fd != 0) {
33 unlink(tmp);
35 g_free(tmp);
37 g_assert(fd >= 0);
39 g_assert(!fd_is_socket(fd));
40 close(fd);
43 static void test_fd_is_socket_good(void)
45 int fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
47 g_assert(fd >= 0);
49 g_assert(fd_is_socket(fd));
50 close(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)
59 g_assert(cur_mon);
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);
63 return -1;
65 return dup(mon_fd);
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(); }
79 #ifndef _WIN32
80 static void test_socket_fd_pass_name_good(void)
82 SocketAddress addr;
83 int fd;
85 cur_mon = g_malloc(1); /* Fake a monitor */
86 mon_fdname = "myfd";
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);
96 close(fd);
98 fd = socket_listen(&addr, 1, &error_abort);
99 g_assert_cmpint(fd, !=, -1);
100 g_assert_cmpint(fd, !=, mon_fd);
101 close(fd);
103 g_free(addr.u.fd.str);
104 mon_fdname = NULL;
105 close(mon_fd);
106 mon_fd = -1;
107 g_free(cur_mon);
108 cur_mon = NULL;
111 static void test_socket_fd_pass_name_bad(void)
113 SocketAddress addr;
114 Error *err = NULL;
115 int fd;
117 cur_mon = g_malloc(1); /* Fake a monitor */
118 mon_fdname = "myfd";
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);
134 mon_fdname = NULL;
135 close(mon_fd);
136 mon_fd = -1;
137 g_free(cur_mon);
138 cur_mon = NULL;
141 static void test_socket_fd_pass_name_nomon(void)
143 SocketAddress addr;
144 Error *err = NULL;
145 int fd;
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)
166 SocketAddress addr;
167 int fd, sfd;
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);
183 close(sfd);
186 static void test_socket_fd_pass_num_bad(void)
188 SocketAddress addr;
189 Error *err = NULL;
190 int fd, sfd;
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);
207 close(sfd);
210 static void test_socket_fd_pass_num_nocli(void)
212 SocketAddress addr;
213 Error *err = NULL;
214 int fd;
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);
231 #endif
233 #ifdef CONFIG_LINUX
235 #define ABSTRACT_SOCKET_VARIANTS 3
237 typedef struct {
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;
245 Error *err = NULL;
246 int i, fd;
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);
252 } else {
253 fd = socket_connect(row->client[i], &err);
254 g_assert_cmpint(fd, ==, -1);
255 error_free_or_abort(&err);
257 close(fd);
259 return NULL;
262 static void test_socket_unix_abstract_row(abstract_socket_matrix_row *test)
264 int fd, connfd, i;
265 GThread *cli;
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,
278 test);
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);
284 close(connfd);
288 close(fd);
289 g_thread_join(cli);
292 static void test_socket_unix_abstract(void)
294 SocketAddress addr, addr_tight, addr_padded;
295 abstract_socket_matrix_row matrix[ABSTRACT_SOCKET_VARIANTS] = {
296 { &addr,
297 { &addr_tight, &addr_padded, &addr },
298 { true, false, true } },
299 { &addr_tight,
300 { &addr_padded, &addr, &addr_tight },
301 { false, true, true } },
302 { &addr_padded,
303 { &addr, &addr_tight, &addr_padded },
304 { false, false, true } }
306 int i;
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;
316 addr_tight = addr;
317 addr_tight.u.q_unix.has_tight = true;
318 addr_tight.u.q_unix.tight = true;
320 addr_padded = addr;
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);
338 socket_init();
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");
349 goto end;
352 if (has_ipv4) {
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);
357 #ifndef _WIN32
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);
370 #endif
373 #ifdef CONFIG_LINUX
374 g_test_add_func("/util/socket/unix-abstract",
375 test_socket_unix_abstract);
376 #endif
378 end:
379 return g_test_run();