]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/test/test_write_format_tar_ustar.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / test / test_write_format_tar_ustar.c
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, 0, archive_write_set_format_ustar(a));
80         assertEqualIntA(a, 0, archive_write_set_compression_none(a));
81         assertEqualIntA(a, 0, archive_write_open_memory(a, buff, buffsize, &used));
82
83         /*
84          * Add various files to it.
85          * TODO: Extend this to cover more filetypes.
86          */
87
88         /* "file" with 10 bytes of content */
89         assert((entry = archive_entry_new()) != NULL);
90         archive_entry_set_mtime(entry, 1, 10);
91         archive_entry_set_pathname(entry, "file");
92         archive_entry_set_mode(entry, S_IFREG | 0664);
93         archive_entry_set_size(entry, 10);
94         archive_entry_set_uid(entry, 80);
95         archive_entry_set_gid(entry, 90);
96         archive_entry_set_dev(entry, 12);
97         archive_entry_set_ino(entry, 89);
98         archive_entry_set_nlink(entry, 2);
99         assertEqualIntA(a, 0, archive_write_header(a, entry));
100         archive_entry_free(entry);
101         assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
102
103         /* Hardlink to "file" with 10 bytes of content */
104         assert((entry = archive_entry_new()) != NULL);
105         archive_entry_set_mtime(entry, 1, 10);
106         archive_entry_set_pathname(entry, "linkfile");
107         archive_entry_set_mode(entry, S_IFREG | 0664);
108         /* TODO: Put this back and fix the bug. */
109         /* archive_entry_set_size(entry, 10); */
110         archive_entry_set_uid(entry, 80);
111         archive_entry_set_gid(entry, 90);
112         archive_entry_set_dev(entry, 12);
113         archive_entry_set_ino(entry, 89);
114         archive_entry_set_nlink(entry, 2);
115         assertEqualIntA(a, 0, archive_write_header(a, entry));
116         archive_entry_free(entry);
117         /* Write of data to dir should fail == zero bytes get written. */
118         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
119
120         /* "dir" */
121         assert((entry = archive_entry_new()) != NULL);
122         archive_entry_set_mtime(entry, 2, 20);
123         archive_entry_set_pathname(entry, "dir");
124         archive_entry_set_mode(entry, S_IFDIR | 0775);
125         archive_entry_set_size(entry, 10);
126         archive_entry_set_nlink(entry, 2);
127         assertEqualIntA(a, 0, archive_write_header(a, entry));
128         archive_entry_free(entry);
129         /* Write of data to dir should fail == zero bytes get written. */
130         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
131
132         /* "symlink" pointing to "file" */
133         assert((entry = archive_entry_new()) != NULL);
134         archive_entry_set_mtime(entry, 3, 30);
135         archive_entry_set_pathname(entry, "symlink");
136         archive_entry_set_mode(entry, S_IFLNK | 0664);
137         archive_entry_set_symlink(entry,"file");
138         archive_entry_set_size(entry, 0);
139         archive_entry_set_uid(entry, 88);
140         archive_entry_set_gid(entry, 98);
141         archive_entry_set_dev(entry, 12);
142         archive_entry_set_ino(entry, 90);
143         archive_entry_set_nlink(entry, 1);
144         assertEqualIntA(a, 0, archive_write_header(a, entry));
145         archive_entry_free(entry);
146         /* Write of data to symlink should fail == zero bytes get written. */
147         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
148
149         /* file with 99-char filename. */
150         assert((entry = archive_entry_new()) != NULL);
151         archive_entry_set_mtime(entry, 1, 10);
152         archive_entry_set_pathname(entry, f99);
153         archive_entry_set_mode(entry, S_IFREG | 0664);
154         archive_entry_set_size(entry, 0);
155         archive_entry_set_uid(entry, 82);
156         archive_entry_set_gid(entry, 93);
157         archive_entry_set_dev(entry, 102);
158         archive_entry_set_ino(entry, 7);
159         archive_entry_set_nlink(entry, 1);
160         assertEqualIntA(a, 0, archive_write_header(a, entry));
161         archive_entry_free(entry);
162
163         /* file with 100-char filename. */
164         assert((entry = archive_entry_new()) != NULL);
165         archive_entry_set_mtime(entry, 1, 10);
166         archive_entry_set_pathname(entry, f100);
167         archive_entry_set_mode(entry, S_IFREG | 0664);
168         archive_entry_set_size(entry, 0);
169         archive_entry_set_uid(entry, 82);
170         archive_entry_set_gid(entry, 93);
171         archive_entry_set_dev(entry, 102);
172         archive_entry_set_ino(entry, 7);
173         archive_entry_set_nlink(entry, 1);
174         assertEqualIntA(a, 0, archive_write_header(a, entry));
175         archive_entry_free(entry);
176
177         /* file with 256-char filename. */
178         assert((entry = archive_entry_new()) != NULL);
179         archive_entry_set_mtime(entry, 1, 10);
180         archive_entry_set_pathname(entry, f256);
181         archive_entry_set_mode(entry, S_IFREG | 0664);
182         archive_entry_set_size(entry, 0);
183         archive_entry_set_uid(entry, 82);
184         archive_entry_set_gid(entry, 93);
185         archive_entry_set_dev(entry, 102);
186         archive_entry_set_ino(entry, 7);
187         archive_entry_set_nlink(entry, 1);
188         assertEqualIntA(a, 0, archive_write_header(a, entry));
189         archive_entry_free(entry);
190
191 #if ARCHIVE_VERSION_NUMBER < 2000000
192         archive_write_finish(a);
193 #else
194         assert(0 == archive_write_finish(a));
195 #endif
196
197         /*
198          * Verify the archive format.
199          */
200         e = buff;
201
202         /* "file" */
203         myAssertEqualMem(e + 0, "file", 5); /* Filename */
204         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
205         myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
206         myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
207         myAssertEqualMem(e + 124, "00000000012 ", 12); /* size */
208         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
209         myAssertEqualMem(e + 148, "010034\0 ", 8); /* checksum */
210         myAssertEqualMem(e + 156, "0", 1); /* linkflag */
211         myAssertEqualMem(e + 157, "", 1); /* linkname */
212         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
213         myAssertEqualMem(e + 265, "", 1); /* uname */
214         myAssertEqualMem(e + 297, "", 1); /* gname */
215         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
216         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
217         myAssertEqualMem(e + 345, "", 1); /* prefix */
218         assert(is_null(e + 0, 512));
219         myAssertEqualMem(e + 512, "1234567890", 10);
220         assert(is_null(e + 512, 512));
221         e += 1024;
222
223         /* hardlink to "file" */
224         myAssertEqualMem(e + 0, "linkfile", 9); /* Filename */
225         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
226         myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
227         myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
228         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
229         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
230         myAssertEqualMem(e + 148, "010707\0 ", 8); /* checksum */
231         myAssertEqualMem(e + 156, "0", 1); /* linkflag */
232         myAssertEqualMem(e + 157, "", 1); /* linkname */
233         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
234         myAssertEqualMem(e + 265, "", 1); /* uname */
235         myAssertEqualMem(e + 297, "", 1); /* gname */
236         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
237         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
238         myAssertEqualMem(e + 345, "", 1); /* prefix */
239         assert(is_null(e + 0, 512));
240         e += 512;
241
242         /* "dir" */
243         myAssertEqualMem(e + 0, "dir/", 4); /* Filename */
244         myAssertEqualMem(e + 100, "000775 ", 8); /* mode */
245         myAssertEqualMem(e + 108, "000000 ", 8); /* uid */
246         myAssertEqualMem(e + 116, "000000 ", 8); /* gid */
247         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
248         myAssertEqualMem(e + 136, "00000000002 ", 12); /* mtime */
249         myAssertEqualMem(e + 148, "007747\0 ", 8); /* checksum */
250         myAssertEqualMem(e + 156, "5", 1); /* typeflag */
251         myAssertEqualMem(e + 157, "", 1); /* linkname */
252         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
253         myAssertEqualMem(e + 265, "", 1); /* uname */
254         myAssertEqualMem(e + 297, "", 1); /* gname */
255         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
256         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
257         myAssertEqualMem(e + 345, "", 1); /* prefix */
258         assert(is_null(e + 0, 512));
259         e += 512;
260
261         /* "symlink" pointing to "file" */
262         myAssertEqualMem(e + 0, "symlink", 8); /* Filename */
263         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
264         myAssertEqualMem(e + 108, "000130 ", 8); /* uid */
265         myAssertEqualMem(e + 116, "000142 ", 8); /* gid */
266         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
267         myAssertEqualMem(e + 136, "00000000003 ", 12); /* mtime */
268         myAssertEqualMem(e + 148, "011446\0 ", 8); /* checksum */
269         myAssertEqualMem(e + 156, "2", 1); /* linkflag */
270         myAssertEqualMem(e + 157, "file", 5); /* linkname */
271         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
272         myAssertEqualMem(e + 265, "", 1); /* uname */
273         myAssertEqualMem(e + 297, "", 1); /* gname */
274         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
275         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
276         myAssertEqualMem(e + 345, "", 1); /* prefix */
277         assert(is_null(e + 0, 512));
278         e += 512;
279
280         /* File with 99-char filename */
281         myAssertEqualMem(e + 0, f99, 100); /* Filename */
282         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
283         myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
284         myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
285         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
286         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
287         myAssertEqualMem(e + 148, "034242\0 ", 8); /* checksum */
288         myAssertEqualMem(e + 156, "0", 1); /* linkflag */
289         myAssertEqualMem(e + 157, "", 1); /* linkname */
290         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
291         myAssertEqualMem(e + 265, "", 1); /* uname */
292         myAssertEqualMem(e + 297, "", 1); /* gname */
293         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
294         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
295         myAssertEqualMem(e + 345, "", 1); /* prefix */
296         assert(is_null(e + 0, 512));
297         e += 512;
298
299         /* File with 100-char filename */
300         myAssertEqualMem(e + 0, f100, 100); /* Filename */
301         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
302         myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
303         myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
304         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
305         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
306         myAssertEqualMem(e + 148, "026230\0 ", 8); /* checksum */
307         myAssertEqualMem(e + 156, "0", 1); /* linkflag */
308         myAssertEqualMem(e + 157, "", 1); /* linkname */
309         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
310         myAssertEqualMem(e + 265, "", 1); /* uname */
311         myAssertEqualMem(e + 297, "", 1); /* gname */
312         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
313         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
314         myAssertEqualMem(e + 345, "", 1); /* prefix */
315         assert(is_null(e + 0, 512));
316         e += 512;
317
318         /* File with 256-char filename */
319         myAssertEqualMem(e + 0, f256 + 156, 100); /* Filename */
320         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
321         myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
322         myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
323         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
324         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
325         myAssertEqualMem(e + 148, "055570\0 ", 8); /* checksum */
326         myAssertEqualMem(e + 156, "0", 1); /* linkflag */
327         myAssertEqualMem(e + 157, "", 1); /* linkname */
328         myAssertEqualMem(e + 257, "ustar\000000", 8); /* signature/version */
329         myAssertEqualMem(e + 265, "", 1); /* uname */
330         myAssertEqualMem(e + 297, "", 1); /* gname */
331         myAssertEqualMem(e + 329, "000000 ", 8); /* devmajor */
332         myAssertEqualMem(e + 337, "000000 ", 8); /* devminor */
333         myAssertEqualMem(e + 345, f256, 155); /* prefix */
334         assert(is_null(e + 0, 512));
335         e += 512;
336
337         /* TODO: Verify other types of entries. */
338
339         /* Last entry is end-of-archive marker. */
340         assert(is_null(e, 1024));
341         e += 1024;
342
343         assertEqualInt((int)used, e - buff);
344
345         free(buff);
346 }