Fix up failure-to-see-expected-message logging
[platform/upstream/glib.git] / glib / tests / testing.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2007 Imendio AB
3  * Authors: Tim Janik
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22
23 /* We want to distinguish between messages originating from libglib
24  * and messages originating from this program.
25  */
26 #undef G_LOG_DOMAIN
27 #define G_LOG_DOMAIN "testing"
28
29 #include <glib.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 /* test assertion variants */
35 static void
36 test_assertions_bad_cmpstr (void)
37 {
38   g_assert_cmpstr ("fzz", !=, "fzz");
39   exit (0);
40 }
41
42 static void
43 test_assertions_bad_cmpint (void)
44 {
45   g_assert_cmpint (4, !=, 4);
46   exit (0);
47 }
48
49 static void
50 test_assertions (void)
51 {
52   gchar *fuu;
53   g_assert_cmpint (1, >, 0);
54   g_assert_cmphex (2, ==, 2);
55   g_assert_cmpfloat (3.3, !=, 7);
56   g_assert_cmpfloat (7, <=, 3 + 4);
57   g_assert (TRUE);
58   g_assert_cmpstr ("foo", !=, "faa");
59   fuu = g_strdup_printf ("f%s", "uu");
60   g_test_queue_free (fuu);
61   g_assert_cmpstr ("foo", !=, fuu);
62   g_assert_cmpstr ("fuu", ==, fuu);
63   g_assert_cmpstr (NULL, <, "");
64   g_assert_cmpstr (NULL, ==, NULL);
65   g_assert_cmpstr ("", >, NULL);
66   g_assert_cmpstr ("foo", <, "fzz");
67   g_assert_cmpstr ("fzz", >, "faa");
68   g_assert_cmpstr ("fzz", ==, "fzz");
69
70   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstr", 0, 0);
71   g_test_trap_assert_failed ();
72   g_test_trap_assert_stderr ("*assertion failed*");
73
74   g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpint", 0, 0);
75   g_test_trap_assert_failed ();
76   g_test_trap_assert_stderr ("*assertion failed*");
77 }
78
79 /* test g_test_timer* API */
80 static void
81 test_timer (void)
82 {
83   double ttime;
84   g_test_timer_start();
85   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
86   g_usleep (25 * 1000);
87   ttime = g_test_timer_elapsed();
88   g_assert_cmpfloat (ttime, >, 0);
89   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
90   g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
91   g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
92 }
93
94 #ifdef G_OS_UNIX
95 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
96
97 /* fork out for a failing test */
98 static void
99 test_fork_fail (void)
100 {
101   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
102     {
103       g_assert_not_reached();
104     }
105   g_test_trap_assert_failed();
106   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
107 }
108
109 /* fork out to assert stdout and stderr patterns */
110 static void
111 test_fork_patterns (void)
112 {
113   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
114     {
115       g_print ("some stdout text: somagic17\n");
116       g_printerr ("some stderr text: semagic43\n");
117       exit (0);
118     }
119   g_test_trap_assert_passed();
120   g_test_trap_assert_stdout ("*somagic17*");
121   g_test_trap_assert_stderr ("*semagic43*");
122 }
123
124 /* fork out for a timeout test */
125 static void
126 test_fork_timeout (void)
127 {
128   /* allow child to run for only a fraction of a second */
129   if (g_test_trap_fork (0.11 * 1000000, 0))
130     {
131       /* loop and sleep forever */
132       while (TRUE)
133         g_usleep (1000 * 1000);
134     }
135   g_test_trap_assert_failed();
136   g_assert (g_test_trap_reached_timeout());
137 }
138
139 G_GNUC_END_IGNORE_DEPRECATIONS
140 #endif /* G_OS_UNIX */
141
142 static void
143 test_subprocess_fail (void)
144 {
145   if (g_test_subprocess ())
146     {
147       g_assert_not_reached ();
148       return;
149     }
150
151   g_test_trap_subprocess (NULL, 0, 0);
152   g_test_trap_assert_failed ();
153   g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail*should not be reached*");
154 }
155
156 static void
157 test_subprocess_no_such_test (void)
158 {
159   if (g_test_subprocess ())
160     {
161       g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0, 0);
162       g_assert_not_reached ();
163       return;
164     }
165   g_test_trap_subprocess (NULL, 0, 0);
166   g_test_trap_assert_failed ();
167   g_test_trap_assert_stderr ("*test does not exist*");
168   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
169 }
170
171 static void
172 test_subprocess_patterns (void)
173 {
174   if (g_test_subprocess ())
175     {
176       g_print ("some stdout text: somagic17\n");
177       g_printerr ("some stderr text: semagic43\n");
178       exit (0);
179     }
180   g_test_trap_subprocess (NULL, 0,  0);
181   g_test_trap_assert_passed ();
182   g_test_trap_assert_stdout ("*somagic17*");
183   g_test_trap_assert_stderr ("*semagic43*");
184 }
185
186 static void
187 test_subprocess_timeout (void)
188 {
189   if (g_test_subprocess ())
190     {
191       /* loop and sleep forever */
192       while (TRUE)
193         g_usleep (1000 * 1000);
194       return;
195     }
196   /* allow child to run for only a fraction of a second */
197   g_test_trap_subprocess (NULL, 0.11 * 1000000, 0);
198   g_test_trap_assert_failed ();
199   g_assert (g_test_trap_reached_timeout ());
200 }
201
202 /* run a test with fixture setup and teardown */
203 typedef struct {
204   guint  seed;
205   guint  prime;
206   gchar *msg;
207 } Fixturetest;
208 static void
209 fixturetest_setup (Fixturetest  *fix,
210                    gconstpointer test_data)
211 {
212   g_assert (test_data == (void*) 0xc0cac01a);
213   fix->seed = 18;
214   fix->prime = 19;
215   fix->msg = g_strdup_printf ("%d", fix->prime);
216 }
217 static void
218 fixturetest_test (Fixturetest  *fix,
219                   gconstpointer test_data)
220 {
221   guint prime = g_spaced_primes_closest (fix->seed);
222   g_assert_cmpint (prime, ==, fix->prime);
223   prime = g_ascii_strtoull (fix->msg, NULL, 0);
224   g_assert_cmpint (prime, ==, fix->prime);
225   g_assert (test_data == (void*) 0xc0cac01a);
226 }
227 static void
228 fixturetest_teardown (Fixturetest  *fix,
229                       gconstpointer test_data)
230 {
231   g_assert (test_data == (void*) 0xc0cac01a);
232   g_free (fix->msg);
233 }
234
235 static struct {
236   int bit, vint1, vint2, irange;
237   long double vdouble, drange;
238 } shared_rand_state;
239
240 static void
241 test_rand1 (void)
242 {
243   shared_rand_state.bit = g_test_rand_bit();
244   shared_rand_state.vint1 = g_test_rand_int();
245   shared_rand_state.vint2 = g_test_rand_int();
246   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
247   shared_rand_state.irange = g_test_rand_int_range (17, 35);
248   g_assert_cmpint (shared_rand_state.irange, >=, 17);
249   g_assert_cmpint (shared_rand_state.irange, <=, 35);
250   shared_rand_state.vdouble = g_test_rand_double();
251   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
252   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
253   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
254 }
255
256 static void
257 test_rand2 (void)
258 {
259   /* this test only works if run after test1.
260    * we do this to check that random number generators
261    * are reseeded upon fixture setup.
262    */
263   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
264   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
265   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
266   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
267   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
268   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
269 }
270
271 static void
272 test_data_test (gconstpointer test_data)
273 {
274   g_assert (test_data == (void*) 0xc0c0baba);
275 }
276
277 static void
278 test_random_conversions (void)
279 {
280   /* very simple conversion test using random numbers */
281   int vint = g_test_rand_int();
282   char *err, *str = g_strdup_printf ("%d", vint);
283   gint64 vint64 = g_ascii_strtoll (str, &err, 10);
284   g_assert_cmphex (vint, ==, vint64);
285   g_assert (!err || *err == 0);
286   g_free (str);
287 }
288
289 static gboolean
290 fatal_handler (const gchar    *log_domain,
291                GLogLevelFlags  log_level,
292                const gchar    *message,
293                gpointer        user_data)
294 {
295   return FALSE;
296 }
297
298 static void
299 test_fatal_log_handler_critical_pass (void)
300 {
301   g_test_log_set_fatal_handler (fatal_handler, NULL);
302   g_str_has_prefix (NULL, "file://");
303   g_critical ("Test passing");
304   exit (0);
305 }
306
307 static void
308 test_fatal_log_handler_error_fail (void)
309 {
310   g_error ("Test failing");
311   exit (0);
312 }
313
314 static void
315 test_fatal_log_handler_critical_fail (void)
316 {
317   g_str_has_prefix (NULL, "file://");
318   g_critical ("Test passing");
319   exit (0);
320 }
321
322 static void
323 test_fatal_log_handler (void)
324 {
325   g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-pass", 0, 0);
326   g_test_trap_assert_passed ();
327   g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
328   g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
329
330   g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/error-fail", 0, 0);
331   g_test_trap_assert_failed ();
332   g_test_trap_assert_stderr ("*ERROR*Test failing*");
333
334   g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-fail", 0, 0);
335   g_test_trap_assert_failed ();
336   g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
337   g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
338 }
339
340 static void
341 test_expected_messages_warning (void)
342 {
343   g_warning ("This is a %d warning", g_random_int ());
344   g_return_if_reached ();
345 }
346
347 static void
348 test_expected_messages_expect_warning (void)
349 {
350   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
351                          "This is a * warning");
352   test_expected_messages_warning ();
353 }
354
355 static void
356 test_expected_messages_wrong_warning (void)
357 {
358   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
359                          "*should not be *");
360   test_expected_messages_warning ();
361 }
362
363 static void
364 test_expected_messages_expected (void)
365 {
366   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
367                          "This is a * warning");
368   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
369                          "*should not be reached");
370
371   test_expected_messages_warning ();
372
373   g_test_assert_expected_messages ();
374   exit (0);
375 }
376
377 static void
378 test_expected_messages_null_domain (void)
379 {
380   g_test_expect_message (NULL, G_LOG_LEVEL_WARNING, "no domain");
381   g_log (NULL, G_LOG_LEVEL_WARNING, "no domain");
382   g_test_assert_expected_messages ();
383 }
384
385 static void
386 test_expected_messages_expect_error (void)
387 {
388   /* make sure we can't try to expect a g_error() */
389   g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*");
390   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "this won't work");
391   g_test_assert_expected_messages ();
392 }
393
394 static void
395 test_expected_messages_extra_warning (void)
396 {
397   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
398                          "This is a * warning");
399   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
400                          "*should not be reached");
401   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
402                          "nope");
403
404   test_expected_messages_warning ();
405
406   /* If we don't assert, it won't notice the missing message */
407   exit (0);
408 }
409
410 static void
411 test_expected_messages_unexpected_extra_warning (void)
412 {
413   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
414                          "This is a * warning");
415   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
416                          "*should not be reached");
417   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
418                          "nope");
419
420   test_expected_messages_warning ();
421
422   g_test_assert_expected_messages ();
423   exit (0);
424 }
425
426 static void
427 test_expected_messages (void)
428 {
429   g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0, 0);
430   g_test_trap_assert_failed ();
431   g_test_trap_assert_stderr ("*This is a * warning*");
432   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
433
434   g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0, 0);
435   g_test_trap_assert_failed ();
436   g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
437   g_test_trap_assert_stderr ("*should not be reached*");
438
439   g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0);
440   g_test_trap_assert_failed ();
441   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
442   g_test_trap_assert_stderr ("*GLib-CRITICAL*Did not see expected message testing-CRITICAL*should not be *WARNING*This is a * warning*");
443
444   g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0);
445   g_test_trap_assert_passed ();
446   g_test_trap_assert_stderr ("");
447
448   g_test_trap_subprocess ("/misc/expected-messages/subprocess/null-domain", 0, 0);
449   g_test_trap_assert_passed ();
450   g_test_trap_assert_stderr ("");
451
452   g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0);
453   g_test_trap_assert_passed ();
454   g_test_trap_assert_stderr ("");
455
456   g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0);
457   g_test_trap_assert_failed ();
458   g_test_trap_assert_stderr ("*GLib:ERROR*Did not see expected message testing-CRITICAL*nope*");
459 }
460
461 static void
462 test_expected_messages_debug (void)
463 {
464   g_test_expect_message ("Test", G_LOG_LEVEL_WARNING, "warning message");
465   g_log ("Test", G_LOG_LEVEL_DEBUG, "should be ignored");
466   g_log ("Test", G_LOG_LEVEL_WARNING, "warning message");
467   g_test_assert_expected_messages ();
468
469   g_test_expect_message ("Test", G_LOG_LEVEL_DEBUG, "debug message");
470   g_log ("Test", G_LOG_LEVEL_DEBUG, "debug message");
471   g_test_assert_expected_messages ();
472 }
473
474 static void
475 test_dash_p_hidden (void)
476 {
477   if (!g_test_subprocess ())
478     g_assert_not_reached ();
479
480   g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
481 }
482
483 static void
484 test_dash_p_hidden_sub (void)
485 {
486   if (!g_test_subprocess ())
487     g_assert_not_reached ();
488
489   g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
490 }
491
492 /* The rest of the dash_p tests will get run by the toplevel test
493  * process, but they shouldn't do anything there.
494  */
495
496 static void
497 test_dash_p_child (void)
498 {
499   if (!g_test_subprocess ())
500     return;
501
502   g_print ("Test /misc/dash-p/child ran\n");
503 }
504
505 static void
506 test_dash_p_child_sub (void)
507 {
508   if (!g_test_subprocess ())
509     return;
510
511   g_print ("Test /misc/dash-p/child/sub ran\n");
512 }
513
514 static void
515 test_dash_p_child_sub2 (void)
516 {
517   if (!g_test_subprocess ())
518     return;
519
520   g_print ("Test /misc/dash-p/child/sub2 ran\n");
521 }
522
523 static void
524 test_dash_p_child_sub_child (void)
525 {
526   if (!g_test_subprocess ())
527     return;
528
529   g_print ("Test /misc/dash-p/child/subprocess ran\n");
530 }
531
532 static void
533 test_dash_p (void)
534 {
535   g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0);
536   g_test_trap_assert_passed ();
537   g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
538   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
539   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
540   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
541   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
542
543   g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0);
544   g_test_trap_assert_passed ();
545   g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
546   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
547   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
548   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
549   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
550
551   g_test_trap_subprocess ("/misc/dash-p/child", 0, 0);
552   g_test_trap_assert_passed ();
553   g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
554   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
555   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
556   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
557   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
558
559   g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0);
560   g_test_trap_assert_passed ();
561   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
562   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
563   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
564   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
565   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
566 }
567
568 static void
569 test_nonfatal (void)
570 {
571   if (g_test_subprocess ())
572     {
573       g_test_set_nonfatal_assertions ();
574       g_assert_cmpint (4, ==, 5);
575       g_print ("The End\n");
576       return;
577     }
578   g_test_trap_subprocess (NULL, 0, 0);
579   g_test_trap_assert_failed ();
580   g_test_trap_assert_stderr ("*assertion failed*4 == 5*");
581   g_test_trap_assert_stdout ("*The End*");
582 }
583
584 static void
585 test_skip (void)
586 {
587   g_test_skip ("Skipped should count as passed, not failed");
588 }
589
590 static void
591 test_pass (void)
592 {
593 }
594
595 static void
596 test_fail (void)
597 {
598   if (g_test_subprocess ())
599     {
600       g_test_fail ();
601       g_assert (g_test_failed ());
602       return;
603     }
604   g_test_trap_subprocess (NULL, 0, 0);
605   g_test_trap_assert_failed ();
606 }
607
608 static void
609 test_incomplete (void)
610 {
611   if (g_test_subprocess ())
612     {
613       g_test_incomplete ("not done");
614       g_assert (g_test_failed ());
615       return;
616     }
617   g_test_trap_subprocess (NULL, 0, 0);
618   g_test_trap_assert_failed ();
619 }
620
621 static void
622 test_subprocess_timed_out (void)
623 {
624   if (g_test_subprocess ())
625     {
626       g_usleep (1000000);
627       return;
628     }
629   g_test_trap_subprocess (NULL, 50000, 0);
630   g_assert (g_test_trap_reached_timeout ());
631 }
632
633 static const char *argv0;
634
635 static void
636 test_skip_all (void)
637 {
638   GPtrArray *argv;
639   GError *error = NULL;
640   int status;
641
642   argv = g_ptr_array_new ();
643   g_ptr_array_add (argv, (char *) argv0);
644   g_ptr_array_add (argv, "--GTestSubprocess");
645   g_ptr_array_add (argv, "-p");
646   g_ptr_array_add (argv, "/misc/skip");
647   g_ptr_array_add (argv, NULL);
648
649   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
650                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
651                 NULL, NULL, NULL, NULL, &status,
652                 &error);
653   g_assert_no_error (error);
654
655   g_spawn_check_exit_status (status, &error);
656   g_assert_error (error, G_SPAWN_EXIT_ERROR, 77);
657   g_clear_error (&error);
658
659   g_ptr_array_set_size (argv, 0);
660   g_ptr_array_add (argv, (char *) argv0);
661   g_ptr_array_add (argv, "--GTestSubprocess");
662   g_ptr_array_add (argv, "-p");
663   g_ptr_array_add (argv, "/misc/skip");
664   g_ptr_array_add (argv, "-p");
665   g_ptr_array_add (argv, "/misc/skip-all/subprocess/skip1");
666   g_ptr_array_add (argv, "-p");
667   g_ptr_array_add (argv, "/misc/skip-all/subprocess/skip2");
668   g_ptr_array_add (argv, NULL);
669
670   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
671                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
672                 NULL, NULL, NULL, NULL, &status,
673                 &error);
674   g_assert_no_error (error);
675
676   g_spawn_check_exit_status (status, &error);
677   g_assert_error (error, G_SPAWN_EXIT_ERROR, 77);
678   g_clear_error (&error);
679
680   g_ptr_array_set_size (argv, 0);
681   g_ptr_array_add (argv, (char *) argv0);
682   g_ptr_array_add (argv, "--GTestSubprocess");
683   g_ptr_array_add (argv, "-p");
684   g_ptr_array_add (argv, "/misc/skip");
685   g_ptr_array_add (argv, "-p");
686   g_ptr_array_add (argv, "/misc/skip-all/subprocess/pass");
687   g_ptr_array_add (argv, "-p");
688   g_ptr_array_add (argv, "/misc/skip-all/subprocess/skip1");
689   g_ptr_array_add (argv, NULL);
690
691   g_spawn_sync (NULL, (char **) argv->pdata, NULL,
692                 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
693                 NULL, NULL, NULL, NULL, &status,
694                 &error);
695   g_assert_no_error (error);
696
697   g_spawn_check_exit_status (status, &error);
698   g_assert_no_error (error);
699
700   g_ptr_array_unref (argv);
701 }
702
703 int
704 main (int   argc,
705       char *argv[])
706 {
707   argv0 = argv[0];
708
709   g_test_init (&argc, &argv, NULL);
710
711   g_test_add_func ("/random-generator/rand-1", test_rand1);
712   g_test_add_func ("/random-generator/rand-2", test_rand2);
713   g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
714   g_test_add_func ("/misc/assertions", test_assertions);
715   g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
716   g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
717   g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
718   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
719   if (g_test_perf())
720     g_test_add_func ("/misc/timer", test_timer);
721
722 #ifdef G_OS_UNIX
723   g_test_add_func ("/forking/fail assertion", test_fork_fail);
724   g_test_add_func ("/forking/patterns", test_fork_patterns);
725   if (g_test_slow())
726     g_test_add_func ("/forking/timeout", test_fork_timeout);
727 #endif
728
729   g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
730   g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
731   if (g_test_slow ())
732     g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
733
734   g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
735
736   g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
737   g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass);
738   g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail);
739   g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail);
740
741   g_test_add_func ("/misc/expected-messages", test_expected_messages);
742   g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning);
743   g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
744   g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
745   g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
746   g_test_add_func ("/misc/expected-messages/subprocess/null-domain", test_expected_messages_null_domain);
747   g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
748   g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
749   g_test_add_func ("/misc/expected-messages/expect-error", test_expected_messages_expect_error);
750   g_test_add_func ("/misc/expected-messages/skip-debug", test_expected_messages_debug);
751
752   g_test_add_func ("/misc/dash-p", test_dash_p);
753   g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
754   g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
755   g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
756   g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
757   g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
758   g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
759   g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);
760
761   g_test_add_func ("/misc/nonfatal", test_nonfatal);
762
763   g_test_add_func ("/misc/skip", test_skip);
764   g_test_add_func ("/misc/skip-all", test_skip_all);
765   g_test_add_func ("/misc/skip-all/subprocess/skip1", test_skip);
766   g_test_add_func ("/misc/skip-all/subprocess/skip2", test_skip);
767   g_test_add_func ("/misc/skip-all/subprocess/pass", test_pass);
768   g_test_add_func ("/misc/fail", test_fail);
769   g_test_add_func ("/misc/incomplete", test_incomplete);
770   g_test_add_func ("/misc/timeout", test_subprocess_timed_out);
771
772   return g_test_run();
773 }