test_basic.c revision 228753
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"
26228753Smm__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_basic.c,v 1.4 2008/08/25 06:39:29 kientzle Exp $");
27228753Smm
28228753Smmstatic void
29228753Smmverify_files(const char *msg)
30228753Smm{
31228753Smm	/*
32228753Smm	 * Verify unpacked files.
33228753Smm	 */
34228753Smm
35228753Smm	/* Regular file with 2 links. */
36228753Smm	assertIsReg("file", 0644);
37228753Smm	failure(msg);
38228753Smm	assertFileSize("file", 10);
39228753Smm	assertFileNLinks("file", 2);
40228753Smm
41228753Smm	/* Another name for the same file. */
42228753Smm	assertIsHardlink("linkfile", "file");
43228753Smm
44228753Smm	/* Symlink */
45228753Smm	if (canSymlink())
46228753Smm		assertIsSymlink("symlink", "file");
47228753Smm
48228753Smm	/* Another file with 1 link and different permissions. */
49228753Smm	assertIsReg("file2", 0777);
50228753Smm	assertFileSize("file2", 10);
51228753Smm	assertFileNLinks("file2", 1);
52228753Smm
53228753Smm	/* dir */
54228753Smm	assertIsDir("dir", 0775);
55228753Smm}
56228753Smm
57228753Smmstatic void
58228753Smmbasic_cpio(const char *target,
59228753Smm    const char *pack_options,
60228753Smm    const char *unpack_options,
61228753Smm    const char *se)
62228753Smm{
63228753Smm	int r;
64228753Smm
65228753Smm	if (!assertMakeDir(target, 0775))
66228753Smm	    return;
67228753Smm
68228753Smm	/* Use the cpio program to create an archive. */
69228753Smm	r = systemf("%s -o %s < filelist >%s/archive 2>%s/pack.err",
70228753Smm	    testprog, pack_options, target, target);
71228753Smm	failure("Error invoking %s -o %s", testprog, pack_options);
72228753Smm	assertEqualInt(r, 0);
73228753Smm
74228753Smm	assertChdir(target);
75228753Smm
76228753Smm	/* Verify stderr. */
77228753Smm	failure("Expected: %s, options=%s", se, pack_options);
78228753Smm	assertTextFileContents(se, "pack.err");
79228753Smm
80228753Smm	/*
81228753Smm	 * Use cpio to unpack the archive into another directory.
82228753Smm	 */
83228753Smm	r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
84228753Smm	    testprog, unpack_options);
85228753Smm	failure("Error invoking %s -i %s", testprog, unpack_options);
86228753Smm	assertEqualInt(r, 0);
87228753Smm
88228753Smm	/* Verify stderr. */
89228753Smm	failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
90228753Smm	assertTextFileContents(se, "unpack.err");
91228753Smm
92228753Smm	verify_files(pack_options);
93228753Smm
94228753Smm	assertChdir("..");
95228753Smm}
96228753Smm
97228753Smmstatic void
98228753Smmpassthrough(const char *target)
99228753Smm{
100228753Smm	int r;
101228753Smm
102228753Smm	if (!assertMakeDir(target, 0775))
103228753Smm		return;
104228753Smm
105228753Smm	/*
106228753Smm	 * Use cpio passthrough mode to copy files to another directory.
107228753Smm	 */
108228753Smm	r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
109228753Smm	    testprog, target, target, target);
110228753Smm	failure("Error invoking %s -p", testprog);
111228753Smm	assertEqualInt(r, 0);
112228753Smm
113228753Smm	assertChdir(target);
114228753Smm
115228753Smm	/* Verify stderr. */
116228753Smm	failure("Error invoking %s -p in dir %s",
117228753Smm	    testprog, target);
118228753Smm	assertTextFileContents("1 block\n", "stderr");
119228753Smm
120228753Smm	verify_files("passthrough");
121228753Smm	assertChdir("..");
122228753Smm}
123228753Smm
124228753SmmDEFINE_TEST(test_basic)
125228753Smm{
126228753Smm	FILE *filelist;
127228753Smm	const char *msg;
128228753Smm
129228753Smm	assertUmask(0);
130228753Smm
131228753Smm	/*
132228753Smm	 * Create an assortment of files on disk.
133228753Smm	 */
134228753Smm	filelist = fopen("filelist", "w");
135228753Smm
136228753Smm	/* File with 10 bytes content. */
137228753Smm	assertMakeFile("file", 0644, "1234567890");
138228753Smm	fprintf(filelist, "file\n");
139228753Smm
140228753Smm	/* hardlink to above file. */
141228753Smm	assertMakeHardlink("linkfile", "file");
142228753Smm	fprintf(filelist, "linkfile\n");
143228753Smm
144228753Smm	/* Symlink to above file. */
145228753Smm	if (canSymlink()) {
146228753Smm		assertMakeSymlink("symlink", "file");
147228753Smm		fprintf(filelist, "symlink\n");
148228753Smm	}
149228753Smm
150228753Smm	/* Another file with different permissions. */
151228753Smm	assertMakeFile("file2", 0777, "1234567890");
152228753Smm	fprintf(filelist, "file2\n");
153228753Smm
154228753Smm	/* Directory. */
155228753Smm	assertMakeDir("dir", 0775);
156228753Smm	fprintf(filelist, "dir\n");
157228753Smm	/* All done. */
158228753Smm	fclose(filelist);
159228753Smm
160228753Smm	assertUmask(022);
161228753Smm
162228753Smm	/* Archive/dearchive with a variety of options. */
163228753Smm	msg = canSymlink() ? "2 blocks\n" : "1 block\n";
164228753Smm	basic_cpio("copy", "", "", msg);
165228753Smm	basic_cpio("copy_odc", "--format=odc", "", msg);
166228753Smm	basic_cpio("copy_newc", "-H newc", "", "2 blocks\n");
167228753Smm	basic_cpio("copy_cpio", "-H odc", "", msg);
168228753Smm	msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";
169228753Smm	basic_cpio("copy_ustar", "-H ustar", "", msg);
170228753Smm
171228753Smm	/* Copy in one step using -p */
172228753Smm	passthrough("passthrough");
173228753Smm}
174