1231200Smm/*-
2231200Smm * Copyright (c) 2008, 2010 Michihiro NAKAJIMA
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231200Smm */
25231200Smm#include "test.h"
26231200Smm__FBSDID("$FreeBSD");
27231200Smm
28231200Smm/*
29231200SmmExecute the following command to rebuild the data for this program:
30231200Smm   tail -n +32 test_read_format_lha.c | /bin/sh
31231200Smm
32231200Smm#/bin/sh
33231200Smm#
34231200Smm# How to make test data.
35231200Smm#
36231200Smm# Temporary directory.
37231200Smmbase=/tmp/lha
38231200Smm# Owner id
39231200Smmowner=1001
40231200Smm# Group id
41231200Smmgroup=1001
42231200Smm#
43231200Smm# Make contents of a lha archive.
44231200Smm#
45231200Smmrm -rf ${base}
46231200Smmmkdir ${base}
47231200Smmmkdir ${base}/dir
48231200Smmcat > ${base}/file1 << END
49231200Smm                          file 1 contents
50231200Smmhello
51231200Smmhello
52231200Smmhello
53231200SmmEND
54231200Smmcat > ${base}/file2 << END
55231200Smm                          file 2 contents
56231200Smmhello
57231200Smmhello
58231200Smmhello
59231200Smmhello
60231200Smmhello
61231200Smmhello
62231200SmmEND
63231200Smmmkdir ${base}/dir2
64231200Smm#
65231200Smm# Set up a file mode, owner and group.
66231200Smm#
67231200Smm(cd ${base}/dir2; ln -s ../file1 symlink1)
68231200Smm(cd ${base}/dir2; ln -s ../file2 symlink2)
69231200Smm(cd ${base}; chown ${owner}:${group} dir file1 file2)
70231200Smm(cd ${base}; chown -h ${owner}:${group} dir2 dir2/symlink1 dir2/symlink2)
71231200Smm(cd ${base}; chmod 0750 dir)
72231200Smm(cd ${base}; chmod 0755 dir2)
73231200Smm(cd ${base}; chmod 0755 dir2/symlink1 dir2/symlink2)
74231200Smm(cd ${base}; chmod 0644 file1)
75231200Smm(cd ${base}; chmod 0666 file2)
76231200SmmTZ=utc touch -afhm -t 197001030000.02 ${base}/dir2/symlink1 ${base}/dir2/symlink2
77231200SmmTZ=utc touch -afhm -t 197001020000.01 ${base}/dir ${base}/dir2
78231200SmmTZ=utc touch -afhm -t 197001020000.01 ${base}/file1 ${base}/file2
79231200Smm#
80231200Smm# Make several lha archives.
81231200Smm#
82231200Smm# Make a lha archive with header level 0
83231200Smmlha0=test_read_format_lha_header0.lzh
84231200Smm(cd ${base}; lha c0q ${lha0} dir file1 file2 dir2)
85231200Smm# Make a lha archive with header level 1
86231200Smmlha1=test_read_format_lha_header1.lzh
87231200Smm(cd ${base}; lha c1q ${lha1} dir file1 file2 dir2)
88231200Smm# Make a lha archive with header level 2
89231200Smmlha2=test_read_format_lha_header2.lzh
90231200Smm(cd ${base}; lha c2q ${lha2} dir file1 file2 dir2)
91231200Smm# Make a lha archive with -lh6- compression mode
92231200Smmlha3=test_read_format_lha_lh6.lzh
93231200Smm(cd ${base}; lha co6q ${lha3} dir file1 file2 dir2)
94231200Smm# Make a lha archive with -lh7- compression mode
95231200Smmlha4=test_read_format_lha_lh7.lzh
96231200Smm(cd ${base}; lha co7q ${lha4} dir file1 file2 dir2)
97231200Smm# Make a lha archive with -lh0- no compression
98231200Smmlha5=test_read_format_lha_lh0.lzh
99231200Smm(cd ${base}; lha czq ${lha5} dir file1 file2 dir2)
100231200Smm# make a lha archive with junk data
101231200Smmlha6=test_read_format_lha_withjunk.lzh
102231200Smm(cd ${base}; cp ${lha2} ${lha6}; echo "junk data!!!!" >> ${lha6})
103231200Smm#
104231200Smmuuencode ${base}/${lha0} ${lha0} > ${lha0}.uu
105231200Smmuuencode ${base}/${lha1} ${lha1} > ${lha1}.uu
106231200Smmuuencode ${base}/${lha2} ${lha2} > ${lha2}.uu
107231200Smmuuencode ${base}/${lha3} ${lha3} > ${lha3}.uu
108231200Smmuuencode ${base}/${lha4} ${lha4} > ${lha4}.uu
109231200Smmuuencode ${base}/${lha5} ${lha5} > ${lha5}.uu
110231200Smmuuencode ${base}/${lha6} ${lha5} > ${lha5}.uu
111231200Smmuuencode ${base}/${lha6} ${lha6} > ${lha6}.uu
112231200Smm#
113231200Smm# Finish making test data.
114231200Smmexit 1
115231200Smm*/
116231200Smm
117231200Smmstatic const char file1[] = {
118231200Smm"                          file 1 contents\n"
119231200Smm"hello\n"
120231200Smm"hello\n"
121231200Smm"hello\n"
122231200Smm};
123231200Smm#define file1_size (sizeof(file1)-1)
124231200Smmstatic const char file2[] = {
125231200Smm"                          file 2 contents\n"
126231200Smm"hello\n"
127231200Smm"hello\n"
128231200Smm"hello\n"
129231200Smm"hello\n"
130231200Smm"hello\n"
131231200Smm"hello\n"
132231200Smm};
133231200Smm#define file2_size (sizeof(file2)-1)
134231200Smm
135231200Smmstatic void
136231200Smmverify(const char *refname, int posix)
137231200Smm{
138231200Smm	struct archive_entry *ae;
139231200Smm	struct archive *a;
140231200Smm	char buff[128];
141231200Smm	const void *pv;
142231200Smm	size_t s;
143231200Smm	int64_t o;
144231200Smm	int uid, gid;
145231200Smm
146231200Smm	if (posix)
147231200Smm		uid = gid = 1001;
148231200Smm	else
149231200Smm		uid = gid = 0;
150231200Smm
151231200Smm	extract_reference_file(refname);
152231200Smm	assert((a = archive_read_new()) != NULL);
153231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
154231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
155231200Smm	assertEqualIntA(a, ARCHIVE_OK,
156231200Smm	    archive_read_open_filename(a, refname, 10240));
157231200Smm
158231200Smm	/* Verify directory1.  */
159231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
160231200Smm	if (posix)
161231200Smm		assertEqualInt((AE_IFDIR | 0750), archive_entry_mode(ae));
162231200Smm	else
163231200Smm		assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
164231200Smm	assertEqualString("dir/", archive_entry_pathname(ae));
165231200Smm	assertEqualInt(86401, archive_entry_mtime(ae));
166231200Smm	assertEqualInt(uid, archive_entry_uid(ae));
167231200Smm	assertEqualInt(gid, archive_entry_gid(ae));
168231200Smm	assertEqualInt(0, archive_entry_size(ae));
169231200Smm	assertEqualIntA(a, ARCHIVE_EOF,
170231200Smm	    archive_read_data_block(a, &pv, &s, &o));
171231200Smm	assertEqualInt(s, 0);
172231200Smm
173231200Smm	/* Verify directory2.  */
174231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
175231200Smm	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
176231200Smm	assertEqualString("dir2/", archive_entry_pathname(ae));
177231200Smm	assertEqualInt(86401, archive_entry_mtime(ae));
178231200Smm	assertEqualInt(uid, archive_entry_uid(ae));
179231200Smm	assertEqualInt(gid, archive_entry_gid(ae));
180231200Smm	assertEqualInt(0, archive_entry_size(ae));
181231200Smm	assertEqualIntA(a, ARCHIVE_EOF,
182231200Smm	    archive_read_data_block(a, &pv, &s, &o));
183231200Smm	assertEqualInt(s, 0);
184231200Smm
185231200Smm	if (posix) {
186231200Smm		/* Verify symbolic link file1. */
187231200Smm		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
188231200Smm		assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
189231200Smm		assertEqualString("dir2/symlink1", archive_entry_pathname(ae));
190231200Smm		assertEqualString("../file1", archive_entry_symlink(ae));
191231200Smm		assertEqualInt(172802, archive_entry_mtime(ae));
192231200Smm		assertEqualInt(uid, archive_entry_uid(ae));
193231200Smm		assertEqualInt(gid, archive_entry_gid(ae));
194231200Smm		assertEqualInt(0, archive_entry_size(ae));
195231200Smm
196231200Smm		/* Verify symbolic link file2. */
197231200Smm		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
198231200Smm		assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
199231200Smm		assertEqualString("dir2/symlink2", archive_entry_pathname(ae));
200231200Smm		assertEqualString("../file2", archive_entry_symlink(ae));
201231200Smm		assertEqualInt(172802, archive_entry_mtime(ae));
202231200Smm		assertEqualInt(uid, archive_entry_uid(ae));
203231200Smm		assertEqualInt(gid, archive_entry_gid(ae));
204231200Smm		assertEqualInt(0, archive_entry_size(ae));
205231200Smm	}
206231200Smm
207231200Smm	/* Verify regular file1. */
208231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
209231200Smm	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
210231200Smm	assertEqualString("file1", archive_entry_pathname(ae));
211231200Smm	assertEqualInt(86401, archive_entry_mtime(ae));
212231200Smm	assertEqualInt(uid, archive_entry_uid(ae));
213231200Smm	assertEqualInt(gid, archive_entry_gid(ae));
214231200Smm	assertEqualInt(file1_size, archive_entry_size(ae));
215231200Smm	assertEqualInt(file1_size, archive_read_data(a, buff, file1_size));
216231200Smm	assertEqualMem(buff, file1, file1_size);
217231200Smm
218231200Smm	/* Verify regular file2. */
219231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
220231200Smm	if (posix)
221231200Smm		assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
222231200Smm	else
223231200Smm		assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
224231200Smm	assertEqualString("file2", archive_entry_pathname(ae));
225231200Smm	assertEqualInt(86401, archive_entry_mtime(ae));
226231200Smm	assertEqualInt(uid, archive_entry_uid(ae));
227231200Smm	assertEqualInt(gid, archive_entry_gid(ae));
228231200Smm	assertEqualInt(file2_size, archive_entry_size(ae));
229231200Smm	assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
230231200Smm	assertEqualMem(buff, file2, file2_size);
231231200Smm
232231200Smm	/* Verify the number of files read. */
233231200Smm	if (posix) {
234231200Smm		assertEqualInt(6, archive_file_count(a));
235231200Smm	} else {
236231200Smm		assertEqualInt(4, archive_file_count(a));
237231200Smm	}
238231200Smm
239231200Smm	/* End of archive. */
240231200Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
241231200Smm
242231200Smm	/* Verify the number of files read. */
243231200Smm	if (posix) {
244231200Smm		assertEqualInt(6, archive_file_count(a));
245231200Smm	} else {
246231200Smm		assertEqualInt(4, archive_file_count(a));
247231200Smm	}
248231200Smm
249231200Smm	/* Verify archive format. */
250248616Smm	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
251231200Smm	assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
252231200Smm
253231200Smm	/* Close the archive. */
254231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
255231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
256231200Smm}
257231200Smm
258231200SmmDEFINE_TEST(test_read_format_lha)
259231200Smm{
260231200Smm	/* Verify Header level 0 */
261231200Smm	verify("test_read_format_lha_header0.lzh", 1);
262231200Smm	/* Verify Header level 1 */
263231200Smm	verify("test_read_format_lha_header1.lzh", 1);
264231200Smm	/* Verify Header level 2 */
265231200Smm	verify("test_read_format_lha_header2.lzh", 1);
266231200Smm	/* Verify Header level 3
267231200Smm	 * This test data can be made in Windows only. */
268231200Smm	verify("test_read_format_lha_header3.lzh", 0);
269231200Smm	/* Verify compression mode -lh6- */
270231200Smm	verify("test_read_format_lha_lh6.lzh", 1);
271231200Smm	/* Verify compression mode -lh7- */
272231200Smm	verify("test_read_format_lha_lh7.lzh", 1);
273231200Smm	/* Verify no compression -lh0- */
274231200Smm	verify("test_read_format_lha_lh0.lzh", 1);
275231200Smm	/* Verify an lha file with junk data. */
276231200Smm	verify("test_read_format_lha_withjunk.lzh", 1);
277231200Smm}
278231200Smm
279