xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip_stream.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2003-2023 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 #include "test.h"
28 
29 /*
30  * Detailed byte-for-byte verification of the format of a zip archive
31  * written in streaming mode WITHOUT Zip64 extensions enabled.
32  */
33 
DEFINE_TEST(test_write_format_zip_stream)34 DEFINE_TEST(test_write_format_zip_stream)
35 {
36 	struct archive *a;
37 	struct archive_entry *ae;
38 	size_t used, buffsize = 1000000;
39 	unsigned long crc;
40 	unsigned long compressed_size = 0;
41 	__LA_MODE_T file_perm = 00644;
42 #ifdef HAVE_ZLIB_H
43 	int zip_version = 20;
44 #else
45 	int zip_version = 10;
46 #endif
47 	int zip_compression = 8;
48 	short file_uid = 10, file_gid = 20;
49 	unsigned char *buff, *buffend, *p;
50 	unsigned char *central_header, *local_header, *eocd, *eocd_record;
51 	unsigned char *extension_start, *extension_end;
52 	unsigned char *data_start, *data_end;
53 	char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
54 	const char *file_name = "file";
55 
56 #ifndef HAVE_ZLIB_H
57 	zip_version = 10;
58 	zip_compression = 0;
59 #endif
60 
61 	buff = malloc(buffsize);
62 
63 	/* Create a new archive in memory. */
64 	assert((a = archive_write_new()) != NULL);
65 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
66 	assertEqualIntA(a, ARCHIVE_OK,
67 	    archive_write_set_options(a, "zip:!zip64"));
68 	assertEqualIntA(a, ARCHIVE_OK,
69 	    archive_write_open_memory(a, buff, buffsize, &used));
70 
71 	assert((ae = archive_entry_new()) != NULL);
72 	archive_entry_copy_pathname(ae, file_name);
73 	archive_entry_set_mode(ae, AE_IFREG | file_perm);
74 	archive_entry_set_uid(ae, file_uid);
75 	archive_entry_set_gid(ae, file_gid);
76 	archive_entry_set_mtime(ae, 0, 0);
77 	assertEqualInt(0, archive_write_header(a, ae));
78 	archive_entry_free(ae);
79 	assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data)));
80 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
81 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
82 	buffend = buff + used;
83 	dumpfile("constructed.zip", buff, used);
84 
85 	/* Verify "End of Central Directory" record. */
86 	/* Get address of end-of-central-directory record. */
87 	eocd_record = p = buffend - 22; /* Assumes there is no zip comment field. */
88 	failure("End-of-central-directory begins with PK\\005\\006 signature");
89 	assertEqualMem(p, "PK\005\006", 4);
90 	failure("This must be disk 0");
91 	assertEqualInt(i2le(p + 4), 0);
92 	failure("Central dir must start on disk 0");
93 	assertEqualInt(i2le(p + 6), 0);
94 	failure("All central dir entries are on this disk");
95 	assertEqualInt(i2le(p + 8), i2le(p + 10));
96 	eocd = buff + i4le(p + 12) + i4le(p + 16);
97 	failure("no zip comment");
98 	assertEqualInt(i2le(p + 20), 0);
99 
100 	/* Get address of first entry in central directory. */
101 	central_header = p = buff + i4le(buffend - 6);
102 	failure("Central file record at offset %u should begin with"
103 	    " PK\\001\\002 signature",
104 	    i4le(buffend - 10));
105 
106 	/* Verify file entry in central directory. */
107 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
108 	assertEqualInt(i2le(p + 4), 3 * 256 + zip_version); /* Version made by */
109 	assertEqualInt(i2le(p + 6), zip_version); /* Version needed to extract */
110 	assertEqualInt(i2le(p + 8), 8); /* Flags */
111 	assertEqualInt(i2le(p + 10), zip_compression); /* Compression method */
112 	assertEqualInt(i2le(p + 12), 0); /* File time */
113 	assertEqualInt(i2le(p + 14), 33); /* File date */
114 	crc = bitcrc32(0, file_data, sizeof(file_data));
115 	assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
116 	compressed_size = i4le(p + 20);  /* Compressed size */
117 	assertEqualInt(i4le(p + 24), sizeof(file_data)); /* Uncompressed size */
118 	assertEqualInt(i2le(p + 28), strlen(file_name)); /* Pathname length */
119 	/* assertEqualInt(i2le(p + 30), 28); */ /* Extra field length: See below */
120 	assertEqualInt(i2le(p + 32), 0); /* File comment length */
121 	assertEqualInt(i2le(p + 34), 0); /* Disk number start */
122 	assertEqualInt(i2le(p + 36), 0); /* Internal file attrs */
123 	assertEqualInt(i4le(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
124 	assertEqualInt(i4le(p + 42), 0); /* Offset of local header */
125 	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
126 	p = extension_start = central_header + 46 + strlen(file_name);
127 	extension_end = extension_start + i2le(central_header + 30);
128 
129 	assertEqualInt(i2le(p), 0x7875);  /* 'ux' extension header */
130 	assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
131 	assertEqualInt(p[4], 1); /* 'ux' version */
132 	assertEqualInt(p[5], 4); /* 'ux' uid size */
133 	assertEqualInt(i4le(p + 6), file_uid); /* 'Ux' UID */
134 	assertEqualInt(p[10], 4); /* 'ux' gid size */
135 	assertEqualInt(i4le(p + 11), file_gid); /* 'Ux' GID */
136 	p += 4 + i2le(p + 2);
137 
138 	assertEqualInt(i2le(p), 0x5455);  /* 'UT' extension header */
139 	assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
140 	assertEqualInt(p[4], 1); /* 'UT' flags */
141 	assertEqualInt(i4le(p + 5), 0); /* 'UT' mtime */
142 	p += 4 + i2le(p + 2);
143 
144 	/* Note: We don't expect to see zip64 extension in the central
145 	 * directory, since the writer knows the actual full size by
146 	 * the time it is ready to write the central directory and has
147 	 * no reason to insert it then.  Info-Zip seems to do the same
148 	 * thing. */
149 
150 	/* Just in case: Report any extra extensions. */
151 	while (p < extension_end) {
152 		failure("Unexpected extension 0x%04X", i2le(p));
153 		assert(0);
154 		p += 4 + i2le(p + 2);
155 	}
156 
157 	/* Should have run exactly to end of extra data. */
158 	assert(p == extension_end);
159 
160 	assert(p == eocd);
161 	assert(p == eocd_record);
162 
163 	/* Verify local header of file entry. */
164 	p = local_header = buff;
165 	assertEqualMem(p, "PK\003\004", 4); /* Signature */
166 	assertEqualInt(i2le(p + 4), zip_version); /* Version needed to extract */
167 	assertEqualInt(i2le(p + 6), 8); /* Flags */
168 	assertEqualInt(i2le(p + 8), zip_compression); /* Compression method */
169 	assertEqualInt(i2le(p + 10), 0); /* File time */
170 	assertEqualInt(i2le(p + 12), 33); /* File date */
171 	assertEqualInt(i4le(p + 14), 0); /* CRC-32 */
172 	assertEqualInt(i4le(p + 18), 0); /* Compressed size */
173 	assertEqualInt(i4le(p + 22), 0); /* Uncompressed size */
174 	assertEqualInt(i2le(p + 26), strlen(file_name)); /* Pathname length */
175 	assertEqualInt(i2le(p + 28), 24); /* Extra field length */
176 	assertEqualMem(p + 30, file_name, strlen(file_name)); /* Pathname */
177 	p = extension_start = local_header + 30 + strlen(file_name);
178 	extension_end = extension_start + i2le(local_header + 28);
179 
180 	assertEqualInt(i2le(p), 0x7875);  /* 'ux' extension header */
181 	assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
182 	assertEqualInt(p[4], 1); /* 'ux' version */
183 	assertEqualInt(p[5], 4); /* 'ux' uid size */
184 	assertEqualInt(i4le(p + 6), file_uid); /* 'Ux' UID */
185 	assertEqualInt(p[10], 4); /* 'ux' gid size */
186 	assertEqualInt(i4le(p + 11), file_gid); /* 'Ux' GID */
187 	p += 4 + i2le(p + 2);
188 
189 	assertEqualInt(i2le(p), 0x5455);  /* 'UT' extension header */
190 	assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
191 	assertEqualInt(p[4], 1); /* 'UT' flags */
192 	assertEqualInt(i4le(p + 5), 0); /* 'UT' mtime */
193 	p += 4 + i2le(p + 2);
194 
195 	/* Just in case: Report any extra extensions. */
196 	while (p < extension_end) {
197 		failure("Unexpected extension 0x%04X", i2le(p));
198 		assert(0);
199 		p += 4 + i2le(p + 2);
200 	}
201 
202 	/* Should have run exactly to end of extra data. */
203 	assert(p == extension_end);
204 	data_start = p;
205 
206 	/* Data descriptor should follow compressed data. */
207 	while (p < central_header && memcmp(p, "PK\007\010", 4) != 0)
208 		++p;
209 	data_end = p;
210 	assertEqualInt(data_end - data_start, compressed_size);
211 	assertEqualMem(p, "PK\007\010", 4);
212 	assertEqualInt(i4le(p + 4), crc); /* CRC-32 */
213 	assertEqualInt(i4le(p + 8), compressed_size); /* compressed size */
214 	assertEqualInt(i4le(p + 12), sizeof(file_data)); /* uncompressed size */
215 
216 	/* Central directory should immediately follow the data descriptor. */
217 	assert(p + 16 == central_header);
218 
219 	free(buff);
220 }
221