PERFORCE change 146318 for review
Anselm Strauss
strauss at FreeBSD.org
Thu Jul 31 14:17:34 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=146318
Change 146318 by strauss at strauss_marvelman on 2008/07/31 14:16:48
More tests
Affected files ...
.. //depot/projects/soc2008/strauss_libarchive/libarchive/archive_write_set_format_zip.c#21 edit
.. //depot/projects/soc2008/strauss_libarchive/libarchive/test/test_write_format_zip_no_compression.c#4 edit
Differences ...
==== //depot/projects/soc2008/strauss_libarchive/libarchive/archive_write_set_format_zip.c#21 (text+ko) ====
==== //depot/projects/soc2008/strauss_libarchive/libarchive/test/test_write_format_zip_no_compression.c#4 (text+ko) ====
@@ -1,14 +1,19 @@
/*-
- * By Anselm Strauss.
- */
+* By Anselm Strauss.
+*/
#include "test.h"
__FBSDID("$Id$ $Change$ $DateTime$ $Author$");
+/* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
+static int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
+static int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
+
DEFINE_TEST(test_write_format_zip_no_compression)
{
struct archive *a;
struct archive_entry *entry;
char buff[100000];
+ const char *p, *q, *buffend;
size_t used;
/* Create new ZIP archive in memory without padding. */
@@ -18,9 +23,9 @@
assertA(0 == archive_write_set_bytes_per_block(a, 1));
assertA(0 == archive_write_set_bytes_in_last_block(a, 1));
assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
-
+
/* Write entries. */
-
+
/* Regular file */
assert((entry = archive_entry_new()) != NULL);
archive_entry_set_pathname(entry, "file");
@@ -33,16 +38,61 @@
archive_entry_set_nlink(entry, 1);
assertEqualIntA(a, 0, archive_write_header(a, entry));
assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
+ archive_entry_free(entry);
/* Close out the archive . */
assertA(0 == archive_write_close(a));
assertA(0 == archive_write_finish(a));
- /* Verify the correct format for an empy Zip archive. */
- //assertEqualInt(used, 22);
- //assertEqualMem(buff,
- // "PK\005\006\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
- // 22);
+ /* Verify the format of the Zip file. */
+ buffend = buff + used;
+
+ /* Verify "End of Central Directory" record. */
+ /* Get address of end-of-central-directory record. */
+ p = buffend - 22; /* Assumes there is no zip comment field. */
+ 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));
+ failure("CD start (%d) + CD length (%d) should == archive size - 22",
+ i4(p + 12), i4(p + 16));
+ assertEqualInt(i4(p + 12) + i4(p + 16), used - 22);
+ failure("no zip comment");
+ assertEqualInt(i2(p + 20), 0);
+
+ /* Get address of first entry in central directory. */
+ 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);
+ /* TODO: Verify that this central file record makes sense. */
+ /* assertEqualInt(i2(p + 4), XXXX); */ /* Version made by */
+ /* assertEqualInt(i2(p + 6), XXXX); */ /* Version needed to extract */
+ /* assertEqualInt(i2(p + 8), XXXX); */ /* Flags */
+ /* assertEqualInt(i2(p + 10), XXXX); */ /* Compression method */
+ /* assertEqualInt(i2(p + 12), XXXX); */ /* File time */
+ /* assertEqualInt(i2(p + 14), XXXX); */ /* File date */
+ /* assertEqualInt(i4(p + 16), XXXX); */ /* CRC-32 */
+ /* assertEqualInt(i4(p + 20), XXXX); */ /* Compressed size */
+ /* assertEqualInt(i4(p + 24), XXXX); */ /* Uncompressed size */
+ /* assertEqualInt(i2(p + 28), XXXX); */ /* Filename length */
+ /* assertEqualInt(i2(p + 30), XXXX); */ /* Extra field length */
+ assertEqualInt(i2(p + 32), 0); /* File comment length */
+ assertEqualInt(i2(p + 34), 0); /* Disk number start */
+ /* assertEqualInt(i2(p + 36), XXXX); */ /* Internal file attrs */
+ /* assertEqualInt(i4(p + 38), XXXX); */ /* External file attrs */
+
+ /* Get address of local header for this file. */
+ q = buff + i4(p + 42);
+ failure("Local file header at offset %d should begin with"
+ " PK\\003\\004 signature",
+ i4(p + 42));
+ assertEqualMem(q, "PK\003\004", 4);
+ /* TODO: Verify local header */
- /* TODO: free archive entries */
}
More information about the p4-projects
mailing list