xref: /freebsd/contrib/libarchive/cpio/test/test_format_newc.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25 #include "test.h"
26 
27 /* Number of bytes needed to pad 'n' to multiple of 'block', assuming
28  * that 'block' is a power of two. This trick can be more easily
29  * remembered as -n & (block - 1), but many compilers quite reasonably
30  * warn about "-n" when n is an unsigned value.  (~(n) + 1) is the
31  * same thing, but written in a way that won't offend anyone. */
32 #define PAD(n, block)  ((~(n) + 1) & ((block) - 1))
33 
34 static int
35 is_hex(const char *p, size_t l)
36 {
37 	while (l > 0) {
38 		if ((*p >= '0' && *p <= '9')
39 		    || (*p >= 'a' && *p <= 'f')
40 		    || (*p >= 'A' && *p <= 'F'))
41 		{
42 			--l;
43 			++p;
44 		} else
45 			return (0);
46 
47 	}
48 	return (1);
49 }
50 
51 /* Convert up to 8 hex characters to unsigned 32-bit decimal integer */
52 static uint32_t
53 from_hex(const char *p, size_t l)
54 {
55 	uint32_t r = 0;
56 
57 	while (l > 0) {
58 		r *= 16;
59 		if (*p >= 'a' && *p <= 'f')
60 			r += *p + 10 - 'a';
61 		else if (*p >= 'A' && *p <= 'F')
62 			r += *p + 10 - 'A';
63 		else
64 			r += *p - '0';
65 		--l;
66 		++p;
67 	}
68 	return (r);
69 }
70 
71 #if !defined(_WIN32) || defined(__CYGWIN__)
72 static int
73 nlinks(const char *p)
74 {
75 	struct stat st;
76 	assertEqualInt(0, stat(p, &st));
77 	return st.st_nlink;
78 }
79 #endif
80 
81 DEFINE_TEST(test_format_newc)
82 {
83 	FILE *list;
84 	int r;
85 	uint32_t devmajor, devminor, ino, gid, uid;
86 	time_t t, t2, now;
87 	char *p, *e;
88 	size_t s;
89 	uint64_t fs, ns;
90 	char result[1024];
91 
92 	assertUmask(0);
93 
94 #if !defined(_WIN32)
95 	uid = getuid();
96 #endif
97 
98 	/*
99 	 * Create an assortment of files.
100 	 * TODO: Extend this to cover more filetypes.
101 	 */
102 	list = fopen("list", "w");
103 
104 	/* "file1" */
105 	assertMakeFile("file1", 0644, "1234567890");
106 	fprintf(list, "file1\n");
107 
108 	/* "hardlink" */
109 	assertMakeHardlink("hardlink", "file1");
110 	fprintf(list, "hardlink\n");
111 
112 	/* Another hardlink, but this one won't be archived. */
113 	assertMakeHardlink("hardlink2", "file1");
114 
115 	/* "symlink" */
116 	if (canSymlink()) {
117 		assertMakeSymlink("symlink", "file1", 0);
118 		fprintf(list, "symlink\n");
119 	}
120 
121 	/* "dir" */
122 	assertMakeDir("dir", 0775);
123 	fprintf(list, "dir\n");
124 
125 	/* Setup result message. */
126 	memset(result, 0, sizeof(result));
127 	if (is_LargeInode("file1")) {
128 		strncat(result,
129 		    "bsdcpio: file1: large inode number truncated: ",
130 		    sizeof(result) - strlen(result) -1);
131 		strncat(result, strerror(ERANGE),
132 		    sizeof(result) - strlen(result) -1);
133 		strncat(result, "\n",
134 		    sizeof(result) - strlen(result) -1);
135 	}
136 	if (canSymlink() && is_LargeInode("symlink")) {
137 		strncat(result,
138 		    "bsdcpio: symlink: large inode number truncated: ",
139 		    sizeof(result) - strlen(result) -1);
140 		strncat(result, strerror(ERANGE),
141 		    sizeof(result) - strlen(result) -1);
142 		strncat(result, "\n",
143 		    sizeof(result) - strlen(result) -1);
144 	}
145 	if (is_LargeInode("dir")) {
146 		strncat(result,
147 		    "bsdcpio: dir: large inode number truncated: ",
148 		    sizeof(result) - strlen(result) -1);
149 		strncat(result, strerror(ERANGE),
150 		    sizeof(result) - strlen(result) -1);
151 		strncat(result, "\n",
152 		    sizeof(result) - strlen(result) -1);
153 	}
154 	if (is_LargeInode("hardlink")) {
155 		strncat(result,
156 		    "bsdcpio: hardlink: large inode number truncated: ",
157 		    sizeof(result) - strlen(result) -1);
158 		strncat(result, strerror(ERANGE),
159 		    sizeof(result) - strlen(result) -1);
160 		strncat(result, "\n",
161 		    sizeof(result) - strlen(result) -1);
162 	}
163 
164 	/* Record some facts about what we just created: */
165 	now = time(NULL); /* They were all created w/in last two seconds. */
166 
167 	/* Use the cpio program to create an archive. */
168 	fclose(list);
169 	r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
170 	    testprog);
171 	if (!assertEqualInt(r, 0))
172 		return;
173 
174 	/* Verify that nothing went to stderr. */
175 	if (canSymlink()) {
176 		strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
177 	} else {
178 		strncat(result, "1 block\n", sizeof(result) - strlen(result) -1);
179 	}
180 	assertTextFileContents(result, "newc.err");
181 
182 	/* Verify that stdout is a well-formed cpio file in "newc" format. */
183 	p = slurpfile(&s, "newc.out");
184 	assertEqualInt(s, canSymlink() ? 1024 : 512);
185 	e = p;
186 
187 	/*
188 	 * Some of these assertions could be stronger, but it's
189 	 * a little tricky because they depend on the local environment.
190 	 */
191 
192 	/* First entry is "file1" */
193 	assert(is_hex(e, 110)); /* Entire header is octal digits. */
194 	assertEqualMem(e + 0, "070701", 6); /* Magic */
195 	ino = from_hex(e + 6, 8); /* ino */
196 #if defined(_WIN32) && !defined(__CYGWIN__)
197 	/* Group members bits and others bits do not work. */
198 	assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
199 #else
200 	assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
201 #endif
202 #if defined(_WIN32)
203 	uid = from_hex(e + 22, 8);
204 #else
205 	assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
206 #endif
207 	gid = from_hex(e + 30, 8); /* gid */
208 	assertEqualMem(e + 38, "00000003", 8); /* nlink */
209 	t = from_hex(e + 46, 8); /* mtime */
210 	failure("t=%#08jx now=%#08jx=%jd", (intmax_t)t, (intmax_t)now,
211 	    (intmax_t)now);
212 	assert(t <= now); /* File wasn't created in future. */
213 	failure("t=%#08jx now - 2=%#08jx=%jd", (intmax_t)t, (intmax_t)now - 2,
214 	    (intmax_t)now - 2);
215 	assert(t >= now - 2); /* File was created w/in last 2 secs. */
216 	failure("newc format stores body only with last appearance of a link\n"
217 	    "       first appearance should be empty, so this file size\n"
218 	    "       field should be zero");
219 	assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
220 	fs = (uint64_t)from_hex(e + 54, 8);
221 	fs += PAD(fs, 4);
222 	devmajor = from_hex(e + 62, 8); /* devmajor */
223 	devminor = from_hex(e + 70, 8); /* devminor */
224 	assert(is_hex(e + 78, 8)); /* rdevmajor */
225 	assert(is_hex(e + 86, 8)); /* rdevminor */
226 	assertEqualMem(e + 94, "00000006", 8); /* Name size */
227 	ns = (uint64_t)from_hex(e + 94, 8);
228 	ns += PAD(ns + 2, 4);
229 	assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
230 	assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
231 	/* Since there's another link, no file contents here. */
232 	/* But add in file size so that an error here doesn't cascade. */
233 	e += 110 + fs + ns;
234 
235 	if (canSymlink()) {
236 		/* "symlink" pointing to "file1" */
237 		assert(is_hex(e, 110));
238 		assertEqualMem(e + 0, "070701", 6); /* Magic */
239 		assert(is_hex(e + 6, 8)); /* ino */
240 #if defined(_WIN32) && !defined(CYGWIN)
241 		/* Mode: Group members bits and others bits do not work. */
242 		assertEqualInt(0xa180, from_hex(e + 14, 8) & 0xffc0);
243 #else
244 		assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
245 #endif
246 		assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
247 		assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
248 		assertEqualMem(e + 38, "00000001", 8); /* nlink */
249 		t2 = from_hex(e + 46, 8); /* mtime */
250 		failure("First entry created at t=%#08jx this entry created"
251 		    " at t2=%#08jx", (intmax_t)t, (intmax_t)t2);
252 		assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
253 		assertEqualMem(e + 54, "00000005", 8); /* File size */
254 		fs = (uint64_t)from_hex(e + 54, 8);
255 		fs += PAD(fs, 4);
256 		assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
257 		assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
258 		assert(is_hex(e + 78, 8)); /* rdevmajor */
259 		assert(is_hex(e + 86, 8)); /* rdevminor */
260 		assertEqualMem(e + 94, "00000008", 8); /* Name size */
261 		ns = (uint64_t)from_hex(e + 94, 8);
262 		ns += PAD(ns + 2, 4);
263 		assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
264 		assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
265 		assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
266 		e += 110 + fs + ns;
267 	}
268 
269 	/* "dir" */
270 	assert(is_hex(e, 110));
271 	assertEqualMem(e + 0, "070701", 6); /* Magic */
272 	assert(is_hex(e + 6, 8)); /* ino */
273 #if defined(_WIN32) && !defined(__CYGWIN__)
274 	/* Group members bits and others bits do not work. */
275 	assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */
276 #else
277 	/* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */
278 	assertEqualInt(040775, from_hex(e + 14, 8) & ~02000);
279 #endif
280 	assertEqualInt(uid, from_hex(e + 22, 8)); /* uid */
281 	assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
282 #if !defined(_WIN32) || defined(__CYGWIN__)
283 	assertEqualInt(nlinks("dir"), from_hex(e + 38, 8)); /* nlinks */
284 #endif
285 	t2 = from_hex(e + 46, 8); /* mtime */
286 	failure("First entry created at t=%#08jx this entry created at"
287 	    "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
288 	assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
289 	assertEqualMem(e + 54, "00000000", 8); /* File size */
290 	fs = (uint64_t)from_hex(e + 54, 8);
291 	fs += PAD(fs, 4);
292 	assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
293 	assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
294 	assert(is_hex(e + 78, 8)); /* rdevmajor */
295 	assert(is_hex(e + 86, 8)); /* rdevminor */
296 	assertEqualMem(e + 94, "00000004", 8); /* Name size */
297 	ns = (uint64_t)from_hex(e + 94, 8);
298 	ns += PAD(ns + 2, 4);
299 	assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
300 	assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
301 	e += 110 + fs + ns;
302 
303 	/* Hardlink identical to "file1" */
304 	/* Since we only wrote two of the three links to this
305 	 * file, this link should get deferred by the hardlink logic. */
306 	assert(is_hex(e, 110));
307 	assertEqualMem(e + 0, "070701", 6); /* Magic */
308 	failure("If these aren't the same, then the hardlink detection failed to match them.");
309 	assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */
310 #if defined(_WIN32) && !defined(__CYGWIN__)
311 	/* Group members bits and others bits do not work. */
312 	assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
313 #else
314 	assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
315 #endif
316 	assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
317 	assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
318 	assertEqualMem(e + 38, "00000003", 8); /* nlink */
319 	t2 = from_hex(e + 46, 8); /* mtime */
320 	failure("First entry created at t=%#08jx this entry created at"
321 	    "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
322 	assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
323 	assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
324 	fs = (uint64_t)from_hex(e + 54, 8);
325 	fs += PAD(fs, 4);
326 	assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
327 	assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
328 	assert(is_hex(e + 78, 8)); /* rdevmajor */
329 	assert(is_hex(e + 86, 8)); /* rdevminor */
330 	assertEqualMem(e + 94, "00000009", 8); /* Name size */
331 	ns = (uint64_t)from_hex(e + 94, 8);
332 	ns += PAD(ns + 2, 4);
333 	assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
334 	assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */
335 	assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */
336 	e += 110 + ns + fs;
337 
338 	/* Last entry is end-of-archive marker. */
339 	assert(is_hex(e, 110));
340 	assertEqualMem(e + 0, "070701", 6); /* Magic */
341 	assertEqualMem(e + 8, "00000000", 8); /* ino */
342 	assertEqualMem(e + 14, "00000000", 8); /* mode */
343 	assertEqualMem(e + 22, "00000000", 8); /* uid */
344 	assertEqualMem(e + 30, "00000000", 8); /* gid */
345 	assertEqualMem(e + 38, "00000001", 8); /* nlink */
346 	assertEqualMem(e + 46, "00000000", 8); /* mtime */
347 	assertEqualMem(e + 54, "00000000", 8); /* size */
348 	assertEqualMem(e + 62, "00000000", 8); /* devmajor */
349 	assertEqualMem(e + 70, "00000000", 8); /* devminor */
350 	assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
351 	assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
352 	assertEqualInt(11, from_hex(e + 94, 8)); /* name size */
353 	assertEqualMem(e + 102, "00000000", 8); /* check field */
354 	assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */
355 
356 	free(p);
357 }
358