1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
10 #include <sys/types.h>
20 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
21 g_assert_no_error (error);
23 g_mapped_file_ref (file);
24 g_mapped_file_unref (file);
26 g_mapped_file_unref (file);
36 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
37 g_assert_no_error (error);
39 g_assert (g_mapped_file_get_contents (file) == NULL);
41 g_mapped_file_free (file);
50 file = g_mapped_file_new ("/dev/null", FALSE, &error);
51 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
52 g_assert (file == NULL);
57 test_nonexisting (void)
63 file = g_mapped_file_new ("no-such-file", FALSE, &error);
64 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
65 g_clear_error (&error);
66 g_assert (file == NULL);
75 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
76 const gchar *new = "abcdefghijklmnopqrstuvxyz";
78 if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
80 g_test_message ("Skipping writable mapping test");
85 file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
86 g_assert_no_error (error);
88 contents = g_mapped_file_get_contents (file);
89 g_assert (strncmp (contents, old, strlen (old)) == 0);
91 memcpy (contents, new, strlen (new));
92 g_assert (strncmp (contents, new, strlen (new)) == 0);
94 g_mapped_file_free (file);
97 file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
98 g_assert_no_error (error);
100 contents = g_mapped_file_get_contents (file);
101 g_assert (strncmp (contents, old, strlen (old)) == 0);
103 g_mapped_file_free (file);
107 test_writable_fd (void)
112 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
113 const gchar *new = "abcdefghijklmnopqrstuvxyz";
116 if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
118 g_test_message ("Skipping writable mapping test");
123 fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
125 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
126 g_assert_no_error (error);
128 contents = g_mapped_file_get_contents (file);
129 g_assert (strncmp (contents, old, strlen (old)) == 0);
131 memcpy (contents, new, strlen (new));
132 g_assert (strncmp (contents, new, strlen (new)) == 0);
134 g_mapped_file_free (file);
138 fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
140 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
141 g_assert_no_error (error);
143 contents = g_mapped_file_get_contents (file);
144 g_assert (strncmp (contents, old, strlen (old)) == 0);
146 g_mapped_file_free (file);
151 main (int argc, char *argv[])
153 g_test_init (&argc, &argv, NULL);
155 g_test_add_func ("/mappedfile/basic", test_basic);
156 g_test_add_func ("/mappedfile/empty", test_empty);
157 g_test_add_func ("/mappedfile/device", test_device);
158 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
159 g_test_add_func ("/mappedfile/writable", test_writable);
160 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
162 return g_test_run ();