1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
9 #include <glib/gstdio.h>
11 #include <sys/types.h>
22 path = g_test_build_filename (G_TEST_DISTED, "empty", NULL);
23 file = g_mapped_file_new (path, FALSE, &error);
25 g_assert_no_error (error);
27 g_mapped_file_ref (file);
28 g_mapped_file_unref (file);
30 g_mapped_file_unref (file);
41 path = g_test_build_filename (G_TEST_DISTED, "empty", NULL);
42 file = g_mapped_file_new (path, FALSE, &error);
44 g_assert_no_error (error);
46 g_assert (g_mapped_file_get_contents (file) == NULL);
48 g_mapped_file_free (file);
58 file = g_mapped_file_new ("/dev/null", FALSE, &error);
59 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
60 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
61 g_assert (file == NULL);
67 test_nonexisting (void)
73 file = g_mapped_file_new ("no-such-file", FALSE, &error);
74 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
75 g_clear_error (&error);
76 g_assert (file == NULL);
86 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
87 const gchar *new = "abcdefghijklmnopqrstuvxyz";
91 srcpath = g_test_build_filename (G_TEST_DISTED, "4096-random-bytes", NULL);
92 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
94 g_file_get_contents (srcpath, &contents, &len, &error);
95 g_assert_no_error (error);
96 g_file_set_contents (tmp_copy_path, contents, len, &error);
97 g_assert_no_error (error);
101 file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
102 g_assert_no_error (error);
104 contents = g_mapped_file_get_contents (file);
105 g_assert (strncmp (contents, old, strlen (old)) == 0);
107 memcpy (contents, new, strlen (new));
108 g_assert (strncmp (contents, new, strlen (new)) == 0);
110 g_mapped_file_free (file);
113 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
114 g_assert_no_error (error);
116 contents = g_mapped_file_get_contents (file);
117 g_assert (strncmp (contents, old, strlen (old)) == 0);
119 g_mapped_file_free (file);
122 g_free (tmp_copy_path);
126 test_writable_fd (void)
129 GError *error = NULL;
131 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
132 const gchar *new = "abcdefghijklmnopqrstuvxyz";
136 gchar *tmp_copy_path;
138 srcpath = g_test_build_filename (G_TEST_DISTED, "4096-random-bytes", NULL);
139 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
141 g_file_get_contents (srcpath, &contents, &len, &error);
142 g_assert_no_error (error);
143 g_file_set_contents (tmp_copy_path, contents, len, &error);
144 g_assert_no_error (error);
148 fd = g_open (tmp_copy_path, O_RDWR, 0);
150 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
151 g_assert_no_error (error);
153 contents = g_mapped_file_get_contents (file);
154 g_assert (strncmp (contents, old, strlen (old)) == 0);
156 memcpy (contents, new, strlen (new));
157 g_assert (strncmp (contents, new, strlen (new)) == 0);
159 g_mapped_file_free (file);
163 fd = g_open (tmp_copy_path, O_RDWR, 0);
165 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
166 g_assert_no_error (error);
168 contents = g_mapped_file_get_contents (file);
169 g_assert (strncmp (contents, old, strlen (old)) == 0);
171 g_mapped_file_free (file);
174 g_free (tmp_copy_path);
186 path = g_test_build_filename (G_TEST_DISTED, "empty", NULL);
187 file = g_mapped_file_new (path, FALSE, &error);
189 g_assert_no_error (error);
191 bytes = g_mapped_file_get_bytes (file);
192 g_mapped_file_unref (file);
194 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
195 g_bytes_unref (bytes);
199 main (int argc, char *argv[])
201 g_test_init (&argc, &argv, NULL);
203 g_test_add_func ("/mappedfile/basic", test_basic);
204 g_test_add_func ("/mappedfile/empty", test_empty);
206 g_test_add_func ("/mappedfile/device", test_device);
208 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
209 g_test_add_func ("/mappedfile/writable", test_writable);
210 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
211 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
213 return g_test_run ();