18 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
19 g_assert_no_error (error);
21 g_mapped_file_ref (file);
22 g_mapped_file_unref (file);
24 g_mapped_file_unref (file);
34 file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
35 g_assert_no_error (error);
37 g_assert (g_mapped_file_get_contents (file) == NULL);
39 g_mapped_file_free (file);
48 file = g_mapped_file_new ("/dev/null", FALSE, &error);
49 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
50 g_assert (file == NULL);
55 test_nonexisting (void)
61 file = g_mapped_file_new ("no-such-file", FALSE, &error);
62 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
63 g_clear_error (&error);
64 g_assert (file == NULL);
73 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
74 const gchar *new = "abcdefghijklmnopqrstuvxyz";
76 if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
78 g_test_message ("Skipping writable mapping test");
83 file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
84 g_assert_no_error (error);
86 contents = g_mapped_file_get_contents (file);
87 g_assert (strncmp (contents, old, strlen (old)) == 0);
89 memcpy (contents, new, strlen (new));
90 g_assert (strncmp (contents, new, strlen (new)) == 0);
92 g_mapped_file_free (file);
95 file = g_mapped_file_new (SRCDIR "/4096-random-bytes", 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 g_mapped_file_free (file);
105 test_writable_fd (void)
110 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
111 const gchar *new = "abcdefghijklmnopqrstuvxyz";
114 if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
116 g_test_message ("Skipping writable mapping test");
121 fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
123 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
124 g_assert_no_error (error);
126 contents = g_mapped_file_get_contents (file);
127 g_assert (strncmp (contents, old, strlen (old)) == 0);
129 memcpy (contents, new, strlen (new));
130 g_assert (strncmp (contents, new, strlen (new)) == 0);
132 g_mapped_file_free (file);
136 fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
138 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
139 g_assert_no_error (error);
141 contents = g_mapped_file_get_contents (file);
142 g_assert (strncmp (contents, old, strlen (old)) == 0);
144 g_mapped_file_free (file);
149 main (int argc, char *argv[])
151 g_test_init (&argc, &argv, NULL);
153 g_test_add_func ("/mappedfile/basic", test_basic);
154 g_test_add_func ("/mappedfile/empty", test_empty);
155 g_test_add_func ("/mappedfile/device", test_device);
156 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
157 g_test_add_func ("/mappedfile/writable", test_writable);
158 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
160 return g_test_run ();