1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/* This file is part of GLib
*
* Copyright (C) 2010 Sven Herzberg
*
* SPDX-License-Identifier: LicenseRef-old-glib-tests
*
* This work is provided "as is"; redistribution and modification
* in whole or in part, in any medium, physical or electronic is
* permitted without restriction.
*
* This work is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* In no event shall the authors or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*/
#include <errno.h> /* errno */
#include <glib.h>
#ifdef G_OS_UNIX
#include <unistd.h> /* pipe() */
#endif
#ifdef G_OS_WIN32
#include <io.h>
#include <fcntl.h>
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
#endif
static const char *argv0;
static void
debug (void)
{
if (g_test_subprocess ())
g_debug ("this is a regular g_debug() from the test suite");
}
static void
info (void)
{
if (g_test_subprocess ())
g_info ("this is a regular g_info from the test suite");
}
static void
message (void)
{
if (g_test_subprocess ())
g_message ("this is a regular g_message() from the test suite");
}
static void
warning (void)
{
if (g_test_subprocess ())
g_warning ("this is a regular g_warning() from the test suite");
}
static void
critical (void)
{
if (g_test_subprocess ())
g_critical ("this is a regular g_critical() from the test suite");
}
static void
error (void)
{
if (g_test_subprocess ())
g_error ("this is a regular g_error() from the test suite");
}
static void
gtest_message (void)
{
if (g_test_subprocess ())
g_test_message ("this is a regular g_test_message() from the test suite");
}
static gboolean
test_message_cb1 (GIOChannel * channel,
GIOCondition condition,
gpointer user_data)
{
GIOStatus status;
guchar buf[512];
gsize read_bytes = 0;
g_assert_cmpuint (condition, ==, G_IO_IN);
for (status = g_io_channel_read_chars (channel, (gchar*)buf, sizeof (buf), &read_bytes, NULL);
status == G_IO_STATUS_NORMAL;
status = g_io_channel_read_chars (channel, (gchar*)buf, sizeof (buf), &read_bytes, NULL))
{
g_test_log_buffer_push (user_data, read_bytes, buf);
}
if (status == G_IO_STATUS_EOF)
return FALSE;
else
g_assert_cmpuint (status, ==, G_IO_STATUS_AGAIN);
return TRUE;
}
static void
test_message_cb2 (GPid pid,
gint status,
gpointer user_data)
{
g_spawn_close_pid (pid);
g_main_loop_quit (user_data);
}
static void
test_message (void)
{
gchar* argv[] = {
(gchar*)argv0,
NULL,
"--GTestSubprocess",
"-p", "/glib/testing/protocol/gtest-message",
"-p", "/glib/testing/protocol/message",
"-p", "/glib/testing/protocol/debug",
NULL
};
GTestLogBuffer* tlb;
GTestLogMsg * msg;
GIOChannel * channel;
GMainLoop * loop;
GError * error = NULL;
gulong child_source;
GPid pid = 0;
int pipes[2];
int passed = 0;
int messages = 0;
const char * line_term;
int line_term_len;
if (0 > pipe (pipes))
{
int errsv = errno;
g_error ("error creating pipe: %s", g_strerror (errsv));
}
argv[1] = g_strdup_printf ("--GTestLogFD=%u", pipes[1]);
if (!g_spawn_async (NULL,
argv, NULL,
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &pid,
&error))
{
g_error ("error spawning the test: %s", error->message);
}
close (pipes[1]);
tlb = g_test_log_buffer_new ();
loop = g_main_loop_new (NULL, FALSE);
#ifdef G_OS_WIN32
channel = g_io_channel_win32_new_fd (pipes[0]);
#else
channel = g_io_channel_unix_new (pipes[0]);
#endif
g_io_channel_set_close_on_unref (channel, TRUE);
g_io_channel_set_encoding (channel, NULL, NULL);
g_io_channel_set_buffered (channel, FALSE);
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
g_assert (g_io_channel_get_line_term (channel, NULL) == NULL);
g_io_channel_set_line_term (channel, "\n", 1);
line_term = g_io_channel_get_line_term (channel, &line_term_len);
g_assert_cmpint (*line_term, ==, '\n');
g_assert_cmpint (line_term_len, ==, 1);
g_assert (g_io_channel_get_close_on_unref (channel));
g_assert (g_io_channel_get_encoding (channel) == NULL);
g_assert (!g_io_channel_get_buffered (channel));
g_io_add_watch (channel, G_IO_IN, test_message_cb1, tlb);
child_source = g_child_watch_add (pid, test_message_cb2, loop);
g_main_loop_run (loop);
test_message_cb1 (channel, G_IO_IN, tlb);
g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "Source ID*");
g_assert (!g_source_remove (child_source));
g_test_assert_expected_messages ();
g_io_channel_unref (channel);
for (msg = g_test_log_buffer_pop (tlb);
msg;
msg = g_test_log_buffer_pop (tlb))
{
switch (msg->log_type)
{
case G_TEST_LOG_START_BINARY:
case G_TEST_LOG_START_CASE:
case G_TEST_LOG_START_SUITE:
case G_TEST_LOG_STOP_SUITE:
/* ignore */
break;
case G_TEST_LOG_STOP_CASE:
passed++;
break;
case G_TEST_LOG_MESSAGE:
{
gchar const* known_messages[] = {
"this is a regular g_test_message() from the test suite",
"GLib-MESSAGE: this is a regular g_message() from the test suite",
"GLib-DEBUG: this is a regular g_debug() from the test suite"
};
g_assert_cmpint (messages, <, G_N_ELEMENTS (known_messages));
g_assert_cmpstr (msg->strings[0], ==, known_messages[messages]);
messages++;
}
break;
case G_TEST_LOG_ERROR:
g_assert_not_reached ();
break;
default:
g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type));
}
g_test_log_msg_free (msg);
}
g_assert_cmpint (passed, ==, 3);
g_assert_cmpint (messages, ==, 3);
g_free (argv[1]);
g_main_loop_unref (loop);
g_test_log_buffer_free (tlb);
}
static void
test_error (void)
{
gchar* tests[] = {
"/glib/testing/protocol/warning",
"/glib/testing/protocol/critical",
"/glib/testing/protocol/error"
};
gsize i;
int messages = 0;
for (i = 0; i < G_N_ELEMENTS (tests); i++)
{
gchar* argv[] = {
(gchar*)argv0,
NULL,
"--GTestSubprocess",
"-p", tests[i],
NULL
};
GTestLogBuffer* tlb;
GTestLogMsg * msg;
GIOChannel * channel;
GMainLoop * loop;
GError * error = NULL;
gulong child_source;
GPid pid = 0;
int pipes[2];
if (0 > pipe (pipes))
{
int errsv = errno;
g_error ("error creating pipe: %s", g_strerror (errsv));
}
argv[1] = g_strdup_printf ("--GTestLogFD=%u", pipes[1]);
if (!g_spawn_async (NULL,
argv, NULL,
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL, &pid,
&error))
{
g_error ("error spawning the test: %s", error->message);
}
close (pipes[1]);
tlb = g_test_log_buffer_new ();
loop = g_main_loop_new (NULL, FALSE);
#ifdef G_OS_WIN32
channel = g_io_channel_win32_new_fd (pipes[0]);
#else
channel = g_io_channel_unix_new (pipes[0]);
#endif
g_io_channel_set_close_on_unref (channel, TRUE);
g_io_channel_set_encoding (channel, NULL, NULL);
g_io_channel_set_buffered (channel, FALSE);
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
g_io_add_watch (channel, G_IO_IN, test_message_cb1, tlb);
child_source = g_child_watch_add (pid, test_message_cb2, loop);
g_main_loop_run (loop);
test_message_cb1 (channel, G_IO_IN, tlb);
g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "Source ID*");
g_assert (!g_source_remove (child_source));
g_test_assert_expected_messages ();
g_io_channel_unref (channel);
for (msg = g_test_log_buffer_pop (tlb);
msg;
msg = g_test_log_buffer_pop (tlb))
{
switch (msg->log_type)
{
case G_TEST_LOG_START_BINARY:
case G_TEST_LOG_START_CASE:
case G_TEST_LOG_START_SUITE:
case G_TEST_LOG_STOP_SUITE:
/* ignore */
break;
case G_TEST_LOG_STOP_CASE:
case G_TEST_LOG_MESSAGE:
g_assert_not_reached ();
break;
case G_TEST_LOG_ERROR:
{
gchar const* known_messages[] = {
"GLib-FATAL-WARNING: this is a regular g_warning() from the test suite",
"GLib-FATAL-CRITICAL: this is a regular g_critical() from the test suite",
"GLib-FATAL-ERROR: this is a regular g_error() from the test suite"
};
g_assert_cmpint (messages, <, G_N_ELEMENTS (known_messages));
g_assert_cmpstr (msg->strings[0], ==, known_messages[messages]);
messages++;
}
break;
default:
g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type));
}
g_test_log_msg_free (msg);
}
g_free (argv[1]);
g_main_loop_unref (loop);
g_test_log_buffer_free (tlb);
}
g_assert_cmpint (messages, ==, 3);
}
int
main (int argc,
char**argv)
{
argv0 = argv[0];
g_test_init (&argc, &argv, NULL);
/* we use ourself as the testcase, these are the ones we need internally */
g_test_add_func ("/glib/testing/protocol/debug", debug);
g_test_add_func ("/glib/testing/protocol/info", info);
g_test_add_func ("/glib/testing/protocol/message", message);
g_test_add_func ("/glib/testing/protocol/warning", warning);
g_test_add_func ("/glib/testing/protocol/critical", critical);
g_test_add_func ("/glib/testing/protocol/error", error);
g_test_add_func ("/glib/testing/protocol/gtest-message", gtest_message);
/* these are the real tests */
g_test_add_func ("/glib/testing/protocol/test-message", test_message);
g_test_add_func ("/glib/testing/protocol/test-error", test_error);
return g_test_run ();
}
/* vim:set et sw=2 cino=t0,f0,(0,{s,>2s,n-1s,^-1s,e2s: */
|