nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | static void |
||
177 | test_fatal_log_mask (void) |
||
178 | { |
||
179 | if (g_test_subprocess ()) |
||
180 | { |
||
181 | g_log_set_fatal_mask ("bu", G_LOG_LEVEL_INFO); |
||
182 | g_log ("bu", G_LOG_LEVEL_INFO, "fatal"); |
||
183 | return; |
||
184 | } |
||
185 | g_test_trap_subprocess (NULL, 0, 0); |
||
186 | g_test_trap_assert_failed (); |
||
187 | /* G_LOG_LEVEL_INFO isn't printed by default */ |
||
188 | g_test_trap_assert_stdout_unmatched ("*fatal*"); |
||
189 | } |
||
190 | |||
191 | static gint my_print_count = 0; |
||
192 | static void |
||
193 | my_print_handler (const gchar *text) |
||
194 | { |
||
195 | my_print_count++; |
||
196 | } |
||
197 | |||
198 | static void |
||
199 | test_print_handler (void) |
||
200 | { |
||
201 | GPrintFunc old_print_handler; |
||
202 | |||
203 | old_print_handler = g_set_print_handler (my_print_handler); |
||
204 | g_assert (old_print_handler == NULL); |
||
205 | |||
206 | my_print_count = 0; |
||
207 | g_print ("bu ba"); |
||
208 | g_assert_cmpint (my_print_count, ==, 1); |
||
209 | |||
210 | g_set_print_handler (NULL); |
||
211 | } |
||
212 | |||
213 | static void |
||
214 | test_printerr_handler (void) |
||
215 | { |
||
216 | GPrintFunc old_printerr_handler; |
||
217 | |||
218 | old_printerr_handler = g_set_printerr_handler (my_print_handler); |
||
219 | g_assert (old_printerr_handler == NULL); |
||
220 | |||
221 | my_print_count = 0; |
||
222 | g_printerr ("bu ba"); |
||
223 | g_assert_cmpint (my_print_count, ==, 1); |
||
224 | |||
225 | g_set_printerr_handler (NULL); |
||
226 | } |
||
227 | |||
228 | static char *fail_str = "foo"; |
||
229 | static char *log_str = "bar"; |
||
230 | |||
231 | static gboolean |
||
232 | good_failure_handler (const gchar *log_domain, |
||
233 | GLogLevelFlags log_level, |
||
234 | const gchar *msg, |
||
235 | gpointer user_data) |
||
236 | { |
||
237 | g_test_message ("The Good Fail Message Handler\n"); |
||
238 | g_assert ((char *)user_data != log_str); |
||
239 | g_assert ((char *)user_data == fail_str); |
||
240 | |||
241 | return FALSE; |
||
242 | } |
||
243 | |||
244 | static gboolean |
||
245 | bad_failure_handler (const gchar *log_domain, |
||
246 | GLogLevelFlags log_level, |
||
247 | const gchar *msg, |
||
248 | gpointer user_data) |
||
249 | { |
||
250 | g_test_message ("The Bad Fail Message Handler\n"); |
||
251 | g_assert ((char *)user_data == log_str); |
||
252 | g_assert ((char *)user_data != fail_str); |
||
253 | |||
254 | return FALSE; |
||
255 | } |
||
256 | |||
257 | static void |
||
258 | test_handler (const gchar *log_domain, |
||
259 | GLogLevelFlags log_level, |
||
260 | const gchar *msg, |
||
261 | gpointer user_data) |
||
262 | { |
||
263 | g_test_message ("The Log Message Handler\n"); |
||
264 | g_assert ((char *)user_data != fail_str); |
||
265 | g_assert ((char *)user_data == log_str); |
||
266 | } |
||
267 | |||
268 | static void |
||
269 | bug653052 (void) |
||
270 | { |
||
271 | g_test_bug ("653052"); |
||
272 | |||
273 | g_test_log_set_fatal_handler (good_failure_handler, fail_str); |
||
274 | g_log_set_default_handler (test_handler, log_str); |
||
275 | |||
276 | g_return_if_fail (0); |
||
277 | |||
278 | g_test_log_set_fatal_handler (bad_failure_handler, fail_str); |
||
279 | g_log_set_default_handler (test_handler, log_str); |
||
280 | |||
281 | g_return_if_fail (0); |
||
282 | } |
||
283 | |||
284 | static void |
||
285 | test_gibberish (void) |
||
286 | { |
||
287 | if (g_test_subprocess ()) |
||
288 | { |
||
289 | g_warning ("bla bla \236\237\190"); |
||
290 | return; |
||
291 | } |
||
292 | g_test_trap_subprocess (NULL, 0, 0); |
||
293 | g_test_trap_assert_failed (); |
||
294 | g_test_trap_assert_stderr ("*bla bla \\x9e\\x9f\\u000190*"); |
||
295 | } |
||
296 | |||
297 | int |
||
298 | main (int argc, char *argv[]) |
||
299 | { |
||
300 | g_unsetenv ("G_MESSAGES_DEBUG"); |
||
301 | |||
302 | g_test_init (&argc, &argv, NULL); |
||
303 | g_test_bug_base ("http://bugzilla.gnome.org/"); |
||
304 | |||
305 | g_test_add_func ("/logging/default-handler", test_default_handler); |
||
306 | g_test_add_func ("/logging/default-handler/subprocess/error", test_default_handler_error); |
||
307 | g_test_add_func ("/logging/default-handler/subprocess/critical", test_default_handler_critical); |
||
308 | g_test_add_func ("/logging/default-handler/subprocess/warning", test_default_handler_warning); |
||
309 | g_test_add_func ("/logging/default-handler/subprocess/message", test_default_handler_message); |
||
310 | g_test_add_func ("/logging/default-handler/subprocess/info", test_default_handler_info); |
||
311 | g_test_add_func ("/logging/default-handler/subprocess/bar-info", test_default_handler_bar_info); |
||
312 | g_test_add_func ("/logging/default-handler/subprocess/baz-debug", test_default_handler_baz_debug); |
||
313 | g_test_add_func ("/logging/default-handler/subprocess/debug", test_default_handler_debug); |
||
314 | g_test_add_func ("/logging/default-handler/subprocess/0x400", test_default_handler_0x400); |
||
315 | g_test_add_func ("/logging/warnings", test_warnings); |
||
316 | g_test_add_func ("/logging/fatal-log-mask", test_fatal_log_mask); |
||
317 | g_test_add_func ("/logging/set-handler", test_set_handler); |
||
318 | g_test_add_func ("/logging/print-handler", test_print_handler); |
||
319 | g_test_add_func ("/logging/printerr-handler", test_printerr_handler); |
||
320 | g_test_add_func ("/logging/653052", bug653052); |
||
321 | g_test_add_func ("/logging/gibberish", test_gibberish); |
||
322 | |||
323 | return g_test_run (); |
||
324 | } |
||
325 |