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