xref: /freebsd/contrib/libarchive/cpio/test/test_option_zstd.c (revision 47ef2a131091508e049ab10cad7f91a3c1342cd9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2017 Sean Purcell
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 DEFINE_TEST(test_option_zstd)
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 zstd compression. */
19 	r = systemf("echo f | %s -o --zstd >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, "Unsupported compression") != NULL) {
25 			skipping("This version of bsdcpio was compiled "
26 			    "without zstd support");
27 			goto done;
28 		}
29 		/* POSIX permits different handling of the spawnp
30 		 * system call used to launch the subsidiary
31 		 * program: */
32 		/* Some systems fail immediately to spawn the new process. */
33 		if (strstr(p, "Can't launch") != NULL && !canZstd()) {
34 			skipping("This version of bsdcpio uses an external zstd program "
35 			    "but no such program is available on this system.");
36 			goto done;
37 		}
38 		/* Some systems successfully spawn the new process,
39 		 * but fail to exec a program within that process.
40 		 * This results in failure at the first attempt to
41 		 * write. */
42 		if (strstr(p, "Can't write") != NULL && !canZstd()) {
43 			skipping("This version of bsdcpio uses an external zstd program "
44 			    "but no such program is available on this system.");
45 			goto done;
46 		}
47 		/* On some systems the error won't be detected until closing
48 		   time, by a 127 exit error returned by waitpid. */
49 		if (strstr(p, "Error closing") != NULL && !canZstd()) {
50 			skipping("This version of bsdcpio uses an external zstd program "
51 			    "but no such program is available on this system.");
52 			return;
53 		}
54 		failure("--zstd option is broken: %s", p);
55 		assertEqualInt(r, 0);
56 		goto done;
57 	}
58 	free(p);
59 	/* Check that the archive file has an zstd signature. */
60 	p = slurpfile(&s, "archive.out");
61 	assert(s > 2);
62 	assertEqualMem(p, "\x28\xb5\x2f\xfd", 4);
63 
64 done:
65 	free(p);
66 }
67