authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-03-03 15:39:03+01:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-03-04 09:53:01+01:00
logc680b5d138ee9114f23f13fb8aed62ccac8ea32d
tree7c1c65c14668e876eaaf22d5b173c17c70ceafe5
parentf2508abfa6461b4d28d6a8b1d72026810ff30a21

compress.zlib: add overshoot test cast

Using example from [zigimg](https://github.com/zigimg/zigimg/pull/164) project.

1 files changed, 35 insertions(+), 0 deletions(-)

lib/std/compress/zlib.zig+35
......@@ -64,3 +64,38 @@ pub const store = struct {
6464 return deflate.store.compressor(.zlib, writer);
6565 }
6666};
67
68test "should not overshoot" {
69 const std = @import("std");
70
71 // Compressed zlib data with extra 4 bytes at the end.
72 const data = [_]u8{
73 0x78, 0x9c, 0x73, 0xce, 0x2f, 0xa8, 0x2c, 0xca, 0x4c, 0xcf, 0x28, 0x51, 0x08, 0xcf, 0xcc, 0xc9,
74 0x49, 0xcd, 0x55, 0x28, 0x4b, 0xcc, 0x53, 0x08, 0x4e, 0xce, 0x48, 0xcc, 0xcc, 0xd6, 0x51, 0x08,
75 0xce, 0xcc, 0x4b, 0x4f, 0x2c, 0xc8, 0x2f, 0x4a, 0x55, 0x30, 0xb4, 0xb4, 0x34, 0xd5, 0xb5, 0x34,
76 0x03, 0x00, 0x8b, 0x61, 0x0f, 0xa4, 0x52, 0x5a, 0x94, 0x12,
77 };
78
79 var stream = std.io.fixedBufferStream(data[0..]);
80 const reader = stream.reader();
81
82 var dcp = decompressor(reader);
83 var out: [128]u8 = undefined;
84
85 // Decompress
86 var n = try dcp.reader().readAll(out[0..]);
87
88 // Expected decompressed data
89 try std.testing.expectEqual(46, n);
90 try std.testing.expectEqualStrings("Copyright Willem van Schaik, Singapore 1995-96", out[0..n]);
91
92 // Decompressor don't overshoot underlying reader.
93 // It is leaving it at the end of compressed data chunk.
94 try std.testing.expectEqual(data.len - 4, stream.getPos());
95 try std.testing.expectEqual(0, dcp.unreadBytes());
96
97 // 4 bytes after compressed chunk are available in reader.
98 n = try reader.readAll(out[0..]);
99 try std.testing.expectEqual(n, 4);
100 try std.testing.expectEqualSlices(u8, data[data.len - 4 .. data.len], out[0..n]);
101}