Valgrind integration, refactor build process
[platform/upstream/libsecret.git] / egg / egg-testing.c
1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2011 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General  License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General  License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.
20  *
21  * Stef Walter <stefw@collabora.co.uk>
22  */
23
24 #include "config.h"
25
26 #include "egg-testing.h"
27
28 #include <glib-object.h>
29
30 #include <valgrind/valgrind.h>
31
32 #include <errno.h>
33 #include <unistd.h>
34
35 static const char HEXC[] = "0123456789ABCDEF";
36
37 static gchar*
38 hex_dump (const guchar *data, gsize n_data)
39 {
40         GString *result;
41         gsize i;
42         guchar j;
43
44         g_assert (data);
45
46         result = g_string_sized_new (n_data * 2 + 1);
47         for (i = 0; i < n_data; ++i) {
48                 g_string_append (result, "\\x");
49
50                 j = data[i] >> 4 & 0xf;
51                 g_string_append_c (result, HEXC[j]);
52                 j = data[i] & 0xf;
53                 g_string_append_c (result, HEXC[j]);
54         }
55
56         return g_string_free (result, FALSE);
57 }
58
59 void
60 egg_assertion_not_object (const char *domain,
61                           const char *file,
62                           int         line,
63                           const char *func,
64                           const char *expr,
65                           gpointer was_object)
66 {
67         gchar *s;
68
69         if (RUNNING_ON_VALGRIND)
70                 return;
71         if (G_IS_OBJECT (was_object)) {
72                 s = g_strdup_printf ("assertion failed: %s is still referenced", expr);
73                 g_assertion_message (domain, file, line, func, s);
74                 g_free (s);
75         }
76 }
77
78 void
79 egg_assertion_message_cmpmem (const char     *domain,
80                               const char     *file,
81                               int             line,
82                               const char     *func,
83                               const char     *expr,
84                               gconstpointer   arg1,
85                               gsize           n_arg1,
86                               const char     *cmp,
87                               gconstpointer   arg2,
88                               gsize           n_arg2)
89 {
90   char *a1, *a2, *s;
91   a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL");
92   a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL");
93   s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
94   g_free (a1);
95   g_free (a2);
96   g_assertion_message (domain, file, line, func, s);
97   g_free (s);
98 }
99
100 static void (*wait_stop_impl) (void);
101 static gboolean (*wait_until_impl) (int timeout);
102
103 void
104 egg_test_wait_stop (void)
105 {
106         g_assert (wait_stop_impl != NULL);
107         (wait_stop_impl) ();
108 }
109
110 gboolean
111 egg_test_wait_until (int timeout)
112 {
113         g_assert (wait_until_impl != NULL);
114         return (wait_until_impl) (timeout);
115 }
116
117 static GMainLoop *wait_loop = NULL;
118
119 static void
120 loop_wait_stop (void)
121 {
122         g_assert (wait_loop != NULL);
123         g_main_loop_quit (wait_loop);
124 }
125
126 static gboolean
127 on_loop_wait_timeout (gpointer data)
128 {
129         gboolean *timed_out = data;
130         *timed_out = TRUE;
131
132         g_assert (wait_loop != NULL);
133         g_main_loop_quit (wait_loop);
134
135         return TRUE; /* we remove this source later */
136 }
137
138 static gboolean
139 loop_wait_until (int timeout)
140 {
141         gboolean ret = FALSE;
142         gboolean timed_out = FALSE;
143         guint source;
144
145         g_assert (wait_loop == NULL);
146         wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
147
148         source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
149
150         g_main_loop_run (wait_loop);
151
152         if (timed_out) {
153                 g_source_remove (source);
154                 ret = FALSE;
155         } else {
156                 ret = TRUE;
157         }
158
159         g_main_loop_unref (wait_loop);
160         wait_loop = NULL;
161         return ret;
162 }
163
164 gint
165 egg_tests_run_with_loop (void)
166 {
167         gint ret;
168
169         wait_stop_impl = loop_wait_stop;
170         wait_until_impl = loop_wait_until;
171
172         ret = g_test_run ();
173
174         wait_stop_impl = NULL;
175         wait_until_impl = NULL;
176
177         while (g_main_context_iteration (NULL, FALSE));
178
179         return ret;
180 }