| ... | @@ -64,3 +64,38 @@ pub const store = struct { | ... | @@ -64,3 +64,38 @@ pub const store = struct { |
| 64 | return deflate.store.compressor(.zlib, writer); | 64 | return deflate.store.compressor(.zlib, writer); |
| 65 | } | 65 | } |
| 66 | }; | 66 | }; |
| | 67 | |
| | 68 | test "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 | } |