service: add support for service-db
[dconf.git] / tests / shm.c
blob86e3b34698885b63125212e3daed697498637a7a
1 #define _GNU_SOURCE
2 #include "../common/dconf-paths.h"
3 #include <glib/gstdio.h>
4 #include <sys/stat.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <dlfcn.h>
11 #include "../shm/dconf-shm.h"
12 #include "tmpdir.h"
14 static void
15 test_mkdir_fail (void)
17 guint8 *shm;
19 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
21 gchar *evil;
22 gint fd;
24 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
26 evil = g_build_filename (g_get_user_runtime_dir (), "dconf", NULL);
27 fd = open (evil, O_WRONLY | O_CREAT, 0600);
28 close (fd);
30 shm = dconf_shm_open ("foo");
31 g_assert (shm == NULL);
33 g_unlink (evil);
34 g_free (evil);
36 exit (0);
38 g_test_trap_assert_passed ();
39 g_test_trap_assert_stderr ("*unable to create directory*");
42 static void
43 test_close_null (void)
45 dconf_shm_close (NULL);
48 static void
49 test_open_and_flag (void)
51 guint8 *shm;
53 shm = dconf_shm_open ("foo");
54 g_assert (shm != NULL);
55 g_assert (!dconf_shm_is_flagged (shm));
56 dconf_shm_flag ("foo");
57 g_assert (dconf_shm_is_flagged (shm));
58 dconf_shm_close (shm);
61 static void
62 test_invalid_name (void)
64 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
66 guint8 *shm;
68 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
70 shm = dconf_shm_open ("foo/bar");
71 g_assert (shm == NULL);
72 g_assert (dconf_shm_is_flagged (shm));
73 exit (0);
75 g_test_trap_assert_passed ();
76 g_test_trap_assert_stderr ("*unable to create*foo/bar*");
79 static void
80 test_flag_nonexistent (void)
82 dconf_shm_flag ("does-not-exist");
85 static gboolean should_fail_pwrite;
86 /* interpose */
87 ssize_t
88 pwrite (int fd, const void *buf, size_t count, off_t offset)
90 static ssize_t (* real_pwrite) (int, const void *, size_t, off_t);
92 if (!real_pwrite)
93 real_pwrite = dlsym (RTLD_NEXT, "pwrite");
95 if (should_fail_pwrite)
97 errno = ENOSPC;
98 return -1;
101 return (* real_pwrite) (fd, buf, count, offset);
104 static void
105 test_out_of_space_open (void)
107 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
109 guint8 *shm;
111 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
112 should_fail_pwrite = TRUE;
114 shm = dconf_shm_open ("foo");
115 g_assert (shm == NULL);
116 g_assert (dconf_shm_is_flagged (shm));
117 exit (0);
119 g_test_trap_assert_passed ();
120 g_test_trap_assert_stderr ("*failed to allocate*foo*");
123 static void
124 test_out_of_space_flag (void)
126 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
128 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
129 should_fail_pwrite = TRUE;
131 dconf_shm_flag ("foo");
132 exit (0);
134 g_test_trap_assert_passed ();
138 main (int argc, char **argv)
140 gchar *temp;
141 gint status;
143 temp = dconf_test_create_tmpdir ();
145 g_setenv ("XDG_RUNTIME_DIR", temp, TRUE);
146 /* This currently works, but it is possible that one day GLib will
147 * read the XDG_RUNTIME_DIR variable (and cache its value) as a
148 * side-effect of the dconf_test_create_tmpdir() call above.
150 * This assert will quickly uncover the problem in that case...
152 g_assert_cmpstr (g_get_user_runtime_dir (), ==, temp);
154 g_test_init (&argc, &argv, NULL);
156 g_test_add_func ("/shm/mkdir-fail", test_mkdir_fail);
157 g_test_add_func ("/shm/close-null", test_close_null);
158 g_test_add_func ("/shm/open-and-flag", test_open_and_flag);
159 g_test_add_func ("/shm/invalid-name", test_invalid_name);
160 g_test_add_func ("/shm/flag-nonexistent", test_flag_nonexistent);
161 g_test_add_func ("/shm/out-of-space-open", test_out_of_space_open);
162 g_test_add_func ("/shm/out-of-space-flag", test_out_of_space_flag);
164 status = g_test_run ();
166 dconf_test_remove_tmpdir (temp);
167 g_free (temp);
169 return status;