//! What a zlib-compressed Matroska block comes back as, pinned byte for byte. //! //! The block path inflates through `ec_inflate::inflate_zlib` with a 64 MiB //! ceiling. These cases are the ones that path has to get right: the bytes it //! hands back for a valid stream, and an error (not a panic, not a short read) //! for each way a stream can be wrong. The fixtures were compressed by Python's //! `zlib` at writing time; the expected output is computed here, never read back //! from the decoder under test. use super::Unpack; /// The per-frame ceiling `Unpack::frame` applies, mirrored because the constant /// is local to that function. const LIMIT: usize = 64 << 20; /// `zlib.compress(b"")`. const EMPTY_ZLIB: [u8; 8] = [0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01]; /// `zlib.compress(b"Hello there")`. const ZLIB_HELLO: [u8; 19] = [ 0x78, 0xda, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xc9, 0x48, 0x2d, 0x4a, 0x05, 0x00, 0x18, 0x66, 0x04, 0x2d, ]; /// `zlib.compress(RANDOM_300, 6)` for the 300 bytes [`xorshift_bytes`] makes from /// seed `0x9E37_79B9_7F4A_7C15`. At this size Python emits stored blocks. const RANDOM_300_ZLIB: [u8; 311] = [ 0x78, 0x9c, 0x01, 0x2c, 0x01, 0xd3, 0xfe, 0xae, 0xb9, 0x91, 0x0c, 0xe0, 0xce, 0x44, 0x2e, 0x85, 0xd3, 0xff, 0x47, 0xf8, 0x9b, 0xe8, 0x79, 0x38, 0xbf, 0xb1, 0xcf, 0xc7, 0x7e, 0xd5, 0x20, 0x02, 0xe8, 0xd3, 0xc2, 0x7f, 0x2c, 0x1e, 0x21, 0x7a, 0xe4, 0x36, 0xb0, 0xb3, 0x1d, 0xa1, 0xff, 0x88, 0xed, 0x49, 0x88, 0xab, 0x41, 0x14, 0x67, 0xf8, 0x7d, 0x2e, 0x2e, 0xe9, 0xf3, 0x8e, 0x10, 0xfe, 0x9b, 0x3b, 0xeb, 0xad, 0xa9, 0xdd, 0xd8, 0x16, 0x26, 0xbc, 0x74, 0x14, 0x05, 0x2b, 0x5f, 0x3e, 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0x34, 0xee, 0x5d, 0x2f, 0xbe, 0xb4, 0x3c, 0x5e, 0x88, 0xb4, 0x37, 0x55, 0xca, 0xfa, 0xa9, 0xf6, 0x9d, 0x35, 0xf0, 0x40, 0x92, 0x5d, 0xa4, 0x51, 0x66, 0x6b, 0xab, 0x89, 0x98, 0x88, 0xbc, 0x16, 0x64, 0xe3, 0x63, 0x28, 0x83, 0x52, 0x9d, 0x0d, 0xdb, 0x98, 0x7b, 0xde, 0x93, 0xf2, ]; /// `zlib.compress(TEXT, 9)` for `b"the quick brown fox jumps over the lazy dog\n"` /// repeated 20 times (880 bytes): a fixed-Huffman stream with back-references. const TEXT_ZLIB: [u8; 60] = [ 0x78, 0xda, 0x2b, 0xc9, 0x48, 0x55, 0x28, 0x2c, 0xcd, 0x4c, 0xce, 0x56, 0x48, 0x2a, 0xca, 0x2f, 0xcf, 0x53, 0x48, 0xcb, 0xaf, 0x50, 0xc8, 0x2a, 0xcd, 0x2d, 0x28, 0x56, 0xc8, 0x2f, 0x4b, 0x2d, 0x52, 0x28, 0x01, 0x4a, 0xe7, 0x24, 0x56, 0x55, 0x2a, 0xa4, 0xe4, 0xa7, 0x73, 0x95, 0x8c, 0xaa, 0x1d, 0x55, 0x3b, 0xaa, 0x96, 0x22, 0xb5, 0x00, 0xda, 0x15, 0x40, 0x4c, ]; /// `len` bytes of xorshift64 output: a stand-in for incompressible data that /// is the same on every run and in the fixture's generator. fn xorshift_bytes(seed: u64, len: usize) -> Vec { let mut x = seed; (0..len) .map(|_| { x ^= x << 13; x ^= x >> 7; x ^= x << 17; (x >> 32) as u8 }) .collect() } fn text() -> Vec { b"the quick brown fox jumps over the lazy dog\n".repeat(20) } /// Adler-32 (RFC 1950), with the modulus deferred across 5552-byte runs, which /// is the largest run whose sums fit a `u32`. fn adler32(data: &[u8]) -> u32 { let (mut a, mut b) = (1u32, 0u32); for run in data.chunks(5552) { for &byte in run { a += u32::from(byte); b += a; } a %= 65521; b %= 65521; } (b << 16) | a } /// A zlib stream of `plain` made of stored (uncompressed) DEFLATE blocks, split /// at the 65535-byte block ceiling. It exists for the sizes where a fixture /// would be too large to write out, and for the ceiling test; it is valid zlib, /// not a shortcut around the decoder. fn stored_zlib(plain: &[u8]) -> Vec { let mut out = vec![0x78, 0x01]; let mut blocks: Vec<&[u8]> = plain.chunks(65535).collect(); if blocks.is_empty() { blocks.push(&[]); } let last = blocks.len() - 1; for (i, block) in blocks.iter().enumerate() { out.push(u8::from(i == last)); // BFINAL in bit 0, BTYPE 00 (stored) let len = block.len() as u16; out.extend_from_slice(&len.to_le_bytes()); out.extend_from_slice(&(!len).to_le_bytes()); out.extend_from_slice(block); } out.extend_from_slice(&adler32(plain).to_be_bytes()); out } /// What `Unpack::Zlib` makes of one frame: the bytes it left, or the message it /// was refused with. fn inflate(frame: &[u8]) -> Result, String> { let mut buf = frame.to_vec(); Unpack::Zlib .frame(&mut buf) .map(|()| buf) .map_err(|e| e.to_string()) } #[test] fn an_empty_stream_gives_an_empty_frame() { assert_eq!(inflate(&EMPTY_ZLIB), Ok(Vec::new())); } #[test] fn a_short_text_stream_gives_its_bytes() { assert_eq!(inflate(&ZLIB_HELLO).unwrap(), b"Hello there"); } #[test] fn a_repetitive_text_stream_gives_its_bytes() { assert_eq!(inflate(&TEXT_ZLIB), Ok(text())); } #[test] fn random_bytes_at_two_sizes_come_back_unchanged() { assert_eq!( inflate(&RANDOM_300_ZLIB), Ok(xorshift_bytes(0x9E37_79B9_7F4A_7C15, 300)) ); // Past one stored block, so the stream carries two and the second is final. let plain = xorshift_bytes(0x0123_4567_89AB_CDEF, 70_000); assert_eq!(inflate(&stored_zlib(&plain)), Ok(plain)); } #[test] fn a_frame_of_exactly_the_ceiling_is_kept() { let plain = vec![0u8; LIMIT]; assert_eq!( inflate(&stored_zlib(&plain)).map(|out| out.len()), Ok(LIMIT) ); } #[test] fn a_frame_one_byte_past_the_ceiling_is_refused() { let err = inflate(&stored_zlib(&vec![0u8; LIMIT + 1])).unwrap_err(); assert!(err.contains("did not inflate"), "{err}"); assert!( err.contains("limit"), "the refusal names the ceiling: {err}" ); } #[test] fn a_bad_zlib_header_is_refused() { let err = inflate(&[0x12, 0x34, 0x03, 0x00, 0, 0, 0, 1]).unwrap_err(); assert!(err.contains("did not inflate"), "{err}"); } #[test] fn a_reserved_block_type_is_refused() { // BFINAL 1, BTYPE 11: the one block type RFC 1951 forbids. let err = inflate(&[0x78, 0x9c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01]).unwrap_err(); assert!(err.contains("did not inflate"), "{err}"); } #[test] fn a_wrong_adler32_trailer_is_refused() { let mut bad = ZLIB_HELLO; let last = bad.len() - 1; bad[last] ^= 1; assert!(inflate(&bad).is_err()); } #[test] fn a_truncated_stream_is_refused_at_every_cut() { for cut in [0, 1, 2, 12, ZLIB_HELLO.len() - 1] { assert!(inflate(&ZLIB_HELLO[..cut]).is_err(), "cut at {cut}"); } }