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
;
56 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
59 g_assert(mon
== cur_mon
);
60 if (mon_fd
== -1 || !g_str_equal(mon_fdname
, fdname
)) {
61 error_setg(errp
, "No fd named %s", fdname
);
67 /* Syms in libqemustub.a are discarded at .o file granularity.
68 * To replace monitor_get_fd() we must ensure everything in
69 * stubs/monitor.c is defined, to make sure monitor.o is discarded
70 * otherwise we get duplicate syms at link time.
72 __thread Monitor
*cur_mon
;
73 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
) { abort(); }
74 void monitor_init_qmp(Chardev
*chr
, bool pretty
) {}
75 void monitor_init_hmp(Chardev
*chr
, bool use_readline
) {}
78 static void test_socket_fd_pass_name_good(void)
83 cur_mon
= g_malloc(1); /* Fake a monitor */
85 mon_fd
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
86 g_assert_cmpint(mon_fd
, >, STDERR_FILENO
);
88 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
89 addr
.u
.fd
.str
= g_strdup(mon_fdname
);
91 fd
= socket_connect(&addr
, &error_abort
);
92 g_assert_cmpint(fd
, !=, -1);
93 g_assert_cmpint(fd
, !=, mon_fd
);
96 fd
= socket_listen(&addr
, 1, &error_abort
);
97 g_assert_cmpint(fd
, !=, -1);
98 g_assert_cmpint(fd
, !=, mon_fd
);
101 g_free(addr
.u
.fd
.str
);
109 static void test_socket_fd_pass_name_bad(void)
115 cur_mon
= g_malloc(1); /* Fake a monitor */
117 mon_fd
= dup(STDOUT_FILENO
);
118 g_assert_cmpint(mon_fd
, >, STDERR_FILENO
);
120 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
121 addr
.u
.fd
.str
= g_strdup(mon_fdname
);
123 fd
= socket_connect(&addr
, &err
);
124 g_assert_cmpint(fd
, ==, -1);
125 error_free_or_abort(&err
);
127 fd
= socket_listen(&addr
, 1, &err
);
128 g_assert_cmpint(fd
, ==, -1);
129 error_free_or_abort(&err
);
131 g_free(addr
.u
.fd
.str
);
139 static void test_socket_fd_pass_name_nomon(void)
145 g_assert(cur_mon
== NULL
);
147 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
148 addr
.u
.fd
.str
= g_strdup("myfd");
150 fd
= socket_connect(&addr
, &err
);
151 g_assert_cmpint(fd
, ==, -1);
152 error_free_or_abort(&err
);
154 fd
= socket_listen(&addr
, 1, &err
);
155 g_assert_cmpint(fd
, ==, -1);
156 error_free_or_abort(&err
);
158 g_free(addr
.u
.fd
.str
);
162 static void test_socket_fd_pass_num_good(void)
167 g_assert(cur_mon
== NULL
);
168 sfd
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
169 g_assert_cmpint(sfd
, >, STDERR_FILENO
);
171 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
172 addr
.u
.fd
.str
= g_strdup_printf("%d", sfd
);
174 fd
= socket_connect(&addr
, &error_abort
);
175 g_assert_cmpint(fd
, ==, sfd
);
177 fd
= socket_listen(&addr
, 1, &error_abort
);
178 g_assert_cmpint(fd
, ==, sfd
);
180 g_free(addr
.u
.fd
.str
);
184 static void test_socket_fd_pass_num_bad(void)
190 g_assert(cur_mon
== NULL
);
191 sfd
= dup(STDOUT_FILENO
);
193 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
194 addr
.u
.fd
.str
= g_strdup_printf("%d", sfd
);
196 fd
= socket_connect(&addr
, &err
);
197 g_assert_cmpint(fd
, ==, -1);
198 error_free_or_abort(&err
);
200 fd
= socket_listen(&addr
, 1, &err
);
201 g_assert_cmpint(fd
, ==, -1);
202 error_free_or_abort(&err
);
204 g_free(addr
.u
.fd
.str
);
208 static void test_socket_fd_pass_num_nocli(void)
214 cur_mon
= g_malloc(1); /* Fake a monitor */
216 addr
.type
= SOCKET_ADDRESS_TYPE_FD
;
217 addr
.u
.fd
.str
= g_strdup_printf("%d", STDOUT_FILENO
);
219 fd
= socket_connect(&addr
, &err
);
220 g_assert_cmpint(fd
, ==, -1);
221 error_free_or_abort(&err
);
223 fd
= socket_listen(&addr
, 1, &err
);
224 g_assert_cmpint(fd
, ==, -1);
225 error_free_or_abort(&err
);
227 g_free(addr
.u
.fd
.str
);
231 int main(int argc
, char **argv
)
233 bool has_ipv4
, has_ipv6
;
237 g_test_init(&argc
, &argv
, NULL
);
239 /* We're creating actual IPv4/6 sockets, so we should
240 * check if the host running tests actually supports
241 * each protocol to avoid breaking tests on machines
242 * with either IPv4 or IPv6 disabled.
244 if (socket_check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
245 g_printerr("socket_check_protocol_support() failed\n");
250 g_test_add_func("/util/socket/is-socket/bad",
251 test_fd_is_socket_bad
);
252 g_test_add_func("/util/socket/is-socket/good",
253 test_fd_is_socket_good
);
254 g_test_add_func("/socket/fd-pass/name/good",
255 test_socket_fd_pass_name_good
);
256 g_test_add_func("/socket/fd-pass/name/bad",
257 test_socket_fd_pass_name_bad
);
258 g_test_add_func("/socket/fd-pass/name/nomon",
259 test_socket_fd_pass_name_nomon
);
260 g_test_add_func("/socket/fd-pass/num/good",
261 test_socket_fd_pass_num_good
);
262 g_test_add_func("/socket/fd-pass/num/bad",
263 test_socket_fd_pass_num_bad
);
264 g_test_add_func("/socket/fd-pass/num/nocli",
265 test_socket_fd_pass_num_nocli
);