aboutsummaryrefslogtreecommitdiffstats
path: root/cpio/test/test_format_newc.c
blob: 6e3b9e359bbde3548622b666ee8541abdb811dcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*-
 * Copyright (c) 2003-2007 Tim Kientzle
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "test.h"
__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_format_newc.c,v 1.2 2008/08/22 02:09:10 kientzle Exp $");

/* Number of bytes needed to pad 'n' to multiple of 'block', assuming
 * that 'block' is a power of two. This trick can be more easily
 * remembered as -n & (block - 1), but many compilers quite reasonably
 * warn about "-n" when n is an unsigned value.  (~(n) + 1) is the
 * same thing, but written in a way that won't offend anyone. */
#define PAD(n, block)  ((~(n) + 1) & ((block) - 1))

static int
is_hex(const char *p, size_t l)
{
	while (l > 0) {
		if ((*p >= '0' && *p <= '9')
		    || (*p >= 'a' && *p <= 'f')
		    || (*p >= 'A' && *p <= 'F'))
		{
			--l;
			++p;
		} else
			return (0);

	}
	return (1);
}

/* Convert up to 8 hex characters to unsigned 32-bit decimal integer */
static uint32_t
from_hex(const char *p, size_t l)
{
	uint32_t r = 0;

	while (l > 0) {
		r *= 16;
		if (*p >= 'a' && *p <= 'f')
			r += *p + 10 - 'a';
		else if (*p >= 'A' && *p <= 'F')
			r += *p + 10 - 'A';
		else
			r += *p - '0';
		--l;
		++p;
	}
	return (r);
}

#if !defined(_WIN32) || defined(__CYGWIN__)
static int
nlinks(const char *p)
{
	struct stat st;
	assertEqualInt(0, stat(p, &st));
	return st.st_nlink;
}
#endif

DEFINE_TEST(test_format_newc)
{
	FILE *list;
	int r;
	uint32_t devmajor, devminor, ino, gid, uid;
	time_t t, t2, now;
	char *p, *e;
	size_t s;
	uint64_t fs, ns;
	char result[1024];

	assertUmask(0);

#if !defined(_WIN32)
	uid = getuid();
#endif

	/*
	 * Create an assortment of files.
	 * TODO: Extend this to cover more filetypes.
	 */
	list = fopen("list", "w");

	/* "file1" */
	assertMakeFile("file1", 0644, "1234567890");
	fprintf(list, "file1\n");

	/* "hardlink" */
	assertMakeHardlink("hardlink", "file1");
	fprintf(list, "hardlink\n");

	/* Another hardlink, but this one won't be archived. */
	assertMakeHardlink("hardlink2", "file1");

	/* "symlink" */
	if (canSymlink()) {
		assertMakeSymlink("symlink", "file1", 0);
		fprintf(list, "symlink\n");
	}

	/* "dir" */
	assertMakeDir("dir", 0775);
	fprintf(list, "dir\n");

	/* Setup result message. */
	memset(result, 0, sizeof(result));
	if (is_LargeInode("file1")) {
		strncat(result,
		    "bsdcpio: file1: large inode number truncated: ",
		    sizeof(result) - strlen(result) -1);
		strncat(result, strerror(ERANGE),
		    sizeof(result) - strlen(result) -1);
		strncat(result, "\n",
		    sizeof(result) - strlen(result) -1);
	}
	if (canSymlink() && is_LargeInode("symlink")) {
		strncat(result,
		    "bsdcpio: symlink: large inode number truncated: ",
		    sizeof(result) - strlen(result) -1);
		strncat(result, strerror(ERANGE),
		    sizeof(result) - strlen(result) -1);
		strncat(result, "\n",
		    sizeof(result) - strlen(result) -1);
	}
	if (is_LargeInode("dir")) {
		strncat(result,
		    "bsdcpio: dir: large inode number truncated: ",
		    sizeof(result) - strlen(result) -1);
		strncat(result, strerror(ERANGE),
		    sizeof(result) - strlen(result) -1);
		strncat(result, "\n",
		    sizeof(result) - strlen(result) -1);
	}
	if (is_LargeInode("hardlink")) {
		strncat(result,
		    "bsdcpio: hardlink: large inode number truncated: ",
		    sizeof(result) - strlen(result) -1);
		strncat(result, strerror(ERANGE),
		    sizeof(result) - strlen(result) -1);
		strncat(result, "\n",
		    sizeof(result) - strlen(result) -1);
	}

	/* Record some facts about what we just created: */
	now = time(NULL); /* They were all created w/in last two seconds. */

	/* Use the cpio program to create an archive. */
	fclose(list);
	r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
	    testprog);
	if (!assertEqualInt(r, 0))
		return;

	/* Verify that nothing went to stderr. */
	if (canSymlink()) {
		strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
	} else {
		strncat(result, "1 block\n", sizeof(result) - strlen(result) -1);
	}
	assertTextFileContents(result, "newc.err");

	/* Verify that stdout is a well-formed cpio file in "newc" format. */
	p = slurpfile(&s, "newc.out");
	assertEqualInt(s, canSymlink() ? 1024 : 512);
	e = p;

	/*
	 * Some of these assertions could be stronger, but it's
	 * a little tricky because they depend on the local environment.
	 */

	/* First entry is "file1" */
	assert(is_hex(e, 110)); /* Entire header is octal digits. */
	assertEqualMem(e + 0, "070701", 6); /* Magic */
	ino = from_hex(e + 6, 8); /* ino */
