hw/acpi: extract acpi_add_rom_blob()
[qemu.git] / tests / test-util-sockets.c
blobfd1ced058c4238aa1e277bb1b3b031785af93a78
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;
56 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
58 g_assert(cur_mon);
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);
62 return -1;
64 return dup(mon_fd);
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(Chardev *chr, int flags) {}
77 static void test_socket_fd_pass_name_good(void)
79 SocketAddress addr;
80 int fd;
82 cur_mon = g_malloc(1); /* Fake a monitor */
83 mon_fdname = "myfd";
84 mon_fd = qemu_socket(AF_INET, SOCK_STREAM, 0);
85 g_assert_cmpint(mon_fd, >, STDERR_FILENO);
87 addr.type = SOCKET_ADDRESS_TYPE_FD;
88 addr.u.fd.str = g_strdup(mon_fdname);
90 fd = socket_connect(&addr, &error_abort);
91 g_assert_cmpint(fd, !=, -1);
92 g_assert_cmpint(fd, !=, mon_fd);
93 close(fd);
95 fd = socket_listen(&addr, &error_abort);
96 g_assert_cmpint(fd, !=, -1);
97 g_assert_cmpint(fd, !=, mon_fd);
98 close(fd);
100 g_free(addr.u.fd.str);
101 mon_fdname = NULL;
102 close(mon_fd);
103 mon_fd = -1;
104 g_free(cur_mon);
105 cur_mon = NULL;
108 static void test_socket_fd_pass_name_bad(void)
110 SocketAddress addr;
111 Error *err = NULL;
112 int fd;
114 cur_mon = g_malloc(1); /* Fake a monitor */
115 mon_fdname = "myfd";
116 mon_fd = dup(STDOUT_FILENO);
117 g_assert_cmpint(mon_fd, >, STDERR_FILENO);
119 addr.type = SOCKET_ADDRESS_TYPE_FD;
120 addr.u.fd.str = g_strdup(mon_fdname);
122 fd = socket_connect(&addr, &err);
123 g_assert_cmpint(fd, ==, -1);
124 error_free_or_abort(&err);
126 fd = socket_listen(&addr, &err);
127 g_assert_cmpint(fd, ==, -1);
128 error_free_or_abort(&err);
130 g_free(addr.u.fd.str);
131 mon_fdname = NULL;
132 close(mon_fd);
133 mon_fd = -1;
134 g_free(cur_mon);
135 cur_mon = NULL;
138 static void test_socket_fd_pass_name_nomon(void)
140 SocketAddress addr;
141 Error *err = NULL;
142 int fd;
144 g_assert(cur_mon == NULL);
146 addr.type = SOCKET_ADDRESS_TYPE_FD;
147 addr.u.fd.str = g_strdup("myfd");
149 fd = socket_connect(&addr, &err);
150 g_assert_cmpint(fd, ==, -1);
151 error_free_or_abort(&err);
153 fd = socket_listen(&addr, &err);
154 g_assert_cmpint(fd, ==, -1);
155 error_free_or_abort(&err);
157 g_free(addr.u.fd.str);
161 static void test_socket_fd_pass_num_good(void)
163 SocketAddress addr;
164 int fd, sfd;
166 g_assert(cur_mon == NULL);
167 sfd = qemu_socket(AF_INET, SOCK_STREAM, 0);
168 g_assert_cmpint(sfd, >, STDERR_FILENO);
170 addr.type = SOCKET_ADDRESS_TYPE_FD;
171 addr.u.fd.str = g_strdup_printf("%d", sfd);
173 fd = socket_connect(&addr, &error_abort);
174 g_assert_cmpint(fd, ==, sfd);
176 fd = socket_listen(&addr, &error_abort);
177 g_assert_cmpint(fd, ==, sfd);
179 g_free(addr.u.fd.str);
180 close(sfd);
183 static void test_socket_fd_pass_num_bad(void)
185 SocketAddress addr;
186 Error *err = NULL;
187 int fd, sfd;
189 g_assert(cur_mon == NULL);
190 sfd = dup(STDOUT_FILENO);
192 addr.type = SOCKET_ADDRESS_TYPE_FD;
193 addr.u.fd.str = g_strdup_printf("%d", sfd);
195 fd = socket_connect(&addr, &err);
196 g_assert_cmpint(fd, ==, -1);
197 error_free_or_abort(&err);
199 fd = socket_listen(&addr, &err);
200 g_assert_cmpint(fd, ==, -1);
201 error_free_or_abort(&err);
203 g_free(addr.u.fd.str);
204 close(sfd);
207 static void test_socket_fd_pass_num_nocli(void)
209 SocketAddress addr;
210 Error *err = NULL;
211 int fd;
213 cur_mon = g_malloc(1); /* Fake a monitor */
215 addr.type = SOCKET_ADDRESS_TYPE_FD;
216 addr.u.fd.str = g_strdup_printf("%d", STDOUT_FILENO);
218 fd = socket_connect(&addr, &err);
219 g_assert_cmpint(fd, ==, -1);
220 error_free_or_abort(&err);
222 fd = socket_listen(&addr, &err);
223 g_assert_cmpint(fd, ==, -1);
224 error_free_or_abort(&err);
226 g_free(addr.u.fd.str);
230 int main(int argc, char **argv)
232 bool has_ipv4, has_ipv6;
234 socket_init();
236 g_test_init(&argc, &argv, NULL);
238 /* We're creating actual IPv4/6 sockets, so we should
239 * check if the host running tests actually supports
240 * each protocol to avoid breaking tests on machines
241 * with either IPv4 or IPv6 disabled.
243 if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
244 return 1;
247 if (has_ipv4) {
248 g_test_add_func("/util/socket/is-socket/bad",
249 test_fd_is_socket_bad);
250 g_test_add_func("/util/socket/is-socket/good",
251 test_fd_is_socket_good);
252 g_test_add_func("/socket/fd-pass/name/good",
253 test_socket_fd_pass_name_good);
254 g_test_add_func("/socket/fd-pass/name/bad",
255 test_socket_fd_pass_name_bad);
256 g_test_add_func("/socket/fd-pass/name/nomon",
257 test_socket_fd_pass_name_nomon);
258 g_test_add_func("/socket/fd-pass/num/good",
259 test_socket_fd_pass_num_good);
260 g_test_add_func("/socket/fd-pass/num/bad",
261 test_socket_fd_pass_num_bad);
262 g_test_add_func("/socket/fd-pass/num/nocli",
263 test_socket_fd_pass_num_nocli);
266 return g_test_run();