/
gtestutils.c
4667 lines (4255 loc) · 140 KB
/
gtestutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GLib testing utilities
* Copyright (C) 2007 Imendio AB
* Authors: Tim Janik, Sven Herzberg
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtestutils.h"
#include "gfileutils.h"
#include <sys/types.h>
#ifdef G_OS_UNIX
#include <sys/wait.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef G_OS_WIN32
#include <crtdbg.h>
#include <io.h>
#include <windows.h>
#endif
#include <errno.h>
#include <signal.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
#include <glib/gstdio.h>
#include "gmain.h"
#include "gpattern.h"
#include "grand.h"
#include "gstrfuncs.h"
#include "gtimer.h"
#include "gslice.h"
#include "gspawn.h"
#include "glib-private.h"
#include "gutilsprivate.h"
#define TAP_VERSION G_STRINGIFY (13)
/* FIXME: Remove '#' prefix when we'll depend on a meson version supporting TAP 14
* See https://gitlab.gnome.org/GNOME/glib/-/issues/2885 */
#define TAP_SUBTEST_PREFIX "# " /* a 4-space indented line */
/**
* g_test_initialized:
*
* Returns %TRUE if g_test_init() has been called.
*
* Returns: %TRUE if g_test_init() has been called.
*
* Since: 2.36
*/
/**
* g_test_quick:
*
* Returns %TRUE if tests are run in quick mode.
* Exactly one of g_test_quick() and g_test_slow() is active in any run;
* there is no "medium speed".
*
* By default, tests are run in quick mode. In tests that use
* g_test_init(), the options `-m quick`, `-m slow` and `-m thorough`
* can be used to change this.
*
* Returns: %TRUE if in quick mode
*/
/**
* g_test_slow:
*
* Returns %TRUE if tests are run in slow mode.
* Exactly one of g_test_quick() and g_test_slow() is active in any run;
* there is no "medium speed".
*
* By default, tests are run in quick mode. In tests that use
* g_test_init(), the options `-m quick`, `-m slow` and `-m thorough`
* can be used to change this.
*
* Returns: the opposite of g_test_quick()
*/
/**
* g_test_thorough:
*
* Returns %TRUE if tests are run in thorough mode, equivalent to
* g_test_slow().
*
* By default, tests are run in quick mode. In tests that use
* g_test_init(), the options `-m quick`, `-m slow` and `-m thorough`
* can be used to change this.
*
* Returns: the same thing as g_test_slow()
*/
/**
* g_test_perf:
*
* Returns %TRUE if tests are run in performance mode.
*
* By default, tests are run in quick mode. In tests that use
* g_test_init(), the option `-m perf` enables performance tests, while
* `-m quick` disables them.
*
* Returns: %TRUE if in performance mode
*/
/**
* g_test_undefined:
*
* Returns %TRUE if tests may provoke assertions and other formally-undefined
* behaviour, to verify that appropriate warnings are given. It might, in some
* cases, be useful to turn this off with if running tests under valgrind;
* in tests that use g_test_init(), the option `-m no-undefined` disables
* those tests, while `-m undefined` explicitly enables them (normally
* the default behaviour).
*
* Since GLib 2.68, if GLib was compiled with gcc or clang and
* [AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer)
* is enabled, the default changes to not exercising undefined behaviour.
*
* Returns: %TRUE if tests may provoke programming errors
*/
/**
* g_test_verbose:
*
* Returns %TRUE if tests are run in verbose mode.
* In tests that use g_test_init(), the option `--verbose` enables this,
* while `-q` or `--quiet` disables it.
* The default is neither g_test_verbose() nor g_test_quiet().
*
* Returns: %TRUE if in verbose mode
*/
/**
* g_test_quiet:
*
* Returns %TRUE if tests are run in quiet mode.
* In tests that use g_test_init(), the option `-q` or `--quiet` enables
* this, while `--verbose` disables it.
* The default is neither g_test_verbose() nor g_test_quiet().
*
* Returns: %TRUE if in quiet mode
*/
/**
* g_test_queue_unref:
* @gobject: the object to unref
*
* Enqueue an object to be released with g_object_unref() during
* the next teardown phase. This is equivalent to calling
* g_test_queue_destroy() with a destroy callback of g_object_unref().
*
* Since: 2.16
*/
/**
* GTestSubprocessFlags:
* @G_TEST_SUBPROCESS_DEFAULT: Default behaviour. Since: 2.74
* @G_TEST_SUBPROCESS_INHERIT_STDIN: If this flag is given, the child
* process will inherit the parent's stdin. Otherwise, the child's
* stdin is redirected to `/dev/null`.
* @G_TEST_SUBPROCESS_INHERIT_STDOUT: If this flag is given, the child
* process will inherit the parent's stdout. Otherwise, the child's
* stdout will not be visible, but it will be captured to allow
* later tests with g_test_trap_assert_stdout().
* @G_TEST_SUBPROCESS_INHERIT_STDERR: If this flag is given, the child
* process will inherit the parent's stderr. Otherwise, the child's
* stderr will not be visible, but it will be captured to allow
* later tests with g_test_trap_assert_stderr().
*
* Flags to pass to g_test_trap_subprocess() to control input and output.
*
* Note that in contrast with g_test_trap_fork(), the default is to
* not show stdout and stderr.
*/
/**
* g_test_trap_assert_passed:
*
* Assert that the last test subprocess passed.
* See g_test_trap_subprocess().
*
* Since: 2.16
*/
/**
* g_test_trap_assert_failed:
*
* Assert that the last test subprocess failed.
* See g_test_trap_subprocess().
*
* This is sometimes used to test situations that are formally considered to
* be undefined behaviour, like inputs that fail a g_return_if_fail()
* check. In these situations you should skip the entire test, including the
* call to g_test_trap_subprocess(), unless g_test_undefined() returns %TRUE
* to indicate that undefined behaviour may be tested.
*
* Since: 2.16
*/
/**
* g_test_trap_assert_stdout:
* @soutpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
*
* Assert that the stdout output of the last test subprocess matches
* @soutpattern. See g_test_trap_subprocess().
*
* Since: 2.16
*/
/**
* g_test_trap_assert_stdout_unmatched:
* @soutpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
*
* Assert that the stdout output of the last test subprocess
* does not match @soutpattern. See g_test_trap_subprocess().
*
* Since: 2.16
*/
/**
* g_test_trap_assert_stderr:
* @serrpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
*
* Assert that the stderr output of the last test subprocess
* matches @serrpattern. See g_test_trap_subprocess().
*
* This is sometimes used to test situations that are formally
* considered to be undefined behaviour, like code that hits a
* g_assert() or g_error(). In these situations you should skip the
* entire test, including the call to g_test_trap_subprocess(), unless
* g_test_undefined() returns %TRUE to indicate that undefined
* behaviour may be tested.
*
* Since: 2.16
*/
/**
* g_test_trap_assert_stderr_unmatched:
* @serrpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
*
* Assert that the stderr output of the last test subprocess
* does not match @serrpattern. See g_test_trap_subprocess().
*
* Since: 2.16
*/
/**
* g_test_rand_bit:
*
* Get a reproducible random bit (0 or 1), see g_test_rand_int()
* for details on test case random numbers.
*
* Since: 2.16
*/
/**
* g_assert:
* @expr: the expression to check
*
* Debugging macro to terminate the application if the assertion
* fails. If the assertion fails (i.e. the expression is not true),
* an error message is logged and the application is terminated.
*
* The macro can be turned off in final releases of code by defining
* `G_DISABLE_ASSERT` when compiling the application, so code must
* not depend on any side effects from @expr. Similarly, it must not be used
* in unit tests, otherwise the unit tests will be ineffective if compiled with
* `G_DISABLE_ASSERT`. Use g_assert_true() and related macros in unit tests
* instead.
*/
/**
* g_assert_not_reached:
*
* Debugging macro to terminate the application if it is ever
* reached. If it is reached, an error message is logged and the
* application is terminated.
*
* The macro can be turned off in final releases of code by defining
* `G_DISABLE_ASSERT` when compiling the application. Hence, it should not be
* used in unit tests, where assertions should always be effective.
*/
/**
* g_assert_true:
* @expr: the expression to check
*
* Debugging macro to check that an expression is true.
*
* If the assertion fails (i.e. the expression is not true),
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.38
*/
/**
* g_assert_false:
* @expr: the expression to check
*
* Debugging macro to check an expression is false.
*
* If the assertion fails (i.e. the expression is not false),
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.38
*/
/**
* g_assert_null:
* @expr: the expression to check
*
* Debugging macro to check an expression is %NULL.
*
* If the assertion fails (i.e. the expression is not %NULL),
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.38
*/
/**
* g_assert_nonnull:
* @expr: the expression to check
*
* Debugging macro to check an expression is not %NULL.
*
* If the assertion fails (i.e. the expression is %NULL),
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.40
*/
/**
* g_assert_cmpstr:
* @s1: a string (may be %NULL)
* @cmp: The comparison operator to use.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @s2: another string (may be %NULL)
*
* Debugging macro to compare two strings. If the comparison fails,
* an error message is logged and the application is either terminated
* or the testcase marked as failed.
* The strings are compared using g_strcmp0().
*
* The effect of `g_assert_cmpstr (s1, op, s2)` is
* the same as `g_assert_true (g_strcmp0 (s1, s2) op 0)`.
* The advantage of this macro is that it can produce a message that
* includes the actual values of @s1 and @s2.
*
* |[<!-- language="C" -->
* g_assert_cmpstr (mystring, ==, "fubar");
* ]|
*
* Since: 2.16
*/
/**
* g_assert_cmpstrv:
* @strv1: (nullable): a string array (may be %NULL)
* @strv2: (nullable): another string array (may be %NULL)
*
* Debugging macro to check if two %NULL-terminated string arrays (i.e. 2
* #GStrv) are equal. If they are not equal, an error message is logged and the
* application is either terminated or the testcase marked as failed.
* If both arrays are %NULL, the check passes. If one array is %NULL but the
* other is not, an error message is logged.
*
* The effect of `g_assert_cmpstrv (strv1, strv2)` is the same as
* `g_assert_true (g_strv_equal (strv1, strv2))` (if both arrays are not
* %NULL). The advantage of this macro is that it can produce a message that
* includes how @strv1 and @strv2 are different.
*
* |[<!-- language="C" -->
* const char *expected[] = { "one", "two", "three", NULL };
* g_assert_cmpstrv (mystrv, expected);
* ]|
*
* Since: 2.68
*/
/**
* g_assert_cmpint:
* @n1: an integer
* @cmp: The comparison operator to use.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another integer
*
* Debugging macro to compare two integers.
*
* The effect of `g_assert_cmpint (n1, op, n2)` is
* the same as `g_assert_true (n1 op n2)`. The advantage
* of this macro is that it can produce a message that includes the
* actual values of @n1 and @n2.
*
* Since: 2.16
*/
/**
* g_assert_cmpuint:
* @n1: an unsigned integer
* @cmp: The comparison operator to use.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another unsigned integer
*
* Debugging macro to compare two unsigned integers.
*
* The effect of `g_assert_cmpuint (n1, op, n2)` is
* the same as `g_assert_true (n1 op n2)`. The advantage
* of this macro is that it can produce a message that includes the
* actual values of @n1 and @n2.
*
* Since: 2.16
*/
/**
* g_assert_cmphex:
* @n1: an unsigned integer
* @cmp: The comparison operator to use.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another unsigned integer
*
* Debugging macro to compare to unsigned integers.
*
* This is a variant of g_assert_cmpuint() that displays the numbers
* in hexadecimal notation in the message.
*
* Since: 2.16
*/
/**
* g_assert_cmpfloat:
* @n1: a floating point number
* @cmp: The comparison operator to use.
* One of `==`, `!=`, `<`, `>`, `<=`, `>=`.
* @n2: another floating point number
*
* Debugging macro to compare two floating point numbers.
*
* The effect of `g_assert_cmpfloat (n1, op, n2)` is
* the same as `g_assert_true (n1 op n2)`. The advantage
* of this macro is that it can produce a message that includes the
* actual values of @n1 and @n2.
*
* Since: 2.16
*/
/**
* g_assert_cmpfloat_with_epsilon:
* @n1: a floating point number
* @n2: another floating point number
* @epsilon: a numeric value that expresses the expected tolerance
* between @n1 and @n2
*
* Debugging macro to compare two floating point numbers within an epsilon.
*
* The effect of `g_assert_cmpfloat_with_epsilon (n1, n2, epsilon)` is
* the same as `g_assert_true (abs (n1 - n2) < epsilon)`. The advantage
* of this macro is that it can produce a message that includes the
* actual values of @n1 and @n2.
*
* Since: 2.58
*/
/**
* g_assert_no_errno:
* @expr: the expression to check
*
* Debugging macro to check that an expression has a non-negative return value,
* as used by traditional POSIX functions (such as `rmdir()`) to indicate
* success.
*
* If the assertion fails (i.e. the @expr returns a negative value), an error
* message is logged and the testcase is marked as failed. The error message
* will contain the value of `errno` and its human-readable message from
* g_strerror().
*
* This macro will clear the value of `errno` before executing @expr.
*
* Since: 2.66
*/
/**
* g_assert_cmpmem:
* @m1: (nullable): pointer to a buffer
* @l1: length of @m1
* @m2: (nullable): pointer to another buffer
* @l2: length of @m2
*
* Debugging macro to compare memory regions. If the comparison fails,
* an error message is logged and the application is either terminated
* or the testcase marked as failed.
*
* The effect of `g_assert_cmpmem (m1, l1, m2, l2)` is
* the same as `g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)`.
* The advantage of this macro is that it can produce a message that
* includes the actual values of @l1 and @l2.
*
* @m1 may be %NULL if (and only if) @l1 is zero; similarly for @m2 and @l2.
*
* |[<!-- language="C" -->
* g_assert_cmpmem (buf->data, buf->len, expected, sizeof (expected));
* ]|
*
* Since: 2.46
*/
/**
* g_assert_cmpvariant:
* @v1: pointer to a #GVariant
* @v2: pointer to another #GVariant
*
* Debugging macro to compare two #GVariants. If the comparison fails,
* an error message is logged and the application is either terminated
* or the testcase marked as failed. The variants are compared using
* g_variant_equal().
*
* The effect of `g_assert_cmpvariant (v1, v2)` is the same as
* `g_assert_true (g_variant_equal (v1, v2))`. The advantage of this macro is
* that it can produce a message that includes the actual values of @v1 and @v2.
*
* Since: 2.60
*/
/**
* g_assert_no_error:
* @err: a #GError, possibly %NULL
*
* Debugging macro to check that a #GError is not set.
*
* The effect of `g_assert_no_error (err)` is
* the same as `g_assert_true (err == NULL)`. The advantage
* of this macro is that it can produce a message that includes
* the error message and code.
*
* Since: 2.20
*/
/**
* g_assert_error:
* @err: a #GError, possibly %NULL
* @dom: the expected error domain (a #GQuark)
* @c: the expected error code
*
* Debugging macro to check that a method has returned
* the correct #GError.
*
* The effect of `g_assert_error (err, dom, c)` is
* the same as `g_assert_true (err != NULL && err->domain
* == dom && err->code == c)`. The advantage of this
* macro is that it can produce a message that includes the incorrect
* error message and code.
*
* This can only be used to test for a specific error. If you want to
* test that @err is set, but don't care what it's set to, just use
* `g_assert_nonnull (err)`.
*
* Since: 2.20
*/
/**
* GTestCase:
*
* An opaque structure representing a test case.
*/
/**
* GTestSuite:
*
* An opaque structure representing a test suite.
*/
/* Global variable for storing assertion messages; this is the counterpart to
* glibc's (private) __abort_msg variable, and allows developers and crash
* analysis systems like Apport and ABRT to fish out assertion messages from
* core dumps, instead of having to catch them on screen output.
*/
GLIB_VAR char *__glib_assert_msg;
char *__glib_assert_msg = NULL;
/* --- constants --- */
#define G_TEST_STATUS_TIMED_OUT 1024
/* --- structures --- */
struct GTestCase
{
gchar *name;
guint fixture_size;
void (*fixture_setup) (void*, gconstpointer);
void (*fixture_test) (void*, gconstpointer);
void (*fixture_teardown) (void*, gconstpointer);
gpointer test_data;
};
struct GTestSuite
{
gchar *name;
GSList *suites;
GSList *cases;
};
typedef struct DestroyEntry DestroyEntry;
struct DestroyEntry
{
DestroyEntry *next;
GDestroyNotify destroy_func;
gpointer destroy_data;
};
/* --- prototypes --- */
static void test_cleanup (void);
static void test_run_seed (const gchar *rseed);
static void test_trap_clear (void);
static guint8* g_test_log_dump (GTestLogMsg *msg,
guint *len);
static void gtest_default_log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer unused_data);
static void g_test_tap_print (unsigned subtest_level,
gboolean commented,
const char *format,
...) G_GNUC_PRINTF (3, 4);
static const char * const g_test_result_names[] = {
"OK",
"SKIP",
"FAIL",
"TODO"
};
/* --- variables --- */
static int test_log_fd = -1;
static gboolean test_mode_fatal = TRUE;
static gboolean g_test_run_once = TRUE;
static gboolean test_isolate_dirs = FALSE;
static gchar *test_isolate_dirs_tmpdir = NULL;
static const gchar *test_tmpdir = NULL;
static gboolean test_run_list = FALSE;
static gchar *test_run_seedstr = NULL;
G_LOCK_DEFINE_STATIC (test_run_rand);
static GRand *test_run_rand = NULL;
static gchar *test_run_name = "";
static gchar *test_run_name_path = "";
static GSList **test_filename_free_list;
static guint test_run_forks = 0;
static guint test_run_count = 0;
static guint test_count = 0;
static guint test_skipped_count = 0;
static GTestResult test_run_success = G_TEST_RUN_FAILURE;
static gchar *test_run_msg = NULL;
static guint test_startup_skip_count = 0;
static GTimer *test_user_timer = NULL;
static double test_user_stamp = 0;
static GSList *test_paths = NULL;
static gboolean test_prefix = FALSE;
static gboolean test_prefix_extended = FALSE;
static GSList *test_paths_skipped = NULL;
static gboolean test_prefix_skipped = FALSE;
static gboolean test_prefix_extended_skipped = FALSE;
static GTestSuite *test_suite_root = NULL;
static int test_trap_last_status = 0; /* unmodified platform-specific status */
static GPid test_trap_last_pid = 0;
static char *test_trap_last_subprocess = NULL;
static char *test_trap_last_stdout = NULL;
static char *test_trap_last_stderr = NULL;
static char *test_uri_base = NULL;
static gboolean test_debug_log = FALSE;
static gboolean test_tap_log = TRUE; /* default to TAP as of GLib 2.62; see #1619; the non-TAP output mode is deprecated */
static gboolean test_nonfatal_assertions = FALSE;
static DestroyEntry *test_destroy_queue = NULL;
static const char *test_argv0 = NULL; /* (nullable), points into global argv */
static char *test_argv0_dirname = NULL; /* owned by GLib */
static const char *test_disted_files_dir; /* points into test_argv0_dirname or an environment variable */
static const char *test_built_files_dir; /* points into test_argv0_dirname or an environment variable */
static char *test_initial_cwd = NULL;
static gboolean test_in_forked_child = FALSE;
static gboolean test_in_subprocess = FALSE;
static gboolean test_is_subtest = FALSE;
static GTestConfig mutable_test_config_vars = {
FALSE, /* test_initialized */
TRUE, /* test_quick */
FALSE, /* test_perf */
FALSE, /* test_verbose */
FALSE, /* test_quiet */
TRUE, /* test_undefined */
};
const GTestConfig * const g_test_config_vars = &mutable_test_config_vars;
static gboolean no_g_set_prgname = FALSE;
static GPrintFunc g_default_print_func = NULL;
enum
{
G_TEST_CASE_LARGS_RESULT = 0,
G_TEST_CASE_LARGS_RUN_FORKS = 1,
G_TEST_CASE_LARGS_EXECUTION_TIME = 2,
G_TEST_CASE_LARGS_MAX
};
/* --- functions --- */
static inline gboolean
is_subtest (void)
{
return test_is_subtest || test_in_forked_child || test_in_subprocess;
}
static void
g_test_print_handler_full (const gchar *string,
gboolean use_tap_format,
gboolean is_tap_comment,
unsigned subtest_level)
{
g_assert (string != NULL);
if (G_LIKELY (use_tap_format) && strchr (string, '\n') != NULL)
{
static gboolean last_had_final_newline = TRUE;
GString *output = g_string_new_len (NULL, strlen (string) + 2);
const char *line = string;
do
{
const char *next = strchr (line, '\n');
if (last_had_final_newline && (next || *line != '\0'))
{
for (unsigned l = 0; l < subtest_level; ++l)
g_string_append (output, TAP_SUBTEST_PREFIX);
if G_LIKELY (is_tap_comment)
g_string_append (output, "# ");
}
if (next)
{
next += 1; /* Include the newline */
g_string_append_len (output, line, next - line);
}
else
{
g_string_append (output, line);
last_had_final_newline = (*line == '\0');
}
line = next;
}
while (line != NULL);
g_default_print_func (output->str);
g_string_free (g_steal_pointer (&output), TRUE);
}
else
{
g_default_print_func (string);
}
}
static void
g_test_print_handler (const gchar *string)
{
g_test_print_handler_full (string, test_tap_log, TRUE, is_subtest () ? 1 : 0);
}
static void
g_test_tap_print (unsigned subtest_level,
gboolean commented,
const char *format,
...)
{
va_list args;
char *string;
va_start (args, format);
string = g_strdup_vprintf (format, args);
va_end (args);
g_test_print_handler_full (string, TRUE, commented, subtest_level);
g_free (string);
}
const char*
g_test_log_type_name (GTestLogType log_type)
{
switch (log_type)
{
case G_TEST_LOG_NONE: return "none";
case G_TEST_LOG_ERROR: return "error";
case G_TEST_LOG_START_BINARY: return "binary";
case G_TEST_LOG_LIST_CASE: return "list";
case G_TEST_LOG_SKIP_CASE: return "skip";
case G_TEST_LOG_START_CASE: return "start";
case G_TEST_LOG_STOP_CASE: return "stop";
case G_TEST_LOG_MIN_RESULT: return "minperf";
case G_TEST_LOG_MAX_RESULT: return "maxperf";
case G_TEST_LOG_MESSAGE: return "message";
case G_TEST_LOG_START_SUITE: return "start suite";
case G_TEST_LOG_STOP_SUITE: return "stop suite";
}
return "???";
}
static void
g_test_log_send (guint n_bytes,
const guint8 *buffer)
{
if (test_log_fd >= 0)
{
int r;
do
r = write (test_log_fd, buffer, n_bytes);
while (r < 0 && errno == EINTR);
}
if (test_debug_log)
{
GTestLogBuffer *lbuffer = g_test_log_buffer_new ();
GTestLogMsg *msg;
GString *output;
guint ui;
g_test_log_buffer_push (lbuffer, n_bytes, buffer);
msg = g_test_log_buffer_pop (lbuffer);
g_warn_if_fail (msg != NULL);
g_warn_if_fail (lbuffer->data->len == 0);
g_test_log_buffer_free (lbuffer);
/* print message */
output = g_string_new (NULL);
g_string_printf (output, "{*LOG(%s)", g_test_log_type_name (msg->log_type));
for (ui = 0; ui < msg->n_strings; ui++)
g_string_append_printf (output, ":{%s}", msg->strings[ui]);
if (msg->n_nums)
{
g_string_append (output, ":(");
for (ui = 0; ui < msg->n_nums; ui++)
{
if ((long double) (long) msg->nums[ui] == msg->nums[ui])
g_string_append_printf (output, "%s%ld", ui ? ";" : "", (long) msg->nums[ui]);
else
g_string_append_printf (output, "%s%.16g", ui ? ";" : "", (double) msg->nums[ui]);
}
g_string_append_c (output, ')');
}
g_string_append (output, ":LOG*}");
g_printerr ("%s\n", output->str);
g_string_free (output, TRUE);
g_test_log_msg_free (msg);
}
}
static void
g_test_log (GTestLogType lbit,
const gchar *string1,
const gchar *string2,
guint n_args,
long double *largs)
{
GTestResult result;
gboolean fail;
GTestLogMsg msg;
gchar *astrings[3] = { NULL, NULL, NULL };
guint8 *dbuffer;
guint32 dbufferlen;
unsigned subtest_level;
gdouble timing;
if (g_once_init_enter_pointer (&g_default_print_func))
{
g_once_init_leave_pointer (&g_default_print_func,
g_set_print_handler (g_test_print_handler));
g_assert_nonnull (g_default_print_func);
}
subtest_level = is_subtest () ? 1 : 0;
switch (lbit)
{
case G_TEST_LOG_START_BINARY:
if (test_tap_log)
{
if (!is_subtest ())
{
g_test_tap_print (0, FALSE, "TAP version " TAP_VERSION "\n");
}
else
{
g_test_tap_print (subtest_level > 0 ? subtest_level - 1 : 0, TRUE,
"Subtest: %s\n", test_argv0);
}
g_print ("random seed: %s\n", string2);
}
else if (g_test_verbose ())
{
g_print ("GTest: random seed: %s\n", string2);
}
break;
case G_TEST_LOG_START_SUITE:
if (test_tap_log)
{
/* We only print the TAP "plan" (1..n) ahead of time if we did
* not use the -p option to select specific tests to be run. */
if (string1[0] != 0)
g_print ("Start of %s tests\n", string1);
else if (test_paths == NULL)
g_test_tap_print (subtest_level, FALSE, "1..%d\n", test_count);
}
break;
case G_TEST_LOG_STOP_SUITE:
if (test_tap_log)
{
/* If we didn't print the TAP "plan" at the beginning because
* we were using -p, we need to print how many tests we ran at
* the end instead. */
if (string1[0] != 0)
g_print ("End of %s tests\n", string1);
else if (test_paths != NULL)
g_test_tap_print (subtest_level, FALSE, "1..%d\n", test_run_count);
}
break;
case G_TEST_LOG_STOP_CASE:
result = largs[G_TEST_CASE_LARGS_RESULT];
timing = largs[G_TEST_CASE_LARGS_EXECUTION_TIME];
fail = result == G_TEST_RUN_FAILURE;
if (test_tap_log)
{
GString *tap_output;
/* The TAP representation for an expected failure starts with
* "not ok", even though it does not actually count as failing
* due to the use of the TODO directive. "ok # TODO" would mean
* a test that was expected to fail unexpectedly succeeded,
* for which GTestResult does not currently have a
* representation. */
if (fail || result == G_TEST_RUN_INCOMPLETE)
tap_output = g_string_new ("not ok");
else
tap_output = g_string_new ("ok");
if (is_subtest ())
g_string_prepend (tap_output, TAP_SUBTEST_PREFIX);
g_string_append_printf (tap_output, " %d %s", test_run_count, string1);
if (result == G_TEST_RUN_INCOMPLETE)
g_string_append_printf (tap_output, " # TODO %s", string2 ? string2 : "");
else if (result == G_TEST_RUN_SKIPPED)
g_string_append_printf (tap_output, " # SKIP %s", string2 ? string2 : "");
else if (result == G_TEST_RUN_FAILURE && string2 != NULL)
g_string_append_printf (tap_output, " - %s", string2);