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