egg-testing.c 5.5 KB
Newer Older
1 2 3 4
/*
 * gnome-keyring
 *
 * Copyright (C) 2011 Collabora Ltd.
Stef Walter's avatar
Stef Walter committed
5 6 7
 * 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as
8 9
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
Stef Walter's avatar
Stef Walter committed
10
 *  
11 12 13
 * 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
Stef Walter's avatar
Stef Walter committed
14 15 16
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this program; if not, write to the Free Software
Stef Walter's avatar
Stef Walter committed
18 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
20
 *
Stef Walter's avatar
Stef Walter committed
21
 * Author: Stef Walter <stefw@collabora.co.uk>
22 23 24 25 26 27
 */

#include "config.h"

#include "egg-testing.h"

28 29
#include <glib-object.h>

30 31 32
#include <glib.h>
#include <glib/gstdio.h>

33
#include <errno.h>
34
#include <fcntl.h>
35 36 37 38
#include <unistd.h>

static const char HEXC[] = "0123456789ABCDEF";

39 40 41
gchar *
egg_test_escape_data (const guchar *data,
                      gsize n_data)
42 43
{
	GString *result;
44
	gchar c;
45 46 47
	gsize i;
	guchar j;

48
	g_assert_nonnull (data);
49 50 51

	result = g_string_sized_new (n_data * 2 + 1);
	for (i = 0; i < n_data; ++i) {
52 53 54 55 56 57 58 59 60 61
		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]);
		}
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	}

	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;
80 81
  a1 = arg1 ? egg_test_escape_data (arg1, n_arg1) : g_strdup ("NULL");
  a2 = arg2 ? egg_test_escape_data (arg2, n_arg2) : g_strdup ("NULL");
82 83 84 85 86 87 88
  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);
}

89 90 91
static void (*wait_stop_impl) (void);
static gboolean (*wait_until_impl) (int timeout);

92 93 94
void
egg_test_wait_stop (void)
{
95
	g_assert_nonnull (wait_stop_impl);
96
	(wait_stop_impl) ();
97 98 99 100 101
}

gboolean
egg_test_wait_until (int timeout)
{
102
	g_assert_nonnull (wait_until_impl);
103
	return (wait_until_impl) (timeout);
Stef Walter's avatar
Stef Walter committed
104 105 106 107 108 109 110
}

void
egg_test_wait_idle (void)
{
	GMainContext *context;

111
	g_assert_nonnull (wait_until_impl);
Stef Walter's avatar
Stef Walter committed
112 113 114

	context = g_main_context_get_thread_default ();
	while (g_main_context_iteration (context, FALSE));
115
}
116

117 118 119 120 121
static GMainLoop *wait_loop = NULL;

static void
loop_wait_stop (void)
{
122
	g_assert_nonnull (wait_loop);
123
	g_main_loop_quit (wait_loop);
124 125
}

126 127
static gboolean
on_loop_wait_timeout (gpointer data)
128
{
129 130 131
	gboolean *timed_out = data;
	*timed_out = TRUE;

132
	g_assert_nonnull (wait_loop);
133 134 135
	g_main_loop_quit (wait_loop);

	return TRUE; /* we remove this source later */
136 137
}

138 139
static gboolean
loop_wait_until (int timeout)
140
{
141 142
	gboolean timed_out = FALSE;
	guint source;
143

144
	g_assert_null (wait_loop);
145
	wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
146

147
	source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
148

149
	g_main_loop_run (wait_loop);
150

151
	g_source_remove (source);
152 153
	g_main_loop_unref (wait_loop);
	wait_loop = NULL;
154
	return !timed_out;
155
}
156

157 158 159 160
gint
egg_tests_run_with_loop (void)
{
	gint ret;
161

162 163 164 165 166 167 168 169 170 171 172
	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;
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

void
egg_tests_copy_scratch_file (const gchar *directory,
                             const gchar *filename)
{
	GError *error = NULL;
	gchar *basename;
	gchar *contents;
	gchar *destination;
	gsize length;

	g_assert (directory);

	g_file_get_contents (filename, &contents, &length, &error);
	g_assert_no_error (error);

	basename = g_path_get_basename (filename);
	destination = g_build_filename (directory, basename, NULL);
	g_free (basename);

	g_file_set_contents (destination, contents, length, &error);
	g_assert_no_error (error);
	g_free (destination);
	g_free (contents);
}

gchar *
egg_tests_create_scratch_directory (const gchar *file_to_copy,
                                    ...)
{
	gchar *basename;
	gchar *directory;
	va_list va;

	basename = g_path_get_basename (g_get_prgname ());
	directory = g_strdup_printf ("/tmp/scratch-%s.XXXXXX", basename);
	g_free (basename);

	if (!g_mkdtemp (directory))
		g_assert_not_reached ();

	va_start (va, file_to_copy);

	while (file_to_copy != NULL) {
		egg_tests_copy_scratch_file (directory, file_to_copy);
		file_to_copy = va_arg (va, const gchar *);
	}

	va_end (va);

	return directory;
}

void
egg_tests_remove_scratch_directory (const gchar *directory)
{
	gchar *argv[] = { "rm", "-rf", (gchar *)directory, NULL };
	GError *error = NULL;
	gint rm_status;

	g_assert_cmpstr (directory, !=, "");
	g_assert_cmpstr (directory, !=, "/");

	g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
	              NULL, NULL, NULL, &rm_status, &error);
	g_assert_no_error (error);
	g_assert_cmpint (rm_status, ==, 0);
}