1248590Smm/*- 2248590Smm * Copyright (c) 2012 Matthias Brantner 3248590Smm * All rights reserved. 4248590Smm * 5248590Smm * Redistribution and use in source and binary forms, with or without 6248590Smm * modification, are permitted provided that the following conditions 7248590Smm * are met: 8248590Smm * 1. Redistributions of source code must retain the above copyright 9248590Smm * notice, this list of conditions and the following disclaimer. 10248590Smm * 2. Redistributions in binary form must reproduce the above copyright 11248590Smm * notice, this list of conditions and the following disclaimer in the 12248590Smm * documentation and/or other materials provided with the distribution. 13248590Smm * 14248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17248590Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24248590Smm */ 25248590Smm 26248590Smm#include "test.h" 27248590Smm 28248590Smmstatic unsigned long 29248590Smmbitcrc32(unsigned long c, void *_p, size_t s) 30248590Smm{ 31248590Smm /* This is a drop-in replacement for crc32() from zlib. 32248590Smm * Libarchive should be able to correctly generate 33248590Smm * uncompressed zip archives (including correct CRCs) even 34248590Smm * when zlib is unavailable, and this function helps us verify 35248590Smm * that. Yes, this is very, very slow and unsuitable for 36248590Smm * production use, but it's correct, compact, and works well 37248590Smm * enough for this particular usage. Libarchive internally 38248590Smm * uses a much more efficient implementation. */ 39248590Smm const unsigned char *p = _p; 40248590Smm int bitctr; 41248590Smm 42248590Smm if (p == NULL) 43248590Smm return (0); 44248590Smm 45248590Smm for (; s > 0; --s) { 46248590Smm c ^= *p++; 47248590Smm for (bitctr = 8; bitctr > 0; --bitctr) { 48248590Smm if (c & 1) c = (c >> 1); 49248590Smm else c = (c >> 1) ^ 0xedb88320; 50248590Smm c ^= 0x80000000; 51248590Smm } 52248590Smm } 53248590Smm return (c); 54248590Smm} 55248590Smm 56248590Smm/* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */ 57248590Smmstatic int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); } 58248590Smmstatic int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); } 59248590Smm 60248590SmmDEFINE_TEST(test_write_zip_set_compression_store) 61248590Smm{ 62248590Smm /* Buffer data */ 63248590Smm struct archive *a; 64248590Smm struct archive_entry *entry; 65248590Smm char buff[100000]; 66248590Smm const char *buffend; 67248590Smm /* p is the pointer to walk over the central directory, 68248590Smm * q walks over the local headers, the data and the data descriptors. */ 69248590Smm const char *p, *q; 70248590Smm size_t used; 71248590Smm 72248590Smm /* File data */ 73248590Smm char file_name[] = "file"; 74248590Smm char file_data1[] = {'1', '2', '3', '4', '5'}; 75248590Smm char file_data2[] = {'6', '7', '8', '9', '0'}; 76248590Smm int file_perm = 00644; 77248590Smm short file_uid = 10; 78248590Smm short file_gid = 20; 79248590Smm 80248590Smm /* Folder data */ 81248590Smm char folder_name[] = "folder/"; 82248590Smm int folder_perm = 00755; 83248590Smm short folder_uid = 30; 84248590Smm short folder_gid = 40; 85248590Smm 86248590Smm /* Time data */ 87248590Smm time_t t = time(NULL); 88248590Smm struct tm *tm = localtime(&t); 89248590Smm 90248590Smm /* Misc variables */ 91248590Smm unsigned long crc; 92248590Smm 93248590Smm /* Create new ZIP archive in memory without padding. */ 94248590Smm assert((a = archive_write_new()) != NULL); 95248590Smm assertA(0 == archive_write_set_format_zip(a)); 96248590Smm assertA(0 == archive_write_add_filter_none(a)); 97248590Smm assertA(0 == archive_write_set_bytes_per_block(a, 1)); 98248590Smm assertA(0 == archive_write_set_bytes_in_last_block(a, 1)); 99248590Smm assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used)); 100248590Smm 101248590Smm /* Write entries. */ 102248590Smm 103248590Smm /* Regular file */ 104248590Smm assert((entry = archive_entry_new()) != NULL); 105248590Smm archive_entry_set_pathname(entry, file_name); 106248590Smm archive_entry_set_mode(entry, S_IFREG | 0644); 107248590Smm archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2)); 108248590Smm archive_entry_set_uid(entry, file_uid); 109248590Smm archive_entry_set_gid(entry, file_gid); 110248590Smm archive_entry_set_mtime(entry, t, 0); 111248590Smm archive_entry_set_atime(entry, t, 0); 112248590Smm archive_entry_set_ctime(entry, t, 0); 113248590Smm archive_write_zip_set_compression_store(a); 114248590Smm assertEqualIntA(a, 0, archive_write_header(a, entry)); 115248590Smm assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1))); 116248590Smm assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2))); 117248590Smm archive_entry_free(entry); 118248590Smm archive_write_finish_entry(a); 119248590Smm 120248590Smm /* Folder */ 121248590Smm assert((entry = archive_entry_new()) != NULL); 122248590Smm archive_entry_set_pathname(entry, folder_name); 123248590Smm archive_entry_set_mode(entry, S_IFDIR | folder_perm); 124248590Smm archive_entry_set_size(entry, 0); 125248590Smm archive_entry_set_uid(entry, folder_uid); 126248590Smm archive_entry_set_gid(entry, folder_gid); 127248590Smm archive_entry_set_mtime(entry, t, 0); 128248590Smm archive_entry_set_atime(entry, t, 0); 129248590Smm archive_entry_set_ctime(entry, t, 0); 130248590Smm archive_write_zip_set_compression_store(a); 131248590Smm assertEqualIntA(a, 0, archive_write_header(a, entry)); 132248590Smm archive_entry_free(entry); 133248590Smm archive_write_finish_entry(a); 134248590Smm 135248590Smm /* Close the archive . */ 136248590Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 137248590Smm assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 138248590Smm 139248590Smm /* Remember the end of the archive in memory. */ 140248590Smm buffend = buff + used; 141248590Smm 142248590Smm /* Verify "End of Central Directory" record. */ 143248590Smm /* Get address of end-of-central-directory record. */ 144248590Smm p = buffend - 22; /* Assumes there is no zip comment field. */ 145248590Smm failure("End-of-central-directory begins with PK\\005\\006 signature"); 146248590Smm assertEqualMem(p, "PK\005\006", 4); 147248590Smm failure("This must be disk 0"); 148248590Smm assertEqualInt(i2(p + 4), 0); 149248590Smm failure("Central dir must start on disk 0"); 150248590Smm assertEqualInt(i2(p + 6), 0); 151248590Smm failure("All central dir entries are on this disk"); 152248590Smm assertEqualInt(i2(p + 8), i2(p + 10)); 153248590Smm failure("CD start (%d) + CD length (%d) should == archive size - 22", 154248590Smm i4(p + 12), i4(p + 16)); 155248590Smm assertEqualInt(i4(p + 12) + i4(p + 16), used - 22); 156248590Smm failure("no zip comment"); 157248590Smm assertEqualInt(i2(p + 20), 0); 158248590Smm 159248590Smm /* Get address of first entry in central directory. */ 160248590Smm p = buff + i4(buffend - 6); 161248590Smm failure("Central file record at offset %d should begin with" 162248590Smm " PK\\001\\002 signature", 163248590Smm i4(buffend - 10)); 164248590Smm 165248590Smm /* Verify file entry in central directory. */ 166248590Smm assertEqualMem(p, "PK\001\002", 4); /* Signature */ 167248590Smm assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */ 168248590Smm assertEqualInt(i2(p + 6), 20); /* Version needed to extract */ 169248590Smm assertEqualInt(i2(p + 8), 8); /* Flags */ 170248590Smm assertEqualInt(i2(p + 10), 0); /* Compression method */ 171248590Smm assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 172248590Smm assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 173248590Smm crc = bitcrc32(0, file_data1, sizeof(file_data1)); 174248590Smm crc = bitcrc32(crc, file_data2, sizeof(file_data2)); 175248590Smm assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 176248590Smm assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 177248590Smm assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 178248590Smm assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */ 179248590Smm assertEqualInt(i2(p + 30), 13); /* Extra field length */ 180248590Smm assertEqualInt(i2(p + 32), 0); /* File comment length */ 181248590Smm assertEqualInt(i2(p + 34), 0); /* Disk number start */ 182248590Smm assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 183248590Smm assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */ 184248590Smm assertEqualInt(i4(p + 42), 0); /* Offset of local header */ 185248590Smm assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */ 186248590Smm p = p + 46 + strlen(file_name); 187248590Smm assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 188248590Smm assertEqualInt(i2(p + 2), 5); /* 'UT' size */ 189248590Smm assertEqualInt(p[4], 7); /* 'UT' flags */ 190248590Smm assertEqualInt(i4(p + 5), t); /* 'UT' mtime */ 191248590Smm p = p + 9; 192248590Smm assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 193248590Smm assertEqualInt(i2(p + 2), 0); /* 'ux' size */ 194248590Smm p = p + 4; 195248590Smm 196248590Smm /* Verify local header of file entry. */ 197248590Smm q = buff; 198248590Smm assertEqualMem(q, "PK\003\004", 4); /* Signature */ 199248590Smm assertEqualInt(i2(q + 4), 20); /* Version needed to extract */ 200248590Smm assertEqualInt(i2(q + 6), 8); /* Flags */ 201248590Smm assertEqualInt(i2(q + 8), 0); /* Compression method */ 202248590Smm assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 203248590Smm assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 204248590Smm assertEqualInt(i4(q + 14), 0); /* CRC-32 */ 205248590Smm assertEqualInt(i4(q + 18), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 206248590Smm assertEqualInt(i4(q + 22), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 207248590Smm assertEqualInt(i2(q + 26), strlen(file_name)); /* Pathname length */ 208248590Smm assertEqualInt(i2(q + 28), 32); /* Extra field length */ 209248590Smm assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */ 210248590Smm q = q + 30 + strlen(file_name); 211248590Smm assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */ 212248590Smm assertEqualInt(i2(q + 2), 13); /* 'UT' size */ 213248590Smm assertEqualInt(q[4], 7); /* 'UT' flags */ 214248590Smm assertEqualInt(i4(q + 5), t); /* 'UT' mtime */ 215248590Smm assertEqualInt(i4(q + 9), t); /* 'UT' atime */ 216248590Smm assertEqualInt(i4(q + 13), t); /* 'UT' ctime */ 217248590Smm q = q + 17; 218248590Smm assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */ 219248590Smm assertEqualInt(i2(q + 2), 11); /* 'ux' size */ 220248590Smm assertEqualInt(q[4], 1); /* 'ux' version */ 221248590Smm assertEqualInt(q[5], 4); /* 'ux' uid size */ 222248590Smm assertEqualInt(i4(q + 6), file_uid); /* 'Ux' UID */ 223248590Smm assertEqualInt(q[10], 4); /* 'ux' gid size */ 224248590Smm assertEqualInt(i4(q + 11), file_gid); /* 'Ux' GID */ 225248590Smm q = q + 15; 226248590Smm 227248590Smm /* Verify data of file entry. */ 228248590Smm assertEqualMem(q, file_data1, sizeof(file_data1)); 229248590Smm assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2)); 230248590Smm q = q + sizeof(file_data1) + sizeof(file_data2); 231248590Smm 232248590Smm /* Verify data descriptor of file entry. */ 233248590Smm assertEqualMem(q, "PK\007\010", 4); /* Signature */ 234248590Smm assertEqualInt(i4(q + 4), crc); /* CRC-32 */ 235248590Smm assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 236248590Smm assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 237248590Smm q = q + 16; 238248590Smm 239248590Smm /* Verify folder entry in central directory. */ 240248590Smm assertEqualMem(p, "PK\001\002", 4); /* Signature */ 241248590Smm assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */ 242248590Smm assertEqualInt(i2(p + 6), 20); /* Version needed to extract */ 243248590Smm assertEqualInt(i2(p + 8), 8); /* Flags */ 244248590Smm assertEqualInt(i2(p + 10), 0); /* Compression method */ 245248590Smm assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 246248590Smm assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 247248590Smm crc = 0; 248248590Smm assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 249248590Smm assertEqualInt(i4(p + 20), 0); /* Compressed size */ 250248590Smm assertEqualInt(i4(p + 24), 0); /* Uncompressed size */ 251248590Smm assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */ 252248590Smm assertEqualInt(i2(p + 30), 13); /* Extra field length */ 253248590Smm assertEqualInt(i2(p + 32), 0); /* File comment length */ 254248590Smm assertEqualInt(i2(p + 34), 0); /* Disk number start */ 255248590Smm assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 256248590Smm assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */ 257248590Smm assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */ 258248590Smm assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */ 259248590Smm p = p + 46 + strlen(folder_name); 260248590Smm assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 261248590Smm assertEqualInt(i2(p + 2), 5); /* 'UT' size */ 262248590Smm assertEqualInt(p[4], 7); /* 'UT' flags */ 263248590Smm assertEqualInt(i4(p + 5), t); /* 'UT' mtime */ 264248590Smm p = p + 9; 265248590Smm assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 266248590Smm assertEqualInt(i2(p + 2), 0); /* 'ux' size */ 267248590Smm /*p = p + 4;*/ 268248590Smm 269248590Smm /* Verify local header of folder entry. */ 270248590Smm assertEqualMem(q, "PK\003\004", 4); /* Signature */ 271248590Smm assertEqualInt(i2(q + 4), 20); /* Version needed to extract */ 272248590Smm assertEqualInt(i2(q + 6), 8); /* Flags */ 273248590Smm assertEqualInt(i2(q + 8), 0); /* Compression method */ 274248590Smm assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 275248590Smm assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 276248590Smm assertEqualInt(i4(q + 14), 0); /* CRC-32 */ 277248590Smm assertEqualInt(i4(q + 18), 0); /* Compressed size */ 278248590Smm assertEqualInt(i4(q + 22), 0); /* Uncompressed size */ 279248590Smm assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */ 280248590Smm assertEqualInt(i2(q + 28), 32); /* Extra field length */ 281248590Smm assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */ 282248590Smm q = q + 30 + strlen(folder_name); 283248590Smm assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */ 284248590Smm assertEqualInt(i2(q + 2), 13); /* 'UT' size */ 285248590Smm assertEqualInt(q[4], 7); /* 'UT' flags */ 286248590Smm assertEqualInt(i4(q + 5), t); /* 'UT' mtime */ 287248590Smm assertEqualInt(i4(q + 9), t); /* 'UT' atime */ 288248590Smm assertEqualInt(i4(q + 13), t); /* 'UT' ctime */ 289248590Smm q = q + 17; 290248590Smm assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */ 291248590Smm assertEqualInt(i2(q + 2), 11); /* 'ux' size */ 292248590Smm assertEqualInt(q[4], 1); /* 'ux' version */ 293248590Smm assertEqualInt(q[5], 4); /* 'ux' uid size */ 294248590Smm assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */ 295248590Smm assertEqualInt(q[10], 4); /* 'ux' gid size */ 296248590Smm assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */ 297248590Smm q = q + 15; 298248590Smm 299248590Smm /* There should not be any data in the folder entry, 300248590Smm * meaning next is the data descriptor header. */ 301248590Smm 302248590Smm /* Verify data descriptor of folder entry. */ 303248590Smm assertEqualMem(q, "PK\007\010", 4); /* Signature */ 304248590Smm assertEqualInt(i4(q + 4), crc); /* CRC-32 */ 305248590Smm assertEqualInt(i4(q + 8), 0); /* Compressed size */ 306248590Smm assertEqualInt(i4(q + 12), 0); /* Uncompressed size */ 307248590Smm /*q = q + 16;*/ 308248590Smm} 309