test_write_format_zip_file.c [plain text]
#include "test.h"
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_format_zip.c 201247 2009-12-30 05:59:21Z kientzle $");
static unsigned long
bitcrc32(unsigned long c, void *_p, size_t s)
{
const unsigned char *p = _p;
int bitctr;
if (p == NULL)
return (0);
for (; s > 0; --s) {
c ^= *p++;
for (bitctr = 8; bitctr > 0; --bitctr) {
if (c & 1) c = (c >> 1);
else c = (c >> 1) ^ 0xedb88320;
c ^= 0x80000000;
}
}
return (c);
}
static unsigned i2(const unsigned char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
static unsigned i4(const unsigned char *p) { return (i2(p) | (i2(p + 2) << 16)); }
DEFINE_TEST(test_write_format_zip_file)
{
struct archive *a;
struct archive_entry *ae;
time_t t = 1234567890;
struct tm *tm = localtime(&t);
size_t used, buffsize = 1000000;
unsigned long crc;
int file_perm = 00644;
int zip_version = 20;
int zip_compression = 8;
short file_uid = 10, file_gid = 20;
unsigned char *buff, *buffend, *p;
unsigned char *central_header, *local_header, *eocd, *eocd_record;
unsigned char *extension_start, *extension_end;
char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
char *file_name = "file";
#ifndef HAVE_ZLIB_H
zip_version = 10;
zip_compression = 0;
#endif
buff = malloc(buffsize);
assert((a = archive_write_new()) != NULL);
assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
assertEqualIntA(a, ARCHIVE_OK,
archive_write_set_options(a, "zip:experimental"));
assertEqualIntA(a, ARCHIVE_OK,
archive_write_open_memory(a, buff, buffsize, &used));
assert((ae = archive_entry_new()) != NULL);
archive_entry_copy_pathname(ae, file_name);
archive_entry_set_mode(ae, AE_IFREG | file_perm);
archive_entry_set_size(ae, sizeof(file_data));
archive_entry_set_uid(ae, file_uid);
archive_entry_set_gid(ae, file_gid);
archive_entry_set_mtime(ae, t, 0);
assertEqualInt(0, archive_write_header(a, ae));
archive_entry_free(ae);
assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data)));
assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
assertEqualInt(ARCHIVE_OK, archive_write_free(a));
buffend = buff + used;
dumpfile("constructed.zip", buff, used);
eocd_record = p = buffend - 22;
failure("End-of-central-directory begins with PK\\005\\006 signature");
assertEqualMem(p, "PK\005\006", 4);
failure("This must be disk 0");
assertEqualInt(i2(p + 4), 0);
failure("Central dir must start on disk 0");
assertEqualInt(i2(p + 6), 0);
failure("All central dir entries are on this disk");
assertEqualInt(i2(p + 8), i2(p + 10));
eocd = buff + i4(p + 12) + i4(p + 16);
failure("no zip comment");
assertEqualInt(i2(p + 20), 0);
central_header = p = buff + i4(buffend - 6);
failure("Central file record at offset %d should begin with"
" PK\\001\\002 signature",
i4(buffend - 10));
assertEqualMem(p, "PK\001\002", 4);
assertEqualInt(i2(p + 4), 3 * 256 + zip_version);
assertEqualInt(i2(p + 6), zip_version);
assertEqualInt(i2(p + 8), 8);
assertEqualInt(i2(p + 10), zip_compression);
assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2));
assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday);
crc = bitcrc32(0, file_data, sizeof(file_data));
assertEqualInt(i4(p + 16), crc);
assertEqualInt(i4(p + 24), sizeof(file_data));
assertEqualInt(i2(p + 28), strlen(file_name));
assertEqualInt(i2(p + 32), 0);
assertEqualInt(i2(p + 34), 0);
assertEqualInt(i2(p + 36), 0);
assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm);
assertEqualInt(i4(p + 42), 0);
assertEqualMem(p + 46, file_name, strlen(file_name));
p = extension_start = central_header + 46 + strlen(file_name);
extension_end = extension_start + i2(central_header + 30);
assertEqualInt(i2(p), 0x5455);
assertEqualInt(i2(p + 2), 5);
assertEqualInt(p[4], 1);
assertEqualInt(i4(p + 5), t);
p += 4 + i2(p + 2);
assertEqualInt(i2(p), 0x7875);
assertEqualInt(i2(p + 2), 11);
p += 4 + i2(p + 2);
while (p < extension_end) {
failure("Unexpected extension 0x%04X", i2(p));
assert(0);
p += 4 + i2(p + 2);
}
assert(p == extension_end);
assert(p == eocd);
assert(p == eocd_record);
p = local_header = buff;
assertEqualMem(p, "PK\003\004", 4);
assertEqualInt(i2(p + 4), zip_version);
assertEqualInt(i2(p + 6), 8);
assertEqualInt(i2(p + 8), zip_compression);
assertEqualInt(i2(p + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2));
assertEqualInt(i2(p + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday);
assertEqualInt(i4(p + 14), 0);
assertEqualInt(i2(p + 26), strlen(file_name));
assertEqualInt(i2(p + 28), 37);
assertEqualMem(p + 30, file_name, strlen(file_name));
p = extension_start = local_header + 30 + strlen(file_name);
extension_end = extension_start + i2(local_header + 28);
assertEqualInt(i2(p), 0x5455);
assertEqualInt(i2(p + 2), 5);
assertEqualInt(p[4], 1);
assertEqualInt(i4(p + 5), t);
p += 4 + i2(p + 2);
assertEqualInt(i2(p), 0x7875);
assertEqualInt(i2(p + 2), 11);
assertEqualInt(p[4], 1);
assertEqualInt(p[5], 4);
assertEqualInt(i4(p + 6), file_uid);
assertEqualInt(p[10], 4);
assertEqualInt(i4(p + 11), file_gid);
p += 4 + i2(p + 2);
assertEqualInt(i2(p), 0x6c78);
assertEqualInt(i2(p + 2), 9);
assertEqualInt(p[4], 7);
assertEqualInt(i2(p + 5) >> 8, 3);
assertEqualInt(i2(p + 7), 0);
assertEqualInt(i4(p + 9) >> 16 & 01777, file_perm);
p += 4 + i2(p + 2);
while (p < extension_end) {
failure("Unexpected extension 0x%04X", i2(p));
assert(0);
p += 4 + i2(p + 2);
}
assert(p == extension_end);
while (p < central_header && memcmp(p, "PK\007\010", 4) != 0)
++p;
assertEqualMem(p, "PK\007\010", 4);
assertEqualInt(i4(p + 4), crc);
assertEqualInt(i4(p + 12), sizeof(file_data));
assert(p + 16 == central_header);
free(buff);
}