1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/cpio/test/test_gcpio_compat.c 348608 2019-06-04 10:36:26Z mm $");
27228753Smm
28228753Smmstatic void
29228753Smmunpack_test(const char *from, const char *options, const char *se)
30228753Smm{
31228753Smm	int r;
32228753Smm
33228753Smm	/* Create a work dir named after the file we're unpacking. */
34228753Smm	assertMakeDir(from, 0775);
35228753Smm	assertChdir(from);
36228753Smm
37228753Smm	/*
38228753Smm	 * Use cpio to unpack the sample archive
39228753Smm	 */
40228753Smm	extract_reference_file(from);
41228753Smm	r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
42228753Smm	    testprog, options, from);
43228753Smm	failure("Error invoking %s -i %s < %s",
44228753Smm	    testprog, options, from);
45228753Smm	assertEqualInt(r, 0);
46228753Smm
47228753Smm	/* Verify that nothing went to stderr. */
48228753Smm	if (canSymlink()) {
49228753Smm		failure("Error invoking %s -i %s < %s",
50228753Smm		    testprog, options, from);
51228753Smm		assertTextFileContents(se, "unpack.err");
52228753Smm	}
53228753Smm
54228753Smm	/*
55228753Smm	 * Verify unpacked files.
56228753Smm	 */
57228753Smm
58228753Smm	/* Regular file with 2 links. */
59228753Smm	assertIsReg("file", 0644);
60228753Smm	failure("%s", from);
61228753Smm	assertFileSize("file", 10);
62228753Smm	assertFileSize("linkfile", 10);
63228753Smm	failure("%s", from);
64228753Smm	assertFileNLinks("file", 2);
65228753Smm
66228753Smm	/* Another name for the same file. */
67228753Smm	failure("%s", from);
68228753Smm	assertIsHardlink("linkfile", "file");
69228753Smm	assertFileSize("file", 10);
70228753Smm	assertFileSize("linkfile", 10);
71228753Smm
72228753Smm	/* Symlink */
73228753Smm	if (canSymlink())
74348608Smm		assertIsSymlink("symlink", "file", 0);
75228753Smm
76228753Smm	/* dir */
77228753Smm	assertIsDir("dir", 0775);
78228753Smm
79228753Smm	assertChdir("..");
80228753Smm}
81228753Smm
82228753SmmDEFINE_TEST(test_gcpio_compat)
83228753Smm{
84228753Smm	assertUmask(0);
85228753Smm
86228753Smm	/* Dearchive sample files with a variety of options. */
87228753Smm	if (canSymlink()) {
88228753Smm		unpack_test("test_gcpio_compat_ref.bin",
89228753Smm		    "--no-preserve-owner", "1 block\n");
90228753Smm		unpack_test("test_gcpio_compat_ref.crc",
91228753Smm		    "--no-preserve-owner", "2 blocks\n");
92228753Smm		unpack_test("test_gcpio_compat_ref.newc",
93228753Smm		    "--no-preserve-owner", "2 blocks\n");
94228753Smm		/* gcpio-2.9 only reads 6 blocks here */
95228753Smm		unpack_test("test_gcpio_compat_ref.ustar",
96228753Smm		    "--no-preserve-owner", "7 blocks\n");
97228753Smm	} else {
98228753Smm		unpack_test("test_gcpio_compat_ref_nosym.bin",
99228753Smm		    "--no-preserve-owner", "1 block\n");
100228753Smm		unpack_test("test_gcpio_compat_ref_nosym.crc",
101228753Smm		    "--no-preserve-owner", "2 blocks\n");
102228753Smm		unpack_test("test_gcpio_compat_ref_nosym.newc",
103228753Smm		    "--no-preserve-owner", "2 blocks\n");
104228753Smm		/* gcpio-2.9 only reads 6 blocks here */
105228753Smm		unpack_test("test_gcpio_compat_ref_nosym.ustar",
106228753Smm		    "--no-preserve-owner", "7 blocks\n");
107228753Smm	}
108228753Smm}
109