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'))
52 /* Convert up to 8 hex characters to unsigned 32-bit decimal integer */
54 from_hex(const char *p, size_t l)
60 if (*p >= 'a' && *p <= 'f')
62 else if (*p >= 'A' && *p <= 'F')
72 #if !defined(_WIN32) || defined(__CYGWIN__)
77 assertEqualInt(0, stat(p, &st));
82 DEFINE_TEST(test_format_newc)
86 uint32_t devmajor, devminor, ino, gid, uid;
100 * Create an assortment of files.
101 * TODO: Extend this to cover more filetypes.
103 list = fopen("list", "w");
106 assertMakeFile("file1", 0644, "1234567890");
107 fprintf(list, "file1\n");
110 assertMakeHardlink("hardlink", "file1");
111 fprintf(list, "hardlink\n");
113 /* Another hardlink, but this one won't be archived. */
114 assertMakeHardlink("hardlink2", "file1");
118 assertMakeSymlink("symlink", "file1", 0);
119 fprintf(list, "symlink\n");
123 assertMakeDir("dir", 0775);
124 fprintf(list, "dir\n");
126 /* Setup result message. */
127 memset(result, 0, sizeof(result));
128 if (is_LargeInode("file1")) {
130 "bsdcpio: file1: large inode number truncated: ",
131 sizeof(result) - strlen(result) -1);
132 strncat(result, strerror(ERANGE),
133 sizeof(result) - strlen(result) -1);
134 strncat(result, "\n",
135 sizeof(result) - strlen(result) -1);
137 if (canSymlink() && is_LargeInode("symlink")) {
139 "bsdcpio: symlink: large inode number truncated: ",
140 sizeof(result) - strlen(result) -1);
141 strncat(result, strerror(ERANGE),
142 sizeof(result) - strlen(result) -1);
143 strncat(result, "\n",
144 sizeof(result) - strlen(result) -1);
146 if (is_LargeInode("dir")) {
148 "bsdcpio: dir: large inode number truncated: ",
149 sizeof(result) - strlen(result) -1);
150 strncat(result, strerror(ERANGE),
151 sizeof(result) - strlen(result) -1);
152 strncat(result, "\n",
153 sizeof(result) - strlen(result) -1);
155 if (is_LargeInode("hardlink")) {
157 "bsdcpio: hardlink: large inode number truncated: ",
158 sizeof(result) - strlen(result) -1);
159 strncat(result, strerror(ERANGE),
160 sizeof(result) - strlen(result) -1);
161 strncat(result, "\n",
162 sizeof(result) - strlen(result) -1);
165 /* Record some facts about what we just created: */
166 now = time(NULL); /* They were all created w/in last two seconds. */
168 /* Use the cpio program to create an archive. */
170 r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
172 if (!assertEqualInt(r, 0))
175 /* Verify that nothing went to stderr. */
177 strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
179 strncat(result, "1 block\n", sizeof(result) - strlen(result) -1);
181 assertTextFileContents(result, "newc.err");
183 /* Verify that stdout is a well-formed cpio file in "newc" format. */
184 p = slurpfile(&s, "newc.out");
185 assertEqualInt(s, canSymlink() ? 1024 : 512);
189 * Some of these assertions could be stronger, but it's
190 * a little tricky because they depend on the local environment.
193 /* First entry is "file1" */
194 assert(is_hex(e, 110)); /* Entire header is octal digits. */
195 assertEqualMem(e + 0, "070701", 6); /* Magic */
196 ino = from_hex(e + 6, 8); /* ino */
197 #if defined(_WIN32) && !defined(__CYGWIN__)
198 /* Group members bits and others bits do not work. */
199 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
201 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
204 uid = from_hex(e + 22, 8);
206 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
208 gid = from_hex(e + 30, 8); /* gid */
209 assertEqualMem(e + 38, "00000003", 8); /* nlink */
210 t = from_hex(e + 46, 8); /* mtime */
211 failure("t=%#08jx now=%#08jx=%jd", (intmax_t)t, (intmax_t)now,
213 assert(t <= now); /* File wasn't created in future. */
214 failure("t=%#08jx now - 2=%#08jx=%jd", (intmax_t)t, (intmax_t)now - 2,
216 assert(t >= now - 2); /* File was created w/in last 2 secs. */
217 failure("newc format stores body only with last appearance of a link\n"
218 " first appearance should be empty, so this file size\n"
219 " field should be zero");
220 assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
221 fs = (uint64_t)from_hex(e + 54, 8);
223 devmajor = from_hex(e + 62, 8); /* devmajor */
224 devminor = from_hex(e + 70, 8); /* devminor */
225 assert(is_hex(e + 78, 8)); /* rdevmajor */
226 assert(is_hex(e + 86, 8)); /* rdevminor */
227 assertEqualMem(e + 94, "00000006", 8); /* Name size */
228 ns = (uint64_t)from_hex(e + 94, 8);
229 ns += PAD(ns + 2, 4);
230 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
231 assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
232 /* Since there's another link, no file contents here. */
233 /* But add in file size so that an error here doesn't cascade. */
237 /* "symlink" pointing to "file1" */
238 assert(is_hex(e, 110));
239 assertEqualMem(e + 0, "070701", 6); /* Magic */
240 assert(is_hex(e + 6, 8)); /* ino */
241 #if defined(_WIN32) && !defined(CYGWIN)
242 /* Mode: Group members bits and others bits do not work. */
243 assertEqualInt(0xa180, from_hex(e + 14, 8) & 0xffc0);
245 assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
247 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
248 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
249 assertEqualMem(e + 38, "00000001", 8); /* nlink */
250 t2 = from_hex(e + 46, 8); /* mtime */
251 failure("First entry created at t=%#08jx this entry created"
252 " at t2=%#08jx", (intmax_t)t, (intmax_t)t2);
253 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
254 assertEqualMem(e + 54, "00000005", 8); /* File size */
255 fs = (uint64_t)from_hex(e + 54, 8);
257 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
258 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
259 assert(is_hex(e + 78, 8)); /* rdevmajor */
260 assert(is_hex(e + 86, 8)); /* rdevminor */
261 assertEqualMem(e + 94, "00000008", 8); /* Name size */
262 ns = (uint64_t)from_hex(e + 94, 8);
263 ns += PAD(ns + 2, 4);
264 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
265 assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
266 assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
271 assert(is_hex(e, 110));
272 assertEqualMem(e + 0, "070701", 6); /* Magic */
273 assert(is_hex(e + 6, 8)); /* ino */
274 #if defined(_WIN32) && !defined(__CYGWIN__)
275 /* Group members bits and others bits do not work. */
276 assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */
278 /* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */
279 assertEqualInt(040775, from_hex(e + 14, 8) & ~02000);
281 assertEqualInt(uid, from_hex(e + 22, 8)); /* uid */
282 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
283 #if !defined(_WIN32) || defined(__CYGWIN__)
284 assertEqualInt(nlinks("dir"), from_hex(e + 38, 8)); /* nlinks */
286 t2 = from_hex(e + 46, 8); /* mtime */
287 failure("First entry created at t=%#08jx this entry created at"
288 "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
289 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
290 assertEqualMem(e + 54, "00000000", 8); /* File size */
291 fs = (uint64_t)from_hex(e + 54, 8);
293 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
294 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
295 assert(is_hex(e + 78, 8)); /* rdevmajor */
296 assert(is_hex(e + 86, 8)); /* rdevminor */
297 assertEqualMem(e + 94, "00000004", 8); /* Name size */
298 ns = (uint64_t)from_hex(e + 94, 8);
299 ns += PAD(ns + 2, 4);
300 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
301 assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
304 /* Hardlink identical to "file1" */
305 /* Since we only wrote two of the three links to this
306 * file, this link should get deferred by the hardlink logic. */
307 assert(is_hex(e, 110));
308 assertEqualMem(e + 0, "070701", 6); /* Magic */
309 failure("If these aren't the same, then the hardlink detection failed to match them.");
310 assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */
311 #if defined(_WIN32) && !defined(__CYGWIN__)
312 /* Group members bits and others bits do not work. */
313 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
315 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
317 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
318 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
319 assertEqualMem(e + 38, "00000003", 8); /* nlink */
320 t2 = from_hex(e + 46, 8); /* mtime */
321 failure("First entry created at t=%#08jx this entry created at"
322 "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
323 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
324 assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
325 fs = (uint64_t)from_hex(e + 54, 8);
327 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
328 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
329 assert(is_hex(e + 78, 8)); /* rdevmajor */
330 assert(is_hex(e + 86, 8)); /* rdevminor */
331 assertEqualMem(e + 94, "00000009", 8); /* Name size */
332 ns = (uint64_t)from_hex(e + 94, 8);
333 ns += PAD(ns + 2, 4);
334 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
335 assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */
336 assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */
339 /* Last entry is end-of-archive marker. */
340 assert(is_hex(e, 110));
341 assertEqualMem(e + 0, "070701", 6); /* Magic */
342 assertEqualMem(e + 8, "00000000", 8); /* ino */
343 assertEqualMem(e + 14, "00000000", 8); /* mode */
344 assertEqualMem(e + 22, "00000000", 8); /* uid */
345 assertEqualMem(e + 30, "00000000", 8); /* gid */
346 assertEqualMem(e + 38, "00000001", 8); /* nlink */
347 assertEqualMem(e + 46, "00000000", 8); /* mtime */
348 assertEqualMem(e + 54, "00000000", 8); /* size */
349 assertEqualMem(e + 62, "00000000", 8); /* devmajor */
350 assertEqualMem(e + 70, "00000000", 8); /* devminor */
351 assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
352 assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
353 assertEqualInt(11, from_hex(e + 94, 8)); /* name size */
354 assertEqualMem(e + 102, "00000000", 8); /* check field */
355 assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */