etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / libarchive / dist / cpio / test / test_basic.c
blob852b069a16e7c496046449e97b2f7754c40b36f1
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "test.h"
26 __FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_basic.c,v 1.4 2008/08/25 06:39:29 kientzle Exp $");
28 static void
29 verify_files(const char *msg)
32 * Verify unpacked files.
35 /* Regular file with 2 links. */
36 assertIsReg("file", 0644);
37 failure(msg);
38 assertFileSize("file", 10);
39 assertFileNLinks("file", 2);
41 /* Another name for the same file. */
42 assertIsHardlink("linkfile", "file");
44 /* Symlink */
45 if (canSymlink())
46 assertIsSymlink("symlink", "file");
48 /* Another file with 1 link and different permissions. */
49 assertIsReg("file2", 0777);
50 assertFileSize("file2", 10);
51 assertFileNLinks("file2", 1);
53 /* dir */
54 assertIsDir("dir", 0775);
57 static void
58 basic_cpio(const char *target,
59 const char *pack_options,
60 const char *unpack_options,
61 const char *se)
63 int r;
65 if (!assertMakeDir(target, 0775))
66 return;
68 /* Use the cpio program to create an archive. */
69 r = systemf("%s -o %s < filelist >%s/archive 2>%s/pack.err",
70 testprog, pack_options, target, target);
71 failure("Error invoking %s -o %s", testprog, pack_options);
72 assertEqualInt(r, 0);
74 assertChdir(target);
76 /* Verify stderr. */
77 failure("Expected: %s, options=%s", se, pack_options);
78 assertTextFileContents(se, "pack.err");
81 * Use cpio to unpack the archive into another directory.
83 r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
84 testprog, unpack_options);
85 failure("Error invoking %s -i %s", testprog, unpack_options);
86 assertEqualInt(r, 0);
88 /* Verify stderr. */
89 failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
90 assertTextFileContents(se, "unpack.err");
92 verify_files(pack_options);
94 assertChdir("..");
97 static void
98 passthrough(const char *target)
100 int r;
102 if (!assertMakeDir(target, 0775))
103 return;
106 * Use cpio passthrough mode to copy files to another directory.
108 r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
109 testprog, target, target, target);
110 failure("Error invoking %s -p", testprog);
111 assertEqualInt(r, 0);
113 assertChdir(target);
115 /* Verify stderr. */
116 failure("Error invoking %s -p in dir %s",
117 testprog, target);
118 assertTextFileContents("1 block\n", "stderr");
120 verify_files("passthrough");
121 assertChdir("..");
124 DEFINE_TEST(test_basic)
126 FILE *filelist;
127 const char *msg;
129 assertUmask(0);
132 * Create an assortment of files on disk.
134 filelist = fopen("filelist", "w");
136 /* File with 10 bytes content. */
137 assertMakeFile("file", 0644, "1234567890");
138 fprintf(filelist, "file\n");
140 /* hardlink to above file. */
141 assertMakeHardlink("linkfile", "file");
142 fprintf(filelist, "linkfile\n");
144 /* Symlink to above file. */
145 if (canSymlink()) {
146 assertMakeSymlink("symlink", "file");
147 fprintf(filelist, "symlink\n");
150 /* Another file with different permissions. */
151 assertMakeFile("file2", 0777, "1234567890");
152 fprintf(filelist, "file2\n");
154 /* Directory. */
155 assertMakeDir("dir", 0775);
156 fprintf(filelist, "dir\n");
157 /* All done. */
158 fclose(filelist);
160 assertUmask(022);
162 /* Archive/dearchive with a variety of options. */
163 msg = canSymlink() ? "2 blocks\n" : "1 block\n";
164 basic_cpio("copy", "", "", msg);
165 basic_cpio("copy_odc", "--format=odc", "", msg);
166 basic_cpio("copy_newc", "-H newc", "", "2 blocks\n");
167 basic_cpio("copy_cpio", "-H odc", "", msg);
168 msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";
169 basic_cpio("copy_ustar", "-H ustar", "", msg);
171 /* Copy in one step using -p */
172 passthrough("passthrough");