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 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 274DEFINE_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