Simplify subprocesses in tests
[platform/upstream/glib.git] / glib / tests / logging.c
1 #include <stdlib.h>
2 #include <glib.h>
3
4 /* Test g_warn macros */
5 static void
6 test_warnings (void)
7 {
8   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
9                          "*test_warnings*should not be reached*");
10   g_warn_if_reached ();
11   g_test_assert_expected_messages ();
12
13   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
14                          "*test_warnings*runtime check failed*");
15   g_warn_if_fail (FALSE);
16   g_test_assert_expected_messages ();
17 }
18
19 static guint log_count = 0;
20
21 static void
22 log_handler (const gchar    *log_domain,
23              GLogLevelFlags  log_level,
24              const gchar    *message,
25              gpointer        user_data)
26 {
27   g_assert_cmpstr (log_domain, ==, "bu");
28   g_assert_cmpint (log_level, ==, G_LOG_LEVEL_INFO);
29
30   log_count++;
31 }
32
33 /* test that custom log handlers only get called for
34  * their domain and level
35  */
36 static void
37 test_set_handler (void)
38 {
39   guint id;
40
41   id = g_log_set_handler ("bu", G_LOG_LEVEL_INFO, log_handler, NULL);
42
43   g_log ("bu", G_LOG_LEVEL_DEBUG, "message");
44   g_log ("ba", G_LOG_LEVEL_DEBUG, "message");
45   g_log ("bu", G_LOG_LEVEL_INFO, "message");
46   g_log ("ba", G_LOG_LEVEL_INFO, "message");
47
48   g_assert_cmpint (log_count, ==, 1);
49
50   g_log_remove_handler ("bu", id);
51 }
52
53 static void
54 test_default_handler_error (void)
55 {
56   g_log_set_default_handler (g_log_default_handler, NULL);
57   g_error ("message1");
58   exit (0);
59 }
60
61 static void
62 test_default_handler_critical (void)
63 {
64   g_log_set_default_handler (g_log_default_handler, NULL);
65   g_critical ("message2");
66   exit (0);
67 }
68
69 static void
70 test_default_handler_warning (void)
71 {
72   g_log_set_default_handler (g_log_default_handler, NULL);
73   g_warning ("message3");
74   exit (0);
75 }
76
77 static void
78 test_default_handler_message (void)
79 {
80   g_log_set_default_handler (g_log_default_handler, NULL);
81   g_message ("message4");
82   exit (0);
83 }
84
85 static void
86 test_default_handler_info (void)
87 {
88   g_log_set_default_handler (g_log_default_handler, NULL);
89   g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "message5");
90   exit (0);
91 }
92
93 static void
94 test_default_handler_bar_info (void)
95 {
96   g_log_set_default_handler (g_log_default_handler, NULL);
97
98   g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);
99
100   g_log ("bar", G_LOG_LEVEL_INFO, "message5");
101   exit (0);
102 }
103
104 static void
105 test_default_handler_baz_debug (void)
106 {
107   g_log_set_default_handler (g_log_default_handler, NULL);
108
109   g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);
110
111   g_log ("baz", G_LOG_LEVEL_DEBUG, "message6");
112   exit (0);
113 }
114
115 static void
116 test_default_handler_debug (void)
117 {
118   g_log_set_default_handler (g_log_default_handler, NULL);
119
120   g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
121
122   g_log ("foo", G_LOG_LEVEL_DEBUG, "6");
123   g_log ("bar", G_LOG_LEVEL_DEBUG, "6");
124   g_log ("baz", G_LOG_LEVEL_DEBUG, "6");
125   exit (0);
126 }
127
128 static void
129 test_default_handler_0x400 (void)
130 {
131   g_log_set_default_handler (g_log_default_handler, NULL);
132   g_log (G_LOG_DOMAIN, 1<<10, "message7");
133   exit (0);
134 }
135
136 static void
137 test_default_handler (void)
138 {
139   g_test_trap_subprocess ("/logging/default-handler/subprocess/error", 0, 0);
140   g_test_trap_assert_failed ();
141   g_test_trap_assert_stderr ("*ERROR*message1*");
142
143   g_test_trap_subprocess ("/logging/default-handler/subprocess/critical", 0, 0);
144   g_test_trap_assert_failed ();
145   g_test_trap_assert_stderr ("*CRITICAL*message2*");
146
147   g_test_trap_subprocess ("/logging/default-handler/subprocess/warning", 0, 0);
148   g_test_trap_assert_failed ();
149   g_test_trap_assert_stderr ("*WARNING*message3*");
150
151   g_test_trap_subprocess ("/logging/default-handler/subprocess/message", 0, 0);
152   g_test_trap_assert_passed ();
153   g_test_trap_assert_stderr ("*Message*message4*");
154
155   g_test_trap_subprocess ("/logging/default-handler/subprocess/info", 0, 0);
156   g_test_trap_assert_passed ();
157   g_test_trap_assert_stdout_unmatched ("*INFO*message5*");
158
159   g_test_trap_subprocess ("/logging/default-handler/subprocess/bar-info", 0, 0);
160   g_test_trap_assert_passed ();
161   g_test_trap_assert_stdout ("*INFO*message5*");
162
163   g_test_trap_subprocess ("/logging/default-handler/subprocess/baz-debug", 0, 0);
164   g_test_trap_assert_passed ();
165   g_test_trap_assert_stdout ("*DEBUG*message6*");
166
167   g_test_trap_subprocess ("/logging/default-handler/subprocess/debug", 0, 0);
168   g_test_trap_assert_passed ();
169   g_test_trap_assert_stdout ("*DEBUG*6*6*6*");
170
171   g_test_trap_subprocess ("/logging/default-handler/subprocess/0x400", 0, 0);
172   g_test_trap_assert_passed ();
173   g_test_trap_assert_stdout ("*LOG-0x400*message7*");
174 }
175
176 /* test that setting levels as fatal works */
177 static void
178 test_fatal_log_mask_subprocess (void)
179 {
180   g_log_set_fatal_mask ("bu", G_LOG_LEVEL_INFO);
181   g_log ("bu", G_LOG_LEVEL_INFO, "fatal");
182   exit (0);
183 }
184
185 static void
186 test_fatal_log_mask (void)
187 {
188   g_test_trap_subprocess ("/logging/fatal-log-mask/subprocess", 0, 0);
189   g_test_trap_assert_failed ();
190   /* G_LOG_LEVEL_INFO isn't printed by default */
191   g_test_trap_assert_stdout_unmatched ("*fatal*");
192 }
193
194 static gint my_print_count = 0;
195 static void
196 my_print_handler (const gchar *text)
197 {
198   my_print_count++;
199 }
200
201 static void
202 test_print_handler (void)
203 {
204   GPrintFunc old_print_handler;
205
206   old_print_handler = g_set_print_handler (my_print_handler);
207   g_assert (old_print_handler == NULL);
208
209   my_print_count = 0;
210   g_print ("bu ba");
211   g_assert_cmpint (my_print_count, ==, 1);
212
213   g_set_print_handler (NULL);
214 }
215
216 static void
217 test_printerr_handler (void)
218 {
219   GPrintFunc old_printerr_handler;
220
221   old_printerr_handler = g_set_printerr_handler (my_print_handler);
222   g_assert (old_printerr_handler == NULL);
223
224   my_print_count = 0;
225   g_printerr ("bu ba");
226   g_assert_cmpint (my_print_count, ==, 1);
227
228   g_set_printerr_handler (NULL);
229 }
230
231 static char *fail_str = "foo";
232 static char *log_str = "bar";
233
234 static gboolean
235 good_failure_handler (const gchar    *log_domain,
236                       GLogLevelFlags  log_level,
237                       const gchar    *msg,
238                       gpointer        user_data)
239 {
240   g_test_message ("The Good Fail Message Handler\n");
241   g_assert ((char *)user_data != log_str);
242   g_assert ((char *)user_data == fail_str);
243
244   return FALSE;
245 }
246
247 static gboolean
248 bad_failure_handler (const gchar    *log_domain,
249                      GLogLevelFlags  log_level,
250                      const gchar    *msg,
251                      gpointer        user_data)
252 {
253   g_test_message ("The Bad Fail Message Handler\n");
254   g_assert ((char *)user_data == log_str);
255   g_assert ((char *)user_data != fail_str);
256
257   return FALSE;
258 }
259
260 static void
261 test_handler (const gchar    *log_domain,
262               GLogLevelFlags  log_level,
263               const gchar    *msg,
264               gpointer        user_data)
265 {
266   g_test_message ("The Log Message Handler\n");
267   g_assert ((char *)user_data != fail_str);
268   g_assert ((char *)user_data == log_str);
269 }
270
271 static void
272 bug653052 (void)
273 {
274   g_test_bug ("653052");
275
276   g_test_log_set_fatal_handler (good_failure_handler, fail_str);
277   g_log_set_default_handler (test_handler, log_str);
278
279   g_return_if_fail (0);
280
281   g_test_log_set_fatal_handler (bad_failure_handler, fail_str);
282   g_log_set_default_handler (test_handler, log_str);
283
284   g_return_if_fail (0);
285 }
286
287 int
288 main (int argc, char *argv[])
289 {
290   g_unsetenv ("G_MESSAGES_DEBUG");
291
292   g_test_init (&argc, &argv, NULL);
293   g_test_bug_base ("http://bugzilla.gnome.org/");
294
295   g_test_add_func ("/logging/default-handler", test_default_handler);
296   g_test_add_func ("/logging/default-handler/subprocess/error", test_default_handler_error);
297   g_test_add_func ("/logging/default-handler/subprocess/critical", test_default_handler_critical);
298   g_test_add_func ("/logging/default-handler/subprocess/warning", test_default_handler_warning);
299   g_test_add_func ("/logging/default-handler/subprocess/message", test_default_handler_message);
300   g_test_add_func ("/logging/default-handler/subprocess/info", test_default_handler_info);
301   g_test_add_func ("/logging/default-handler/subprocess/bar-info", test_default_handler_bar_info);
302   g_test_add_func ("/logging/default-handler/subprocess/baz-debug", test_default_handler_baz_debug);
303   g_test_add_func ("/logging/default-handler/subprocess/debug", test_default_handler_debug);
304   g_test_add_func ("/logging/default-handler/subprocess/0x400", test_default_handler_0x400);
305   g_test_add_func ("/logging/warnings", test_warnings);
306   g_test_add_func ("/logging/fatal-log-mask", test_fatal_log_mask);
307   g_test_add_func ("/logging/fatal-log-mask/subprocess", test_fatal_log_mask_subprocess);
308   g_test_add_func ("/logging/set-handler", test_set_handler);
309   g_test_add_func ("/logging/print-handler", test_print_handler);
310   g_test_add_func ("/logging/printerr-handler", test_printerr_handler);
311   g_test_add_func ("/logging/653052", bug653052);
312
313   return g_test_run ();
314 }
315