glib/tests/mappedfile: Copy test file before writing to it
[platform/upstream/glib.git] / glib / tests / mappedfile.c
1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
2
3 #include <config.h>
4 #include <glib.h>
5 #include <string.h>
6 #ifdef HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9 #include <glib/gstdio.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13
14 static void
15 test_basic (void)
16 {
17   GMappedFile *file;
18   GError *error;
19
20   error = NULL;
21   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
22   g_assert_no_error (error);
23
24   g_mapped_file_ref (file);
25   g_mapped_file_unref (file);
26
27   g_mapped_file_unref (file);
28 }
29
30 static void
31 test_empty (void)
32 {
33   GMappedFile *file;
34   GError *error;
35
36   error = NULL;
37   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
38   g_assert_no_error (error);
39
40   g_assert (g_mapped_file_get_contents (file) == NULL);
41
42   g_mapped_file_free (file);
43 }
44
45 #ifdef G_OS_UNIX
46 static void
47 test_device (void)
48 {
49   GError *error = NULL;
50   GMappedFile *file;
51
52   file = g_mapped_file_new ("/dev/null", FALSE, &error);
53   g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
54             g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
55   g_assert (file == NULL);
56   g_error_free (error);
57 }
58 #endif
59
60 static void
61 test_nonexisting (void)
62 {
63   GMappedFile *file;
64   GError *error;
65
66   error = NULL;
67   file = g_mapped_file_new ("no-such-file", FALSE, &error);
68   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
69   g_clear_error (&error);
70   g_assert (file == NULL);
71 }
72
73 static void
74 test_writable (void)
75 {
76   GMappedFile *file;
77   GError *error = NULL;
78   gchar *contents;
79   gsize len;
80   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
81   const gchar *new = "abcdefghijklmnopqrstuvxyz";
82   char *srcpath;
83   gchar *tmp_copy_path;
84
85   srcpath = g_build_filename (SRCDIR, "4096-random-bytes", NULL);
86   tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
87
88   g_file_get_contents (srcpath, &contents, &len, &error);
89   g_assert_no_error (error);
90   g_file_set_contents (tmp_copy_path, contents, len, &error);
91   g_assert_no_error (error);
92   
93   g_free (contents);
94
95   file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
96   g_assert_no_error (error);
97
98   contents = g_mapped_file_get_contents (file);
99   g_assert (strncmp (contents, old, strlen (old)) == 0);
100
101   memcpy (contents, new, strlen (new));
102   g_assert (strncmp (contents, new, strlen (new)) == 0);
103
104   g_mapped_file_free (file);
105
106   error = NULL;
107   file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
108   g_assert_no_error (error);
109
110   contents = g_mapped_file_get_contents (file);
111   g_assert (strncmp (contents, old, strlen (old)) == 0);
112
113   g_mapped_file_free (file);
114
115   g_free (srcpath);
116   g_free (tmp_copy_path);
117 }
118
119 static void
120 test_writable_fd (void)
121 {
122   GMappedFile *file;
123   GError *error = NULL;
124   gchar *contents;
125   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
126   const gchar *new = "abcdefghijklmnopqrstuvxyz";
127   gsize len;
128   int fd;
129   char *srcpath;
130   gchar *tmp_copy_path;
131
132   srcpath = g_build_filename (SRCDIR, "4096-random-bytes", NULL);
133   tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
134
135   g_file_get_contents (srcpath, &contents, &len, &error);
136   g_assert_no_error (error);
137   g_file_set_contents (tmp_copy_path, contents, len, &error);
138   g_assert_no_error (error);
139   
140   g_free (contents);
141
142   fd = g_open (tmp_copy_path, O_RDWR, 0);
143   g_assert (fd != -1);
144   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
145   g_assert_no_error (error);
146
147   contents = g_mapped_file_get_contents (file);
148   g_assert (strncmp (contents, old, strlen (old)) == 0);
149
150   memcpy (contents, new, strlen (new));
151   g_assert (strncmp (contents, new, strlen (new)) == 0);
152
153   g_mapped_file_free (file);
154   close (fd);
155
156   error = NULL;
157   fd = g_open (tmp_copy_path, O_RDWR, 0);
158   g_assert (fd != -1);
159   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
160   g_assert_no_error (error);
161
162   contents = g_mapped_file_get_contents (file);
163   g_assert (strncmp (contents, old, strlen (old)) == 0);
164
165   g_mapped_file_free (file);
166
167   g_free (srcpath);
168   g_free (tmp_copy_path);
169 }
170
171 static void
172 test_gbytes (void)
173 {
174   GMappedFile *file;
175   GBytes *bytes;
176   GError *error;
177
178   error = NULL;
179   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
180   g_assert_no_error (error);
181
182   bytes = g_mapped_file_get_bytes (file);
183   g_mapped_file_unref (file);
184
185   g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
186   g_bytes_unref (bytes);
187 }
188
189 int
190 main (int argc, char *argv[])
191 {
192   g_test_init (&argc, &argv, NULL);
193
194   g_test_add_func ("/mappedfile/basic", test_basic);
195   g_test_add_func ("/mappedfile/empty", test_empty);
196 #ifdef G_OS_UNIX
197   g_test_add_func ("/mappedfile/device", test_device);
198 #endif
199   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
200   g_test_add_func ("/mappedfile/writable", test_writable);
201   g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
202   g_test_add_func ("/mappedfile/gbytes", test_gbytes);
203
204   return g_test_run ();
205 }