test_option_c.c revision 228753
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__FBSDID("$FreeBSD$");
27
28static int
29is_octal(const char *p, size_t l)
30{
31	while (l > 0) {
32		if (*p < '0' || *p > '7')
33			return (0);
34		--l;
35		++p;
36	}
37	return (1);
38}
39
40static int
41from_octal(const char *p, size_t l)
42{
43	int r = 0;
44
45	while (l > 0) {
46		r *= 8;
47		r += *p - '0';
48		--l;
49		++p;
50	}
51	return (r);
52}
53
54DEFINE_TEST(test_option_c)
55{
56	FILE *filelist;
57	int r;
58	int uid = -1;
59	int dev, ino, gid;
60	time_t t, now;
61	char *p, *e;
62	size_t s;
63
64	assertUmask(0);
65
66#if !defined(_WIN32)
67	uid = getuid();
68#endif
69
70	/*
71	 * Create an assortment of files.
72	 * TODO: Extend this to cover more filetypes.
73	 */
74	filelist = fopen("filelist", "w");
75
76	/* "file" */
77	assertMakeFile("file", 0644, "1234567890");
78	fprintf(filelist, "file\n");
79
80	/* "symlink" */
81	if (canSymlink()) {
82		assertMakeSymlink("symlink", "file");
83		fprintf(filelist, "symlink\n");
84	}
85
86	/* "dir" */
87	assertMakeDir("dir", 0775);
88	/* Record some facts about what we just created: */
89	now = time(NULL); /* They were all created w/in last two seconds. */
90	fprintf(filelist, "dir\n");
91
92	/* Use the cpio program to create an archive. */
93	fclose(filelist);
94	r = systemf("%s -oc <filelist >basic.out 2>basic.err", testprog);
95	/* Verify that nothing went to stderr. */
96	assertTextFileContents("1 block\n", "basic.err");
97
98	/* Assert that the program finished. */
99	failure("%s -oc crashed", testprog);
100	if (!assertEqualInt(r, 0))
101		return;
102
103	/* Verify that stdout is a well-formed cpio file in "odc" format. */
104	p = slurpfile(&s, "basic.out");
105	assertEqualInt(s, 512);
106	e = p;
107
108	/*
109	 * Some of these assertions could be stronger, but it's
110	 * a little tricky because they depend on the local environment.
111	 */
112
113	/* First entry is "file" */
114	assert(is_octal(e, 76)); /* Entire header is octal digits. */
115	assertEqualMem(e + 0, "070707", 6); /* Magic */
116	assert(is_octal(e + 6, 6)); /* dev */
117	dev = from_octal(e + 6, 6);
118	assert(is_octal(e + 12, 6)); /* ino */
119	ino = from_octal(e + 12, 6);
120#if defined(_WIN32) && !defined(__CYGWIN__)
121	/* Group members bits and others bits do not work. */
122	assertEqualMem(e + 18, "100666", 6); /* Mode */
123#else
124	assertEqualMem(e + 18, "100644", 6); /* Mode */
125#endif
126	if (uid < 0)
127		uid = from_octal(e + 24, 6);
128	assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
129	assert(is_octal(e + 30, 6)); /* gid */
130	gid = from_octal(e + 30, 6);
131	assertEqualMem(e + 36, "000001", 6); /* nlink */
132	failure("file entries should not have rdev set (dev field was 0%o)",
133	    dev);
134	assertEqualMem(e + 42, "000000", 6); /* rdev */
135	t = from_octal(e + 48, 11); /* mtime */
136	assert(t <= now); /* File wasn't created in future. */
137	assert(t >= now - 2); /* File was created w/in last 2 secs. */
138	assertEqualMem(e + 59, "000005", 6); /* Name size */
139	assertEqualMem(e + 65, "00000000012", 11); /* File size */
140	assertEqualMem(e + 76, "file\0", 5); /* Name contents */
141	assertEqualMem(e + 81, "1234567890", 10); /* File contents */
142	e += 91;
143
144	/* "symlink" pointing to "file" */
145	if (canSymlink()) {
146		assert(is_octal(e, 76)); /* Entire header is octal digits. */
147		assertEqualMem(e + 0, "070707", 6); /* Magic */
148		assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */
149		assert(ino != from_octal(e + 12, 6)); /* ino */
150#if !defined(_WIN32) || defined(__CYGWIN__)
151		/* On Windows, symbolic link and group members bits and
152		 * others bits do not work. */
153		assertEqualMem(e + 18, "120777", 6); /* Mode */
154#endif
155		assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
156		assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */
157		assertEqualMem(e + 36, "000001", 6); /* nlink */
158		failure("file entries should have rdev == 0 (dev was 0%o)",
159		    from_octal(e + 6, 6));
160		assertEqualMem(e + 42, "000000", 6); /* rdev */
161		t = from_octal(e + 48, 11); /* mtime */
162		assert(t <= now); /* File wasn't created in future. */
163		assert(t >= now - 2); /* File was created w/in last 2 secs. */
164		assertEqualMem(e + 59, "000010", 6); /* Name size */
165		assertEqualMem(e + 65, "00000000004", 11); /* File size */
166		assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
167		assertEqualMem(e + 84, "file", 4); /* Symlink target. */
168		e += 88;
169	}
170
171	/* "dir" */
172	assert(is_octal(e, 76));
173	assertEqualMem(e + 0, "070707", 6); /* Magic */
174	/* Dev should be same as first entry. */
175	assert(is_octal(e + 6, 6)); /* dev */
176	assertEqualInt(dev, from_octal(e + 6, 6));
177	/* Ino must be different from first entry. */
178	assert(is_octal(e + 12, 6)); /* ino */
179	assert(ino != from_octal(e + 12, 6));
180#if defined(_WIN32) && !defined(__CYGWIN__)
181	/* Group members bits and others bits do not work. */
182	assertEqualMem(e + 18, "040777", 6); /* Mode */
183#else
184	/* Accept 042775 to accomodate systems where sgid bit propagates. */
185	if (memcmp(e + 18, "042775", 6) != 0)
186		assertEqualMem(e + 18, "040775", 6); /* Mode */
187#endif
188	assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
189	/* Gid should be same as first entry. */
190	assert(is_octal(e + 30, 6)); /* gid */
191	assertEqualInt(gid, from_octal(e + 30, 6));
192#ifndef NLINKS_INACCURATE_FOR_DIRS
193	assertEqualMem(e + 36, "000002", 6); /* Nlink */
194#endif
195	t = from_octal(e + 48, 11); /* mtime */
196	assert(t <= now); /* File wasn't created in future. */
197	assert(t >= now - 2); /* File was created w/in last 2 secs. */
198	assertEqualMem(e + 59, "000004", 6); /* Name size */
199	assertEqualMem(e + 65, "00000000000", 11); /* File size */
200	assertEqualMem(e + 76, "dir\0", 4); /* name */
201	e += 80;
202
203	/* TODO: Verify other types of entries. */
204
205	/* Last entry is end-of-archive marker. */
206	assert(is_octal(e, 76));
207	assertEqualMem(e + 0, "070707", 6); /* Magic */
208	assertEqualMem(e + 6, "000000", 6); /* dev */
209	assertEqualMem(e + 12, "000000", 6); /* ino */
210	assertEqualMem(e + 18, "000000", 6); /* Mode */
211	assertEqualMem(e + 24, "000000", 6); /* uid */
212	assertEqualMem(e + 30, "000000", 6); /* gid */
213	assertEqualMem(e + 36, "000001", 6); /* Nlink */
214	assertEqualMem(e + 42, "000000", 6); /* rdev */
215	assertEqualMem(e + 48, "00000000000", 11); /* mtime */
216	assertEqualMem(e + 59, "000013", 6); /* Name size */
217	assertEqualMem(e + 65, "00000000000", 11); /* File size */
218	assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
219
220	free(p);
221}
222