#![cfg(feature = "zencodec")]
use std::borrow::Cow;
use zencodec::ResourceLimits;
use zencodec::decode::{Decode, DecodeJob, DecoderConfig};
use ultrahdr_rs::codec::UltraHdrDecoderConfig;
const TEST_ULTRAHDR: &[u8] = include_bytes!("../../test_ultrahdr.jpg");
#[test]
fn decode_rejects_width_over_limit() {
let config = UltraHdrDecoderConfig;
let limits = ResourceLimits::none().with_max_width(10);
let result = config
.job()
.with_limits(limits)
.decoder(Cow::Borrowed(TEST_ULTRAHDR), &[])
.unwrap()
.decode();
let err = match result {
Err(e) => e,
Ok(_) => panic!("decode should fail when image width exceeds max_width=10"),
};
let msg = err.to_string();
assert!(
msg.contains("width") || msg.contains("limit"),
"error should mention width limit: {msg}"
);
}
#[test]
fn decode_rejects_height_over_limit() {
let config = UltraHdrDecoderConfig;
let limits = ResourceLimits::none().with_max_height(10);
let result = config
.job()
.with_limits(limits)
.decoder(Cow::Borrowed(TEST_ULTRAHDR), &[])
.unwrap()
.decode();
let err = match result {
Err(e) => e,
Ok(_) => panic!("decode should fail when image height exceeds max_height=10"),
};
let msg = err.to_string();
assert!(
msg.contains("height") || msg.contains("limit"),
"error should mention height limit: {msg}"
);
}
#[test]
fn decode_rejects_input_over_limit() {
let config = UltraHdrDecoderConfig;
let limits = ResourceLimits::none().with_max_input_bytes(100);
let result = config
.job()
.with_limits(limits)
.decoder(Cow::Borrowed(TEST_ULTRAHDR), &[]);
let err = match result {
Err(e) => e,
Ok(_) => panic!("decoder() should fail when input exceeds max_input_bytes=100"),
};
let msg = err.to_string();
assert!(
msg.contains("input") || msg.contains("limit"),
"error should mention input size limit: {msg}"
);
}
#[test]
fn decode_rejects_memory_over_limit() {
let config = UltraHdrDecoderConfig;
let limits = ResourceLimits::none().with_max_memory(100);
let result = config
.job()
.with_limits(limits)
.decoder(Cow::Borrowed(TEST_ULTRAHDR), &[])
.unwrap()
.decode();
let err = match result {
Err(e) => e,
Ok(_) => panic!("decode should fail when pixel buffer exceeds max_memory_bytes=100"),
};
let msg = err.to_string();
assert!(
msg.contains("memory") || msg.contains("limit"),
"error should mention memory limit: {msg}"
);
}
#[test]
fn probe_rejects_dimensions_over_limit() {
let config = UltraHdrDecoderConfig;
let limits = ResourceLimits::none().with_max_width(10);
let result = config.job().with_limits(limits).probe(TEST_ULTRAHDR);
assert!(
result.is_err(),
"probe should fail when image width exceeds max_width=10"
);
}
#[test]
fn decode_accepts_generous_limits() {
let config = UltraHdrDecoderConfig;
let limits = ResourceLimits::none()
.with_max_width(10000)
.with_max_height(10000)
.with_max_input_bytes(100_000_000)
.with_max_memory(100_000_000);
let result = config
.job()
.with_limits(limits)
.decoder(Cow::Borrowed(TEST_ULTRAHDR), &[])
.unwrap()
.decode();
assert!(
result.is_ok(),
"generous limits should not reject test image: {:?}",
result.err()
);
}
#[test]
fn decode_succeeds_with_no_limits() {
let config = UltraHdrDecoderConfig;
let result = config
.job()
.decoder(Cow::Borrowed(TEST_ULTRAHDR), &[])
.unwrap()
.decode();
assert!(
result.is_ok(),
"no limits should not reject test image: {:?}",
result.err()
);
}