forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

zlib_tests.rs195 lines, 7.6 KiBHistoryRawDownload
1//! What a zlib-compressed Matroska block comes back as, pinned byte for byte.
2//!
3//! The block path inflates through `ec_inflate::inflate_zlib` with a 64 MiB
4//! ceiling. These cases are the ones that path has to get right: the bytes it
5//! hands back for a valid stream, and an error (not a panic, not a short read)
6//! for each way a stream can be wrong. The fixtures were compressed by Python's
7//! `zlib` at writing time; the expected output is computed here, never read back
8//! from the decoder under test.
9
10use super::Unpack;
11
12/// The per-frame ceiling `Unpack::frame` applies, mirrored because the constant
13/// is local to that function.
14const LIMIT: usize = 64 << 20;
15
16/// `zlib.compress(b"")`.
17const EMPTY_ZLIB: [u8; 8] = [0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01];
18
19/// `zlib.compress(b"Hello there")`.
20const ZLIB_HELLO: [u8; 19] = [
21 0x78, 0xda, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xc9, 0x48, 0x2d, 0x4a, 0x05, 0x00, 0x18,
22 0x66, 0x04, 0x2d,
23];
24
25/// `zlib.compress(RANDOM_300, 6)` for the 300 bytes [`xorshift_bytes`] makes from
26/// seed `0x9E37_79B9_7F4A_7C15`. At this size Python emits stored blocks.
27const RANDOM_300_ZLIB: [u8; 311] = [
28 0x78, 0x9c, 0x01, 0x2c, 0x01, 0xd3, 0xfe, 0xae, 0xb9, 0x91, 0x0c, 0xe0, 0xce, 0x44, 0x2e, 0x85,
29 0xd3, 0xff, 0x47, 0xf8, 0x9b, 0xe8, 0x79, 0x38, 0xbf, 0xb1, 0xcf, 0xc7, 0x7e, 0xd5, 0x20, 0x02,
30 0xe8, 0xd3, 0xc2, 0x7f, 0x2c, 0x1e, 0x21, 0x7a, 0xe4, 0x36, 0xb0, 0xb3, 0x1d, 0xa1, 0xff, 0x88,
31 0xed, 0x49, 0x88, 0x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, 0x08, 0xbe, 0x03, 0x0a, 0xad, 0x69, 0xf8,
43 0xdd, 0xe7, 0x50, 0x4c, 0x23, 0x6c, 0x2b, 0xd1, 0x3b, 0x25, 0xa4, 0x1c, 0x80, 0x09, 0x68, 0x0d,
44 0xbe, 0x6d, 0xbb, 0xf5, 0xb3, 0xcb, 0x9a, 0x2f, 0x68, 0x34, 0xee, 0x5d, 0x2f, 0xbe, 0xb4, 0x3c,
45 0x5e, 0x88, 0xb4, 0x37, 0x55, 0xca, 0xfa, 0xa9, 0xf6, 0x9d, 0x35, 0xf0, 0x40, 0x92, 0x5d, 0xa4,
46 0x51, 0x66, 0x6b, 0xab, 0x89, 0x98, 0x88, 0xbc, 0x16, 0x64, 0xe3, 0x63, 0x28, 0x83, 0x52, 0x9d,
47 0x0d, 0xdb, 0x98, 0x7b, 0xde, 0x93, 0xf2,
48];
49
50/// `zlib.compress(TEXT, 9)` for `b"the quick brown fox jumps over the lazy dog\n"`
51/// repeated 20 times (880 bytes): a fixed-Huffman stream with back-references.
52const TEXT_ZLIB: [u8; 60] = [
53 0x78, 0xda, 0x2b, 0xc9, 0x48, 0x55, 0x28, 0x2c, 0xcd, 0x4c, 0xce, 0x56, 0x48, 0x2a, 0xca, 0x2f,
54 0xcf, 0x53, 0x48, 0xcb, 0xaf, 0x50, 0xc8, 0x2a, 0xcd, 0x2d, 0x28, 0x56, 0xc8, 0x2f, 0x4b, 0x2d,
55 0x52, 0x28, 0x01, 0x4a, 0xe7, 0x24, 0x56, 0x55, 0x2a, 0xa4, 0xe4, 0xa7, 0x73, 0x95, 0x8c, 0xaa,
56 0x1d, 0x55, 0x3b, 0xaa, 0x96, 0x22, 0xb5, 0x00, 0xda, 0x15, 0x40, 0x4c,
57];
58
59/// `len` bytes of xorshift64 output: a stand-in for incompressible data that
60/// is the same on every run and in the fixture's generator.
61fn xorshift_bytes(seed: u64, len: usize) -> Vec<u8> {
62 let mut x = seed;
63 (0..len)
64 .map(|_| {
65 x ^= x << 13;
66 x ^= x >> 7;
67 x ^= x << 17;
68 (x >> 32) as u8
69 })
70 .collect()
71}
72
73fn text() -> Vec<u8> {
74 b"the quick brown fox jumps over the lazy dog\n".repeat(20)
75}
76
77/// Adler-32 (RFC 1950), with the modulus deferred across 5552-byte runs, which
78/// is the largest run whose sums fit a `u32`.
79fn adler32(data: &[u8]) -> u32 {
80 let (mut a, mut b) = (1u32, 0u32);
81 for run in data.chunks(5552) {
82 for &byte in run {
83 a += u32::from(byte);
84 b += a;
85 }
86 a %= 65521;
87 b %= 65521;
88 }
89 (b << 16) | a
90}
91
92/// A zlib stream of `plain` made of stored (uncompressed) DEFLATE blocks, split
93/// at the 65535-byte block ceiling. It exists for the sizes where a fixture
94/// would be too large to write out, and for the ceiling test; it is valid zlib,
95/// not a shortcut around the decoder.
96fn stored_zlib(plain: &[u8]) -> Vec<u8> {
97 let mut out = vec![0x78, 0x01];
98 let mut blocks: Vec<&[u8]> = plain.chunks(65535).collect();
99 if blocks.is_empty() {
100 blocks.push(&[]);
101 }
102 let last = blocks.len() - 1;
103 for (i, block) in blocks.iter().enumerate() {
104 out.push(u8::from(i == last)); // BFINAL in bit 0, BTYPE 00 (stored)
105 let len = block.len() as u16;
106 out.extend_from_slice(&len.to_le_bytes());
107 out.extend_from_slice(&(!len).to_le_bytes());
108 out.extend_from_slice(block);
109 }
110 out.extend_from_slice(&adler32(plain).to_be_bytes());
111 out
112}
113
114/// What `Unpack::Zlib` makes of one frame: the bytes it left, or the message it
115/// was refused with.
116fn inflate(frame: &[u8]) -> Result<Vec<u8>, String> {
117 let mut buf = frame.to_vec();
118 Unpack::Zlib
119 .frame(&mut buf)
120 .map(|()| buf)
121 .map_err(|e| e.to_string())
122}
123
124#[test]
125fn an_empty_stream_gives_an_empty_frame() {
126 assert_eq!(inflate(&EMPTY_ZLIB), Ok(Vec::new()));
127}
128
129#[test]
130fn a_short_text_stream_gives_its_bytes() {
131 assert_eq!(inflate(&ZLIB_HELLO).unwrap(), b"Hello there");
132}
133
134#[test]
135fn a_repetitive_text_stream_gives_its_bytes() {
136 assert_eq!(inflate(&TEXT_ZLIB), Ok(text()));
137}
138
139#[test]
140fn random_bytes_at_two_sizes_come_back_unchanged() {
141 assert_eq!(
142 inflate(&RANDOM_300_ZLIB),
143 Ok(xorshift_bytes(0x9E37_79B9_7F4A_7C15, 300))
144 );
145 // Past one stored block, so the stream carries two and the second is final.
146 let plain = xorshift_bytes(0x0123_4567_89AB_CDEF, 70_000);
147 assert_eq!(inflate(&stored_zlib(&plain)), Ok(plain));
148}
149
150#[test]
151fn a_frame_of_exactly_the_ceiling_is_kept() {
152 let plain = vec![0u8; LIMIT];
153 assert_eq!(
154 inflate(&stored_zlib(&plain)).map(|out| out.len()),
155 Ok(LIMIT)
156 );
157}
158
159#[test]
160fn a_frame_one_byte_past_the_ceiling_is_refused() {
161 let err = inflate(&stored_zlib(&vec![0u8; LIMIT + 1])).unwrap_err();
162 assert!(err.contains("did not inflate"), "{err}");
163 assert!(
164 err.contains("limit"),
165 "the refusal names the ceiling: {err}"
166 );
167}
168
169#[test]
170fn a_bad_zlib_header_is_refused() {
171 let err = inflate(&[0x12, 0x34, 0x03, 0x00, 0, 0, 0, 1]).unwrap_err();
172 assert!(err.contains("did not inflate"), "{err}");
173}
174
175#[test]
176fn a_reserved_block_type_is_refused() {
177 // BFINAL 1, BTYPE 11: the one block type RFC 1951 forbids.
178 let err = inflate(&[0x78, 0x9c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01]).unwrap_err();
179 assert!(err.contains("did not inflate"), "{err}");
180}
181
182#[test]
183fn a_wrong_adler32_trailer_is_refused() {
184 let mut bad = ZLIB_HELLO;
185 let last = bad.len() - 1;
186 bad[last] ^= 1;
187 assert!(inflate(&bad).is_err());
188}
189
190#[test]
191fn a_truncated_stream_is_refused_at_every_cut() {
192 for cut in [0, 1, 2, 12, ZLIB_HELLO.len() - 1] {
193 assert!(inflate(&ZLIB_HELLO[..cut]).is_err(), "cut at {cut}");
194 }
195}