Debian Codegraph @

testsuite/test-watch.c

raw
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 <unistd.h>
26
27 #include "dex-watch-private.h"
28
29 #define ASSERT_STATUS(f,status) g_assert_cmpint(status, ==, dex_future_get_status(DEX_FUTURE(f)))
30
31 static DexFuture *
32 quit_cb (DexFuture *future,
33 gpointer user_data)
34 {
35 g_main_loop_quit (user_data);
36 return NULL;
37 }
38
39 static void
40 test_watch_read_ready (void)
41 {
42 int pipefd[2];
43 DexFuture *watch;
44 GMainLoop *main_loop;
45 GError *error = NULL;
46 const GValue *value;
47 int revents;
48
49 g_assert_cmpint (pipe (pipefd), ==, 0);
50
51 /* Watch for read readiness */
52 watch = dex_watch_new (pipefd[0], G_IO_IN);
53 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
54
55 main_loop = g_main_loop_new (NULL, FALSE);
56 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
57
58 /* Write data to make read side ready */
59 g_assert_cmpint (write (pipefd[1], "test", 4), ==, 4);
60
61 g_main_loop_run (main_loop);
62
63 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
64 value = dex_future_get_value (watch, &error);
65 g_assert_no_error (error);
66 g_assert_nonnull (value);
67 g_assert_true (G_VALUE_HOLDS_INT (value));
68 revents = g_value_get_int (value);
69 g_assert_cmpint (revents & G_IO_IN, !=, 0);
70
71 close (pipefd[0]);
72 close (pipefd[1]);
73 g_main_loop_unref (main_loop);
74 dex_unref (watch);
75 }
76
77 static void
78 test_watch_write_ready (void)
79 {
80 int pipefd[2];
81 DexFuture *watch;
82 GMainLoop *main_loop;
83 GError *error = NULL;
84 const GValue *value;
85 int revents;
86
87 g_assert_cmpint (pipe (pipefd), ==, 0);
88
89 /* Watch for write readiness (pipe should be writable immediately) */
90 watch = dex_watch_new (pipefd[1], G_IO_OUT);
91 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
92
93 main_loop = g_main_loop_new (NULL, FALSE);
94 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
95
96 g_main_loop_run (main_loop);
97
98 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
99 value = dex_future_get_value (watch, &error);
100 g_assert_no_error (error);
101 g_assert_nonnull (value);
102 g_assert_true (G_VALUE_HOLDS_INT (value));
103 revents = g_value_get_int (value);
104 g_assert_cmpint (revents & G_IO_OUT, !=, 0);
105
106 close (pipefd[0]);
107 close (pipefd[1]);
108 g_main_loop_unref (main_loop);
109 dex_unref (watch);
110 }
111
112 static void
113 test_watch_write_side_closed (void)
114 {
115 int pipefd[2];
116 DexFuture *watch;
117 GMainLoop *main_loop;
118 GError *error = NULL;
119 const GValue *value;
120 int revents;
121
122 g_assert_cmpint (pipe (pipefd), ==, 0);
123
124 /* Watch for read readiness and hangup */
125 watch = dex_watch_new (pipefd[0], G_IO_IN);
126 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
127
128 /* Close write side, which should trigger HUP on read side */
129 close (pipefd[1]);
130
131 main_loop = g_main_loop_new (NULL, FALSE);
132 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
133
134 g_main_loop_run (main_loop);
135
136 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
137 value = dex_future_get_value (watch, &error);
138 g_assert_no_error (error);
139 g_assert_nonnull (value);
140 g_assert_true (G_VALUE_HOLDS_INT (value));
141 revents = g_value_get_int (value);
142 g_assert_cmpint (revents & G_IO_HUP, !=, 0);
143
144 close (pipefd[0]);
145 g_main_loop_unref (main_loop);
146 dex_unref (watch);
147 }
148
149 static void
150 test_watch_read_waiting_write_closed (void)
151 {
152 int pipefd[2];
153 DexFuture *watch;
154 GMainLoop *main_loop;
155 GError *error = NULL;
156 const GValue *value;
157 int revents;
158
159 g_assert_cmpint (pipe (pipefd), ==, 0);
160
161 /* Watch for read readiness only (not explicitly HUP) */
162 watch = dex_watch_new (pipefd[0], G_IO_IN);
163 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
164
165 main_loop = g_main_loop_new (NULL, FALSE);
166 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
167
168 /* Close write side, which should trigger HUP on read side */
169 close (pipefd[1]);
170
171 g_main_loop_run (main_loop);
172
173 /* Future should complete even though we only asked for G_IO_IN */
174 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
175 value = dex_future_get_value (watch, &error);
176 g_assert_no_error (error);
177 g_assert_nonnull (value);
178 g_assert_true (G_VALUE_HOLDS_INT (value));
179 revents = g_value_get_int (value);
180 /* When write side closes, read side gets HUP */
181 g_assert_cmpint (revents & G_IO_HUP, !=, 0);
182
183 close (pipefd[0]);
184 g_main_loop_unref (main_loop);
185 dex_unref (watch);
186 }
187
188 static void
189 test_watch_read_side_closed (void)
190 {
191 int pipefd[2];
192 DexFuture *watch;
193 GMainLoop *main_loop;
194 GError *error = NULL;
195 const GValue *value;
196 int revents;
197
198 g_assert_cmpint (pipe (pipefd), ==, 0);
199
200 /* Watch for write readiness and hangup */
201 watch = dex_watch_new (pipefd[1], G_IO_OUT | G_IO_HUP);
202 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
203
204 main_loop = g_main_loop_new (NULL, FALSE);
205 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
206
207 /* Close read side, which should trigger HUP on write side */
208 close (pipefd[0]);
209
210 g_main_loop_run (main_loop);
211
212 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
213 value = dex_future_get_value (watch, &error);
214 g_assert_no_error (error);
215 g_assert_nonnull (value);
216 g_assert_true (G_VALUE_HOLDS_INT (value));
217 revents = g_value_get_int (value);
218 /* When read side is closed, write side gets HUP or ERR */
219 g_assert_true ((revents & G_IO_HUP) != 0 || (revents & G_IO_ERR) != 0);
220
221 close (pipefd[1]);
222 g_main_loop_unref (main_loop);
223 dex_unref (watch);
224 }
225
226 static void
227 test_watch_multiple_events (void)
228 {
229 int pipefd[2];
230 DexFuture *watch;
231 GMainLoop *main_loop;
232 GError *error = NULL;
233 const GValue *value;
234 int revents;
235
236 g_assert_cmpint (pipe (pipefd), ==, 0);
237
238 /* Watch for both read and write readiness */
239 watch = dex_watch_new (pipefd[1], G_IO_OUT | G_IO_IN);
240 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_PENDING);
241
242 main_loop = g_main_loop_new (NULL, FALSE);
243 watch = dex_future_then (watch, quit_cb, main_loop, NULL);
244
245 /* Write side should be immediately writable */
246 g_main_loop_run (main_loop);
247
248 ASSERT_STATUS (watch, DEX_FUTURE_STATUS_RESOLVED);
249 value = dex_future_get_value (watch, &error);
250 g_assert_no_error (error);
251 g_assert_nonnull (value);
252 g_assert_true (G_VALUE_HOLDS_INT (value));
253 revents = g_value_get_int (value);
254 g_assert_cmpint (revents & G_IO_OUT, !=, 0);
255
256 close (pipefd[0]);
257 close (pipefd[1]);
258 g_main_loop_unref (main_loop);
259 dex_unref (watch);
260 }
261
262 int
263 main (int argc,
264 char *argv[])
265 {
266 dex_init ();
267 g_test_init (&argc, &argv, NULL);
268 g_test_add_func ("/Dex/TestSuite/Watch/read_ready", test_watch_read_ready);
269 g_test_add_func ("/Dex/TestSuite/Watch/write_ready", test_watch_write_ready);
270 g_test_add_func ("/Dex/TestSuite/Watch/write_side_closed", test_watch_write_side_closed);
271 g_test_add_func ("/Dex/TestSuite/Watch/read_waiting_write_closed", test_watch_read_waiting_write_closed);
272 g_test_add_func ("/Dex/TestSuite/Watch/read_side_closed", test_watch_read_side_closed);
273 g_test_add_func ("/Dex/TestSuite/Watch/multiple_events", test_watch_multiple_events);
274 return g_test_run ();
275 }