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 | test_basic (void) |
21 | { |
22 | GMappedFile *file; |
23 | GError *error; |
24 | |
25 | error = NULL; |
26 | file = g_mapped_file_new (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "empty" , NULL), FALSE, error: &error); |
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 | |
41 | error = NULL; |
42 | file = g_mapped_file_new (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "empty" , NULL), FALSE, error: &error); |
43 | g_assert_no_error (error); |
44 | |
45 | g_assert_null (g_mapped_file_get_contents (file)); |
46 | |
47 | g_mapped_file_free (file); |
48 | } |
49 | |
50 | #ifdef G_OS_UNIX |
51 | static void |
52 | test_device (void) |
53 | { |
54 | GError *error = NULL; |
55 | GMappedFile *file; |
56 | |
57 | file = g_mapped_file_new (filename: "/dev/null" , FALSE, error: &error); |
58 | 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 | g_assert_null (file); |
62 | g_error_free (error); |
63 | } |
64 | #endif |
65 | |
66 | static void |
67 | test_nonexisting (void) |
68 | { |
69 | GMappedFile *file; |
70 | GError *error; |
71 | |
72 | error = NULL; |
73 | file = g_mapped_file_new (filename: "no-such-file" , FALSE, error: &error); |
74 | g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); |
75 | g_clear_error (err: &error); |
76 | g_assert_null (file); |
77 | } |
78 | |
79 | static void |
80 | test_writable (void) |
81 | { |
82 | GMappedFile *file; |
83 | GError *error = NULL; |
84 | gchar *contents; |
85 | gsize len; |
86 | const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM" ; |
87 | const gchar *new = "abcdefghijklmnopqrstuvxyz" ; |
88 | gchar *tmp_copy_path; |
89 | |
90 | tmp_copy_path = g_build_filename (first_element: g_get_tmp_dir (), "glib-test-4096-random-bytes" , NULL); |
91 | |
92 | g_file_get_contents (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "4096-random-bytes" , NULL), contents: &contents, length: &len, error: &error); |
93 | g_assert_no_error (error); |
94 | g_file_set_contents (filename: tmp_copy_path, contents, length: len, error: &error); |
95 | g_assert_no_error (error); |
96 | |
97 | g_free (mem: contents); |
98 | |
99 | file = g_mapped_file_new (filename: tmp_copy_path, TRUE, error: &error); |
100 | g_assert_no_error (error); |
101 | |
102 | contents = g_mapped_file_get_contents (file); |
103 | g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0); |
104 | |
105 | memcpy (dest: contents, src: new, n: strlen (s: new)); |
106 | g_assert_cmpuint (strncmp (contents, new, strlen (new)), ==, 0); |
107 | |
108 | g_mapped_file_free (file); |
109 | |
110 | error = NULL; |
111 | file = g_mapped_file_new (filename: tmp_copy_path, FALSE, error: &error); |
112 | g_assert_no_error (error); |
113 | |
114 | contents = g_mapped_file_get_contents (file); |
115 | g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0); |
116 | |
117 | g_mapped_file_free (file); |
118 | |
119 | g_free (mem: tmp_copy_path); |
120 | } |
121 | |
122 | static void |
123 | test_writable_fd (void) |
124 | { |
125 | GMappedFile *file; |
126 | GError *error = NULL; |
127 | gchar *contents; |
128 | const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM" ; |
129 | const gchar *new = "abcdefghijklmnopqrstuvxyz" ; |
130 | gsize len; |
131 | int fd; |
132 | gchar *tmp_copy_path; |
133 | |
134 | tmp_copy_path = g_build_filename (first_element: g_get_tmp_dir (), "glib-test-4096-random-bytes" , NULL); |
135 | |
136 | g_file_get_contents (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "4096-random-bytes" , NULL), contents: &contents, length: &len, error: &error); |
137 | g_assert_no_error (error); |
138 | g_file_set_contents (filename: tmp_copy_path, contents, length: len, error: &error); |
139 | g_assert_no_error (error); |
140 | |
141 | g_free (mem: contents); |
142 | |
143 | fd = g_open (file: tmp_copy_path, O_RDWR, 0); |
144 | g_assert_cmpint (fd, !=, -1); |
145 | file = g_mapped_file_new_from_fd (fd, TRUE, error: &error); |
146 | g_assert_no_error (error); |
147 | |
148 | contents = g_mapped_file_get_contents (file); |
149 | g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0); |
150 | |
151 | memcpy (dest: contents, src: new, n: strlen (s: new)); |
152 | g_assert_cmpuint (strncmp (contents, new, strlen (new)), ==, 0); |
153 | |
154 | g_mapped_file_free (file); |
155 | close (fd: fd); |
156 | |
157 | error = NULL; |
158 | fd = g_open (file: tmp_copy_path, O_RDWR, 0); |
159 | g_assert_cmpint (fd, !=, -1); |
160 | file = g_mapped_file_new_from_fd (fd, TRUE, error: &error); |
161 | g_assert_no_error (error); |
162 | |
163 | contents = g_mapped_file_get_contents (file); |
164 | g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0); |
165 | |
166 | g_mapped_file_free (file); |
167 | |
168 | g_free (mem: tmp_copy_path); |
169 | } |
170 | |
171 | static void |
172 | test_gbytes (void) |
173 | { |
174 | GMappedFile *file; |
175 | GBytes *bytes; |
176 | GError *error; |
177 | |
178 | error = NULL; |
179 | file = g_mapped_file_new (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "empty" , NULL), FALSE, error: &error); |
180 | g_assert_no_error (error); |
181 | |
182 | bytes = g_mapped_file_get_bytes (file); |
183 | g_mapped_file_unref (file); |
184 | |
185 | g_assert_cmpint (g_bytes_get_size (bytes), ==, 0); |
186 | g_bytes_unref (bytes); |
187 | } |
188 | |
189 | int |
190 | main (int argc, char *argv[]) |
191 | { |
192 | g_test_init (argc: &argc, argv: &argv, NULL); |
193 | |
194 | g_test_add_func (testpath: "/mappedfile/basic" , test_func: test_basic); |
195 | g_test_add_func (testpath: "/mappedfile/empty" , test_func: test_empty); |
196 | #ifdef G_OS_UNIX |
197 | g_test_add_func (testpath: "/mappedfile/device" , test_func: test_device); |
198 | #endif |
199 | g_test_add_func (testpath: "/mappedfile/nonexisting" , test_func: test_nonexisting); |
200 | g_test_add_func (testpath: "/mappedfile/writable" , test_func: test_writable); |
201 | g_test_add_func (testpath: "/mappedfile/writable_fd" , test_func: test_writable_fd); |
202 | g_test_add_func (testpath: "/mappedfile/gbytes" , test_func: test_gbytes); |
203 | |
204 | return g_test_run (); |
205 | } |
206 | |