144574Sphk/*-
244574Sphk * Copyright (c) 2014 Michihiro NAKAJIMA
375540Sjhay * All rights reserved.
444574Sphk *
544574Sphk * Redistribution and use in source and binary forms, with or without
644574Sphk * modification, are permitted provided that the following conditions
744574Sphk * are met:
844574Sphk * 1. Redistributions of source code must retain the above copyright
944574Sphk *    notice, this list of conditions and the following disclaimer.
1044574Sphk * 2. Redistributions in binary form must reproduce the above copyright
1144574Sphk *    notice, this list of conditions and the following disclaimer in the
1244574Sphk *    documentation and/or other materials provided with the distribution.
1344574Sphk *
1444574Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1544574Sphk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1644574Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1744574Sphk * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
182858Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
192858Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2044574Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2144666Sphk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
222858Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2344574Sphk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2444574Sphk */
2544574Sphk#include "test.h"
2621101Sjhay__FBSDID("$FreeBSD: stable/11/contrib/libarchive/cpio/test/test_option_lz4.c 318482 2017-05-18 19:47:43Z mm $");
2744574Sphk
2844574SphkDEFINE_TEST(test_option_lz4)
2944574Sphk{
302858Swollman	char *p;
3132925Seivind	int r;
32116182Sobrien	size_t s;
33116182Sobrien
34116182Sobrien	/* Create a file. */
3544666Sphk	assertMakeFile("f", 0644, "a");
3644666Sphk
372858Swollman	/* Archive it with lz4 compression. */
382858Swollman	r = systemf("echo f | %s -o --lz4 >archive.out 2>archive.err",
3912221Sbde	    testprog);
402858Swollman	p = slurpfile(&s, "archive.err");
412858Swollman	p[s] = '\0';
4282717Sdillon	if (r != 0) {
4382717Sdillon		if (strstr(p, "compression not available") != NULL) {
4444574Sphk			skipping("This version of bsdcpio was compiled "
452858Swollman			    "without lz4 support");
4658377Sphk			free(p);
4736941Sphk			return;
482858Swollman		}
492858Swollman		/* POSIX permits different handling of the spawnp
502858Swollman		 * system call used to launch the subsidiary
5144574Sphk		 * program: */
5244574Sphk		/* Some systems fail immediately to spawn the new process. */
53126974Sphk		if (strstr(p, "Can't launch") != NULL && !canLz4()) {
5444574Sphk			skipping("This version of bsdcpio uses an external lz4 program "
5544574Sphk			    "but no such program is available on this system.");
56126974Sphk			free(p);
5744574Sphk			return;
5844574Sphk		}
5944574Sphk		/* Some systems successfully spawn the new process,
6044574Sphk		 * but fail to exec a program within that process.
6144574Sphk		 * This results in failure at the first attempt to
6244574Sphk		 * write. */
6344574Sphk		if (strstr(p, "Can't write") != NULL && !canLz4()) {
6444574Sphk			skipping("This version of bsdcpio uses an external lz4 program "
6544574Sphk			    "but no such program is available on this system.");
6644574Sphk			free(p);
6744574Sphk			return;
68126974Sphk		}
6944574Sphk		/* On some systems the error won't be detected until closing
7044574Sphk		   time, by a 127 exit error returned by waitpid. */
7144574Sphk		if (strstr(p, "Error closing") != NULL && !canLz4()) {
7244574Sphk			skipping("This version of bsdcpio uses an external lz4 program "
7332513Sphk			    "but no such program is available on this system.");
7444574Sphk			free(p);
7544574Sphk			return;
7644574Sphk		}
7744574Sphk		failure("--lz4 option is broken: %s", p);
7844574Sphk		free(p);
7944574Sphk		assertEqualInt(r, 0);
8044574Sphk		return;
8144574Sphk	}
8244574Sphk	free(p);
8332513Sphk	/* Check that the archive file has an lz4 signature. */
8445294Sphk	p = slurpfile(&s, "archive.out");
8544574Sphk	assert(s > 2);
8644574Sphk	assertEqualMem(p, "\x04\x22\x4d\x18", 4);
8745294Sphk	free(p);
8845294Sphk}
8945294Sphk