]>
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; | |
55 | ||
56 | int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) | |
57 | { | |
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; | |
63 | } | |
64 | return dup(mon_fd); | |
65 | } | |
66 | ||
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. | |
71 | */ | |
62aa1d88 | 72 | __thread Monitor *cur_mon; |
637de4db | 73 | int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); } |
30bdb3c5 DB |
74 | void monitor_init(Chardev *chr, int flags) {} |
75 | ||
76 | ||
1723d6b1 | 77 | static void test_socket_fd_pass_name_good(void) |
30bdb3c5 DB |
78 | { |
79 | SocketAddress addr; | |
80 | int fd; | |
81 | ||
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); | |
86 | ||
87 | addr.type = SOCKET_ADDRESS_TYPE_FD; | |
88 | addr.u.fd.str = g_strdup(mon_fdname); | |
89 | ||
90 | fd = socket_connect(&addr, &error_abort); | |
91 | g_assert_cmpint(fd, !=, -1); | |
92 | g_assert_cmpint(fd, !=, mon_fd); | |
93 | close(fd); | |
94 | ||
95 | fd = socket_listen(&addr, &error_abort); | |
96 | g_assert_cmpint(fd, !=, -1); | |
97 | g_assert_cmpint(fd, !=, mon_fd); | |
98 | close(fd); | |
99 | ||
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; | |
106 | } | |
107 | ||
1723d6b1 | 108 | static void test_socket_fd_pass_name_bad(void) |
30bdb3c5 DB |
109 | { |
110 | SocketAddress addr; | |
111 | Error *err = NULL; | |
112 | int fd; | |
113 | ||
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); | |
118 | ||
119 | addr.type = SOCKET_ADDRESS_TYPE_FD; | |
120 | addr.u.fd.str = g_strdup(mon_fdname); | |
121 | ||
122 | fd = socket_connect(&addr, &err); | |
123 | g_assert_cmpint(fd, ==, -1); | |
124 | error_free_or_abort(&err); | |
125 | ||
126 | fd = socket_listen(&addr, &err); | |
127 | g_assert_cmpint(fd, ==, -1); | |
128 | error_free_or_abort(&err); | |
129 | ||
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; | |
136 | } | |
137 | ||
1723d6b1 DB |
138 | static void test_socket_fd_pass_name_nomon(void) |
139 | { | |
140 | SocketAddress addr; | |
141 | Error *err = NULL; | |
142 | int fd; | |
143 | ||
144 | g_assert(cur_mon == NULL); | |
145 | ||
146 | addr.type = SOCKET_ADDRESS_TYPE_FD; | |
147 | addr.u.fd.str = g_strdup("myfd"); | |
148 | ||
149 | fd = socket_connect(&addr, &err); | |
150 | g_assert_cmpint(fd, ==, -1); | |
151 | error_free_or_abort(&err); | |
152 | ||
153 | fd = socket_listen(&addr, &err); | |
154 | g_assert_cmpint(fd, ==, -1); | |
155 | error_free_or_abort(&err); | |
156 | ||
157 | g_free(addr.u.fd.str); | |
158 | } | |
159 | ||
160 | ||
161 | static void test_socket_fd_pass_num_good(void) | |
162 | { | |
163 | SocketAddress addr; | |
164 | int fd, sfd; | |
165 | ||
166 | g_assert(cur_mon == NULL); | |
167 | sfd = qemu_socket(AF_INET, SOCK_STREAM, 0); | |
168 | g_assert_cmpint(sfd, >, STDERR_FILENO); | |
169 | ||
170 | addr.type = SOCKET_ADDRESS_TYPE_FD; | |
171 | addr.u.fd.str = g_strdup_printf("%d", sfd); | |
172 | ||
173 | fd = socket_connect(&addr, &error_abort); | |
174 | g_assert_cmpint(fd, ==, sfd); | |
175 | ||
176 | fd = socket_listen(&addr, &error_abort); | |
177 | g_assert_cmpint(fd, ==, sfd); | |
178 | ||
179 | g_free(addr.u.fd.str); | |
180 | close(sfd); | |
181 | } | |
182 | ||
183 | static void test_socket_fd_pass_num_bad(void) | |
184 | { | |
185 | SocketAddress addr; | |
186 | Error *err = NULL; | |
187 | int fd, sfd; | |
188 | ||
189 | g_assert(cur_mon == NULL); | |
190 | sfd = dup(STDOUT_FILENO); | |
191 | ||
192 | addr.type = SOCKET_ADDRESS_TYPE_FD; | |
193 | addr.u.fd.str = g_strdup_printf("%d", sfd); | |
194 | ||
195 | fd = socket_connect(&addr, &err); | |
196 | g_assert_cmpint(fd, ==, -1); | |
197 | error_free_or_abort(&err); | |
198 | ||
199 | fd = socket_listen(&addr, &err); | |
200 | g_assert_cmpint(fd, ==, -1); | |
201 | error_free_or_abort(&err); | |
202 | ||
203 | g_free(addr.u.fd.str); | |
204 | close(sfd); | |
205 | } | |
206 | ||
207 | static void test_socket_fd_pass_num_nocli(void) | |
208 | { | |
209 | SocketAddress addr; | |
210 | Error *err = NULL; | |
211 | int fd; | |
212 | ||
213 | cur_mon = g_malloc(1); /* Fake a monitor */ | |
214 | ||
215 | addr.type = SOCKET_ADDRESS_TYPE_FD; | |
216 | addr.u.fd.str = g_strdup_printf("%d", STDOUT_FILENO); | |
217 | ||
218 | fd = socket_connect(&addr, &err); | |
219 | g_assert_cmpint(fd, ==, -1); | |
220 | error_free_or_abort(&err); | |
221 | ||
222 | fd = socket_listen(&addr, &err); | |
223 | g_assert_cmpint(fd, ==, -1); | |
224 | error_free_or_abort(&err); | |
225 | ||
226 | g_free(addr.u.fd.str); | |
227 | } | |
228 | ||
229 | ||
58dc31f1 DB |
230 | int main(int argc, char **argv) |
231 | { | |
232 | bool has_ipv4, has_ipv6; | |
233 | ||
234 | socket_init(); | |
235 | ||
236 | g_test_init(&argc, &argv, NULL); | |
237 | ||
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. | |
242 | */ | |
243 | if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) { | |
244 | return 1; | |
245 | } | |
246 | ||
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); | |
1723d6b1 DB |
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); | |
58dc31f1 DB |
264 | } |
265 | ||
266 | return g_test_run(); | |
267 | } |