From 955f0e1510baee72fa7447d2045df1a3f31982b1 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Tue, 5 Oct 2021 04:45:05 +1000 Subject: [PATCH] litl bit more testing --- src/files.rs | 4 ++-- src/findings.rs | 8 +------- src/main.rs | 7 +++---- src/tests/mod.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/files.rs b/src/files.rs index 514275d..620fb87 100644 --- a/src/files.rs +++ b/src/files.rs @@ -262,7 +262,7 @@ pub fn mime_type(db: &T, path: &Path) -> io::Result> { Err(_) => break, } } - + let read = read?; let r = db.get_type(&buffer); @@ -298,7 +298,7 @@ pub fn mime_type(db: &T, path: &Path) -> io::Result> { /// Returns a list of known extensions for this mime type, if any. /// This function uses the [`Mime`]'s "essence" rather than the [`Mime`] itself - [`mime_guess::get_mime_extensions`] /// ignores the type suffix, treating "image/svg+xml" as "image/svg", and thus fails to find any extensions. Passing the -/// essence_str (which includes the suffix) fixes this. +/// `essence_str` (which includes the suffix) fixes this. pub fn mime_extension_lookup(essence: String) -> Option> { if let Ok(cache) = MIMEXT.read() { if let Some(exts) = cache.get(&essence) { diff --git a/src/findings.rs b/src/findings.rs index 67f5940..669d95b 100644 --- a/src/findings.rs +++ b/src/findings.rs @@ -13,7 +13,7 @@ use crate::files::mime_extension_lookup; use crate::String; /// Information about a scanned file. -#[derive(Eq, PartialEq)] +#[derive(Eq, PartialEq, Debug)] pub struct Findings { /// The location of the scanned file. pub file: PathBuf, @@ -45,12 +45,6 @@ impl Ord for Findings { fn cmp(&self, other: &Self) -> Ordering { // files with no recommended extension should appear first, so that fif outputs the "no known extension for x" // comments before the "mv x y" instructions - // since fif doesn't output anything for valid files, the comparison will consider any comparison involving a - // valid Findings to be equal, avoiding the (somewhat) expensive call to recommended_extension. after all, since - // fif never displays valid files, it really doesn't matter what position they end up in. - if self.valid || other.valid { - return Ordering::Equal; - } match (self.recommended_extension(), other.recommended_extension()) { (None, Some(_)) => Ordering::Greater, (Some(_), None) => Ordering::Less, diff --git a/src/main.rs b/src/main.rs index 889e57f..cd4e216 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,10 +23,9 @@ use std::process::exit; use cfg_if::cfg_if; use clap::Clap; use fif::files::{scan_directory, scan_from_walkdir}; -use fif::formats::Format; -use fif::parameters::{OutputFormat, Prompt}; +use fif::formats::{self, Format}; +use fif::parameters::{self, OutputFormat, Prompt}; use fif::utils::{os_name, CLAP_LONG_VERSION}; -use fif::{formats, parameters}; use itertools::Itertools; use log::{debug, error, info, trace, warn, Level}; @@ -205,7 +204,7 @@ fn main() { let mut buffered_stdout = BufWriter::new(stdout()); let result = match args.output_format { - // i want to simplify this to something like formats::write_all(args.output_format, ...) + // TODO: simplify this to something like formats::write_all(args.output_format, ...) OutputFormat::Sh => formats::Shell.write_all(&mut buffered_stdout, &findings, &errors), OutputFormat::PowerShell => formats::PowerShell.write_all(&mut buffered_stdout, &findings, &errors), #[cfg(feature = "json")] diff --git a/src/tests/mod.rs b/src/tests/mod.rs index bb12b91..e08e775 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -8,6 +8,7 @@ use fif::findings::Findings; use fif::formats::{Format, PowerShell, Shell}; use fif::mime_db::MimeDb; use fif::{String, MIMEDB}; +use itertools::Itertools; use maplit::{btreeset, hashmap}; use mime::{Mime, APPLICATION_OCTET_STREAM, APPLICATION_PDF, IMAGE_JPEG, IMAGE_PNG}; @@ -324,7 +325,7 @@ fn identify_random_bytes() { let mut bytes: [u8; BUF_SIZE * 2] = [0; BUF_SIZE * 2]; let mut results: BTreeMap = BTreeMap::new(); - for _ in 1..10000 { + for _ in 1..1000 { rng.fill_bytes(&mut bytes); if let Some(detected_type) = MIMEDB.get_type(&bytes) { *results.entry(detected_type).or_insert(0) += 1; @@ -467,7 +468,44 @@ fn verbosity() { } #[test] -/// Ensures that smart strings don't deviate from std's Strings +/// Ensures `os_name()`'s output is the same as [`std::env::consts::OS`], capitalisation notwithstanding +fn validate_os_name() { + assert_eq!( + fif::utils::os_name().to_lowercase(), + std::env::consts::OS.to_lowercase() + ); +} + +#[test] +/// Ensures that [`Findings`] are sorted properly. +fn sort_findings() { + let findings = vec![ + Findings { + file: Path::new("ccc").to_path_buf(), + valid: false, + mime: IMAGE_JPEG, + }, + Findings { + file: Path::new("bbb.xyz").to_path_buf(), + valid: true, + mime: IMAGE_PNG, + }, + Findings { + file: Path::new("aaa").to_path_buf(), + valid: true, + mime: APPLICATION_PDF, + }, + ]; + let mut findings = findings.iter().sorted_unstable(); + + assert_eq!(findings.next().unwrap().file, Path::new("aaa")); + assert_eq!(findings.next().unwrap().file, Path::new("bbb.xyz")); + assert_eq!(findings.next().unwrap().file, Path::new("ccc")); + assert_eq!(findings.next(), None); +} + +#[test] +/// Ensures that [`SmartString`]s don't deviate from std's Strings fn validate_string_type() { use std::string::String as StdString;