1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
9 #include <glib/gstdio.h>
11 #include <sys/types.h>
21 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
22 g_assert_no_error (error);
24 g_mapped_file_ref (file);
25 g_mapped_file_unref (file);
27 g_mapped_file_unref (file);
37 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
38 g_assert_no_error (error);
40 g_assert (g_mapped_file_get_contents (file) == NULL);
42 g_mapped_file_free (file);
52 file = g_mapped_file_new ("/dev/null", FALSE, &error);
53 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
54 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
55 g_assert (file == NULL);
61 test_nonexisting (void)
67 file = g_mapped_file_new ("no-such-file", FALSE, &error);
68 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
69 g_clear_error (&error);
70 g_assert (file == NULL);
80 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
81 const gchar *new = "abcdefghijklmnopqrstuvxyz";
85 srcpath = g_build_filename (SRCDIR, "4096-random-bytes", NULL);
86 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
88 g_file_get_contents (srcpath, &contents, &len, &error);
89 g_assert_no_error (error);
90 g_file_set_contents (tmp_copy_path, contents, len, &error);
91 g_assert_no_error (error);
95 file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
96 g_assert_no_error (error);
98 contents = g_mapped_file_get_contents (file);
99 g_assert (strncmp (contents, old, strlen (old)) == 0);
101 memcpy (contents, new, strlen (new));
102 g_assert (strncmp (contents, new, strlen (new)) == 0);
104 g_mapped_file_free (file);
107 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
108 g_assert_no_error (error);
110 contents = g_mapped_file_get_contents (file);
111 g_assert (strncmp (contents, old, strlen (old)) == 0);
113 g_mapped_file_free (file);
116 g_free (tmp_copy_path);
120 test_writable_fd (void)
123 GError *error = NULL;
125 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
126 const gchar *new = "abcdefghijklmnopqrstuvxyz";
130 gchar *tmp_copy_path;
132 srcpath = g_build_filename (SRCDIR, "4096-random-bytes", NULL);
133 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
135 g_file_get_contents (srcpath, &contents, &len, &error);
136 g_assert_no_error (error);
137 g_file_set_contents (tmp_copy_path, contents, len, &error);
138 g_assert_no_error (error);
142 fd = g_open (tmp_copy_path, O_RDWR, 0);
144 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
145 g_assert_no_error (error);
147 contents = g_mapped_file_get_contents (file);
148 g_assert (strncmp (contents, old, strlen (old)) == 0);
150 memcpy (contents, new, strlen (new));
151 g_assert (strncmp (contents, new, strlen (new)) == 0);
153 g_mapped_file_free (file);
157 fd = g_open (tmp_copy_path, O_RDWR, 0);
159 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
160 g_assert_no_error (error);
162 contents = g_mapped_file_get_contents (file);
163 g_assert (strncmp (contents, old, strlen (old)) == 0);
165 g_mapped_file_free (file);
168 g_free (tmp_copy_path);
179 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
180 g_assert_no_error (error);
182 bytes = g_mapped_file_get_bytes (file);
183 g_mapped_file_unref (file);
185 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
186 g_bytes_unref (bytes);
190 main (int argc, char *argv[])
192 g_test_init (&argc, &argv, NULL);
194 g_test_add_func ("/mappedfile/basic", test_basic);
195 g_test_add_func ("/mappedfile/empty", test_empty);
197 g_test_add_func ("/mappedfile/device", test_device);
199 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
200 g_test_add_func ("/mappedfile/writable", test_writable);
201 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
202 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
204 return g_test_run ();