test_option_zstd.c revision 324417
1139825Simp/*-
21541Srgrimes * Copyright (c) 2017 Sean Purcell
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151541Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161541Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171541Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
181541Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191541Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201541Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211541Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221541Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231541Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241541Srgrimes */
251541Srgrimes#include "test.h"
261541Srgrimes__FBSDID("$FreeBSD: stable/11/contrib/libarchive/tar/test/test_option_zstd.c 324417 2017-10-08 20:54:53Z mm $");
271541Srgrimes
281541SrgrimesDEFINE_TEST(test_option_zstd)
291541Srgrimes{
301541Srgrimes	char *p;
311541Srgrimes	int r;
321541Srgrimes	size_t s;
331541Srgrimes
341541Srgrimes	/* Create a file. */
351541Srgrimes	assertMakeFile("f", 0644, "a");
361541Srgrimes
371541Srgrimes	/* Archive it with lz4 compression. */
381541Srgrimes	r = systemf("%s -cf - --zstd f >archive.out 2>archive.err",
391541Srgrimes	    testprog);
401541Srgrimes	p = slurpfile(&s, "archive.err");
411541Srgrimes	p[s] = '\0';
421541Srgrimes	if (r != 0) {
43116226Sobrien		if (strstr(p, "Unsupported compression") != NULL) {
44116226Sobrien			skipping("This version of bsdtar was compiled "
45116226Sobrien			    "without zstd support");
4631778Seivind			goto done;
47157144Sjkoshy		}
4820821Sjoerg		/* POSIX permits different handling of the spawnp
491541Srgrimes		 * system call used to launch the subsidiary
5076166Smarkm		 * program: */
51280258Srwatson		/* Some systems fail immediately to spawn the new process. */
52224778Srwatson		if (strstr(p, "Can't launch") != NULL && !canZstd()) {
5376166Smarkm			skipping("This version of bsdtar uses an external zstd program "
5476827Salfred			    "but no such program is available on this system.");
5512221Sbde			goto done;
561541Srgrimes		}
57164033Srwatson		/* Some systems successfully spawn the new process,
581541Srgrimes		 * but fail to exec a program within that process.
59255708Sjhb		 * This results in failure at the first attempt to
60220373Strasz		 * write. */
6198833Sdillon		if (strstr(p, "Can't write") != NULL && !canZstd()) {
6298833Sdillon			skipping("This version of bsdtar uses an external zstd program "
63248084Sattilio			    "but no such program is available on this system.");
64244384Szont			goto done;
651541Srgrimes		}
6624131Sbde		/* On some systems the error won't be detected until closing
671541Srgrimes		   time, by a 127 exit error returned by waitpid. */
681541Srgrimes		if (strstr(p, "Error closing") != NULL && !canZstd()) {
69127187Sguido			skipping("This version of bsdcpio uses an external zstd program "
701541Srgrimes			    "but no such program is available on this system.");
7136177Speter			return;
72255708Sjhb		}
73197348Skib		failure("--zstd option is broken: %s", p);
7412662Sdg		assertEqualInt(r, 0);
751541Srgrimes		goto done;
76163606Srwatson	}
77163606Srwatson	free(p);
781541Srgrimes	/* Check that the archive file has an lz4 signature. */
7912662Sdg	p = slurpfile(&s, "archive.out");
8012662Sdg	assert(s > 2);
8112662Sdg	assertEqualMem(p, "\x28\xb5\x2f\xfd", 4);
8212662Sdg
8342957Sdillondone:
841541Srgrimes	free(p);
857090Sbde}
8612662Sdg