Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / tests / protocol.c
blob18ebe61d1757f1a273c137f68fe075087736909a
1 /* This file is part of GLib
3 * Copyright (C) 2010 Sven Herzberg
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.
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.
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.
23 #include <errno.h> /* errno */
24 #include <glib.h>
25 #ifdef G_OS_UNIX
26 #include <unistd.h> /* pipe() */
27 #endif
28 #ifdef G_OS_WIN32
29 #include <io.h>
30 #include <fcntl.h>
31 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
32 #endif
34 static const char *argv0;
36 static void
37 debug (void)
39 if (g_test_subprocess ())
40 g_debug ("this is a regular g_debug() from the test suite");
43 static void
44 info (void)
46 if (g_test_subprocess ())
47 g_info ("this is a regular g_info from the test suite");
50 static void
51 message (void)
53 if (g_test_subprocess ())
54 g_message ("this is a regular g_message() from the test suite");
57 static void
58 warning (void)
60 if (g_test_subprocess ())
61 g_warning ("this is a regular g_warning() from the test suite");
64 static void
65 critical (void)
67 if (g_test_subprocess ())
68 g_critical ("this is a regular g_critical() from the test suite");
71 static void
72 error (void)
74 if (g_test_subprocess ())
75 g_error ("this is a regular g_error() from the test suite");
78 static void
79 gtest_message (void)
81 if (g_test_subprocess ())
82 g_test_message ("this is a regular g_test_message() from the test suite");
85 static gboolean
86 test_message_cb1 (GIOChannel * channel,
87 GIOCondition condition,
88 gpointer user_data)
90 GIOStatus status;
91 guchar buf[512];
92 gsize read_bytes = 0;
94 g_assert_cmpuint (condition, ==, G_IO_IN);
96 for (status = g_io_channel_read_chars (channel, (gchar*)buf, sizeof (buf), &read_bytes, NULL);
97 status == G_IO_STATUS_NORMAL;
98 status = g_io_channel_read_chars (channel, (gchar*)buf, sizeof (buf), &read_bytes, NULL))
100 g_test_log_buffer_push (user_data, read_bytes, buf);
103 g_assert_cmpuint (status, ==, G_IO_STATUS_AGAIN);
105 return TRUE;
108 static void
109 test_message_cb2 (GPid pid,
110 gint status,
111 gpointer user_data)
113 g_spawn_close_pid (pid);
115 g_main_loop_quit (user_data);
118 static void
119 test_message (void)
121 gchar* argv[] = {
122 (gchar*)argv0,
123 NULL,
124 "--GTestSubprocess",
125 "-p", "/glib/testing/protocol/debug",
126 "-p", "/glib/testing/protocol/message",
127 "-p", "/glib/testing/protocol/gtest-message",
128 NULL
130 GTestLogBuffer* tlb;
131 GTestLogMsg * msg;
132 GIOChannel * channel;
133 GMainLoop * loop;
134 GError * error = NULL;
135 gulong child_source;
136 gulong io_source;
137 GPid pid = 0;
138 int pipes[2];
139 int passed = 0;
140 int messages = 0;
141 const char * line_term;
142 int line_term_len;
144 if (0 > pipe (pipes))
146 int errsv = errno;
147 g_error ("error creating pipe: %s", g_strerror (errsv));
150 argv[1] = g_strdup_printf ("--GTestLogFD=%u", pipes[1]);
152 if (!g_spawn_async (NULL,
153 argv, NULL,
154 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
155 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
156 NULL, NULL, &pid,
157 &error))
159 g_error ("error spawning the test: %s", error->message);
162 tlb = g_test_log_buffer_new ();
163 loop = g_main_loop_new (NULL, FALSE);
165 channel = g_io_channel_unix_new (pipes[0]);
166 g_io_channel_set_close_on_unref (channel, TRUE);
167 g_io_channel_set_encoding (channel, NULL, NULL);
168 g_io_channel_set_buffered (channel, FALSE);
169 g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
170 g_assert (g_io_channel_get_line_term (channel, NULL) == NULL);
171 g_io_channel_set_line_term (channel, "\n", 1);
172 line_term = g_io_channel_get_line_term (channel, &line_term_len);
173 g_assert_cmpint (*line_term, ==, '\n');
174 g_assert_cmpint (line_term_len, ==, 1);
176 g_assert (g_io_channel_get_close_on_unref (channel));
177 g_assert (g_io_channel_get_encoding (channel) == NULL);
178 g_assert (!g_io_channel_get_buffered (channel));
180 io_source = g_io_add_watch (channel, G_IO_IN, test_message_cb1, tlb);
181 child_source = g_child_watch_add (pid, test_message_cb2, loop);
183 g_main_loop_run (loop);
185 test_message_cb1 (channel, G_IO_IN, tlb);
187 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "Source ID*");
188 g_assert (!g_source_remove (child_source));
189 g_test_assert_expected_messages ();
190 g_assert (g_source_remove (io_source));
191 g_io_channel_unref (channel);
193 for (msg = g_test_log_buffer_pop (tlb);
194 msg;
195 msg = g_test_log_buffer_pop (tlb))
197 switch (msg->log_type)
199 case G_TEST_LOG_START_BINARY:
200 case G_TEST_LOG_START_CASE:
201 case G_TEST_LOG_START_SUITE:
202 case G_TEST_LOG_STOP_SUITE:
203 /* ignore */
204 break;
205 case G_TEST_LOG_STOP_CASE:
206 passed++;
207 break;
208 case G_TEST_LOG_MESSAGE:
210 gchar const* known_messages[] = {
211 "this is a regular g_test_message() from the test suite",
212 "GLib-MESSAGE: this is a regular g_message() from the test suite",
213 "GLib-DEBUG: this is a regular g_debug() from the test suite"
215 g_assert_cmpint (messages, <, G_N_ELEMENTS (known_messages));
216 g_assert_cmpstr (msg->strings[0], ==, known_messages[messages]);
217 messages++;
219 break;
220 case G_TEST_LOG_ERROR:
221 g_assert_not_reached ();
222 break;
223 default:
224 g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type));
226 g_test_log_msg_free (msg);
229 g_assert_cmpint (passed, ==, 3);
230 g_assert_cmpint (messages, ==, 3);
232 g_free (argv[1]);
233 g_main_loop_unref (loop);
234 g_test_log_buffer_free (tlb);
237 static void
238 test_error (void)
240 gchar* tests[] = {
241 "/glib/testing/protocol/warning",
242 "/glib/testing/protocol/critical",
243 "/glib/testing/protocol/error"
245 gint i;
246 int messages = 0;
248 for (i = 0; i < G_N_ELEMENTS (tests); i++)
250 gchar* argv[] = {
251 (gchar*)argv0,
252 NULL,
253 "--GTestSubprocess",
254 "-p", tests[i],
255 NULL
257 GTestLogBuffer* tlb;
258 GTestLogMsg * msg;
259 GIOChannel * channel;
260 GMainLoop * loop;
261 GError * error = NULL;
262 gulong child_source;
263 gulong io_source;
264 GPid pid = 0;
265 int pipes[2];
267 if (0 > pipe (pipes))
269 int errsv = errno;
270 g_error ("error creating pipe: %s", g_strerror (errsv));
273 argv[1] = g_strdup_printf ("--GTestLogFD=%u", pipes[1]);
275 if (!g_spawn_async (NULL,
276 argv, NULL,
277 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
278 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
279 NULL, NULL, &pid,
280 &error))
282 g_error ("error spawning the test: %s", error->message);
285 tlb = g_test_log_buffer_new ();
286 loop = g_main_loop_new (NULL, FALSE);
288 channel = g_io_channel_unix_new (pipes[0]);
289 g_io_channel_set_close_on_unref (channel, TRUE);
290 g_io_channel_set_encoding (channel, NULL, NULL);
291 g_io_channel_set_buffered (channel, FALSE);
292 g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
294 io_source = g_io_add_watch (channel, G_IO_IN, test_message_cb1, tlb);
295 child_source = g_child_watch_add (pid, test_message_cb2, loop);
297 g_main_loop_run (loop);
299 test_message_cb1 (channel, G_IO_IN, tlb);
301 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "Source ID*");
302 g_assert (!g_source_remove (child_source));
303 g_test_assert_expected_messages ();
304 g_assert (g_source_remove (io_source));
305 g_io_channel_unref (channel);
307 for (msg = g_test_log_buffer_pop (tlb);
308 msg;
309 msg = g_test_log_buffer_pop (tlb))
311 switch (msg->log_type)
313 case G_TEST_LOG_START_BINARY:
314 case G_TEST_LOG_START_CASE:
315 case G_TEST_LOG_START_SUITE:
316 case G_TEST_LOG_STOP_SUITE:
317 /* ignore */
318 break;
319 case G_TEST_LOG_STOP_CASE:
320 case G_TEST_LOG_MESSAGE:
321 g_assert_not_reached ();
322 break;
323 case G_TEST_LOG_ERROR:
325 gchar const* known_messages[] = {
326 "GLib-FATAL-WARNING: this is a regular g_warning() from the test suite",
327 "GLib-FATAL-CRITICAL: this is a regular g_critical() from the test suite",
328 "GLib-FATAL-ERROR: this is a regular g_error() from the test suite"
330 g_assert_cmpint (messages, <, G_N_ELEMENTS (known_messages));
331 g_assert_cmpstr (msg->strings[0], ==, known_messages[messages]);
332 messages++;
334 break;
335 default:
336 g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type));
338 g_test_log_msg_free (msg);
341 g_free (argv[1]);
342 g_main_loop_unref (loop);
343 g_test_log_buffer_free (tlb);
346 g_assert_cmpint (messages, ==, 3);
350 main (int argc,
351 char**argv)
353 argv0 = argv[0];
355 g_test_init (&argc, &argv, NULL);
357 /* we use ourself as the testcase, these are the ones we need internally */
358 g_test_add_func ("/glib/testing/protocol/debug", debug);
359 g_test_add_func ("/glib/testing/protocol/info", info);
360 g_test_add_func ("/glib/testing/protocol/message", message);
361 g_test_add_func ("/glib/testing/protocol/warning", warning);
362 g_test_add_func ("/glib/testing/protocol/critical", critical);
363 g_test_add_func ("/glib/testing/protocol/error", error);
364 g_test_add_func ("/glib/testing/protocol/gtest-message", gtest_message);
366 /* these are the real tests */
367 g_test_add_func ("/glib/testing/protocol/test-message", test_message);
368 g_test_add_func ("/glib/testing/protocol/test-error", test_error);
370 return g_test_run ();
373 /* vim:set et sw=2 cino=t0,f0,(0,{s,>2s,n-1s,^-1s,e2s: */