Debian Codegraph @

test/XpmWrite.c

raw
1 /*
2 * Copyright (c) 2023, Oracle and/or its affiliates.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "config.h"
25
26 #include <X11/xpm.h>
27
28 #include <glib.h>
29 #include <glib/gstdio.h>
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include "TestAllFiles.h"
39 #include "CompareXpmImage.h"
40
41 #ifndef O_CLOEXEC
42 # define O_CLOEXEC 0
43 #endif
44
45 #ifndef g_assert_no_errno /* defined in glib 2.66 & later */
46 #define g_assert_no_errno(n) g_assert_cmpint(n, >=, 0)
47 #endif
48
49 /* Tunables for the compression-finish wait */
50 #define XPM_STABLE_USEC (200 * 1000) /* 200 ms */
51 #define XPM_TIMEOUT_USEC (10 * G_USEC_PER_SEC) /* 10 s */
52 #define XPM_POLL_USEC (10 * 1000) /* 10 ms */
53
54 /* ------------------------------------------------------------------------- */
55 /* Wait until PATH exists and has not changed for STABLE_USEC microseconds.
56 * We watch both mtime and size because some filesystems only tick mtime
57 * at 1s resolution. Fail the test after TIMEOUT_USEC microseconds.
58 */
59 static void
60 wait_for_stable_file(const gchar *path, gint64 stable_usec, gint64 timeout_usec)
61 {
62 const gint64 poll_interval = XPM_POLL_USEC;
63 gint64 now = g_get_monotonic_time();
64 const gint64 deadline = now + timeout_usec;
65 gboolean seen = FALSE;
66 struct stat st;
67 off_t last_size = -1;
68 time_t last_mtime = 0;
69 gint64 last_change = now; /* monotonic time of last observed change */
70
71 for (;;) {
72 now = g_get_monotonic_time();
73 if (now >= deadline)
74 g_error("Timeout waiting for %s to be created and become stable", path);
75
76 if (g_stat(path, &st) == 0) {
77 if (!seen || st.st_size != last_size || st.st_mtime != last_mtime) {
78 last_size = st.st_size;
79 last_mtime = st.st_mtime;
80 last_change = now;
81 seen = TRUE;
82 }
83 if (seen && (now - last_change) >= stable_usec)
84 return; /* stable long enough */
85 }
86 g_usleep(poll_interval);
87 }
88 }
89
90 /*
91 * Check if a filename ends in ".Z" or ".gz"
92 */
93 static inline gboolean
94 is_compressed(const char *filepath)
95 {
96 const char *ext = strrchr(filepath, '.');
97
98 if ((ext != NULL) &&
99 (((ext[1] == 'Z') && (ext[2] == 0)) ||
100 ((ext[1] == 'g') && (ext[2] == 'z') && (ext[3] == 0)))) {
101 return TRUE;
102 }
103
104 return FALSE;
105 }
106
107 /*
108 * If a filename ends in ".Z" or ".gz", remove that extension to avoid
109 * confusing libXpm into applying compression when not desired.
110 */
111 static inline void
112 strip_compress_ext(char *filepath)
113 {
114 char *ext = strrchr(filepath, '.');
115
116 if ((ext != NULL) &&
117 (((ext[1] == 'Z') && (ext[2] == 0)) ||
118 ((ext[1] == 'g') && (ext[2] == 'z') && (ext[3] == 0)))) {
119 *ext = '\0';
120 }
121 }
122
123 /*
124 * XpmWriteFileFromXpmImage - Write XPM files without requiring an X Display
125 */
126 static void
127 test_WFFXI_helper(const gchar *newfilepath, XpmImage *imageA, XpmInfo *infoA)
128 {
129 XpmImage imageB;
130 XpmInfo infoB;
131 int status;
132
133 g_test_message("...writing %s", newfilepath);
134
135 status = XpmWriteFileFromXpmImage(newfilepath, imageA, infoA);
136 g_assert_cmpint(status, ==, XpmSuccess);
137
138 if (is_compressed(newfilepath)) {
139 /* OpenWriteFile() double-forks; wait for the compressor to finish
140 * and for the output file to stop changing before we read it back.
141 */
142 wait_for_stable_file(newfilepath, XPM_STABLE_USEC, XPM_TIMEOUT_USEC);
143 }
144
145 status = XpmReadFileToXpmImage(newfilepath, &imageB, &infoB);
146 g_assert_cmpint(status, ==, XpmSuccess);
147
148 CompareXpmImage(imageA, &imageB);
149 XpmFreeXpmImage(&imageB);
150 XpmFreeXpmInfo(&infoB);
151
152 status = remove(newfilepath);
153 g_assert_no_errno(status);
154
155 }
156
157 static int
158 TestWriteFileFromXpmImage(const gchar *filepath)
159 {
160 XpmImage imageA;
161 XpmInfo infoA;
162 int status;
163 gchar *testdir, *filename, *newfilepath;
164 GError *err = NULL;
165
166 #ifndef NO_ZPIPE
167 gchar *cmpfilepath;
168 #endif
169
170 status = XpmReadFileToXpmImage(filepath, &imageA, &infoA);
171 g_assert_cmpint(status, ==, XpmSuccess);
172
173 testdir = g_dir_make_tmp("XpmWrite-test-XXXXXX", &err);
174 g_assert_no_error(err);
175
176 filename = g_path_get_basename(filepath);
177 strip_compress_ext(filename);
178 newfilepath = g_build_filename(testdir, filename, NULL);
179
180 test_WFFXI_helper(newfilepath, &imageA, &infoA);
181
182 #ifndef NO_ZPIPE
183 cmpfilepath = g_strdup_printf("%s.gz", newfilepath);
184 test_WFFXI_helper(cmpfilepath, &imageA, &infoA);
185 g_free(cmpfilepath);
186
187 #ifdef XPM_PATH_COMPRESS
188 cmpfilepath = g_strdup_printf("%s.Z", newfilepath);
189 test_WFFXI_helper(cmpfilepath, &imageA, &infoA);
190 g_free(cmpfilepath);
191 #endif
192 #endif
193
194 XpmFreeXpmImage(&imageA);
195 XpmFreeXpmInfo(&infoA);
196
197 g_assert_no_errno(g_rmdir(testdir));
198
199 g_free(newfilepath);
200 g_free(filename);
201 g_free(testdir);
202
203 return status;
204 }
205
206 static void
207 test_XpmWriteFileFromXpmImage(void)
208 {
209 /* Todo: verify trying to write to an unwritable file fails */
210
211 TestAllNormalFiles("good", XpmSuccess, TestWriteFileFromXpmImage);
212 /* XpmReadFileToXpmImage supports compressed files */
213 TestAllCompressedFiles("good", XpmSuccess, TestWriteFileFromXpmImage);
214 }
215
216 /*
217 * XpmWriteFileFromData - wrapper around XpmWriteFileFromXpmImage that
218 * converts the image into a list of strings.
219 */
220 static void
221 test_WFFXD_helper(const gchar *newfilepath, char **dataA)
222 {
223 char **dataB;
224 int status;
225
226 g_test_message("...writing %s", newfilepath);
227
228 status = XpmWriteFileFromData(newfilepath, dataA);
229 g_assert_cmpint(status, ==, XpmSuccess);
230
231 if (is_compressed(newfilepath)) {
232 /* OpenWriteFile() double-forks; wait for the compressor to finish
233 * and for the output file to stop changing before we read it back.
234 */
235 wait_for_stable_file(newfilepath, XPM_STABLE_USEC, XPM_TIMEOUT_USEC);
236 }
237
238 status = XpmReadFileToData(newfilepath, &dataB);
239 g_assert_cmpint(status, ==, XpmSuccess);
240
241 /* Todo: compare data fields */
242 XpmFree(dataB);
243
244 status = remove(newfilepath);
245 g_assert_no_errno(status);
246
247 }
248
249 static int
250 TestWriteFileFromData(const gchar *filepath)
251 {
252 char **data = NULL;
253 int status;
254 gchar *testdir, *filename, *newfilepath;
255 GError *err = NULL;
256
257 #ifndef NO_ZPIPE
258 gchar *cmpfilepath;
259 #endif
260
261 status = XpmReadFileToData(filepath, &data);
262 g_assert_cmpint(status, ==, XpmSuccess);
263
264 testdir = g_dir_make_tmp("XpmWrite-test-XXXXXX", &err);
265 g_assert_no_error(err);
266
267 filename = g_path_get_basename(filepath);
268 strip_compress_ext(filename);
269 newfilepath = g_build_filename(testdir, filename, NULL);
270
271 test_WFFXD_helper(newfilepath, data);
272
273 #ifndef NO_ZPIPE
274 cmpfilepath = g_strdup_printf("%s.gz", newfilepath);
275 test_WFFXD_helper(cmpfilepath, data);
276 g_free(cmpfilepath);
277
278 #ifdef XPM_PATH_COMPRESS
279 cmpfilepath = g_strdup_printf("%s.Z", newfilepath);
280 test_WFFXD_helper(cmpfilepath, data);
281 g_free(cmpfilepath);
282 #endif
283 #endif
284
285 XpmFree(data);
286
287 g_assert_no_errno(g_rmdir(testdir));
288
289 g_free(newfilepath);
290 g_free(filename);
291 g_free(testdir);
292
293 return status;
294 }
295
296 static void
297 test_XpmWriteFileFromData(void)
298 {
299 /* Todo - verify trying to write to an unwritable file fails */
300
301 TestAllNormalFiles("good", XpmSuccess, TestWriteFileFromData);
302 /* XpmReadFileToData calls XpmReadFileToXpmImage so it
303 supports compressed files */
304 TestAllCompressedFiles("good", XpmSuccess, TestWriteFileFromData);
305 }
306
307 /*
308 * XpmWriteFileFromBuffer - helper function to write files & read them back in
309 * XpmWriteFileFromBuffer() does not support compressed files.
310 */
311 static int
312 TestWriteFileFromBuffer(const gchar *filepath)
313 {
314 char *buffer = NULL;
315 gchar *testdir, *filename, *newfilepath;
316 GError *err = NULL;
317 int status;
318
319 status = XpmReadFileToBuffer(filepath, &buffer);
320 g_assert_cmpint(status, ==, XpmSuccess);
321 g_assert_nonnull(buffer);
322
323 testdir = g_dir_make_tmp("XpmWrite-test-XXXXXX", &err);
324 g_assert_no_error(err);
325
326 filename = g_path_get_basename(filepath);
327 strip_compress_ext(filename);
328 newfilepath = g_build_filename(testdir, filename, NULL);
329 g_test_message("...writing %s", newfilepath);
330
331 status = XpmWriteFileFromBuffer(newfilepath, buffer);
332 g_assert_cmpint(status, ==, XpmSuccess);
333
334 if (status == XpmSuccess) {
335 char readbuf[8192];
336 char *b = buffer;
337 int fd;
338 ssize_t rd;
339
340 /* Read file ourselves and verify the data matches */
341 g_assert_no_errno(fd = open(newfilepath, O_RDONLY | O_CLOEXEC));
342 while ((rd = read(fd, readbuf, sizeof(readbuf))) > 0) {
343 g_assert_cmpmem(b, rd, readbuf, rd);
344 b += rd;
345 }
346 /* Verify we're at the end of the buffer */
347 g_assert_cmpint(b[0], ==, '\0');
348
349 g_assert_no_errno(close(fd));
350 g_assert_no_errno(remove(newfilepath));
351 }
352 XpmFree(buffer);
353
354 g_assert_no_errno(g_rmdir(testdir));
355
356 g_free(newfilepath);
357 g_free(filename);
358 g_free(testdir);
359
360 return status;
361 }
362
363 static void
364 test_XpmWriteFileFromBuffer(void)
365 {
366 /* Todo: verify trying to write to an unwritable file fails */
367
368 TestAllNormalFiles("good", XpmSuccess, TestWriteFileFromBuffer);
369 /* XpmReadFileToBuffer does not support compressed files */
370 }
371
372 int
373 main(int argc, char** argv)
374 {
375 g_test_init(&argc, &argv, NULL);
376 g_test_bug_base(PACKAGE_BUGREPORT);
377
378
379 g_test_add_func("/XpmRead/XpmWriteFileFromXpmImage",
380 test_XpmWriteFileFromXpmImage);
381 g_test_add_func("/XpmRead/XpmWriteFileFromData",
382 test_XpmWriteFileFromData);
383 g_test_add_func("/XpmRead/XpmWriteFileFromBuffer",
384 test_XpmWriteFileFromBuffer);
385
386 return g_test_run();
387 }