xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip_file.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2003-2008 Tim Kientzle
3  * Copyright (c) 2008 Anselm Strauss
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * Development supported by Google Summer of Code 2008.
29  */
30 
31 #include "test.h"
32 
33 /*
34  * Detailed byte-for-byte verification of the format of a zip archive
35  * with a single file written to it.
36  */
37 
DEFINE_TEST(test_write_format_zip_file)38 DEFINE_TEST(test_write_format_zip_file)
39 {
40 	struct archive *a;
41 	struct archive_entry *ae;
42 	time_t t = 1234567890;
43 	struct tm *tm;
44 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
45 	struct tm tmbuf;
46 #endif
47 	size_t used, buffsize = 1000000;
48 	unsigned long crc;
49 	__LA_MODE_T file_perm = 00644;
50 	int zip_version = 20;
51 	int zip_compression = 8;
52 	short file_uid = 10, file_gid = 20;
53 	unsigned char *buff, *buffend, *p;
54 	unsigned char *central_header, *local_header, *eocd, *eocd_record;
55 	unsigned char *extension_start, *extension_end;
56 	char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
57 	const char *file_name = "file";
58 
59 #ifndef HAVE_ZLIB_H
60 	zip_version = 10;
61 	zip_compression = 0;
62 #endif
63 
64 #if defined(HAVE_LOCALTIME_S)
65 	tm = localtime_s(&tmbuf, &t) ? NULL : &tmbuf;
66 #elif defined(HAVE_LOCALTIME_R)
67 	tm = localtime_r(&t, &tmbuf);
68 #else
69 	tm = localtime(&t);
70 #endif
71 	buff = malloc(buffsize);
72 
73 	/* Create a new archive in memory. */
74 	assert((a = archive_write_new()) != NULL);
75 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
76 	assertEqualIntA(a, ARCHIVE_OK,
77 	    archive_write_set_options(a, "zip:experimental"));
78 	assertEqualIntA(a, ARCHIVE_OK,
79 	    archive_write_open_memory(a, buff, buffsize, &used));
80 
81 	assert((ae = archive_entry_new()) != NULL);
82 	archive_entry_copy_pathname(ae, file_name);
83 	archive_entry_set_mode(ae, AE_IFREG | file_perm);
84 	archive_entry_set_size(ae, sizeof(file_data));
85 	archive_entry_set_uid(ae, file_uid);
86 	archive_entry_set_gid(ae, file_gid);
87 	archive_entry_set_mtime(ae, t, 0);
88 	assertEqualInt(0, archive_write_header(a, ae));
89 	archive_entry_free(ae);
90 	assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data)));
91 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
92 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
93 	buffend = buff + used;
94 	dumpfile("constructed.zip", buff, used);
95 
96 	/* Verify "End of Central Directory" record. */
97 	/* Get address of end-of-central-directory record. */
98 	eocd_record = p = buffend - 22; /* Assumes there is no zip comment field. */
99 	failure("End-of-central-directory begins with PK\\005\\006 signature");
100 	assertEqualMem(p, "PK\005\006", 4);
101 	failure("This must be disk 0");
102 	assertEqualInt(i2le(p + 4), 0);
103 	failure("Central dir must start on disk 0");
104 	assertEqualInt(i2le(p + 6), 0);
105 	failure("All central dir entries are on this disk");
106 	assertEqualInt(i2le(p + 8), i2le(p + 10));
107 	eocd = buff + i4le(p + 12) + i4le(p + 16);
108 	failure("no zip comment");
109 	assertEqualInt(i2le(p + 20), 0);
110 
111 	/* Get address of first entry in central directory. */
112 	central_header = p = buff + i4le(buffend - 6);
113 	failure("Central file record at offset %u should begin with"
114 	    " PK\\001\\002 signature",
115 	    i4le(buffend - 10));
116 
117 	/* Verify file entry in central directory. */
118 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
119 	assertEqualInt(i2le(p + 4), 3 * 256 + zip_version); /* Version made by */
120 	assertEqualInt(i2le(p + 6), zip_version); /* Version needed to extract */
121 	assertEqualInt(i2le(p + 8), 8); /* Flags */
122 	assertEqualInt(i2le(p + 10), zip_compression); /* Compression method */
123 	assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
124 	assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
125 	crc = bitcrc32(0, file_data, sizeof(file_data));
126 	assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
127 	/* assertEqualInt(i4le(p + 20), sizeof(file_data)); */ /* Compressed size */
128 	assertEqualInt(i4le(p + 24), sizeof(file_data)); /* Uncompressed size */
129 	assertEqualInt(i2le(p + 28), strlen(file_name)); /* Pathname length */
130 	/* assertEqualInt(i2le(p + 30), 28); */ /* Extra field length: See below */
131 	assertEqualInt(i2le(p + 32), 0); /* File comment length */
132 	assertEqualInt(i2le(p + 34), 0); /* Disk number start */
133 	assertEqualInt(i2le(p + 36), 0); /* Internal file attrs */
134 	assertEqualInt(i4le(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
135 	assertEqualInt(i4le(p + 42), 0); /* Offset of local header */
136 	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
137 	p = extension_start = central_header + 46 + strlen(file_name);
138 	extension_end = extension_start + i2le(central_header + 30);
139 
140 	assertEqualInt(i2le(p), 0x7875);  /* 'ux' extension header */
141 	assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
142 	/* TODO: verify 'ux' contents */
143 	p += 4 + i2le(p + 2);
144 
145 	assertEqualInt(i2le(p), 0x5455);  /* 'UT' extension header */
146 	assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
147 	assertEqualInt(p[4], 1); /* 'UT' flags */
148 	assertEqualInt(i4le(p + 5), t); /* 'UT' mtime */
149 	p += 4 + i2le(p + 2);
150 
151 	/* Just in case: Report any extra extensions. */
152 	while (p < extension_end) {
153 		failure("Unexpected extension 0x%04X", i2le(p));
154 		assert(0);
155 		p += 4 + i2le(p + 2);
156 	}
157 
158 	/* Should have run exactly to end of extra data. */
159 	assertEqualAddress(p, extension_end);
160 
161 	assertEqualAddress(p, eocd);
162 
163 	/* Regular EOCD immediately follows central directory. */
164 	assertEqualAddress(p, eocd_record);
165 
166 	/* Verify local header of file entry. */
167 	p = local_header = buff;
168 	assertEqualMem(p, "PK\003\004", 4); /* Signature */
169 	assertEqualInt(i2le(p + 4), zip_version); /* Version needed to extract */
170 	assertEqualInt(i2le(p + 6), 8); /* Flags: bit 3 = length-at-end */
171 	assertEqualInt(i2le(p + 8), zip_compression); /* Compression method */
172 	assertEqualInt(i2le(p + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
173 	assertEqualInt(i2le(p + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
174 	assertEqualInt(i4le(p + 14), 0); /* CRC-32 stored as zero because we're using length-at-end */
175 	assertEqualInt(i4le(p + 18), 0); /* Compressed size stored as zero because we're using length-at-end. */
176 	assertEqualInt(i4le(p + 22), 0); /* Uncompressed size stored as zero because we're using length-at-end. */
177 	assertEqualInt(i2le(p + 26), strlen(file_name)); /* Pathname length */
178 	assertEqualInt(i2le(p + 28), 37); /* Extra field length */
179 	assertEqualMem(p + 30, file_name, strlen(file_name)); /* Pathname */
180 	p = extension_start = local_header + 30 + strlen(file_name);
181 	extension_end = extension_start + i2le(local_header + 28);
182 
183 	assertEqualInt(i2le(p), 0x7875);  /* 'ux' extension header */
184 	assertEqualInt(i2le(p + 2), 11); /* size */
185 	assertEqualInt(p[4], 1); /* 'ux' version */
186 	assertEqualInt(p[5], 4); /* 'ux' uid size */
187 	assertEqualInt(i4le(p + 6), file_uid); /* 'Ux' UID */
188 	assertEqualInt(p[10], 4); /* 'ux' gid size */
189 	assertEqualInt(i4le(p + 11), file_gid); /* 'Ux' GID */
190 	p += 4 + i2le(p + 2);
191 
192 	assertEqualInt(i2le(p), 0x5455);  /* 'UT' extension header */
193 	assertEqualInt(i2le(p + 2), 5); /* size */
194 	assertEqualInt(p[4], 1); /* 'UT' flags */
195 	assertEqualInt(i4le(p + 5), t); /* 'UT' mtime */
196 	p += 4 + i2le(p + 2);
197 
198 	assertEqualInt(i2le(p), 0x6c78); /* 'xl' experimental extension block */
199 	assertEqualInt(i2le(p + 2), 9); /* size */
200 	assertEqualInt(p[4], 7); /* bitmap of fields in this block */
201 	assertEqualInt(i2le(p + 5) >> 8, 3); /* System & version made by */
202 	assertEqualInt(i2le(p + 7), 0); /* internal file attributes */
203 	assertEqualInt(i4le(p + 9) >> 16 & 01777, file_perm); /* external file attributes */
204 	p += 4 + i2le(p + 2);
205 
206 	/* Just in case: Report any extra extensions. */
207 	while (p < extension_end) {
208 		failure("Unexpected extension 0x%04X", i2le(p));
209 		assert(0);
210 		p += 4 + i2le(p + 2);
211 	}
212 
213 	/* Should have run exactly to end of extra data. */
214 	assertEqualAddress(p, extension_end);
215 
216 	/* Data descriptor should follow compressed data. */
217 	while (p < central_header && memcmp(p, "PK\007\010", 4) != 0)
218 		++p;
219 	assertEqualMem(p, "PK\007\010", 4);
220 	assertEqualInt(i4le(p + 4), crc); /* CRC-32 */
221 	assertEqualInt(i4le(p + 8), p - extension_end); /* compressed size */
222 	assertEqualInt(i4le(p + 12), sizeof(file_data)); /* uncompressed size */
223 
224 	/* Central directory should immediately follow the only entry. */
225 	assertEqualAddress(p + 16, central_header);
226 
227 	free(buff);
228 }
229