1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2014 Michihiro NAKAJIMA
5 * All rights reserved.
6 */
7 #include "test.h"
8
DEFINE_TEST(test_option_lz4)9 DEFINE_TEST(test_option_lz4)
10 {
11 char *p;
12 int r;
13 size_t s;
14
15 /* Create a file. */
16 assertMakeFile("f", 0644, "a");
17
18 /* Archive it with lz4 compression. */
19 r = systemf("echo f | %s -o --lz4 >archive.out 2>archive.err",
20 testprog);
21 p = slurpfile(&s, "archive.err");
22 p[s] = '\0';
23 if (r != 0) {
24 if (strstr(p, "compression not available") != NULL) {
25 skipping("This version of bsdcpio was compiled "
26 "without lz4 support");
27 free(p);
28 return;
29 }
30 /* POSIX permits different handling of the spawnp
31 * system call used to launch the subsidiary
32 * program: */
33 /* Some systems fail immediately to spawn the new process. */
34 if (strstr(p, "Can't launch") != NULL && !canLz4()) {
35 skipping("This version of bsdcpio uses an external lz4 program "
36 "but no such program is available on this system.");
37 free(p);
38 return;
39 }
40 /* Some systems successfully spawn the new process,
41 * but fail to exec a program within that process.
42 * This results in failure at the first attempt to
43 * write. */
44 if (strstr(p, "Can't write") != NULL && !canLz4()) {
45 skipping("This version of bsdcpio uses an external lz4 program "
46 "but no such program is available on this system.");
47 free(p);
48 return;
49 }
50 /* On some systems the error won't be detected until closing
51 time, by a 127 exit error returned by waitpid. */
52 if (strstr(p, "Error closing") != NULL && !canLz4()) {
53 skipping("This version of bsdcpio uses an external lz4 program "
54 "but no such program is available on this system.");
55 free(p);
56 return;
57 }
58 failure("--lz4 option is broken: %s", p);
59 free(p);
60 assertEqualInt(r, 0);
61 return;
62 }
63 free(p);
64 /* Check that the archive file has an lz4 signature. */
65 p = slurpfile(&s, "archive.out");
66 assert(s > 2);
67 assertEqualMem(p, "\x04\x22\x4d\x18", 4);
68 free(p);
69 }
70