Debian Codegraph @

subprojects/libglnx/tests/test-libglnx-testing.c

raw
1 /*
2 * Copyright 2022 Simon McVittie
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but 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 Lesser General
16 * Public License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
18 */
19
20 #include "libglnx-config.h"
21 #include "libglnx.h"
22
23 #include <glib.h>
24 #include <glib/gstdio.h>
25
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30
31 #if GLIB_CHECK_VERSION (2, 38, 0)
32 #define GTEST_TAP_OR_VERBOSE "--tap"
33 #else
34 #define GTEST_TAP_OR_VERBOSE "--verbose"
35 #endif
36
37 static const char *null = NULL;
38 static const char *nonnull = "not null";
39
40 static void
41 test_assertions (void)
42 {
43 const char *other_nonnull = "not null";
44 g_autoptr(GVariant) va = g_variant_ref_sink (g_variant_new ("i", 42));
45 g_autoptr(GVariant) vb = g_variant_ref_sink (g_variant_new ("i", 42));
46 const char * const strv1[] = {"one", "two", NULL};
47 const char * const strv2[] = {"one", "two", NULL};
48 GStatBuf statbuf;
49
50 g_assert_true (null == NULL);
51 g_assert_false (null != NULL);
52 g_assert_null (null);
53 g_assert_nonnull (nonnull);
54 g_assert_cmpmem (null, 0, null, 0);
55 g_assert_cmpmem (nonnull, strlen (nonnull), other_nonnull, strlen (other_nonnull));
56 g_assert_cmpfloat_with_epsilon (1.0, 1.00001, 0.01);
57 g_assert_cmpvariant (va, vb);
58 g_assert_no_errno (g_stat ("/", &statbuf));
59 g_assert_cmpstrv (NULL, NULL);
60 g_assert_cmpstrv (&null, &null);
61 g_assert_cmpstrv (strv1, strv2);
62 }
63
64 static void
65 test_assertion_failures (void)
66 {
67 static const char * const assertion_failures[] =
68 {
69 "true",
70 "false",
71 "nonnull",
72 "null",
73 "mem_null_nonnull",
74 "mem_nonnull_null",
75 "mem_len",
76 "mem_cmp",
77 "cmpfloat_with_epsilon",
78 "cmpvariant",
79 "errno",
80 "cmpstrv_null_nonnull",
81 "cmpstrv_nonnull_null",
82 "cmpstrv_len",
83 "cmpstrv_cmp",
84 };
85 g_autoptr(GError) error = NULL;
86 g_autofree char *self = NULL;
87 g_autofree char *dir = NULL;
88 g_autofree char *exe = NULL;
89 gsize i;
90
91 self = glnx_readlinkat_malloc (-1, "/proc/self/exe", NULL, &error);
92 g_assert_no_error (error);
93
94 dir = g_path_get_dirname (self);
95 exe = g_build_filename (dir, "testing-helper", NULL);
96
97 for (i = 0; i < G_N_ELEMENTS (assertion_failures); i++)
98 {
99 g_autofree char *out = NULL;
100 g_autofree char *err = NULL;
101 g_autofree char *name = g_strdup_printf ("/assertion-failure/%s", assertion_failures[i]);
102 int wait_status = -1;
103 const char *argv[] = { NULL, "assertion-failures", "-p", NULL, NULL, NULL };
104 char *line;
105 char *saveptr = NULL;
106
107 argv[0] = exe;
108 argv[3] = name;
109 argv[4] = GTEST_TAP_OR_VERBOSE;
110 g_test_message ("%s assertion-failures -p %s %s...", exe, name, GTEST_TAP_OR_VERBOSE);
111
112 g_spawn_sync (NULL, /* cwd */
113 (char **) argv,
114 NULL, /* envp */
115 G_SPAWN_DEFAULT,
116 NULL, /* child setup */
117 NULL, /* user data */
118 &out,
119 &err,
120 &wait_status,
121 &error);
122 g_assert_no_error (error);
123
124 g_assert_nonnull (out);
125 g_assert_nonnull (err);
126
127 for (line = strtok_r (out, "\n", &saveptr);
128 line != NULL;
129 line = strtok_r (NULL, "\n", &saveptr))
130 g_test_message ("stdout: %s", line);
131
132 saveptr = NULL;
133
134 for (line = strtok_r (err, "\n", &saveptr);
135 line != NULL;
136 line = strtok_r (NULL, "\n", &saveptr))
137 g_test_message ("stderr: %s", line);
138
139 g_test_message ("wait status: 0x%x", wait_status);
140
141 /* It exited with a nonzero status that was not exit status 77 */
142 G_STATIC_ASSERT (WIFEXITED (0));
143 G_STATIC_ASSERT (WEXITSTATUS (0) == 0);
144 g_assert_cmphex (wait_status, !=, 0);
145 G_STATIC_ASSERT (WIFEXITED (77 << 8));
146 G_STATIC_ASSERT (WEXITSTATUS (77 << 8) == 77);
147 g_assert_cmphex (wait_status, !=, (77 << 8));
148 }
149 }
150
151 static void
152 test_failures (void)
153 {
154 static const char * const failures[] =
155 {
156 "fail",
157 "fail-printf",
158 };
159 g_autoptr(GError) error = NULL;
160 g_autofree char *self = NULL;
161 g_autofree char *dir = NULL;
162 g_autofree char *exe = NULL;
163 gsize i;
164
165 self = glnx_readlinkat_malloc (-1, "/proc/self/exe", NULL, &error);
166 g_assert_no_error (error);
167
168 dir = g_path_get_dirname (self);
169 exe = g_build_filename (dir, "testing-helper", NULL);
170
171 for (i = 0; i < G_N_ELEMENTS (failures); i++)
172 {
173 g_autofree char *out = NULL;
174 g_autofree char *err = NULL;
175 int wait_status = -1;
176 const char *argv[] = { NULL, NULL, NULL, NULL };
177 char *line;
178 char *saveptr = NULL;
179
180 argv[0] = exe;
181 argv[1] = failures[i];
182 argv[2] = GTEST_TAP_OR_VERBOSE;
183 g_test_message ("%s %s %s...", exe, failures[i], GTEST_TAP_OR_VERBOSE);
184
185 g_spawn_sync (NULL, /* cwd */
186 (char **) argv,
187 NULL, /* envp */
188 G_SPAWN_DEFAULT,
189 NULL, /* child setup */
190 NULL, /* user data */
191 &out,
192 &err,
193 &wait_status,
194 &error);
195 g_assert_no_error (error);
196
197 for (line = strtok_r (out, "\n", &saveptr);
198 line != NULL;
199 line = strtok_r (NULL, "\n", &saveptr))
200 g_test_message ("stdout: %s", line);
201
202 saveptr = NULL;
203
204 for (line = strtok_r (err, "\n", &saveptr);
205 line != NULL;
206 line = strtok_r (NULL, "\n", &saveptr))
207 g_test_message ("stderr: %s", line);
208
209 g_test_message ("wait status: 0x%x", wait_status);
210
211 G_STATIC_ASSERT (WIFEXITED (0));
212 G_STATIC_ASSERT (WEXITSTATUS (0) == 0);
213 G_STATIC_ASSERT (WIFEXITED (77 << 8));
214 G_STATIC_ASSERT (WEXITSTATUS (77 << 8) == 77);
215
216 g_assert_cmphex (wait_status, !=, 0);
217 g_assert_cmphex (wait_status, !=, (77 << 8));
218 }
219 }
220
221 static void
222 test_skips (void)
223 {
224 static const char * const skips[] =
225 {
226 "skip",
227 "skip-printf",
228 "incomplete",
229 "incomplete-printf",
230 };
231 g_autoptr(GError) error = NULL;
232 g_autofree char *self = NULL;
233 g_autofree char *dir = NULL;
234 g_autofree char *exe = NULL;
235 gsize i;
236
237 self = glnx_readlinkat_malloc (-1, "/proc/self/exe", NULL, &error);
238 g_assert_no_error (error);
239
240 dir = g_path_get_dirname (self);
241 exe = g_build_filename (dir, "testing-helper", NULL);
242
243 for (i = 0; i < G_N_ELEMENTS (skips); i++)
244 {
245 g_autofree char *out = NULL;
246 g_autofree char *err = NULL;
247 int wait_status = -1;
248 const char *argv[] = { NULL, NULL, NULL, NULL };
249 char *line;
250 char *saveptr = NULL;
251
252 argv[0] = exe;
253 argv[1] = skips[i];
254 argv[2] = GTEST_TAP_OR_VERBOSE;
255 g_test_message ("%s %s %s...", exe, skips[i], GTEST_TAP_OR_VERBOSE);
256
257 g_spawn_sync (NULL, /* cwd */
258 (char **) argv,
259 NULL, /* envp */
260 G_SPAWN_DEFAULT,
261 NULL, /* child setup */
262 NULL, /* user data */
263 &out,
264 &err,
265 &wait_status,
266 &error);
267 g_assert_no_error (error);
268
269 for (line = strtok_r (out, "\n", &saveptr);
270 line != NULL;
271 line = strtok_r (NULL, "\n", &saveptr))
272 g_test_message ("stdout: %s", line);
273
274 saveptr = NULL;
275
276 for (line = strtok_r (err, "\n", &saveptr);
277 line != NULL;
278 line = strtok_r (NULL, "\n", &saveptr))
279 g_test_message ("stderr: %s", line);
280
281 g_test_message ("wait status: 0x%x", wait_status);
282
283 G_STATIC_ASSERT (WIFEXITED (0));
284 G_STATIC_ASSERT (WEXITSTATUS (0) == 0);
285 G_STATIC_ASSERT (WIFEXITED (77 << 8));
286 G_STATIC_ASSERT (WEXITSTATUS (77 << 8) == 77);
287
288 /* Ideally the exit status is 77, but it might be 0 with older GLib */
289 if (wait_status != 0)
290 g_assert_cmphex (wait_status, ==, (77 << 8));
291 }
292 }
293
294 static void
295 test_successes (void)
296 {
297 static const char * const successes[] =
298 {
299 "messages",
300 "pass",
301 "summary",
302 };
303 g_autoptr(GError) error = NULL;
304 g_autofree char *self = NULL;
305 g_autofree char *dir = NULL;
306 g_autofree char *exe = NULL;
307 gsize i;
308
309 self = glnx_readlinkat_malloc (-1, "/proc/self/exe", NULL, &error);
310 g_assert_no_error (error);
311
312 dir = g_path_get_dirname (self);
313 exe = g_build_filename (dir, "testing-helper", NULL);
314
315 for (i = 0; i < G_N_ELEMENTS (successes); i++)
316 {
317 g_autofree char *out = NULL;
318 g_autofree char *err = NULL;
319 int wait_status = -1;
320 const char *argv[] = { NULL, NULL, NULL, NULL };
321 char *line;
322 char *saveptr = NULL;
323
324 argv[0] = exe;
325 argv[1] = successes[i];
326 argv[2] = GTEST_TAP_OR_VERBOSE;
327 g_test_message ("%s %s %s...", exe, successes[i], GTEST_TAP_OR_VERBOSE);
328
329 g_spawn_sync (NULL, /* cwd */
330 (char **) argv,
331 NULL, /* envp */
332 G_SPAWN_DEFAULT,
333 NULL, /* child setup */
334 NULL, /* user data */
335 &out,
336 &err,
337 &wait_status,
338 &error);
339 g_assert_no_error (error);
340
341 for (line = strtok_r (out, "\n", &saveptr);
342 line != NULL;
343 line = strtok_r (NULL, "\n", &saveptr))
344 g_test_message ("stdout: %s", line);
345
346 saveptr = NULL;
347
348 for (line = strtok_r (err, "\n", &saveptr);
349 line != NULL;
350 line = strtok_r (NULL, "\n", &saveptr))
351 g_test_message ("stderr: %s", line);
352
353 g_test_message ("wait status: 0x%x", wait_status);
354
355 G_STATIC_ASSERT (WIFEXITED (0));
356 G_STATIC_ASSERT (WEXITSTATUS (0) == 0);
357 g_assert_cmphex (wait_status, ==, 0);
358 }
359 }
360
361 int
362 main (int argc, char **argv)
363 {
364 g_test_init (&argc, &argv, NULL);
365 g_test_add_func ("/assertions", test_assertions);
366 g_test_add_func ("/assertion_failures", test_assertion_failures);
367 g_test_add_func ("/failures", test_failures);
368 g_test_add_func ("/skips", test_skips);
369 g_test_add_func ("/successes", test_successes);
370 return g_test_run();
371 }