use std::path::Path;
use crate::ExtractionOptions;
use crate::ExtractionReport;
use crate::ProgressCallback;
use crate::Result;
use crate::VerificationOptions;
use crate::inspection::ArchiveManifest;
use crate::inspection::VerificationReport;
pub(crate) mod sealed {
pub trait Sealed {}
}
pub trait ArchiveFormat: sealed::Sealed {
fn extract(
self,
output_dir: &Path,
options: &ExtractionOptions,
progress: &mut dyn ProgressCallback,
) -> Result<ExtractionReport>;
fn list(self) -> Result<ArchiveManifest>;
fn verify(self, options: &VerificationOptions) -> Result<VerificationReport>;
}
#[cfg(test)]
mod tests {
use super::*;
struct TestFormat;
impl sealed::Sealed for TestFormat {}
impl ArchiveFormat for TestFormat {
fn extract(
self,
_output_dir: &Path,
_options: &ExtractionOptions,
_progress: &mut dyn ProgressCallback,
) -> Result<ExtractionReport> {
Ok(ExtractionReport::new())
}
fn list(self) -> Result<ArchiveManifest> {
use crate::formats::detect::ArchiveType;
Ok(ArchiveManifest::new(ArchiveType::Tar))
}
fn verify(self, _options: &VerificationOptions) -> Result<VerificationReport> {
let config = crate::SecurityConfig::default();
let manifest = self.list()?;
Ok(crate::inspection::verify::verify_manifest(
&manifest, &config, None,
))
}
}
#[test]
#[allow(clippy::unwrap_used)]
fn test_trait_extract_returns_report() {
let format = TestFormat;
let temp = tempfile::TempDir::new().unwrap();
let options = ExtractionOptions::default();
let mut noop = crate::NoopProgress;
let report = format.extract(temp.path(), &options, &mut noop).unwrap();
assert_eq!(report.files_extracted, 0);
}
#[test]
#[allow(clippy::unwrap_used)]
fn test_trait_list_returns_empty_manifest() {
let format = TestFormat;
let manifest = format.list().unwrap();
assert_eq!(manifest.total_entries(), 0);
assert_eq!(manifest.total_size(), 0);
}
#[test]
#[allow(clippy::unwrap_used)]
fn test_trait_verify_returns_clean_report_for_empty_archive() {
let format = TestFormat;
let report = format
.verify(&crate::VerificationOptions::default())
.unwrap();
assert_eq!(report.total_entries(), 0);
assert!(report.is_safe());
}
}