test_basic.c revision 358088
1230832Sgnn/*-
2230832Sgnn * Copyright (c) 2003-2007 Tim Kientzle
3230832Sgnn * All rights reserved.
4230832Sgnn *
5230832Sgnn * Redistribution and use in source and binary forms, with or without
6230832Sgnn * modification, are permitted provided that the following conditions
7230832Sgnn * are met:
8230832Sgnn * 1. Redistributions of source code must retain the above copyright
9230832Sgnn *    notice, this list of conditions and the following disclaimer.
10230832Sgnn * 2. Redistributions in binary form must reproduce the above copyright
11230832Sgnn *    notice, this list of conditions and the following disclaimer in the
12230832Sgnn *    documentation and/or other materials provided with the distribution.
13230832Sgnn *
14230832Sgnn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15230832Sgnn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16230832Sgnn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17230832Sgnn * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18230832Sgnn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19230832Sgnn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20230832Sgnn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21230832Sgnn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22230832Sgnn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23230832Sgnn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24230832Sgnn */
25230832Sgnn#include "test.h"
26230832Sgnn__FBSDID("$FreeBSD: stable/11/contrib/libarchive/cpio/test/test_basic.c 358088 2020-02-19 01:50:47Z mm $");
27230832Sgnn
28230832Sgnnstatic void
29230832Sgnnverify_files(const char *msg)
30230832Sgnn{
31230832Sgnn	/*
32230832Sgnn	 * Verify unpacked files.
33230832Sgnn	 */
34230832Sgnn
35230832Sgnn	/* Regular file with 2 links. */
36230832Sgnn	failure("%s", msg);
37230832Sgnn	assertIsReg("file", 0644);
38230832Sgnn	failure("%s", msg);
39230832Sgnn	assertFileSize("file", 10);
40230832Sgnn	failure("%s", msg);
41230832Sgnn	assertFileNLinks("file", 2);
42230832Sgnn
43230832Sgnn	/* Another name for the same file. */
44230832Sgnn	failure("%s", msg);
45230832Sgnn	assertIsHardlink("linkfile", "file");
46230832Sgnn
47230832Sgnn	/* Symlink */
48230832Sgnn	if (canSymlink())
49230832Sgnn		assertIsSymlink("symlink", "file", 0);
50230832Sgnn
51230832Sgnn	/* Another file with 1 link and different permissions. */
52230832Sgnn	failure("%s", msg);
53230832Sgnn	assertIsReg("file2", 0777);
54230832Sgnn	failure("%s", msg);
55230832Sgnn	assertFileSize("file2", 10);
56230832Sgnn	failure("%s", msg);
57230832Sgnn	assertFileNLinks("file2", 1);
58230832Sgnn
59230832Sgnn	/* dir */
60230832Sgnn	assertIsDir("dir", 0775);
61230832Sgnn}
62230832Sgnn
63230832Sgnnstatic void
64230832Sgnnbasic_cpio(const char *target,
65230832Sgnn    const char *pack_options,
66230832Sgnn    const char *unpack_options,
67230832Sgnn    const char *se, const char *se2)
68230832Sgnn{
69230832Sgnn	int r;
70230832Sgnn
71230832Sgnn	if (!assertMakeDir(target, 0775))
72230832Sgnn	    return;
73230832Sgnn
74230832Sgnn	/* Use the cpio program to create an archive. */
75230832Sgnn	r = systemf("%s -R 1000:1000 -o %s < filelist >%s/archive 2>%s/pack.err",
76230832Sgnn	    testprog, pack_options, target, target);
77230832Sgnn	failure("Error invoking %s -o %s", testprog, pack_options);
78230832Sgnn	assertEqualInt(r, 0);
79230832Sgnn
80230832Sgnn	assertChdir(target);
81230832Sgnn
82230832Sgnn	/* Verify stderr. */
83230832Sgnn	failure("Expected: %s, options=%s", se, pack_options);
84230832Sgnn	assertTextFileContents(se, "pack.err");
85230832Sgnn
86230832Sgnn	/*
87230832Sgnn	 * Use cpio to unpack the archive into another directory.
88230832Sgnn	 */
89230832Sgnn	r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
90230832Sgnn	    testprog, unpack_options);
91230832Sgnn	failure("Error invoking %s -i %s", testprog, unpack_options);
92230832Sgnn	assertEqualInt(r, 0);
93230832Sgnn
94230832Sgnn	/* Verify stderr. */
95230832Sgnn	failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
96230832Sgnn	assertTextFileContents(se2, "unpack.err");
97230832Sgnn
98230832Sgnn	verify_files(pack_options);
99230832Sgnn
100230832Sgnn	assertChdir("..");
101230832Sgnn}
102230832Sgnn
103230832Sgnnstatic void
104230832Sgnnpassthrough(const char *target)
105230832Sgnn{
106230832Sgnn	int r;
107230832Sgnn
108230832Sgnn	if (!assertMakeDir(target, 0775))
109230832Sgnn		return;
110230832Sgnn
111230832Sgnn	/*
112230832Sgnn	 * Use cpio passthrough mode to copy files to another directory.
113230832Sgnn	 */
114230832Sgnn	r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
115230832Sgnn	    testprog, target, target, target);
116230832Sgnn	failure("Error invoking %s -p", testprog);
117230832Sgnn	assertEqualInt(r, 0);
118230832Sgnn
119230832Sgnn	assertChdir(target);
120230832Sgnn
121230832Sgnn	/* Verify stderr. */
122230832Sgnn	failure("Error invoking %s -p in dir %s",
123230832Sgnn	    testprog, target);
124230832Sgnn	assertTextFileContents("1 block\n", "stderr");
125230832Sgnn
126230832Sgnn	verify_files("passthrough");
127230832Sgnn	assertChdir("..");
128230832Sgnn}
129230832Sgnn
130230832SgnnDEFINE_TEST(test_basic)
131230832Sgnn{
132230832Sgnn	FILE *filelist;
133230832Sgnn	const char *msg;
134230832Sgnn	char result[1024];
135230832Sgnn
136230832Sgnn	assertUmask(0);
137230832Sgnn
138230832Sgnn	/*
139230832Sgnn	 * Create an assortment of files on disk.
140230832Sgnn	 */
141230832Sgnn	filelist = fopen("filelist", "w");
142230832Sgnn	memset(result, 0, sizeof(result));
143230832Sgnn
144230832Sgnn	/* File with 10 bytes content. */
145230832Sgnn	assertMakeFile("file", 0644, "1234567890");
146230832Sgnn	fprintf(filelist, "file\n");
147230832Sgnn	if (is_LargeInode("file")) {
148230832Sgnn		strncat(result,
149230832Sgnn		    "bsdcpio: file: large inode number truncated: ",
150230832Sgnn		    sizeof(result) - strlen(result) -1);
151230832Sgnn		strncat(result,
152230832Sgnn		    strerror(ERANGE),
153230832Sgnn		    sizeof(result) - strlen(result) -1);
154230832Sgnn		strncat(result,
155230832Sgnn		    "\n",
156230832Sgnn		    sizeof(result) - strlen(result) -1);
157230832Sgnn	}
158230832Sgnn
159230832Sgnn	/* hardlink to above file. */
160230832Sgnn	assertMakeHardlink("linkfile", "file");
161230832Sgnn	fprintf(filelist, "linkfile\n");
162230832Sgnn	if (is_LargeInode("linkfile")) {
163230832Sgnn		strncat(result,
164230832Sgnn		    "bsdcpio: linkfile: large inode number truncated: ",
165230832Sgnn		    sizeof(result) - strlen(result) -1);
166230832Sgnn		strncat(result,
167230832Sgnn		    strerror(ERANGE),
168230832Sgnn		    sizeof(result) - strlen(result) -1);
169230832Sgnn		strncat(result,
170230832Sgnn		    "\n",
171230832Sgnn		    sizeof(result) - strlen(result) -1);
172230832Sgnn	}
173230832Sgnn
174230832Sgnn	/* Symlink to above file. */
175230832Sgnn	if (canSymlink()) {
176230832Sgnn		assertMakeSymlink("symlink", "file", 0);
177230832Sgnn		fprintf(filelist, "symlink\n");
178230832Sgnn		if (is_LargeInode("symlink")) {
179230832Sgnn			strncat(result,
180230832Sgnn			    "bsdcpio: symlink: large inode number truncated: ",
181230832Sgnn			    sizeof(result) - strlen(result) -1);
182230832Sgnn			strncat(result,
183230832Sgnn			    strerror(ERANGE),
184230832Sgnn			    sizeof(result) - strlen(result) -1);
185230832Sgnn			strncat(result,
186230832Sgnn			    "\n",
187230832Sgnn			    sizeof(result) - strlen(result) -1);
188230832Sgnn		}
189230832Sgnn	}
190230832Sgnn
191230832Sgnn	/* Another file with different permissions. */
192230832Sgnn	assertMakeFile("file2", 0777, "1234567890");
193230832Sgnn	fprintf(filelist, "file2\n");
194230832Sgnn	if (is_LargeInode("file2")) {
195230832Sgnn		strncat(result,
196230832Sgnn		    "bsdcpio: file2: large inode number truncated: ",
197230832Sgnn		    sizeof(result) - strlen(result) -1);
198230832Sgnn		strncat(result,
199230832Sgnn		    strerror(ERANGE),
200230832Sgnn		    sizeof(result) - strlen(result) -1);
201230832Sgnn		strncat(result,
202230832Sgnn		    "\n",
203230832Sgnn		    sizeof(result) - strlen(result) -1);
204230832Sgnn	}
205230832Sgnn
206230832Sgnn	/* Directory. */
207230832Sgnn	assertMakeDir("dir", 0775);
208230832Sgnn	fprintf(filelist, "dir\n");
209230832Sgnn	if (is_LargeInode("dir")) {
210230832Sgnn		strncat(result,
211230832Sgnn		    "bsdcpio: dir: large inode number truncated: ",
212230832Sgnn		    sizeof(result) - strlen(result) -1);
213230832Sgnn		strncat(result,
214230832Sgnn		    strerror(ERANGE),
215230832Sgnn		    sizeof(result) - strlen(result) -1);
216230832Sgnn		strncat(result,
217230832Sgnn		    "\n",
218230832Sgnn		    sizeof(result) - strlen(result) -1);
219230832Sgnn	}
220230832Sgnn	strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
221230832Sgnn
222230832Sgnn	/* All done. */
223230832Sgnn	fclose(filelist);
224230832Sgnn
225230832Sgnn	assertUmask(022);
226230832Sgnn
227	/* Archive/dearchive with a variety of options. */
228	msg = canSymlink() ? "2 blocks\n" : "1 block\n";
229	basic_cpio("copy", "", "", msg, msg);
230	basic_cpio("copy_odc", "--format=odc", "", msg, msg);
231	basic_cpio("copy_newc", "-H newc", "", result, "2 blocks\n");
232	basic_cpio("copy_cpio", "-H odc", "", msg, msg);
233	msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";
234	basic_cpio("copy_ustar", "-H ustar", "", msg, msg);
235
236	/* Copy in one step using -p */
237	passthrough("passthrough");
238}
239