#if defined(_WIN32) && !defined(__CYGWIN__)
	/* Group members bits and others bits do not work. */ 
	assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
#else
	assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
#endif	
#if defined(_WIN32)
	uid = from_hex(e + 22, 8);
#else
	assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
#endif
	gid = from_hex(e + 30, 8); /* gid */
	assertEqualMem(e + 38, "00000003", 8); /* nlink */
	t = from_hex(e + 46, 8); /* mtime */
	failure("t=%#08jx now=%#08jx=%jd", (intmax_t)t, (intmax_t)now,
	    (intmax_t)now);
	assert(t <= now); /* File wasn't created in future. */
	failure("t=%#08jx now - 2=%#08jx=%jd", (intmax_t)t, (intmax_t)now - 2,
	    (intmax_t)now - 2);
	assert(t >= now - 2); /* File was created w/in last 2 secs. */
	failure("newc format stores body only with last appearance of a link\n"
	    "       first appearance should be empty, so this file size\n"
	    "       field should be zero");
	assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
	fs = (uint64_t)from_hex(e + 54, 8);
	fs += PAD(fs, 4);
	devmajor = from_hex(e + 62, 8); /* devmajor */
	devminor = from_hex(e + 70, 8); /* devminor */
	assert(is_hex(e + 78, 8)); /* rdevmajor */
	assert(is_hex(e + 86, 8)); /* rdevminor */
	assertEqualMem(e + 94, "00000006", 8); /* Name size */
	ns = (uint64_t)from_hex(e + 94, 8);
	ns += PAD(ns + 2, 4);
	assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
	assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
	/* Since there's another link, no file contents here. */
	/* But add in file size so that an error here doesn't cascade. */
	e += 110 + fs + ns;

	if (canSymlink()) {
		/* "symlink" pointing to "file1" */
		assert(is_hex(e, 110));
		assertEqualMem(e + 0, "070701", 6); /* Magic */
		assert(is_hex(e + 6, 8)); /* ino */
#if defined(_WIN32) && !defined(CYGWIN)
		/* Mode: Group members bits and others bits do not work. */
		assertEqualInt(0xa180, from_hex(e + 14, 8) & 0xffc0);
#else
		assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
#endif
		assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
		assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
		assertEqualMem(e + 38, "00000001", 8); /* nlink */
		t2 = from_hex(e + 46, 8); /* mtime */
		failure("First entry created at t=%#08jx this entry created"
		    " at t2=%#08jx", (intmax_t)t, (intmax_t)t2);
		assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
		assertEqualMem(e + 54, "00000005", 8); /* File size */
		fs = (uint64_t)from_hex(e + 54, 8);
		fs += PAD(fs, 4);
		assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
		assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
		assert(is_hex(e + 78, 8)); /* rdevmajor */
		assert(is_hex(e + 86, 8)); /* rdevminor */
		assertEqualMem(e + 94, "00000008", 8); /* Name size */
		ns = (uint64_t)from_hex(e + 94, 8);
		ns += PAD(ns + 2, 4);
		assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
		assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
		assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
		e += 110 + fs + ns;
	}

	/* "dir" */
	assert(is_hex(e, 110));
	assertEqualMem(e + 0, "070701", 6); /* Magic */
	assert(is_hex(e + 6, 8)); /* ino */
