Rename G_TEST_DISTED to G_TEST_DIST
[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 (g_test_get_filename (G_TEST_DIST, "empty", NULL), 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 (g_test_get_filename (G_TEST_DIST, "empty", NULL), 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   gchar *tmp_copy_path;
83
84   tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
85
86   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
87   g_assert_no_error (error);
88   g_file_set_contents (tmp_copy_path, contents, len, &error);
89   g_assert_no_error (error);
90   
91   g_free (contents);
92
93   file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
94   g_assert_no_error (error);
95
96   contents = g_mapped_file_get_contents (file);
97   g_assert (strncmp (contents, old, strlen (old)) == 0);
98
99   memcpy (contents, new, strlen (new));
100   g_assert (strncmp (contents, new, strlen (new)) == 0);
101
102   g_mapped_file_free (file);
103
104   error = NULL;
105   file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
106   g_assert_no_error (error);
107
108   contents = g_mapped_file_get_contents (file);
109   g_assert (strncmp (contents, old, strlen (old)) == 0);
110
111   g_mapped_file_free (file);
112
113   g_free (tmp_copy_path);
114 }
115
116 static void
117 test_writable_fd (void)
118 {
119   GMappedFile *file;
120   GError *error = NULL;
121   gchar *contents;
122   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
123   const gchar *new = "abcdefghijklmnopqrstuvxyz";
124   gsize len;
125   int fd;
126   gchar *tmp_copy_path;
127
128   tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
129
130   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
131   g_assert_no_error (error);
132   g_file_set_contents (tmp_copy_path, contents, len, &error);
133   g_assert_no_error (error);
134   
135   g_free (contents);
136
137   fd = g_open (tmp_copy_path, O_RDWR, 0);
138   g_assert (fd != -1);
139   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
140   g_assert_no_error (error);
141
142   contents = g_mapped_file_get_contents (file);
143   g_assert (strncmp (contents, old, strlen (old)) == 0);
144
145   memcpy (contents, new, strlen (new));
146   g_assert (strncmp (contents, new, strlen (new)) == 0);
147
148   g_mapped_file_free (file);
149   close (fd);
150
151   error = NULL;
152   fd = g_open (tmp_copy_path, O_RDWR, 0);
153   g_assert (fd != -1);
154   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
155   g_assert_no_error (error);
156
157   contents = g_mapped_file_get_contents (file);
158   g_assert (strncmp (contents, old, strlen (old)) == 0);
159
160   g_mapped_file_free (file);
161
162   g_free (tmp_copy_path);
163 }
164
165 static void
166 test_gbytes (void)
167 {
168   GMappedFile *file;
169   GBytes *bytes;
170   GError *error;
171
172   error = NULL;
173   file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
174   g_assert_no_error (error);
175
176   bytes = g_mapped_file_get_bytes (file);
177   g_mapped_file_unref (file);
178
179   g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
180   g_bytes_unref (bytes);
181 }
182
183 int
184 main (int argc, char *argv[])
185 {
186   g_test_init (&argc, &argv, NULL);
187
188   g_test_add_func ("/mappedfile/basic", test_basic);
189   g_test_add_func ("/mappedfile/empty", test_empty);
190 #ifdef G_OS_UNIX
191   g_test_add_func ("/mappedfile/device", test_device);
192 #endif
193   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
194   g_test_add_func ("/mappedfile/writable", test_writable);
195   g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
196   g_test_add_func ("/mappedfile/gbytes", test_gbytes);
197
198   return g_test_run ();
199 }