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