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 
28 static int
29 is_null(const char *p, size_t l)
30 {
31 	while (l > 0) {
32 		if (*p != '\0')
33 			return (0);
34 		--l;
35 		++p;
36 	}
37 	return (1);
38 }
39 
40 /* Verify the contents, then erase them to NUL bytes. */
41 /* Tar requires all "unused" bytes be set to NUL; this allows us
42  * to easily verify that by invoking is_null() over the entire header
43  * after verifying each field. */
44 #define myAssertEqualMem(a,b,s) assertEqualMem(a, b, s); memset(a, 0, s)
45 
46 /*
47  * Detailed verification that 'ustar' archives are written with
48  * the correct format.
49  */
50 DEFINE_TEST(test_write_format_tar_ustar)
51 {
52 	struct archive *a;
53 	struct archive_entry *entry;
54 	char *buff, *e;
55 	size_t buffsize = 100000;
56 	size_t used;
57 	int i;
58 	char f99[100];
59 	char f100[101];
60 	char f256[257];
61 
62 	for (i = 0; i < 99; ++i)
63 		f99[i] = 'a' + i % 26;
64 	f99[99] = '\0';
65 
66 	for (i = 0; i < 100; ++i)
67 		f100[i] = 'A' + i % 26;
68 	f100[100] = '\0';
69 
70 	for (i = 0; i < 256; ++i)
71 		f256[i] = 'A' + i % 26;
72 	f256[155] = '/';
73 	f256[256] = '\0';
74 
75 	buff = malloc(buffsize);
76 
77 	/* Create a new archive in memory. */
78 	assert((a = archive_write_new()) != NULL);
79 	assertEqualIntA(a, ARCHIVE_OK,
80 	    archive_write_set_format_ustar(a));
81 	assertEqualIntA(a, ARCHIVE_OK,
82 	    archive_write_add_filter_none(a));
83 	assertEqualIntA(a, ARCHIVE_OK,
84 	    archive_write_open_memory(a, buff, buffsize, &used));
85 
86 	/*
87 	 * Add various files to it.
88 	 * TODO: Extend this to cover more filetypes.
89 	 */
90 
91 	/* "file" with 10 bytes of content */
92 	assert((entry = archive_entry_new()) != NULL);
93 	archive_entry_set_mtime(entry, 1, 10);
94 	archive_entry_set_pathname(entry, "file");
95 	archive_entry_set_mode(entry, S_IFREG | 0664);
96 	archive_entry_set_size(entry, 10);
97 	archive_entry_set_uid(entry, 80);
98 	archive_entry_set_gid(entry, 90);
99 	archive_entry_set_dev(entry, 12);
100 	archive_entry_set_ino(entry, 89);
101 	archive_entry_set_nlink(entry, 2);
102 	assertEqualIntA(a, ARCHIVE_OK,
103 	    archive_write_header(a, entry));
104 	archive_entry_free(entry);
105 	assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
106 
107 	/* Hardlink to "file" with 10 bytes of content */
108 	assert((entry = archive_entry_new()) != NULL);
109 	archive_entry_set_mtime(entry, 1, 10);
110 	archive_entry_set_pathname(entry, "linkfile");
111 	archive_entry_set_mode(entry, S_IFREG | 0664);
112 	/* TODO: Put this back and fix the bug. */
113 	/* archive_entry_set_size(entry, 10); */
114 	archive_entry_set_uid(entry, 80);
115 	archive_entry_set_gid(entry, 90);
116 	archive_entry_set_dev(entry, 12);
117 	archive_entry_set_ino(entry, 89);
118 	archive_entry_set_nlink(entry, 2);
119 	assertEqualIntA(a, ARCHIVE_OK,
120 	    archive_write_header(a, entry));
121 	archive_entry_free(entry);
122 	/* Write of data to dir should fail == zero bytes get written. */
123 	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
124 
125 	/* "dir" */
126 	assert((entry = archive_entry_new()) != NULL);
127 	archive_entry_set_mtime(entry, 2, 20);
128 	archive_entry_set_pathname(entry, "dir");
129 	archive_entry_set_mode(entry, S_IFDIR | 0775);
130 	archive_entry_set_size(entry, 10);
131 	archive_entry_set_nlink(entry, 2);
132 	assertEqualIntA(a, ARCHIVE_OK,
133 	    archive_write_header(a, entry));
134 	archive_entry_free(entry);
135 	/* Write of data to dir should fail == zero bytes get written. */
136 	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
137 
138 	/* "symlink" pointing to "file" */
139 	assert((entry = archive_entry_new()) != NULL);
140 	archive_entry_set_mtime(entry, 3, 30);
141 	archive_entry_set_pathname(entry, "symlink");
142 	archive_entry_set_mode(entry, 0664);
143 	archive_entry_set_filetype(entry, AE_IFLNK);
144 	archive_entry_set_symlink(entry,"file");
145 	archive_entry_set_size(entry, 0);
146 	archive_entry_set_uid(entry, 88);
147 	archive_entry_set_gid(entry, 98);
148 	archive_entry_set_dev(entry, 12);
149 	archive_entry_set_ino(entry, 90);
150 	archive_entry_set_nlink(entry, 1);
151 	assertEqualIntA(a, ARCHIVE_OK,
152 	    archive_write_header(a, entry));
153 	archive_entry_free(entry);
154 	/* Write of data to symlink should fail == zero bytes get written. */
155 	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
156 
157 	/* file with 99-char filename. */
158 	assert((entry = archive_entry_new()) != NULL);
159 	archive_entry_set_mtime(entry, 1, 10);
160 	archive_entry_set_pathname(entry, f99);
161 	archive_entry_set_mode(entry, S_IFREG | 0664);
162 	archive_entry_set_size(entry, 0);
163 	archive_entry_set_uid(entry, 82);
164 	archive_entry_set_gid(entry, 93);
165 	archive_entry_set_dev(entry, 102);
166 	archive_entry_set_ino(entry, 7);
167 	archive_entry_set_nlink(entry, 1);
168 	assertEqualIntA(a, ARCHIVE_OK,
169 	    archive_write_header(a, entry));
170 	archive_entry_free(entry);
171 
172 	/* file with 100-char filename. */
173 	assert((entry = archive_entry_new()) != NULL);
174 	archive_entry_set_mtime(entry, 1, 10);
175 	archive_entry_set_pathname(entry, f100);
176 	archive_entry_set_mode(entry, S_IFREG | 0664);
177 	archive_entry_set_size(entry, 0);
178 	archive_entry_set_uid(entry, 82);
179 	archive_entry_set_gid(entry, 93);
180 	archive_entry_set_dev(entry, 102);
181 	archive_entry_set_ino(entry, 7);
182 	archive_entry_set_nlink(entry, 1);
183 	assertEqualIntA(a, ARCHIVE_OK,
184 	    archive_write_header(a, entry));
185 	archive_entry_free(entry);
186 
187 	/* file with 256-char filename. */
188 	assert((entry = archive_entry_new()) != NULL);
189 	archive_entry_set_mtime(entry, 1, 10);
190 	archive_entry_set_pathname(entry, f256);
191 	archive_entry_set_mode(entry, S_IFREG | 0664);
192 	archive_entry_set_size(entry, 0);
193 	archive_entry_set_uid(entry, 82);
194 	archive_entry_set_gid(entry, 93);
195 	archive_entry_set_dev(entry, 102);
196 	archive_entry_set_ino(entry, 7);
197 	archive_entry_set_nlink(entry, 1);
198 	assertEqualIntA(a, ARCHIVE_OK,
199 	    archive_write_header(a, entry));
200 	archive_entry_free(entry);
201 
202 	/* Close out the archive. */
203 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
204 
205 	/*
206 	 * Verify the archive format.
207 	 */
208 	e = buff;
209 
210 	/* "file" */
211 	myAssertEqualMem(e + 0, "file", 5); /* Filename */
212 	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
213 	myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
214 	myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
215 	myAssertEqualMem(e + 124, "00000000012 ", 12); /* size */
216 	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
217 	myAssertEqualMem(e + 148, "010034\0 ", 8); /* checksum */
218 	myAssertEqualMem(e + 156, "0", 1); /* linkflag */
219 	myAssertEqualMem(e + 157, "", 1); /* linkname */
220 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
221 	myAssertEqualMem(e + 265, "", 1); /* uname */
222 	myAssertEqualMem(e + 297, "", 1); /* gname */
223 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
224 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
225 	myAssertEqualMem(e + 345, "", 1); /* prefix */
226 	assert(is_null(e + 0, 512));
227 	myAssertEqualMem(e + 512, "1234567890", 10);
228 	assert(is_null(e + 512, 512));
229 	e += 1024;
230 
231 	/* hardlink to "file" */
232 	myAssertEqualMem(e + 0, "linkfile", 9); /* Filename */
233 	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
234 	myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
235 	myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
236 	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
237 	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
238 	myAssertEqualMem(e + 148, "010707\0 ", 8); /* checksum */
239 	myAssertEqualMem(e + 156, "0", 1); /* linkflag */
240 	myAssertEqualMem(e + 157, "", 1); /* linkname */
241 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
242 	myAssertEqualMem(e + 265, "", 1); /* uname */
243 	myAssertEqualMem(e + 297, "", 1); /* gname */
244 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
245 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
246 	myAssertEqualMem(e + 345, "", 1); /* prefix */
247 	assert(is_null(e + 0, 512));
248 	e += 512;
249 
250 	/* "dir" */
251 	myAssertEqualMem(e + 0, "dir/", 4); /* Filename */
252 	myAssertEqualMem(e + 100, "000775 ", 8); /* mode */
253 	myAssertEqualMem(e + 108, "000000 ", 8); /* uid */
254 	myAssertEqualMem(e + 116, "000000 ", 8); /* gid */
255 	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
256 	myAssertEqualMem(e + 136, "00000000002 ", 12); /* mtime */
257 	myAssertEqualMem(e + 148, "007747\0 ", 8); /* checksum */
258 	myAssertEqualMem(e + 156, "5", 1); /* typeflag */
259 	myAssertEqualMem(e + 157, "", 1); /* linkname */
260 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
261 	myAssertEqualMem(e + 265, "", 1); /* uname */
262 	myAssertEqualMem(e + 297, "", 1); /* gname */
263 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
264 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
265 	myAssertEqualMem(e + 345, "", 1); /* prefix */
266 	assert(is_null(e + 0, 512));
267 	e += 512;
268 
269 	/* "symlink" pointing to "file" */
270 	myAssertEqualMem(e + 0, "symlink", 8); /* Filename */
271 	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
272 	myAssertEqualMem(e + 108, "000130 ", 8); /* uid */
273 	myAssertEqualMem(e + 116, "000142 ", 8); /* gid */
274 	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
275 	myAssertEqualMem(e + 136, "00000000003 ", 12); /* mtime */
276 	myAssertEqualMem(e + 148, "011446\0 ", 8); /* checksum */
277 	myAssertEqualMem(e + 156, "2", 1); /* linkflag */
278 	myAssertEqualMem(e + 157, "file", 5); /* linkname */
279 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
280 	myAssertEqualMem(e + 265, "", 1); /* uname */
281 	myAssertEqualMem(e + 297, "", 1); /* gname */
282 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
283 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
284 	myAssertEqualMem(e + 345, "", 1); /* prefix */
285 	assert(is_null(e + 0, 512));
286 	e += 512;
287 
288 	/* File with 99-char filename */
289 	myAssertEqualMem(e + 0, f99, 100); /* Filename */
290 	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
291 	myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
292 	myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
293 	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
294 	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
295 	myAssertEqualMem(e + 148, "034242\0 ", 8); /* checksum */
296 	myAssertEqualMem(e + 156, "0", 1); /* linkflag */
297 	myAssertEqualMem(e + 157, "", 1); /* linkname */
298 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
299 	myAssertEqualMem(e + 265, "", 1); /* uname */
300 	myAssertEqualMem(e + 297, "", 1); /* gname */
301 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
302 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
303 	myAssertEqualMem(e + 345, "", 1); /* prefix */
304 	assert(is_null(e + 0, 512));
305 	e += 512;
306 
307 	/* File with 100-char filename */
308 	myAssertEqualMem(e + 0, f100, 100); /* Filename */
309 	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
310 	myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
311 	myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
312 	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
313 	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
314 	myAssertEqualMem(e + 148, "026230\0 ", 8); /* checksum */
315 	myAssertEqualMem(e + 156, "0", 1); /* linkflag */
316 	myAssertEqualMem(e + 157, "", 1); /* linkname */
317 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
318 	myAssertEqualMem(e + 265, "", 1); /* uname */
319 	myAssertEqualMem(e + 297, "", 1); /* gname */
320 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
321 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
322 	myAssertEqualMem(e + 345, "", 1); /* prefix */
323 	assert(is_null(e + 0, 512));
324 	e += 512;
325 
326 	/* File with 256-char filename */
327 	myAssertEqualMem(e + 0, f256 + 156, 100); /* Filename */
328 	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
329 	myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
330 	myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
331 	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
332 	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
333 	myAssertEqualMem(e + 148, "055570\0 ", 8); /* checksum */
334 	myAssertEqualMem(e + 156, "0", 1); /* linkflag */
335 	myAssertEqualMem(e + 157, "", 1); /* linkname */
336 	myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
337 	myAssertEqualMem(e + 265, "", 1); /* uname */
338 	myAssertEqualMem(e + 297, "", 1); /* gname */
339 	myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
340 	myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
341 	myAssertEqualMem(e + 345, f256, 155); /* prefix */
342 	assert(is_null(e + 0, 512));
343 	e += 512;
344 
345 	/* TODO: Verify other types of entries. */
346 
347 	/* Last entry is end-of-archive marker. */
348 	assert(is_null(e, 1024));
349 	e += 1024;
350 
351 	assertEqualInt((int)used, e - buff);
352 
353 	free(buff);
354 }
355