1 /*-
2 * Copyright (c) 2008, 2010 Michihiro NAKAJIMA
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 /*
29 Execute the following command to rebuild the data for this program:
30 tail -n +32 test_read_format_lha.c | /bin/sh
31
32 #/bin/sh
33 #
34 # How to make test data.
35 #
36 # Temporary directory.
37 base=/tmp/lha
38 # Owner id
39 owner=1001
40 # Group id
41 group=1001
42 #
43 # Make contents of a lha archive.
44 #
45 rm -rf ${base}
46 mkdir ${base}
47 mkdir ${base}/dir
48 cat > ${base}/file1 << END
49 file 1 contents
50 hello
51 hello
52 hello
53 END
54 cat > ${base}/file2 << END
55 file 2 contents
56 hello
57 hello
58 hello
59 hello
60 hello
61 hello
62 END
63 mkdir ${base}/dir2
64 #
65 # Set up a file mode, owner and group.
66 #
67 (cd ${base}/dir2; ln -s ../file1 symlink1)
68 (cd ${base}/dir2; ln -s ../file2 symlink2)
69 (cd ${base}; chown ${owner}:${group} dir file1 file2)
70 (cd ${base}; chown -h ${owner}:${group} dir2 dir2/symlink1 dir2/symlink2)
71 (cd ${base}; chmod 0750 dir)
72 (cd ${base}; chmod 0755 dir2)
73 (cd ${base}; chmod 0755 dir2/symlink1 dir2/symlink2)
74 (cd ${base}; chmod 0644 file1)
75 (cd ${base}; chmod 0666 file2)
76 TZ=utc touch -afhm -t 197001030000.02 ${base}/dir2/symlink1 ${base}/dir2/symlink2
77 TZ=utc touch -afhm -t 197001020000.01 ${base}/dir ${base}/dir2
78 TZ=utc touch -afhm -t 197001020000.01 ${base}/file1 ${base}/file2
79 #
80 # Make several lha archives.
81 #
82 # Make a lha archive with header level 0
83 lha0=test_read_format_lha_header0.lzh
84 (cd ${base}; lha c0q ${lha0} dir file1 file2 dir2)
85 # Make a lha archive with header level 1
86 lha1=test_read_format_lha_header1.lzh
87 (cd ${base}; lha c1q ${lha1} dir file1 file2 dir2)
88 # Make a lha archive with header level 2
89 lha2=test_read_format_lha_header2.lzh
90 (cd ${base}; lha c2q ${lha2} dir file1 file2 dir2)
91 # Make a lha archive with -lh6- compression mode
92 lha3=test_read_format_lha_lh6.lzh
93 (cd ${base}; lha co6q ${lha3} dir file1 file2 dir2)
94 # Make a lha archive with -lh7- compression mode
95 lha4=test_read_format_lha_lh7.lzh
96 (cd ${base}; lha co7q ${lha4} dir file1 file2 dir2)
97 # Make a lha archive with -lh0- no compression
98 lha5=test_read_format_lha_lh0.lzh
99 (cd ${base}; lha czq ${lha5} dir file1 file2 dir2)
100 # make a lha archive with junk data
101 lha6=test_read_format_lha_withjunk.lzh
102 (cd ${base}; cp ${lha2} ${lha6}; echo "junk data!!!!" >> ${lha6})
103 #
104 uuencode ${base}/${lha0} ${lha0} > ${lha0}.uu
105 uuencode ${base}/${lha1} ${lha1} > ${lha1}.uu
106 uuencode ${base}/${lha2} ${lha2} > ${lha2}.uu
107 uuencode ${base}/${lha3} ${lha3} > ${lha3}.uu
108 uuencode ${base}/${lha4} ${lha4} > ${lha4}.uu
109 uuencode ${base}/${lha5} ${lha5} > ${lha5}.uu
110 uuencode ${base}/${lha6} ${lha5} > ${lha5}.uu
111 uuencode ${base}/${lha6} ${lha6} > ${lha6}.uu
112 #
113 # Finish making test data.
114 exit 1
115 */
116
117 static const char file1[] = {
118 " file 1 contents\n"
119 "hello\n"
120 "hello\n"
121 "hello\n"
122 };
123 #define file1_size (sizeof(file1)-1)
124 static const char file2[] = {
125 " file 2 contents\n"
126 "hello\n"
127 "hello\n"
128 "hello\n"
129 "hello\n"
130 "hello\n"
131 "hello\n"
132 };
133 #define file2_size (sizeof(file2)-1)
134
135 static void
verify(const char * refname,int posix)136 verify(const char *refname, int posix)
137 {
138 struct archive_entry *ae;
139 struct archive *a;
140 char buff[128];
141 const void *pv;
142 size_t s;
143 int64_t o;
144 int uid, gid;
145
146 if (posix)
147 uid = gid = 1001;
148 else
149 uid = gid = 0;
150
151 extract_reference_file(refname);
152 assert((a = archive_read_new()) != NULL);
153 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
154 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
155 assertEqualIntA(a, ARCHIVE_OK,
156 archive_read_open_filename(a, refname, 10240));
157
158 /* Verify directory1. */
159 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
160 if (posix)
161 assertEqualInt((AE_IFDIR | 0750), archive_entry_mode(ae));
162 else
163 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
164 assertEqualString("dir/", archive_entry_pathname(ae));
165 assertEqualInt(86401, archive_entry_mtime(ae));
166 assertEqualInt(uid, archive_entry_uid(ae));
167 assertEqualInt(gid, archive_entry_gid(ae));
168 assertEqualInt(0, archive_entry_size(ae));
169 assertEqualIntA(a, ARCHIVE_EOF,
170 archive_read_data_block(a, &pv, &s, &o));
171 assertEqualInt(s, 0);
172 assertEqualInt(archive_entry_is_encrypted(ae), 0);
173 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
174
175 /* Verify directory2. */
176 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
177 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
178 assertEqualString("dir2/", archive_entry_pathname(ae));
179 assertEqualInt(86401, archive_entry_mtime(ae));
180 assertEqualInt(uid, archive_entry_uid(ae));
181 assertEqualInt(gid, archive_entry_gid(ae));
182 assertEqualInt(0, archive_entry_size(ae));
183 assertEqualIntA(a, ARCHIVE_EOF,
184 archive_read_data_block(a, &pv, &s, &o));
185 assertEqualInt(s, 0);
186 assertEqualInt(archive_entry_is_encrypted(ae), 0);
187 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
188
189 if (posix) {
190 /* Verify symbolic link file1. */
191 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
192 assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
193 assertEqualString("dir2/symlink1", archive_entry_pathname(ae));
194 assertEqualString("../file1", archive_entry_symlink(ae));
195 assertEqualInt(172802, archive_entry_mtime(ae));
196 assertEqualInt(uid, archive_entry_uid(ae));
197 assertEqualInt(gid, archive_entry_gid(ae));
198 assertEqualInt(0, archive_entry_size(ae));
199 assertEqualInt(archive_entry_is_encrypted(ae), 0);
200 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
201
202 /* Verify symbolic link file2. */
203 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
204 assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
205 assertEqualString("dir2/symlink2", archive_entry_pathname(ae));
206 assertEqualString("../file2", archive_entry_symlink(ae));
207 assertEqualInt(172802, archive_entry_mtime(ae));
208 assertEqualInt(uid, archive_entry_uid(ae));
209 assertEqualInt(gid, archive_entry_gid(ae));
210 assertEqualInt(0, archive_entry_size(ae));
211 assertEqualInt(archive_entry_is_encrypted(ae), 0);
212 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
213 }
214
215 /* Verify regular file1. */
216 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
217 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
218 assertEqualString("file1", archive_entry_pathname(ae));
219 assertEqualInt(86401, archive_entry_mtime(ae));
220 assertEqualInt(uid, archive_entry_uid(ae));
221 assertEqualInt(gid, archive_entry_gid(ae));
222 assertEqualInt(file1_size, archive_entry_size(ae));
223 assertEqualInt(file1_size, archive_read_data(a, buff, file1_size));
224 assertEqualMem(buff, file1, file1_size);
225 assertEqualInt(archive_entry_is_encrypted(ae), 0);
226 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
227
228 /* Verify regular file2. */
229 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
230 if (posix)
231 assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
232 else
233 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
234 assertEqualString("file2", archive_entry_pathname(ae));
235 assertEqualInt(86401, archive_entry_mtime(ae));
236 assertEqualInt(uid, archive_entry_uid(ae));
237 assertEqualInt(gid, archive_entry_gid(ae));
238 assertEqualInt(file2_size, archive_entry_size(ae));
239 assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
240 assertEqualMem(buff, file2, file2_size);
241 assertEqualInt(archive_entry_is_encrypted(ae), 0);
242 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
243
244 /* Verify the number of files read. */
245 if (posix) {
246 assertEqualInt(6, archive_file_count(a));
247 } else {
248 assertEqualInt(4, archive_file_count(a));
249 }
250
251 /* End of archive. */
252 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
253
254 /* Verify the number of files read. */
255 if (posix) {
256 assertEqualInt(6, archive_file_count(a));
257 } else {
258 assertEqualInt(4, archive_file_count(a));
259 }
260
261 /* Verify encryption status */
262 assertEqualInt(archive_entry_is_encrypted(ae), 0);
263 assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
264
265 /* Verify archive format. */
266 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
267 assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
268
269 /* Close the archive. */
270 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
271 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
272 }
273
DEFINE_TEST(test_read_format_lha)274 DEFINE_TEST(test_read_format_lha)
275 {
276 /* Verify Header level 0 */
277 verify("test_read_format_lha_header0.lzh", 1);
278 /* Verify Header level 1 */
279 verify("test_read_format_lha_header1.lzh", 1);
280 /* Verify Header level 2 */
281 verify("test_read_format_lha_header2.lzh", 1);
282 /* Verify Header level 3
283 * This test data can be made in Windows only. */
284 verify("test_read_format_lha_header3.lzh", 0);
285 /* Verify compression mode -lh6- */
286 verify("test_read_format_lha_lh6.lzh", 1);
287 /* Verify compression mode -lh7- */
288 verify("test_read_format_lha_lh7.lzh", 1);
289 /* Verify no compression -lh0- */
290 verify("test_read_format_lha_lh0.lzh", 1);
291 /* Verify an lha file with junk data. */
292 verify("test_read_format_lha_withjunk.lzh", 1);
293 }
294
295