extern crate ssdeep;
use ssdeep::compare;
use ssdeep::hash;
use ssdeep::hash_from_file;
use ssdeep::Error;
#[test]
fn compare_returns_one_hundred_score_when_hashes_are_equal() {
let h1 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C";
let h2 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C";
assert_eq!(compare(h1, h2), Ok(100));
}
#[test]
fn compare_returns_nonzero_score_when_hashes_are_similar() {
let h1 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C";
let h2 = "3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2Cx";
assert_eq!(compare(h1, h2), Ok(22));
}
#[test]
fn compare_returns_zero_when_hashes_are_not_similar() {
let h1 = "3:u+N:u+N";
let h2 = "3:OWIXTn:OWQ";
assert_eq!(compare(h1, h2), Ok(0));
}
#[test]
fn compare_returns_error_when_hash_is_invalid() {
let h1 = "XYZ";
let h2 = "3:tc:u";
assert_eq!(
compare(h1, h2),
Err(Error::CFunctionFailed {
name: "fuzzy_compare".to_string(),
return_code: -1,
})
);
}
#[test]
fn compare_accepts_hashes_as_str() {
let h1 = "3:OWR:OWR";
let h2 = "3:OWR:OWR";
assert_eq!(compare(h1, h2), Ok(100));
}
#[test]
fn compare_accepts_hashes_as_string() {
let h1 = "3:OWR:OWR".to_string();
let h2 = "3:OWR:OWR".to_string();
assert_eq!(compare(&h1, &h2), Ok(100));
}
#[test]
fn hash_returns_correct_hash() {
let h = hash(b"Hello there!").unwrap();
assert_eq!(h, "3:aNRn:aNRn");
}
#[test]
fn hash_accepts_str_as_bytes() {
let h = hash("Hello there!".as_bytes()).unwrap();
assert_eq!(h, "3:aNRn:aNRn");
}
#[test]
fn hash_accepts_string_as_bytes() {
let h = hash("Hello there!".to_string().as_bytes()).unwrap();
assert_eq!(h, "3:aNRn:aNRn");
}
#[test]
fn hash_from_file_returns_correct_hash() {
let h = hash_from_file("tests/file.txt").unwrap();
assert_eq!(
h,
"48:9MABzSwnjpDeSrLp8+nagE4f3ZMvcDT0MIhqy6Ic:9XMwnjdeSHS+n5ZfScX0MJ7",
);
}