~ubuntu-branches/ubuntu/quantal/libgnome-keyring/quantal

« back to all changes in this revision

Viewing changes to egg/egg-testing.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-03-30 16:12:59 UTC
  • mfrom: (0.4.12)
  • Revision ID: package-import@ubuntu.com-20120330161259-21i2lhexhai0uiaf
Tags: 3.4.0-1
* New upstream release.
* Remove 00git_introspection.patch, merged upstream.
* Update 99_ltmain_as-needed.patch to dh-autoreconf's version.
* Update to debhelper v9.
* Bump Standards-Version to 3.9.3, with no further changes.
* Watch for .xz tarballs.
* Update Vcs-* URLs.
* Stop configuring with --enable-static and drop .a files.
* Pass -c4 to dh_makeshlibs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "egg-testing.h"
27
27
 
 
28
#include <glib-object.h>
 
29
 
 
30
#include <valgrind/valgrind.h>
 
31
 
 
32
#include <glib.h>
 
33
#include <glib/gstdio.h>
 
34
 
 
35
#include <errno.h>
 
36
#include <fcntl.h>
 
37
#include <unistd.h>
 
38
 
28
39
static const char HEXC[] = "0123456789ABCDEF";
29
40
 
30
 
static gchar*
31
 
hex_dump (const guchar *data, gsize n_data)
 
41
gchar *
 
42
egg_test_escape_data (const guchar *data,
 
43
                      gsize n_data)
32
44
{
33
45
        GString *result;
 
46
        gchar c;
34
47
        gsize i;
35
48
        guchar j;
36
49
 
37
 
        g_assert (data);
 
50
        g_assert (data != NULL);
38
51
 
39
52
        result = g_string_sized_new (n_data * 2 + 1);
40
53
        for (i = 0; i < n_data; ++i) {
41
 
                g_string_append (result, "\\x");
42
 
 
43
 
                j = data[i] >> 4 & 0xf;
44
 
                g_string_append_c (result, HEXC[j]);
45
 
                j = data[i] & 0xf;
46
 
                g_string_append_c (result, HEXC[j]);
 
54
                c = data[i];
 
55
                if (g_ascii_isprint (c) && !strchr ("\n\r\v", c)) {
 
56
                        g_string_append_c (result, c);
 
57
                } else {
 
58
                        g_string_append (result, "\\x");
 
59
                        j = c >> 4 & 0xf;
 
60
                        g_string_append_c (result, HEXC[j]);
 
61
                        j = c & 0xf;
 
62
                        g_string_append_c (result, HEXC[j]);
 
63
                }
47
64
        }
48
65
 
49
66
        return g_string_free (result, FALSE);
50
67
}
51
68
 
 
69
static gboolean
 
70
is_readable_ptr (gpointer was_object)
 
71
{
 
72
        static gint test_memory_fd = -1;
 
73
 
 
74
        /* First make sure this memory is still accessible */
 
75
        if (test_memory_fd < 0)
 
76
                test_memory_fd = g_open ("/dev/null", O_WRONLY, 0);
 
77
        if (write (test_memory_fd, was_object, 1) > 0)
 
78
                return TRUE;
 
79
        return (errno != EFAULT);
 
80
}
 
81
 
 
82
void
 
83
egg_assertion_not_object (const char *domain,
 
84
                          const char *file,
 
85
                          int         line,
 
86
                          const char *func,
 
87
                          const char *expr,
 
88
                          gpointer was_object)
 
89
{
 
90
        gchar *s;
 
91
 
 
92
        if (RUNNING_ON_VALGRIND)
 
93
                return;
 
94
 
 
95
        if (!is_readable_ptr (was_object))
 
96
                return;
 
97
 
 
98
        if (G_IS_OBJECT (was_object)) {
 
99
                s = g_strdup_printf ("assertion failed: %s is still referenced", expr);
 
100
                g_assertion_message (domain, file, line, func, s);
 
101
                g_free (s);
 
102
        }
 
103
}
 
104
 
52
105
void
53
106
egg_assertion_message_cmpmem (const char     *domain,
54
107
                              const char     *file,
62
115
                              gsize           n_arg2)
63
116
{
64
117
  char *a1, *a2, *s;
65
 
  a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL");
66
 
  a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL");
 
118
  a1 = arg1 ? egg_test_escape_data (arg1, n_arg1) : g_strdup ("NULL");
 
119
  a2 = arg2 ? egg_test_escape_data (arg2, n_arg2) : g_strdup ("NULL");
67
120
  s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
68
121
  g_free (a1);
69
122
  g_free (a2);
70
123
  g_assertion_message (domain, file, line, func, s);
71
124
  g_free (s);
72
125
}
 
126
 
 
127
static void (*wait_stop_impl) (void);
 
128
static gboolean (*wait_until_impl) (int timeout);
 
129
 
 
130
void
 
131
egg_test_wait_stop (void)
 
132
{
 
133
        g_assert (wait_stop_impl != NULL);
 
134
        (wait_stop_impl) ();
 
135
}
 
136
 
 
137
gboolean
 
138
egg_test_wait_until (int timeout)
 
139
{
 
140
        g_assert (wait_until_impl != NULL);
 
141
        return (wait_until_impl) (timeout);
 
142
}
 
143
 
 
144
void
 
145
egg_test_wait_idle (void)
 
146
{
 
147
        GMainContext *context;
 
148
 
 
149
        g_assert (wait_until_impl != NULL);
 
150
 
 
151
        context = g_main_context_get_thread_default ();
 
152
        while (g_main_context_iteration (context, FALSE));
 
153
}
 
154
 
 
155
static GMainLoop *wait_loop = NULL;
 
156
 
 
157
static void
 
158
loop_wait_stop (void)
 
159
{
 
160
        g_assert (wait_loop != NULL);
 
161
        g_main_loop_quit (wait_loop);
 
162
}
 
163
 
 
164
static gboolean
 
165
on_loop_wait_timeout (gpointer data)
 
166
{
 
167
        gboolean *timed_out = data;
 
168
        *timed_out = TRUE;
 
169
 
 
170
        g_assert (wait_loop != NULL);
 
171
        g_main_loop_quit (wait_loop);
 
172
 
 
173
        return TRUE; /* we remove this source later */
 
174
}
 
175
 
 
176
static gboolean
 
177
loop_wait_until (int timeout)
 
178
{
 
179
        gboolean timed_out = FALSE;
 
180
        guint source;
 
181
 
 
182
        g_assert (wait_loop == NULL);
 
183
        wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
 
184
 
 
185
        source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
 
186
 
 
187
        g_main_loop_run (wait_loop);
 
188
 
 
189
        g_source_remove (source);
 
190
        g_main_loop_unref (wait_loop);
 
191
        wait_loop = NULL;
 
192
        return !timed_out;
 
193
}
 
194
 
 
195
gint
 
196
egg_tests_run_with_loop (void)
 
197
{
 
198
        gint ret;
 
199
 
 
200
        wait_stop_impl = loop_wait_stop;
 
201
        wait_until_impl = loop_wait_until;
 
202
 
 
203
        ret = g_test_run ();
 
204
 
 
205
        wait_stop_impl = NULL;
 
206
        wait_until_impl = NULL;
 
207
 
 
208
        while (g_main_context_iteration (NULL, FALSE));
 
209
 
 
210
        return ret;
 
211
}