use crate::{
BLOCK_SIZE_MAX,
block::parse_block_header,
decode::{
Decoder, DecoderOptions, decode_all_with_dict_and_options, decode_all_with_options,
decode_all_with_prepared_dict_and_options, decode_into_slice_with_options,
},
dictionary::{DecoderDictionary, EncoderDictionary},
encode::{
CompressionLevel, Encoder, EncoderOptions, LiteralCompressionMode, ParameterBounds,
ParameterOverrides, RowMatchFinderMode, Strategy, compression_parameters_for_options,
encode_all_with_options,
},
error::Error,
frame::{
Format, FrameHeader, parse_frame_header, parse_frame_header_with_format,
representable_window_size,
},
literals::{LiteralsState, decode_literals_section},
sequence::{
SequenceTablesState, TableTarget, decode_sequence_commands, parse_sequence_section,
},
streaming::{StreamingDecoder, StreamingEncoder},
window::LdmMode,
};
const MAX_FUZZ_OUTPUT_SIZE: usize = 1 << 20;
const MAX_FUZZ_WINDOW_SIZE: u64 = 1 << 20;
const MAX_FUZZ_SEQUENCE_COUNT: usize = 4096;
pub fn frame_parse(data: &[u8]) {
if let Ok(header) = parse_frame_header(data) {
match header {
FrameHeader::Zstandard(header) => {
if let Some(blocks) = data.get(header.header_size..) {
let _ = parse_block_header(blocks);
}
}
FrameHeader::Skippable(frame) => {
let _ = data.get(..frame.header_size.saturating_add(frame.size as usize));
}
}
}
}
pub fn literals_parse(data: &[u8]) {
let split = split_point(data);
let (first, second) = data.split_at(split);
let mut state = LiteralsState::default();
let _ = decode_literals_section(first, &mut state, BLOCK_SIZE_MAX);
let _ = decode_literals_section(second, &mut state, BLOCK_SIZE_MAX);
}
pub fn sequence_parse(data: &[u8]) {
let split = split_point(data);
let (first, second) = data.split_at(split);
let mut tables = SequenceTablesState::default();
parse_sequence_chunk(first, &mut tables);
parse_sequence_chunk(second, &mut tables);
}
pub fn full_decode(data: &[u8]) {
let permissive = DecoderOptions {
max_window_size: Some(MAX_FUZZ_WINDOW_SIZE),
max_output_size: Some(MAX_FUZZ_OUTPUT_SIZE),
verify_checksum: false,
..Default::default()
};
let relaxed = decode_all_with_options(data, permissive);
let strict = decode_all_with_options(
data,
DecoderOptions {
single_frame: true,
..permissive
},
);
if let Ok(strict) = strict {
assert_eq!(
relaxed.as_deref(),
Ok(strict.as_slice()),
"single_frame accepted input the default decoder did not decode identically"
);
}
}
pub fn slice_decode(data: &[u8]) {
const GUARD: usize = 64;
const GUARD_BYTE: u8 = 0xA5;
let permissive = DecoderOptions {
max_window_size: Some(MAX_FUZZ_WINDOW_SIZE),
max_output_size: Some(MAX_FUZZ_OUTPUT_SIZE),
verify_checksum: false,
..Default::default()
};
let Ok(expected) = decode_all_with_options(data, permissive) else {
return;
};
let mut backing = vec![GUARD_BYTE; expected.len() + GUARD];
let (dst, guard) = backing.split_at_mut(expected.len());
let written = decode_into_slice_with_options(data, dst, permissive)
.expect("a destination sized by the growable decode has to be enough");
assert_eq!(
written,
expected.len(),
"slice decode reported a short write"
);
assert_eq!(
dst,
expected.as_slice(),
"slice decode diverged from decode_all"
);
assert!(
guard.iter().all(|&byte| byte == GUARD_BYTE),
"slice decode wrote past the end of an exactly-sized destination"
);
let Some(short) = expected.len().checked_sub(1) else {
return;
};
let mut backing = vec![GUARD_BYTE; short + GUARD];
let (dst, guard) = backing.split_at_mut(short);
let result = decode_into_slice_with_options(data, dst, permissive);
assert!(
matches!(result, Err(Error::DstSizeTooSmall)),
"a destination one byte short reported {result:?} instead of refusing"
);
assert!(
guard.iter().all(|&byte| byte == GUARD_BYTE),
"a refused slice decode wrote past the end of the destination"
);
}
pub fn streaming_decode(data: &[u8]) {
let Some((&selector, frame)) = data.split_first() else {
return;
};
let chunk = match selector % 9 {
8 => frame.len().max(1),
shift => 1usize << shift,
};
let mut decoder = StreamingDecoder::new(DecoderOptions {
max_window_size: Some(MAX_FUZZ_WINDOW_SIZE),
max_output_size: Some(MAX_FUZZ_OUTPUT_SIZE),
verify_checksum: false,
..Default::default()
});
for piece in frame.chunks(chunk) {
if decoder.push(piece).is_err() {
return;
}
let _ = decoder.take_output();
}
let _ = decoder.finish();
let _ = decoder.take_output();
}
pub fn dictionary_decode(data: &[u8]) {
const GUARD: usize = 64;
const GUARD_BYTE: u8 = 0x5A;
let Some((control, rest)) = split_control::<2>(data) else {
return;
};
let split = usize::from(u16::from_le_bytes(control)).min(rest.len());
let (dictionary, frame) = rest.split_at(split);
let permissive = DecoderOptions {
max_window_size: Some(MAX_FUZZ_WINDOW_SIZE),
max_output_size: Some(MAX_FUZZ_OUTPUT_SIZE),
verify_checksum: false,
..Default::default()
};
let Ok(prepared) = DecoderDictionary::new(dictionary) else {
return;
};
let Ok(expected) = decode_all_with_prepared_dict_and_options(frame, &prepared, permissive)
else {
return;
};
assert_eq!(
decode_all_with_dict_and_options(frame, dictionary, permissive).as_deref(),
Ok(expected.as_slice()),
"decoding with a dictionary disagreed with decoding with the same dictionary prepared"
);
let mut backing = vec![GUARD_BYTE; expected.len() + GUARD];
let (dst, guard) = backing.split_at_mut(expected.len());
let mut decoder = Decoder::new();
let written = decoder
.decode_into_slice_with_prepared_dict_and_options(frame, dst, &prepared, permissive)
.expect("a destination sized by the growable decode has to be enough");
assert_eq!(
written,
expected.len(),
"slice decode reported a short write"
);
assert_eq!(
dst,
expected.as_slice(),
"slice decode diverged from decode_all"
);
assert!(
guard.iter().all(|&byte| byte == GUARD_BYTE),
"slice decode wrote past the end of an exactly-sized destination"
);
if let Some(short) = expected.len().checked_sub(1) {
let mut backing = vec![GUARD_BYTE; short + GUARD];
let (dst, guard) = backing.split_at_mut(short);
let result = decoder
.decode_into_slice_with_prepared_dict_and_options(frame, dst, &prepared, permissive);
assert!(
matches!(result, Err(Error::DstSizeTooSmall)),
"a destination one byte short reported {result:?} instead of refusing"
);
assert!(
guard.iter().all(|&byte| byte == GUARD_BYTE),
"a refused slice decode wrote past the end of the destination"
);
}
let mut streaming = StreamingDecoder::with_prepared_dict(&prepared, permissive);
let mut streamed = Vec::new();
let chunk = 1 + usize::from(control[0] % 64);
for piece in frame.chunks(chunk) {
if streaming.push(piece).is_err() {
return;
}
streamed.extend_from_slice(&streaming.take_output());
}
if streaming.finish().is_err() {
return;
}
streamed.extend_from_slice(&streaming.take_output());
assert_eq!(
streamed, expected,
"the streaming decoder disagreed with the one-shot decode on the same dictionary"
);
}
fn body_cap_for(level: CompressionLevel) -> usize {
match level.as_i32() {
..=4 => 3 << 19,
_ => 320 << 10,
}
}
const FUZZ_BODY_SIZES: [usize; 12] = [
1 << 8,
(16 << 10) - 1,
16 << 10,
(16 << 10) + 1,
(128 << 10) - 1,
128 << 10,
(128 << 10) + 1,
(256 << 10) - 1,
256 << 10,
(256 << 10) + 1,
320 << 10,
3 << 19,
];
const FUZZ_BLOCK_SIZES: [usize; 8] = [
1 << 10,
(1 << 10) + 7,
1 << 12,
5000,
1 << 14,
1 << 16,
BLOCK_SIZE_MAX - 1,
BLOCK_SIZE_MAX,
];
fn level_from(byte: u8) -> CompressionLevel {
const NEGATIVE: [i32; 10] = [-1, -2, -3, -5, -9, -17, -33, -1000, -100_000, -131_072];
let slot = byte % 32;
let level = if slot < 22 {
1 + i32::from(slot)
} else {
NEGATIVE[usize::from(slot - 22)]
};
CompressionLevel::try_new(level).expect("level_from stays inside the supported range")
}
fn split_control<const N: usize>(data: &[u8]) -> Option<([u8; N], &[u8])> {
let (control, body) = data.split_at_checked(N)?;
Some((control.try_into().ok()?, body))
}
fn tile_with_drift(seed: &[u8], target: usize) -> Vec<u8> {
debug_assert!(!seed.is_empty());
let mut out = Vec::with_capacity(target);
let mut tile = 0u32;
while out.len() < target {
let start = out.len();
let take = seed.len().min(target - start);
out.extend_from_slice(&seed[..take]);
out[start + (tile as usize * 7) % take] ^= (tile as u8) | 1;
tile = tile.wrapping_add(1);
}
out
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BodyShape {
Seed,
Tiled { target: usize },
}
fn body_shape_from(flags: u8, level: CompressionLevel) -> BodyShape {
if flags & 4 == 0 {
return BodyShape::Seed;
}
let index = usize::from(flags >> 3) % FUZZ_BODY_SIZES.len();
BodyShape::Tiled {
target: FUZZ_BODY_SIZES[index].min(body_cap_for(level)),
}
}
fn shape_body(seed: &[u8], level: CompressionLevel, shape: BodyShape) -> Vec<u8> {
let cap = body_cap_for(level);
let BodyShape::Tiled { target } = shape else {
return seed[..seed.len().min(cap)].to_vec();
};
if seed.is_empty() || seed.len() >= target {
return seed[..seed.len().min(target)].to_vec();
}
tile_with_drift(seed, target)
}
fn encoder_options_from(control: [u8; 7]) -> (EncoderOptions, BodyShape) {
let level = level_from(control[0]);
let options = EncoderOptions {
block_size: FUZZ_BLOCK_SIZES[usize::from(control[1]) % FUZZ_BLOCK_SIZES.len()],
checksum: control[2] & 1 != 0,
write_dict_id: control[2] & 2 != 0,
compression_level: level,
parameters: overrides_from(
control[3],
[control[4], control[5]],
control[6] >> 2,
control[1] >> 3,
),
pledged_src_size: None,
write_content_size: control[6] & 1 == 0,
format: if control[6] & 2 == 0 {
Format::Zstd1
} else {
Format::Zstd1Magicless
},
};
(options, body_shape_from(control[2], level))
}
fn overrides_from(mask: u8, values: [u8; 2], ldm: u8, modes: u8) -> ParameterOverrides {
const MAX_FUZZ_TABLE_LOG: u32 = 22;
const MAX_FUZZ_LDM_LOG: u32 = 18;
const MIN_FUZZ_LDM_LOG: u32 = 14;
fn within(bounds: ParameterBounds, byte: u8) -> u32 {
let span = bounds.max - bounds.min + 1;
bounds.min + (u32::from(byte) % span)
}
fn within_table(bounds: ParameterBounds, byte: u8) -> u32 {
within(bounds, byte).min(MAX_FUZZ_TABLE_LOG)
}
let pick = |bit: u8, byte: u8, bounds: ParameterBounds| {
(mask & (1 << bit) != 0).then(|| within(bounds, byte))
};
let pick_table = |bit: u8, byte: u8, bounds: ParameterBounds| {
(mask & (1 << bit) != 0).then(|| within_table(bounds, byte))
};
let long_distance_matching = if ldm & 1 != 0 {
LdmMode::Enabled
} else {
LdmMode::Auto
};
let pick_ldm = |bit: u8, byte: u8, bounds: ParameterBounds| {
(ldm & 1 != 0 && ldm & (1 << bit) != 0).then(|| within(bounds, byte))
};
ParameterOverrides {
window_log: match long_distance_matching {
LdmMode::Enabled => Some(
within(ParameterOverrides::WINDOW_LOG, values[0])
.clamp(MIN_FUZZ_LDM_LOG, MAX_FUZZ_LDM_LOG),
),
_ => pick(0, values[0], ParameterOverrides::WINDOW_LOG),
},
hash_log: pick_table(1, values[1], ParameterOverrides::HASH_LOG),
chain_log: pick_table(2, values[0].rotate_left(3), ParameterOverrides::CHAIN_LOG),
search_log: pick(3, values[1].rotate_left(3), ParameterOverrides::SEARCH_LOG),
min_match: pick(4, values[0].rotate_left(5), ParameterOverrides::MIN_MATCH),
target_length: pick(
5,
values[1].rotate_left(5),
ParameterOverrides::TARGET_LENGTH,
),
strategy: (mask & 0xc0 != 0).then(|| {
const STRATEGIES: [Strategy; 9] = [
Strategy::Fast,
Strategy::DoubleFast,
Strategy::Greedy,
Strategy::Lazy,
Strategy::Lazy2,
Strategy::BinaryTreeLazy2,
Strategy::BinaryTreeOpt,
Strategy::BinaryTreeUltra,
Strategy::BinaryTreeUltra2,
];
STRATEGIES[usize::from(values[0] ^ values[1]) % STRATEGIES.len()]
}),
use_row_match_finder: match modes & 0b11 {
1 => RowMatchFinderMode::Enabled,
2 => RowMatchFinderMode::Disabled,
_ => RowMatchFinderMode::Auto,
},
literal_compression: match (modes >> 2) & 0b11 {
1 => LiteralCompressionMode::Enabled,
2 => LiteralCompressionMode::Disabled,
_ => LiteralCompressionMode::Auto,
},
long_distance_matching,
ldm_hash_log: pick_ldm(
1,
values[1].rotate_left(1),
ParameterOverrides::LDM_HASH_LOG,
)
.map(|log| log.min(MAX_FUZZ_LDM_LOG)),
ldm_min_match: pick_ldm(
2,
values[0].rotate_left(1),
ParameterOverrides::LDM_MIN_MATCH,
),
ldm_bucket_size_log: pick_ldm(
3,
values[1].rotate_left(7),
ParameterOverrides::LDM_BUCKET_SIZE_LOG,
),
ldm_hash_rate_log: pick_ldm(
4,
values[0].rotate_left(7),
ParameterOverrides::LDM_HASH_RATE_LOG,
),
}
}
fn decode_own_frame(encoded: &[u8], format: Format) -> Vec<u8> {
decode_all_with_options(
encoded,
DecoderOptions {
format,
max_window_size: None,
max_output_size: Some(MAX_FUZZ_ENCODE_OUTPUT),
verify_checksum: true,
..Default::default()
},
)
.expect("a frame this crate produced must decode")
}
const MAX_FUZZ_ENCODE_OUTPUT: usize = 3 << 19;
const MAX_FUZZ_PUSHES: usize = 4096;
fn check_declared_window(
body: &[u8],
encoded: &[u8],
options: EncoderOptions,
size_hint: Option<usize>,
) {
let header = match parse_frame_header_with_format(encoded, options.format)
.expect("a frame this crate produced must parse")
{
FrameHeader::Zstandard(header) => header,
FrameHeader::Skippable(_) => panic!("the encoder never emits a skippable frame"),
};
let params = compression_parameters_for_options(options, size_hint, None);
let intended = params
.max_history_bytes
.max(options.block_size.max(1 << 10))
.max(body.len()) as u64;
let ceiling = representable_window_size(intended)
.expect("a window this encoder can declare must be representable");
assert!(
header.window_size <= ceiling,
"frame declares a {}-byte window for a {}-byte body at level {}, above the {}-byte ceiling",
header.window_size,
body.len(),
options.compression_level.as_i32(),
ceiling,
);
}
pub fn encode_roundtrip(data: &[u8]) {
let _ = run_encode_roundtrip(data);
}
fn run_encode_roundtrip(data: &[u8]) -> Option<usize> {
let (control, seed) = split_control::<7>(data)?;
let (options, shape) = encoder_options_from(control);
let body = shape_body(seed, options.compression_level, shape);
let encoded = encode_all_with_options(&body, options)
.expect("encoding arbitrary bytes with valid options cannot fail");
check_declared_window(&body, &encoded, options, Some(body.len()));
assert_eq!(
decode_own_frame(&encoded, options.format),
body,
"one-shot round trip"
);
Some(body.len())
}
pub fn streaming_encode_roundtrip(data: &[u8]) {
let _ = run_streaming_encode_roundtrip(data);
}
fn run_streaming_encode_roundtrip(data: &[u8]) -> Option<usize> {
let (control, seed) = split_control::<8>(data)?;
let (options, shape) = encoder_options_from(control[..7].try_into().expect("seven bytes"));
let body = shape_body(seed, options.compression_level, shape);
let chunk = match control[7] & 0x0f {
shift @ 0..=8 => 1usize << shift,
_ => body.len().max(1),
}
.max(body.len() / MAX_FUZZ_PUSHES)
.max(1);
let flush_every = usize::from(control[7] >> 4);
let mut encoder = StreamingEncoder::new(options).expect("valid options");
let mut encoded = Vec::new();
for (index, piece) in body.chunks(chunk).enumerate() {
encoder.push(piece).expect("push cannot fail before finish");
if flush_every != 0 && index % flush_every == 0 {
encoder.flush().expect("flush cannot fail before finish");
}
encoded.extend_from_slice(&encoder.take_output());
}
encoder.finish().expect("finish cannot fail");
encoded.extend_from_slice(&encoder.take_output());
check_declared_window(&body, &encoded, options, None);
assert_eq!(
decode_own_frame(&encoded, options.format),
body,
"streaming round trip"
);
encoder.reset().expect("reset after finish");
let second = &body[..body.len() / 2];
encoder.push(second).expect("push after reset");
encoder.finish().expect("finish after reset");
let second_frame = encoder.take_output();
check_declared_window(second, &second_frame, options, None);
assert_eq!(
decode_own_frame(&second_frame, options.format),
second,
"round trip of the frame after a reset"
);
Some(body.len())
}
pub fn dictionary_encode_roundtrip(data: &[u8]) {
let _ = run_dictionary_encode_roundtrip(data);
}
fn run_dictionary_encode_roundtrip(data: &[u8]) -> Option<usize> {
let (control, rest) = split_control::<8>(data)?;
let (options, _) = encoder_options_from(control[..7].try_into().expect("seven bytes"));
let split = usize::from(control[7]) * rest.len() / 256;
let (dict, seed) = rest.split_at(split);
let body = shape_body(seed, options.compression_level, BodyShape::Seed);
let prepared = EncoderDictionary::new(dict).ok()?;
let prepared_decoding = DecoderDictionary::new(dict).ok()?;
let mut context = Encoder::new();
let encoded = context
.encode_all_with_prepared_dict_and_options(&body, &prepared, options)
.expect("encoding with a parsed dictionary cannot fail");
let decoded = decode_all_with_prepared_dict_and_options(
&encoded,
&prepared_decoding,
DecoderOptions {
max_window_size: None,
max_output_size: Some(MAX_FUZZ_ENCODE_OUTPUT),
verify_checksum: true,
format: options.format,
..Default::default()
},
)
.expect("a frame this crate produced must decode against its own dictionary");
assert_eq!(decoded, body, "dictionary round trip");
let again = context
.encode_all_with_prepared_dict_and_options(&body, &prepared, options)
.expect("second encode on the same context");
assert_eq!(
encoded, again,
"a reused encoder produced different bytes for the same input"
);
Some(body.len())
}
fn split_point(data: &[u8]) -> usize {
if data.is_empty() {
0
} else {
(data[0] as usize) % (data.len() + 1)
}
}
fn parse_sequence_chunk(data: &[u8], tables: &mut SequenceTablesState) {
if let Ok(parsed) = parse_sequence_section(data, tables, TableTarget::Both) {
if parsed.number_of_sequences <= MAX_FUZZ_SEQUENCE_COUNT {
let _ = decode_sequence_commands(&parsed, tables);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn structured_body(len: usize) -> Vec<u8> {
let words = ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot"];
let mut out = Vec::with_capacity(len);
let mut index = 0u64;
while out.len() < len {
let record = format!(
"{{\"id\":{},\"tag\":\"{}\",\"n\":{}}}\n",
index,
words[(index as usize) % words.len()],
index.wrapping_mul(2_654_435_761) as u32,
);
let take = record.len().min(len - out.len());
out.extend_from_slice(&record.as_bytes()[..take]);
index += 1;
}
out
}
fn input(control: &[u8], body: &[u8]) -> Vec<u8> {
let mut out = control.to_vec();
out.extend_from_slice(body);
out
}
const FUZZ_OVERRIDE_MASKS: [u8; 5] = [0x00, 0x01, 0x20, 0x3f, 0xff];
const FUZZ_FRAME_FLAGS: [u8; 4] = [0, 1, 2, 3];
#[test]
fn every_level_and_block_size_round_trips_one_shot() {
let body = structured_body(9000);
for level in 0..32u8 {
for block in 0..FUZZ_BLOCK_SIZES.len() as u8 {
for flags in [0u8, 1, 3] {
for mask in FUZZ_OVERRIDE_MASKS {
let compressed = run_encode_roundtrip(&input(
&[level, block, flags, mask, 0x37, 0x9b, 0],
&body,
))
.expect("the target must reach the encoder, not bail on a short input");
assert!(compressed > 0, "the target compressed an empty body");
}
}
}
}
}
#[test]
fn every_frame_format_round_trips_one_shot() {
let body = structured_body(9000);
for level in [0u8, 3, 12, 22, 25] {
for frame_flags in FUZZ_FRAME_FLAGS {
let compressed =
run_encode_roundtrip(&input(&[level, 2, 1, 0, 0, 0, frame_flags], &body))
.expect("the target must reach the encoder, not bail on a short input");
assert!(compressed > 0, "the target compressed an empty body");
}
}
}
#[test]
fn every_level_round_trips_through_the_streaming_encoder() {
let body = structured_body(9000);
for level in 0..32u8 {
for chunking in [0x00u8, 0x05, 0x0f, 0x23] {
for mask in FUZZ_OVERRIDE_MASKS {
let compressed = run_streaming_encode_roundtrip(&input(
&[level, 2, 1, mask, 0x37, 0x9b, 0, chunking],
&body,
))
.expect("the target must reach the encoder, not bail on a short input");
assert!(compressed > 0, "the target compressed an empty body");
}
}
}
}
#[test]
fn every_level_round_trips_with_a_dictionary() {
let body = structured_body(9000);
for level in 0..32u8 {
for split in [32u8, 128, 200] {
for mask in FUZZ_OVERRIDE_MASKS {
let compressed = run_dictionary_encode_roundtrip(&input(
&[level, 3, 1, mask, 0x37, 0x9b, 0, split],
&body,
))
.expect("raw dictionary content always parses");
assert!(compressed > 0, "the target compressed an empty body");
}
}
}
}
#[test]
fn the_targets_consume_the_control_prefix_they_document() {
let body = structured_body(64);
assert!(run_encode_roundtrip(&input(&[0; 7], &body)).is_some());
assert!(run_encode_roundtrip(&[0; 6]).is_none());
assert!(run_streaming_encode_roundtrip(&input(&[0; 8], &body)).is_some());
assert!(run_streaming_encode_roundtrip(&[0; 7]).is_none());
assert!(run_dictionary_encode_roundtrip(&input(&[0; 8], &body)).is_some());
assert!(run_dictionary_encode_roundtrip(&[0; 7]).is_none());
}
#[test]
fn the_amplified_body_outgrows_the_level_one_window() {
let level = CompressionLevel::MIN_POSITIVE;
let params = crate::encode::compression_parameters_for_input(level, None, None);
let amplified = shape_body(
&structured_body(700),
level,
BodyShape::Tiled {
target: body_cap_for(level),
},
);
assert!(
amplified.len() > params.max_history_bytes * 2,
"amplified body is {} bytes against a {}-byte window",
amplified.len(),
params.max_history_bytes,
);
}
fn reachable_body_sizes(level: CompressionLevel) -> Vec<usize> {
(0..=u8::MAX)
.filter_map(|flags| match body_shape_from(flags | 4, level) {
BodyShape::Tiled { target } => Some(target),
BodyShape::Seed => None,
})
.collect()
}
#[test]
fn every_level_reaches_every_cparams_tier() {
let per_tier = [1 << 8, (16 << 10) + 1, (128 << 10) + 1, (256 << 10) + 1];
for byte in 0..22u8 {
let level = level_from(byte);
let sizes = reachable_body_sizes(level);
let reached: Vec<_> = sizes
.iter()
.map(|&size| {
crate::encode::compression_parameters_for_input(level, Some(size), None)
.upstream_cparams
})
.collect();
for size in per_tier {
let wanted =
crate::encode::compression_parameters_for_input(level, Some(size), None);
assert!(
reached.contains(&wanted.upstream_cparams),
"level {} cannot reach the parameters it uses for a {}-byte source; \
largest reachable body is {}",
level.as_i32(),
size,
sizes.iter().max().expect("at least one tiled size"),
);
}
}
}
#[test]
fn tiled_bodies_bracket_every_tier_boundary() {
for byte in 0..22u8 {
let level = level_from(byte);
let sizes = reachable_body_sizes(level);
for boundary in [16 << 10, 128 << 10, 256 << 10] {
assert!(
sizes.contains(&boundary) || boundary > body_cap_for(level),
"level {} cannot produce a body of exactly {boundary} bytes",
level.as_i32(),
);
assert!(
sizes.iter().any(|&size| size > boundary),
"level {} cannot produce a body past {boundary} bytes",
level.as_i32(),
);
}
}
}
#[test]
fn tiling_perturbs_every_repetition() {
let seed = structured_body(64);
let tiled = tile_with_drift(&seed, seed.len() * 8);
let differing = tiled
.chunks(seed.len())
.filter(|tile| tile.len() == seed.len() && *tile != &seed[..])
.count();
assert_eq!(differing, 8, "every tile should differ from the seed");
}
#[test]
fn every_long_distance_configuration_round_trips() {
let body = tile_with_drift(&structured_body(700), 1 << 16);
for ldm in 0..32u8 {
let frame_flags = ldm << 2;
let case = format!("ldm bits {ldm:05b}");
for level in [1u8, 5, 13] {
let compressed =
run_encode_roundtrip(&input(&[level, 2, 1, 0, 0x37, 0x9b, frame_flags], &body))
.expect("the target must reach the encoder, not bail on a short input");
assert!(
compressed > 0,
"{case}: the target compressed an empty body"
);
}
for chunking in [0x05u8, 0x0f] {
let compressed = run_streaming_encode_roundtrip(&input(
&[5, 2, 1, 0, 0x37, 0x9b, frame_flags, chunking],
&body,
))
.expect("the target must reach the encoder, not bail on a short input");
assert!(
compressed > 0,
"{case}: the target compressed an empty body"
);
}
let compressed = run_dictionary_encode_roundtrip(&input(
&[5, 3, 1, 0, 0x37, 0x9b, frame_flags, 64],
&body,
))
.expect("raw dictionary content always parses");
assert!(
compressed > 0,
"{case}: the target compressed an empty body"
);
}
}
#[test]
fn the_long_distance_bit_reaches_the_matcher_with_a_bounded_table() {
const MAX_ENTRIES: usize = 1 << 18;
const WINDOW_BAND: std::ops::RangeInclusive<u32> = 14..=18;
for mask in FUZZ_OVERRIDE_MASKS {
for values in [[0u8, 0], [0x37, 0x9b], [0xff, 0xff], [7, 200], [17, 3]] {
for ldm in 0..32u8 {
let case = format!("mask {mask:#04x} values {values:?} ldm {ldm:05b}");
let options = EncoderOptions {
compression_level: CompressionLevel::try_new(9).expect("a valid level"),
parameters: overrides_from(mask, values, ldm, 0),
..Default::default()
};
let params = compression_parameters_for_options(options, None, None);
if ldm & 1 == 0 {
assert!(params.ldm.is_none(), "{case}: the matcher ran unasked");
continue;
}
let ldm_params = params.ldm.expect("the bit is set, so the matcher must run");
assert!(
1usize << ldm_params.hash_log <= MAX_ENTRIES,
"{case}: asks for a table of 2^{} entries",
ldm_params.hash_log,
);
let window_log = options
.parameters
.window_log
.expect("the matcher's window is always supplied, never forced");
assert!(
WINDOW_BAND.contains(&window_log),
"{case}: window log {window_log} is outside {WINDOW_BAND:?}",
);
}
}
}
}
#[test]
fn every_mode_switch_is_reachable() {
let mut rows = Vec::new();
let mut literals = Vec::new();
for byte in 0..=u8::MAX {
let (options, _) = encoder_options_from([5, byte, 1, 0, 0, 0, 0]);
rows.push(options.parameters.use_row_match_finder);
literals.push(options.parameters.literal_compression);
}
for mode in [
RowMatchFinderMode::Auto,
RowMatchFinderMode::Enabled,
RowMatchFinderMode::Disabled,
] {
assert!(rows.contains(&mode), "no control byte names {mode:?}");
}
for mode in [
LiteralCompressionMode::Auto,
LiteralCompressionMode::Enabled,
LiteralCompressionMode::Disabled,
] {
assert!(literals.contains(&mode), "no control byte names {mode:?}");
}
assert!(
(0..=u8::MAX).any(|byte| {
let (options, _) = encoder_options_from([5, byte, 1, 0, 0, 0, 0]);
options.parameters.use_row_match_finder == RowMatchFinderMode::Enabled
&& options.parameters.literal_compression == LiteralCompressionMode::Disabled
}),
"the two mode switches cannot be set independently"
);
}
#[test]
fn the_matcher_changes_the_frame_it_runs_on() {
let body = tile_with_drift(&structured_body(700), 1 << 16);
let (off, _) = encoder_options_from([5, 2, 1, 0x01, 6, 0x9b, 0]);
let (on, _) = encoder_options_from([5, 2, 1, 0x01, 6, 0x9b, 0b100]);
assert_eq!(
(off.parameters.window_log, on.parameters.window_log),
(Some(16), Some(16)),
"the two sides did not resolve to the same window"
);
assert!(
compression_parameters_for_options(off, None, None)
.ldm
.is_none()
);
assert!(
compression_parameters_for_options(on, None, None)
.ldm
.is_some()
);
let without = encode_all_with_options(&body, off).expect("valid options");
let with = encode_all_with_options(&body, on).expect("valid options");
assert_ne!(
without, with,
"the long-distance matcher was enabled and changed nothing"
);
}
#[test]
fn inputs_too_short_to_configure_are_declined() {
for len in 0..3 {
assert_eq!(run_encode_roundtrip(&vec![0u8; len]), None);
}
for len in 0..4 {
assert_eq!(run_streaming_encode_roundtrip(&vec![0u8; len]), None);
assert_eq!(run_dictionary_encode_roundtrip(&vec![0u8; len]), None);
}
}
}