1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
9 #include <glib/gstdio.h>
11 #include <sys/types.h>
14 static const gchar *datapath;
24 path = g_build_filename (datapath, "empty", NULL);
25 file = g_mapped_file_new (path, FALSE, &error);
27 g_assert_no_error (error);
29 g_mapped_file_ref (file);
30 g_mapped_file_unref (file);
32 g_mapped_file_unref (file);
43 path = g_build_filename (datapath, "empty", NULL);
44 file = g_mapped_file_new (path, FALSE, &error);
46 g_assert_no_error (error);
48 g_assert (g_mapped_file_get_contents (file) == NULL);
50 g_mapped_file_free (file);
60 file = g_mapped_file_new ("/dev/null", FALSE, &error);
61 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
62 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
63 g_assert (file == NULL);
69 test_nonexisting (void)
75 file = g_mapped_file_new ("no-such-file", FALSE, &error);
76 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
77 g_clear_error (&error);
78 g_assert (file == NULL);
88 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
89 const gchar *new = "abcdefghijklmnopqrstuvxyz";
93 srcpath = g_build_filename (datapath, "4096-random-bytes", NULL);
94 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
96 g_file_get_contents (srcpath, &contents, &len, &error);
97 g_assert_no_error (error);
98 g_file_set_contents (tmp_copy_path, contents, len, &error);
99 g_assert_no_error (error);
103 file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
104 g_assert_no_error (error);
106 contents = g_mapped_file_get_contents (file);
107 g_assert (strncmp (contents, old, strlen (old)) == 0);
109 memcpy (contents, new, strlen (new));
110 g_assert (strncmp (contents, new, strlen (new)) == 0);
112 g_mapped_file_free (file);
115 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
116 g_assert_no_error (error);
118 contents = g_mapped_file_get_contents (file);
119 g_assert (strncmp (contents, old, strlen (old)) == 0);
121 g_mapped_file_free (file);
124 g_free (tmp_copy_path);
128 test_writable_fd (void)
131 GError *error = NULL;
133 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
134 const gchar *new = "abcdefghijklmnopqrstuvxyz";
138 gchar *tmp_copy_path;
140 srcpath = g_build_filename (datapath, "4096-random-bytes", NULL);
141 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
143 g_file_get_contents (srcpath, &contents, &len, &error);
144 g_assert_no_error (error);
145 g_file_set_contents (tmp_copy_path, contents, len, &error);
146 g_assert_no_error (error);
150 fd = g_open (tmp_copy_path, O_RDWR, 0);
152 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
153 g_assert_no_error (error);
155 contents = g_mapped_file_get_contents (file);
156 g_assert (strncmp (contents, old, strlen (old)) == 0);
158 memcpy (contents, new, strlen (new));
159 g_assert (strncmp (contents, new, strlen (new)) == 0);
161 g_mapped_file_free (file);
165 fd = g_open (tmp_copy_path, O_RDWR, 0);
167 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
168 g_assert_no_error (error);
170 contents = g_mapped_file_get_contents (file);
171 g_assert (strncmp (contents, old, strlen (old)) == 0);
173 g_mapped_file_free (file);
176 g_free (tmp_copy_path);
188 path = g_build_filename (datapath, "empty", NULL);
189 file = g_mapped_file_new (path, FALSE, &error);
191 g_assert_no_error (error);
193 bytes = g_mapped_file_get_bytes (file);
194 g_mapped_file_unref (file);
196 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
197 g_bytes_unref (bytes);
201 main (int argc, char *argv[])
203 if (g_getenv ("G_TEST_DATA"))
204 datapath = g_getenv ("G_TEST_DATA");
208 g_test_init (&argc, &argv, NULL);
210 g_test_add_func ("/mappedfile/basic", test_basic);
211 g_test_add_func ("/mappedfile/empty", test_empty);
213 g_test_add_func ("/mappedfile/device", test_device);
215 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
216 g_test_add_func ("/mappedfile/writable", test_writable);
217 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
218 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
220 return g_test_run ();