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