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