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