2011-09-25 19:13:00 +00:00
|
|
|
/*
|
|
|
|
* gnome-keyring
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Collabora Ltd.
|
2012-03-31 13:32:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
2011-09-25 19:13:00 +00:00
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
2012-03-31 13:32:43 +00:00
|
|
|
*
|
2011-09-25 19:13:00 +00:00
|
|
|
* This program 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. See the GNU
|
2012-03-31 13:32:43 +00:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-09-25 19:13:00 +00:00
|
|
|
* License along with this program; if not, write to the Free Software
|
2012-03-31 13:32:43 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
2011-09-25 19:13:00 +00:00
|
|
|
*
|
2012-03-31 13:32:43 +00:00
|
|
|
* Author: Stef Walter <stefw@collabora.co.uk>
|
2011-09-25 19:13:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "egg-testing.h"
|
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
|
2012-01-30 15:07:55 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2011-09-25 19:13:00 +00:00
|
|
|
#include <errno.h>
|
2012-01-30 15:07:55 +00:00
|
|
|
#include <fcntl.h>
|
2011-09-25 19:13:00 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static const char HEXC[] = "0123456789ABCDEF";
|
|
|
|
|
2012-01-30 16:49:22 +00:00
|
|
|
gchar *
|
|
|
|
egg_test_escape_data (const guchar *data,
|
|
|
|
gsize n_data)
|
2011-09-25 19:13:00 +00:00
|
|
|
{
|
|
|
|
GString *result;
|
2012-01-30 16:49:22 +00:00
|
|
|
gchar c;
|
2011-09-25 19:13:00 +00:00
|
|
|
gsize i;
|
|
|
|
guchar j;
|
|
|
|
|
2012-01-30 16:49:22 +00:00
|
|
|
g_assert (data != NULL);
|
2011-09-25 19:13:00 +00:00
|
|
|
|
|
|
|
result = g_string_sized_new (n_data * 2 + 1);
|
|
|
|
for (i = 0; i < n_data; ++i) {
|
2012-01-30 16:49:22 +00:00
|
|
|
c = data[i];
|
|
|
|
if (g_ascii_isprint (c) && !strchr ("\n\r\v", c)) {
|
|
|
|
g_string_append_c (result, c);
|
|
|
|
} else {
|
|
|
|
g_string_append (result, "\\x");
|
|
|
|
j = c >> 4 & 0xf;
|
|
|
|
g_string_append_c (result, HEXC[j]);
|
|
|
|
j = c & 0xf;
|
|
|
|
g_string_append_c (result, HEXC[j]);
|
|
|
|
}
|
2011-09-25 19:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (result, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
egg_assertion_message_cmpmem (const char *domain,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *func,
|
|
|
|
const char *expr,
|
|
|
|
gconstpointer arg1,
|
|
|
|
gsize n_arg1,
|
|
|
|
const char *cmp,
|
|
|
|
gconstpointer arg2,
|
|
|
|
gsize n_arg2)
|
|
|
|
{
|
|
|
|
char *a1, *a2, *s;
|
2012-01-30 16:49:22 +00:00
|
|
|
a1 = arg1 ? egg_test_escape_data (arg1, n_arg1) : g_strdup ("NULL");
|
|
|
|
a2 = arg2 ? egg_test_escape_data (arg2, n_arg2) : g_strdup ("NULL");
|
2011-09-25 19:13:00 +00:00
|
|
|
s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
|
|
|
|
g_free (a1);
|
|
|
|
g_free (a2);
|
|
|
|
g_assertion_message (domain, file, line, func, s);
|
|
|
|
g_free (s);
|
|
|
|
}
|
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
static void (*wait_stop_impl) (void);
|
|
|
|
static gboolean (*wait_until_impl) (int timeout);
|
|
|
|
|
2011-09-25 19:13:00 +00:00
|
|
|
void
|
|
|
|
egg_test_wait_stop (void)
|
|
|
|
{
|
2011-11-06 12:38:19 +00:00
|
|
|
g_assert (wait_stop_impl != NULL);
|
|
|
|
(wait_stop_impl) ();
|
2011-09-25 19:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
egg_test_wait_until (int timeout)
|
|
|
|
{
|
2011-11-06 12:38:19 +00:00
|
|
|
g_assert (wait_until_impl != NULL);
|
|
|
|
return (wait_until_impl) (timeout);
|
2012-01-11 06:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
egg_test_wait_idle (void)
|
|
|
|
{
|
|
|
|
GMainContext *context;
|
|
|
|
|
|
|
|
g_assert (wait_until_impl != NULL);
|
|
|
|
|
|
|
|
context = g_main_context_get_thread_default ();
|
|
|
|
while (g_main_context_iteration (context, FALSE));
|
2011-11-06 12:38:19 +00:00
|
|
|
}
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
static GMainLoop *wait_loop = NULL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
loop_wait_stop (void)
|
|
|
|
{
|
|
|
|
g_assert (wait_loop != NULL);
|
|
|
|
g_main_loop_quit (wait_loop);
|
2011-09-25 19:13:00 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
static gboolean
|
|
|
|
on_loop_wait_timeout (gpointer data)
|
2011-09-25 19:13:00 +00:00
|
|
|
{
|
2011-11-06 12:38:19 +00:00
|
|
|
gboolean *timed_out = data;
|
|
|
|
*timed_out = TRUE;
|
|
|
|
|
|
|
|
g_assert (wait_loop != NULL);
|
|
|
|
g_main_loop_quit (wait_loop);
|
|
|
|
|
|
|
|
return TRUE; /* we remove this source later */
|
2011-09-25 19:13:00 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
static gboolean
|
|
|
|
loop_wait_until (int timeout)
|
2011-09-25 19:13:00 +00:00
|
|
|
{
|
2011-11-06 12:38:19 +00:00
|
|
|
gboolean timed_out = FALSE;
|
|
|
|
guint source;
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
g_assert (wait_loop == NULL);
|
|
|
|
wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
g_main_loop_run (wait_loop);
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-12 07:08:12 +00:00
|
|
|
g_source_remove (source);
|
2011-11-06 12:38:19 +00:00
|
|
|
g_main_loop_unref (wait_loop);
|
|
|
|
wait_loop = NULL;
|
2011-11-12 07:08:12 +00:00
|
|
|
return !timed_out;
|
2011-11-06 12:38:19 +00:00
|
|
|
}
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
gint
|
|
|
|
egg_tests_run_with_loop (void)
|
|
|
|
{
|
|
|
|
gint ret;
|
2011-09-25 19:13:00 +00:00
|
|
|
|
2011-11-06 12:38:19 +00:00
|
|
|
wait_stop_impl = loop_wait_stop;
|
|
|
|
wait_until_impl = loop_wait_until;
|
|
|
|
|
|
|
|
ret = g_test_run ();
|
|
|
|
|
|
|
|
wait_stop_impl = NULL;
|
|
|
|
wait_until_impl = NULL;
|
|
|
|
|
|
|
|
while (g_main_context_iteration (NULL, FALSE));
|
|
|
|
|
|
|
|
return ret;
|
2011-09-25 19:13:00 +00:00
|
|
|
}
|