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