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/*
29Execute 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.
37base=/tmp/lha
38# Owner id
39owner=1001
40# Group id
41group=1001
42#
43# Make contents of a lha archive.
44#
45rm -rf ${base}
46mkdir ${base}
47mkdir ${base}/dir
48cat > ${base}/file1 << END
49                          file 1 contents
50hello
51hello
52hello
53END
54cat > ${base}/file2 << END
55                          file 2 contents
56hello
57hello
58hello
59hello
60hello
61hello
62END
63mkdir ${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)
76TZ=utc touch -afhm -t 197001030000.02 ${base}/dir2/symlink1 ${base}/dir2/symlink2
77TZ=utc touch -afhm -t 197001020000.01 ${base}/dir ${base}/dir2
78TZ=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
83lha0=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
86lha1=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
89lha2=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
92lha3=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
95lha4=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
98lha5=test_read_format_lha_lh0.lzh
99(cd ${base}; lha czq ${lha5} dir file1 file2 dir2)
100# make a lha archive with junk data
101lha6=test_read_format_lha_withjunk.lzh
102(cd ${base}; cp ${lha2} ${lha6}; echo "junk data!!!!" >> ${lha6})
103#
104uuencode ${base}/${lha0} ${lha0} > ${lha0}.uu
105uuencode ${base}/${lha1} ${lha1} > ${lha1}.uu
106uuencode ${base}/${lha2} ${lha2} > ${lha2}.uu
107uuencode ${base}/${lha3} ${lha3} > ${lha3}.uu
108uuencode ${base}/${lha4} ${lha4} > ${lha4}.uu
109uuencode ${base}/${lha5} ${lha5} > ${lha5}.uu
110uuencode ${base}/${lha6} ${lha5} > ${lha5}.uu
111uuencode ${base}/${lha6} ${lha6} > ${lha6}.uu
112#
113# Finish making test data.
114exit 1
115*/
116
117static 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)
124static 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
135static void
136verify(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
173	/* Verify directory2.  */
174	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
175	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
176	assertEqualString("dir2/", archive_entry_pathname(ae));
177	assertEqualInt(86401, archive_entry_mtime(ae));
178	assertEqualInt(uid, archive_entry_uid(ae));
179	assertEqualInt(gid, archive_entry_gid(ae));
180	assertEqualInt(0, archive_entry_size(ae));
181	assertEqualIntA(a, ARCHIVE_EOF,
182	    archive_read_data_block(a, &pv, &s, &o));
183	assertEqualInt(s, 0);
184
185	if (posix) {
186		/* Verify symbolic link file1. */
187		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
188		assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
189		assertEqualString("dir2/symlink1", archive_entry_pathname(ae));
190		assertEqualString("../file1", archive_entry_symlink(ae));
191		assertEqualInt(172802, archive_entry_mtime(ae));
192		assertEqualInt(uid, archive_entry_uid(ae));
193		assertEqualInt(gid, archive_entry_gid(ae));
194		assertEqualInt(0, archive_entry_size(ae));
195
196		/* Verify symbolic link file2. */
197		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
198		assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
199		assertEqualString("dir2/symlink2", archive_entry_pathname(ae));
200		assertEqualString("../file2", archive_entry_symlink(ae));
201		assertEqualInt(172802, archive_entry_mtime(ae));
202		assertEqualInt(uid, archive_entry_uid(ae));
203		assertEqualInt(gid, archive_entry_gid(ae));
204		assertEqualInt(0, archive_entry_size(ae));
205	}
206
207	/* Verify regular file1. */
208	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
209	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
210	assertEqualString("file1", archive_entry_pathname(ae));
211	assertEqualInt(86401, archive_entry_mtime(ae));
212	assertEqualInt(uid, archive_entry_uid(ae));
213	assertEqualInt(gid, archive_entry_gid(ae));
214	assertEqualInt(file1_size, archive_entry_size(ae));
215	assertEqualInt(file1_size, archive_read_data(a, buff, file1_size));
216	assertEqualMem(buff, file1, file1_size);
217
218	/* Verify regular file2. */
219	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
220	if (posix)
221		assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
222	else
223		assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
224	assertEqualString("file2", archive_entry_pathname(ae));
225	assertEqualInt(86401, archive_entry_mtime(ae));
226	assertEqualInt(uid, archive_entry_uid(ae));
227	assertEqualInt(gid, archive_entry_gid(ae));
228	assertEqualInt(file2_size, archive_entry_size(ae));
229	assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
230	assertEqualMem(buff, file2, file2_size);
231
232	/* Verify the number of files read. */
233	if (posix) {
234		assertEqualInt(6, archive_file_count(a));
235	} else {
236		assertEqualInt(4, archive_file_count(a));
237	}
238
239	/* End of archive. */
240	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
241
242	/* Verify the number of files read. */
243	if (posix) {
244		assertEqualInt(6, archive_file_count(a));
245	} else {
246		assertEqualInt(4, archive_file_count(a));
247	}
248
249	/* Verify archive format. */
250	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
251	assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
252
253	/* Close the archive. */
254	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
255	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
256}
257
258DEFINE_TEST(test_read_format_lha)
259{
260	/* Verify Header level 0 */
261	verify("test_read_format_lha_header0.lzh", 1);
262	/* Verify Header level 1 */
263	verify("test_read_format_lha_header1.lzh", 1);
264	/* Verify Header level 2 */
265	verify("test_read_format_lha_header2.lzh", 1);
266	/* Verify Header level 3
267	 * This test data can be made in Windows only. */
268	verify("test_read_format_lha_header3.lzh", 0);
269	/* Verify compression mode -lh6- */
270	verify("test_read_format_lha_lh6.lzh", 1);
271	/* Verify compression mode -lh7- */
272	verify("test_read_format_lha_lh7.lzh", 1);
273	/* Verify no compression -lh0- */
274	verify("test_read_format_lha_lh0.lzh", 1);
275	/* Verify an lha file with junk data. */
276	verify("test_read_format_lha_withjunk.lzh", 1);
277}
278
279