/src/glib/glib/gtestutils.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GLib testing utilities |
2 | | * Copyright (C) 2007 Imendio AB |
3 | | * Authors: Tim Janik, Sven Herzberg |
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 Public |
16 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include "gtestutils.h" |
22 | | #include "gfileutils.h" |
23 | | |
24 | | #include <sys/types.h> |
25 | | #ifdef G_OS_UNIX |
26 | | #include <sys/wait.h> |
27 | | #include <sys/time.h> |
28 | | #include <fcntl.h> |
29 | | #include <unistd.h> |
30 | | #endif |
31 | | #include <string.h> |
32 | | #include <stdlib.h> |
33 | | #include <stdio.h> |
34 | | #ifdef HAVE_SYS_RESOURCE_H |
35 | | #include <sys/resource.h> |
36 | | #endif |
37 | | #ifdef G_OS_WIN32 |
38 | | #include <io.h> |
39 | | #include <windows.h> |
40 | | #endif |
41 | | #include <errno.h> |
42 | | #include <signal.h> |
43 | | #ifdef HAVE_SYS_SELECT_H |
44 | | #include <sys/select.h> |
45 | | #endif /* HAVE_SYS_SELECT_H */ |
46 | | #include <glib/gstdio.h> |
47 | | |
48 | | #include "gmain.h" |
49 | | #include "gpattern.h" |
50 | | #include "grand.h" |
51 | | #include "gstrfuncs.h" |
52 | | #include "gtimer.h" |
53 | | #include "gslice.h" |
54 | | #include "gspawn.h" |
55 | | #include "glib-private.h" |
56 | | #include "gutilsprivate.h" |
57 | | |
58 | | |
59 | | /** |
60 | | * SECTION:testing |
61 | | * @title: Testing |
62 | | * @short_description: a test framework |
63 | | * |
64 | | * GLib provides a framework for writing and maintaining unit tests |
65 | | * in parallel to the code they are testing. The API is designed according |
66 | | * to established concepts found in the other test frameworks (JUnit, NUnit, |
67 | | * RUnit), which in turn is based on smalltalk unit testing concepts. |
68 | | * |
69 | | * - Test case: Tests (test methods) are grouped together with their |
70 | | * fixture into test cases. |
71 | | * |
72 | | * - Fixture: A test fixture consists of fixture data and setup and |
73 | | * teardown methods to establish the environment for the test |
74 | | * functions. We use fresh fixtures, i.e. fixtures are newly set |
75 | | * up and torn down around each test invocation to avoid dependencies |
76 | | * between tests. |
77 | | * |
78 | | * - Test suite: Test cases can be grouped into test suites, to allow |
79 | | * subsets of the available tests to be run. Test suites can be |
80 | | * grouped into other test suites as well. |
81 | | * |
82 | | * The API is designed to handle creation and registration of test suites |
83 | | * and test cases implicitly. A simple call like |
84 | | * |[<!-- language="C" --> |
85 | | * g_test_add_func ("/misc/assertions", test_assertions); |
86 | | * ]| |
87 | | * creates a test suite called "misc" with a single test case named |
88 | | * "assertions", which consists of running the test_assertions function. |
89 | | * |
90 | | * In addition to the traditional g_assert_true(), the test framework provides |
91 | | * an extended set of assertions for comparisons: g_assert_cmpfloat(), |
92 | | * g_assert_cmpfloat_with_epsilon(), g_assert_cmpint(), g_assert_cmpuint(), |
93 | | * g_assert_cmphex(), g_assert_cmpstr(), g_assert_cmpmem() and |
94 | | * g_assert_cmpvariant(). The |
95 | | * advantage of these variants over plain g_assert_true() is that the assertion |
96 | | * messages can be more elaborate, and include the values of the compared |
97 | | * entities. |
98 | | * |
99 | | * Note that g_assert() should not be used in unit tests, since it is a no-op |
100 | | * when compiling with `G_DISABLE_ASSERT`. Use g_assert() in production code, |
101 | | * and g_assert_true() in unit tests. |
102 | | * |
103 | | * A full example of creating a test suite with two tests using fixtures: |
104 | | * |[<!-- language="C" --> |
105 | | * #include <glib.h> |
106 | | * #include <locale.h> |
107 | | * |
108 | | * typedef struct { |
109 | | * MyObject *obj; |
110 | | * OtherObject *helper; |
111 | | * } MyObjectFixture; |
112 | | * |
113 | | * static void |
114 | | * my_object_fixture_set_up (MyObjectFixture *fixture, |
115 | | * gconstpointer user_data) |
116 | | * { |
117 | | * fixture->obj = my_object_new (); |
118 | | * my_object_set_prop1 (fixture->obj, "some-value"); |
119 | | * my_object_do_some_complex_setup (fixture->obj, user_data); |
120 | | * |
121 | | * fixture->helper = other_object_new (); |
122 | | * } |
123 | | * |
124 | | * static void |
125 | | * my_object_fixture_tear_down (MyObjectFixture *fixture, |
126 | | * gconstpointer user_data) |
127 | | * { |
128 | | * g_clear_object (&fixture->helper); |
129 | | * g_clear_object (&fixture->obj); |
130 | | * } |
131 | | * |
132 | | * static void |
133 | | * test_my_object_test1 (MyObjectFixture *fixture, |
134 | | * gconstpointer user_data) |
135 | | * { |
136 | | * g_assert_cmpstr (my_object_get_property (fixture->obj), ==, "initial-value"); |
137 | | * } |
138 | | * |
139 | | * static void |
140 | | * test_my_object_test2 (MyObjectFixture *fixture, |
141 | | * gconstpointer user_data) |
142 | | * { |
143 | | * my_object_do_some_work_using_helper (fixture->obj, fixture->helper); |
144 | | * g_assert_cmpstr (my_object_get_property (fixture->obj), ==, "updated-value"); |
145 | | * } |
146 | | * |
147 | | * int |
148 | | * main (int argc, char *argv[]) |
149 | | * { |
150 | | * setlocale (LC_ALL, ""); |
151 | | * |
152 | | * g_test_init (&argc, &argv, NULL); |
153 | | * |
154 | | * // Define the tests. |
155 | | * g_test_add ("/my-object/test1", MyObjectFixture, "some-user-data", |
156 | | * my_object_fixture_set_up, test_my_object_test1, |
157 | | * my_object_fixture_tear_down); |
158 | | * g_test_add ("/my-object/test2", MyObjectFixture, "some-user-data", |
159 | | * my_object_fixture_set_up, test_my_object_test2, |
160 | | * my_object_fixture_tear_down); |
161 | | * |
162 | | * return g_test_run (); |
163 | | * } |
164 | | * ]| |
165 | | * |
166 | | * ### Integrating GTest in your project |
167 | | * |
168 | | * If you are using the [Meson](http://mesonbuild.com) build system, you will |
169 | | * typically use the provided `test()` primitive to call the test binaries, |
170 | | * e.g.: |
171 | | * |
172 | | * |[<!-- language="plain" --> |
173 | | * test( |
174 | | * 'foo', |
175 | | * executable('foo', 'foo.c', dependencies: deps), |
176 | | * env: [ |
177 | | * 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()), |
178 | | * 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), |
179 | | * ], |
180 | | * ) |
181 | | * |
182 | | * test( |
183 | | * 'bar', |
184 | | * executable('bar', 'bar.c', dependencies: deps), |
185 | | * env: [ |
186 | | * 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()), |
187 | | * 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), |
188 | | * ], |
189 | | * ) |
190 | | * ]| |
191 | | * |
192 | | * If you are using Autotools, you're strongly encouraged to use the Automake |
193 | | * [TAP](https://testanything.org/) harness; GLib provides template files for |
194 | | * easily integrating with it: |
195 | | * |
196 | | * - [glib-tap.mk](https://gitlab.gnome.org/GNOME/glib/blob/glib-2-58/glib-tap.mk) |
197 | | * - [tap-test](https://gitlab.gnome.org/GNOME/glib/blob/glib-2-58/tap-test) |
198 | | * - [tap-driver.sh](https://gitlab.gnome.org/GNOME/glib/blob/glib-2-58/tap-driver.sh) |
199 | | * |
200 | | * You can copy these files in your own project's root directory, and then |
201 | | * set up your `Makefile.am` file to reference them, for instance: |
202 | | * |
203 | | * |[<!-- language="plain" --> |
204 | | * include $(top_srcdir)/glib-tap.mk |
205 | | * |
206 | | * # test binaries |
207 | | * test_programs = \ |
208 | | * foo \ |
209 | | * bar |
210 | | * |
211 | | * # data distributed in the tarball |
212 | | * dist_test_data = \ |
213 | | * foo.data.txt \ |
214 | | * bar.data.txt |
215 | | * |
216 | | * # data not distributed in the tarball |
217 | | * test_data = \ |
218 | | * blah.data.txt |
219 | | * ]| |
220 | | * |
221 | | * Make sure to distribute the TAP files, using something like the following |
222 | | * in your top-level `Makefile.am`: |
223 | | * |
224 | | * |[<!-- language="plain" --> |
225 | | * EXTRA_DIST += \ |
226 | | * tap-driver.sh \ |
227 | | * tap-test |
228 | | * ]| |
229 | | * |
230 | | * `glib-tap.mk` will be distributed implicitly due to being included in a |
231 | | * `Makefile.am`. All three files should be added to version control. |
232 | | * |
233 | | * If you don't have access to the Autotools TAP harness, you can use the |
234 | | * [gtester][gtester] and [gtester-report][gtester-report] tools, and use |
235 | | * the [glib.mk](https://gitlab.gnome.org/GNOME/glib/blob/glib-2-58/glib.mk) |
236 | | * Automake template provided by GLib. Note, however, that since GLib 2.62, |
237 | | * [gtester][gtester] and [gtester-report][gtester-report] have been deprecated |
238 | | * in favour of using TAP. The `--tap` argument to tests is enabled by default |
239 | | * as of GLib 2.62. |
240 | | */ |
241 | | |
242 | | /** |
243 | | * g_test_initialized: |
244 | | * |
245 | | * Returns %TRUE if g_test_init() has been called. |
246 | | * |
247 | | * Returns: %TRUE if g_test_init() has been called. |
248 | | * |
249 | | * Since: 2.36 |
250 | | */ |
251 | | |
252 | | /** |
253 | | * g_test_quick: |
254 | | * |
255 | | * Returns %TRUE if tests are run in quick mode. |
256 | | * Exactly one of g_test_quick() and g_test_slow() is active in any run; |
257 | | * there is no "medium speed". |
258 | | * |
259 | | * By default, tests are run in quick mode. In tests that use |
260 | | * g_test_init(), the options `-m quick`, `-m slow` and `-m thorough` |
261 | | * can be used to change this. |
262 | | * |
263 | | * Returns: %TRUE if in quick mode |
264 | | */ |
265 | | |
266 | | /** |
267 | | * g_test_slow: |
268 | | * |
269 | | * Returns %TRUE if tests are run in slow mode. |
270 | | * Exactly one of g_test_quick() and g_test_slow() is active in any run; |
271 | | * there is no "medium speed". |
272 | | * |
273 | | * By default, tests are run in quick mode. In tests that use |
274 | | * g_test_init(), the options `-m quick`, `-m slow` and `-m thorough` |
275 | | * can be used to change this. |
276 | | * |
277 | | * Returns: the opposite of g_test_quick() |
278 | | */ |
279 | | |
280 | | /** |
281 | | * g_test_thorough: |
282 | | * |
283 | | * Returns %TRUE if tests are run in thorough mode, equivalent to |
284 | | * g_test_slow(). |
285 | | * |
286 | | * By default, tests are run in quick mode. In tests that use |
287 | | * g_test_init(), the options `-m quick`, `-m slow` and `-m thorough` |
288 | | * can be used to change this. |
289 | | * |
290 | | * Returns: the same thing as g_test_slow() |
291 | | */ |
292 | | |
293 | | /** |
294 | | * g_test_perf: |
295 | | * |
296 | | * Returns %TRUE if tests are run in performance mode. |
297 | | * |
298 | | * By default, tests are run in quick mode. In tests that use |
299 | | * g_test_init(), the option `-m perf` enables performance tests, while |
300 | | * `-m quick` disables them. |
301 | | * |
302 | | * Returns: %TRUE if in performance mode |
303 | | */ |
304 | | |
305 | | /** |
306 | | * g_test_undefined: |
307 | | * |
308 | | * Returns %TRUE if tests may provoke assertions and other formally-undefined |
309 | | * behaviour, to verify that appropriate warnings are given. It might, in some |
310 | | * cases, be useful to turn this off with if running tests under valgrind; |
311 | | * in tests that use g_test_init(), the option `-m no-undefined` disables |
312 | | * those tests, while `-m undefined` explicitly enables them (normally |
313 | | * the default behaviour). |
314 | | * |
315 | | * Since GLib 2.68, if GLib was compiled with gcc or clang and |
316 | | * [AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) |
317 | | * is enabled, the default changes to not exercising undefined behaviour. |
318 | | * |
319 | | * Returns: %TRUE if tests may provoke programming errors |
320 | | */ |
321 | | |
322 | | /** |
323 | | * g_test_verbose: |
324 | | * |
325 | | * Returns %TRUE if tests are run in verbose mode. |
326 | | * In tests that use g_test_init(), the option `--verbose` enables this, |
327 | | * while `-q` or `--quiet` disables it. |
328 | | * The default is neither g_test_verbose() nor g_test_quiet(). |
329 | | * |
330 | | * Returns: %TRUE if in verbose mode |
331 | | */ |
332 | | |
333 | | /** |
334 | | * g_test_quiet: |
335 | | * |
336 | | * Returns %TRUE if tests are run in quiet mode. |
337 | | * In tests that use g_test_init(), the option `-q` or `--quiet` enables |
338 | | * this, while `--verbose` disables it. |
339 | | * The default is neither g_test_verbose() nor g_test_quiet(). |
340 | | * |
341 | | * Returns: %TRUE if in quiet mode |
342 | | */ |
343 | | |
344 | | /** |
345 | | * g_test_queue_unref: |
346 | | * @gobject: the object to unref |
347 | | * |
348 | | * Enqueue an object to be released with g_object_unref() during |
349 | | * the next teardown phase. This is equivalent to calling |
350 | | * g_test_queue_destroy() with a destroy callback of g_object_unref(). |
351 | | * |
352 | | * Since: 2.16 |
353 | | */ |
354 | | |
355 | | /** |
356 | | * GTestSubprocessFlags: |
357 | | * @G_TEST_SUBPROCESS_INHERIT_STDIN: If this flag is given, the child |
358 | | * process will inherit the parent's stdin. Otherwise, the child's |
359 | | * stdin is redirected to `/dev/null`. |
360 | | * @G_TEST_SUBPROCESS_INHERIT_STDOUT: If this flag is given, the child |
361 | | * process will inherit the parent's stdout. Otherwise, the child's |
362 | | * stdout will not be visible, but it will be captured to allow |
363 | | * later tests with g_test_trap_assert_stdout(). |
364 | | * @G_TEST_SUBPROCESS_INHERIT_STDERR: If this flag is given, the child |
365 | | * process will inherit the parent's stderr. Otherwise, the child's |
366 | | * stderr will not be visible, but it will be captured to allow |
367 | | * later tests with g_test_trap_assert_stderr(). |
368 | | * |
369 | | * Flags to pass to g_test_trap_subprocess() to control input and output. |
370 | | * |
371 | | * Note that in contrast with g_test_trap_fork(), the default is to |
372 | | * not show stdout and stderr. |
373 | | */ |
374 | | |
375 | | /** |
376 | | * g_test_trap_assert_passed: |
377 | | * |
378 | | * Assert that the last test subprocess passed. |
379 | | * See g_test_trap_subprocess(). |
380 | | * |
381 | | * Since: 2.16 |
382 | | */ |
383 | | |
384 | | /** |
385 | | * g_test_trap_assert_failed: |
386 | | * |
387 | | * Assert that the last test subprocess failed. |
388 | | * See g_test_trap_subprocess(). |
389 | | * |
390 | | * This is sometimes used to test situations that are formally considered to |
391 | | * be undefined behaviour, like inputs that fail a g_return_if_fail() |
392 | | * check. In these situations you should skip the entire test, including the |
393 | | * call to g_test_trap_subprocess(), unless g_test_undefined() returns %TRUE |
394 | | * to indicate that undefined behaviour may be tested. |
395 | | * |
396 | | * Since: 2.16 |
397 | | */ |
398 | | |
399 | | /** |
400 | | * g_test_trap_assert_stdout: |
401 | | * @soutpattern: a glob-style [pattern][glib-Glob-style-pattern-matching] |
402 | | * |
403 | | * Assert that the stdout output of the last test subprocess matches |
404 | | * @soutpattern. See g_test_trap_subprocess(). |
405 | | * |
406 | | * Since: 2.16 |
407 | | */ |
408 | | |
409 | | /** |
410 | | * g_test_trap_assert_stdout_unmatched: |
411 | | * @soutpattern: a glob-style [pattern][glib-Glob-style-pattern-matching] |
412 | | * |
413 | | * Assert that the stdout output of the last test subprocess |
414 | | * does not match @soutpattern. See g_test_trap_subprocess(). |
415 | | * |
416 | | * Since: 2.16 |
417 | | */ |
418 | | |
419 | | /** |
420 | | * g_test_trap_assert_stderr: |
421 | | * @serrpattern: a glob-style [pattern][glib-Glob-style-pattern-matching] |
422 | | * |
423 | | * Assert that the stderr output of the last test subprocess |
424 | | * matches @serrpattern. See g_test_trap_subprocess(). |
425 | | * |
426 | | * This is sometimes used to test situations that are formally |
427 | | * considered to be undefined behaviour, like code that hits a |
428 | | * g_assert() or g_error(). In these situations you should skip the |
429 | | * entire test, including the call to g_test_trap_subprocess(), unless |
430 | | * g_test_undefined() returns %TRUE to indicate that undefined |
431 | | * behaviour may be tested. |
432 | | * |
433 | | * Since: 2.16 |
434 | | */ |
435 | | |
436 | | /** |
437 | | * g_test_trap_assert_stderr_unmatched: |
438 | | * @serrpattern: a glob-style [pattern][glib-Glob-style-pattern-matching] |
439 | | * |
440 | | * Assert that the stderr output of the last test subprocess |
441 | | * does not match @serrpattern. See g_test_trap_subprocess(). |
442 | | * |
443 | | * Since: 2.16 |
444 | | */ |
445 | | |
446 | | /** |
447 | | * g_test_rand_bit: |
448 | | * |
449 | | * Get a reproducible random bit (0 or 1), see g_test_rand_int() |
450 | | * for details on test case random numbers. |
451 | | * |
452 | | * Since: 2.16 |
453 | | */ |
454 | | |
455 | | /** |
456 | | * g_assert: |
457 | | * @expr: the expression to check |
458 | | * |
459 | | * Debugging macro to terminate the application if the assertion |
460 | | * fails. If the assertion fails (i.e. the expression is not true), |
461 | | * an error message is logged and the application is terminated. |
462 | | * |
463 | | * The macro can be turned off in final releases of code by defining |
464 | | * `G_DISABLE_ASSERT` when compiling the application, so code must |
465 | | * not depend on any side effects from @expr. Similarly, it must not be used |
466 | | * in unit tests, otherwise the unit tests will be ineffective if compiled with |
467 | | * `G_DISABLE_ASSERT`. Use g_assert_true() and related macros in unit tests |
468 | | * instead. |
469 | | */ |
470 | | |
471 | | /** |
472 | | * g_assert_not_reached: |
473 | | * |
474 | | * Debugging macro to terminate the application if it is ever |
475 | | * reached. If it is reached, an error message is logged and the |
476 | | * application is terminated. |
477 | | * |
478 | | * The macro can be turned off in final releases of code by defining |
479 | | * `G_DISABLE_ASSERT` when compiling the application. Hence, it should not be |
480 | | * used in unit tests, where assertions should always be effective. |
481 | | */ |
482 | | |
483 | | /** |
484 | | * g_assert_true: |
485 | | * @expr: the expression to check |
486 | | * |
487 | | * Debugging macro to check that an expression is true. |
488 | | * |
489 | | * If the assertion fails (i.e. the expression is not true), |
490 | | * an error message is logged and the application is either |
491 | | * terminated or the testcase marked as failed. |
492 | | * |
493 | | * Note that unlike g_assert(), this macro is unaffected by whether |
494 | | * `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and, |
495 | | * conversely, g_assert() should not be used in tests. |
496 | | * |
497 | | * See g_test_set_nonfatal_assertions(). |
498 | | * |
499 | | * Since: 2.38 |
500 | | */ |
501 | | |
502 | | /** |
503 | | * g_assert_false: |
504 | | * @expr: the expression to check |
505 | | * |
506 | | * Debugging macro to check an expression is false. |
507 | | * |
508 | | * If the assertion fails (i.e. the expression is not false), |
509 | | * an error message is logged and the application is either |
510 | | * terminated or the testcase marked as failed. |
511 | | * |
512 | | * Note that unlike g_assert(), this macro is unaffected by whether |
513 | | * `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and, |
514 | | * conversely, g_assert() should not be used in tests. |
515 | | * |
516 | | * See g_test_set_nonfatal_assertions(). |
517 | | * |
518 | | * Since: 2.38 |
519 | | */ |
520 | | |
521 | | /** |
522 | | * g_assert_null: |
523 | | * @expr: the expression to check |
524 | | * |
525 | | * Debugging macro to check an expression is %NULL. |
526 | | * |
527 | | * If the assertion fails (i.e. the expression is not %NULL), |
528 | | * an error message is logged and the application is either |
529 | | * terminated or the testcase marked as failed. |
530 | | * |
531 | | * Note that unlike g_assert(), this macro is unaffected by whether |
532 | | * `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and, |
533 | | * conversely, g_assert() should not be used in tests. |
534 | | * |
535 | | * See g_test_set_nonfatal_assertions(). |
536 | | * |
537 | | * Since: 2.38 |
538 | | */ |
539 | | |
540 | | /** |
541 | | * g_assert_nonnull: |
542 | | * @expr: the expression to check |
543 | | * |
544 | | * Debugging macro to check an expression is not %NULL. |
545 | | * |
546 | | * If the assertion fails (i.e. the expression is %NULL), |
547 | | * an error message is logged and the application is either |
548 | | * terminated or the testcase marked as failed. |
549 | | * |
550 | | * Note that unlike g_assert(), this macro is unaffected by whether |
551 | | * `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and, |
552 | | * conversely, g_assert() should not be used in tests. |
553 | | * |
554 | | * See g_test_set_nonfatal_assertions(). |
555 | | * |
556 | | * Since: 2.40 |
557 | | */ |
558 | | |
559 | | /** |
560 | | * g_assert_cmpstr: |
561 | | * @s1: a string (may be %NULL) |
562 | | * @cmp: The comparison operator to use. |
563 | | * One of `==`, `!=`, `<`, `>`, `<=`, `>=`. |
564 | | * @s2: another string (may be %NULL) |
565 | | * |
566 | | * Debugging macro to compare two strings. If the comparison fails, |
567 | | * an error message is logged and the application is either terminated |
568 | | * or the testcase marked as failed. |
569 | | * The strings are compared using g_strcmp0(). |
570 | | * |
571 | | * The effect of `g_assert_cmpstr (s1, op, s2)` is |
572 | | * the same as `g_assert_true (g_strcmp0 (s1, s2) op 0)`. |
573 | | * The advantage of this macro is that it can produce a message that |
574 | | * includes the actual values of @s1 and @s2. |
575 | | * |
576 | | * |[<!-- language="C" --> |
577 | | * g_assert_cmpstr (mystring, ==, "fubar"); |
578 | | * ]| |
579 | | * |
580 | | * Since: 2.16 |
581 | | */ |
582 | | |
583 | | /** |
584 | | * g_assert_cmpstrv: |
585 | | * @strv1: (nullable): a string array (may be %NULL) |
586 | | * @strv2: (nullable): another string array (may be %NULL) |
587 | | * |
588 | | * Debugging macro to check if two %NULL-terminated string arrays (i.e. 2 |
589 | | * #GStrv) are equal. If they are not equal, an error message is logged and the |
590 | | * application is either terminated or the testcase marked as failed. |
591 | | * If both arrays are %NULL, the check passes. If one array is %NULL but the |
592 | | * other is not, an error message is logged. |
593 | | * |
594 | | * The effect of `g_assert_cmpstrv (strv1, strv2)` is the same as |
595 | | * `g_assert_true (g_strv_equal (strv1, strv2))` (if both arrays are not |
596 | | * %NULL). The advantage of this macro is that it can produce a message that |
597 | | * includes how @strv1 and @strv2 are different. |
598 | | * |
599 | | * |[<!-- language="C" --> |
600 | | * const char *expected[] = { "one", "two", "three", NULL }; |
601 | | * g_assert_cmpstrv (mystrv, expected); |
602 | | * ]| |
603 | | * |
604 | | * Since: 2.68 |
605 | | */ |
606 | | |
607 | | /** |
608 | | * g_assert_cmpint: |
609 | | * @n1: an integer |
610 | | * @cmp: The comparison operator to use. |
611 | | * One of `==`, `!=`, `<`, `>`, `<=`, `>=`. |
612 | | * @n2: another integer |
613 | | * |
614 | | * Debugging macro to compare two integers. |
615 | | * |
616 | | * The effect of `g_assert_cmpint (n1, op, n2)` is |
617 | | * the same as `g_assert_true (n1 op n2)`. The advantage |
618 | | * of this macro is that it can produce a message that includes the |
619 | | * actual values of @n1 and @n2. |
620 | | * |
621 | | * Since: 2.16 |
622 | | */ |
623 | | |
624 | | /** |
625 | | * g_assert_cmpuint: |
626 | | * @n1: an unsigned integer |
627 | | * @cmp: The comparison operator to use. |
628 | | * One of `==`, `!=`, `<`, `>`, `<=`, `>=`. |
629 | | * @n2: another unsigned integer |
630 | | * |
631 | | * Debugging macro to compare two unsigned integers. |
632 | | * |
633 | | * The effect of `g_assert_cmpuint (n1, op, n2)` is |
634 | | * the same as `g_assert_true (n1 op n2)`. The advantage |
635 | | * of this macro is that it can produce a message that includes the |
636 | | * actual values of @n1 and @n2. |
637 | | * |
638 | | * Since: 2.16 |
639 | | */ |
640 | | |
641 | | /** |
642 | | * g_assert_cmphex: |
643 | | * @n1: an unsigned integer |
644 | | * @cmp: The comparison operator to use. |
645 | | * One of `==`, `!=`, `<`, `>`, `<=`, `>=`. |
646 | | * @n2: another unsigned integer |
647 | | * |
648 | | * Debugging macro to compare to unsigned integers. |
649 | | * |
650 | | * This is a variant of g_assert_cmpuint() that displays the numbers |
651 | | * in hexadecimal notation in the message. |
652 | | * |
653 | | * Since: 2.16 |
654 | | */ |
655 | | |
656 | | /** |
657 | | * g_assert_cmpfloat: |
658 | | * @n1: a floating point number |
659 | | * @cmp: The comparison operator to use. |
660 | | * One of `==`, `!=`, `<`, `>`, `<=`, `>=`. |
661 | | * @n2: another floating point number |
662 | | * |
663 | | * Debugging macro to compare two floating point numbers. |
664 | | * |
665 | | * The effect of `g_assert_cmpfloat (n1, op, n2)` is |
666 | | * the same as `g_assert_true (n1 op n2)`. The advantage |
667 | | * of this macro is that it can produce a message that includes the |
668 | | * actual values of @n1 and @n2. |
669 | | * |
670 | | * Since: 2.16 |
671 | | */ |
672 | | |
673 | | /** |
674 | | * g_assert_cmpfloat_with_epsilon: |
675 | | * @n1: a floating point number |
676 | | * @n2: another floating point number |
677 | | * @epsilon: a numeric value that expresses the expected tolerance |
678 | | * between @n1 and @n2 |
679 | | * |
680 | | * Debugging macro to compare two floating point numbers within an epsilon. |
681 | | * |
682 | | * The effect of `g_assert_cmpfloat_with_epsilon (n1, n2, epsilon)` is |
683 | | * the same as `g_assert_true (abs (n1 - n2) < epsilon)`. The advantage |
684 | | * of this macro is that it can produce a message that includes the |
685 | | * actual values of @n1 and @n2. |
686 | | * |
687 | | * Since: 2.58 |
688 | | */ |
689 | | |
690 | | /** |
691 | | * g_assert_no_errno: |
692 | | * @expr: the expression to check |
693 | | * |
694 | | * Debugging macro to check that an expression has a non-negative return value, |
695 | | * as used by traditional POSIX functions (such as `rmdir()`) to indicate |
696 | | * success. |
697 | | * |
698 | | * If the assertion fails (i.e. the @expr returns a negative value), an error |
699 | | * message is logged and the testcase is marked as failed. The error message |
700 | | * will contain the value of `errno` and its human-readable message from |
701 | | * g_strerror(). |
702 | | * |
703 | | * This macro will clear the value of `errno` before executing @expr. |
704 | | * |
705 | | * Since: 2.66 |
706 | | */ |
707 | | |
708 | | /** |
709 | | * g_assert_cmpmem: |
710 | | * @m1: (nullable): pointer to a buffer |
711 | | * @l1: length of @m1 |
712 | | * @m2: (nullable): pointer to another buffer |
713 | | * @l2: length of @m2 |
714 | | * |
715 | | * Debugging macro to compare memory regions. If the comparison fails, |
716 | | * an error message is logged and the application is either terminated |
717 | | * or the testcase marked as failed. |
718 | | * |
719 | | * The effect of `g_assert_cmpmem (m1, l1, m2, l2)` is |
720 | | * the same as `g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)`. |
721 | | * The advantage of this macro is that it can produce a message that |
722 | | * includes the actual values of @l1 and @l2. |
723 | | * |
724 | | * @m1 may be %NULL if (and only if) @l1 is zero; similarly for @m2 and @l2. |
725 | | * |
726 | | * |[<!-- language="C" --> |
727 | | * g_assert_cmpmem (buf->data, buf->len, expected, sizeof (expected)); |
728 | | * ]| |
729 | | * |
730 | | * Since: 2.46 |
731 | | */ |
732 | | |
733 | | /** |
734 | | * g_assert_cmpvariant: |
735 | | * @v1: pointer to a #GVariant |
736 | | * @v2: pointer to another #GVariant |
737 | | * |
738 | | * Debugging macro to compare two #GVariants. If the comparison fails, |
739 | | * an error message is logged and the application is either terminated |
740 | | * or the testcase marked as failed. The variants are compared using |
741 | | * g_variant_equal(). |
742 | | * |
743 | | * The effect of `g_assert_cmpvariant (v1, v2)` is the same as |
744 | | * `g_assert_true (g_variant_equal (v1, v2))`. The advantage of this macro is |
745 | | * that it can produce a message that includes the actual values of @v1 and @v2. |
746 | | * |
747 | | * Since: 2.60 |
748 | | */ |
749 | | |
750 | | /** |
751 | | * g_assert_no_error: |
752 | | * @err: a #GError, possibly %NULL |
753 | | * |
754 | | * Debugging macro to check that a #GError is not set. |
755 | | * |
756 | | * The effect of `g_assert_no_error (err)` is |
757 | | * the same as `g_assert_true (err == NULL)`. The advantage |
758 | | * of this macro is that it can produce a message that includes |
759 | | * the error message and code. |
760 | | * |
761 | | * Since: 2.20 |
762 | | */ |
763 | | |
764 | | /** |
765 | | * g_assert_error: |
766 | | * @err: a #GError, possibly %NULL |
767 | | * @dom: the expected error domain (a #GQuark) |
768 | | * @c: the expected error code |
769 | | * |
770 | | * Debugging macro to check that a method has returned |
771 | | * the correct #GError. |
772 | | * |
773 | | * The effect of `g_assert_error (err, dom, c)` is |
774 | | * the same as `g_assert_true (err != NULL && err->domain |
775 | | * == dom && err->code == c)`. The advantage of this |
776 | | * macro is that it can produce a message that includes the incorrect |
777 | | * error message and code. |
778 | | * |
779 | | * This can only be used to test for a specific error. If you want to |
780 | | * test that @err is set, but don't care what it's set to, just use |
781 | | * `g_assert_nonnull (err)`. |
782 | | * |
783 | | * Since: 2.20 |
784 | | */ |
785 | | |
786 | | /** |
787 | | * GTestCase: |
788 | | * |
789 | | * An opaque structure representing a test case. |
790 | | */ |
791 | | |
792 | | /** |
793 | | * GTestSuite: |
794 | | * |
795 | | * An opaque structure representing a test suite. |
796 | | */ |
797 | | |
798 | | |
799 | | /* Global variable for storing assertion messages; this is the counterpart to |
800 | | * glibc's (private) __abort_msg variable, and allows developers and crash |
801 | | * analysis systems like Apport and ABRT to fish out assertion messages from |
802 | | * core dumps, instead of having to catch them on screen output. |
803 | | */ |
804 | | GLIB_VAR char *__glib_assert_msg; |
805 | | char *__glib_assert_msg = NULL; |
806 | | |
807 | | /* --- constants --- */ |
808 | | #define G_TEST_STATUS_TIMED_OUT 1024 |
809 | | |
810 | | /* --- structures --- */ |
811 | | struct GTestCase |
812 | | { |
813 | | gchar *name; |
814 | | guint fixture_size; |
815 | | void (*fixture_setup) (void*, gconstpointer); |
816 | | void (*fixture_test) (void*, gconstpointer); |
817 | | void (*fixture_teardown) (void*, gconstpointer); |
818 | | gpointer test_data; |
819 | | }; |
820 | | struct GTestSuite |
821 | | { |
822 | | gchar *name; |
823 | | GSList *suites; |
824 | | GSList *cases; |
825 | | }; |
826 | | typedef struct DestroyEntry DestroyEntry; |
827 | | struct DestroyEntry |
828 | | { |
829 | | DestroyEntry *next; |
830 | | GDestroyNotify destroy_func; |
831 | | gpointer destroy_data; |
832 | | }; |
833 | | |
834 | | /* --- prototypes --- */ |
835 | | static void test_run_seed (const gchar *rseed); |
836 | | static void test_trap_clear (void); |
837 | | static guint8* g_test_log_dump (GTestLogMsg *msg, |
838 | | guint *len); |
839 | | static void gtest_default_log_handler (const gchar *log_domain, |
840 | | GLogLevelFlags log_level, |
841 | | const gchar *message, |
842 | | gpointer unused_data); |
843 | | |
844 | | |
845 | | static const char * const g_test_result_names[] = { |
846 | | "OK", |
847 | | "SKIP", |
848 | | "FAIL", |
849 | | "TODO" |
850 | | }; |
851 | | |
852 | | /* --- variables --- */ |
853 | | static int test_log_fd = -1; |
854 | | static gboolean test_mode_fatal = TRUE; |
855 | | static gboolean g_test_run_once = TRUE; |
856 | | static gboolean test_isolate_dirs = FALSE; |
857 | | static gchar *test_isolate_dirs_tmpdir = NULL; |
858 | | static const gchar *test_tmpdir = NULL; |
859 | | static gboolean test_run_list = FALSE; |
860 | | static gchar *test_run_seedstr = NULL; |
861 | | G_LOCK_DEFINE_STATIC (test_run_rand); |
862 | | static GRand *test_run_rand = NULL; |
863 | | static gchar *test_run_name = ""; |
864 | | static GSList **test_filename_free_list; |
865 | | static guint test_run_forks = 0; |
866 | | static guint test_run_count = 0; |
867 | | static guint test_count = 0; |
868 | | static guint test_skipped_count = 0; |
869 | | static GTestResult test_run_success = G_TEST_RUN_FAILURE; |
870 | | static gchar *test_run_msg = NULL; |
871 | | static guint test_startup_skip_count = 0; |
872 | | static GTimer *test_user_timer = NULL; |
873 | | static double test_user_stamp = 0; |
874 | | static GSList *test_paths = NULL; |
875 | | static gboolean test_prefix = FALSE; |
876 | | static gboolean test_prefix_extended = FALSE; |
877 | | static GSList *test_paths_skipped = NULL; |
878 | | static gboolean test_prefix_skipped = FALSE; |
879 | | static gboolean test_prefix_extended_skipped = FALSE; |
880 | | static GTestSuite *test_suite_root = NULL; |
881 | | static int test_trap_last_status = 0; /* unmodified platform-specific status */ |
882 | | static GPid test_trap_last_pid = 0; |
883 | | static char *test_trap_last_subprocess = NULL; |
884 | | static char *test_trap_last_stdout = NULL; |
885 | | static char *test_trap_last_stderr = NULL; |
886 | | static char *test_uri_base = NULL; |
887 | | static gboolean test_debug_log = FALSE; |
888 | | static gboolean test_tap_log = TRUE; /* default to TAP as of GLib 2.62; see #1619; the non-TAP output mode is deprecated */ |
889 | | static gboolean test_nonfatal_assertions = FALSE; |
890 | | static DestroyEntry *test_destroy_queue = NULL; |
891 | | static char *test_argv0 = NULL; |
892 | | static char *test_argv0_dirname; |
893 | | static const char *test_disted_files_dir; |
894 | | static const char *test_built_files_dir; |
895 | | static char *test_initial_cwd = NULL; |
896 | | static gboolean test_in_forked_child = FALSE; |
897 | | static gboolean test_in_subprocess = FALSE; |
898 | | static GTestConfig mutable_test_config_vars = { |
899 | | FALSE, /* test_initialized */ |
900 | | TRUE, /* test_quick */ |
901 | | FALSE, /* test_perf */ |
902 | | FALSE, /* test_verbose */ |
903 | | FALSE, /* test_quiet */ |
904 | | TRUE, /* test_undefined */ |
905 | | }; |
906 | | const GTestConfig * const g_test_config_vars = &mutable_test_config_vars; |
907 | | static gboolean no_g_set_prgname = FALSE; |
908 | | |
909 | | /* --- functions --- */ |
910 | | const char* |
911 | | g_test_log_type_name (GTestLogType log_type) |
912 | 0 | { |
913 | 0 | switch (log_type) |
914 | 0 | { |
915 | 0 | case G_TEST_LOG_NONE: return "none"; |
916 | 0 | case G_TEST_LOG_ERROR: return "error"; |
917 | 0 | case G_TEST_LOG_START_BINARY: return "binary"; |
918 | 0 | case G_TEST_LOG_LIST_CASE: return "list"; |
919 | 0 | case G_TEST_LOG_SKIP_CASE: return "skip"; |
920 | 0 | case G_TEST_LOG_START_CASE: return "start"; |
921 | 0 | case G_TEST_LOG_STOP_CASE: return "stop"; |
922 | 0 | case G_TEST_LOG_MIN_RESULT: return "minperf"; |
923 | 0 | case G_TEST_LOG_MAX_RESULT: return "maxperf"; |
924 | 0 | case G_TEST_LOG_MESSAGE: return "message"; |
925 | 0 | case G_TEST_LOG_START_SUITE: return "start suite"; |
926 | 0 | case G_TEST_LOG_STOP_SUITE: return "stop suite"; |
927 | 0 | } |
928 | 0 | return "???"; |
929 | 0 | } |
930 | | |
931 | | static void |
932 | | g_test_log_send (guint n_bytes, |
933 | | const guint8 *buffer) |
934 | 0 | { |
935 | 0 | if (test_log_fd >= 0) |
936 | 0 | { |
937 | 0 | int r; |
938 | 0 | do |
939 | 0 | r = write (test_log_fd, buffer, n_bytes); |
940 | 0 | while (r < 0 && errno == EINTR); |
941 | 0 | } |
942 | 0 | if (test_debug_log) |
943 | 0 | { |
944 | 0 | GTestLogBuffer *lbuffer = g_test_log_buffer_new (); |
945 | 0 | GTestLogMsg *msg; |
946 | 0 | guint ui; |
947 | 0 | g_test_log_buffer_push (lbuffer, n_bytes, buffer); |
948 | 0 | msg = g_test_log_buffer_pop (lbuffer); |
949 | 0 | g_warn_if_fail (msg != NULL); |
950 | 0 | g_warn_if_fail (lbuffer->data->len == 0); |
951 | 0 | g_test_log_buffer_free (lbuffer); |
952 | | /* print message */ |
953 | 0 | g_printerr ("{*LOG(%s)", g_test_log_type_name (msg->log_type)); |
954 | 0 | for (ui = 0; ui < msg->n_strings; ui++) |
955 | 0 | g_printerr (":{%s}", msg->strings[ui]); |
956 | 0 | if (msg->n_nums) |
957 | 0 | { |
958 | 0 | g_printerr (":("); |
959 | 0 | for (ui = 0; ui < msg->n_nums; ui++) |
960 | 0 | { |
961 | 0 | if ((long double) (long) msg->nums[ui] == msg->nums[ui]) |
962 | 0 | g_printerr ("%s%ld", ui ? ";" : "", (long) msg->nums[ui]); |
963 | 0 | else |
964 | 0 | g_printerr ("%s%.16g", ui ? ";" : "", (double) msg->nums[ui]); |
965 | 0 | } |
966 | 0 | g_printerr (")"); |
967 | 0 | } |
968 | 0 | g_printerr (":LOG*}\n"); |
969 | 0 | g_test_log_msg_free (msg); |
970 | 0 | } |
971 | 0 | } |
972 | | |
973 | | static void |
974 | | g_test_log (GTestLogType lbit, |
975 | | const gchar *string1, |
976 | | const gchar *string2, |
977 | | guint n_args, |
978 | | long double *largs) |
979 | 0 | { |
980 | 0 | GTestResult result; |
981 | 0 | gboolean fail; |
982 | 0 | GTestLogMsg msg; |
983 | 0 | gchar *astrings[3] = { NULL, NULL, NULL }; |
984 | 0 | guint8 *dbuffer; |
985 | 0 | guint32 dbufferlen; |
986 | |
|
987 | 0 | switch (lbit) |
988 | 0 | { |
989 | 0 | case G_TEST_LOG_START_BINARY: |
990 | 0 | if (test_tap_log) |
991 | 0 | g_print ("# random seed: %s\n", string2); |
992 | 0 | else if (g_test_verbose ()) |
993 | 0 | g_print ("GTest: random seed: %s\n", string2); |
994 | 0 | break; |
995 | 0 | case G_TEST_LOG_START_SUITE: |
996 | 0 | if (test_tap_log) |
997 | 0 | { |
998 | | /* We only print the TAP "plan" (1..n) ahead of time if we did |
999 | | * not use the -p option to select specific tests to be run. */ |
1000 | 0 | if (string1[0] != 0) |
1001 | 0 | g_print ("# Start of %s tests\n", string1); |
1002 | 0 | else if (test_paths == NULL) |
1003 | 0 | g_print ("1..%d\n", test_count); |
1004 | 0 | } |
1005 | 0 | break; |
1006 | 0 | case G_TEST_LOG_STOP_SUITE: |
1007 | 0 | if (test_tap_log) |
1008 | 0 | { |
1009 | | /* If we didn't print the TAP "plan" at the beginning because |
1010 | | * we were using -p, we need to print how many tests we ran at |
1011 | | * the end instead. */ |
1012 | 0 | if (string1[0] != 0) |
1013 | 0 | g_print ("# End of %s tests\n", string1); |
1014 | 0 | else if (test_paths != NULL) |
1015 | 0 | g_print ("1..%d\n", test_run_count); |
1016 | 0 | } |
1017 | 0 | break; |
1018 | 0 | case G_TEST_LOG_STOP_CASE: |
1019 | 0 | result = largs[0]; |
1020 | 0 | fail = result == G_TEST_RUN_FAILURE; |
1021 | 0 | if (test_tap_log) |
1022 | 0 | { |
1023 | 0 | const gchar *ok; |
1024 | | |
1025 | | /* The TAP representation for an expected failure starts with |
1026 | | * "not ok", even though it does not actually count as failing |
1027 | | * due to the use of the TODO directive. "ok # TODO" would mean |
1028 | | * a test that was expected to fail unexpectedly succeeded, |
1029 | | * for which GTestResult does not currently have a |
1030 | | * representation. */ |
1031 | 0 | if (fail || result == G_TEST_RUN_INCOMPLETE) |
1032 | 0 | ok = "not ok"; |
1033 | 0 | else |
1034 | 0 | ok = "ok"; |
1035 | |
|
1036 | 0 | g_print ("%s %d %s", ok, test_run_count, string1); |
1037 | 0 | if (result == G_TEST_RUN_INCOMPLETE) |
1038 | 0 | g_print (" # TODO %s\n", string2 ? string2 : ""); |
1039 | 0 | else if (result == G_TEST_RUN_SKIPPED) |
1040 | 0 | g_print (" # SKIP %s\n", string2 ? string2 : ""); |
1041 | 0 | else |
1042 | 0 | g_print ("\n"); |
1043 | 0 | } |
1044 | 0 | else if (g_test_verbose ()) |
1045 | 0 | g_print ("GTest: result: %s\n", g_test_result_names[result]); |
1046 | 0 | else if (!g_test_quiet ()) |
1047 | 0 | g_print ("%s\n", g_test_result_names[result]); |
1048 | 0 | if (fail && test_mode_fatal) |
1049 | 0 | { |
1050 | 0 | if (test_tap_log) |
1051 | 0 | g_print ("Bail out!\n"); |
1052 | 0 | g_abort (); |
1053 | 0 | } |
1054 | 0 | if (result == G_TEST_RUN_SKIPPED || result == G_TEST_RUN_INCOMPLETE) |
1055 | 0 | test_skipped_count++; |
1056 | 0 | break; |
1057 | 0 | case G_TEST_LOG_SKIP_CASE: |
1058 | 0 | if (test_tap_log) |
1059 | 0 | g_print ("ok %d %s # SKIP\n", test_run_count, string1); |
1060 | 0 | break; |
1061 | 0 | case G_TEST_LOG_MIN_RESULT: |
1062 | 0 | if (test_tap_log) |
1063 | 0 | g_print ("# min perf: %s\n", string1); |
1064 | 0 | else if (g_test_verbose ()) |
1065 | 0 | g_print ("(MINPERF:%s)\n", string1); |
1066 | 0 | break; |
1067 | 0 | case G_TEST_LOG_MAX_RESULT: |
1068 | 0 | if (test_tap_log) |
1069 | 0 | g_print ("# max perf: %s\n", string1); |
1070 | 0 | else if (g_test_verbose ()) |
1071 | 0 | g_print ("(MAXPERF:%s)\n", string1); |
1072 | 0 | break; |
1073 | 0 | case G_TEST_LOG_MESSAGE: |
1074 | 0 | if (test_tap_log) |
1075 | 0 | { |
1076 | 0 | if (strstr (string1, "\n") == NULL) |
1077 | 0 | g_print ("# %s\n", string1); |
1078 | 0 | else |
1079 | 0 | { |
1080 | 0 | char **lines = g_strsplit (string1, "\n", -1); |
1081 | 0 | gsize i; |
1082 | |
|
1083 | 0 | for (i = 0; lines[i] != NULL; i++) |
1084 | 0 | g_print ("# %s\n", lines[i]); |
1085 | |
|
1086 | 0 | g_strfreev (lines); |
1087 | 0 | } |
1088 | 0 | } |
1089 | 0 | else if (g_test_verbose ()) |
1090 | 0 | g_print ("(MSG: %s)\n", string1); |
1091 | 0 | break; |
1092 | 0 | case G_TEST_LOG_ERROR: |
1093 | 0 | if (test_tap_log) |
1094 | 0 | g_print ("Bail out! %s\n", string1); |
1095 | 0 | else if (g_test_verbose ()) |
1096 | 0 | g_print ("(ERROR: %s)\n", string1); |
1097 | 0 | break; |
1098 | 0 | default: ; |
1099 | 0 | } |
1100 | | |
1101 | 0 | msg.log_type = lbit; |
1102 | 0 | msg.n_strings = (string1 != NULL) + (string1 && string2); |
1103 | 0 | msg.strings = astrings; |
1104 | 0 | astrings[0] = (gchar*) string1; |
1105 | 0 | astrings[1] = astrings[0] ? (gchar*) string2 : NULL; |
1106 | 0 | msg.n_nums = n_args; |
1107 | 0 | msg.nums = largs; |
1108 | 0 | dbuffer = g_test_log_dump (&msg, &dbufferlen); |
1109 | 0 | g_test_log_send (dbufferlen, dbuffer); |
1110 | 0 | g_free (dbuffer); |
1111 | |
|
1112 | 0 | switch (lbit) |
1113 | 0 | { |
1114 | 0 | case G_TEST_LOG_START_CASE: |
1115 | 0 | if (test_tap_log) |
1116 | 0 | ; |
1117 | 0 | else if (g_test_verbose ()) |
1118 | 0 | g_print ("GTest: run: %s\n", string1); |
1119 | 0 | else if (!g_test_quiet ()) |
1120 | 0 | g_print ("%s: ", string1); |
1121 | 0 | break; |
1122 | 0 | default: ; |
1123 | 0 | } |
1124 | 0 | } |
1125 | | |
1126 | | /* We intentionally parse the command line without GOptionContext |
1127 | | * because otherwise you would never be able to test it. |
1128 | | */ |
1129 | | static void |
1130 | | parse_args (gint *argc_p, |
1131 | | gchar ***argv_p) |
1132 | 0 | { |
1133 | 0 | guint argc = *argc_p; |
1134 | 0 | gchar **argv = *argv_p; |
1135 | 0 | guint i, e; |
1136 | |
|
1137 | 0 | test_argv0 = argv[0]; |
1138 | 0 | test_initial_cwd = g_get_current_dir (); |
1139 | | |
1140 | | /* parse known args */ |
1141 | 0 | for (i = 1; i < argc; i++) |
1142 | 0 | { |
1143 | 0 | if (strcmp (argv[i], "--g-fatal-warnings") == 0) |
1144 | 0 | { |
1145 | 0 | GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK); |
1146 | 0 | fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL); |
1147 | 0 | g_log_set_always_fatal (fatal_mask); |
1148 | 0 | argv[i] = NULL; |
1149 | 0 | } |
1150 | 0 | else if (strcmp (argv[i], "--keep-going") == 0 || |
1151 | 0 | strcmp (argv[i], "-k") == 0) |
1152 | 0 | { |
1153 | 0 | test_mode_fatal = FALSE; |
1154 | 0 | argv[i] = NULL; |
1155 | 0 | } |
1156 | 0 | else if (strcmp (argv[i], "--debug-log") == 0) |
1157 | 0 | { |
1158 | 0 | test_debug_log = TRUE; |
1159 | 0 | argv[i] = NULL; |
1160 | 0 | } |
1161 | 0 | else if (strcmp (argv[i], "--tap") == 0) |
1162 | 0 | { |
1163 | 0 | test_tap_log = TRUE; |
1164 | 0 | argv[i] = NULL; |
1165 | 0 | } |
1166 | 0 | else if (strcmp ("--GTestLogFD", argv[i]) == 0 || strncmp ("--GTestLogFD=", argv[i], 13) == 0) |
1167 | 0 | { |
1168 | 0 | gchar *equal = argv[i] + 12; |
1169 | 0 | if (*equal == '=') |
1170 | 0 | test_log_fd = g_ascii_strtoull (equal + 1, NULL, 0); |
1171 | 0 | else if (i + 1 < argc) |
1172 | 0 | { |
1173 | 0 | argv[i++] = NULL; |
1174 | 0 | test_log_fd = g_ascii_strtoull (argv[i], NULL, 0); |
1175 | 0 | } |
1176 | 0 | argv[i] = NULL; |
1177 | | |
1178 | | /* Force non-TAP output when using gtester */ |
1179 | 0 | test_tap_log = FALSE; |
1180 | 0 | } |
1181 | 0 | else if (strcmp ("--GTestSkipCount", argv[i]) == 0 || strncmp ("--GTestSkipCount=", argv[i], 17) == 0) |
1182 | 0 | { |
1183 | 0 | gchar *equal = argv[i] + 16; |
1184 | 0 | if (*equal == '=') |
1185 | 0 | test_startup_skip_count = g_ascii_strtoull (equal + 1, NULL, 0); |
1186 | 0 | else if (i + 1 < argc) |
1187 | 0 | { |
1188 | 0 | argv[i++] = NULL; |
1189 | 0 | test_startup_skip_count = g_ascii_strtoull (argv[i], NULL, 0); |
1190 | 0 | } |
1191 | 0 | argv[i] = NULL; |
1192 | 0 | } |
1193 | 0 | else if (strcmp ("--GTestSubprocess", argv[i]) == 0) |
1194 | 0 | { |
1195 | 0 | test_in_subprocess = TRUE; |
1196 | | /* We typically expect these child processes to crash, and some |
1197 | | * tests spawn a *lot* of them. Avoid spamming system crash |
1198 | | * collection programs such as systemd-coredump and abrt. |
1199 | | */ |
1200 | 0 | #ifdef HAVE_SYS_RESOURCE_H |
1201 | 0 | { |
1202 | 0 | struct rlimit limit = { 0, 0 }; |
1203 | 0 | (void) setrlimit (RLIMIT_CORE, &limit); |
1204 | 0 | } |
1205 | 0 | #endif |
1206 | 0 | argv[i] = NULL; |
1207 | | |
1208 | | /* Force non-TAP output when spawning a subprocess, since people often |
1209 | | * test the stdout/stderr of the subprocess strictly */ |
1210 | 0 | test_tap_log = FALSE; |
1211 | 0 | } |
1212 | 0 | else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0) |
1213 | 0 | { |
1214 | 0 | gchar *equal = argv[i] + 2; |
1215 | 0 | if (*equal == '=') |
1216 | 0 | test_paths = g_slist_prepend (test_paths, equal + 1); |
1217 | 0 | else if (i + 1 < argc) |
1218 | 0 | { |
1219 | 0 | argv[i++] = NULL; |
1220 | 0 | test_paths = g_slist_prepend (test_paths, argv[i]); |
1221 | 0 | } |
1222 | 0 | argv[i] = NULL; |
1223 | 0 | if (test_prefix_extended) { |
1224 | 0 | printf ("do not mix [-r | --run-prefix] with '-p'\n"); |
1225 | 0 | exit (1); |
1226 | 0 | } |
1227 | 0 | test_prefix = TRUE; |
1228 | 0 | } |
1229 | 0 | else if (strcmp ("-r", argv[i]) == 0 || |
1230 | 0 | strncmp ("-r=", argv[i], 3) == 0 || |
1231 | 0 | strcmp ("--run-prefix", argv[i]) == 0 || |
1232 | 0 | strncmp ("--run-prefix=", argv[i], 13) == 0) |
1233 | 0 | { |
1234 | 0 | gchar *equal = argv[i] + 2; |
1235 | 0 | if (*equal == '=') |
1236 | 0 | test_paths = g_slist_prepend (test_paths, equal + 1); |
1237 | 0 | else if (i + 1 < argc) |
1238 | 0 | { |
1239 | 0 | argv[i++] = NULL; |
1240 | 0 | test_paths = g_slist_prepend (test_paths, argv[i]); |
1241 | 0 | } |
1242 | 0 | argv[i] = NULL; |
1243 | 0 | if (test_prefix) { |
1244 | 0 | printf ("do not mix [-r | --run-prefix] with '-p'\n"); |
1245 | 0 | exit (1); |
1246 | 0 | } |
1247 | 0 | test_prefix_extended = TRUE; |
1248 | 0 | } |
1249 | 0 | else if (strcmp ("-s", argv[i]) == 0 || strncmp ("-s=", argv[i], 3) == 0) |
1250 | 0 | { |
1251 | 0 | gchar *equal = argv[i] + 2; |
1252 | 0 | if (*equal == '=') |
1253 | 0 | test_paths_skipped = g_slist_prepend (test_paths_skipped, equal + 1); |
1254 | 0 | else if (i + 1 < argc) |
1255 | 0 | { |
1256 | 0 | argv[i++] = NULL; |
1257 | 0 | test_paths_skipped = g_slist_prepend (test_paths_skipped, argv[i]); |
1258 | 0 | } |
1259 | 0 | argv[i] = NULL; |
1260 | 0 | if (test_prefix_extended_skipped) { |
1261 | 0 | printf ("do not mix [-x | --skip-prefix] with '-s'\n"); |
1262 | 0 | exit (1); |
1263 | 0 | } |
1264 | 0 | test_prefix_skipped = TRUE; |
1265 | 0 | } |
1266 | 0 | else if (strcmp ("-x", argv[i]) == 0 || |
1267 | 0 | strncmp ("-x=", argv[i], 3) == 0 || |
1268 | 0 | strcmp ("--skip-prefix", argv[i]) == 0 || |
1269 | 0 | strncmp ("--skip-prefix=", argv[i], 14) == 0) |
1270 | 0 | { |
1271 | 0 | gchar *equal = argv[i] + 2; |
1272 | 0 | if (*equal == '=') |
1273 | 0 | test_paths_skipped = g_slist_prepend (test_paths_skipped, equal + 1); |
1274 | 0 | else if (i + 1 < argc) |
1275 | 0 | { |
1276 | 0 | argv[i++] = NULL; |
1277 | 0 | test_paths_skipped = g_slist_prepend (test_paths_skipped, argv[i]); |
1278 | 0 | } |
1279 | 0 | argv[i] = NULL; |
1280 | 0 | if (test_prefix_skipped) { |
1281 | 0 | printf ("do not mix [-x | --skip-prefix] with '-s'\n"); |
1282 | 0 | exit (1); |
1283 | 0 | } |
1284 | 0 | test_prefix_extended_skipped = TRUE; |
1285 | 0 | } |
1286 | 0 | else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0) |
1287 | 0 | { |
1288 | 0 | gchar *equal = argv[i] + 2; |
1289 | 0 | const gchar *mode = ""; |
1290 | 0 | if (*equal == '=') |
1291 | 0 | mode = equal + 1; |
1292 | 0 | else if (i + 1 < argc) |
1293 | 0 | { |
1294 | 0 | argv[i++] = NULL; |
1295 | 0 | mode = argv[i]; |
1296 | 0 | } |
1297 | 0 | if (strcmp (mode, "perf") == 0) |
1298 | 0 | mutable_test_config_vars.test_perf = TRUE; |
1299 | 0 | else if (strcmp (mode, "slow") == 0) |
1300 | 0 | mutable_test_config_vars.test_quick = FALSE; |
1301 | 0 | else if (strcmp (mode, "thorough") == 0) |
1302 | 0 | mutable_test_config_vars.test_quick = FALSE; |
1303 | 0 | else if (strcmp (mode, "quick") == 0) |
1304 | 0 | { |
1305 | 0 | mutable_test_config_vars.test_quick = TRUE; |
1306 | 0 | mutable_test_config_vars.test_perf = FALSE; |
1307 | 0 | } |
1308 | 0 | else if (strcmp (mode, "undefined") == 0) |
1309 | 0 | mutable_test_config_vars.test_undefined = TRUE; |
1310 | 0 | else if (strcmp (mode, "no-undefined") == 0) |
1311 | 0 | mutable_test_config_vars.test_undefined = FALSE; |
1312 | 0 | else |
1313 | 0 | g_error ("unknown test mode: -m %s", mode); |
1314 | 0 | argv[i] = NULL; |
1315 | 0 | } |
1316 | 0 | else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0) |
1317 | 0 | { |
1318 | 0 | mutable_test_config_vars.test_quiet = TRUE; |
1319 | 0 | mutable_test_config_vars.test_verbose = FALSE; |
1320 | 0 | argv[i] = NULL; |
1321 | 0 | } |
1322 | 0 | else if (strcmp ("--verbose", argv[i]) == 0) |
1323 | 0 | { |
1324 | 0 | mutable_test_config_vars.test_quiet = FALSE; |
1325 | 0 | mutable_test_config_vars.test_verbose = TRUE; |
1326 | 0 | argv[i] = NULL; |
1327 | 0 | } |
1328 | 0 | else if (strcmp ("-l", argv[i]) == 0) |
1329 | 0 | { |
1330 | 0 | test_run_list = TRUE; |
1331 | 0 | argv[i] = NULL; |
1332 | 0 | } |
1333 | 0 | else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0) |
1334 | 0 | { |
1335 | 0 | gchar *equal = argv[i] + 6; |
1336 | 0 | if (*equal == '=') |
1337 | 0 | test_run_seedstr = equal + 1; |
1338 | 0 | else if (i + 1 < argc) |
1339 | 0 | { |
1340 | 0 | argv[i++] = NULL; |
1341 | 0 | test_run_seedstr = argv[i]; |
1342 | 0 | } |
1343 | 0 | argv[i] = NULL; |
1344 | 0 | } |
1345 | 0 | else if (strcmp ("-?", argv[i]) == 0 || |
1346 | 0 | strcmp ("-h", argv[i]) == 0 || |
1347 | 0 | strcmp ("--help", argv[i]) == 0) |
1348 | 0 | { |
1349 | 0 | printf ("Usage:\n" |
1350 | 0 | " %s [OPTION...]\n\n" |
1351 | 0 | "Help Options:\n" |
1352 | 0 | " -h, --help Show help options\n\n" |
1353 | 0 | "Test Options:\n" |
1354 | 0 | " --g-fatal-warnings Make all warnings fatal\n" |
1355 | 0 | " -l List test cases available in a test executable\n" |
1356 | 0 | " -m {perf|slow|thorough|quick} Execute tests according to mode\n" |
1357 | 0 | " -m {undefined|no-undefined} Execute tests according to mode\n" |
1358 | 0 | " -p TESTPATH Only start test cases matching TESTPATH\n" |
1359 | 0 | " -s TESTPATH Skip all tests matching TESTPATH\n" |
1360 | 0 | " [-r | --run-prefix] PREFIX Only start test cases (or suites) matching PREFIX (incompatible with -p).\n" |
1361 | 0 | " Unlike the -p option (which only goes one level deep), this option would \n" |
1362 | 0 | " run all tests path that have PREFIX at the beginning of their name.\n" |
1363 | 0 | " Note that the prefix used should be a valid test path (and not a simple prefix).\n" |
1364 | 0 | " [-x | --skip-prefix] PREFIX Skip all tests matching PREFIX (incompatible with -s)\n" |
1365 | 0 | " Unlike the -s option (which only skips the exact TESTPATH), this option will \n" |
1366 | 0 | " skip all the tests that begins with PREFIX).\n" |
1367 | 0 | " --seed=SEEDSTRING Start tests with random seed SEEDSTRING\n" |
1368 | 0 | " --debug-log debug test logging output\n" |
1369 | 0 | " -q, --quiet Run tests quietly\n" |
1370 | 0 | " --verbose Run tests verbosely\n", |
1371 | 0 | argv[0]); |
1372 | 0 | exit (0); |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | | /* We've been prepending to test_paths, but its order matters, so |
1377 | | * permute it */ |
1378 | 0 | test_paths = g_slist_reverse (test_paths); |
1379 | | |
1380 | | /* collapse argv */ |
1381 | 0 | e = 1; |
1382 | 0 | for (i = 1; i < argc; i++) |
1383 | 0 | if (argv[i]) |
1384 | 0 | { |
1385 | 0 | argv[e++] = argv[i]; |
1386 | 0 | if (i >= e) |
1387 | 0 | argv[i] = NULL; |
1388 | 0 | } |
1389 | 0 | *argc_p = e; |
1390 | 0 | } |
1391 | | |
1392 | | /* A fairly naive `rm -rf` implementation to clean up after unit tests. */ |
1393 | | static void |
1394 | | rm_rf (const gchar *path) |
1395 | 0 | { |
1396 | 0 | GDir *dir = NULL; |
1397 | 0 | const gchar *entry; |
1398 | |
|
1399 | 0 | dir = g_dir_open (path, 0, NULL); |
1400 | 0 | if (dir == NULL) |
1401 | 0 | { |
1402 | | /* Assume it’s a file. Ignore failure. */ |
1403 | 0 | (void) g_remove (path); |
1404 | 0 | return; |
1405 | 0 | } |
1406 | | |
1407 | 0 | while ((entry = g_dir_read_name (dir)) != NULL) |
1408 | 0 | { |
1409 | 0 | gchar *sub_path = g_build_filename (path, entry, NULL); |
1410 | 0 | rm_rf (sub_path); |
1411 | 0 | g_free (sub_path); |
1412 | 0 | } |
1413 | |
|
1414 | 0 | g_dir_close (dir); |
1415 | |
|
1416 | 0 | g_rmdir (path); |
1417 | 0 | } |
1418 | | |
1419 | | /* Implement the %G_TEST_OPTION_ISOLATE_DIRS option, iff it’s enabled. Create |
1420 | | * a temporary directory for this unit test (disambiguated using @test_run_name) |
1421 | | * and use g_set_user_dirs() to point various XDG directories into it, without |
1422 | | * having to call setenv() in a process which potentially has threads running. |
1423 | | * |
1424 | | * Note that this is called for each unit test, and hence won’t have taken |
1425 | | * effect before g_test_run() is called in the unit test’s main(). Hence |
1426 | | * references to XDG variables in main() will not be using the temporary |
1427 | | * directory. */ |
1428 | | static gboolean |
1429 | | test_do_isolate_dirs (GError **error) |
1430 | 0 | { |
1431 | 0 | gchar *subdir = NULL; |
1432 | 0 | gchar *home_dir = NULL, *cache_dir = NULL, *config_dir = NULL; |
1433 | 0 | gchar *data_dir = NULL, *runtime_dir = NULL; |
1434 | 0 | gchar *config_dirs[3]; |
1435 | 0 | gchar *data_dirs[3]; |
1436 | |
|
1437 | 0 | if (!test_isolate_dirs) |
1438 | 0 | return TRUE; |
1439 | | |
1440 | | /* The @test_run_name includes the test suites, so may be several directories |
1441 | | * deep. Add a `.dirs` directory to contain all the paths we create, and |
1442 | | * guarantee none of them clash with test paths below the current one — test |
1443 | | * paths may not contain components starting with `.`. */ |
1444 | 0 | subdir = g_build_filename (test_tmpdir, test_run_name, ".dirs", NULL); |
1445 | | |
1446 | | /* We have to create the runtime directory (because it must be bound to |
1447 | | * the session lifetime, which we consider to be the lifetime of the unit |
1448 | | * test for testing purposes — see |
1449 | | * https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html. |
1450 | | * We don’t need to create the other directories — the specification |
1451 | | * requires that client code create them if they don’t exist. Not creating |
1452 | | * them automatically is a good test of clients’ adherence to the spec |
1453 | | * and error handling of missing directories. */ |
1454 | 0 | runtime_dir = g_build_filename (subdir, "runtime", NULL); |
1455 | 0 | if (g_mkdir_with_parents (runtime_dir, 0700) != 0) |
1456 | 0 | { |
1457 | 0 | gint saved_errno = errno; |
1458 | 0 | g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno), |
1459 | 0 | "Failed to create XDG_RUNTIME_DIR ‘%s’: %s", |
1460 | 0 | runtime_dir, g_strerror (saved_errno)); |
1461 | 0 | g_free (runtime_dir); |
1462 | 0 | g_free (subdir); |
1463 | 0 | return FALSE; |
1464 | 0 | } |
1465 | | |
1466 | 0 | home_dir = g_build_filename (subdir, "home", NULL); |
1467 | 0 | cache_dir = g_build_filename (subdir, "cache", NULL); |
1468 | 0 | config_dir = g_build_filename (subdir, "config", NULL); |
1469 | 0 | data_dir = g_build_filename (subdir, "data", NULL); |
1470 | |
|
1471 | 0 | config_dirs[0] = g_build_filename (subdir, "system-config1", NULL); |
1472 | 0 | config_dirs[1] = g_build_filename (subdir, "system-config2", NULL); |
1473 | 0 | config_dirs[2] = NULL; |
1474 | |
|
1475 | 0 | data_dirs[0] = g_build_filename (subdir, "system-data1", NULL); |
1476 | 0 | data_dirs[1] = g_build_filename (subdir, "system-data2", NULL); |
1477 | 0 | data_dirs[2] = NULL; |
1478 | | |
1479 | | /* Remember to update the documentation for %G_TEST_OPTION_ISOLATE_DIRS if |
1480 | | * this list changes. */ |
1481 | 0 | g_set_user_dirs ("HOME", home_dir, |
1482 | 0 | "XDG_CACHE_HOME", cache_dir, |
1483 | 0 | "XDG_CONFIG_DIRS", config_dirs, |
1484 | 0 | "XDG_CONFIG_HOME", config_dir, |
1485 | 0 | "XDG_DATA_DIRS", data_dirs, |
1486 | 0 | "XDG_DATA_HOME", data_dir, |
1487 | 0 | "XDG_RUNTIME_DIR", runtime_dir, |
1488 | 0 | NULL); |
1489 | |
|
1490 | 0 | g_free (runtime_dir); |
1491 | 0 | g_free (data_dir); |
1492 | 0 | g_free (config_dir); |
1493 | 0 | g_free (cache_dir); |
1494 | 0 | g_free (home_dir); |
1495 | 0 | g_free (data_dirs[1]); |
1496 | 0 | g_free (data_dirs[0]); |
1497 | 0 | g_free (config_dirs[1]); |
1498 | 0 | g_free (config_dirs[0]); |
1499 | 0 | g_free (subdir); |
1500 | |
|
1501 | 0 | return TRUE; |
1502 | 0 | } |
1503 | | |
1504 | | /* Clean up after test_do_isolate_dirs(). */ |
1505 | | static void |
1506 | | test_rm_isolate_dirs (void) |
1507 | 0 | { |
1508 | 0 | gchar *subdir = NULL; |
1509 | |
|
1510 | 0 | if (!test_isolate_dirs) |
1511 | 0 | return; |
1512 | | |
1513 | 0 | subdir = g_build_filename (test_tmpdir, test_run_name, NULL); |
1514 | 0 | rm_rf (subdir); |
1515 | 0 | g_free (subdir); |
1516 | 0 | } |
1517 | | |
1518 | | /** |
1519 | | * g_test_init: |
1520 | | * @argc: Address of the @argc parameter of the main() function. |
1521 | | * Changed if any arguments were handled. |
1522 | | * @argv: Address of the @argv parameter of main(). |
1523 | | * Any parameters understood by g_test_init() stripped before return. |
1524 | | * @...: %NULL-terminated list of special options, documented below. |
1525 | | * |
1526 | | * Initialize the GLib testing framework, e.g. by seeding the |
1527 | | * test random number generator, the name for g_get_prgname() |
1528 | | * and parsing test related command line args. |
1529 | | * |
1530 | | * So far, the following arguments are understood: |
1531 | | * |
1532 | | * - `-l`: List test cases available in a test executable. |
1533 | | * - `--seed=SEED`: Provide a random seed to reproduce test |
1534 | | * runs using random numbers. |
1535 | | * - `--verbose`: Run tests verbosely. |
1536 | | * - `-q`, `--quiet`: Run tests quietly. |
1537 | | * - `-p PATH`: Execute all tests matching the given path. |
1538 | | * - `-s PATH`: Skip all tests matching the given path. |
1539 | | * This can also be used to force a test to run that would otherwise |
1540 | | * be skipped (ie, a test whose name contains "/subprocess"). |
1541 | | * - `-m {perf|slow|thorough|quick|undefined|no-undefined}`: Execute tests according to these test modes: |
1542 | | * |
1543 | | * `perf`: Performance tests, may take long and report results (off by default). |
1544 | | * |
1545 | | * `slow`, `thorough`: Slow and thorough tests, may take quite long and maximize coverage |
1546 | | * (off by default). |
1547 | | * |
1548 | | * `quick`: Quick tests, should run really quickly and give good coverage (the default). |
1549 | | * |
1550 | | * `undefined`: Tests for undefined behaviour, may provoke programming errors |
1551 | | * under g_test_trap_subprocess() or g_test_expect_message() to check |
1552 | | * that appropriate assertions or warnings are given (the default). |
1553 | | * |
1554 | | * `no-undefined`: Avoid tests for undefined behaviour |
1555 | | * |
1556 | | * - `--debug-log`: Debug test logging output. |
1557 | | * |
1558 | | * Options which can be passed to @... are: |
1559 | | * |
1560 | | * - `"no_g_set_prgname"`: Causes g_test_init() to not call g_set_prgname(). |
1561 | | * - %G_TEST_OPTION_ISOLATE_DIRS: Creates a unique temporary directory for each |
1562 | | * unit test and uses g_set_user_dirs() to set XDG directories to point into |
1563 | | * that temporary directory for the duration of the unit test. See the |
1564 | | * documentation for %G_TEST_OPTION_ISOLATE_DIRS. |
1565 | | * |
1566 | | * Since 2.58, if tests are compiled with `G_DISABLE_ASSERT` defined, |
1567 | | * g_test_init() will print an error and exit. This is to prevent no-op tests |
1568 | | * from being executed, as g_assert() is commonly (erroneously) used in unit |
1569 | | * tests, and is a no-op when compiled with `G_DISABLE_ASSERT`. Ensure your |
1570 | | * tests are compiled without `G_DISABLE_ASSERT` defined. |
1571 | | * |
1572 | | * Since: 2.16 |
1573 | | */ |
1574 | | void |
1575 | | (g_test_init) (int *argc, |
1576 | | char ***argv, |
1577 | | ...) |
1578 | 0 | { |
1579 | 0 | static char seedstr[4 + 4 * 8 + 1]; |
1580 | 0 | va_list args; |
1581 | 0 | gpointer option; |
1582 | | /* make warnings and criticals fatal for all test programs */ |
1583 | 0 | GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK); |
1584 | |
|
1585 | 0 | fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL); |
1586 | 0 | g_log_set_always_fatal (fatal_mask); |
1587 | | /* check caller args */ |
1588 | 0 | g_return_if_fail (argc != NULL); |
1589 | 0 | g_return_if_fail (argv != NULL); |
1590 | 0 | g_return_if_fail (g_test_config_vars->test_initialized == FALSE); |
1591 | 0 | mutable_test_config_vars.test_initialized = TRUE; |
1592 | |
|
1593 | | #ifdef _GLIB_ADDRESS_SANITIZER |
1594 | | mutable_test_config_vars.test_undefined = FALSE; |
1595 | | #endif |
1596 | |
|
1597 | 0 | va_start (args, argv); |
1598 | 0 | while ((option = va_arg (args, char *))) |
1599 | 0 | { |
1600 | 0 | if (g_strcmp0 (option, "no_g_set_prgname") == 0) |
1601 | 0 | no_g_set_prgname = TRUE; |
1602 | 0 | else if (g_strcmp0 (option, G_TEST_OPTION_ISOLATE_DIRS) == 0) |
1603 | 0 | test_isolate_dirs = TRUE; |
1604 | 0 | } |
1605 | 0 | va_end (args); |
1606 | | |
1607 | | /* setup random seed string */ |
1608 | 0 | g_snprintf (seedstr, sizeof (seedstr), "R02S%08x%08x%08x%08x", g_random_int(), g_random_int(), g_random_int(), g_random_int()); |
1609 | 0 | test_run_seedstr = seedstr; |
1610 | | |
1611 | | /* parse args, sets up mode, changes seed, etc. */ |
1612 | 0 | parse_args (argc, argv); |
1613 | |
|
1614 | 0 | if (!g_get_prgname() && !no_g_set_prgname) |
1615 | 0 | g_set_prgname ((*argv)[0]); |
1616 | | |
1617 | | /* Set up the temporary directory for isolating the test. We have to do this |
1618 | | * early, as we want the return values from g_get_user_data_dir() (and |
1619 | | * friends) to return subdirectories of the temporary directory throughout |
1620 | | * the setup function, test, and teardown function, for each unit test. |
1621 | | * See test_do_isolate_dirs(). |
1622 | | * |
1623 | | * The directory is deleted at the bottom of g_test_run(). |
1624 | | * |
1625 | | * Rather than setting the XDG_* environment variables we use a new |
1626 | | * G_TEST_TMPDIR variable which gives the top-level temporary directory. This |
1627 | | * allows test subprocesses to reuse the same temporary directory when |
1628 | | * g_test_init() is called in them. */ |
1629 | 0 | if (test_isolate_dirs) |
1630 | 0 | { |
1631 | 0 | if (g_getenv ("G_TEST_TMPDIR") == NULL) |
1632 | 0 | { |
1633 | 0 | gchar *test_prgname = NULL; |
1634 | 0 | gchar *tmpl = NULL; |
1635 | 0 | GError *local_error = NULL; |
1636 | |
|
1637 | 0 | test_prgname = g_path_get_basename (g_get_prgname ()); |
1638 | 0 | if (*test_prgname == '\0') |
1639 | 0 | { |
1640 | 0 | g_free (test_prgname); |
1641 | 0 | test_prgname = g_strdup ("unknown"); |
1642 | 0 | } |
1643 | 0 | tmpl = g_strdup_printf ("test_%s_XXXXXX", test_prgname); |
1644 | 0 | g_free (test_prgname); |
1645 | |
|
1646 | 0 | test_isolate_dirs_tmpdir = g_dir_make_tmp (tmpl, &local_error); |
1647 | 0 | if (local_error != NULL) |
1648 | 0 | { |
1649 | 0 | g_printerr ("%s: Failed to create temporary directory: %s\n", |
1650 | 0 | (*argv)[0], local_error->message); |
1651 | 0 | g_error_free (local_error); |
1652 | 0 | exit (1); |
1653 | 0 | } |
1654 | 0 | g_free (tmpl); |
1655 | | |
1656 | | /* Propagate the temporary directory to subprocesses. */ |
1657 | 0 | g_setenv ("G_TEST_TMPDIR", test_isolate_dirs_tmpdir, TRUE); |
1658 | | |
1659 | | /* And clear the traditional environment variables so subprocesses |
1660 | | * spawned by the code under test can’t trash anything. If a test |
1661 | | * spawns a process, the test is responsible for propagating |
1662 | | * appropriate environment variables. |
1663 | | * |
1664 | | * We assume that any in-process code will use g_get_user_data_dir() |
1665 | | * and friends, rather than getenv() directly. |
1666 | | * |
1667 | | * We set them to ‘/dev/null’ as that should fairly obviously not |
1668 | | * accidentally work, and should be fairly greppable. */ |
1669 | 0 | { |
1670 | 0 | const gchar *overridden_environment_variables[] = |
1671 | 0 | { |
1672 | 0 | "HOME", |
1673 | 0 | "XDG_CACHE_HOME", |
1674 | 0 | "XDG_CONFIG_DIRS", |
1675 | 0 | "XDG_CONFIG_HOME", |
1676 | 0 | "XDG_DATA_DIRS", |
1677 | 0 | "XDG_DATA_HOME", |
1678 | 0 | "XDG_RUNTIME_DIR", |
1679 | 0 | }; |
1680 | 0 | gsize i; |
1681 | |
|
1682 | 0 | for (i = 0; i < G_N_ELEMENTS (overridden_environment_variables); i++) |
1683 | 0 | g_setenv (overridden_environment_variables[i], "/dev/null", TRUE); |
1684 | 0 | } |
1685 | 0 | } |
1686 | | |
1687 | | /* Cache this for the remainder of this process’ lifetime. */ |
1688 | 0 | test_tmpdir = g_getenv ("G_TEST_TMPDIR"); |
1689 | 0 | } |
1690 | | |
1691 | | /* verify GRand reliability, needed for reliable seeds */ |
1692 | 0 | if (1) |
1693 | 0 | { |
1694 | 0 | GRand *rg = g_rand_new_with_seed (0xc8c49fb6); |
1695 | 0 | guint32 t1 = g_rand_int (rg), t2 = g_rand_int (rg), t3 = g_rand_int (rg), t4 = g_rand_int (rg); |
1696 | | /* g_print ("GRand-current: 0x%x 0x%x 0x%x 0x%x\n", t1, t2, t3, t4); */ |
1697 | 0 | if (t1 != 0xfab39f9b || t2 != 0xb948fb0e || t3 != 0x3d31be26 || t4 != 0x43a19d66) |
1698 | 0 | g_warning ("random numbers are not GRand-2.2 compatible, seeds may be broken (check $G_RANDOM_VERSION)"); |
1699 | 0 | g_rand_free (rg); |
1700 | 0 | } |
1701 | | |
1702 | | /* check rand seed */ |
1703 | 0 | test_run_seed (test_run_seedstr); |
1704 | | |
1705 | | /* report program start */ |
1706 | 0 | g_log_set_default_handler (gtest_default_log_handler, NULL); |
1707 | 0 | g_test_log (G_TEST_LOG_START_BINARY, g_get_prgname(), test_run_seedstr, 0, NULL); |
1708 | |
|
1709 | 0 | test_argv0_dirname = g_path_get_dirname (test_argv0); |
1710 | | |
1711 | | /* Make sure we get the real dirname that the test was run from */ |
1712 | 0 | if (g_str_has_suffix (test_argv0_dirname, "/.libs")) |
1713 | 0 | { |
1714 | 0 | gchar *tmp; |
1715 | 0 | tmp = g_path_get_dirname (test_argv0_dirname); |
1716 | 0 | g_free (test_argv0_dirname); |
1717 | 0 | test_argv0_dirname = tmp; |
1718 | 0 | } |
1719 | |
|
1720 | 0 | test_disted_files_dir = g_getenv ("G_TEST_SRCDIR"); |
1721 | 0 | if (!test_disted_files_dir) |
1722 | 0 | test_disted_files_dir = test_argv0_dirname; |
1723 | |
|
1724 | 0 | test_built_files_dir = g_getenv ("G_TEST_BUILDDIR"); |
1725 | 0 | if (!test_built_files_dir) |
1726 | 0 | test_built_files_dir = test_argv0_dirname; |
1727 | 0 | } |
1728 | | |
1729 | | static void |
1730 | | test_run_seed (const gchar *rseed) |
1731 | 0 | { |
1732 | 0 | guint seed_failed = 0; |
1733 | 0 | if (test_run_rand) |
1734 | 0 | g_rand_free (test_run_rand); |
1735 | 0 | test_run_rand = NULL; |
1736 | 0 | while (strchr (" \t\v\r\n\f", *rseed)) |
1737 | 0 | rseed++; |
1738 | 0 | if (strncmp (rseed, "R02S", 4) == 0) /* seed for random generator 02 (GRand-2.2) */ |
1739 | 0 | { |
1740 | 0 | const char *s = rseed + 4; |
1741 | 0 | if (strlen (s) >= 32) /* require 4 * 8 chars */ |
1742 | 0 | { |
1743 | 0 | guint32 seedarray[4]; |
1744 | 0 | gchar *p, hexbuf[9] = { 0, }; |
1745 | 0 | memcpy (hexbuf, s + 0, 8); |
1746 | 0 | seedarray[0] = g_ascii_strtoull (hexbuf, &p, 16); |
1747 | 0 | seed_failed += p != NULL && *p != 0; |
1748 | 0 | memcpy (hexbuf, s + 8, 8); |
1749 | 0 | seedarray[1] = g_ascii_strtoull (hexbuf, &p, 16); |
1750 | 0 | seed_failed += p != NULL && *p != 0; |
1751 | 0 | memcpy (hexbuf, s + 16, 8); |
1752 | 0 | seedarray[2] = g_ascii_strtoull (hexbuf, &p, 16); |
1753 | 0 | seed_failed += p != NULL && *p != 0; |
1754 | 0 | memcpy (hexbuf, s + 24, 8); |
1755 | 0 | seedarray[3] = g_ascii_strtoull (hexbuf, &p, 16); |
1756 | 0 | seed_failed += p != NULL && *p != 0; |
1757 | 0 | if (!seed_failed) |
1758 | 0 | { |
1759 | 0 | test_run_rand = g_rand_new_with_seed_array (seedarray, 4); |
1760 | 0 | return; |
1761 | 0 | } |
1762 | 0 | } |
1763 | 0 | } |
1764 | 0 | g_error ("Unknown or invalid random seed: %s", rseed); |
1765 | 0 | } |
1766 | | |
1767 | | /** |
1768 | | * g_test_rand_int: |
1769 | | * |
1770 | | * Get a reproducible random integer number. |
1771 | | * |
1772 | | * The random numbers generated by the g_test_rand_*() family of functions |
1773 | | * change with every new test program start, unless the --seed option is |
1774 | | * given when starting test programs. |
1775 | | * |
1776 | | * For individual test cases however, the random number generator is |
1777 | | * reseeded, to avoid dependencies between tests and to make --seed |
1778 | | * effective for all test cases. |
1779 | | * |
1780 | | * Returns: a random number from the seeded random number generator. |
1781 | | * |
1782 | | * Since: 2.16 |
1783 | | */ |
1784 | | gint32 |
1785 | | g_test_rand_int (void) |
1786 | 0 | { |
1787 | 0 | gint32 r; |
1788 | |
|
1789 | 0 | G_LOCK (test_run_rand); |
1790 | 0 | r = g_rand_int (test_run_rand); |
1791 | 0 | G_UNLOCK (test_run_rand); |
1792 | |
|
1793 | 0 | return r; |
1794 | 0 | } |
1795 | | |
1796 | | /** |
1797 | | * g_test_rand_int_range: |
1798 | | * @begin: the minimum value returned by this function |
1799 | | * @end: the smallest value not to be returned by this function |
1800 | | * |
1801 | | * Get a reproducible random integer number out of a specified range, |
1802 | | * see g_test_rand_int() for details on test case random numbers. |
1803 | | * |
1804 | | * Returns: a number with @begin <= number < @end. |
1805 | | * |
1806 | | * Since: 2.16 |
1807 | | */ |
1808 | | gint32 |
1809 | | g_test_rand_int_range (gint32 begin, |
1810 | | gint32 end) |
1811 | 0 | { |
1812 | 0 | gint32 r; |
1813 | |
|
1814 | 0 | G_LOCK (test_run_rand); |
1815 | 0 | r = g_rand_int_range (test_run_rand, begin, end); |
1816 | 0 | G_UNLOCK (test_run_rand); |
1817 | |
|
1818 | 0 | return r; |
1819 | 0 | } |
1820 | | |
1821 | | /** |
1822 | | * g_test_rand_double: |
1823 | | * |
1824 | | * Get a reproducible random floating point number, |
1825 | | * see g_test_rand_int() for details on test case random numbers. |
1826 | | * |
1827 | | * Returns: a random number from the seeded random number generator. |
1828 | | * |
1829 | | * Since: 2.16 |
1830 | | */ |
1831 | | double |
1832 | | g_test_rand_double (void) |
1833 | 0 | { |
1834 | 0 | double r; |
1835 | |
|
1836 | 0 | G_LOCK (test_run_rand); |
1837 | 0 | r = g_rand_double (test_run_rand); |
1838 | 0 | G_UNLOCK (test_run_rand); |
1839 | |
|
1840 | 0 | return r; |
1841 | 0 | } |
1842 | | |
1843 | | /** |
1844 | | * g_test_rand_double_range: |
1845 | | * @range_start: the minimum value returned by this function |
1846 | | * @range_end: the minimum value not returned by this function |
1847 | | * |
1848 | | * Get a reproducible random floating pointer number out of a specified range, |
1849 | | * see g_test_rand_int() for details on test case random numbers. |
1850 | | * |
1851 | | * Returns: a number with @range_start <= number < @range_end. |
1852 | | * |
1853 | | * Since: 2.16 |
1854 | | */ |
1855 | | double |
1856 | | g_test_rand_double_range (double range_start, |
1857 | | double range_end) |
1858 | 0 | { |
1859 | 0 | double r; |
1860 | |
|
1861 | 0 | G_LOCK (test_run_rand); |
1862 | 0 | r = g_rand_double_range (test_run_rand, range_start, range_end); |
1863 | 0 | G_UNLOCK (test_run_rand); |
1864 | |
|
1865 | 0 | return r; |
1866 | 0 | } |
1867 | | |
1868 | | /** |
1869 | | * g_test_timer_start: |
1870 | | * |
1871 | | * Start a timing test. Call g_test_timer_elapsed() when the task is supposed |
1872 | | * to be done. Call this function again to restart the timer. |
1873 | | * |
1874 | | * Since: 2.16 |
1875 | | */ |
1876 | | void |
1877 | | g_test_timer_start (void) |
1878 | 0 | { |
1879 | 0 | if (!test_user_timer) |
1880 | 0 | test_user_timer = g_timer_new(); |
1881 | 0 | test_user_stamp = 0; |
1882 | 0 | g_timer_start (test_user_timer); |
1883 | 0 | } |
1884 | | |
1885 | | /** |
1886 | | * g_test_timer_elapsed: |
1887 | | * |
1888 | | * Get the time since the last start of the timer with g_test_timer_start(). |
1889 | | * |
1890 | | * Returns: the time since the last start of the timer, as a double |
1891 | | * |
1892 | | * Since: 2.16 |
1893 | | */ |
1894 | | double |
1895 | | g_test_timer_elapsed (void) |
1896 | 0 | { |
1897 | 0 | test_user_stamp = test_user_timer ? g_timer_elapsed (test_user_timer, NULL) : 0; |
1898 | 0 | return test_user_stamp; |
1899 | 0 | } |
1900 | | |
1901 | | /** |
1902 | | * g_test_timer_last: |
1903 | | * |
1904 | | * Report the last result of g_test_timer_elapsed(). |
1905 | | * |
1906 | | * Returns: the last result of g_test_timer_elapsed(), as a double |
1907 | | * |
1908 | | * Since: 2.16 |
1909 | | */ |
1910 | | double |
1911 | | g_test_timer_last (void) |
1912 | 0 | { |
1913 | 0 | return test_user_stamp; |
1914 | 0 | } |
1915 | | |
1916 | | /** |
1917 | | * g_test_minimized_result: |
1918 | | * @minimized_quantity: the reported value |
1919 | | * @format: the format string of the report message |
1920 | | * @...: arguments to pass to the printf() function |
1921 | | * |
1922 | | * Report the result of a performance or measurement test. |
1923 | | * The test should generally strive to minimize the reported |
1924 | | * quantities (smaller values are better than larger ones), |
1925 | | * this and @minimized_quantity can determine sorting |
1926 | | * order for test result reports. |
1927 | | * |
1928 | | * Since: 2.16 |
1929 | | */ |
1930 | | void |
1931 | | g_test_minimized_result (double minimized_quantity, |
1932 | | const char *format, |
1933 | | ...) |
1934 | 0 | { |
1935 | 0 | long double largs = minimized_quantity; |
1936 | 0 | gchar *buffer; |
1937 | 0 | va_list args; |
1938 | |
|
1939 | 0 | va_start (args, format); |
1940 | 0 | buffer = g_strdup_vprintf (format, args); |
1941 | 0 | va_end (args); |
1942 | |
|
1943 | 0 | g_test_log (G_TEST_LOG_MIN_RESULT, buffer, NULL, 1, &largs); |
1944 | 0 | g_free (buffer); |
1945 | 0 | } |
1946 | | |
1947 | | /** |
1948 | | * g_test_maximized_result: |
1949 | | * @maximized_quantity: the reported value |
1950 | | * @format: the format string of the report message |
1951 | | * @...: arguments to pass to the printf() function |
1952 | | * |
1953 | | * Report the result of a performance or measurement test. |
1954 | | * The test should generally strive to maximize the reported |
1955 | | * quantities (larger values are better than smaller ones), |
1956 | | * this and @maximized_quantity can determine sorting |
1957 | | * order for test result reports. |
1958 | | * |
1959 | | * Since: 2.16 |
1960 | | */ |
1961 | | void |
1962 | | g_test_maximized_result (double maximized_quantity, |
1963 | | const char *format, |
1964 | | ...) |
1965 | 0 | { |
1966 | 0 | long double largs = maximized_quantity; |
1967 | 0 | gchar *buffer; |
1968 | 0 | va_list args; |
1969 | |
|
1970 | 0 | va_start (args, format); |
1971 | 0 | buffer = g_strdup_vprintf (format, args); |
1972 | 0 | va_end (args); |
1973 | |
|
1974 | 0 | g_test_log (G_TEST_LOG_MAX_RESULT, buffer, NULL, 1, &largs); |
1975 | 0 | g_free (buffer); |
1976 | 0 | } |
1977 | | |
1978 | | /** |
1979 | | * g_test_message: |
1980 | | * @format: the format string |
1981 | | * @...: printf-like arguments to @format |
1982 | | * |
1983 | | * Add a message to the test report. |
1984 | | * |
1985 | | * Since: 2.16 |
1986 | | */ |
1987 | | void |
1988 | | g_test_message (const char *format, |
1989 | | ...) |
1990 | 0 | { |
1991 | 0 | gchar *buffer; |
1992 | 0 | va_list args; |
1993 | |
|
1994 | 0 | va_start (args, format); |
1995 | 0 | buffer = g_strdup_vprintf (format, args); |
1996 | 0 | va_end (args); |
1997 | |
|
1998 | 0 | g_test_log (G_TEST_LOG_MESSAGE, buffer, NULL, 0, NULL); |
1999 | 0 | g_free (buffer); |
2000 | 0 | } |
2001 | | |
2002 | | /** |
2003 | | * g_test_bug_base: |
2004 | | * @uri_pattern: the base pattern for bug URIs |
2005 | | * |
2006 | | * Specify the base URI for bug reports. |
2007 | | * |
2008 | | * The base URI is used to construct bug report messages for |
2009 | | * g_test_message() when g_test_bug() is called. |
2010 | | * Calling this function outside of a test case sets the |
2011 | | * default base URI for all test cases. Calling it from within |
2012 | | * a test case changes the base URI for the scope of the test |
2013 | | * case only. |
2014 | | * Bug URIs are constructed by appending a bug specific URI |
2015 | | * portion to @uri_pattern, or by replacing the special string |
2016 | | * '\%s' within @uri_pattern if that is present. |
2017 | | * |
2018 | | * If g_test_bug_base() is not called, bug URIs are formed solely |
2019 | | * from the value provided by g_test_bug(). |
2020 | | * |
2021 | | * Since: 2.16 |
2022 | | */ |
2023 | | void |
2024 | | g_test_bug_base (const char *uri_pattern) |
2025 | 0 | { |
2026 | 0 | g_free (test_uri_base); |
2027 | 0 | test_uri_base = g_strdup (uri_pattern); |
2028 | 0 | } |
2029 | | |
2030 | | /** |
2031 | | * g_test_bug: |
2032 | | * @bug_uri_snippet: Bug specific bug tracker URI portion. |
2033 | | * |
2034 | | * This function adds a message to test reports that |
2035 | | * associates a bug URI with a test case. |
2036 | | * Bug URIs are constructed from a base URI set with g_test_bug_base() |
2037 | | * and @bug_uri_snippet. If g_test_bug_base() has not been called, it is |
2038 | | * assumed to be the empty string, so a full URI can be provided to |
2039 | | * g_test_bug() instead. |
2040 | | * |
2041 | | * Since: 2.16 |
2042 | | * See also: g_test_summary() |
2043 | | */ |
2044 | | void |
2045 | | g_test_bug (const char *bug_uri_snippet) |
2046 | 0 | { |
2047 | 0 | const char *c = NULL; |
2048 | |
|
2049 | 0 | g_return_if_fail (bug_uri_snippet != NULL); |
2050 | | |
2051 | 0 | if (test_uri_base != NULL) |
2052 | 0 | c = strstr (test_uri_base, "%s"); |
2053 | 0 | if (c) |
2054 | 0 | { |
2055 | 0 | char *b = g_strndup (test_uri_base, c - test_uri_base); |
2056 | 0 | char *s = g_strconcat (b, bug_uri_snippet, c + 2, NULL); |
2057 | 0 | g_free (b); |
2058 | 0 | g_test_message ("Bug Reference: %s", s); |
2059 | 0 | g_free (s); |
2060 | 0 | } |
2061 | 0 | else |
2062 | 0 | g_test_message ("Bug Reference: %s%s", |
2063 | 0 | test_uri_base ? test_uri_base : "", bug_uri_snippet); |
2064 | 0 | } |
2065 | | |
2066 | | /** |
2067 | | * g_test_summary: |
2068 | | * @summary: One or two sentences summarising what the test checks, and how it |
2069 | | * checks it. |
2070 | | * |
2071 | | * Set the summary for a test, which describes what the test checks, and how it |
2072 | | * goes about checking it. This may be included in test report output, and is |
2073 | | * useful documentation for anyone reading the source code or modifying a test |
2074 | | * in future. It must be a single line. |
2075 | | * |
2076 | | * This should be called at the top of a test function. |
2077 | | * |
2078 | | * For example: |
2079 | | * |[<!-- language="C" --> |
2080 | | * static void |
2081 | | * test_array_sort (void) |
2082 | | * { |
2083 | | * g_test_summary ("Test my_array_sort() sorts the array correctly and stably, " |
2084 | | * "including testing zero length and one-element arrays."); |
2085 | | * |
2086 | | * … |
2087 | | * } |
2088 | | * ]| |
2089 | | * |
2090 | | * Since: 2.62 |
2091 | | * See also: g_test_bug() |
2092 | | */ |
2093 | | void |
2094 | | g_test_summary (const char *summary) |
2095 | 0 | { |
2096 | 0 | g_return_if_fail (summary != NULL); |
2097 | 0 | g_return_if_fail (strchr (summary, '\n') == NULL); |
2098 | 0 | g_return_if_fail (strchr (summary, '\r') == NULL); |
2099 | | |
2100 | 0 | g_test_message ("%s summary: %s", test_run_name, summary); |
2101 | 0 | } |
2102 | | |
2103 | | /** |
2104 | | * g_test_get_root: |
2105 | | * |
2106 | | * Get the toplevel test suite for the test path API. |
2107 | | * |
2108 | | * Returns: the toplevel #GTestSuite |
2109 | | * |
2110 | | * Since: 2.16 |
2111 | | */ |
2112 | | GTestSuite* |
2113 | | g_test_get_root (void) |
2114 | 0 | { |
2115 | 0 | if (!test_suite_root) |
2116 | 0 | { |
2117 | 0 | test_suite_root = g_test_create_suite ("root"); |
2118 | 0 | g_free (test_suite_root->name); |
2119 | 0 | test_suite_root->name = g_strdup (""); |
2120 | 0 | } |
2121 | |
|
2122 | 0 | return test_suite_root; |
2123 | 0 | } |
2124 | | |
2125 | | /** |
2126 | | * g_test_run: |
2127 | | * |
2128 | | * Runs all tests under the toplevel suite which can be retrieved |
2129 | | * with g_test_get_root(). Similar to g_test_run_suite(), the test |
2130 | | * cases to be run are filtered according to test path arguments |
2131 | | * (`-p testpath` and `-s testpath`) as parsed by g_test_init(). |
2132 | | * g_test_run_suite() or g_test_run() may only be called once in a |
2133 | | * program. |
2134 | | * |
2135 | | * In general, the tests and sub-suites within each suite are run in |
2136 | | * the order in which they are defined. However, note that prior to |
2137 | | * GLib 2.36, there was a bug in the `g_test_add_*` |
2138 | | * functions which caused them to create multiple suites with the same |
2139 | | * name, meaning that if you created tests "/foo/simple", |
2140 | | * "/bar/simple", and "/foo/using-bar" in that order, they would get |
2141 | | * run in that order (since g_test_run() would run the first "/foo" |
2142 | | * suite, then the "/bar" suite, then the second "/foo" suite). As of |
2143 | | * 2.36, this bug is fixed, and adding the tests in that order would |
2144 | | * result in a running order of "/foo/simple", "/foo/using-bar", |
2145 | | * "/bar/simple". If this new ordering is sub-optimal (because it puts |
2146 | | * more-complicated tests before simpler ones, making it harder to |
2147 | | * figure out exactly what has failed), you can fix it by changing the |
2148 | | * test paths to group tests by suite in a way that will result in the |
2149 | | * desired running order. Eg, "/simple/foo", "/simple/bar", |
2150 | | * "/complex/foo-using-bar". |
2151 | | * |
2152 | | * However, you should never make the actual result of a test depend |
2153 | | * on the order that tests are run in. If you need to ensure that some |
2154 | | * particular code runs before or after a given test case, use |
2155 | | * g_test_add(), which lets you specify setup and teardown functions. |
2156 | | * |
2157 | | * If all tests are skipped or marked as incomplete (expected failures), |
2158 | | * this function will return 0 if producing TAP output, or 77 (treated |
2159 | | * as "skip test" by Automake) otherwise. |
2160 | | * |
2161 | | * Returns: 0 on success, 1 on failure (assuming it returns at all), |
2162 | | * 0 or 77 if all tests were skipped with g_test_skip() and/or |
2163 | | * g_test_incomplete() |
2164 | | * |
2165 | | * Since: 2.16 |
2166 | | */ |
2167 | | int |
2168 | | g_test_run (void) |
2169 | 0 | { |
2170 | 0 | if (g_test_run_suite (g_test_get_root()) != 0) |
2171 | 0 | return 1; |
2172 | | |
2173 | | /* Clean up the temporary directory. */ |
2174 | 0 | if (test_isolate_dirs_tmpdir != NULL) |
2175 | 0 | { |
2176 | 0 | rm_rf (test_isolate_dirs_tmpdir); |
2177 | 0 | g_free (test_isolate_dirs_tmpdir); |
2178 | 0 | test_isolate_dirs_tmpdir = NULL; |
2179 | 0 | } |
2180 | | |
2181 | | /* 77 is special to Automake's default driver, but not Automake's TAP driver |
2182 | | * or Perl's prove(1) TAP driver. */ |
2183 | 0 | if (test_tap_log) |
2184 | 0 | return 0; |
2185 | | |
2186 | 0 | if (test_run_count > 0 && test_run_count == test_skipped_count) |
2187 | 0 | return 77; |
2188 | 0 | else |
2189 | 0 | return 0; |
2190 | 0 | } |
2191 | | |
2192 | | /** |
2193 | | * g_test_create_case: |
2194 | | * @test_name: the name for the test case |
2195 | | * @data_size: the size of the fixture data structure |
2196 | | * @test_data: test data argument for the test functions |
2197 | | * @data_setup: (scope async): the function to set up the fixture data |
2198 | | * @data_test: (scope async): the actual test function |
2199 | | * @data_teardown: (scope async): the function to teardown the fixture data |
2200 | | * |
2201 | | * Create a new #GTestCase, named @test_name. |
2202 | | * |
2203 | | * This API is fairly low level, and calling g_test_add() or g_test_add_func() |
2204 | | * is preferable. |
2205 | | * |
2206 | | * When this test is executed, a fixture structure of size @data_size |
2207 | | * will be automatically allocated and filled with zeros. Then @data_setup is |
2208 | | * called to initialize the fixture. After fixture setup, the actual test |
2209 | | * function @data_test is called. Once the test run completes, the |
2210 | | * fixture structure is torn down by calling @data_teardown and |
2211 | | * after that the memory is automatically released by the test framework. |
2212 | | * |
2213 | | * Splitting up a test run into fixture setup, test function and |
2214 | | * fixture teardown is most useful if the same fixture type is used for |
2215 | | * multiple tests. In this cases, g_test_create_case() will be |
2216 | | * called with the same type of fixture (the @data_size argument), but varying |
2217 | | * @test_name and @data_test arguments. |
2218 | | * |
2219 | | * Returns: a newly allocated #GTestCase. |
2220 | | * |
2221 | | * Since: 2.16 |
2222 | | */ |
2223 | | GTestCase* |
2224 | | g_test_create_case (const char *test_name, |
2225 | | gsize data_size, |
2226 | | gconstpointer test_data, |
2227 | | GTestFixtureFunc data_setup, |
2228 | | GTestFixtureFunc data_test, |
2229 | | GTestFixtureFunc data_teardown) |
2230 | 0 | { |
2231 | 0 | GTestCase *tc; |
2232 | |
|
2233 | 0 | g_return_val_if_fail (test_name != NULL, NULL); |
2234 | 0 | g_return_val_if_fail (strchr (test_name, '/') == NULL, NULL); |
2235 | 0 | g_return_val_if_fail (test_name[0] != 0, NULL); |
2236 | 0 | g_return_val_if_fail (data_test != NULL, NULL); |
2237 | | |
2238 | 0 | tc = g_slice_new0 (GTestCase); |
2239 | 0 | tc->name = g_strdup (test_name); |
2240 | 0 | tc->test_data = (gpointer) test_data; |
2241 | 0 | tc->fixture_size = data_size; |
2242 | 0 | tc->fixture_setup = (void*) data_setup; |
2243 | 0 | tc->fixture_test = (void*) data_test; |
2244 | 0 | tc->fixture_teardown = (void*) data_teardown; |
2245 | |
|
2246 | 0 | return tc; |
2247 | 0 | } |
2248 | | |
2249 | | static gint |
2250 | | find_suite (gconstpointer l, gconstpointer s) |
2251 | 0 | { |
2252 | 0 | const GTestSuite *suite = l; |
2253 | 0 | const gchar *str = s; |
2254 | |
|
2255 | 0 | return strcmp (suite->name, str); |
2256 | 0 | } |
2257 | | |
2258 | | static gint |
2259 | | find_case (gconstpointer l, gconstpointer s) |
2260 | 0 | { |
2261 | 0 | const GTestCase *tc = l; |
2262 | 0 | const gchar *str = s; |
2263 | |
|
2264 | 0 | return strcmp (tc->name, str); |
2265 | 0 | } |
2266 | | |
2267 | | /** |
2268 | | * GTestFixtureFunc: |
2269 | | * @fixture: (not nullable): the test fixture |
2270 | | * @user_data: the data provided when registering the test |
2271 | | * |
2272 | | * The type used for functions that operate on test fixtures. This is |
2273 | | * used for the fixture setup and teardown functions as well as for the |
2274 | | * testcases themselves. |
2275 | | * |
2276 | | * @user_data is a pointer to the data that was given when registering |
2277 | | * the test case. |
2278 | | * |
2279 | | * @fixture will be a pointer to the area of memory allocated by the |
2280 | | * test framework, of the size requested. If the requested size was |
2281 | | * zero then @fixture will be equal to @user_data. |
2282 | | * |
2283 | | * Since: 2.28 |
2284 | | */ |
2285 | | void |
2286 | | g_test_add_vtable (const char *testpath, |
2287 | | gsize data_size, |
2288 | | gconstpointer test_data, |
2289 | | GTestFixtureFunc data_setup, |
2290 | | GTestFixtureFunc fixture_test_func, |
2291 | | GTestFixtureFunc data_teardown) |
2292 | 0 | { |
2293 | 0 | gchar **segments; |
2294 | 0 | guint ui; |
2295 | 0 | GTestSuite *suite; |
2296 | |
|
2297 | 0 | g_return_if_fail (testpath != NULL); |
2298 | 0 | g_return_if_fail (g_path_is_absolute (testpath)); |
2299 | 0 | g_return_if_fail (fixture_test_func != NULL); |
2300 | 0 | g_return_if_fail (!test_isolate_dirs || strstr (testpath, "/.") == NULL); |
2301 | | |
2302 | 0 | suite = g_test_get_root(); |
2303 | 0 | segments = g_strsplit (testpath, "/", -1); |
2304 | 0 | for (ui = 0; segments[ui] != NULL; ui++) |
2305 | 0 | { |
2306 | 0 | const char *seg = segments[ui]; |
2307 | 0 | gboolean islast = segments[ui + 1] == NULL; |
2308 | 0 | if (islast && !seg[0]) |
2309 | 0 | g_error ("invalid test case path: %s", testpath); |
2310 | 0 | else if (!seg[0]) |
2311 | 0 | continue; /* initial or duplicate slash */ |
2312 | 0 | else if (!islast) |
2313 | 0 | { |
2314 | 0 | GSList *l; |
2315 | 0 | GTestSuite *csuite; |
2316 | 0 | l = g_slist_find_custom (suite->suites, seg, find_suite); |
2317 | 0 | if (l) |
2318 | 0 | { |
2319 | 0 | csuite = l->data; |
2320 | 0 | } |
2321 | 0 | else |
2322 | 0 | { |
2323 | 0 | csuite = g_test_create_suite (seg); |
2324 | 0 | g_test_suite_add_suite (suite, csuite); |
2325 | 0 | } |
2326 | 0 | suite = csuite; |
2327 | 0 | } |
2328 | 0 | else /* islast */ |
2329 | 0 | { |
2330 | 0 | GTestCase *tc; |
2331 | |
|
2332 | 0 | if (g_slist_find_custom (suite->cases, seg, find_case)) |
2333 | 0 | g_error ("duplicate test case path: %s", testpath); |
2334 | |
|
2335 | 0 | tc = g_test_create_case (seg, data_size, test_data, data_setup, fixture_test_func, data_teardown); |
2336 | 0 | g_test_suite_add (suite, tc); |
2337 | 0 | } |
2338 | 0 | } |
2339 | 0 | g_strfreev (segments); |
2340 | 0 | } |
2341 | | |
2342 | | /** |
2343 | | * g_test_fail: |
2344 | | * |
2345 | | * Indicates that a test failed. This function can be called |
2346 | | * multiple times from the same test. You can use this function |
2347 | | * if your test failed in a recoverable way. |
2348 | | * |
2349 | | * Do not use this function if the failure of a test could cause |
2350 | | * other tests to malfunction. |
2351 | | * |
2352 | | * Calling this function will not stop the test from running, you |
2353 | | * need to return from the test function yourself. So you can |
2354 | | * produce additional diagnostic messages or even continue running |
2355 | | * the test. |
2356 | | * |
2357 | | * If not called from inside a test, this function does nothing. |
2358 | | * |
2359 | | * Since: 2.30 |
2360 | | **/ |
2361 | | void |
2362 | | g_test_fail (void) |
2363 | 0 | { |
2364 | 0 | test_run_success = G_TEST_RUN_FAILURE; |
2365 | 0 | } |
2366 | | |
2367 | | /** |
2368 | | * g_test_incomplete: |
2369 | | * @msg: (nullable): explanation |
2370 | | * |
2371 | | * Indicates that a test failed because of some incomplete |
2372 | | * functionality. This function can be called multiple times |
2373 | | * from the same test. |
2374 | | * |
2375 | | * Calling this function will not stop the test from running, you |
2376 | | * need to return from the test function yourself. So you can |
2377 | | * produce additional diagnostic messages or even continue running |
2378 | | * the test. |
2379 | | * |
2380 | | * If not called from inside a test, this function does nothing. |
2381 | | * |
2382 | | * Since: 2.38 |
2383 | | */ |
2384 | | void |
2385 | | g_test_incomplete (const gchar *msg) |
2386 | 0 | { |
2387 | 0 | test_run_success = G_TEST_RUN_INCOMPLETE; |
2388 | 0 | g_free (test_run_msg); |
2389 | 0 | test_run_msg = g_strdup (msg); |
2390 | 0 | } |
2391 | | |
2392 | | /** |
2393 | | * g_test_skip: |
2394 | | * @msg: (nullable): explanation |
2395 | | * |
2396 | | * Indicates that a test was skipped. |
2397 | | * |
2398 | | * Calling this function will not stop the test from running, you |
2399 | | * need to return from the test function yourself. So you can |
2400 | | * produce additional diagnostic messages or even continue running |
2401 | | * the test. |
2402 | | * |
2403 | | * If not called from inside a test, this function does nothing. |
2404 | | * |
2405 | | * Since: 2.38 |
2406 | | */ |
2407 | | void |
2408 | | g_test_skip (const gchar *msg) |
2409 | 0 | { |
2410 | 0 | test_run_success = G_TEST_RUN_SKIPPED; |
2411 | 0 | g_free (test_run_msg); |
2412 | 0 | test_run_msg = g_strdup (msg); |
2413 | 0 | } |
2414 | | |
2415 | | /** |
2416 | | * g_test_failed: |
2417 | | * |
2418 | | * Returns whether a test has already failed. This will |
2419 | | * be the case when g_test_fail(), g_test_incomplete() |
2420 | | * or g_test_skip() have been called, but also if an |
2421 | | * assertion has failed. |
2422 | | * |
2423 | | * This can be useful to return early from a test if |
2424 | | * continuing after a failed assertion might be harmful. |
2425 | | * |
2426 | | * The return value of this function is only meaningful |
2427 | | * if it is called from inside a test function. |
2428 | | * |
2429 | | * Returns: %TRUE if the test has failed |
2430 | | * |
2431 | | * Since: 2.38 |
2432 | | */ |
2433 | | gboolean |
2434 | | g_test_failed (void) |
2435 | 0 | { |
2436 | 0 | return test_run_success != G_TEST_RUN_SUCCESS; |
2437 | 0 | } |
2438 | | |
2439 | | /** |
2440 | | * g_test_set_nonfatal_assertions: |
2441 | | * |
2442 | | * Changes the behaviour of the various `g_assert_*()` macros, |
2443 | | * g_test_assert_expected_messages() and the various |
2444 | | * `g_test_trap_assert_*()` macros to not abort to program, but instead |
2445 | | * call g_test_fail() and continue. (This also changes the behavior of |
2446 | | * g_test_fail() so that it will not cause the test program to abort |
2447 | | * after completing the failed test.) |
2448 | | * |
2449 | | * Note that the g_assert_not_reached() and g_assert() macros are not |
2450 | | * affected by this. |
2451 | | * |
2452 | | * This function can only be called after g_test_init(). |
2453 | | * |
2454 | | * Since: 2.38 |
2455 | | */ |
2456 | | void |
2457 | | g_test_set_nonfatal_assertions (void) |
2458 | 0 | { |
2459 | 0 | if (!g_test_config_vars->test_initialized) |
2460 | 0 | g_error ("g_test_set_nonfatal_assertions called without g_test_init"); |
2461 | 0 | test_nonfatal_assertions = TRUE; |
2462 | 0 | test_mode_fatal = FALSE; |
2463 | 0 | } |
2464 | | |
2465 | | /** |
2466 | | * GTestFunc: |
2467 | | * |
2468 | | * The type used for test case functions. |
2469 | | * |
2470 | | * Since: 2.28 |
2471 | | */ |
2472 | | |
2473 | | /** |
2474 | | * g_test_add_func: |
2475 | | * @testpath: /-separated test case path name for the test. |
2476 | | * @test_func: (scope async): The test function to invoke for this test. |
2477 | | * |
2478 | | * Create a new test case, similar to g_test_create_case(). However |
2479 | | * the test is assumed to use no fixture, and test suites are automatically |
2480 | | * created on the fly and added to the root fixture, based on the |
2481 | | * slash-separated portions of @testpath. |
2482 | | * |
2483 | | * If @testpath includes the component "subprocess" anywhere in it, |
2484 | | * the test will be skipped by default, and only run if explicitly |
2485 | | * required via the `-p` command-line option or g_test_trap_subprocess(). |
2486 | | * |
2487 | | * No component of @testpath may start with a dot (`.`) if the |
2488 | | * %G_TEST_OPTION_ISOLATE_DIRS option is being used; and it is recommended to |
2489 | | * do so even if it isn’t. |
2490 | | * |
2491 | | * Since: 2.16 |
2492 | | */ |
2493 | | void |
2494 | | g_test_add_func (const char *testpath, |
2495 | | GTestFunc test_func) |
2496 | 0 | { |
2497 | 0 | g_return_if_fail (testpath != NULL); |
2498 | 0 | g_return_if_fail (testpath[0] == '/'); |
2499 | 0 | g_return_if_fail (test_func != NULL); |
2500 | 0 | g_test_add_vtable (testpath, 0, NULL, NULL, (GTestFixtureFunc) test_func, NULL); |
2501 | 0 | } |
2502 | | |
2503 | | /** |
2504 | | * GTestDataFunc: |
2505 | | * @user_data: the data provided when registering the test |
2506 | | * |
2507 | | * The type used for test case functions that take an extra pointer |
2508 | | * argument. |
2509 | | * |
2510 | | * Since: 2.28 |
2511 | | */ |
2512 | | |
2513 | | /** |
2514 | | * g_test_add_data_func: |
2515 | | * @testpath: /-separated test case path name for the test. |
2516 | | * @test_data: Test data argument for the test function. |
2517 | | * @test_func: (scope async): The test function to invoke for this test. |
2518 | | * |
2519 | | * Create a new test case, similar to g_test_create_case(). However |
2520 | | * the test is assumed to use no fixture, and test suites are automatically |
2521 | | * created on the fly and added to the root fixture, based on the |
2522 | | * slash-separated portions of @testpath. The @test_data argument |
2523 | | * will be passed as first argument to @test_func. |
2524 | | * |
2525 | | * If @testpath includes the component "subprocess" anywhere in it, |
2526 | | * the test will be skipped by default, and only run if explicitly |
2527 | | * required via the `-p` command-line option or g_test_trap_subprocess(). |
2528 | | * |
2529 | | * No component of @testpath may start with a dot (`.`) if the |
2530 | | * %G_TEST_OPTION_ISOLATE_DIRS option is being used; and it is recommended to |
2531 | | * do so even if it isn’t. |
2532 | | * |
2533 | | * Since: 2.16 |
2534 | | */ |
2535 | | void |
2536 | | g_test_add_data_func (const char *testpath, |
2537 | | gconstpointer test_data, |
2538 | | GTestDataFunc test_func) |
2539 | 0 | { |
2540 | 0 | g_return_if_fail (testpath != NULL); |
2541 | 0 | g_return_if_fail (testpath[0] == '/'); |
2542 | 0 | g_return_if_fail (test_func != NULL); |
2543 | | |
2544 | 0 | g_test_add_vtable (testpath, 0, test_data, NULL, (GTestFixtureFunc) test_func, NULL); |
2545 | 0 | } |
2546 | | |
2547 | | /** |
2548 | | * g_test_add_data_func_full: |
2549 | | * @testpath: /-separated test case path name for the test. |
2550 | | * @test_data: Test data argument for the test function. |
2551 | | * @test_func: The test function to invoke for this test. |
2552 | | * @data_free_func: #GDestroyNotify for @test_data. |
2553 | | * |
2554 | | * Create a new test case, as with g_test_add_data_func(), but freeing |
2555 | | * @test_data after the test run is complete. |
2556 | | * |
2557 | | * Since: 2.34 |
2558 | | */ |
2559 | | void |
2560 | | g_test_add_data_func_full (const char *testpath, |
2561 | | gpointer test_data, |
2562 | | GTestDataFunc test_func, |
2563 | | GDestroyNotify data_free_func) |
2564 | 0 | { |
2565 | 0 | g_return_if_fail (testpath != NULL); |
2566 | 0 | g_return_if_fail (testpath[0] == '/'); |
2567 | 0 | g_return_if_fail (test_func != NULL); |
2568 | | |
2569 | 0 | g_test_add_vtable (testpath, 0, test_data, NULL, |
2570 | 0 | (GTestFixtureFunc) test_func, |
2571 | 0 | (GTestFixtureFunc) data_free_func); |
2572 | 0 | } |
2573 | | |
2574 | | static gboolean |
2575 | | g_test_suite_case_exists (GTestSuite *suite, |
2576 | | const char *test_path) |
2577 | 0 | { |
2578 | 0 | GSList *iter; |
2579 | 0 | char *slash; |
2580 | 0 | GTestCase *tc; |
2581 | |
|
2582 | 0 | test_path++; |
2583 | 0 | slash = strchr (test_path, '/'); |
2584 | |
|
2585 | 0 | if (slash) |
2586 | 0 | { |
2587 | 0 | for (iter = suite->suites; iter; iter = iter->next) |
2588 | 0 | { |
2589 | 0 | GTestSuite *child_suite = iter->data; |
2590 | |
|
2591 | 0 | if (!strncmp (child_suite->name, test_path, slash - test_path)) |
2592 | 0 | if (g_test_suite_case_exists (child_suite, slash)) |
2593 | 0 | return TRUE; |
2594 | 0 | } |
2595 | 0 | } |
2596 | 0 | else |
2597 | 0 | { |
2598 | 0 | for (iter = suite->cases; iter; iter = iter->next) |
2599 | 0 | { |
2600 | 0 | tc = iter->data; |
2601 | 0 | if (!strcmp (tc->name, test_path)) |
2602 | 0 | return TRUE; |
2603 | 0 | } |
2604 | 0 | } |
2605 | | |
2606 | 0 | return FALSE; |
2607 | 0 | } |
2608 | | |
2609 | | /** |
2610 | | * g_test_create_suite: |
2611 | | * @suite_name: a name for the suite |
2612 | | * |
2613 | | * Create a new test suite with the name @suite_name. |
2614 | | * |
2615 | | * Returns: A newly allocated #GTestSuite instance. |
2616 | | * |
2617 | | * Since: 2.16 |
2618 | | */ |
2619 | | GTestSuite* |
2620 | | g_test_create_suite (const char *suite_name) |
2621 | 0 | { |
2622 | 0 | GTestSuite *ts; |
2623 | 0 | g_return_val_if_fail (suite_name != NULL, NULL); |
2624 | 0 | g_return_val_if_fail (strchr (suite_name, '/') == NULL, NULL); |
2625 | 0 | g_return_val_if_fail (suite_name[0] != 0, NULL); |
2626 | 0 | ts = g_slice_new0 (GTestSuite); |
2627 | 0 | ts->name = g_strdup (suite_name); |
2628 | 0 | return ts; |
2629 | 0 | } |
2630 | | |
2631 | | /** |
2632 | | * g_test_suite_add: |
2633 | | * @suite: a #GTestSuite |
2634 | | * @test_case: a #GTestCase |
2635 | | * |
2636 | | * Adds @test_case to @suite. |
2637 | | * |
2638 | | * Since: 2.16 |
2639 | | */ |
2640 | | void |
2641 | | g_test_suite_add (GTestSuite *suite, |
2642 | | GTestCase *test_case) |
2643 | 0 | { |
2644 | 0 | g_return_if_fail (suite != NULL); |
2645 | 0 | g_return_if_fail (test_case != NULL); |
2646 | | |
2647 | 0 | suite->cases = g_slist_append (suite->cases, test_case); |
2648 | 0 | } |
2649 | | |
2650 | | /** |
2651 | | * g_test_suite_add_suite: |
2652 | | * @suite: a #GTestSuite |
2653 | | * @nestedsuite: another #GTestSuite |
2654 | | * |
2655 | | * Adds @nestedsuite to @suite. |
2656 | | * |
2657 | | * Since: 2.16 |
2658 | | */ |
2659 | | void |
2660 | | g_test_suite_add_suite (GTestSuite *suite, |
2661 | | GTestSuite *nestedsuite) |
2662 | 0 | { |
2663 | 0 | g_return_if_fail (suite != NULL); |
2664 | 0 | g_return_if_fail (nestedsuite != NULL); |
2665 | | |
2666 | 0 | suite->suites = g_slist_append (suite->suites, nestedsuite); |
2667 | 0 | } |
2668 | | |
2669 | | /** |
2670 | | * g_test_queue_free: |
2671 | | * @gfree_pointer: the pointer to be stored. |
2672 | | * |
2673 | | * Enqueue a pointer to be released with g_free() during the next |
2674 | | * teardown phase. This is equivalent to calling g_test_queue_destroy() |
2675 | | * with a destroy callback of g_free(). |
2676 | | * |
2677 | | * Since: 2.16 |
2678 | | */ |
2679 | | void |
2680 | | g_test_queue_free (gpointer gfree_pointer) |
2681 | 0 | { |
2682 | 0 | if (gfree_pointer) |
2683 | 0 | g_test_queue_destroy (g_free, gfree_pointer); |
2684 | 0 | } |
2685 | | |
2686 | | /** |
2687 | | * g_test_queue_destroy: |
2688 | | * @destroy_func: Destroy callback for teardown phase. |
2689 | | * @destroy_data: Destroy callback data. |
2690 | | * |
2691 | | * This function enqueus a callback @destroy_func to be executed |
2692 | | * during the next test case teardown phase. This is most useful |
2693 | | * to auto destruct allocated test resources at the end of a test run. |
2694 | | * Resources are released in reverse queue order, that means enqueueing |
2695 | | * callback A before callback B will cause B() to be called before |
2696 | | * A() during teardown. |
2697 | | * |
2698 | | * Since: 2.16 |
2699 | | */ |
2700 | | void |
2701 | | g_test_queue_destroy (GDestroyNotify destroy_func, |
2702 | | gpointer destroy_data) |
2703 | 0 | { |
2704 | 0 | DestroyEntry *dentry; |
2705 | |
|
2706 | 0 | g_return_if_fail (destroy_func != NULL); |
2707 | | |
2708 | 0 | dentry = g_slice_new0 (DestroyEntry); |
2709 | 0 | dentry->destroy_func = destroy_func; |
2710 | 0 | dentry->destroy_data = destroy_data; |
2711 | 0 | dentry->next = test_destroy_queue; |
2712 | 0 | test_destroy_queue = dentry; |
2713 | 0 | } |
2714 | | |
2715 | | static gint |
2716 | | test_has_prefix (gconstpointer a, |
2717 | | gconstpointer b) |
2718 | 0 | { |
2719 | 0 | const gchar *test_path_skipped_local = (const gchar *)a; |
2720 | 0 | const gchar* test_run_name_local = (const gchar*)b; |
2721 | 0 | if (test_prefix_extended_skipped) |
2722 | 0 | { |
2723 | | /* If both are null, we consider that it doesn't match */ |
2724 | 0 | if (!test_path_skipped_local || !test_run_name_local) |
2725 | 0 | return FALSE; |
2726 | 0 | return strncmp (test_run_name_local, test_path_skipped_local, strlen (test_path_skipped_local)); |
2727 | 0 | } |
2728 | 0 | return g_strcmp0 (test_run_name_local, test_path_skipped_local); |
2729 | 0 | } |
2730 | | |
2731 | | static gboolean |
2732 | | test_case_run (GTestCase *tc) |
2733 | 0 | { |
2734 | 0 | gchar *old_base = g_strdup (test_uri_base); |
2735 | 0 | GSList **old_free_list, *filename_free_list = NULL; |
2736 | 0 | gboolean success = G_TEST_RUN_SUCCESS; |
2737 | |
|
2738 | 0 | old_free_list = test_filename_free_list; |
2739 | 0 | test_filename_free_list = &filename_free_list; |
2740 | |
|
2741 | 0 | if (++test_run_count <= test_startup_skip_count) |
2742 | 0 | g_test_log (G_TEST_LOG_SKIP_CASE, test_run_name, NULL, 0, NULL); |
2743 | 0 | else if (test_run_list) |
2744 | 0 | { |
2745 | 0 | g_print ("%s\n", test_run_name); |
2746 | 0 | g_test_log (G_TEST_LOG_LIST_CASE, test_run_name, NULL, 0, NULL); |
2747 | 0 | } |
2748 | 0 | else |
2749 | 0 | { |
2750 | 0 | GTimer *test_run_timer = g_timer_new(); |
2751 | 0 | long double largs[3]; |
2752 | 0 | void *fixture; |
2753 | 0 | g_test_log (G_TEST_LOG_START_CASE, test_run_name, NULL, 0, NULL); |
2754 | 0 | test_run_forks = 0; |
2755 | 0 | test_run_success = G_TEST_RUN_SUCCESS; |
2756 | 0 | g_clear_pointer (&test_run_msg, g_free); |
2757 | 0 | g_test_log_set_fatal_handler (NULL, NULL); |
2758 | 0 | if (test_paths_skipped && g_slist_find_custom (test_paths_skipped, test_run_name, (GCompareFunc)test_has_prefix)) |
2759 | 0 | g_test_skip ("by request (-s option)"); |
2760 | 0 | else |
2761 | 0 | { |
2762 | 0 | GError *local_error = NULL; |
2763 | |
|
2764 | 0 | if (!test_do_isolate_dirs (&local_error)) |
2765 | 0 | { |
2766 | 0 | g_test_log (G_TEST_LOG_ERROR, local_error->message, NULL, 0, NULL); |
2767 | 0 | g_test_fail (); |
2768 | 0 | g_error_free (local_error); |
2769 | 0 | } |
2770 | 0 | else |
2771 | 0 | { |
2772 | 0 | g_timer_start (test_run_timer); |
2773 | 0 | fixture = tc->fixture_size ? g_malloc0 (tc->fixture_size) : tc->test_data; |
2774 | 0 | test_run_seed (test_run_seedstr); |
2775 | 0 | if (tc->fixture_setup) |
2776 | 0 | tc->fixture_setup (fixture, tc->test_data); |
2777 | 0 | tc->fixture_test (fixture, tc->test_data); |
2778 | 0 | test_trap_clear(); |
2779 | 0 | while (test_destroy_queue) |
2780 | 0 | { |
2781 | 0 | DestroyEntry *dentry = test_destroy_queue; |
2782 | 0 | test_destroy_queue = dentry->next; |
2783 | 0 | dentry->destroy_func (dentry->destroy_data); |
2784 | 0 | g_slice_free (DestroyEntry, dentry); |
2785 | 0 | } |
2786 | 0 | if (tc->fixture_teardown) |
2787 | 0 | tc->fixture_teardown (fixture, tc->test_data); |
2788 | 0 | if (tc->fixture_size) |
2789 | 0 | g_free (fixture); |
2790 | 0 | g_timer_stop (test_run_timer); |
2791 | 0 | } |
2792 | |
|
2793 | 0 | test_rm_isolate_dirs (); |
2794 | 0 | } |
2795 | 0 | success = test_run_success; |
2796 | 0 | test_run_success = G_TEST_RUN_FAILURE; |
2797 | 0 | largs[0] = success; /* OK */ |
2798 | 0 | largs[1] = test_run_forks; |
2799 | 0 | largs[2] = g_timer_elapsed (test_run_timer, NULL); |
2800 | 0 | g_test_log (G_TEST_LOG_STOP_CASE, test_run_name, test_run_msg, G_N_ELEMENTS (largs), largs); |
2801 | 0 | g_clear_pointer (&test_run_msg, g_free); |
2802 | 0 | g_timer_destroy (test_run_timer); |
2803 | 0 | } |
2804 | |
|
2805 | 0 | g_slist_free_full (filename_free_list, g_free); |
2806 | 0 | test_filename_free_list = old_free_list; |
2807 | 0 | g_free (test_uri_base); |
2808 | 0 | test_uri_base = old_base; |
2809 | |
|
2810 | 0 | return (success == G_TEST_RUN_SUCCESS || |
2811 | 0 | success == G_TEST_RUN_SKIPPED || |
2812 | 0 | success == G_TEST_RUN_INCOMPLETE); |
2813 | 0 | } |
2814 | | |
2815 | | static gboolean |
2816 | | path_has_prefix (const char *path, |
2817 | | const char *prefix) |
2818 | 0 | { |
2819 | 0 | int prefix_len = strlen (prefix); |
2820 | |
|
2821 | 0 | return (strncmp (path, prefix, prefix_len) == 0 && |
2822 | 0 | (path[prefix_len] == '\0' || |
2823 | 0 | path[prefix_len] == '/')); |
2824 | 0 | } |
2825 | | |
2826 | | static gboolean |
2827 | | test_should_run (const char *test_path, |
2828 | | const char *cmp_path) |
2829 | 0 | { |
2830 | 0 | if (strstr (test_run_name, "/subprocess")) |
2831 | 0 | { |
2832 | 0 | if (g_strcmp0 (test_path, cmp_path) == 0) |
2833 | 0 | return TRUE; |
2834 | | |
2835 | 0 | if (g_test_verbose ()) |
2836 | 0 | g_print ("GTest: skipping: %s\n", test_run_name); |
2837 | 0 | return FALSE; |
2838 | 0 | } |
2839 | | |
2840 | 0 | return !cmp_path || path_has_prefix (test_path, cmp_path); |
2841 | 0 | } |
2842 | | |
2843 | | /* Recurse through @suite, running tests matching @path (or all tests |
2844 | | * if @path is %NULL). |
2845 | | */ |
2846 | | static int |
2847 | | g_test_run_suite_internal (GTestSuite *suite, |
2848 | | const char *path) |
2849 | 0 | { |
2850 | 0 | guint n_bad = 0; |
2851 | 0 | gchar *old_name = test_run_name; |
2852 | 0 | GSList *iter; |
2853 | |
|
2854 | 0 | g_return_val_if_fail (suite != NULL, -1); |
2855 | | |
2856 | 0 | g_test_log (G_TEST_LOG_START_SUITE, suite->name, NULL, 0, NULL); |
2857 | |
|
2858 | 0 | for (iter = suite->cases; iter; iter = iter->next) |
2859 | 0 | { |
2860 | 0 | GTestCase *tc = iter->data; |
2861 | |
|
2862 | 0 | test_run_name = g_build_path ("/", old_name, tc->name, NULL); |
2863 | 0 | if (test_should_run (test_run_name, path)) |
2864 | 0 | { |
2865 | 0 | if (!test_case_run (tc)) |
2866 | 0 | n_bad++; |
2867 | 0 | } |
2868 | 0 | g_free (test_run_name); |
2869 | 0 | } |
2870 | |
|
2871 | 0 | for (iter = suite->suites; iter; iter = iter->next) |
2872 | 0 | { |
2873 | 0 | GTestSuite *ts = iter->data; |
2874 | |
|
2875 | 0 | test_run_name = g_build_path ("/", old_name, ts->name, NULL); |
2876 | 0 | if (test_prefix_extended) { |
2877 | 0 | if (!path || path_has_prefix (test_run_name, path)) |
2878 | 0 | n_bad += g_test_run_suite_internal (ts, test_run_name); |
2879 | 0 | else if (!path || path_has_prefix (path, test_run_name)) |
2880 | 0 | n_bad += g_test_run_suite_internal (ts, path); |
2881 | 0 | } else if (!path || path_has_prefix (path, test_run_name)) { |
2882 | 0 | n_bad += g_test_run_suite_internal (ts, path); |
2883 | 0 | } |
2884 | |
|
2885 | 0 | g_free (test_run_name); |
2886 | 0 | } |
2887 | |
|
2888 | 0 | test_run_name = old_name; |
2889 | |
|
2890 | 0 | g_test_log (G_TEST_LOG_STOP_SUITE, suite->name, NULL, 0, NULL); |
2891 | |
|
2892 | 0 | return n_bad; |
2893 | 0 | } |
2894 | | |
2895 | | static int |
2896 | | g_test_suite_count (GTestSuite *suite) |
2897 | 0 | { |
2898 | 0 | int n = 0; |
2899 | 0 | GSList *iter; |
2900 | |
|
2901 | 0 | g_return_val_if_fail (suite != NULL, -1); |
2902 | | |
2903 | 0 | for (iter = suite->cases; iter; iter = iter->next) |
2904 | 0 | { |
2905 | 0 | GTestCase *tc = iter->data; |
2906 | |
|
2907 | 0 | if (strcmp (tc->name, "subprocess") != 0) |
2908 | 0 | n++; |
2909 | 0 | } |
2910 | |
|
2911 | 0 | for (iter = suite->suites; iter; iter = iter->next) |
2912 | 0 | { |
2913 | 0 | GTestSuite *ts = iter->data; |
2914 | |
|
2915 | 0 | if (strcmp (ts->name, "subprocess") != 0) |
2916 | 0 | n += g_test_suite_count (ts); |
2917 | 0 | } |
2918 | |
|
2919 | 0 | return n; |
2920 | 0 | } |
2921 | | |
2922 | | /** |
2923 | | * g_test_run_suite: |
2924 | | * @suite: a #GTestSuite |
2925 | | * |
2926 | | * Execute the tests within @suite and all nested #GTestSuites. |
2927 | | * The test suites to be executed are filtered according to |
2928 | | * test path arguments (`-p testpath` and `-s testpath`) as parsed by |
2929 | | * g_test_init(). See the g_test_run() documentation for more |
2930 | | * information on the order that tests are run in. |
2931 | | * |
2932 | | * g_test_run_suite() or g_test_run() may only be called once |
2933 | | * in a program. |
2934 | | * |
2935 | | * Returns: 0 on success |
2936 | | * |
2937 | | * Since: 2.16 |
2938 | | */ |
2939 | | int |
2940 | | g_test_run_suite (GTestSuite *suite) |
2941 | 0 | { |
2942 | 0 | int n_bad = 0; |
2943 | |
|
2944 | 0 | g_return_val_if_fail (g_test_run_once == TRUE, -1); |
2945 | | |
2946 | 0 | g_test_run_once = FALSE; |
2947 | 0 | test_count = g_test_suite_count (suite); |
2948 | |
|
2949 | 0 | test_run_name = g_strdup_printf ("/%s", suite->name); |
2950 | |
|
2951 | 0 | if (test_paths) |
2952 | 0 | { |
2953 | 0 | GSList *iter; |
2954 | |
|
2955 | 0 | for (iter = test_paths; iter; iter = iter->next) |
2956 | 0 | n_bad += g_test_run_suite_internal (suite, iter->data); |
2957 | 0 | } |
2958 | 0 | else |
2959 | 0 | n_bad = g_test_run_suite_internal (suite, NULL); |
2960 | |
|
2961 | 0 | g_free (test_run_name); |
2962 | 0 | test_run_name = NULL; |
2963 | |
|
2964 | 0 | return n_bad; |
2965 | 0 | } |
2966 | | |
2967 | | static void |
2968 | | gtest_default_log_handler (const gchar *log_domain, |
2969 | | GLogLevelFlags log_level, |
2970 | | const gchar *message, |
2971 | | gpointer unused_data) |
2972 | 0 | { |
2973 | 0 | const gchar *strv[16]; |
2974 | 0 | gboolean fatal = FALSE; |
2975 | 0 | gchar *msg; |
2976 | 0 | guint i = 0; |
2977 | |
|
2978 | 0 | if (log_domain) |
2979 | 0 | { |
2980 | 0 | strv[i++] = log_domain; |
2981 | 0 | strv[i++] = "-"; |
2982 | 0 | } |
2983 | 0 | if (log_level & G_LOG_FLAG_FATAL) |
2984 | 0 | { |
2985 | 0 | strv[i++] = "FATAL-"; |
2986 | 0 | fatal = TRUE; |
2987 | 0 | } |
2988 | 0 | if (log_level & G_LOG_FLAG_RECURSION) |
2989 | 0 | strv[i++] = "RECURSIVE-"; |
2990 | 0 | if (log_level & G_LOG_LEVEL_ERROR) |
2991 | 0 | strv[i++] = "ERROR"; |
2992 | 0 | if (log_level & G_LOG_LEVEL_CRITICAL) |
2993 | 0 | strv[i++] = "CRITICAL"; |
2994 | 0 | if (log_level & G_LOG_LEVEL_WARNING) |
2995 | 0 | strv[i++] = "WARNING"; |
2996 | 0 | if (log_level & G_LOG_LEVEL_MESSAGE) |
2997 | 0 | strv[i++] = "MESSAGE"; |
2998 | 0 | if (log_level & G_LOG_LEVEL_INFO) |
2999 | 0 | strv[i++] = "INFO"; |
3000 | 0 | if (log_level & G_LOG_LEVEL_DEBUG) |
3001 | 0 | strv[i++] = "DEBUG"; |
3002 | 0 | strv[i++] = ": "; |
3003 | 0 | strv[i++] = message; |
3004 | 0 | strv[i++] = NULL; |
3005 | |
|
3006 | 0 | msg = g_strjoinv ("", (gchar**) strv); |
3007 | 0 | g_test_log (fatal ? G_TEST_LOG_ERROR : G_TEST_LOG_MESSAGE, msg, NULL, 0, NULL); |
3008 | 0 | g_log_default_handler (log_domain, log_level, message, unused_data); |
3009 | |
|
3010 | 0 | g_free (msg); |
3011 | 0 | } |
3012 | | |
3013 | | void |
3014 | | g_assertion_message (const char *domain, |
3015 | | const char *file, |
3016 | | int line, |
3017 | | const char *func, |
3018 | | const char *message) |
3019 | 0 | { |
3020 | 0 | char lstr[32]; |
3021 | 0 | char *s; |
3022 | |
|
3023 | 0 | if (!message) |
3024 | 0 | message = "code should not be reached"; |
3025 | 0 | g_snprintf (lstr, 32, "%d", line); |
3026 | 0 | s = g_strconcat (domain ? domain : "", domain && domain[0] ? ":" : "", |
3027 | 0 | "ERROR:", file, ":", lstr, ":", |
3028 | 0 | func, func[0] ? ":" : "", |
3029 | 0 | " ", message, NULL); |
3030 | 0 | g_printerr ("**\n%s\n", s); |
3031 | | |
3032 | | /* Don't print a fatal error indication if assertions are non-fatal, or |
3033 | | * if we are a child process that might be sharing the parent's stdout. */ |
3034 | 0 | if (test_nonfatal_assertions || test_in_subprocess || test_in_forked_child) |
3035 | 0 | g_test_log (G_TEST_LOG_MESSAGE, s, NULL, 0, NULL); |
3036 | 0 | else |
3037 | 0 | g_test_log (G_TEST_LOG_ERROR, s, NULL, 0, NULL); |
3038 | |
|
3039 | 0 | if (test_nonfatal_assertions) |
3040 | 0 | { |
3041 | 0 | g_free (s); |
3042 | 0 | g_test_fail (); |
3043 | 0 | return; |
3044 | 0 | } |
3045 | | |
3046 | | /* store assertion message in global variable, so that it can be found in a |
3047 | | * core dump */ |
3048 | 0 | if (__glib_assert_msg != NULL) |
3049 | | /* free the old one */ |
3050 | 0 | free (__glib_assert_msg); |
3051 | 0 | __glib_assert_msg = (char*) malloc (strlen (s) + 1); |
3052 | 0 | strcpy (__glib_assert_msg, s); |
3053 | |
|
3054 | 0 | g_free (s); |
3055 | |
|
3056 | 0 | if (test_in_subprocess) |
3057 | 0 | { |
3058 | | /* If this is a test case subprocess then it probably hit this |
3059 | | * assertion on purpose, so just exit() rather than abort()ing, |
3060 | | * to avoid triggering any system crash-reporting daemon. |
3061 | | */ |
3062 | 0 | _exit (1); |
3063 | 0 | } |
3064 | 0 | else |
3065 | 0 | g_abort (); |
3066 | 0 | } |
3067 | | |
3068 | | /** |
3069 | | * g_assertion_message_expr: (skip) |
3070 | | * @domain: (nullable): log domain |
3071 | | * @file: file containing the assertion |
3072 | | * @line: line number of the assertion |
3073 | | * @func: function containing the assertion |
3074 | | * @expr: (nullable): expression which failed |
3075 | | * |
3076 | | * Internal function used to print messages from the public g_assert() and |
3077 | | * g_assert_not_reached() macros. |
3078 | | */ |
3079 | | void |
3080 | | g_assertion_message_expr (const char *domain, |
3081 | | const char *file, |
3082 | | int line, |
3083 | | const char *func, |
3084 | | const char *expr) |
3085 | 0 | { |
3086 | 0 | char *s; |
3087 | 0 | if (!expr) |
3088 | 0 | s = g_strdup ("code should not be reached"); |
3089 | 0 | else |
3090 | 0 | s = g_strconcat ("assertion failed: (", expr, ")", NULL); |
3091 | 0 | g_assertion_message (domain, file, line, func, s); |
3092 | 0 | g_free (s); |
3093 | | |
3094 | | /* Normally g_assertion_message() won't return, but we need this for |
3095 | | * when test_nonfatal_assertions is set, since |
3096 | | * g_assertion_message_expr() is used for always-fatal assertions. |
3097 | | */ |
3098 | 0 | if (test_in_subprocess) |
3099 | 0 | _exit (1); |
3100 | 0 | else |
3101 | 0 | g_abort (); |
3102 | 0 | } |
3103 | | |
3104 | | void |
3105 | | g_assertion_message_cmpnum (const char *domain, |
3106 | | const char *file, |
3107 | | int line, |
3108 | | const char *func, |
3109 | | const char *expr, |
3110 | | long double arg1, |
3111 | | const char *cmp, |
3112 | | long double arg2, |
3113 | | char numtype) |
3114 | 0 | { |
3115 | 0 | char *s = NULL; |
3116 | |
|
3117 | 0 | switch (numtype) |
3118 | 0 | { |
3119 | 0 | case 'i': s = g_strdup_printf ("assertion failed (%s): (%" G_GINT64_MODIFIER "i %s %" G_GINT64_MODIFIER "i)", expr, (gint64) arg1, cmp, (gint64) arg2); break; |
3120 | 0 | case 'x': s = g_strdup_printf ("assertion failed (%s): (0x%08" G_GINT64_MODIFIER "x %s 0x%08" G_GINT64_MODIFIER "x)", expr, (guint64) arg1, cmp, (guint64) arg2); break; |
3121 | 0 | case 'f': s = g_strdup_printf ("assertion failed (%s): (%.9g %s %.9g)", expr, (double) arg1, cmp, (double) arg2); break; |
3122 | | /* ideally use: floats=%.7g double=%.17g */ |
3123 | 0 | } |
3124 | 0 | g_assertion_message (domain, file, line, func, s); |
3125 | 0 | g_free (s); |
3126 | 0 | } |
3127 | | |
3128 | | void |
3129 | | g_assertion_message_cmpstr (const char *domain, |
3130 | | const char *file, |
3131 | | int line, |
3132 | | const char *func, |
3133 | | const char *expr, |
3134 | | const char *arg1, |
3135 | | const char *cmp, |
3136 | | const char *arg2) |
3137 | 0 | { |
3138 | 0 | char *a1, *a2, *s, *t1 = NULL, *t2 = NULL; |
3139 | 0 | a1 = arg1 ? g_strconcat ("\"", t1 = g_strescape (arg1, NULL), "\"", NULL) : g_strdup ("NULL"); |
3140 | 0 | a2 = arg2 ? g_strconcat ("\"", t2 = g_strescape (arg2, NULL), "\"", NULL) : g_strdup ("NULL"); |
3141 | 0 | g_free (t1); |
3142 | 0 | g_free (t2); |
3143 | 0 | s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2); |
3144 | 0 | g_free (a1); |
3145 | 0 | g_free (a2); |
3146 | 0 | g_assertion_message (domain, file, line, func, s); |
3147 | 0 | g_free (s); |
3148 | 0 | } |
3149 | | |
3150 | | void |
3151 | | g_assertion_message_cmpstrv (const char *domain, |
3152 | | const char *file, |
3153 | | int line, |
3154 | | const char *func, |
3155 | | const char *expr, |
3156 | | const char * const *arg1, |
3157 | | const char * const *arg2, |
3158 | | gsize first_wrong_idx) |
3159 | 0 | { |
3160 | 0 | const char *s1 = arg1[first_wrong_idx], *s2 = arg2[first_wrong_idx]; |
3161 | 0 | char *a1, *a2, *s, *t1 = NULL, *t2 = NULL; |
3162 | |
|
3163 | 0 | a1 = g_strconcat ("\"", t1 = g_strescape (s1, NULL), "\"", NULL); |
3164 | 0 | a2 = g_strconcat ("\"", t2 = g_strescape (s2, NULL), "\"", NULL); |
3165 | 0 | g_free (t1); |
3166 | 0 | g_free (t2); |
3167 | 0 | s = g_strdup_printf ("assertion failed (%s): first differing element at index %" G_GSIZE_FORMAT ": %s does not equal %s", |
3168 | 0 | expr, first_wrong_idx, a1, a2); |
3169 | 0 | g_free (a1); |
3170 | 0 | g_free (a2); |
3171 | 0 | g_assertion_message (domain, file, line, func, s); |
3172 | 0 | g_free (s); |
3173 | 0 | } |
3174 | | |
3175 | | void |
3176 | | g_assertion_message_error (const char *domain, |
3177 | | const char *file, |
3178 | | int line, |
3179 | | const char *func, |
3180 | | const char *expr, |
3181 | | const GError *error, |
3182 | | GQuark error_domain, |
3183 | | int error_code) |
3184 | 0 | { |
3185 | 0 | GString *gstring; |
3186 | | |
3187 | | /* This is used by both g_assert_error() and g_assert_no_error(), so there |
3188 | | * are three cases: expected an error but got the wrong error, expected |
3189 | | * an error but got no error, and expected no error but got an error. |
3190 | | */ |
3191 | |
|
3192 | 0 | gstring = g_string_new ("assertion failed "); |
3193 | 0 | if (error_domain) |
3194 | 0 | g_string_append_printf (gstring, "(%s == (%s, %d)): ", expr, |
3195 | 0 | g_quark_to_string (error_domain), error_code); |
3196 | 0 | else |
3197 | 0 | g_string_append_printf (gstring, "(%s == NULL): ", expr); |
3198 | |
|
3199 | 0 | if (error) |
3200 | 0 | g_string_append_printf (gstring, "%s (%s, %d)", error->message, |
3201 | 0 | g_quark_to_string (error->domain), error->code); |
3202 | 0 | else |
3203 | 0 | g_string_append_printf (gstring, "%s is NULL", expr); |
3204 | |
|
3205 | 0 | g_assertion_message (domain, file, line, func, gstring->str); |
3206 | 0 | g_string_free (gstring, TRUE); |
3207 | 0 | } |
3208 | | |
3209 | | /** |
3210 | | * g_strcmp0: |
3211 | | * @str1: (nullable): a C string or %NULL |
3212 | | * @str2: (nullable): another C string or %NULL |
3213 | | * |
3214 | | * Compares @str1 and @str2 like strcmp(). Handles %NULL |
3215 | | * gracefully by sorting it before non-%NULL strings. |
3216 | | * Comparing two %NULL pointers returns 0. |
3217 | | * |
3218 | | * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2. |
3219 | | * |
3220 | | * Since: 2.16 |
3221 | | */ |
3222 | | int |
3223 | | g_strcmp0 (const char *str1, |
3224 | | const char *str2) |
3225 | 12.9M | { |
3226 | 12.9M | if (!str1) |
3227 | 1.24M | return -(str1 != str2); |
3228 | 11.7M | if (!str2) |
3229 | 0 | return str1 != str2; |
3230 | 11.7M | return strcmp (str1, str2); |
3231 | 11.7M | } |
3232 | | |
3233 | | static void |
3234 | | test_trap_clear (void) |
3235 | 0 | { |
3236 | 0 | test_trap_last_status = 0; |
3237 | 0 | test_trap_last_pid = 0; |
3238 | 0 | g_clear_pointer (&test_trap_last_subprocess, g_free); |
3239 | 0 | g_clear_pointer (&test_trap_last_stdout, g_free); |
3240 | 0 | g_clear_pointer (&test_trap_last_stderr, g_free); |
3241 | 0 | } |
3242 | | |
3243 | | #ifdef G_OS_UNIX |
3244 | | |
3245 | | static int |
3246 | | safe_dup2 (int fd1, |
3247 | | int fd2) |
3248 | 0 | { |
3249 | 0 | int ret; |
3250 | 0 | do |
3251 | 0 | ret = dup2 (fd1, fd2); |
3252 | 0 | while (ret < 0 && errno == EINTR); |
3253 | 0 | return ret; |
3254 | 0 | } |
3255 | | |
3256 | | #endif |
3257 | | |
3258 | | typedef struct { |
3259 | | GPid pid; |
3260 | | GMainLoop *loop; |
3261 | | int child_status; /* unmodified platform-specific status */ |
3262 | | |
3263 | | GIOChannel *stdout_io; |
3264 | | gboolean echo_stdout; |
3265 | | GString *stdout_str; |
3266 | | |
3267 | | GIOChannel *stderr_io; |
3268 | | gboolean echo_stderr; |
3269 | | GString *stderr_str; |
3270 | | } WaitForChildData; |
3271 | | |
3272 | | static void |
3273 | | check_complete (WaitForChildData *data) |
3274 | 0 | { |
3275 | 0 | if (data->child_status != -1 && data->stdout_io == NULL && data->stderr_io == NULL) |
3276 | 0 | g_main_loop_quit (data->loop); |
3277 | 0 | } |
3278 | | |
3279 | | static void |
3280 | | child_exited (GPid pid, |
3281 | | gint status, |
3282 | | gpointer user_data) |
3283 | 0 | { |
3284 | 0 | WaitForChildData *data = user_data; |
3285 | |
|
3286 | 0 | g_assert (status != -1); |
3287 | 0 | data->child_status = status; |
3288 | |
|
3289 | 0 | check_complete (data); |
3290 | 0 | } |
3291 | | |
3292 | | static gboolean |
3293 | | child_timeout (gpointer user_data) |
3294 | 0 | { |
3295 | 0 | WaitForChildData *data = user_data; |
3296 | |
|
3297 | | #ifdef G_OS_WIN32 |
3298 | | TerminateProcess (data->pid, G_TEST_STATUS_TIMED_OUT); |
3299 | | #else |
3300 | 0 | kill (data->pid, SIGALRM); |
3301 | 0 | #endif |
3302 | |
|
3303 | 0 | return FALSE; |
3304 | 0 | } |
3305 | | |
3306 | | static gboolean |
3307 | | child_read (GIOChannel *io, GIOCondition cond, gpointer user_data) |
3308 | 0 | { |
3309 | 0 | WaitForChildData *data = user_data; |
3310 | 0 | GIOStatus status; |
3311 | 0 | gsize nread, nwrote, total; |
3312 | 0 | gchar buf[4096]; |
3313 | 0 | FILE *echo_file = NULL; |
3314 | |
|
3315 | 0 | status = g_io_channel_read_chars (io, buf, sizeof (buf), &nread, NULL); |
3316 | 0 | if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) |
3317 | 0 | { |
3318 | | // FIXME data->error = (status == G_IO_STATUS_ERROR); |
3319 | 0 | if (io == data->stdout_io) |
3320 | 0 | g_clear_pointer (&data->stdout_io, g_io_channel_unref); |
3321 | 0 | else |
3322 | 0 | g_clear_pointer (&data->stderr_io, g_io_channel_unref); |
3323 | |
|
3324 | 0 | check_complete (data); |
3325 | 0 | return FALSE; |
3326 | 0 | } |
3327 | 0 | else if (status == G_IO_STATUS_AGAIN) |
3328 | 0 | return TRUE; |
3329 | | |
3330 | 0 | if (io == data->stdout_io) |
3331 | 0 | { |
3332 | 0 | g_string_append_len (data->stdout_str, buf, nread); |
3333 | 0 | if (data->echo_stdout) |
3334 | 0 | echo_file = stdout; |
3335 | 0 | } |
3336 | 0 | else |
3337 | 0 | { |
3338 | 0 | g_string_append_len (data->stderr_str, buf, nread); |
3339 | 0 | if (data->echo_stderr) |
3340 | 0 | echo_file = stderr; |
3341 | 0 | } |
3342 | |
|
3343 | 0 | if (echo_file) |
3344 | 0 | { |
3345 | 0 | for (total = 0; total < nread; total += nwrote) |
3346 | 0 | { |
3347 | 0 | int errsv; |
3348 | |
|
3349 | 0 | nwrote = fwrite (buf + total, 1, nread - total, echo_file); |
3350 | 0 | errsv = errno; |
3351 | 0 | if (nwrote == 0) |
3352 | 0 | g_error ("write failed: %s", g_strerror (errsv)); |
3353 | 0 | } |
3354 | 0 | } |
3355 | |
|
3356 | 0 | return TRUE; |
3357 | 0 | } |
3358 | | |
3359 | | static void |
3360 | | wait_for_child (GPid pid, |
3361 | | int stdout_fd, gboolean echo_stdout, |
3362 | | int stderr_fd, gboolean echo_stderr, |
3363 | | guint64 timeout) |
3364 | 0 | { |
3365 | 0 | WaitForChildData data; |
3366 | 0 | GMainContext *context; |
3367 | 0 | GSource *source; |
3368 | |
|
3369 | 0 | data.pid = pid; |
3370 | 0 | data.child_status = -1; |
3371 | |
|
3372 | 0 | context = g_main_context_new (); |
3373 | 0 | data.loop = g_main_loop_new (context, FALSE); |
3374 | |
|
3375 | 0 | source = g_child_watch_source_new (pid); |
3376 | 0 | g_source_set_callback (source, (GSourceFunc) child_exited, &data, NULL); |
3377 | 0 | g_source_attach (source, context); |
3378 | 0 | g_source_unref (source); |
3379 | |
|
3380 | 0 | data.echo_stdout = echo_stdout; |
3381 | 0 | data.stdout_str = g_string_new (NULL); |
3382 | 0 | data.stdout_io = g_io_channel_unix_new (stdout_fd); |
3383 | 0 | g_io_channel_set_close_on_unref (data.stdout_io, TRUE); |
3384 | 0 | g_io_channel_set_encoding (data.stdout_io, NULL, NULL); |
3385 | 0 | g_io_channel_set_buffered (data.stdout_io, FALSE); |
3386 | 0 | source = g_io_create_watch (data.stdout_io, G_IO_IN | G_IO_ERR | G_IO_HUP); |
3387 | 0 | g_source_set_callback (source, (GSourceFunc) child_read, &data, NULL); |
3388 | 0 | g_source_attach (source, context); |
3389 | 0 | g_source_unref (source); |
3390 | |
|
3391 | 0 | data.echo_stderr = echo_stderr; |
3392 | 0 | data.stderr_str = g_string_new (NULL); |
3393 | 0 | data.stderr_io = g_io_channel_unix_new (stderr_fd); |
3394 | 0 | g_io_channel_set_close_on_unref (data.stderr_io, TRUE); |
3395 | 0 | g_io_channel_set_encoding (data.stderr_io, NULL, NULL); |
3396 | 0 | g_io_channel_set_buffered (data.stderr_io, FALSE); |
3397 | 0 | source = g_io_create_watch (data.stderr_io, G_IO_IN | G_IO_ERR | G_IO_HUP); |
3398 | 0 | g_source_set_callback (source, (GSourceFunc) child_read, &data, NULL); |
3399 | 0 | g_source_attach (source, context); |
3400 | 0 | g_source_unref (source); |
3401 | |
|
3402 | 0 | if (timeout) |
3403 | 0 | { |
3404 | 0 | source = g_timeout_source_new (0); |
3405 | 0 | g_source_set_ready_time (source, g_get_monotonic_time () + timeout); |
3406 | 0 | g_source_set_callback (source, (GSourceFunc) child_timeout, &data, NULL); |
3407 | 0 | g_source_attach (source, context); |
3408 | 0 | g_source_unref (source); |
3409 | 0 | } |
3410 | |
|
3411 | 0 | g_main_loop_run (data.loop); |
3412 | 0 | g_main_loop_unref (data.loop); |
3413 | 0 | g_main_context_unref (context); |
3414 | |
|
3415 | 0 | test_trap_last_pid = pid; |
3416 | 0 | test_trap_last_status = data.child_status; |
3417 | 0 | test_trap_last_stdout = g_string_free (data.stdout_str, FALSE); |
3418 | 0 | test_trap_last_stderr = g_string_free (data.stderr_str, FALSE); |
3419 | |
|
3420 | 0 | g_clear_pointer (&data.stdout_io, g_io_channel_unref); |
3421 | 0 | g_clear_pointer (&data.stderr_io, g_io_channel_unref); |
3422 | 0 | } |
3423 | | |
3424 | | /** |
3425 | | * g_test_trap_fork: |
3426 | | * @usec_timeout: Timeout for the forked test in micro seconds. |
3427 | | * @test_trap_flags: Flags to modify forking behaviour. |
3428 | | * |
3429 | | * Fork the current test program to execute a test case that might |
3430 | | * not return or that might abort. |
3431 | | * |
3432 | | * If @usec_timeout is non-0, the forked test case is aborted and |
3433 | | * considered failing if its run time exceeds it. |
3434 | | * |
3435 | | * The forking behavior can be configured with the #GTestTrapFlags flags. |
3436 | | * |
3437 | | * In the following example, the test code forks, the forked child |
3438 | | * process produces some sample output and exits successfully. |
3439 | | * The forking parent process then asserts successful child program |
3440 | | * termination and validates child program outputs. |
3441 | | * |
3442 | | * |[<!-- language="C" --> |
3443 | | * static void |
3444 | | * test_fork_patterns (void) |
3445 | | * { |
3446 | | * if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) |
3447 | | * { |
3448 | | * g_print ("some stdout text: somagic17\n"); |
3449 | | * g_printerr ("some stderr text: semagic43\n"); |
3450 | | * exit (0); // successful test run |
3451 | | * } |
3452 | | * g_test_trap_assert_passed (); |
3453 | | * g_test_trap_assert_stdout ("*somagic17*"); |
3454 | | * g_test_trap_assert_stderr ("*semagic43*"); |
3455 | | * } |
3456 | | * ]| |
3457 | | * |
3458 | | * Returns: %TRUE for the forked child and %FALSE for the executing parent process. |
3459 | | * |
3460 | | * Since: 2.16 |
3461 | | * |
3462 | | * Deprecated: This function is implemented only on Unix platforms, |
3463 | | * and is not always reliable due to problems inherent in |
3464 | | * fork-without-exec. Use g_test_trap_subprocess() instead. |
3465 | | */ |
3466 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
3467 | | gboolean |
3468 | | g_test_trap_fork (guint64 usec_timeout, |
3469 | | GTestTrapFlags test_trap_flags) |
3470 | 0 | { |
3471 | 0 | #ifdef G_OS_UNIX |
3472 | 0 | int stdout_pipe[2] = { -1, -1 }; |
3473 | 0 | int stderr_pipe[2] = { -1, -1 }; |
3474 | 0 | int errsv; |
3475 | |
|
3476 | 0 | test_trap_clear(); |
3477 | 0 | if (pipe (stdout_pipe) < 0 || pipe (stderr_pipe) < 0) |
3478 | 0 | { |
3479 | 0 | errsv = errno; |
3480 | 0 | g_error ("failed to create pipes to fork test program: %s", g_strerror (errsv)); |
3481 | 0 | } |
3482 | 0 | test_trap_last_pid = fork (); |
3483 | 0 | errsv = errno; |
3484 | 0 | if (test_trap_last_pid < 0) |
3485 | 0 | g_error ("failed to fork test program: %s", g_strerror (errsv)); |
3486 | 0 | if (test_trap_last_pid == 0) /* child */ |
3487 | 0 | { |
3488 | 0 | int fd0 = -1; |
3489 | 0 | test_in_forked_child = TRUE; |
3490 | 0 | close (stdout_pipe[0]); |
3491 | 0 | close (stderr_pipe[0]); |
3492 | 0 | if (!(test_trap_flags & G_TEST_TRAP_INHERIT_STDIN)) |
3493 | 0 | { |
3494 | 0 | fd0 = g_open ("/dev/null", O_RDONLY, 0); |
3495 | 0 | if (fd0 < 0) |
3496 | 0 | g_error ("failed to open /dev/null for stdin redirection"); |
3497 | 0 | } |
3498 | 0 | if (safe_dup2 (stdout_pipe[1], 1) < 0 || safe_dup2 (stderr_pipe[1], 2) < 0 || (fd0 >= 0 && safe_dup2 (fd0, 0) < 0)) |
3499 | 0 | { |
3500 | 0 | errsv = errno; |
3501 | 0 | g_error ("failed to dup2() in forked test program: %s", g_strerror (errsv)); |
3502 | 0 | } |
3503 | 0 | if (fd0 >= 3) |
3504 | 0 | close (fd0); |
3505 | 0 | if (stdout_pipe[1] >= 3) |
3506 | 0 | close (stdout_pipe[1]); |
3507 | 0 | if (stderr_pipe[1] >= 3) |
3508 | 0 | close (stderr_pipe[1]); |
3509 | | |
3510 | | /* We typically expect these child processes to crash, and some |
3511 | | * tests spawn a *lot* of them. Avoid spamming system crash |
3512 | | * collection programs such as systemd-coredump and abrt. |
3513 | | */ |
3514 | 0 | #ifdef HAVE_SYS_RESOURCE_H |
3515 | 0 | { |
3516 | 0 | struct rlimit limit = { 0, 0 }; |
3517 | 0 | (void) setrlimit (RLIMIT_CORE, &limit); |
3518 | 0 | } |
3519 | 0 | #endif |
3520 | |
|
3521 | 0 | return TRUE; |
3522 | 0 | } |
3523 | 0 | else /* parent */ |
3524 | 0 | { |
3525 | 0 | test_run_forks++; |
3526 | 0 | close (stdout_pipe[1]); |
3527 | 0 | close (stderr_pipe[1]); |
3528 | |
|
3529 | 0 | wait_for_child (test_trap_last_pid, |
3530 | 0 | stdout_pipe[0], !(test_trap_flags & G_TEST_TRAP_SILENCE_STDOUT), |
3531 | 0 | stderr_pipe[0], !(test_trap_flags & G_TEST_TRAP_SILENCE_STDERR), |
3532 | 0 | usec_timeout); |
3533 | 0 | return FALSE; |
3534 | 0 | } |
3535 | | #else |
3536 | | g_message ("Not implemented: g_test_trap_fork"); |
3537 | | |
3538 | | return FALSE; |
3539 | | #endif |
3540 | 0 | } |
3541 | | G_GNUC_END_IGNORE_DEPRECATIONS |
3542 | | |
3543 | | /** |
3544 | | * g_test_trap_subprocess: |
3545 | | * @test_path: (nullable): Test to run in a subprocess |
3546 | | * @usec_timeout: Timeout for the subprocess test in micro seconds. |
3547 | | * @test_flags: Flags to modify subprocess behaviour. |
3548 | | * |
3549 | | * Respawns the test program to run only @test_path in a subprocess. |
3550 | | * This can be used for a test case that might not return, or that |
3551 | | * might abort. |
3552 | | * |
3553 | | * If @test_path is %NULL then the same test is re-run in a subprocess. |
3554 | | * You can use g_test_subprocess() to determine whether the test is in |
3555 | | * a subprocess or not. |
3556 | | * |
3557 | | * @test_path can also be the name of the parent test, followed by |
3558 | | * "`/subprocess/`" and then a name for the specific subtest (or just |
3559 | | * ending with "`/subprocess`" if the test only has one child test); |
3560 | | * tests with names of this form will automatically be skipped in the |
3561 | | * parent process. |
3562 | | * |
3563 | | * If @usec_timeout is non-0, the test subprocess is aborted and |
3564 | | * considered failing if its run time exceeds it. |
3565 | | * |
3566 | | * The subprocess behavior can be configured with the |
3567 | | * #GTestSubprocessFlags flags. |
3568 | | * |
3569 | | * You can use methods such as g_test_trap_assert_passed(), |
3570 | | * g_test_trap_assert_failed(), and g_test_trap_assert_stderr() to |
3571 | | * check the results of the subprocess. (But note that |
3572 | | * g_test_trap_assert_stdout() and g_test_trap_assert_stderr() |
3573 | | * cannot be used if @test_flags specifies that the child should |
3574 | | * inherit the parent stdout/stderr.) |
3575 | | * |
3576 | | * If your `main ()` needs to behave differently in |
3577 | | * the subprocess, you can call g_test_subprocess() (after calling |
3578 | | * g_test_init()) to see whether you are in a subprocess. |
3579 | | * |
3580 | | * The following example tests that calling |
3581 | | * `my_object_new(1000000)` will abort with an error |
3582 | | * message. |
3583 | | * |
3584 | | * |[<!-- language="C" --> |
3585 | | * static void |
3586 | | * test_create_large_object (void) |
3587 | | * { |
3588 | | * if (g_test_subprocess ()) |
3589 | | * { |
3590 | | * my_object_new (1000000); |
3591 | | * return; |
3592 | | * } |
3593 | | * |
3594 | | * // Reruns this same test in a subprocess |
3595 | | * g_test_trap_subprocess (NULL, 0, 0); |
3596 | | * g_test_trap_assert_failed (); |
3597 | | * g_test_trap_assert_stderr ("*ERROR*too large*"); |
3598 | | * } |
3599 | | * |
3600 | | * int |
3601 | | * main (int argc, char **argv) |
3602 | | * { |
3603 | | * g_test_init (&argc, &argv, NULL); |
3604 | | * |
3605 | | * g_test_add_func ("/myobject/create_large_object", |
3606 | | * test_create_large_object); |
3607 | | * return g_test_run (); |
3608 | | * } |
3609 | | * ]| |
3610 | | * |
3611 | | * Since: 2.38 |
3612 | | */ |
3613 | | void |
3614 | | g_test_trap_subprocess (const char *test_path, |
3615 | | guint64 usec_timeout, |
3616 | | GTestSubprocessFlags test_flags) |
3617 | 0 | { |
3618 | 0 | GError *error = NULL; |
3619 | 0 | GPtrArray *argv; |
3620 | 0 | GSpawnFlags flags; |
3621 | 0 | int stdout_fd, stderr_fd; |
3622 | 0 | GPid pid; |
3623 | | |
3624 | | /* Sanity check that they used GTestSubprocessFlags, not GTestTrapFlags */ |
3625 | 0 | g_assert ((test_flags & (G_TEST_TRAP_INHERIT_STDIN | G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) == 0); |
3626 | | |
3627 | 0 | if (test_path) |
3628 | 0 | { |
3629 | 0 | if (!g_test_suite_case_exists (g_test_get_root (), test_path)) |
3630 | 0 | g_error ("g_test_trap_subprocess: test does not exist: %s", test_path); |
3631 | 0 | } |
3632 | 0 | else |
3633 | 0 | { |
3634 | 0 | test_path = test_run_name; |
3635 | 0 | } |
3636 | |
|
3637 | 0 | if (g_test_verbose ()) |
3638 | 0 | g_print ("GTest: subprocess: %s\n", test_path); |
3639 | |
|
3640 | 0 | test_trap_clear (); |
3641 | 0 | test_trap_last_subprocess = g_strdup (test_path); |
3642 | |
|
3643 | 0 | argv = g_ptr_array_new (); |
3644 | 0 | g_ptr_array_add (argv, test_argv0); |
3645 | 0 | g_ptr_array_add (argv, "-q"); |
3646 | 0 | g_ptr_array_add (argv, "-p"); |
3647 | 0 | g_ptr_array_add (argv, (char *)test_path); |
3648 | 0 | g_ptr_array_add (argv, "--GTestSubprocess"); |
3649 | 0 | if (test_log_fd != -1) |
3650 | 0 | { |
3651 | 0 | char log_fd_buf[128]; |
3652 | |
|
3653 | 0 | g_ptr_array_add (argv, "--GTestLogFD"); |
3654 | 0 | g_snprintf (log_fd_buf, sizeof (log_fd_buf), "%d", test_log_fd); |
3655 | 0 | g_ptr_array_add (argv, log_fd_buf); |
3656 | 0 | } |
3657 | 0 | g_ptr_array_add (argv, NULL); |
3658 | |
|
3659 | 0 | flags = G_SPAWN_DO_NOT_REAP_CHILD; |
3660 | 0 | if (test_log_fd != -1) |
3661 | 0 | flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN; |
3662 | 0 | if (test_flags & G_TEST_TRAP_INHERIT_STDIN) |
3663 | 0 | flags |= G_SPAWN_CHILD_INHERITS_STDIN; |
3664 | |
|
3665 | 0 | if (!g_spawn_async_with_pipes (test_initial_cwd, |
3666 | 0 | (char **)argv->pdata, |
3667 | 0 | NULL, flags, |
3668 | 0 | NULL, NULL, |
3669 | 0 | &pid, NULL, &stdout_fd, &stderr_fd, |
3670 | 0 | &error)) |
3671 | 0 | { |
3672 | 0 | g_error ("g_test_trap_subprocess() failed: %s", |
3673 | 0 | error->message); |
3674 | 0 | } |
3675 | 0 | g_ptr_array_free (argv, TRUE); |
3676 | |
|
3677 | 0 | wait_for_child (pid, |
3678 | 0 | stdout_fd, !!(test_flags & G_TEST_SUBPROCESS_INHERIT_STDOUT), |
3679 | 0 | stderr_fd, !!(test_flags & G_TEST_SUBPROCESS_INHERIT_STDERR), |
3680 | 0 | usec_timeout); |
3681 | 0 | } |
3682 | | |
3683 | | /** |
3684 | | * g_test_subprocess: |
3685 | | * |
3686 | | * Returns %TRUE (after g_test_init() has been called) if the test |
3687 | | * program is running under g_test_trap_subprocess(). |
3688 | | * |
3689 | | * Returns: %TRUE if the test program is running under |
3690 | | * g_test_trap_subprocess(). |
3691 | | * |
3692 | | * Since: 2.38 |
3693 | | */ |
3694 | | gboolean |
3695 | | g_test_subprocess (void) |
3696 | 0 | { |
3697 | 0 | return test_in_subprocess; |
3698 | 0 | } |
3699 | | |
3700 | | /** |
3701 | | * g_test_trap_has_passed: |
3702 | | * |
3703 | | * Check the result of the last g_test_trap_subprocess() call. |
3704 | | * |
3705 | | * Returns: %TRUE if the last test subprocess terminated successfully. |
3706 | | * |
3707 | | * Since: 2.16 |
3708 | | */ |
3709 | | gboolean |
3710 | | g_test_trap_has_passed (void) |
3711 | 0 | { |
3712 | 0 | #ifdef G_OS_UNIX |
3713 | 0 | return (WIFEXITED (test_trap_last_status) && |
3714 | 0 | WEXITSTATUS (test_trap_last_status) == 0); |
3715 | | #else |
3716 | | return test_trap_last_status == 0; |
3717 | | #endif |
3718 | 0 | } |
3719 | | |
3720 | | /** |
3721 | | * g_test_trap_reached_timeout: |
3722 | | * |
3723 | | * Check the result of the last g_test_trap_subprocess() call. |
3724 | | * |
3725 | | * Returns: %TRUE if the last test subprocess got killed due to a timeout. |
3726 | | * |
3727 | | * Since: 2.16 |
3728 | | */ |
3729 | | gboolean |
3730 | | g_test_trap_reached_timeout (void) |
3731 | 0 | { |
3732 | 0 | #ifdef G_OS_UNIX |
3733 | 0 | return (WIFSIGNALED (test_trap_last_status) && |
3734 | 0 | WTERMSIG (test_trap_last_status) == SIGALRM); |
3735 | | #else |
3736 | | return test_trap_last_status == G_TEST_STATUS_TIMED_OUT; |
3737 | | #endif |
3738 | 0 | } |
3739 | | |
3740 | | static gboolean |
3741 | | log_child_output (const gchar *process_id) |
3742 | 0 | { |
3743 | 0 | gchar *escaped; |
3744 | |
|
3745 | 0 | #ifdef G_OS_UNIX |
3746 | 0 | if (WIFEXITED (test_trap_last_status)) /* normal exit */ |
3747 | 0 | { |
3748 | 0 | if (WEXITSTATUS (test_trap_last_status) == 0) |
3749 | 0 | g_test_message ("child process (%s) exit status: 0 (success)", |
3750 | 0 | process_id); |
3751 | 0 | else |
3752 | 0 | g_test_message ("child process (%s) exit status: %d (error)", |
3753 | 0 | process_id, WEXITSTATUS (test_trap_last_status)); |
3754 | 0 | } |
3755 | 0 | else if (WIFSIGNALED (test_trap_last_status) && |
3756 | 0 | WTERMSIG (test_trap_last_status) == SIGALRM) |
3757 | 0 | { |
3758 | 0 | g_test_message ("child process (%s) timed out", process_id); |
3759 | 0 | } |
3760 | 0 | else if (WIFSIGNALED (test_trap_last_status)) |
3761 | 0 | { |
3762 | 0 | const gchar *maybe_dumped_core = ""; |
3763 | |
|
3764 | 0 | #ifdef WCOREDUMP |
3765 | 0 | if (WCOREDUMP (test_trap_last_status)) |
3766 | 0 | maybe_dumped_core = ", core dumped"; |
3767 | 0 | #endif |
3768 | |
|
3769 | 0 | g_test_message ("child process (%s) killed by signal %d (%s)%s", |
3770 | 0 | process_id, WTERMSIG (test_trap_last_status), |
3771 | 0 | g_strsignal (WTERMSIG (test_trap_last_status)), |
3772 | 0 | maybe_dumped_core); |
3773 | 0 | } |
3774 | 0 | else |
3775 | 0 | { |
3776 | 0 | g_test_message ("child process (%s) unknown wait status %d", |
3777 | 0 | process_id, test_trap_last_status); |
3778 | 0 | } |
3779 | | #else |
3780 | | if (test_trap_last_status == 0) |
3781 | | g_test_message ("child process (%s) exit status: 0 (success)", |
3782 | | process_id); |
3783 | | else |
3784 | | g_test_message ("child process (%s) exit status: %d (error)", |
3785 | | process_id, test_trap_last_status); |
3786 | | #endif |
3787 | |
|
3788 | 0 | escaped = g_strescape (test_trap_last_stdout, NULL); |
3789 | 0 | g_test_message ("child process (%s) stdout: \"%s\"", process_id, escaped); |
3790 | 0 | g_free (escaped); |
3791 | |
|
3792 | 0 | escaped = g_strescape (test_trap_last_stderr, NULL); |
3793 | 0 | g_test_message ("child process (%s) stderr: \"%s\"", process_id, escaped); |
3794 | 0 | g_free (escaped); |
3795 | | |
3796 | | /* so we can use short-circuiting: |
3797 | | * logged_child_output = logged_child_output || log_child_output (...) */ |
3798 | 0 | return TRUE; |
3799 | 0 | } |
3800 | | |
3801 | | void |
3802 | | g_test_trap_assertions (const char *domain, |
3803 | | const char *file, |
3804 | | int line, |
3805 | | const char *func, |
3806 | | guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */ |
3807 | | const char *pattern) |
3808 | 0 | { |
3809 | 0 | gboolean must_pass = assertion_flags == 0; |
3810 | 0 | gboolean must_fail = assertion_flags == 1; |
3811 | 0 | gboolean match_result = 0 == (assertion_flags & 1); |
3812 | 0 | gboolean logged_child_output = FALSE; |
3813 | 0 | const char *stdout_pattern = (assertion_flags & 2) ? pattern : NULL; |
3814 | 0 | const char *stderr_pattern = (assertion_flags & 4) ? pattern : NULL; |
3815 | 0 | const char *match_error = match_result ? "failed to match" : "contains invalid match"; |
3816 | 0 | char *process_id; |
3817 | |
|
3818 | 0 | #ifdef G_OS_UNIX |
3819 | 0 | if (test_trap_last_subprocess != NULL) |
3820 | 0 | { |
3821 | 0 | process_id = g_strdup_printf ("%s [%d]", test_trap_last_subprocess, |
3822 | 0 | test_trap_last_pid); |
3823 | 0 | } |
3824 | 0 | else if (test_trap_last_pid != 0) |
3825 | 0 | process_id = g_strdup_printf ("%d", test_trap_last_pid); |
3826 | | #else |
3827 | | if (test_trap_last_subprocess != NULL) |
3828 | | process_id = g_strdup (test_trap_last_subprocess); |
3829 | | #endif |
3830 | 0 | else |
3831 | 0 | g_error ("g_test_trap_ assertion with no trapped test"); |
3832 | |
|
3833 | 0 | if (must_pass && !g_test_trap_has_passed()) |
3834 | 0 | { |
3835 | 0 | char *msg; |
3836 | |
|
3837 | 0 | logged_child_output = logged_child_output || log_child_output (process_id); |
3838 | |
|
3839 | 0 | msg = g_strdup_printf ("child process (%s) failed unexpectedly", process_id); |
3840 | 0 | g_assertion_message (domain, file, line, func, msg); |
3841 | 0 | g_free (msg); |
3842 | 0 | } |
3843 | 0 | if (must_fail && g_test_trap_has_passed()) |
3844 | 0 | { |
3845 | 0 | char *msg; |
3846 | |
|
3847 | 0 | logged_child_output = logged_child_output || log_child_output (process_id); |
3848 | |
|
3849 | 0 | msg = g_strdup_printf ("child process (%s) did not fail as expected", process_id); |
3850 | 0 | g_assertion_message (domain, file, line, func, msg); |
3851 | 0 | g_free (msg); |
3852 | 0 | } |
3853 | 0 | if (stdout_pattern && match_result == !g_pattern_match_simple (stdout_pattern, test_trap_last_stdout)) |
3854 | 0 | { |
3855 | 0 | char *msg; |
3856 | |
|
3857 | 0 | logged_child_output = logged_child_output || log_child_output (process_id); |
3858 | |
|
3859 | 0 | msg = g_strdup_printf ("stdout of child process (%s) %s: %s\nstdout was:\n%s", |
3860 | 0 | process_id, match_error, stdout_pattern, test_trap_last_stdout); |
3861 | 0 | g_assertion_message (domain, file, line, func, msg); |
3862 | 0 | g_free (msg); |
3863 | 0 | } |
3864 | 0 | if (stderr_pattern && match_result == !g_pattern_match_simple (stderr_pattern, test_trap_last_stderr)) |
3865 | 0 | { |
3866 | 0 | char *msg; |
3867 | |
|
3868 | 0 | logged_child_output = logged_child_output || log_child_output (process_id); |
3869 | |
|
3870 | 0 | msg = g_strdup_printf ("stderr of child process (%s) %s: %s\nstderr was:\n%s", |
3871 | 0 | process_id, match_error, stderr_pattern, test_trap_last_stderr); |
3872 | 0 | g_assertion_message (domain, file, line, func, msg); |
3873 | 0 | g_free (msg); |
3874 | 0 | } |
3875 | |
|
3876 | 0 | (void) logged_child_output; /* shut up scan-build about the final unread assignment */ |
3877 | |
|
3878 | 0 | g_free (process_id); |
3879 | 0 | } |
3880 | | |
3881 | | static void |
3882 | | gstring_overwrite_int (GString *gstring, |
3883 | | guint pos, |
3884 | | guint32 vuint) |
3885 | 0 | { |
3886 | 0 | vuint = g_htonl (vuint); |
3887 | 0 | g_string_overwrite_len (gstring, pos, (const gchar*) &vuint, 4); |
3888 | 0 | } |
3889 | | |
3890 | | static void |
3891 | | gstring_append_int (GString *gstring, |
3892 | | guint32 vuint) |
3893 | 0 | { |
3894 | 0 | vuint = g_htonl (vuint); |
3895 | 0 | g_string_append_len (gstring, (const gchar*) &vuint, 4); |
3896 | 0 | } |
3897 | | |
3898 | | static void |
3899 | | gstring_append_double (GString *gstring, |
3900 | | double vdouble) |
3901 | 0 | { |
3902 | 0 | union { double vdouble; guint64 vuint64; } u; |
3903 | 0 | u.vdouble = vdouble; |
3904 | 0 | u.vuint64 = GUINT64_TO_BE (u.vuint64); |
3905 | 0 | g_string_append_len (gstring, (const gchar*) &u.vuint64, 8); |
3906 | 0 | } |
3907 | | |
3908 | | static guint8* |
3909 | | g_test_log_dump (GTestLogMsg *msg, |
3910 | | guint *len) |
3911 | 0 | { |
3912 | 0 | GString *gstring = g_string_sized_new (1024); |
3913 | 0 | guint ui; |
3914 | 0 | gstring_append_int (gstring, 0); /* message length */ |
3915 | 0 | gstring_append_int (gstring, msg->log_type); |
3916 | 0 | gstring_append_int (gstring, msg->n_strings); |
3917 | 0 | gstring_append_int (gstring, msg->n_nums); |
3918 | 0 | gstring_append_int (gstring, 0); /* reserved */ |
3919 | 0 | for (ui = 0; ui < msg->n_strings; ui++) |
3920 | 0 | { |
3921 | 0 | guint l = strlen (msg->strings[ui]); |
3922 | 0 | gstring_append_int (gstring, l); |
3923 | 0 | g_string_append_len (gstring, msg->strings[ui], l); |
3924 | 0 | } |
3925 | 0 | for (ui = 0; ui < msg->n_nums; ui++) |
3926 | 0 | gstring_append_double (gstring, msg->nums[ui]); |
3927 | 0 | *len = gstring->len; |
3928 | 0 | gstring_overwrite_int (gstring, 0, *len); /* message length */ |
3929 | 0 | return (guint8*) g_string_free (gstring, FALSE); |
3930 | 0 | } |
3931 | | |
3932 | | static inline long double |
3933 | | net_double (const gchar **ipointer) |
3934 | 0 | { |
3935 | 0 | union { guint64 vuint64; double vdouble; } u; |
3936 | 0 | guint64 aligned_int64; |
3937 | 0 | memcpy (&aligned_int64, *ipointer, 8); |
3938 | 0 | *ipointer += 8; |
3939 | 0 | u.vuint64 = GUINT64_FROM_BE (aligned_int64); |
3940 | 0 | return u.vdouble; |
3941 | 0 | } |
3942 | | |
3943 | | static inline guint32 |
3944 | | net_int (const gchar **ipointer) |
3945 | 0 | { |
3946 | 0 | guint32 aligned_int; |
3947 | 0 | memcpy (&aligned_int, *ipointer, 4); |
3948 | 0 | *ipointer += 4; |
3949 | 0 | return g_ntohl (aligned_int); |
3950 | 0 | } |
3951 | | |
3952 | | static gboolean |
3953 | | g_test_log_extract (GTestLogBuffer *tbuffer) |
3954 | 0 | { |
3955 | 0 | const gchar *p = tbuffer->data->str; |
3956 | 0 | GTestLogMsg msg; |
3957 | 0 | guint mlength; |
3958 | 0 | if (tbuffer->data->len < 4 * 5) |
3959 | 0 | return FALSE; |
3960 | 0 | mlength = net_int (&p); |
3961 | 0 | if (tbuffer->data->len < mlength) |
3962 | 0 | return FALSE; |
3963 | 0 | msg.log_type = net_int (&p); |
3964 | 0 | msg.n_strings = net_int (&p); |
3965 | 0 | msg.n_nums = net_int (&p); |
3966 | 0 | if (net_int (&p) == 0) |
3967 | 0 | { |
3968 | 0 | guint ui; |
3969 | 0 | msg.strings = g_new0 (gchar*, msg.n_strings + 1); |
3970 | 0 | msg.nums = g_new0 (long double, msg.n_nums); |
3971 | 0 | for (ui = 0; ui < msg.n_strings; ui++) |
3972 | 0 | { |
3973 | 0 | guint sl = net_int (&p); |
3974 | 0 | msg.strings[ui] = g_strndup (p, sl); |
3975 | 0 | p += sl; |
3976 | 0 | } |
3977 | 0 | for (ui = 0; ui < msg.n_nums; ui++) |
3978 | 0 | msg.nums[ui] = net_double (&p); |
3979 | 0 | if (p <= tbuffer->data->str + mlength) |
3980 | 0 | { |
3981 | 0 | g_string_erase (tbuffer->data, 0, mlength); |
3982 | 0 | tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup2 (&msg, sizeof (msg))); |
3983 | 0 | return TRUE; |
3984 | 0 | } |
3985 | | |
3986 | 0 | g_free (msg.nums); |
3987 | 0 | g_strfreev (msg.strings); |
3988 | 0 | } |
3989 | | |
3990 | 0 | g_error ("corrupt log stream from test program"); |
3991 | 0 | return FALSE; |
3992 | 0 | } |
3993 | | |
3994 | | /** |
3995 | | * g_test_log_buffer_new: |
3996 | | * |
3997 | | * Internal function for gtester to decode test log messages, no ABI guarantees provided. |
3998 | | */ |
3999 | | GTestLogBuffer* |
4000 | | g_test_log_buffer_new (void) |
4001 | 0 | { |
4002 | 0 | GTestLogBuffer *tb = g_new0 (GTestLogBuffer, 1); |
4003 | 0 | tb->data = g_string_sized_new (1024); |
4004 | 0 | return tb; |
4005 | 0 | } |
4006 | | |
4007 | | /** |
4008 | | * g_test_log_buffer_free: |
4009 | | * |
4010 | | * Internal function for gtester to free test log messages, no ABI guarantees provided. |
4011 | | */ |
4012 | | void |
4013 | | g_test_log_buffer_free (GTestLogBuffer *tbuffer) |
4014 | 0 | { |
4015 | 0 | g_return_if_fail (tbuffer != NULL); |
4016 | 0 | while (tbuffer->msgs) |
4017 | 0 | g_test_log_msg_free (g_test_log_buffer_pop (tbuffer)); |
4018 | 0 | g_string_free (tbuffer->data, TRUE); |
4019 | 0 | g_free (tbuffer); |
4020 | 0 | } |
4021 | | |
4022 | | /** |
4023 | | * g_test_log_buffer_push: |
4024 | | * |
4025 | | * Internal function for gtester to decode test log messages, no ABI guarantees provided. |
4026 | | */ |
4027 | | void |
4028 | | g_test_log_buffer_push (GTestLogBuffer *tbuffer, |
4029 | | guint n_bytes, |
4030 | | const guint8 *bytes) |
4031 | 0 | { |
4032 | 0 | g_return_if_fail (tbuffer != NULL); |
4033 | 0 | if (n_bytes) |
4034 | 0 | { |
4035 | 0 | gboolean more_messages; |
4036 | 0 | g_return_if_fail (bytes != NULL); |
4037 | 0 | g_string_append_len (tbuffer->data, (const gchar*) bytes, n_bytes); |
4038 | 0 | do |
4039 | 0 | more_messages = g_test_log_extract (tbuffer); |
4040 | 0 | while (more_messages); |
4041 | 0 | } |
4042 | 0 | } |
4043 | | |
4044 | | /** |
4045 | | * g_test_log_buffer_pop: |
4046 | | * |
4047 | | * Internal function for gtester to retrieve test log messages, no ABI guarantees provided. |
4048 | | */ |
4049 | | GTestLogMsg* |
4050 | | g_test_log_buffer_pop (GTestLogBuffer *tbuffer) |
4051 | 0 | { |
4052 | 0 | GTestLogMsg *msg = NULL; |
4053 | 0 | g_return_val_if_fail (tbuffer != NULL, NULL); |
4054 | 0 | if (tbuffer->msgs) |
4055 | 0 | { |
4056 | 0 | GSList *slist = g_slist_last (tbuffer->msgs); |
4057 | 0 | msg = slist->data; |
4058 | 0 | tbuffer->msgs = g_slist_delete_link (tbuffer->msgs, slist); |
4059 | 0 | } |
4060 | 0 | return msg; |
4061 | 0 | } |
4062 | | |
4063 | | /** |
4064 | | * g_test_log_msg_free: |
4065 | | * |
4066 | | * Internal function for gtester to free test log messages, no ABI guarantees provided. |
4067 | | */ |
4068 | | void |
4069 | | g_test_log_msg_free (GTestLogMsg *tmsg) |
4070 | 0 | { |
4071 | 0 | g_return_if_fail (tmsg != NULL); |
4072 | 0 | g_strfreev (tmsg->strings); |
4073 | 0 | g_free (tmsg->nums); |
4074 | 0 | g_free (tmsg); |
4075 | 0 | } |
4076 | | |
4077 | | static gchar * |
4078 | | g_test_build_filename_va (GTestFileType file_type, |
4079 | | const gchar *first_path, |
4080 | | va_list ap) |
4081 | 0 | { |
4082 | 0 | const gchar *pathv[16]; |
4083 | 0 | gsize num_path_segments; |
4084 | |
|
4085 | 0 | if (file_type == G_TEST_DIST) |
4086 | 0 | pathv[0] = test_disted_files_dir; |
4087 | 0 | else if (file_type == G_TEST_BUILT) |
4088 | 0 | pathv[0] = test_built_files_dir; |
4089 | 0 | else |
4090 | 0 | g_assert_not_reached (); |
4091 | | |
4092 | 0 | pathv[1] = first_path; |
4093 | |
|
4094 | 0 | for (num_path_segments = 2; num_path_segments < G_N_ELEMENTS (pathv); num_path_segments++) |
4095 | 0 | { |
4096 | 0 | pathv[num_path_segments] = va_arg (ap, const char *); |
4097 | 0 | if (pathv[num_path_segments] == NULL) |
4098 | 0 | break; |
4099 | 0 | } |
4100 | |
|
4101 | 0 | g_assert_cmpint (num_path_segments, <, G_N_ELEMENTS (pathv)); |
4102 | |
|
4103 | 0 | return g_build_filenamev ((gchar **) pathv); |
4104 | 0 | } |
4105 | | |
4106 | | /** |
4107 | | * g_test_build_filename: |
4108 | | * @file_type: the type of file (built vs. distributed) |
4109 | | * @first_path: the first segment of the pathname |
4110 | | * @...: %NULL-terminated additional path segments |
4111 | | * |
4112 | | * Creates the pathname to a data file that is required for a test. |
4113 | | * |
4114 | | * This function is conceptually similar to g_build_filename() except |
4115 | | * that the first argument has been replaced with a #GTestFileType |
4116 | | * argument. |
4117 | | * |
4118 | | * The data file should either have been distributed with the module |
4119 | | * containing the test (%G_TEST_DIST) or built as part of the build |
4120 | | * system of that module (%G_TEST_BUILT). |
4121 | | * |
4122 | | * In order for this function to work in srcdir != builddir situations, |
4123 | | * the G_TEST_SRCDIR and G_TEST_BUILDDIR environment variables need to |
4124 | | * have been defined. As of 2.38, this is done by the glib.mk |
4125 | | * included in GLib. Please ensure that your copy is up to date before |
4126 | | * using this function. |
4127 | | * |
4128 | | * In case neither variable is set, this function will fall back to |
4129 | | * using the dirname portion of argv[0], possibly removing ".libs". |
4130 | | * This allows for casual running of tests directly from the commandline |
4131 | | * in the srcdir == builddir case and should also support running of |
4132 | | * installed tests, assuming the data files have been installed in the |
4133 | | * same relative path as the test binary. |
4134 | | * |
4135 | | * Returns: the path of the file, to be freed using g_free() |
4136 | | * |
4137 | | * Since: 2.38 |
4138 | | **/ |
4139 | | /** |
4140 | | * GTestFileType: |
4141 | | * @G_TEST_DIST: a file that was included in the distribution tarball |
4142 | | * @G_TEST_BUILT: a file that was built on the compiling machine |
4143 | | * |
4144 | | * The type of file to return the filename for, when used with |
4145 | | * g_test_build_filename(). |
4146 | | * |
4147 | | * These two options correspond rather directly to the 'dist' and |
4148 | | * 'built' terminology that automake uses and are explicitly used to |
4149 | | * distinguish between the 'srcdir' and 'builddir' being separate. All |
4150 | | * files in your project should either be dist (in the |
4151 | | * `EXTRA_DIST` or `dist_schema_DATA` |
4152 | | * sense, in which case they will always be in the srcdir) or built (in |
4153 | | * the `BUILT_SOURCES` sense, in which case they will |
4154 | | * always be in the builddir). |
4155 | | * |
4156 | | * Note: as a general rule of automake, files that are generated only as |
4157 | | * part of the build-from-git process (but then are distributed with the |
4158 | | * tarball) always go in srcdir (even if doing a srcdir != builddir |
4159 | | * build from git) and are considered as distributed files. |
4160 | | * |
4161 | | * Since: 2.38 |
4162 | | **/ |
4163 | | gchar * |
4164 | | g_test_build_filename (GTestFileType file_type, |
4165 | | const gchar *first_path, |
4166 | | ...) |
4167 | 0 | { |
4168 | 0 | gchar *result; |
4169 | 0 | va_list ap; |
4170 | |
|
4171 | 0 | g_assert (g_test_initialized ()); |
4172 | | |
4173 | 0 | va_start (ap, first_path); |
4174 | 0 | result = g_test_build_filename_va (file_type, first_path, ap); |
4175 | 0 | va_end (ap); |
4176 | |
|
4177 | 0 | return result; |
4178 | 0 | } |
4179 | | |
4180 | | /** |
4181 | | * g_test_get_dir: |
4182 | | * @file_type: the type of file (built vs. distributed) |
4183 | | * |
4184 | | * Gets the pathname of the directory containing test files of the type |
4185 | | * specified by @file_type. |
4186 | | * |
4187 | | * This is approximately the same as calling g_test_build_filename("."), |
4188 | | * but you don't need to free the return value. |
4189 | | * |
4190 | | * Returns: (type filename): the path of the directory, owned by GLib |
4191 | | * |
4192 | | * Since: 2.38 |
4193 | | **/ |
4194 | | const gchar * |
4195 | | g_test_get_dir (GTestFileType file_type) |
4196 | 0 | { |
4197 | 0 | g_assert (g_test_initialized ()); |
4198 | | |
4199 | 0 | if (file_type == G_TEST_DIST) |
4200 | 0 | return test_disted_files_dir; |
4201 | 0 | else if (file_type == G_TEST_BUILT) |
4202 | 0 | return test_built_files_dir; |
4203 | | |
4204 | 0 | g_assert_not_reached (); |
4205 | 0 | } |
4206 | | |
4207 | | /** |
4208 | | * g_test_get_filename: |
4209 | | * @file_type: the type of file (built vs. distributed) |
4210 | | * @first_path: the first segment of the pathname |
4211 | | * @...: %NULL-terminated additional path segments |
4212 | | * |
4213 | | * Gets the pathname to a data file that is required for a test. |
4214 | | * |
4215 | | * This is the same as g_test_build_filename() with two differences. |
4216 | | * The first difference is that must only use this function from within |
4217 | | * a testcase function. The second difference is that you need not free |
4218 | | * the return value -- it will be automatically freed when the testcase |
4219 | | * finishes running. |
4220 | | * |
4221 | | * It is safe to use this function from a thread inside of a testcase |
4222 | | * but you must ensure that all such uses occur before the main testcase |
4223 | | * function returns (ie: it is best to ensure that all threads have been |
4224 | | * joined). |
4225 | | * |
4226 | | * Returns: the path, automatically freed at the end of the testcase |
4227 | | * |
4228 | | * Since: 2.38 |
4229 | | **/ |
4230 | | const gchar * |
4231 | | g_test_get_filename (GTestFileType file_type, |
4232 | | const gchar *first_path, |
4233 | | ...) |
4234 | 0 | { |
4235 | 0 | gchar *result; |
4236 | 0 | GSList *node; |
4237 | 0 | va_list ap; |
4238 | |
|
4239 | 0 | g_assert (g_test_initialized ()); |
4240 | 0 | if (test_filename_free_list == NULL) |
4241 | 0 | g_error ("g_test_get_filename() can only be used within testcase functions"); |
4242 | |
|
4243 | 0 | va_start (ap, first_path); |
4244 | 0 | result = g_test_build_filename_va (file_type, first_path, ap); |
4245 | 0 | va_end (ap); |
4246 | |
|
4247 | 0 | node = g_slist_prepend (NULL, result); |
4248 | 0 | do |
4249 | 0 | node->next = *test_filename_free_list; |
4250 | 0 | while (!g_atomic_pointer_compare_and_exchange (test_filename_free_list, node->next, node)); |
4251 | |
|
4252 | 0 | return result; |
4253 | 0 | } |
4254 | | |
4255 | | /** |
4256 | | * g_test_get_path: |
4257 | | * |
4258 | | * Gets the test path for the test currently being run. |
4259 | | * |
4260 | | * In essence, it will be the same string passed as the first argument to |
4261 | | * e.g. g_test_add() when the test was added. |
4262 | | * |
4263 | | * This function returns a valid string only within a test function. |
4264 | | * |
4265 | | * Returns: the test path for the test currently being run |
4266 | | * |
4267 | | * Since: 2.68 |
4268 | | **/ |
4269 | | const char * |
4270 | | g_test_get_path (void) |
4271 | 0 | { |
4272 | 0 | return test_run_name; |
4273 | 0 | } |