tests: Stop using deprecated g_test_trap_fork() API
[dconf.git] / tests / shm.c
bloba0cf67ea7ee71533619304b606b22674b1f332d8
1 #define _GNU_SOURCE
3 #include "../common/dconf-paths.h"
4 #include <glib/gstdio.h>
5 #include <sys/stat.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <dlfcn.h>
12 #include "../shm/dconf-shm.h"
13 #include "tmpdir.h"
15 static void
16 test_mkdir_fail (void)
18 guint8 *shm;
20 if (g_test_subprocess ())
22 gchar *evil;
23 gint fd;
25 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
27 evil = g_build_filename (g_get_user_runtime_dir (), "dconf", NULL);
28 fd = open (evil, O_WRONLY | O_CREAT, 0600);
29 close (fd);
31 shm = dconf_shm_open ("foo");
32 g_assert (shm == NULL);
34 g_unlink (evil);
35 g_free (evil);
37 return;
40 g_test_trap_subprocess (NULL, 0, 0);
41 g_test_trap_assert_passed ();
42 g_test_trap_assert_stderr ("*unable to create directory*");
45 static void
46 test_close_null (void)
48 dconf_shm_close (NULL);
51 static void
52 test_open_and_flag (void)
54 guint8 *shm;
56 shm = dconf_shm_open ("foo");
57 g_assert (shm != NULL);
58 g_assert (!dconf_shm_is_flagged (shm));
59 dconf_shm_flag ("foo");
60 g_assert (dconf_shm_is_flagged (shm));
61 dconf_shm_close (shm);
64 static void
65 test_invalid_name (void)
67 if (g_test_subprocess ())
69 guint8 *shm;
71 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
73 shm = dconf_shm_open ("foo/bar");
74 g_assert (shm == NULL);
75 g_assert (dconf_shm_is_flagged (shm));
76 return;
79 g_test_trap_subprocess (NULL, 0, 0);
80 g_test_trap_assert_passed ();
81 g_test_trap_assert_stderr ("*unable to create*foo/bar*");
84 static void
85 test_flag_nonexistent (void)
87 dconf_shm_flag ("does-not-exist");
90 static gboolean should_fail_pwrite;
91 /* interpose */
92 ssize_t
93 pwrite (int fd, const void *buf, size_t count, off_t offset)
95 static ssize_t (* real_pwrite) (int, const void *, size_t, off_t);
97 if (!real_pwrite)
98 real_pwrite = dlsym (RTLD_NEXT, "pwrite");
100 if (should_fail_pwrite)
102 errno = ENOSPC;
103 return -1;
106 return (* real_pwrite) (fd, buf, count, offset);
109 static void
110 test_out_of_space_open (void)
112 if (g_test_subprocess ())
114 guint8 *shm;
116 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
117 should_fail_pwrite = TRUE;
119 shm = dconf_shm_open ("foo");
120 g_assert (shm == NULL);
121 g_assert (dconf_shm_is_flagged (shm));
122 return;
125 g_test_trap_subprocess (NULL, 0, 0);
126 g_test_trap_assert_passed ();
127 g_test_trap_assert_stderr ("*failed to allocate*foo*");
130 static void
131 test_out_of_space_flag (void)
133 if (g_test_subprocess ())
135 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
136 should_fail_pwrite = TRUE;
138 dconf_shm_flag ("foo");
139 return;
142 g_test_trap_subprocess (NULL, 0, 0);
143 g_test_trap_assert_passed ();
147 main (int argc, char **argv)
149 gchar *temp;
150 gint status;
152 temp = dconf_test_create_tmpdir ();
154 g_setenv ("XDG_RUNTIME_DIR", temp, TRUE);
155 /* This currently works, but it is possible that one day GLib will
156 * read the XDG_RUNTIME_DIR variable (and cache its value) as a
157 * side-effect of the dconf_test_create_tmpdir() call above.
159 * This assert will quickly uncover the problem in that case...
161 g_assert_cmpstr (g_get_user_runtime_dir (), ==, temp);
163 g_test_init (&argc, &argv, NULL);
165 g_test_add_func ("/shm/mkdir-fail", test_mkdir_fail);
166 g_test_add_func ("/shm/close-null", test_close_null);
167 g_test_add_func ("/shm/open-and-flag", test_open_and_flag);
168 g_test_add_func ("/shm/invalid-name", test_invalid_name);
169 g_test_add_func ("/shm/flag-nonexistent", test_flag_nonexistent);
170 g_test_add_func ("/shm/out-of-space-open", test_out_of_space_open);
171 g_test_add_func ("/shm/out-of-space-flag", test_out_of_space_flag);
173 status = g_test_run ();
175 dconf_test_remove_tmpdir (temp);
176 g_free (temp);
178 return status;