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