#if defined(_WIN32) && !defined(__CYGWIN__)
	/* Group members bits and others bits do not work. */
	assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */
#else
	/* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */
	assertEqualInt(040775, from_hex(e + 14, 8) & ~02000);
#endif
	assertEqualInt(uid, from_hex(e + 22, 8)); /* uid */
	assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
#if !defined(_WIN32) || defined(__CYGWIN__)
	assertEqualInt(nlinks("dir"), from_hex(e + 38, 8)); /* nlinks */
#endif
	t2 = from_hex(e + 46, 8); /* mtime */
	failure("First entry created at t=%#08jx this entry created at"
	    "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
	assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
	assertEqualMem(e + 54, "00000000", 8); /* File size */
	fs = (uint64_t)from_hex(e + 54, 8);
	fs += PAD(fs, 4);
	assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
	assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
	assert(is_hex(e + 78, 8)); /* rdevmajor */
	assert(is_hex(e + 86, 8)); /* rdevminor */
	assertEqualMem(e + 94, "00000004", 8); /* Name size */
	ns = (uint64_t)from_hex(e + 94, 8);
	ns += PAD(ns + 2, 4);
	assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
	assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
	e += 110 + fs + ns;

	/* Hardlink identical to "file1" */
	/* Since we only wrote two of the three links to this
	 * file, this link should get deferred by the hardlink logic. */
	assert(is_hex(e, 110));
	assertEqualMem(e + 0, "070701", 6); /* Magic */
	failure("If these aren't the same, then the hardlink detection failed to match them.");
	assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */
#if defined(_WIN32) && !defined(__CYGWIN__)
	/* Group members bits and others bits do not work. */ 
	assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
#else
	assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
#endif
	assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
	assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
	assertEqualMem(e + 38, "00000003", 8); /* nlink */
	t2 = from_hex(e + 46, 8); /* mtime */
	failure("First entry created at t=%#08jx this entry created at"
	    "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
	assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
	assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
	fs = (uint64_t)from_hex(e + 54, 8);
	fs += PAD(fs, 4);
	assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
	assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
	assert(is_hex(e + 78, 8)); /* rdevmajor */
	assert(is_hex(e + 86, 8)); /* rdevminor */
	assertEqualMem(e + 94, "00000009", 8); /* Name size */
	ns = (uint64_t)from_hex(e + 94, 8);
	ns += PAD(ns + 2, 4);
	assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
	assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */
	assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */
	e += 110 + ns + fs;

	/* Last entry is end-of-archive marker. */
	assert(is_hex(e, 110));
	assertEqualMem(e + 0, "070701", 6); /* Magic */
	assertEqualMem(e + 8, "00000000", 8); /* ino */
	assertEqualMem(e + 14, "00000000", 8); /* mode */
	assertEqualMem(e + 22, "00000000", 8); /* uid */
	assertEqualMem(e + 30, "00000000", 8); /* gid */
	assertEqualMem(e + 38, "00000001", 8); /* nlink */
	assertEqualMem(e + 46, "00000000", 8); /* mtime */
	assertEqualMem(e + 54, "00000000", 8); /* size */
	assertEqualMem(e + 62, "00000000", 8); /* devmajor */
	assertEqualMem(e + 70, "00000000", 8); /* devminor */
	assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
	assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
	assertEqualInt(11, from_hex(e + 94, 8)); /* name size */
	assertEqualMem(e + 102, "00000000", 8); /* check field */
	assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */

	free(p);
}