xref: /freebsd/contrib/libarchive/tar/test/test_option_zstd.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
15c831a5bSMartin Matuska /*-
2*bd66c1b4SMartin Matuska  * SPDX-License-Identifier: BSD-2-Clause
3*bd66c1b4SMartin Matuska  *
45c831a5bSMartin Matuska  * Copyright (c) 2017 Sean Purcell
55c831a5bSMartin Matuska  * All rights reserved.
65c831a5bSMartin Matuska  */
75c831a5bSMartin Matuska #include "test.h"
85c831a5bSMartin Matuska 
DEFINE_TEST(test_option_zstd)95c831a5bSMartin Matuska DEFINE_TEST(test_option_zstd)
105c831a5bSMartin Matuska {
115c831a5bSMartin Matuska 	char *p;
125c831a5bSMartin Matuska 	int r;
135c831a5bSMartin Matuska 	size_t s;
145c831a5bSMartin Matuska 
155c831a5bSMartin Matuska 	/* Create a file. */
165c831a5bSMartin Matuska 	assertMakeFile("f", 0644, "a");
175c831a5bSMartin Matuska 
185c831a5bSMartin Matuska 	/* Archive it with lz4 compression. */
195c831a5bSMartin Matuska 	r = systemf("%s -cf - --zstd f >archive.out 2>archive.err",
205c831a5bSMartin Matuska 	    testprog);
215c831a5bSMartin Matuska 	p = slurpfile(&s, "archive.err");
225c831a5bSMartin Matuska 	p[s] = '\0';
235c831a5bSMartin Matuska 	if (r != 0) {
245c831a5bSMartin Matuska 		if (strstr(p, "Unsupported compression") != NULL) {
255c831a5bSMartin Matuska 			skipping("This version of bsdtar was compiled "
265c831a5bSMartin Matuska 			    "without zstd support");
275c831a5bSMartin Matuska 			goto done;
285c831a5bSMartin Matuska 		}
295c831a5bSMartin Matuska 		/* POSIX permits different handling of the spawnp
305c831a5bSMartin Matuska 		 * system call used to launch the subsidiary
315c831a5bSMartin Matuska 		 * program: */
325c831a5bSMartin Matuska 		/* Some systems fail immediately to spawn the new process. */
335c831a5bSMartin Matuska 		if (strstr(p, "Can't launch") != NULL && !canZstd()) {
345c831a5bSMartin Matuska 			skipping("This version of bsdtar uses an external zstd program "
355c831a5bSMartin Matuska 			    "but no such program is available on this system.");
365c831a5bSMartin Matuska 			goto done;
375c831a5bSMartin Matuska 		}
385c831a5bSMartin Matuska 		/* Some systems successfully spawn the new process,
395c831a5bSMartin Matuska 		 * but fail to exec a program within that process.
405c831a5bSMartin Matuska 		 * This results in failure at the first attempt to
415c831a5bSMartin Matuska 		 * write. */
425c831a5bSMartin Matuska 		if (strstr(p, "Can't write") != NULL && !canZstd()) {
435c831a5bSMartin Matuska 			skipping("This version of bsdtar uses an external zstd program "
445c831a5bSMartin Matuska 			    "but no such program is available on this system.");
455c831a5bSMartin Matuska 			goto done;
465c831a5bSMartin Matuska 		}
475c831a5bSMartin Matuska 		/* On some systems the error won't be detected until closing
485c831a5bSMartin Matuska 		   time, by a 127 exit error returned by waitpid. */
495c831a5bSMartin Matuska 		if (strstr(p, "Error closing") != NULL && !canZstd()) {
505c831a5bSMartin Matuska 			skipping("This version of bsdcpio uses an external zstd program "
515c831a5bSMartin Matuska 			    "but no such program is available on this system.");
525c831a5bSMartin Matuska 			return;
535c831a5bSMartin Matuska 		}
545c831a5bSMartin Matuska 		failure("--zstd option is broken: %s", p);
555c831a5bSMartin Matuska 		assertEqualInt(r, 0);
565c831a5bSMartin Matuska 		goto done;
575c831a5bSMartin Matuska 	}
585c831a5bSMartin Matuska 	free(p);
595c831a5bSMartin Matuska 	/* Check that the archive file has an lz4 signature. */
605c831a5bSMartin Matuska 	p = slurpfile(&s, "archive.out");
615c831a5bSMartin Matuska 	assert(s > 2);
625c831a5bSMartin Matuska 	assertEqualMem(p, "\x28\xb5\x2f\xfd", 4);
635c831a5bSMartin Matuska 
645c831a5bSMartin Matuska done:
655c831a5bSMartin Matuska 	free(p);
665c831a5bSMartin Matuska }
67