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