]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-util-sockets.c
test-util-sockets: Correct to set has_abstract, has_tight
[mirror_qemu.git] / tests / test-util-sockets.c
1 /*
2 * Tests for util/qemu-sockets.c
3 *
4 * Copyright 2018 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
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"
27
28 static void test_fd_is_socket_bad(void)
29 {
30 char *tmp = g_strdup("qemu-test-util-sockets-XXXXXX");
31 int fd = mkstemp(tmp);
32 if (fd != 0) {
33 unlink(tmp);
34 }
35 g_free(tmp);
36
37 g_assert(fd >= 0);
38
39 g_assert(!fd_is_socket(fd));
40 close(fd);
41 }
42
43 static void test_fd_is_socket_good(void)
44 {
45 int fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
46
47 g_assert(fd >= 0);
48
49 g_assert(fd_is_socket(fd));
50 close(fd);
51 }
52
53 static int mon_fd = -1;
54 static const char *mon_fdname;
55 __thread Monitor *cur_mon;
56
57 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
58 {
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;
64 }
65 return dup(mon_fd);
66 }
67
68 /*
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.
74 */
75 Monitor *monitor_cur(void) { return cur_mon; }
76 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); }
77
78 #ifndef _WIN32
79 static void test_socket_fd_pass_name_good(void)
80 {
81 SocketAddress addr;
82 int fd;
83
84 cur_mon = g_malloc(1); /* Fake a monitor */
85 mon_fdname = "myfd";
86 mon_fd = qemu_socket(AF_INET, SOCK_STREAM, 0);
87 g_assert_cmpint(mon_fd, >, STDERR_FILENO);
88
89 addr.type = SOCKET_ADDRESS_TYPE_FD;
90 addr.u.fd.str = g_strdup(mon_fdname);
91
92 fd = socket_connect(&addr, &error_abort);
93 g_assert_cmpint(fd, !=, -1);
94 g_assert_cmpint(fd, !=, mon_fd);
95 close(fd);
96
97 fd = socket_listen(&addr, 1, &error_abort);
98 g_assert_cmpint(fd, !=, -1);
99 g_assert_cmpint(fd, !=, mon_fd);
100 close(fd);
101
102 g_free(addr.u.fd.str);
103 mon_fdname = NULL;
104 close(mon_fd);
105 mon_fd = -1;
106 g_free(cur_mon);
107 cur_mon = NULL;
108 }
109
110 static void test_socket_fd_pass_name_bad(void)
111 {
112 SocketAddress addr;
113 Error *err = NULL;
114 int fd;
115
116 cur_mon = g_malloc(1); /* Fake a monitor */
117 mon_fdname = "myfd";
118 mon_fd = dup(STDOUT_FILENO);
119 g_assert_cmpint(mon_fd, >, STDERR_FILENO);
120
121 addr.type = SOCKET_ADDRESS_TYPE_FD;
122 addr.u.fd.str = g_strdup(mon_fdname);
123
124 fd = socket_connect(&addr, &err);
125 g_assert_cmpint(fd, ==, -1);
126 error_free_or_abort(&err);
127
128 fd = socket_listen(&addr, 1, &err);
129 g_assert_cmpint(fd, ==, -1);
130 error_free_or_abort(&err);
131
132 g_free(addr.u.fd.str);
133 mon_fdname = NULL;
134 close(mon_fd);
135 mon_fd = -1;
136 g_free(cur_mon);
137 cur_mon = NULL;
138 }
139
140 static void test_socket_fd_pass_name_nomon(void)
141 {
142 SocketAddress addr;
143 Error *err = NULL;
144 int fd;
145
146 g_assert(cur_mon == NULL);
147
148 addr.type = SOCKET_ADDRESS_TYPE_FD;
149 addr.u.fd.str = g_strdup("myfd");
150
151 fd = socket_connect(&addr, &err);
152 g_assert_cmpint(fd, ==, -1);
153 error_free_or_abort(&err);
154
155 fd = socket_listen(&addr, 1, &err);
156 g_assert_cmpint(fd, ==, -1);
157 error_free_or_abort(&err);
158
159 g_free(addr.u.fd.str);
160 }
161
162
163 static void test_socket_fd_pass_num_good(void)
164 {
165 SocketAddress addr;
166 int fd, sfd;
167
168 g_assert(cur_mon == NULL);
169 sfd = qemu_socket(AF_INET, SOCK_STREAM, 0);
170 g_assert_cmpint(sfd, >, STDERR_FILENO);
171
172 addr.type = SOCKET_ADDRESS_TYPE_FD;
173 addr.u.fd.str = g_strdup_printf("%d", sfd);
174
175 fd = socket_connect(&addr, &error_abort);
176 g_assert_cmpint(fd, ==, sfd);
177
178 fd = socket_listen(&addr, 1, &error_abort);
179 g_assert_cmpint(fd, ==, sfd);
180
181 g_free(addr.u.fd.str);
182 close(sfd);
183 }
184
185 static void test_socket_fd_pass_num_bad(void)
186 {
187 SocketAddress addr;
188 Error *err = NULL;
189 int fd, sfd;
190
191 g_assert(cur_mon == NULL);
192 sfd = dup(STDOUT_FILENO);
193
194 addr.type = SOCKET_ADDRESS_TYPE_FD;
195 addr.u.fd.str = g_strdup_printf("%d", sfd);
196
197 fd = socket_connect(&addr, &err);
198 g_assert_cmpint(fd, ==, -1);
199 error_free_or_abort(&err);
200
201 fd = socket_listen(&addr, 1, &err);
202 g_assert_cmpint(fd, ==, -1);
203 error_free_or_abort(&err);
204
205 g_free(addr.u.fd.str);
206 close(sfd);
207 }
208
209 static void test_socket_fd_pass_num_nocli(void)
210 {
211 SocketAddress addr;
212 Error *err = NULL;
213 int fd;
214
215 cur_mon = g_malloc(1); /* Fake a monitor */
216
217 addr.type = SOCKET_ADDRESS_TYPE_FD;
218 addr.u.fd.str = g_strdup_printf("%d", STDOUT_FILENO);
219
220 fd = socket_connect(&addr, &err);
221 g_assert_cmpint(fd, ==, -1);
222 error_free_or_abort(&err);
223
224 fd = socket_listen(&addr, 1, &err);
225 g_assert_cmpint(fd, ==, -1);
226 error_free_or_abort(&err);
227
228 g_free(addr.u.fd.str);
229 }
230 #endif
231
232 #ifdef __linux__
233 static gchar *abstract_sock_name;
234
235 static gpointer unix_server_thread_func(gpointer user_data)
236 {
237 SocketAddress addr;
238 Error *err = NULL;
239 int fd = -1;
240 int connfd = -1;
241 struct sockaddr_un un;
242 socklen_t len = sizeof(un);
243
244 addr.type = SOCKET_ADDRESS_TYPE_UNIX;
245 addr.u.q_unix.path = abstract_sock_name;
246 addr.u.q_unix.has_tight = true;
247 addr.u.q_unix.tight = user_data != NULL;
248 addr.u.q_unix.has_abstract = true;
249 addr.u.q_unix.abstract = true;
250
251 fd = socket_listen(&addr, 1, &err);
252 g_assert_cmpint(fd, >=, 0);
253 g_assert(fd_is_socket(fd));
254
255 connfd = accept(fd, (struct sockaddr *)&un, &len);
256 g_assert_cmpint(connfd, !=, -1);
257 close(connfd);
258
259 close(fd);
260
261 return NULL;
262 }
263
264 static gpointer unix_client_thread_func(gpointer user_data)
265 {
266 SocketAddress addr;
267 Error *err = NULL;
268 int fd = -1;
269
270 addr.type = SOCKET_ADDRESS_TYPE_UNIX;
271 addr.u.q_unix.path = abstract_sock_name;
272 addr.u.q_unix.has_tight = true;
273 addr.u.q_unix.tight = user_data != NULL;
274 addr.u.q_unix.has_abstract = true;
275 addr.u.q_unix.abstract = true;
276
277 fd = socket_connect(&addr, &err);
278
279 g_assert_cmpint(fd, >=, 0);
280
281 close(fd);
282
283 return NULL;
284 }
285
286 static void test_socket_unix_abstract_good(void)
287 {
288 GRand *r = g_rand_new();
289
290 abstract_sock_name = g_strdup_printf("unix-%d-%d", getpid(),
291 g_rand_int_range(r, 100, 1000));
292
293 /* non tight socklen serv and cli */
294 GThread *serv = g_thread_new("abstract_unix_server",
295 unix_server_thread_func,
296 NULL);
297
298 sleep(1);
299
300 GThread *cli = g_thread_new("abstract_unix_client",
301 unix_client_thread_func,
302 NULL);
303
304 g_thread_join(cli);
305 g_thread_join(serv);
306
307 /* tight socklen serv and cli */
308 serv = g_thread_new("abstract_unix_server",
309 unix_server_thread_func,
310 (gpointer)1);
311
312 sleep(1);
313
314 cli = g_thread_new("abstract_unix_client",
315 unix_client_thread_func,
316 (gpointer)1);
317
318 g_thread_join(cli);
319 g_thread_join(serv);
320
321 g_free(abstract_sock_name);
322 g_rand_free(r);
323 }
324 #endif
325
326 int main(int argc, char **argv)
327 {
328 bool has_ipv4, has_ipv6;
329
330 qemu_init_main_loop(&error_abort);
331 socket_init();
332
333 g_test_init(&argc, &argv, NULL);
334
335 /* We're creating actual IPv4/6 sockets, so we should
336 * check if the host running tests actually supports
337 * each protocol to avoid breaking tests on machines
338 * with either IPv4 or IPv6 disabled.
339 */
340 if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
341 g_printerr("socket_check_protocol_support() failed\n");
342 goto end;
343 }
344
345 if (has_ipv4) {
346 g_test_add_func("/util/socket/is-socket/bad",
347 test_fd_is_socket_bad);
348 g_test_add_func("/util/socket/is-socket/good",
349 test_fd_is_socket_good);
350 #ifndef _WIN32
351 g_test_add_func("/socket/fd-pass/name/good",
352 test_socket_fd_pass_name_good);
353 g_test_add_func("/socket/fd-pass/name/bad",
354 test_socket_fd_pass_name_bad);
355 g_test_add_func("/socket/fd-pass/name/nomon",
356 test_socket_fd_pass_name_nomon);
357 g_test_add_func("/socket/fd-pass/num/good",
358 test_socket_fd_pass_num_good);
359 g_test_add_func("/socket/fd-pass/num/bad",
360 test_socket_fd_pass_num_bad);
361 g_test_add_func("/socket/fd-pass/num/nocli",
362 test_socket_fd_pass_num_nocli);
363 #endif
364 }
365
366 #ifdef __linux__
367 g_test_add_func("/util/socket/unix-abstract/good",
368 test_socket_unix_abstract_good);
369 #endif
370
371 end:
372 return g_test_run();
373 }