Replace #ifdef HAVE_UNISTD_H checks with #ifdef G_OS_UNIX
[platform/upstream/glib.git] / glib / tests / mappedfile.c
1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
2
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>
9
10 #ifdef G_OS_UNIX
11 #include <unistd.h>
12 #endif
13 #ifdef G_OS_WIN32
14 #include <io.h>
15 #endif
16
17 static void
18 test_basic (void)
19 {
20   GMappedFile *file;
21   GError *error;
22
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);
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
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);
42
43   g_assert (g_mapped_file_get_contents (file) == NULL);
44
45   g_mapped_file_free (file);
46 }
47
48 #ifdef G_OS_UNIX
49 static void
50 test_device (void)
51 {
52   GError *error = NULL;
53   GMappedFile *file;
54
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_NOMEM));
58   g_assert (file == NULL);
59   g_error_free (error);
60 }
61 #endif
62
63 static void
64 test_nonexisting (void)
65 {
66   GMappedFile *file;
67   GError *error;
68
69   error = NULL;
70   file = g_mapped_file_new ("no-such-file", FALSE, &error);
71   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
72   g_clear_error (&error);
73   g_assert (file == NULL);
74 }
75
76 static void
77 test_writable (void)
78 {
79   GMappedFile *file;
80   GError *error = NULL;
81   gchar *contents;
82   gsize len;
83   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
84   const gchar *new = "abcdefghijklmnopqrstuvxyz";
85   gchar *tmp_copy_path;
86
87   tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
88
89   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
90   g_assert_no_error (error);
91   g_file_set_contents (tmp_copy_path, contents, len, &error);
92   g_assert_no_error (error);
93   
94   g_free (contents);
95
96   file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
97   g_assert_no_error (error);
98
99   contents = g_mapped_file_get_contents (file);
100   g_assert (strncmp (contents, old, strlen (old)) == 0);
101
102   memcpy (contents, new, strlen (new));
103   g_assert (strncmp (contents, new, strlen (new)) == 0);
104
105   g_mapped_file_free (file);
106
107   error = NULL;
108   file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
109   g_assert_no_error (error);
110
111   contents = g_mapped_file_get_contents (file);
112   g_assert (strncmp (contents, old, strlen (old)) == 0);
113
114   g_mapped_file_free (file);
115
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   gchar *tmp_copy_path;
130
131   tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
132
133   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
134   g_assert_no_error (error);
135   g_file_set_contents (tmp_copy_path, contents, len, &error);
136   g_assert_no_error (error);
137   
138   g_free (contents);
139
140   fd = g_open (tmp_copy_path, O_RDWR, 0);
141   g_assert (fd != -1);
142   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
143   g_assert_no_error (error);
144
145   contents = g_mapped_file_get_contents (file);
146   g_assert (strncmp (contents, old, strlen (old)) == 0);
147
148   memcpy (contents, new, strlen (new));
149   g_assert (strncmp (contents, new, strlen (new)) == 0);
150
151   g_mapped_file_free (file);
152   close (fd);
153
154   error = NULL;
155   fd = g_open (tmp_copy_path, O_RDWR, 0);
156   g_assert (fd != -1);
157   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
158   g_assert_no_error (error);
159
160   contents = g_mapped_file_get_contents (file);
161   g_assert (strncmp (contents, old, strlen (old)) == 0);
162
163   g_mapped_file_free (file);
164
165   g_free (tmp_copy_path);
166 }
167
168 static void
169 test_gbytes (void)
170 {
171   GMappedFile *file;
172   GBytes *bytes;
173   GError *error;
174
175   error = NULL;
176   file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
177   g_assert_no_error (error);
178
179   bytes = g_mapped_file_get_bytes (file);
180   g_mapped_file_unref (file);
181
182   g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
183   g_bytes_unref (bytes);
184 }
185
186 int
187 main (int argc, char *argv[])
188 {
189   g_test_init (&argc, &argv, NULL);
190
191   g_test_add_func ("/mappedfile/basic", test_basic);
192   g_test_add_func ("/mappedfile/empty", test_empty);
193 #ifdef G_OS_UNIX
194   g_test_add_func ("/mappedfile/device", test_device);
195 #endif
196   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
197   g_test_add_func ("/mappedfile/writable", test_writable);
198   g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
199   g_test_add_func ("/mappedfile/gbytes", test_gbytes);
200
201   return g_test_run ();
202 }