1299425Smm/*- 2299425Smm * Copyright (c) 2003-2008 Tim Kientzle 3299425Smm * Copyright (c) 2008 Anselm Strauss 4299425Smm * All rights reserved. 5299425Smm * 6299425Smm * Redistribution and use in source and binary forms, with or without 7299425Smm * modification, are permitted provided that the following conditions 8299425Smm * are met: 9299425Smm * 1. Redistributions of source code must retain the above copyright 10299425Smm * notice, this list of conditions and the following disclaimer. 11299425Smm * 2. Redistributions in binary form must reproduce the above copyright 12299425Smm * notice, this list of conditions and the following disclaimer in the 13299425Smm * documentation and/or other materials provided with the distribution. 14299425Smm * 15299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25299425Smm */ 26299425Smm 27299425Smm/* 28299425Smm * Development supported by Google Summer of Code 2008. 29299425Smm */ 30299425Smm 31299425Smm#include "test.h" 32370535Sgit2svn__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/test/test_write_format_zip_file.c 370535 2021-09-10 08:34:36Z git2svn $"); 33299425Smm 34299425Smm/* 35299425Smm * Detailed byte-for-byte verification of the format of a zip archive 36299425Smm * with a single file written to it. 37299425Smm */ 38299425Smm 39299425Smmstatic unsigned long 40299425Smmbitcrc32(unsigned long c, void *_p, size_t s) 41299425Smm{ 42299425Smm /* This is a drop-in replacement for crc32() from zlib. 43299425Smm * Libarchive should be able to correctly generate 44299425Smm * uncompressed zip archives (including correct CRCs) even 45299425Smm * when zlib is unavailable, and this function helps us verify 46299425Smm * that. Yes, this is very, very slow and unsuitable for 47299425Smm * production use, but it's correct, compact, and works well 48299425Smm * enough for this particular usage. Libarchive internally 49299425Smm * uses a much more efficient implementation. */ 50299425Smm const unsigned char *p = _p; 51299425Smm int bitctr; 52299425Smm 53299425Smm if (p == NULL) 54299425Smm return (0); 55299425Smm 56299425Smm for (; s > 0; --s) { 57299425Smm c ^= *p++; 58299425Smm for (bitctr = 8; bitctr > 0; --bitctr) { 59299425Smm if (c & 1) c = (c >> 1); 60299425Smm else c = (c >> 1) ^ 0xedb88320; 61299425Smm c ^= 0x80000000; 62299425Smm } 63299425Smm } 64299425Smm return (c); 65299425Smm} 66299425Smm 67299425Smm/* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */ 68299425Smmstatic unsigned i2(const unsigned char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); } 69299425Smmstatic unsigned i4(const unsigned char *p) { return (i2(p) | (i2(p + 2) << 16)); } 70299425Smm 71299425SmmDEFINE_TEST(test_write_format_zip_file) 72299425Smm{ 73299425Smm struct archive *a; 74299425Smm struct archive_entry *ae; 75299425Smm time_t t = 1234567890; 76299425Smm struct tm *tm = localtime(&t); 77299425Smm size_t used, buffsize = 1000000; 78299425Smm unsigned long crc; 79299425Smm int file_perm = 00644; 80299425Smm int zip_version = 20; 81299425Smm int zip_compression = 8; 82299425Smm short file_uid = 10, file_gid = 20; 83299425Smm unsigned char *buff, *buffend, *p; 84299425Smm unsigned char *central_header, *local_header, *eocd, *eocd_record; 85299425Smm unsigned char *extension_start, *extension_end; 86299425Smm char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'}; 87358088Smm const char *file_name = "file"; 88299425Smm 89299425Smm#ifndef HAVE_ZLIB_H 90299425Smm zip_version = 10; 91299425Smm zip_compression = 0; 92299425Smm#endif 93299425Smm 94299425Smm buff = malloc(buffsize); 95299425Smm 96299425Smm /* Create a new archive in memory. */ 97299425Smm assert((a = archive_write_new()) != NULL); 98299425Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 99299425Smm assertEqualIntA(a, ARCHIVE_OK, 100299425Smm archive_write_set_options(a, "zip:experimental")); 101299425Smm assertEqualIntA(a, ARCHIVE_OK, 102299425Smm archive_write_open_memory(a, buff, buffsize, &used)); 103299425Smm 104299425Smm assert((ae = archive_entry_new()) != NULL); 105299425Smm archive_entry_copy_pathname(ae, file_name); 106299425Smm archive_entry_set_mode(ae, AE_IFREG | file_perm); 107299425Smm archive_entry_set_size(ae, sizeof(file_data)); 108299425Smm archive_entry_set_uid(ae, file_uid); 109299425Smm archive_entry_set_gid(ae, file_gid); 110299425Smm archive_entry_set_mtime(ae, t, 0); 111299425Smm assertEqualInt(0, archive_write_header(a, ae)); 112299425Smm archive_entry_free(ae); 113299425Smm assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data))); 114299425Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 115299425Smm assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 116299425Smm buffend = buff + used; 117299425Smm dumpfile("constructed.zip", buff, used); 118299425Smm 119299425Smm /* Verify "End of Central Directory" record. */ 120299425Smm /* Get address of end-of-central-directory record. */ 121299425Smm eocd_record = p = buffend - 22; /* Assumes there is no zip comment field. */ 122299425Smm failure("End-of-central-directory begins with PK\\005\\006 signature"); 123299425Smm assertEqualMem(p, "PK\005\006", 4); 124299425Smm failure("This must be disk 0"); 125299425Smm assertEqualInt(i2(p + 4), 0); 126299425Smm failure("Central dir must start on disk 0"); 127299425Smm assertEqualInt(i2(p + 6), 0); 128299425Smm failure("All central dir entries are on this disk"); 129299425Smm assertEqualInt(i2(p + 8), i2(p + 10)); 130299425Smm eocd = buff + i4(p + 12) + i4(p + 16); 131299425Smm failure("no zip comment"); 132299425Smm assertEqualInt(i2(p + 20), 0); 133299425Smm 134299425Smm /* Get address of first entry in central directory. */ 135299425Smm central_header = p = buff + i4(buffend - 6); 136299425Smm failure("Central file record at offset %d should begin with" 137299425Smm " PK\\001\\002 signature", 138299425Smm i4(buffend - 10)); 139299425Smm 140299425Smm /* Verify file entry in central directory. */ 141299425Smm assertEqualMem(p, "PK\001\002", 4); /* Signature */ 142299425Smm assertEqualInt(i2(p + 4), 3 * 256 + zip_version); /* Version made by */ 143299425Smm assertEqualInt(i2(p + 6), zip_version); /* Version needed to extract */ 144299425Smm assertEqualInt(i2(p + 8), 8); /* Flags */ 145299425Smm assertEqualInt(i2(p + 10), zip_compression); /* Compression method */ 146299425Smm assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 147299425Smm assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 148299425Smm crc = bitcrc32(0, file_data, sizeof(file_data)); 149299425Smm assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 150299425Smm /* assertEqualInt(i4(p + 20), sizeof(file_data)); */ /* Compressed size */ 151299425Smm assertEqualInt(i4(p + 24), sizeof(file_data)); /* Uncompressed size */ 152299425Smm assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */ 153299425Smm /* assertEqualInt(i2(p + 30), 28); */ /* Extra field length: See below */ 154299425Smm assertEqualInt(i2(p + 32), 0); /* File comment length */ 155299425Smm assertEqualInt(i2(p + 34), 0); /* Disk number start */ 156299425Smm assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 157299425Smm assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */ 158299425Smm assertEqualInt(i4(p + 42), 0); /* Offset of local header */ 159299425Smm assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */ 160299425Smm p = extension_start = central_header + 46 + strlen(file_name); 161299425Smm extension_end = extension_start + i2(central_header + 30); 162299425Smm 163299425Smm assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 164299425Smm assertEqualInt(i2(p + 2), 5); /* 'UT' size */ 165299425Smm assertEqualInt(p[4], 1); /* 'UT' flags */ 166299425Smm assertEqualInt(i4(p + 5), t); /* 'UT' mtime */ 167299425Smm p += 4 + i2(p + 2); 168299425Smm 169299425Smm assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 170299425Smm assertEqualInt(i2(p + 2), 11); /* 'ux' size */ 171299425Smm /* TODO: verify 'ux' contents */ 172299425Smm p += 4 + i2(p + 2); 173299425Smm 174299425Smm /* Just in case: Report any extra extensions. */ 175299425Smm while (p < extension_end) { 176299425Smm failure("Unexpected extension 0x%04X", i2(p)); 177299425Smm assert(0); 178299425Smm p += 4 + i2(p + 2); 179299425Smm } 180299425Smm 181299425Smm /* Should have run exactly to end of extra data. */ 182299425Smm assert(p == extension_end); 183299425Smm 184299425Smm assert(p == eocd); 185299425Smm 186299425Smm /* Regular EOCD immediately follows central directory. */ 187299425Smm assert(p == eocd_record); 188299425Smm 189299425Smm /* Verify local header of file entry. */ 190299425Smm p = local_header = buff; 191299425Smm assertEqualMem(p, "PK\003\004", 4); /* Signature */ 192299425Smm assertEqualInt(i2(p + 4), zip_version); /* Version needed to extract */ 193299425Smm assertEqualInt(i2(p + 6), 8); /* Flags */ 194299425Smm assertEqualInt(i2(p + 8), zip_compression); /* Compression method */ 195299425Smm assertEqualInt(i2(p + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 196299425Smm assertEqualInt(i2(p + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 197299425Smm assertEqualInt(i4(p + 14), 0); /* CRC-32 */ 198299425Smm /* assertEqualInt(i4(p + 18), sizeof(file_data)); */ /* Compressed size */ 199299425Smm /* assertEqualInt(i4(p + 22), sizeof(file_data)); */ /* Uncompressed size not stored because we're using length-at-end. */ 200299425Smm assertEqualInt(i2(p + 26), strlen(file_name)); /* Pathname length */ 201299425Smm assertEqualInt(i2(p + 28), 37); /* Extra field length */ 202299425Smm assertEqualMem(p + 30, file_name, strlen(file_name)); /* Pathname */ 203299425Smm p = extension_start = local_header + 30 + strlen(file_name); 204299425Smm extension_end = extension_start + i2(local_header + 28); 205299425Smm 206299425Smm assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 207299425Smm assertEqualInt(i2(p + 2), 5); /* size */ 208299425Smm assertEqualInt(p[4], 1); /* 'UT' flags */ 209299425Smm assertEqualInt(i4(p + 5), t); /* 'UT' mtime */ 210299425Smm p += 4 + i2(p + 2); 211299425Smm 212299425Smm assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 213299425Smm assertEqualInt(i2(p + 2), 11); /* size */ 214299425Smm assertEqualInt(p[4], 1); /* 'ux' version */ 215299425Smm assertEqualInt(p[5], 4); /* 'ux' uid size */ 216299425Smm assertEqualInt(i4(p + 6), file_uid); /* 'Ux' UID */ 217299425Smm assertEqualInt(p[10], 4); /* 'ux' gid size */ 218299425Smm assertEqualInt(i4(p + 11), file_gid); /* 'Ux' GID */ 219299425Smm p += 4 + i2(p + 2); 220299425Smm 221299425Smm assertEqualInt(i2(p), 0x6c78); /* 'xl' experimental extension block */ 222299425Smm assertEqualInt(i2(p + 2), 9); /* size */ 223299425Smm assertEqualInt(p[4], 7); /* bitmap of fields in this block */ 224299425Smm assertEqualInt(i2(p + 5) >> 8, 3); /* System & version made by */ 225299425Smm assertEqualInt(i2(p + 7), 0); /* internal file attributes */ 226299425Smm assertEqualInt(i4(p + 9) >> 16 & 01777, file_perm); /* external file attributes */ 227299425Smm p += 4 + i2(p + 2); 228299425Smm 229299425Smm /* Just in case: Report any extra extensions. */ 230299425Smm while (p < extension_end) { 231299425Smm failure("Unexpected extension 0x%04X", i2(p)); 232299425Smm assert(0); 233299425Smm p += 4 + i2(p + 2); 234299425Smm } 235299425Smm 236299425Smm /* Should have run exactly to end of extra data. */ 237299425Smm assert(p == extension_end); 238299425Smm 239299425Smm /* Data descriptor should follow compressed data. */ 240299425Smm while (p < central_header && memcmp(p, "PK\007\010", 4) != 0) 241299425Smm ++p; 242299425Smm assertEqualMem(p, "PK\007\010", 4); 243299425Smm assertEqualInt(i4(p + 4), crc); /* CRC-32 */ 244299425Smm /* assertEqualInt(i4(p + 8), ???); */ /* compressed size */ 245299425Smm assertEqualInt(i4(p + 12), sizeof(file_data)); /* uncompressed size */ 246299425Smm 247299425Smm /* Central directory should immediately follow the only entry. */ 248299425Smm assert(p + 16 == central_header); 249299425Smm 250299425Smm free(buff); 251299425Smm} 252