Remove redundant header inclusions
[glib.git] / glib / tests / protocol.c
blob4d8f63fd822bb74ce173ed0dcb6d1b0726dbad4d
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 #ifndef _WIN32
26 #include <unistd.h> /* pipe() */
27 #else
28 #include <io.h>
29 #include <fcntl.h>
30 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
31 #endif
33 static void
34 debug (void)
36 if (g_test_verbose ())
37 g_debug ("this is a regular g_debug() from the test suite");
40 static void
41 info (void)
43 #ifdef g_info
44 #error "rewrite this to use g_info()"
45 #endif
46 if (g_test_verbose ())
47 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "this is a regular g_log(..., G_LOG_LEVEL_INFO, ...) from the test suite");
50 static void
51 message (void)
53 if (g_test_verbose ())
54 g_message ("this is a regular g_message() from the test suite");
57 static void
58 warning (void)
60 if (g_test_verbose ())
61 g_warning ("this is a regular g_warning() from the test suite");
64 static void
65 critical (void)
67 if (g_test_verbose ())
68 g_critical ("this is a regular g_critical() from the test suite");
71 static void
72 error (void)
74 if (g_test_verbose ())
75 g_error ("this is a regular g_error() from the test suite");
78 static void
79 gtest_message (void)
81 if (g_test_verbose ())
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 "./protocol",
123 NULL,
124 "--verbose",
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;
142 if (0 > pipe (pipes))
144 g_error ("error creating pipe: %s", g_strerror (errno));
147 argv[1] = g_strdup_printf ("--GTestLogFD=%u", pipes[1]);
149 if (!g_spawn_async (NULL,
150 argv, NULL,
151 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
152 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
153 NULL, NULL, &pid,
154 &error))
156 g_error ("error spawning the test: %s", error->message);
159 tlb = g_test_log_buffer_new ();
160 loop = g_main_loop_new (NULL, FALSE);
162 channel = g_io_channel_unix_new (pipes[0]);
163 g_io_channel_set_close_on_unref (channel, TRUE);
164 g_io_channel_set_encoding (channel, NULL, NULL);
165 g_io_channel_set_buffered (channel, FALSE);
166 g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
168 io_source = g_io_add_watch (channel, G_IO_IN, test_message_cb1, tlb);
169 child_source = g_child_watch_add (pid, test_message_cb2, loop);
171 g_main_loop_run (loop);
173 test_message_cb1 (channel, G_IO_IN, tlb);
175 g_assert (!g_source_remove (child_source));
176 g_assert (g_source_remove (io_source));
177 g_io_channel_unref (channel);
179 for (msg = g_test_log_buffer_pop (tlb);
180 msg;
181 msg = g_test_log_buffer_pop (tlb))
183 switch (msg->log_type)
185 case G_TEST_LOG_START_BINARY:
186 case G_TEST_LOG_START_CASE:
187 /* ignore */
188 break;
189 case G_TEST_LOG_STOP_CASE:
190 passed++;
191 break;
192 case G_TEST_LOG_MESSAGE:
194 gchar const* known_messages[] = {
195 "this is a regular g_test_message() from the test suite",
196 "MESSAGE: this is a regular g_message() from the test suite",
197 "DEBUG: this is a regular g_debug() from the test suite"
199 g_assert_cmpint (messages, <, G_N_ELEMENTS (known_messages));
200 g_assert_cmpstr (msg->strings[0], ==, known_messages[messages]);
201 messages++;
203 break;
204 case G_TEST_LOG_ERROR:
205 g_assert_not_reached ();
206 break;
207 default:
208 g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type));
212 g_assert_cmpint (passed, ==, 3);
213 g_assert_cmpint (messages, ==, 3);
216 static void
217 test_error (void)
219 gchar* tests[] = {
220 "/glib/testing/protocol/warning",
221 "/glib/testing/protocol/critical",
222 "/glib/testing/protocol/error"
224 gint i;
225 int messages = 0;
227 for (i = 0; i < G_N_ELEMENTS (tests); i++)
229 gchar* argv[] = {
230 "./protocol",
231 NULL,
232 "--verbose",
233 "-p", tests[i],
234 NULL
236 GTestLogBuffer* tlb;
237 GTestLogMsg * msg;
238 GIOChannel * channel;
239 GMainLoop * loop;
240 GError * error = NULL;
241 gulong child_source;
242 gulong io_source;
243 GPid pid = 0;
244 int pipes[2];
246 if (0 > pipe (pipes))
248 g_error ("error creating pipe: %s", g_strerror (errno));
251 argv[1] = g_strdup_printf ("--GTestLogFD=%u", pipes[1]);
253 if (!g_spawn_async (NULL,
254 argv, NULL,
255 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
256 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
257 NULL, NULL, &pid,
258 &error))
260 g_error ("error spawning the test: %s", error->message);
263 tlb = g_test_log_buffer_new ();
264 loop = g_main_loop_new (NULL, FALSE);
266 channel = g_io_channel_unix_new (pipes[0]);
267 g_io_channel_set_close_on_unref (channel, TRUE);
268 g_io_channel_set_encoding (channel, NULL, NULL);
269 g_io_channel_set_buffered (channel, FALSE);
270 g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
272 io_source = g_io_add_watch (channel, G_IO_IN, test_message_cb1, tlb);
273 child_source = g_child_watch_add (pid, test_message_cb2, loop);
275 g_main_loop_run (loop);
277 test_message_cb1 (channel, G_IO_IN, tlb);
279 g_assert (!g_source_remove (child_source));
280 g_assert (g_source_remove (io_source));
281 g_io_channel_unref (channel);
283 for (msg = g_test_log_buffer_pop (tlb);
284 msg;
285 msg = g_test_log_buffer_pop (tlb))
287 switch (msg->log_type)
289 case G_TEST_LOG_START_BINARY:
290 case G_TEST_LOG_START_CASE:
291 /* ignore */
292 break;
293 case G_TEST_LOG_STOP_CASE:
294 case G_TEST_LOG_MESSAGE:
295 g_assert_not_reached ();
296 break;
297 case G_TEST_LOG_ERROR:
299 gchar const* known_messages[] = {
300 "FATAL-WARNING: this is a regular g_warning() from the test suite",
301 "FATAL-CRITICAL: this is a regular g_critical() from the test suite",
302 "FATAL-ERROR: this is a regular g_error() from the test suite"
304 g_assert_cmpint (messages, <, G_N_ELEMENTS (known_messages));
305 g_assert_cmpstr (msg->strings[0], ==, known_messages[messages]);
306 messages++;
308 break;
309 default:
310 g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type));
315 g_assert_cmpint (messages, ==, 3);
319 main (int argc,
320 char**argv)
322 g_test_init (&argc, &argv, NULL);
324 /* we use ourself as the testcase, these are the ones we need internally */
325 g_test_add_func ("/glib/testing/protocol/debug", debug);
326 g_test_add_func ("/glib/testing/protocol/info", info);
327 g_test_add_func ("/glib/testing/protocol/message", message);
328 g_test_add_func ("/glib/testing/protocol/warning", warning);
329 g_test_add_func ("/glib/testing/protocol/critical", critical);
330 g_test_add_func ("/glib/testing/protocol/error", error);
331 g_test_add_func ("/glib/testing/protocol/gtest-message", gtest_message);
333 /* these are the real tests */
334 g_test_add_func ("/glib/testing/protocol/test-message", test_message);
335 g_test_add_func ("/glib/testing/protocol/test-error", test_error);
337 return g_test_run ();
340 /* vim:set et sw=2 cino=t0,f0,(0,{s,>2s,n-1s,^-1s,e2s: */