test: cover archive and input safety limits
This commit is contained in:
parent
8d1965b4c5
commit
2c02cc201b
39
src/error.rs
39
src/error.rs
|
|
@ -130,3 +130,42 @@ impl InputError {
|
|||
|
||||
/// Result type used by the input layer.
|
||||
pub type InputResult<T> = Result<T, InputError>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn source_identity_is_available_only_for_document_specific_failures() {
|
||||
let source_id = SourceId::Stdin;
|
||||
for error in [
|
||||
InputError::LimitExceeded {
|
||||
source_id: source_id.clone(),
|
||||
resource: "bytes",
|
||||
actual: 2,
|
||||
limit: 1,
|
||||
},
|
||||
InputError::CompressionRatio {
|
||||
source_id: source_id.clone(),
|
||||
limit: 2,
|
||||
},
|
||||
InputError::EncryptedMember {
|
||||
source_id: source_id.clone(),
|
||||
},
|
||||
InputError::BinaryContent {
|
||||
source_id: source_id.clone(),
|
||||
},
|
||||
InputError::InvalidUtf8 {
|
||||
source_id: source_id.clone(),
|
||||
source: String::from_utf8(vec![0xff]).unwrap_err(),
|
||||
},
|
||||
] {
|
||||
assert_eq!(error.source_id(), Some(&source_id));
|
||||
}
|
||||
|
||||
let error = InputError::MissingPath {
|
||||
path: PathBuf::from("missing"),
|
||||
};
|
||||
assert_eq!(error.source_id(), None);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -213,8 +213,24 @@ fn safe_member_path(archive: &Path, name: &str) -> InputResult<PathBuf> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Write;
|
||||
|
||||
use tempfile::tempdir;
|
||||
use zip::write::FileOptions;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn create_archive(path: &Path, entries: &[(&str, &[u8])], compression: zip::CompressionMethod) {
|
||||
let file = File::create(path).unwrap();
|
||||
let mut writer = zip::ZipWriter::new(file);
|
||||
let options: FileOptions<()> = FileOptions::default().compression_method(compression);
|
||||
for (name, bytes) in entries {
|
||||
writer.start_file(*name, options).unwrap();
|
||||
writer.write_all(bytes).unwrap();
|
||||
}
|
||||
writer.finish().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_absolute_and_traversing_member_paths() {
|
||||
for path in [
|
||||
|
|
@ -232,5 +248,179 @@ mod tests {
|
|||
safe_member_path(Path::new("archive.zip"), "safe/member.txt").unwrap(),
|
||||
Path::new("safe/member.txt")
|
||||
);
|
||||
assert!(safe_member_path(Path::new("archive.zip"), "").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_archives_reports_filesystem_format_size_and_entry_limits() {
|
||||
let directory = tempdir().unwrap();
|
||||
let missing = directory.path().join("missing.zip");
|
||||
assert!(matches!(
|
||||
ArchiveDocuments::open(missing.clone(), &InputLimits::default()),
|
||||
Err(InputError::Io { action: "inspect", path, .. }) if path == missing
|
||||
));
|
||||
|
||||
let malformed = directory.path().join("malformed.zip");
|
||||
fs::write(&malformed, b"not a zip").unwrap();
|
||||
assert!(matches!(
|
||||
ArchiveDocuments::open(malformed.clone(), &InputLimits::default()),
|
||||
Err(InputError::Zip { archive, .. }) if archive == malformed
|
||||
));
|
||||
|
||||
let path = directory.path().join("limits.zip");
|
||||
create_archive(
|
||||
&path,
|
||||
&[("one.txt", b"one"), ("two.txt", b"two")],
|
||||
zip::CompressionMethod::Stored,
|
||||
);
|
||||
let limits = InputLimits {
|
||||
max_archive_bytes: 1,
|
||||
..InputLimits::default()
|
||||
};
|
||||
assert!(matches!(
|
||||
ArchiveDocuments::open(path.clone(), &limits),
|
||||
Err(InputError::LimitExceeded {
|
||||
resource: "bytes",
|
||||
..
|
||||
})
|
||||
));
|
||||
|
||||
let limits = InputLimits {
|
||||
max_archive_bytes: u64::MAX,
|
||||
max_archive_entries: 1,
|
||||
..InputLimits::default()
|
||||
};
|
||||
assert!(matches!(
|
||||
ArchiveDocuments::open(path, &limits),
|
||||
Err(InputError::LimitExceeded {
|
||||
resource: "entries",
|
||||
actual: 2,
|
||||
limit: 1,
|
||||
..
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iterator_skips_directories_and_preserves_safe_member_identity() {
|
||||
let directory = tempdir().unwrap();
|
||||
let path = directory.path().join("documents.zip");
|
||||
let file = File::create(&path).unwrap();
|
||||
let mut writer = zip::ZipWriter::new(file);
|
||||
let options: FileOptions<()> = FileOptions::default();
|
||||
writer.add_directory("docs/", options).unwrap();
|
||||
writer.start_file("docs/article.txt", options).unwrap();
|
||||
writer.write_all("héllo".as_bytes()).unwrap();
|
||||
writer.finish().unwrap();
|
||||
|
||||
let mut archive = ArchiveDocuments::open(path.clone(), &InputLimits::default()).unwrap();
|
||||
let debug = format!("{archive:?}");
|
||||
assert!(debug.contains("members: 2"));
|
||||
let document = archive.next().unwrap().unwrap();
|
||||
assert_eq!(document.content, "héllo");
|
||||
assert_eq!(
|
||||
document.source,
|
||||
SourceId::ArchiveMember {
|
||||
archive: path,
|
||||
member: PathBuf::from("docs/article.txt"),
|
||||
}
|
||||
);
|
||||
assert!(archive.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iterator_rejects_unsafe_binary_and_non_utf8_members_independently() {
|
||||
for (name, bytes, expected) in [
|
||||
("../escape.txt", b"text".as_slice(), "unsafe"),
|
||||
("binary.txt", b"a\0b".as_slice(), "binary"),
|
||||
("invalid.txt", [0xff].as_slice(), "utf8"),
|
||||
] {
|
||||
let directory = tempdir().unwrap();
|
||||
let path = directory.path().join("invalid-member.zip");
|
||||
create_archive(&path, &[(name, bytes)], zip::CompressionMethod::Stored);
|
||||
let error = ArchiveDocuments::open(path, &InputLimits::default())
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(
|
||||
(expected, error),
|
||||
("unsafe", InputError::UnsafeArchiveMember { .. })
|
||||
| ("binary", InputError::BinaryContent { .. })
|
||||
| ("utf8", InputError::InvalidUtf8 { .. })
|
||||
),
|
||||
"wrong error for {name}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iterator_enforces_member_expansion_and_compression_limits() {
|
||||
let directory = tempdir().unwrap();
|
||||
let oversized = directory.path().join("oversized.zip");
|
||||
create_archive(
|
||||
&oversized,
|
||||
&[("large.txt", b"12345")],
|
||||
zip::CompressionMethod::Stored,
|
||||
);
|
||||
let limits = InputLimits {
|
||||
max_document_bytes: 4,
|
||||
..InputLimits::default()
|
||||
};
|
||||
let error = ArchiveDocuments::open(oversized, &limits)
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
InputError::LimitExceeded {
|
||||
resource: "bytes",
|
||||
actual: 5,
|
||||
limit: 4,
|
||||
..
|
||||
}
|
||||
));
|
||||
|
||||
let compressed = directory.path().join("compressed.zip");
|
||||
create_archive(
|
||||
&compressed,
|
||||
&[("repeated.txt", &[b'a'; 4096])],
|
||||
zip::CompressionMethod::Deflated,
|
||||
);
|
||||
let limits = InputLimits {
|
||||
max_compression_ratio: 1,
|
||||
..InputLimits::default()
|
||||
};
|
||||
assert!(matches!(
|
||||
ArchiveDocuments::open(compressed, &limits)
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap(),
|
||||
Err(InputError::CompressionRatio { limit: 1, .. })
|
||||
));
|
||||
|
||||
let cumulative = directory.path().join("cumulative.zip");
|
||||
create_archive(
|
||||
&cumulative,
|
||||
&[("one.txt", b"1234"), ("two.txt", b"5678")],
|
||||
zip::CompressionMethod::Stored,
|
||||
);
|
||||
let limits = InputLimits {
|
||||
max_expanded_bytes: 6,
|
||||
..InputLimits::default()
|
||||
};
|
||||
let mut archive = ArchiveDocuments::open(cumulative, &limits).unwrap();
|
||||
assert!(archive.next().unwrap().is_ok());
|
||||
assert!(matches!(
|
||||
archive.next().unwrap(),
|
||||
Err(InputError::LimitExceeded {
|
||||
resource: "expanded bytes",
|
||||
actual: u64::MAX,
|
||||
limit: 6,
|
||||
..
|
||||
})
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,14 @@ use zip::write::FileOptions;
|
|||
|
||||
use super::*;
|
||||
|
||||
struct FailingReader;
|
||||
|
||||
impl std::io::Read for FailingReader {
|
||||
fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
|
||||
Err(std::io::Error::other("fixture read failure"))
|
||||
}
|
||||
}
|
||||
|
||||
fn limits() -> InputLimits {
|
||||
InputLimits::default()
|
||||
}
|
||||
|
|
@ -166,4 +174,24 @@ fn stdin_is_bounded_and_decoded_as_strict_utf8() {
|
|||
..
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
read_stdin(b"nul\0byte".as_slice(), &limits()),
|
||||
Err(InputError::BinaryContent {
|
||||
source_id: SourceId::Stdin
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
read_stdin(FailingReader, &limits()),
|
||||
Err(InputError::StdinIo { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regular_file_metadata_failures_retain_operation_context() {
|
||||
let directory = tempdir().expect("temp dir");
|
||||
let missing = directory.path().join("missing.txt");
|
||||
assert!(matches!(
|
||||
file::read_text(&missing, &limits()),
|
||||
Err(InputError::Io { action: "inspect", path, .. }) if path == missing
|
||||
));
|
||||
}
|
||||
|
|
|
|||
2
tasks.md
2
tasks.md
|
|
@ -244,7 +244,7 @@ line-coverage run.
|
|||
|
||||
- [x] Cover configuration merging, validation failures, domain formatting, and
|
||||
diagnostic accessors with behavior-focused unit tests.
|
||||
- [ ] Cover archive and input resource-policy branches with generated fixtures,
|
||||
- [x] Cover archive and input resource-policy branches with generated fixtures,
|
||||
including unsafe, binary, malformed, and oversized members.
|
||||
- [ ] Cover remaining practical CLI, formatter, and LSP adapter branches without
|
||||
coupling tests to unreachable platform failures or coverage-tool artifacts.
|
||||
|
|
|
|||
Loading…
Reference in New Issue