2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
26 __FBSDID("$FreeBSD$");
29 verify_files(const char *msg)
32 * Verify unpacked files.
35 /* Regular file with 2 links. */
37 assertIsReg("file", 0644);
39 assertFileSize("file", 10);
41 assertFileNLinks("file", 2);
43 /* Another name for the same file. */
45 assertIsHardlink("linkfile", "file");
49 assertIsSymlink("symlink", "file");
51 /* Another file with 1 link and different permissions. */
53 assertIsReg("file2", 0777);
55 assertFileSize("file2", 10);
57 assertFileNLinks("file2", 1);
60 assertIsDir("dir", 0775);
64 basic_cpio(const char *target,
65 const char *pack_options,
66 const char *unpack_options,
67 const char *se, const char *se2)
71 if (!assertMakeDir(target, 0775))
74 /* Use the cpio program to create an archive. */
75 r = systemf("%s -R 1000:1000 -o %s < filelist >%s/archive 2>%s/pack.err",
76 testprog, pack_options, target, target);
77 failure("Error invoking %s -o %s", testprog, pack_options);
83 failure("Expected: %s, options=%s", se, pack_options);
84 assertTextFileContents(se, "pack.err");
87 * Use cpio to unpack the archive into another directory.
89 r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
90 testprog, unpack_options);
91 failure("Error invoking %s -i %s", testprog, unpack_options);
95 failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
96 assertTextFileContents(se2, "unpack.err");
98 verify_files(pack_options);
104 passthrough(const char *target)
108 if (!assertMakeDir(target, 0775))
112 * Use cpio passthrough mode to copy files to another directory.
114 r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
115 testprog, target, target, target);
116 failure("Error invoking %s -p", testprog);
117 assertEqualInt(r, 0);
122 failure("Error invoking %s -p in dir %s",
124 assertTextFileContents("1 block\n", "stderr");
126 verify_files("passthrough");
130 DEFINE_TEST(test_basic)
139 * Create an assortment of files on disk.
141 filelist = fopen("filelist", "w");
142 memset(result, 0, sizeof(result));
144 /* File with 10 bytes content. */
145 assertMakeFile("file", 0644, "1234567890");
146 fprintf(filelist, "file\n");
147 if (is_LargeInode("file"))
149 "bsdcpio: file: large inode number truncated: "
150 "Numerical result out of range\n",
151 sizeof(result) - strlen(result) -1);
153 /* hardlink to above file. */
154 assertMakeHardlink("linkfile", "file");
155 fprintf(filelist, "linkfile\n");
156 if (is_LargeInode("linkfile"))
158 "bsdcpio: linkfile: large inode number truncated: "
159 "Numerical result out of range\n",
160 sizeof(result) - strlen(result) -1);
162 /* Symlink to above file. */
164 assertMakeSymlink("symlink", "file");
165 fprintf(filelist, "symlink\n");
166 if (is_LargeInode("symlink"))
168 "bsdcpio: symlink: large inode number truncated: "
169 "Numerical result out of range\n",
170 sizeof(result) - strlen(result) -1);
173 /* Another file with different permissions. */
174 assertMakeFile("file2", 0777, "1234567890");
175 fprintf(filelist, "file2\n");
176 if (is_LargeInode("file2"))
178 "bsdcpio: file2: large inode number truncated: "
179 "Numerical result out of range\n",
180 sizeof(result) - strlen(result) -1);
183 assertMakeDir("dir", 0775);
184 fprintf(filelist, "dir\n");
185 if (is_LargeInode("dir"))
187 "bsdcpio: dir: large inode number truncated: "
188 "Numerical result out of range\n",
189 sizeof(result) - strlen(result) -1);
190 strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
197 /* Archive/dearchive with a variety of options. */
198 msg = canSymlink() ? "2 blocks\n" : "1 block\n";
199 basic_cpio("copy", "", "", msg, msg);
200 basic_cpio("copy_odc", "--format=odc", "", msg, msg);
201 basic_cpio("copy_newc", "-H newc", "", result, "2 blocks\n");
202 basic_cpio("copy_cpio", "-H odc", "", msg, msg);
203 msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";
204 basic_cpio("copy_ustar", "-H ustar", "", msg, msg);
206 /* Copy in one step using -p */
207 passthrough("passthrough");