authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-02-14 22:12:54+01:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-02-14 22:12:54+01:00
loge20080be1312da92e441aabc6627fa2721282bad
tree7be93bb6eedc5c9338ae0b766a4dcadb97f94a5d
parent0afe808928f3d4fd122c746d6eae1e88ff7aace1

preserve valuable tests from v1 implementation

Before removal of v1.

2 files changed, 114 insertions(+), 2 deletions(-)

lib/std/compress/flate/container.zig+3-2
...@@ -143,9 +143,10 @@ pub const Container = enum {...@@ -143,9 +143,10 @@ pub const Container = enum {
143 }143 }
144144
145 fn parseZlibHeader(reader: anytype) !void {145 fn parseZlibHeader(reader: anytype) !void {
146 const cinfo_cm = try reader.read(u8);146 const cm = try reader.read(u4);
147 const cinfo = try reader.read(u4);
147 _ = try reader.read(u8);148 _ = try reader.read(u8);
148 if (cinfo_cm != 0x78) {149 if (cm != 8 or cinfo > 7) {
149 return error.BadZlibHeader;150 return error.BadZlibHeader;
150 }151 }
151 }152 }
lib/std/compress/flate/flate.zig+111
...@@ -234,3 +234,114 @@ test "flate compress/decompress" {...@@ -234,3 +234,114 @@ test "flate compress/decompress" {
234 }234 }
235 }235 }
236}236}
237
238fn testDecompress(comptime container: Container, compressed: []const u8, expected_plain: []const u8) !void {
239 var in_stream = std.io.fixedBufferStream(compressed);
240 var al = std.ArrayList(u8).init(testing.allocator);
241 defer al.deinit();
242
243 try inflate.decompress(container, in_stream.reader(), al.writer());
244
245 try testing.expectEqualSlices(u8, expected_plain, al.items);
246}
247
248test "flate don't read past deflate stream's end" {
249 try testDecompress(.zlib, &[_]u8{
250 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0xc0, 0x00, 0xc1, 0xff,
251 0xff, 0x43, 0x30, 0x03, 0x03, 0xc3, 0xff, 0xff, 0xff, 0x01,
252 0x83, 0x95, 0x0b, 0xf5,
253 }, &[_]u8{
254 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
255 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
256 0x00, 0x00, 0xff, 0xff, 0xff,
257 });
258}
259
260test "flate zlib header" {
261 // Truncated header
262 try testing.expectError(
263 error.EndOfStream,
264 testDecompress(.zlib, &[_]u8{0x78}, ""),
265 );
266 // Wrong CM
267 try testing.expectError(
268 error.BadZlibHeader,
269 testDecompress(.zlib, &[_]u8{ 0x79, 0x94 }, ""),
270 );
271 // Wrong CINFO
272 try testing.expectError(
273 error.BadZlibHeader,
274 testDecompress(.zlib, &[_]u8{ 0x88, 0x98 }, ""),
275 );
276 // Wrong checksum
277 try testing.expectError(
278 error.WrongZlibChecksum,
279 testDecompress(.zlib, &[_]u8{ 0x78, 0xda, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }, ""),
280 );
281 // Truncated checksum
282 try testing.expectError(
283 error.EndOfStream,
284 testDecompress(.zlib, &[_]u8{ 0x78, 0xda, 0x03, 0x00, 0x00 }, ""),
285 );
286}
287
288test "flate gzip header" {
289 // Truncated header
290 try testing.expectError(
291 error.EndOfStream,
292 testDecompress(.gzip, &[_]u8{ 0x1f, 0x8B }, undefined),
293 );
294 // Wrong CM
295 try testing.expectError(
296 error.BadGzipHeader,
297 testDecompress(.gzip, &[_]u8{
298 0x1f, 0x8b, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
299 0x00, 0x03,
300 }, undefined),
301 );
302
303 // Wrong checksum
304 try testing.expectError(
305 error.WrongGzipChecksum,
306 testDecompress(.gzip, &[_]u8{
307 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01,
309 0x00, 0x00, 0x00, 0x00,
310 }, undefined),
311 );
312 // Truncated checksum
313 try testing.expectError(
314 error.EndOfStream,
315 testDecompress(.gzip, &[_]u8{
316 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
317 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00,
318 }, undefined),
319 );
320 // Wrong initial size
321 try testing.expectError(
322 error.WrongGzipSize,
323 testDecompress(.gzip, &[_]u8{
324 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
325 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x01,
327 }, undefined),
328 );
329 // Truncated initial size field
330 try testing.expectError(
331 error.EndOfStream,
332 testDecompress(.gzip, &[_]u8{
333 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
334 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
335 0x00, 0x00, 0x00,
336 }, undefined),
337 );
338
339 try testDecompress(.gzip, &[_]u8{
340 // GZIP header
341 0x1f, 0x8b, 0x08, 0x12, 0x00, 0x09, 0x6e, 0x88, 0x00, 0xff, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x00,
342 // header.FHCRC (should cover entire header)
343 0x99, 0xd6,
344 // GZIP data
345 0x01, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 }, "");
347}