gtestutils: add g_test_trap_subprocess()
[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 #include <glib.h>
24
25 #include <stdlib.h>
26
27 /* test assertion variants */
28 static void
29 test_assertions (void)
30 {
31   gchar *fuu;
32   g_assert_cmpint (1, >, 0);
33   g_assert_cmphex (2, ==, 2);
34   g_assert_cmpfloat (3.3, !=, 7);
35   g_assert_cmpfloat (7, <=, 3 + 4);
36   g_assert (TRUE);
37   g_assert_cmpstr ("foo", !=, "faa");
38   fuu = g_strdup_printf ("f%s", "uu");
39   g_test_queue_free (fuu);
40   g_assert_cmpstr ("foo", !=, fuu);
41   g_assert_cmpstr ("fuu", ==, fuu);
42   g_assert_cmpstr (NULL, <, "");
43   g_assert_cmpstr (NULL, ==, NULL);
44   g_assert_cmpstr ("", >, NULL);
45   g_assert_cmpstr ("foo", <, "fzz");
46   g_assert_cmpstr ("fzz", >, "faa");
47   g_assert_cmpstr ("fzz", ==, "fzz");
48
49   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
50     {
51       g_assert_cmpstr ("fzz", !=, "fzz");
52     }
53   g_test_trap_assert_failed ();
54   g_test_trap_assert_stderr ("*assertion failed*");
55
56   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
57     {
58       g_assert_cmpint (4, !=, 4);
59     }
60   g_test_trap_assert_failed ();
61   g_test_trap_assert_stderr ("*assertion failed*");
62 }
63
64 /* test g_test_timer* API */
65 static void
66 test_timer (void)
67 {
68   double ttime;
69   g_test_timer_start();
70   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
71   g_usleep (25 * 1000);
72   ttime = g_test_timer_elapsed();
73   g_assert_cmpfloat (ttime, >, 0);
74   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
75   g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
76   g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
77 }
78
79 /* fork out for a failing test */
80 static void
81 test_fork_fail (void)
82 {
83   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
84     {
85       g_assert_not_reached();
86     }
87   g_test_trap_assert_failed();
88   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
89 }
90
91 /* fork out to assert stdout and stderr patterns */
92 static void
93 test_fork_patterns (void)
94 {
95   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
96     {
97       g_print ("some stdout text: somagic17\n");
98       g_printerr ("some stderr text: semagic43\n");
99       exit (0);
100     }
101   g_test_trap_assert_passed();
102   g_test_trap_assert_stdout ("*somagic17*");
103   g_test_trap_assert_stderr ("*semagic43*");
104 }
105
106 /* fork out for a timeout test */
107 static void
108 test_fork_timeout (void)
109 {
110   /* allow child to run for only a fraction of a second */
111   if (g_test_trap_fork (0.11 * 1000000, 0))
112     {
113       /* loop and sleep forever */
114       while (TRUE)
115         g_usleep (1000 * 1000);
116     }
117   g_test_trap_assert_failed();
118   g_assert (g_test_trap_reached_timeout());
119 }
120
121 static void
122 test_subprocess_fail_child (void)
123 {
124   g_assert_not_reached ();
125 }
126
127 static void
128 test_subprocess_fail (void)
129 {
130   g_test_trap_subprocess ("/trap_subprocess/fail/subprocess", 0, 0);
131   g_test_trap_assert_failed ();
132   g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail_child*should not be reached*");
133 }
134
135 static void
136 test_subprocess_no_such_test_child (void)
137 {
138   g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0, 0);
139   g_assert_not_reached ();
140 }
141
142 static void
143 test_subprocess_no_such_test (void)
144 {
145   g_test_trap_subprocess ("/trap_subprocess/no-such-test/subprocess", 0, 0);
146   g_test_trap_assert_failed ();
147   g_test_trap_assert_stderr ("*test does not exist*");
148   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
149 }
150
151 static void
152 test_subprocess_patterns_child (void)
153 {
154   g_print ("some stdout text: somagic17\n");
155   g_printerr ("some stderr text: semagic43\n");
156   exit (0);
157 }
158
159 static void
160 test_subprocess_patterns (void)
161 {
162   g_test_trap_subprocess ("/trap_subprocess/patterns/subprocess", 0,  0);
163   g_test_trap_assert_passed ();
164   g_test_trap_assert_stdout ("*somagic17*");
165   g_test_trap_assert_stderr ("*semagic43*");
166 }
167
168 static void
169 test_subprocess_timeout_child (void)
170 {
171   /* loop and sleep forever */
172   while (TRUE)
173     g_usleep (1000 * 1000);
174 }
175
176 static void
177 test_subprocess_timeout (void)
178 {
179   /* allow child to run for only a fraction of a second */
180   g_test_trap_subprocess ("/trap_subprocess/timeout/subprocess", 0.11 * 1000000, 0);
181   g_test_trap_assert_failed ();
182   g_assert (g_test_trap_reached_timeout ());
183 }
184
185 /* run a test with fixture setup and teardown */
186 typedef struct {
187   guint  seed;
188   guint  prime;
189   gchar *msg;
190 } Fixturetest;
191 static void
192 fixturetest_setup (Fixturetest  *fix,
193                    gconstpointer test_data)
194 {
195   g_assert (test_data == (void*) 0xc0cac01a);
196   fix->seed = 18;
197   fix->prime = 19;
198   fix->msg = g_strdup_printf ("%d", fix->prime);
199 }
200 static void
201 fixturetest_test (Fixturetest  *fix,
202                   gconstpointer test_data)
203 {
204   guint prime = g_spaced_primes_closest (fix->seed);
205   g_assert_cmpint (prime, ==, fix->prime);
206   prime = g_ascii_strtoull (fix->msg, NULL, 0);
207   g_assert_cmpint (prime, ==, fix->prime);
208   g_assert (test_data == (void*) 0xc0cac01a);
209 }
210 static void
211 fixturetest_teardown (Fixturetest  *fix,
212                       gconstpointer test_data)
213 {
214   g_assert (test_data == (void*) 0xc0cac01a);
215   g_free (fix->msg);
216 }
217
218 static struct {
219   int bit, vint1, vint2, irange;
220   long double vdouble, drange;
221 } shared_rand_state;
222
223 static void
224 test_rand1 (void)
225 {
226   shared_rand_state.bit = g_test_rand_bit();
227   shared_rand_state.vint1 = g_test_rand_int();
228   shared_rand_state.vint2 = g_test_rand_int();
229   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
230   shared_rand_state.irange = g_test_rand_int_range (17, 35);
231   g_assert_cmpint (shared_rand_state.irange, >=, 17);
232   g_assert_cmpint (shared_rand_state.irange, <=, 35);
233   shared_rand_state.vdouble = g_test_rand_double();
234   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
235   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
236   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
237 }
238
239 static void
240 test_rand2 (void)
241 {
242   /* this test only works if run after test1.
243    * we do this to check that random number generators
244    * are reseeded upon fixture setup.
245    */
246   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
247   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
248   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
249   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
250   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
251   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
252 }
253
254 static void
255 test_data_test (gconstpointer test_data)
256 {
257   g_assert (test_data == (void*) 0xc0c0baba);
258 }
259
260 static void
261 test_random_conversions (void)
262 {
263   /* very simple conversion test using random numbers */
264   int vint = g_test_rand_int();
265   char *err, *str = g_strdup_printf ("%d", vint);
266   gint64 vint64 = g_ascii_strtoll (str, &err, 10);
267   g_assert_cmphex (vint, ==, vint64);
268   g_assert (!err || *err == 0);
269   g_free (str);
270 }
271
272 static gboolean
273 fatal_handler (const gchar    *log_domain,
274                GLogLevelFlags  log_level,
275                const gchar    *message,
276                gpointer        user_data)
277 {
278   return FALSE;
279 }
280
281 static void
282 test_fatal_log_handler (void)
283 {
284   g_test_log_set_fatal_handler (fatal_handler, NULL);
285   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
286     {
287       g_str_has_prefix (NULL, "file://");
288       g_critical ("Test passing");
289       exit (0);
290     }
291   g_test_trap_assert_passed ();
292
293   g_test_log_set_fatal_handler (NULL, NULL);
294   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
295     g_error ("Test failing");
296   g_test_trap_assert_failed ();
297
298   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
299     g_str_has_prefix (NULL, "file://");
300   g_test_trap_assert_failed ();
301 }
302
303 static void
304 expected_messages_helper (void)
305 {
306   g_warning ("This is a %d warning", g_random_int ());
307   g_return_if_reached ();
308 }
309
310 static void
311 test_expected_messages (void)
312 {
313   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
314     {
315       expected_messages_helper ();
316       exit (0);
317     }
318   g_test_trap_assert_failed ();
319   g_test_trap_assert_stderr ("*This is a * warning*");
320   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
321
322   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
323     {
324       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
325                              "This is a * warning");
326       expected_messages_helper ();
327       exit (0);
328     }
329   g_test_trap_assert_failed ();
330   g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
331   g_test_trap_assert_stderr ("*should not be reached*");
332
333   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
334     {
335       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
336                              "*should not be *");
337       expected_messages_helper ();
338       exit (0);
339     }
340   g_test_trap_assert_failed ();
341   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
342   g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
343
344   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
345     {
346       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
347                              "This is a * warning");
348       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
349                              "*should not be reached");
350       expected_messages_helper ();
351       g_test_assert_expected_messages ();
352       exit (0);
353     }
354   g_test_trap_assert_passed ();
355   g_test_trap_assert_stderr ("");
356
357   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
358     {
359       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
360                              "This is a * warning");
361       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
362                              "*should not be reached");
363       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
364                              "nope");
365       expected_messages_helper ();
366       /* If we don't assert, it won't notice the missing message */
367       exit (0);
368     }
369   g_test_trap_assert_passed ();
370   g_test_trap_assert_stderr ("");
371
372   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
373     {
374       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
375                              "This is a * warning");
376       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
377                              "*should not be reached");
378       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
379                              "nope");
380       expected_messages_helper ();
381       g_test_assert_expected_messages ();
382       exit (0);
383     }
384   g_test_trap_assert_failed ();
385   g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
386 }
387
388 static void
389 test_dash_p_hidden (void)
390 {
391   if (!g_test_subprocess ())
392     g_assert_not_reached ();
393
394   g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
395 }
396
397 static void
398 test_dash_p_hidden_sub (void)
399 {
400   if (!g_test_subprocess ())
401     g_assert_not_reached ();
402
403   g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
404 }
405
406 /* The rest of the dash_p tests will get run by the toplevel test
407  * process, but they shouldn't do anything there.
408  */
409
410 static void
411 test_dash_p_child (void)
412 {
413   if (!g_test_subprocess ())
414     return;
415
416   g_print ("Test /misc/dash-p/child ran\n");
417 }
418
419 static void
420 test_dash_p_child_sub (void)
421 {
422   if (!g_test_subprocess ())
423     return;
424
425   g_print ("Test /misc/dash-p/child/sub ran\n");
426 }
427
428 static void
429 test_dash_p_child_sub2 (void)
430 {
431   if (!g_test_subprocess ())
432     return;
433
434   g_print ("Test /misc/dash-p/child/sub2 ran\n");
435 }
436
437 static void
438 test_dash_p_child_sub_child (void)
439 {
440   if (!g_test_subprocess ())
441     return;
442
443   g_print ("Test /misc/dash-p/child/subprocess ran\n");
444 }
445
446 static void
447 test_dash_p (void)
448 {
449   g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0);
450   g_test_trap_assert_passed ();
451   g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
452   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
453   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
454   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
455   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
456
457   g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0);
458   g_test_trap_assert_passed ();
459   g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
460   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
461   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
462   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
463   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
464
465   g_test_trap_subprocess ("/misc/dash-p/child", 0, 0);
466   g_test_trap_assert_passed ();
467   g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
468   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
469   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
470   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
471   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
472
473   g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0);
474   g_test_trap_assert_passed ();
475   g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
476   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
477   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
478   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
479   g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
480 }
481
482 int
483 main (int   argc,
484       char *argv[])
485 {
486   g_test_init (&argc, &argv, NULL);
487
488   g_test_add_func ("/random-generator/rand-1", test_rand1);
489   g_test_add_func ("/random-generator/rand-2", test_rand2);
490   g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
491   g_test_add_func ("/misc/assertions", test_assertions);
492   g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
493   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
494   if (g_test_perf())
495     g_test_add_func ("/misc/timer", test_timer);
496
497 #ifdef G_OS_UNIX
498   g_test_add_func ("/forking/fail assertion", test_fork_fail);
499   g_test_add_func ("/forking/patterns", test_fork_patterns);
500   if (g_test_slow())
501     g_test_add_func ("/forking/timeout", test_fork_timeout);
502 #endif
503
504   g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
505   g_test_add_func ("/trap_subprocess/fail/subprocess", test_subprocess_fail_child);
506   g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
507   g_test_add_func ("/trap_subprocess/no-such-test/subprocess", test_subprocess_no_such_test_child);
508   if (g_test_slow ())
509     {
510       g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
511       g_test_add_func ("/trap_subprocess/timeout/subprocess", test_subprocess_timeout_child);
512     }
513   g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
514   g_test_add_func ("/trap_subprocess/patterns/subprocess", test_subprocess_patterns_child);
515
516   g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
517   g_test_add_func ("/misc/expected-messages", test_expected_messages);
518
519   g_test_add_func ("/misc/dash-p", test_dash_p);
520   g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
521   g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
522   g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
523   g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
524   g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
525   g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
526   g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);
527
528   return g_test_run();
529 }