Debian Codegraph @

testsuite/test-watch.c

raw symbols SCIP
1 /* test-watch.c
2 *
3 * Copyright 2025 Christian Hergert
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <libdex.h>
24 #include <gio/gio.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27
28 #include "dex-watch-private.h"
29
30 #define ASSERT_STATUS(f,status) g_assert_cmpint(status, ==, dex_future_get_status(DEX_FUTURE(f)))
31
32 static DexFuture *
33 quit_cb (DexFuture *future,
34 gpointer user_data)
35 {
36 g_main_loop_quit (user_data);
37 return NULL;
38 }
39
40 static void
41 test_watch_read_ready (void)
42 {
43 int pipefd[2];
44 DexFuture *watch;
45 GMainLoop *main_loop;
46 GError *error = NULL;
47 const GValue *value;
48 int revents;
49
50 g_assert_cmpint (pipe (pipefd), ==, 0);
51
52 /* Watch for read readiness */
53 watch = dex_watch_new (pipefd[0], G_IO_IN);
54 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
55
56 main_loop = g_main_loop_new (NULL, FALSE);
57 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
58
59 /* Write data to make read side ready */
60 g_assert_cmpint (write (pipefd[1], "test", 4), ==, 4);
61
62 g_main_loop_run (main_loop);
63
64 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
65 value = dex_future_get_value (watch, &error);
66 g_assert_no_error (error);
67 g_assert_nonnull (value);
68 g_assert_true (G_VALUE_HOLDS_INT (value));
69 revents = g_value_get_int (value);
70 g_assert_cmpint (revents & G_IO_IN, !=, 0);
71
72 close (pipefd[0]);
73 close (pipefd[1]);
74 g_main_loop_unref (main_loop);
75 dex_unref (watch);
76 }
77
78 static void
79 test_watch_write_ready (void)
80 {
81 int pipefd[2];
82 DexFuture *watch;
83 GMainLoop *main_loop;
84 GError *error = NULL;
85 const GValue *value;
86 int revents;
87
88 g_assert_cmpint (pipe (pipefd), ==, 0);
89
90 /* Watch for write readiness (pipe should be writable immediately) */
91 watch = dex_watch_new (pipefd[1], G_IO_OUT);
92 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
93
94 main_loop = g_main_loop_new (NULL, FALSE);
95 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
96
97 g_main_loop_run (main_loop);
98
99 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
100 value = dex_future_get_value (watch, &error);
101 g_assert_no_error (error);
102 g_assert_nonnull (value);
103 g_assert_true (G_VALUE_HOLDS_INT (value));
104 revents = g_value_get_int (value);
105 g_assert_cmpint (revents & G_IO_OUT, !=, 0);
106
107 close (pipefd[0]);
108 close (pipefd[1]);
109 g_main_loop_unref (main_loop);
110 dex_unref (watch);
111 }
112
113 static void
114 test_watch_write_side_closed (void)
115 {
116 int pipefd[2];
117 DexFuture *watch;
118 GMainLoop *main_loop;
119 GError *error = NULL;
120 const GValue *value;
121 int revents;
122
123 g_assert_cmpint (pipe (pipefd), ==, 0);
124
125 /* Watch for read readiness and hangup */
126 watch = dex_watch_new (pipefd[0], G_IO_IN);
127 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
128
129 /* Close write side, which should trigger HUP on read side */
130 close (pipefd[1]);
131
132 main_loop = g_main_loop_new (NULL, FALSE);
133 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
134
135 g_main_loop_run (main_loop);
136
137 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
138 value = dex_future_get_value (watch, &error);
139 g_assert_no_error (error);
140 g_assert_nonnull (value);
141 g_assert_true (G_VALUE_HOLDS_INT (value));
142 revents = g_value_get_int (value);
143 g_assert_cmpint (revents & G_IO_HUP, !=, 0);
144
145 close (pipefd[0]);
146 g_main_loop_unref (main_loop);
147 dex_unref (watch);
148 }
149
150 static void
151 test_watch_read_waiting_write_closed (void)
152 {
153 int pipefd[2];
154 DexFuture *watch;
155 GMainLoop *main_loop;
156 GError *error = NULL;
157 const GValue *value;
158 int revents;
159
160 g_assert_cmpint (pipe (pipefd), ==, 0);
161
162 /* Watch for read readiness only (not explicitly HUP) */
163 watch = dex_watch_new (pipefd[0], G_IO_IN);
164 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
165
166 main_loop = g_main_loop_new (NULL, FALSE);
167 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
168
169 /* Close write side, which should trigger HUP on read side */
170 close (pipefd[1]);
171
172 g_main_loop_run (main_loop);
173
174 /* Future should complete even though we only asked for G_IO_IN */
175 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
176 value = dex_future_get_value (watch, &error);
177 g_assert_no_error (error);
178 g_assert_nonnull (value);
179 g_assert_true (G_VALUE_HOLDS_INT (value));
180 revents = g_value_get_int (value);
181 /* When write side closes, read side gets HUP */
182 g_assert_cmpint (revents & G_IO_HUP, !=, 0);
183
184 close (pipefd[0]);
185 g_main_loop_unref (main_loop);
186 dex_unref (watch);
187 }
188
189 static void
190 test_watch_read_side_closed (void)
191 {
192 int pipefd[2];
193 DexFuture *watch;
194 GMainLoop *main_loop;
195 GError *error = NULL;
196 const GValue *value;
197 int revents;
198
199 g_assert_cmpint (pipe (pipefd), ==, 0);
200
201 /* Watch for write readiness and hangup */
202 watch = dex_watch_new (pipefd[1], G_IO_OUT | G_IO_HUP);
203 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
204
205 main_loop = g_main_loop_new (NULL, FALSE);
206 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
207
208 /* Close read side, which should trigger HUP on write side */
209 close (pipefd[0]);
210
211 g_main_loop_run (main_loop);
212
213 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
214 value = dex_future_get_value (watch, &error);
215 g_assert_no_error (error);
216 g_assert_nonnull (value);
217 g_assert_true (G_VALUE_HOLDS_INT (value));
218 revents = g_value_get_int (value);
219 /* When read side is closed, write side gets HUP or ERR */
220 g_assert_true ((revents & G_IO_HUP) != 0 || (revents & G_IO_ERR) != 0);
221
222 close (pipefd[1]);
223 g_main_loop_unref (main_loop);
224 dex_unref (watch);
225 }
226
227 static void
228 test_watch_multiple_events (void)
229 {
230 int pipefd[2];
231 DexFuture *watch;
232 GMainLoop *main_loop;
233 GError *error = NULL;
234 const GValue *value;
235 int revents;
236
237 g_assert_cmpint (pipe (pipefd), ==, 0);
238
239 /* Watch for both read and write readiness */
240 watch = dex_watch_new (pipefd[1], G_IO_OUT | G_IO_IN);
241 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
242
243 main_loop = g_main_loop_new (NULL, FALSE);
244 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
245
246 /* Write side should be immediately writable */
247 g_main_loop_run (main_loop);
248
249 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
250 value = dex_future_get_value (watch, &error);
251 g_assert_no_error (error);
252 g_assert_nonnull (value);
253 g_assert_true (G_VALUE_HOLDS_INT (value));
254 revents = g_value_get_int (value);
255 g_assert_cmpint (revents & G_IO_OUT, !=, 0);
256
257 close (pipefd[0]);
258 close (pipefd[1]);
259 g_main_loop_unref (main_loop);
260 dex_unref (watch);
261 }
262
263 static void
264 test_socket_wait_read_ready (void)
265 {
266 GError * error = NULL;
267 GSocket * read_socket = NULL;
268 GSocket * write_socket = NULL;
269 int socket_fds[2];
270 DexFuture *wait;
271 GMainLoop *main_loop;
272 const GValue *value;
273 GIOCondition condition;
274
275 g_assert_cmpint (socketpair (AF_UNIX,
276 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
277 0,
278 socket_fds), ==, 0);
279
280 read_socket = g_socket_new_from_fd (socket_fds[0], &error);
281 g_assert_no_error (error);
282 g_assert_nonnull (read_socket);
283
284 write_socket = g_socket_new_from_fd (socket_fds[1], &error);
285 g_assert_no_error (error);
286 g_assert_nonnull (write_socket);
287
288 wait = dex_socket_wait (read_socket, G_IO_IN);
289 ASSERT_STATUS (wait, DEX_FUTURE_STATUS_PENDING);
290
291 main_loop = g_main_loop_new (NULL, FALSE);
292 wait = dex_future_then (wait, quit_cb, main_loop, NULL);
293
294 g_assert_cmpint (g_socket_send (write_socket, "x", 1, NULL, &error), ==, 1);
295 g_assert_no_error (error);
296
297 g_main_loop_run (main_loop);
298
299 ASSERT_STATUS (wait, DEX_FUTURE_STATUS_RESOLVED);
300 value = dex_future_get_value (wait, &error);
301 g_assert_no_error (error);
302 g_assert_nonnull (value);
303 g_assert_true (G_VALUE_HOLDS (value, G_TYPE_IO_CONDITION));
304
305 condition = g_value_get_flags (value);
306 g_assert_cmpint (condition & G_IO_IN, !=, 0);
307
308 g_clear_object (&read_socket);
309 g_clear_object (&write_socket);
310 g_main_loop_unref (main_loop);
311 dex_unref (wait);
312 }
313
314 int
315 main (int argc,
316 char *argv[])
317 {
318 dex_init ();
319 g_test_init (&argc, &argv, NULL);
320 g_test_add_func ("/Dex/TestSuite/Watch/read_ready", test_watch_read_ready);
321 g_test_add_func ("/Dex/TestSuite/Watch/write_ready", test_watch_write_ready);
322 g_test_add_func ("/Dex/TestSuite/Watch/write_side_closed", test_watch_write_side_closed);
323 g_test_add_func ("/Dex/TestSuite/Watch/read_waiting_write_closed", test_watch_read_waiting_write_closed);
324 g_test_add_func ("/Dex/TestSuite/Watch/read_side_closed", test_watch_read_side_closed);
325 g_test_add_func ("/Dex/TestSuite/Watch/multiple_events", test_watch_multiple_events);
326 g_test_add_func ("/Dex/TestSuite/Socket/wait_read_ready", test_socket_wait_read_ready);
327 return g_test_run ();
328 }