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$");
28 /* Number of bytes needed to pad 'n' to multiple of 'block', assuming
29 * that 'block' is a power of two. This trick can be more easily
30 * remembered as -n & (block - 1), but many compilers quite reasonably
31 * warn about "-n" when n is an unsigned value. (~(n) + 1) is the
32 * same thing, but written in a way that won't offend anyone. */
33 #define PAD(n, block) ((~(n) + 1) & ((block) - 1))
36 is_hex(const char *p, size_t l)
39 if ((*p >= '0' && *p <= '9')
40 || (*p >= 'a' && *p <= 'f')
41 || (*p >= 'A' && *p <= 'F'))
53 from_hex(const char *p, size_t l)
59 if (*p >= 'a' && *p <= 'f')
61 else if (*p >= 'A' && *p <= 'F')
71 #if !defined(_WIN32) || defined(__CYGWIN__)
76 assertEqualInt(0, stat(p, &st));
81 DEFINE_TEST(test_format_newc)
85 int devmajor, devminor, ino, gid;
99 * Create an assortment of files.
100 * TODO: Extend this to cover more filetypes.
102 list = fopen("list", "w");
105 assertMakeFile("file1", 0644, "1234567890");
106 fprintf(list, "file1\n");
109 assertMakeHardlink("hardlink", "file1");
110 fprintf(list, "hardlink\n");
112 /* Another hardlink, but this one won't be archived. */
113 assertMakeHardlink("hardlink2", "file1");
117 assertMakeSymlink("symlink", "file1");
118 fprintf(list, "symlink\n");
122 assertMakeDir("dir", 0775);
123 fprintf(list, "dir\n");
125 /* Setup result message. */
126 memset(result, 0, sizeof(result));
127 if (is_LargeInode("file1"))
129 "bsdcpio: file1: large inode number truncated: "
130 "Numerical result out of range\n",
131 sizeof(result) - strlen(result) -1);
132 if (canSymlink() && is_LargeInode("symlink"))
134 "bsdcpio: symlink: large inode number truncated: "
135 "Numerical result out of range\n",
136 sizeof(result) - strlen(result) -1);
137 if (is_LargeInode("dir"))
139 "bsdcpio: dir: large inode number truncated: "
140 "Numerical result out of range\n",
141 sizeof(result) - strlen(result) -1);
142 if (is_LargeInode("hardlink"))
144 "bsdcpio: hardlink: large inode number truncated: "
145 "Numerical result out of range\n",
146 sizeof(result) - strlen(result) -1);
148 /* Record some facts about what we just created: */
149 now = time(NULL); /* They were all created w/in last two seconds. */
151 /* Use the cpio program to create an archive. */
153 r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
155 if (!assertEqualInt(r, 0))
158 /* Verify that nothing went to stderr. */
160 strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
162 strncat(result, "1 block\n", sizeof(result) - strlen(result) -1);
164 assertTextFileContents(result, "newc.err");
166 /* Verify that stdout is a well-formed cpio file in "newc" format. */
167 p = slurpfile(&s, "newc.out");
168 assertEqualInt(s, canSymlink() ? 1024 : 512);
172 * Some of these assertions could be stronger, but it's
173 * a little tricky because they depend on the local environment.
176 /* First entry is "file1" */
177 assert(is_hex(e, 110)); /* Entire header is octal digits. */
178 assertEqualMem(e + 0, "070701", 6); /* Magic */
179 ino = from_hex(e + 6, 8); /* ino */
180 #if defined(_WIN32) && !defined(__CYGWIN__)
181 /* Group members bits and others bits do not work. */
182 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
184 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
187 uid = from_hex(e + 22, 8);
188 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
189 gid = from_hex(e + 30, 8); /* gid */
190 assertEqualMem(e + 38, "00000003", 8); /* nlink */
191 t = from_hex(e + 46, 8); /* mtime */
192 failure("t=0x%08x now=0x%08x=%d", t, now, now);
193 assert(t <= now); /* File wasn't created in future. */
194 failure("t=0x%08x now - 2=0x%08x = %d", t, now - 2, now - 2);
195 assert(t >= now - 2); /* File was created w/in last 2 secs. */
196 failure("newc format stores body only with last appearance of a link\n"
197 " first appearance should be empty, so this file size\n"
198 " field should be zero");
199 assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
200 fs = from_hex(e + 54, 8);
202 devmajor = from_hex(e + 62, 8); /* devmajor */
203 devminor = from_hex(e + 70, 8); /* devminor */
204 assert(is_hex(e + 78, 8)); /* rdevmajor */
205 assert(is_hex(e + 86, 8)); /* rdevminor */
206 assertEqualMem(e + 94, "00000006", 8); /* Name size */
207 ns = from_hex(e + 94, 8);
208 ns += PAD(ns + 2, 4);
209 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
210 assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
211 /* Since there's another link, no file contents here. */
212 /* But add in file size so that an error here doesn't cascade. */
216 /* "symlink" pointing to "file1" */
217 assert(is_hex(e, 110));
218 assertEqualMem(e + 0, "070701", 6); /* Magic */
219 assert(is_hex(e + 6, 8)); /* ino */
220 assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
221 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
222 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
223 assertEqualMem(e + 38, "00000001", 8); /* nlink */
224 t2 = from_hex(e + 46, 8); /* mtime */
225 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
226 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
227 assertEqualMem(e + 54, "00000005", 8); /* File size */
228 fs = from_hex(e + 54, 8);
230 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
231 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
232 assert(is_hex(e + 78, 8)); /* rdevmajor */
233 assert(is_hex(e + 86, 8)); /* rdevminor */
234 assertEqualMem(e + 94, "00000008", 8); /* Name size */
235 ns = from_hex(e + 94, 8);
236 ns += PAD(ns + 2, 4);
237 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
238 assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
239 assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
244 assert(is_hex(e, 110));
245 assertEqualMem(e + 0, "070701", 6); /* Magic */
246 assert(is_hex(e + 6, 8)); /* ino */
247 #if defined(_WIN32) && !defined(__CYGWIN__)
248 /* Group members bits and others bits do not work. */
249 assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */
251 /* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */
252 assertEqualInt(040775, from_hex(e + 14, 8) & ~02000);
254 assertEqualInt(uid, from_hex(e + 22, 8)); /* uid */
255 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
256 #if !defined(_WIN32) || defined(__CYGWIN__)
257 assertEqualInt(nlinks("dir"), from_hex(e + 38, 8)); /* nlinks */
259 t2 = from_hex(e + 46, 8); /* mtime */
260 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
261 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
262 assertEqualMem(e + 54, "00000000", 8); /* File size */
263 fs = from_hex(e + 54, 8);
265 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
266 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
267 assert(is_hex(e + 78, 8)); /* rdevmajor */
268 assert(is_hex(e + 86, 8)); /* rdevminor */
269 assertEqualMem(e + 94, "00000004", 8); /* Name size */
270 ns = from_hex(e + 94, 8);
271 ns += PAD(ns + 2, 4);
272 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
273 assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
276 /* Hardlink identical to "file1" */
277 /* Since we only wrote two of the three links to this
278 * file, this link should get deferred by the hardlink logic. */
279 assert(is_hex(e, 110));
280 assertEqualMem(e + 0, "070701", 6); /* Magic */
281 failure("If these aren't the same, then the hardlink detection failed to match them.");
282 assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */
283 #if defined(_WIN32) && !defined(__CYGWIN__)
284 /* Group members bits and others bits do not work. */
285 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
287 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
289 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
290 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
291 assertEqualMem(e + 38, "00000003", 8); /* nlink */
292 t2 = from_hex(e + 46, 8); /* mtime */
293 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
294 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
295 assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
296 fs = from_hex(e + 54, 8);
298 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
299 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
300 assert(is_hex(e + 78, 8)); /* rdevmajor */
301 assert(is_hex(e + 86, 8)); /* rdevminor */
302 assertEqualMem(e + 94, "00000009", 8); /* Name size */
303 ns = from_hex(e + 94, 8);
304 ns += PAD(ns + 2, 4);
305 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
306 assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */
307 assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */
310 /* Last entry is end-of-archive marker. */
311 assert(is_hex(e, 110));
312 assertEqualMem(e + 0, "070701", 6); /* Magic */
313 assertEqualMem(e + 8, "00000000", 8); /* ino */
314 assertEqualMem(e + 14, "00000000", 8); /* mode */
315 assertEqualMem(e + 22, "00000000", 8); /* uid */
316 assertEqualMem(e + 30, "00000000", 8); /* gid */
317 assertEqualMem(e + 38, "00000001", 8); /* nlink */
318 assertEqualMem(e + 46, "00000000", 8); /* mtime */
319 assertEqualMem(e + 54, "00000000", 8); /* size */
320 assertEqualMem(e + 62, "00000000", 8); /* devmajor */
321 assertEqualMem(e + 70, "00000000", 8); /* devminor */
322 assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
323 assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
324 assertEqualInt(11, from_hex(e + 94, 8)); /* name size */
325 assertEqualMem(e + 102, "00000000", 8); /* check field */
326 assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */