]>
Commit | Line | Data |
---|---|---|
58dc31f1 DB |
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" | |
30bdb3c5 | 26 | #include "monitor/monitor.h" |
58dc31f1 DB |
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 | ||
30bdb3c5 DB |
53 | static int mon_fd = -1; |
54 | static const char *mon_fdname; | |
947e4744 | 55 | __thread Monitor *cur_mon; |
30bdb3c5 DB |
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 | ||
d2a71d74 | 68 | /* |
947e4744 KW |
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, | |
30bdb3c5 DB |
73 | * otherwise we get duplicate syms at link time. |
74 | */ | |
947e4744 | 75 | Monitor *monitor_cur(void) { return cur_mon; } |
637de4db | 76 | int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); } |
30bdb3c5 | 77 | |
8330bd53 | 78 | #ifndef _WIN32 |
1723d6b1 | 79 | static void test_socket_fd_pass_name_good(void) |
30bdb3c5 DB |
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 | ||
e5b6353c | 97 | fd = socket_listen(&addr, 1, &error_abort); |
30bdb3c5 DB |
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 | ||
1723d6b1 | 110 | static void test_socket_fd_pass_name_bad(void) |
30bdb3c5 DB |
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 | ||
e5b6353c | 128 | fd = socket_listen(&addr, 1, &err); |
30bdb3c5 DB |
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 | ||
1723d6b1 DB |
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 | ||
e5b6353c | 155 | fd = socket_listen(&addr, 1, &err); |
1723d6b1 DB |
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 | ||
e5b6353c | 178 | fd = socket_listen(&addr, 1, &error_abort); |
1723d6b1 DB |
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 | ||
e5b6353c | 201 | fd = socket_listen(&addr, 1, &err); |
1723d6b1 DB |
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 | ||
e5b6353c | 224 | fd = socket_listen(&addr, 1, &err); |
1723d6b1 DB |
225 | g_assert_cmpint(fd, ==, -1); |
226 | error_free_or_abort(&err); | |
227 | ||
228 | g_free(addr.u.fd.str); | |
229 | } | |
8330bd53 | 230 | #endif |
1723d6b1 | 231 | |
8acefc79 | 232 | #ifdef CONFIG_LINUX |
a72f6754 MA |
233 | |
234 | #define ABSTRACT_SOCKET_VARIANTS 3 | |
235 | ||
236 | typedef struct { | |
237 | SocketAddress *server, *client[ABSTRACT_SOCKET_VARIANTS]; | |
238 | bool expect_connect[ABSTRACT_SOCKET_VARIANTS]; | |
239 | } abstract_socket_matrix_row; | |
240 | ||
4d3a329a XZ |
241 | static gpointer unix_client_thread_func(gpointer user_data) |
242 | { | |
a72f6754 MA |
243 | abstract_socket_matrix_row *row = user_data; |
244 | Error *err = NULL; | |
245 | int i, fd; | |
246 | ||
247 | for (i = 0; i < ABSTRACT_SOCKET_VARIANTS; i++) { | |
248 | if (row->expect_connect[i]) { | |
249 | fd = socket_connect(row->client[i], &error_abort); | |
250 | g_assert_cmpint(fd, >=, 0); | |
251 | } else { | |
252 | fd = socket_connect(row->client[i], &err); | |
253 | g_assert_cmpint(fd, ==, -1); | |
254 | error_free_or_abort(&err); | |
255 | } | |
256 | close(fd); | |
257 | } | |
4d3a329a XZ |
258 | return NULL; |
259 | } | |
260 | ||
a72f6754 | 261 | static void test_socket_unix_abstract_row(abstract_socket_matrix_row *test) |
89cb0bb5 | 262 | { |
a72f6754 | 263 | int fd, connfd, i; |
39458d4e MA |
264 | GThread *cli; |
265 | struct sockaddr_un un; | |
266 | socklen_t len = sizeof(un); | |
89cb0bb5 | 267 | |
a72f6754 MA |
268 | /* Last one must connect, or else accept() below hangs */ |
269 | assert(test->expect_connect[ABSTRACT_SOCKET_VARIANTS - 1]); | |
270 | ||
271 | fd = socket_listen(test->server, 1, &error_abort); | |
39458d4e MA |
272 | g_assert_cmpint(fd, >=, 0); |
273 | g_assert(fd_is_socket(fd)); | |
89cb0bb5 MA |
274 | |
275 | cli = g_thread_new("abstract_unix_client", | |
276 | unix_client_thread_func, | |
a72f6754 MA |
277 | test); |
278 | ||
279 | for (i = 0; i < ABSTRACT_SOCKET_VARIANTS; i++) { | |
280 | if (test->expect_connect[i]) { | |
281 | connfd = accept(fd, (struct sockaddr *)&un, &len); | |
282 | g_assert_cmpint(connfd, !=, -1); | |
283 | close(connfd); | |
284 | } | |
285 | } | |
39458d4e MA |
286 | |
287 | close(fd); | |
89cb0bb5 | 288 | g_thread_join(cli); |
89cb0bb5 MA |
289 | } |
290 | ||
a72f6754 | 291 | static void test_socket_unix_abstract(void) |
4d3a329a | 292 | { |
a72f6754 MA |
293 | SocketAddress addr, addr_tight, addr_padded; |
294 | abstract_socket_matrix_row matrix[ABSTRACT_SOCKET_VARIANTS] = { | |
295 | { &addr, | |
296 | { &addr_tight, &addr_padded, &addr }, | |
b08cc97d | 297 | { true, false, true } }, |
a72f6754 MA |
298 | { &addr_tight, |
299 | { &addr_padded, &addr, &addr_tight }, | |
b08cc97d | 300 | { false, true, true } }, |
a72f6754 MA |
301 | { &addr_padded, |
302 | { &addr, &addr_tight, &addr_padded }, | |
b08cc97d | 303 | { false, false, true } } |
a72f6754 MA |
304 | }; |
305 | int i; | |
4d3a329a | 306 | |
718a9be0 MA |
307 | addr.type = SOCKET_ADDRESS_TYPE_UNIX; |
308 | addr.u.q_unix.path = g_strdup_printf("unix-%d-%u", | |
309 | getpid(), g_random_int()); | |
310 | addr.u.q_unix.has_abstract = true; | |
311 | addr.u.q_unix.abstract = true; | |
718a9be0 MA |
312 | addr.u.q_unix.has_tight = false; |
313 | addr.u.q_unix.tight = false; | |
4d3a329a | 314 | |
a72f6754 MA |
315 | addr_tight = addr; |
316 | addr_tight.u.q_unix.has_tight = true; | |
317 | addr_tight.u.q_unix.tight = true; | |
318 | ||
319 | addr_padded = addr; | |
320 | addr_padded.u.q_unix.has_tight = true; | |
321 | addr_padded.u.q_unix.tight = false; | |
322 | ||
323 | for (i = 0; i < ABSTRACT_SOCKET_VARIANTS; i++) { | |
324 | test_socket_unix_abstract_row(&matrix[i]); | |
325 | } | |
4d3a329a | 326 | |
718a9be0 | 327 | g_free(addr.u.q_unix.path); |
4d3a329a | 328 | } |
8acefc79 MA |
329 | |
330 | #endif /* CONFIG_LINUX */ | |
1723d6b1 | 331 | |
58dc31f1 DB |
332 | int main(int argc, char **argv) |
333 | { | |
334 | bool has_ipv4, has_ipv6; | |
335 | ||
8330bd53 | 336 | qemu_init_main_loop(&error_abort); |
58dc31f1 DB |
337 | socket_init(); |
338 | ||
339 | g_test_init(&argc, &argv, NULL); | |
340 | ||
341 | /* We're creating actual IPv4/6 sockets, so we should | |
342 | * check if the host running tests actually supports | |
343 | * each protocol to avoid breaking tests on machines | |
344 | * with either IPv4 or IPv6 disabled. | |
345 | */ | |
346 | if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) { | |
a4eb74a6 MAL |
347 | g_printerr("socket_check_protocol_support() failed\n"); |
348 | goto end; | |
58dc31f1 DB |
349 | } |
350 | ||
351 | if (has_ipv4) { | |
352 | g_test_add_func("/util/socket/is-socket/bad", | |
353 | test_fd_is_socket_bad); | |
354 | g_test_add_func("/util/socket/is-socket/good", | |
355 | test_fd_is_socket_good); | |
8330bd53 | 356 | #ifndef _WIN32 |
1723d6b1 DB |
357 | g_test_add_func("/socket/fd-pass/name/good", |
358 | test_socket_fd_pass_name_good); | |
359 | g_test_add_func("/socket/fd-pass/name/bad", | |
360 | test_socket_fd_pass_name_bad); | |
361 | g_test_add_func("/socket/fd-pass/name/nomon", | |
362 | test_socket_fd_pass_name_nomon); | |
363 | g_test_add_func("/socket/fd-pass/num/good", | |
364 | test_socket_fd_pass_num_good); | |
365 | g_test_add_func("/socket/fd-pass/num/bad", | |
366 | test_socket_fd_pass_num_bad); | |
367 | g_test_add_func("/socket/fd-pass/num/nocli", | |
368 | test_socket_fd_pass_num_nocli); | |
8330bd53 | 369 | #endif |
58dc31f1 DB |
370 | } |
371 | ||
8acefc79 | 372 | #ifdef CONFIG_LINUX |
a72f6754 MA |
373 | g_test_add_func("/util/socket/unix-abstract", |
374 | test_socket_unix_abstract); | |
4d3a329a XZ |
375 | #endif |
376 | ||
a4eb74a6 | 377 | end: |
58dc31f1 DB |
378 | return g_test_run(); |
379 | } |