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