use std::fs;
use std::io::{self, Read};
use super::{Payload, digest};
use crate::install::fixtures::Hooks;
use crate::install::tests::{Fixture, TestResult};
struct FailedRead;
impl Read for FailedRead {
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
Err(io::Error::other("fixture read refused"))
}
}
#[test]
fn a_payload_read_failure_keeps_its_original_name() -> TestResult {
let error = digest(&mut FailedRead, false)
.err()
.ok_or("failed reader accepted")?;
assert_eq!(error.code, "bundle_read_failed");
Ok(())
}
#[test]
fn payload_growth_or_change_is_named_after_copying() -> TestResult {
let fixture = Fixture::new()?;
let mut payload = Payload::executable(&fixture.binary)?;
let expected = payload.entry(0o700)?;
fs::write(&fixture.binary, b"changed operator")?;
let target = fixture.directory.path().join("copied");
let error = payload
.write(&target, &expected, &mut Hooks::default())
.err()
.ok_or("source change accepted")?;
assert_eq!(error.code, "bundle_source_changed");
Ok(())
}
#[test]
fn a_payload_copy_bound_overflow_keeps_its_name() -> TestResult {
let fixture = Fixture::new()?;
let mut payload = Payload::executable(&fixture.binary)?;
let mut expected = payload.entry(0o700)?;
expected.bytes = u64::MAX;
let error = payload
.write(
&fixture.directory.path().join("overflow"),
&expected,
&mut Hooks::default(),
)
.err()
.ok_or("copy overflow accepted")?;
assert_eq!(error.code, "bundle_size_overflow");
Ok(())
}
#[test]
fn the_stand_in_marker_is_detected_across_digest_chunks() -> TestResult {
let fixture = Fixture::new()?;
let mut bytes = vec![b'x'; 65_536 + b"embed-stand-in".len() - 4];
bytes.extend_from_slice(b"embed-stand-in");
fs::write(&fixture.binary, bytes)?;
let error = Payload::worker(&fixture.binary)?
.entry(0o700)
.err()
.ok_or("split marker accepted")?;
assert_eq!(error.code, "bundle_test_worker_refused");
Ok(())
}