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);
51 file = g_mapped_file_new ("/dev/null", FALSE, &error);
52 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
53 g_assert (file == NULL);
58 test_nonexisting (void)
64 file = g_mapped_file_new ("no-such-file", FALSE, &error);
65 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
66 g_clear_error (&error);
67 g_assert (file == NULL);
76 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
77 const gchar *new = "abcdefghijklmnopqrstuvxyz";
79 if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
81 g_test_message ("Skipping writable mapping test");
86 file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
87 g_assert_no_error (error);
89 contents = g_mapped_file_get_contents (file);
90 g_assert (strncmp (contents, old, strlen (old)) == 0);
92 memcpy (contents, new, strlen (new));
93 g_assert (strncmp (contents, new, strlen (new)) == 0);
95 g_mapped_file_free (file);
98 file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
99 g_assert_no_error (error);
101 contents = g_mapped_file_get_contents (file);
102 g_assert (strncmp (contents, old, strlen (old)) == 0);
104 g_mapped_file_free (file);
108 test_writable_fd (void)
113 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
114 const gchar *new = "abcdefghijklmnopqrstuvxyz";
117 if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
119 g_test_message ("Skipping writable mapping test");
124 fd = g_open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
126 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
127 g_assert_no_error (error);
129 contents = g_mapped_file_get_contents (file);
130 g_assert (strncmp (contents, old, strlen (old)) == 0);
132 memcpy (contents, new, strlen (new));
133 g_assert (strncmp (contents, new, strlen (new)) == 0);
135 g_mapped_file_free (file);
139 fd = g_open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
141 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
142 g_assert_no_error (error);
144 contents = g_mapped_file_get_contents (file);
145 g_assert (strncmp (contents, old, strlen (old)) == 0);
147 g_mapped_file_free (file);
159 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
160 g_assert_no_error (error);
162 bytes = g_mapped_file_get_bytes (file);
163 g_mapped_file_unref (file);
165 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
166 g_bytes_unref (bytes);
170 main (int argc, char *argv[])
172 g_test_init (&argc, &argv, NULL);
174 g_test_add_func ("/mappedfile/basic", test_basic);
175 g_test_add_func ("/mappedfile/empty", test_empty);
176 g_test_add_func ("/mappedfile/device", test_device);
177 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
178 g_test_add_func ("/mappedfile/writable", test_writable);
179 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
180 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
182 return g_test_run ();