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
|
#define _GNU_SOURCE
#include "../common/dconf-paths.h"
#include <glib/gstdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <dlfcn.h>
#include "../shm/dconf-shm.h"
#include "../shm/dconf-shm-mockable.h"
#include "tmpdir.h"
static void
test_mkdir_fail (void)
{
guint8 *shm;
if (g_test_subprocess ())
{
gchar *evil;
gint fd;
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
evil = g_build_filename (g_get_user_runtime_dir (), "dconf", NULL);
fd = open (evil, O_WRONLY | O_CREAT, 0600);
close (fd);
shm = dconf_shm_open ("foo");
g_assert (shm == NULL);
g_unlink (evil);
g_free (evil);
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("*unable to create directory*");
}
static void
test_close_null (void)
{
dconf_shm_close (NULL);
}
static void
test_open_and_flag (void)
{
guint8 *shm;
shm = dconf_shm_open ("foo");
g_assert (shm != NULL);
g_assert (!dconf_shm_is_flagged (shm));
dconf_shm_flag ("foo");
g_assert (dconf_shm_is_flagged (shm));
dconf_shm_close (shm);
}
static void
test_invalid_name (void)
{
if (g_test_subprocess ())
{
guint8 *shm;
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
shm = dconf_shm_open ("foo/bar");
g_assert (shm == NULL);
g_assert (dconf_shm_is_flagged (shm));
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("*unable to create*foo/bar*");
}
static void
test_flag_nonexistent (void)
{
dconf_shm_flag ("does-not-exist");
}
static gboolean should_fail_pwrite;
/* interpose */
ssize_t
dconf_shm_pwrite (int fd, const void *buf, size_t count, off_t offset)
{
if (should_fail_pwrite)
{
errno = ENOSPC;
return -1;
}
return pwrite (fd, buf, count, offset);
}
static void
test_out_of_space_open (void)
{
if (g_test_subprocess ())
{
guint8 *shm;
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
should_fail_pwrite = TRUE;
shm = dconf_shm_open ("foo");
g_assert (shm == NULL);
g_assert (dconf_shm_is_flagged (shm));
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("*failed to allocate*foo*");
}
static void
test_out_of_space_flag (void)
{
if (g_test_subprocess ())
{
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
should_fail_pwrite = TRUE;
dconf_shm_flag ("foo");
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
int
main (int argc, char **argv)
{
gchar *temp;
gint status;
temp = dconf_test_create_tmpdir ();
g_setenv ("XDG_RUNTIME_DIR", temp, TRUE);
/* This currently works, but it is possible that one day GLib will
* read the XDG_RUNTIME_DIR variable (and cache its value) as a
* side-effect of the dconf_test_create_tmpdir() call above.
*
* This assert will quickly uncover the problem in that case...
*/
g_assert_cmpstr (g_get_user_runtime_dir (), ==, temp);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/shm/mkdir-fail", test_mkdir_fail);
g_test_add_func ("/shm/close-null", test_close_null);
g_test_add_func ("/shm/open-and-flag", test_open_and_flag);
g_test_add_func ("/shm/invalid-name", test_invalid_name);
g_test_add_func ("/shm/flag-nonexistent", test_flag_nonexistent);
g_test_add_func ("/shm/out-of-space-open", test_out_of_space_open);
g_test_add_func ("/shm/out-of-space-flag", test_out_of_space_flag);
status = g_test_run ();
dconf_test_remove_tmpdir (temp);
g_free (temp);
return status;
}
|