Increase the timeout for some GLib tests
[glib.git] / glib / tests / mappedfile.c
blob828a4f38a44d098ab2b56560c64d36eda4000b99
1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
3 #include <glib.h>
4 #include <string.h>
5 #include <glib/gstdio.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
10 #ifdef G_OS_UNIX
11 #include <unistd.h>
12 #endif
13 #ifdef G_OS_WIN32
14 #include <io.h>
15 #endif
17 static void
18 test_basic (void)
20 GMappedFile *file;
21 GError *error;
23 error = NULL;
24 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), 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);
33 static void
34 test_empty (void)
36 GMappedFile *file;
37 GError *error;
39 error = NULL;
40 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
41 g_assert_no_error (error);
43 g_assert (g_mapped_file_get_contents (file) == NULL);
45 g_mapped_file_free (file);
48 #ifdef G_OS_UNIX
49 static void
50 test_device (void)
52 GError *error = NULL;
53 GMappedFile *file;
55 file = g_mapped_file_new ("/dev/null", FALSE, &error);
56 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
57 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NODEV) ||
58 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
59 g_assert (file == NULL);
60 g_error_free (error);
62 #endif
64 static void
65 test_nonexisting (void)
67 GMappedFile *file;
68 GError *error;
70 error = NULL;
71 file = g_mapped_file_new ("no-such-file", FALSE, &error);
72 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
73 g_clear_error (&error);
74 g_assert (file == NULL);
77 static void
78 test_writable (void)
80 GMappedFile *file;
81 GError *error = NULL;
82 gchar *contents;
83 gsize len;
84 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
85 const gchar *new = "abcdefghijklmnopqrstuvxyz";
86 gchar *tmp_copy_path;
88 tmp_copy_path = g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL);
90 g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
91 g_assert_no_error (error);
92 g_file_set_contents (tmp_copy_path, contents, len, &error);
93 g_assert_no_error (error);
95 g_free (contents);
97 file = g_mapped_file_new (tmp_copy_path, 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 memcpy (contents, new, strlen (new));
104 g_assert (strncmp (contents, new, strlen (new)) == 0);
106 g_mapped_file_free (file);
108 error = NULL;
109 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
110 g_assert_no_error (error);
112 contents = g_mapped_file_get_contents (file);
113 g_assert (strncmp (contents, old, strlen (old)) == 0);
115 g_mapped_file_free (file);
117 g_free (tmp_copy_path);
120 static void
121 test_writable_fd (void)
123 GMappedFile *file;
124 GError *error = NULL;
125 gchar *contents;
126 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
127 const gchar *new = "abcdefghijklmnopqrstuvxyz";
128 gsize len;
129 int fd;
130 gchar *tmp_copy_path;
132 tmp_copy_path = g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL);
134 g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
135 g_assert_no_error (error);
136 g_file_set_contents (tmp_copy_path, contents, len, &error);
137 g_assert_no_error (error);
139 g_free (contents);
141 fd = g_open (tmp_copy_path, O_RDWR, 0);
142 g_assert (fd != -1);
143 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
144 g_assert_no_error (error);
146 contents = g_mapped_file_get_contents (file);
147 g_assert (strncmp (contents, old, strlen (old)) == 0);
149 memcpy (contents, new, strlen (new));
150 g_assert (strncmp (contents, new, strlen (new)) == 0);
152 g_mapped_file_free (file);
153 close (fd);
155 error = NULL;
156 fd = g_open (tmp_copy_path, O_RDWR, 0);
157 g_assert (fd != -1);
158 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
159 g_assert_no_error (error);
161 contents = g_mapped_file_get_contents (file);
162 g_assert (strncmp (contents, old, strlen (old)) == 0);
164 g_mapped_file_free (file);
166 g_free (tmp_copy_path);
169 static void
170 test_gbytes (void)
172 GMappedFile *file;
173 GBytes *bytes;
174 GError *error;
176 error = NULL;
177 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
178 g_assert_no_error (error);
180 bytes = g_mapped_file_get_bytes (file);
181 g_mapped_file_unref (file);
183 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
184 g_bytes_unref (bytes);
188 main (int argc, char *argv[])
190 g_test_init (&argc, &argv, NULL);
192 g_test_add_func ("/mappedfile/basic", test_basic);
193 g_test_add_func ("/mappedfile/empty", test_empty);
194 #ifdef G_OS_UNIX
195 g_test_add_func ("/mappedfile/device", test_device);
196 #endif
197 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
198 g_test_add_func ("/mappedfile/writable", test_writable);
199 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
200 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
202 return g_test_run ();