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