xref: /freebsd/contrib/libarchive/tar/test/test_option_zstd.c (revision 5c831a5bd61576cacb48b39f8eeb47b92707a355)
1*5c831a5bSMartin Matuska /*-
2*5c831a5bSMartin Matuska  * Copyright (c) 2017 Sean Purcell
3*5c831a5bSMartin Matuska  * All rights reserved.
4*5c831a5bSMartin Matuska  *
5*5c831a5bSMartin Matuska  * Redistribution and use in source and binary forms, with or without
6*5c831a5bSMartin Matuska  * modification, are permitted provided that the following conditions
7*5c831a5bSMartin Matuska  * are met:
8*5c831a5bSMartin Matuska  * 1. Redistributions of source code must retain the above copyright
9*5c831a5bSMartin Matuska  *    notice, this list of conditions and the following disclaimer.
10*5c831a5bSMartin Matuska  * 2. Redistributions in binary form must reproduce the above copyright
11*5c831a5bSMartin Matuska  *    notice, this list of conditions and the following disclaimer in the
12*5c831a5bSMartin Matuska  *    documentation and/or other materials provided with the distribution.
13*5c831a5bSMartin Matuska  *
14*5c831a5bSMartin Matuska  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*5c831a5bSMartin Matuska  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*5c831a5bSMartin Matuska  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*5c831a5bSMartin Matuska  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*5c831a5bSMartin Matuska  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*5c831a5bSMartin Matuska  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*5c831a5bSMartin Matuska  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*5c831a5bSMartin Matuska  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*5c831a5bSMartin Matuska  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*5c831a5bSMartin Matuska  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*5c831a5bSMartin Matuska  */
25*5c831a5bSMartin Matuska #include "test.h"
26*5c831a5bSMartin Matuska __FBSDID("$FreeBSD$");
27*5c831a5bSMartin Matuska 
28*5c831a5bSMartin Matuska DEFINE_TEST(test_option_zstd)
29*5c831a5bSMartin Matuska {
30*5c831a5bSMartin Matuska 	char *p;
31*5c831a5bSMartin Matuska 	int r;
32*5c831a5bSMartin Matuska 	size_t s;
33*5c831a5bSMartin Matuska 
34*5c831a5bSMartin Matuska 	/* Create a file. */
35*5c831a5bSMartin Matuska 	assertMakeFile("f", 0644, "a");
36*5c831a5bSMartin Matuska 
37*5c831a5bSMartin Matuska 	/* Archive it with lz4 compression. */
38*5c831a5bSMartin Matuska 	r = systemf("%s -cf - --zstd f >archive.out 2>archive.err",
39*5c831a5bSMartin Matuska 	    testprog);
40*5c831a5bSMartin Matuska 	p = slurpfile(&s, "archive.err");
41*5c831a5bSMartin Matuska 	p[s] = '\0';
42*5c831a5bSMartin Matuska 	if (r != 0) {
43*5c831a5bSMartin Matuska 		if (strstr(p, "Unsupported compression") != NULL) {
44*5c831a5bSMartin Matuska 			skipping("This version of bsdtar was compiled "
45*5c831a5bSMartin Matuska 			    "without zstd support");
46*5c831a5bSMartin Matuska 			goto done;
47*5c831a5bSMartin Matuska 		}
48*5c831a5bSMartin Matuska 		/* POSIX permits different handling of the spawnp
49*5c831a5bSMartin Matuska 		 * system call used to launch the subsidiary
50*5c831a5bSMartin Matuska 		 * program: */
51*5c831a5bSMartin Matuska 		/* Some systems fail immediately to spawn the new process. */
52*5c831a5bSMartin Matuska 		if (strstr(p, "Can't launch") != NULL && !canZstd()) {
53*5c831a5bSMartin Matuska 			skipping("This version of bsdtar uses an external zstd program "
54*5c831a5bSMartin Matuska 			    "but no such program is available on this system.");
55*5c831a5bSMartin Matuska 			goto done;
56*5c831a5bSMartin Matuska 		}
57*5c831a5bSMartin Matuska 		/* Some systems successfully spawn the new process,
58*5c831a5bSMartin Matuska 		 * but fail to exec a program within that process.
59*5c831a5bSMartin Matuska 		 * This results in failure at the first attempt to
60*5c831a5bSMartin Matuska 		 * write. */
61*5c831a5bSMartin Matuska 		if (strstr(p, "Can't write") != NULL && !canZstd()) {
62*5c831a5bSMartin Matuska 			skipping("This version of bsdtar uses an external zstd program "
63*5c831a5bSMartin Matuska 			    "but no such program is available on this system.");
64*5c831a5bSMartin Matuska 			goto done;
65*5c831a5bSMartin Matuska 		}
66*5c831a5bSMartin Matuska 		/* On some systems the error won't be detected until closing
67*5c831a5bSMartin Matuska 		   time, by a 127 exit error returned by waitpid. */
68*5c831a5bSMartin Matuska 		if (strstr(p, "Error closing") != NULL && !canZstd()) {
69*5c831a5bSMartin Matuska 			skipping("This version of bsdcpio uses an external zstd program "
70*5c831a5bSMartin Matuska 			    "but no such program is available on this system.");
71*5c831a5bSMartin Matuska 			return;
72*5c831a5bSMartin Matuska 		}
73*5c831a5bSMartin Matuska 		failure("--zstd option is broken: %s", p);
74*5c831a5bSMartin Matuska 		assertEqualInt(r, 0);
75*5c831a5bSMartin Matuska 		goto done;
76*5c831a5bSMartin Matuska 	}
77*5c831a5bSMartin Matuska 	free(p);
78*5c831a5bSMartin Matuska 	/* Check that the archive file has an lz4 signature. */
79*5c831a5bSMartin Matuska 	p = slurpfile(&s, "archive.out");
80*5c831a5bSMartin Matuska 	assert(s > 2);
81*5c831a5bSMartin Matuska 	assertEqualMem(p, "\x28\xb5\x2f\xfd", 4);
82*5c831a5bSMartin Matuska 
83*5c831a5bSMartin Matuska done:
84*5c831a5bSMartin Matuska 	free(p);
85*5c831a5bSMartin Matuska }
86