xref: /freebsd/contrib/libarchive/libarchive/test/test_read_format_tar.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
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 /*
28  * Each of these archives is a short archive with a single entry.  The
29  * corresponding verify function verifies the entry structure returned
30  * from libarchive is what it should be.  The support functions pad with
31  * lots of zeros, so we can trim trailing zero bytes from each hardcoded
32  * archive to save space.
33  *
34  * The naming here follows the tar file type flags.  E.g. '1' is a hardlink,
35  * '2' is a symlink, '5' is a dir, etc.
36  */
37 
38 /* Empty archive. */
39 static unsigned char archiveEmpty[] = {
40 	/* 512 zero bytes */
41 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
42 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
43 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
44 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
45 
46 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
47 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
48 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
49 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
50 
51 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
52 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
53 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
54 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
55 
56 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
57 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
58 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
59 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0
60 };
61 
verifyEmpty(void)62 static void verifyEmpty(void)
63 {
64 	struct archive_entry *ae;
65 	struct archive *a;
66 
67 	assert((a = archive_read_new()) != NULL);
68 	assertA(0 == archive_read_support_filter_all(a));
69 	assertA(0 == archive_read_support_format_all(a));
70 	assertA(0 == archive_read_open_memory(a, archiveEmpty, 512));
71 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
72 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_NONE);
73 	assertEqualString(archive_filter_name(a, 0), "none");
74 	failure("512 zero bytes should be recognized as a tar archive.");
75 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR);
76 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
77 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
78 
79 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
80 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
81 }
82 
83 /* Single entry with a hardlink. */
84 static unsigned char archive1[] = {
85 'h','a','r','d','l','i','n','k',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
86 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
87 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0',
88 '0','6','4','4',' ',0,'0','0','1','7','5','0',' ',0,'0','0','1','7','5','0',
89 ' ',0,'0','0','0','0','0','0','0','0','0','0','0',' ','1','0','6','4','6',
90 '0','5','2','6','6','2',' ','0','1','3','0','5','7',0,' ','1','f','i','l',
91 'e',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
92 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
93 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',0,'0',
94 '0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
95 't','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0',
96 '0','0','0','0','0',' ',0,'0','0','0','0','0','0',' '};
97 
verify1(struct archive_entry * ae)98 static void verify1(struct archive_entry *ae)
99 {
100 	/* A hardlink is not a symlink. */
101 	assert(archive_entry_filetype(ae) != AE_IFLNK);
102 	/* Nor is it a directory. */
103 	assert(archive_entry_filetype(ae) != AE_IFDIR);
104 	assertEqualInt(archive_entry_mode(ae) & 0777, 0644);
105 	assertEqualInt(archive_entry_uid(ae), 1000);
106 	assertEqualInt(archive_entry_gid(ae), 1000);
107 	assertEqualString(archive_entry_uname(ae), "tim");
108 	assertEqualString(archive_entry_gname(ae), "tim");
109 	assertEqualString(archive_entry_pathname(ae), "hardlink");
110 	assertEqualString(archive_entry_hardlink(ae), "file");
111 	assert(archive_entry_symlink(ae) == NULL);
112 	assertEqualInt(archive_entry_mtime(ae), 1184388530);
113 }
114 
115 /* Verify that symlinks are read correctly. */
116 static unsigned char archive2[] = {
117 's','y','m','l','i','n','k',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
118 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
119 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0',
120 '0','0','7','5','5',' ','0','0','0','1','7','5','0',' ','0','0','0','1','7',
121 '5','0',' ','0','0','0','0','0','0','0','0','0','0','0',' ','1','0','6','4',
122 '6','0','5','4','1','0','1',' ','0','0','1','3','3','2','3',' ','2','f','i',
123 'l','e',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
124 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
125 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',0,
126 '0','0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
127 0,0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
128 '0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0',' '};
129 
verify2(struct archive_entry * ae)130 static void verify2(struct archive_entry *ae)
131 {
132 	assertEqualInt(archive_entry_filetype(ae), AE_IFLNK);
133 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
134 	assertEqualInt(archive_entry_uid(ae), 1000);
135 	assertEqualInt(archive_entry_gid(ae), 1000);
136 	assertEqualString(archive_entry_uname(ae), "tim");
137 	assertEqualString(archive_entry_gname(ae), "tim");
138 	assertEqualString(archive_entry_pathname(ae), "symlink");
139 	assertEqualString(archive_entry_symlink(ae), "file");
140 	assert(archive_entry_hardlink(ae) == NULL);
141 	assertEqualInt(archive_entry_mtime(ae), 1184389185);
142 }
143 
144 /* Character device node. */
145 static unsigned char archive3[] = {
146 'd','e','v','c','h','a','r',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
147 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
148 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0',
149 '0','0','7','5','5',' ','0','0','0','1','7','5','0',' ','0','0','0','1','7',
150 '5','0',' ','0','0','0','0','0','0','0','0','0','0','0',' ','1','0','6','4',
151 '6','0','5','4','1','0','1',' ','0','0','1','2','4','1','2',' ','3',0,0,
152 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
153 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
154 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',0,
155 '0','0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
156 0,0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
157 '0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0',' '};
158 
verify3(struct archive_entry * ae)159 static void verify3(struct archive_entry *ae)
160 {
161 	assertEqualInt(archive_entry_filetype(ae), AE_IFCHR);
162 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
163 	assertEqualInt(archive_entry_uid(ae), 1000);
164 	assertEqualInt(archive_entry_gid(ae), 1000);
165 	assertEqualString(archive_entry_uname(ae), "tim");
166 	assertEqualString(archive_entry_gname(ae), "tim");
167 	assertEqualString(archive_entry_pathname(ae), "devchar");
168 	assert(archive_entry_symlink(ae) == NULL);
169 	assert(archive_entry_hardlink(ae) == NULL);
170 	assertEqualInt(archive_entry_mtime(ae), 1184389185);
171 }
172 
173 /* Block device node. */
174 static unsigned char archive4[] = {
175 'd','e','v','b','l','o','c','k',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
176 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
177 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0',
178 '0','0','7','5','5',' ','0','0','0','1','7','5','0',' ','0','0','0','1','7',
179 '5','0',' ','0','0','0','0','0','0','0','0','0','0','0',' ','1','0','6','4',
180 '6','0','5','4','1','0','1',' ','0','0','1','2','5','7','0',' ','4',0,0,
181 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
182 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
183 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',0,
184 '0','0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
185 0,0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
186 '0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0',' '};
187 
verify4(struct archive_entry * ae)188 static void verify4(struct archive_entry *ae)
189 {
190 	assertEqualInt(archive_entry_filetype(ae), AE_IFBLK);
191 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
192 	assertEqualInt(archive_entry_uid(ae), 1000);
193 	assertEqualInt(archive_entry_gid(ae), 1000);
194 	assertEqualString(archive_entry_uname(ae), "tim");
195 	assertEqualString(archive_entry_gname(ae), "tim");
196 	assertEqualString(archive_entry_pathname(ae), "devblock");
197 	assert(archive_entry_symlink(ae) == NULL);
198 	assert(archive_entry_hardlink(ae) == NULL);
199 	assertEqualInt(archive_entry_mtime(ae), 1184389185);
200 }
201 
202 /* Directory. */
203 static unsigned char archive5[] = {
204 '.',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
205 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
206 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0','0',
207 '7','5','5',' ',0,'0','0','1','7','5','0',' ',0,'0','0','1','7','5','0',
208 ' ',0,'0','0','0','0','0','0','0','0','0','0','0',' ','1','0','3','3',
209 '4','0','4','1','7','3','6',' ','0','1','0','5','6','1',0,' ','5',0,0,0,
210 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
211 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
212 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',0,
213 '0','0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
214 0,0,0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
215 0,0,'0','0','0','0','0','0',' ',0,'0','0','0','0','0','0',' '};
216 
verify5(struct archive_entry * ae)217 static void verify5(struct archive_entry *ae)
218 {
219 	assertEqualInt(archive_entry_filetype(ae), AE_IFDIR);
220 	assertEqualInt(archive_entry_mtime(ae), 1131430878);
221 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
222 	assertEqualInt(archive_entry_uid(ae), 1000);
223 	assertEqualInt(archive_entry_gid(ae), 1000);
224 	assertEqualString(archive_entry_uname(ae), "tim");
225 	assertEqualString(archive_entry_gname(ae), "tim");
226 }
227 
228 /* fifo */
229 static unsigned char archive6[] = {
230 'f','i','f','o',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
231 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
232 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0',
233 '0','0','7','5','5',' ','0','0','0','1','7','5','0',' ','0','0','0','1','7',
234 '5','0',' ','0','0','0','0','0','0','0','0','0','0','0',' ','1','0','6','4',
235 '6','0','5','4','1','0','1',' ','0','0','1','1','7','2','4',' ','6',0,0,
236 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
237 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
238 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',0,
239 '0','0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
240 0,0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
241 '0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0',' '};
242 
verify6(struct archive_entry * ae)243 static void verify6(struct archive_entry *ae)
244 {
245 	assertEqualInt(archive_entry_filetype(ae), AE_IFIFO);
246 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
247 	assertEqualInt(archive_entry_uid(ae), 1000);
248 	assertEqualInt(archive_entry_gid(ae), 1000);
249 	assertEqualString(archive_entry_uname(ae), "tim");
250 	assertEqualString(archive_entry_gname(ae), "tim");
251 	assertEqualString(archive_entry_pathname(ae), "fifo");
252 	assert(archive_entry_symlink(ae) == NULL);
253 	assert(archive_entry_hardlink(ae) == NULL);
254 	assertEqualInt(archive_entry_mtime(ae), 1184389185);
255 }
256 
257 /* GNU long link name */
258 static unsigned char archiveK[] = {
259 '.','/','.','/','@','L','o','n','g','L','i','n','k',0,0,0,0,0,0,0,0,0,0,0,
260 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
261 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
262 0,0,'0','0','0','0','0','0','0',0,'0','0','0','0','0','0','0',0,'0','0','0',
263 '0','0','0','0',0,'0','0','0','0','0','0','0','0','6','6','6',0,'0','0','0',
264 '0','0','0','0','0','0','0','0',0,'0','1','1','7','1','5',0,' ','K',0,0,0,
265 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
266 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
267 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u','s','t','a','r',' ',' ',
268 0,'r','o','o','t',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
269 'w','h','e','e','l',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
270 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
271 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
272 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
273 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
274 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'t',
275 'h','i','s','_','i','s','_','a','_','v','e','r','y','_','l','o','n','g','_',
276 's','y','m','l','i','n','k','_','b','o','d','y','_','a','b','c','d','e','f',
277 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y',
278 'z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
279 'r','s','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g','h','i',
280 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a',
281 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
282 'u','v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l',
283 'm','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d',
284 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
285 'x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
286 'p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g',
287 'h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
288 '_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
289 's','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j',
290 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b',
291 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
292 'v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m',
293 'n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d','e',
294 'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x',
295 'y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
296 'q','r','s','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g','h',
297 'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',0,
298 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
299 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
300 's','y','m','l','i','n','k',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
301 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
302 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','1',
303 '2','0','7','5','5',0,'0','0','0','1','7','5','0',0,'0','0','0','1','7','5',
304 '0',0,'0','0','0','0','0','0','0','0','0','0','0',0,'1','0','6','4','6','0',
305 '5','6','7','7','0',0,'0','3','5','4','4','7',0,' ','2','t','h','i','s','_',
306 'i','s','_','a','_','v','e','r','y','_','l','o','n','g','_','s','y','m','l',
307 'i','n','k','_','b','o','d','y','_','a','b','c','d','e','f','g','h','i','j',
308 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b',
309 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
310 'v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l',0,
311 'u','s','t','a','r',' ',' ',0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
312 0,0,0,0,0,0,0,0,0,0,0,0,0,'t','i','m'};
313 
verifyK(struct archive_entry * ae)314 static void verifyK(struct archive_entry *ae)
315 {
316 	assertEqualInt(archive_entry_filetype(ae), AE_IFLNK);
317 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
318 	assertEqualInt(archive_entry_uid(ae), 1000);
319 	assertEqualInt(archive_entry_gid(ae), 1000);
320 	assertEqualString(archive_entry_uname(ae), "tim");
321 	assertEqualString(archive_entry_gname(ae), "tim");
322 	assertEqualString(archive_entry_pathname(ae), "symlink");
323 	assertEqualString(archive_entry_symlink(ae),
324 	    "this_is_a_very_long_symlink_body_abcdefghijklmnopqrstuvwxyz_"
325 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
326 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
327 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
328 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
329 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
330 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
331 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz");
332 	assert(archive_entry_hardlink(ae) == NULL);
333 	assertEqualInt(archive_entry_mtime(ae), 1184390648);
334 }
335 
336 /* TODO: GNU long name */
337 
338 /* TODO: Solaris ACL */
339 
340 /* Pax extended long link name */
341 static unsigned char archivexL[] = {
342 '.','/','P','a','x','H','e','a','d','e','r','s','.','8','6','9','7','5','/',
343 's','y','m','l','i','n','k',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
344 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
345 0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0','0','0','6','4','4',0,'0','0','0','1',
346 '7','5','0',0,'0','0','0','1','7','5','0',0,'0','0','0','0','0','0','0','0',
347 '7','5','3',0,'1','0','6','4','6','0','5','7','6','1','1',0,'0','1','3','7',
348 '1','4',0,' ','x',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
349 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
350 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'u',
351 's','t','a','r',0,'0','0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
352 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
353 0,0,0,'0','0','0','0','0','0','0',0,'0','0','0','0','0','0','0',0,0,0,0,0,
354 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
355 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
356 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
357 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
358 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'4','5','1',' ','l','i','n','k','p','a','t',
359 'h','=','t','h','i','s','_','i','s','_','a','_','v','e','r','y','_','l','o',
360 'n','g','_','s','y','m','l','i','n','k','_','b','o','d','y','_','a','b','c',
361 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
362 'w','x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
363 'o','p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d','e','f',
364 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y',
365 'z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
366 'r','s','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g','h','i',
367 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a',
368 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
369 'u','v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l',
370 'm','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d',
371 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
372 'x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
373 'p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g',
374 'h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
375 '_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
376 's','t','u','v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j',
377 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b',
378 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
379 'v','w','x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m',
380 'n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d','e',
381 'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x',
382 'y','z',10,'2','0',' ','a','t','i','m','e','=','1','1','8','4','3','9','1',
383 '0','2','5',10,'2','0',' ','c','t','i','m','e','=','1','1','8','4','3','9',
384 '0','6','4','8',10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'s','y','m',
385 'l','i','n','k',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
386 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
387 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'0','0','0','0','7',
388 '5','5',0,'0','0','0','1','7','5','0',0,'0','0','0','1','7','5','0',0,'0',
389 '0','0','0','0','0','0','0','0','0','0',0,'1','0','6','4','6','0','5','6',
390 '7','7','0',0,'0','3','7','1','2','1',0,' ','2','t','h','i','s','_','i','s',
391 '_','a','_','v','e','r','y','_','l','o','n','g','_','s','y','m','l','i','n',
392 'k','_','b','o','d','y','_','a','b','c','d','e','f','g','h','i','j','k','l',
393 'm','n','o','p','q','r','s','t','u','v','w','x','y','z','_','a','b','c','d',
394 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
395 'x','y','z','_','a','b','c','d','e','f','g','h','i','j','k','l','m','u','s',
396 't','a','r',0,'0','0','t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
397 0,0,0,0,0,0,0,0,0,'t','i','m',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
398 0,0,0,0,0,0,0,'0','0','0','0','0','0','0',0,'0','0','0','0','0','0','0'};
399 
verifyxL(struct archive_entry * ae)400 static void verifyxL(struct archive_entry *ae)
401 {
402 	assertEqualInt(archive_entry_filetype(ae), AE_IFLNK);
403 	assertEqualInt(archive_entry_mode(ae) & 0777, 0755);
404 	assertEqualInt(archive_entry_uid(ae), 1000);
405 	assertEqualInt(archive_entry_gid(ae), 1000);
406 	assertEqualString(archive_entry_uname(ae), "tim");
407 	assertEqualString(archive_entry_gname(ae), "tim");
408 	assertEqualString(archive_entry_pathname(ae), "symlink");
409 	assertEqualString(archive_entry_symlink(ae),
410 	    "this_is_a_very_long_symlink_body_abcdefghijklmnopqrstuvwxyz_"
411 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
412 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
413 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
414 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
415 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
416 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_"
417 	    "abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz");
418 	assert(archive_entry_hardlink(ae) == NULL);
419 	assertEqualInt(archive_entry_mtime(ae), 1184390648);
420 }
421 
422 
423 /* TODO: Any other types of headers? */
424 
verify(unsigned char * d,size_t s,void (* f)(struct archive_entry *),int compression,int format)425 static void verify(unsigned char *d, size_t s,
426     void (*f)(struct archive_entry *),
427     int compression, int format)
428 {
429 	struct archive_entry *ae;
430 	struct archive *a;
431 	unsigned char *buff = malloc(100000);
432 
433 	memcpy(buff, d, s);
434 	memset(buff + s, 0, 2048);
435 
436 	assert((a = archive_read_new()) != NULL);
437 	assertA(0 == archive_read_support_filter_all(a));
438 	assertA(0 == archive_read_support_format_all(a));
439 	assertA(0 == archive_read_open_memory(a, buff, s + 1024));
440 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
441 	assertEqualInt(archive_filter_code(a, 0), compression);
442 	assertEqualInt(archive_format(a), format);
443 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
444 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
445 
446 	/* Verify the only entry. */
447 	f(ae);
448 
449 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
450 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
451 	free(buff);
452 }
453 
DEFINE_TEST(test_read_format_tar)454 DEFINE_TEST(test_read_format_tar)
455 {
456 	verifyEmpty();
457 	verify(archive1, sizeof(archive1), verify1,
458 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_USTAR);
459 	verify(archive2, sizeof(archive2), verify2,
460 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_USTAR);
461 	verify(archive3, sizeof(archive3), verify3,
462 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_USTAR);
463 	verify(archive4, sizeof(archive4), verify4,
464 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_USTAR);
465 	verify(archive5, sizeof(archive5), verify5,
466 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_USTAR);
467 	verify(archive6, sizeof(archive6), verify6,
468 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_USTAR);
469 	verify(archiveK, sizeof(archiveK), verifyK,
470 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_GNUTAR);
471 	verify(archivexL, sizeof(archivexL), verifyxL,
472 	    ARCHIVE_FILTER_NONE, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
473 }
474 
475 
476