2019-12-12 08:13:16 +01:00
|
|
|
use duct::cmd;
|
|
|
|
use std::fs;
|
2019-12-13 21:56:21 +01:00
|
|
|
use std::io::prelude::*;
|
2019-12-12 08:13:16 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
pub fn b3sum_exe() -> PathBuf {
|
|
|
|
assert_cmd::cargo::cargo_bin("b3sum")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_one() {
|
|
|
|
let expected = blake3::hash(b"foo").to_hex();
|
|
|
|
let output = cmd!(b3sum_exe()).stdin_bytes("foo").read().unwrap();
|
|
|
|
assert_eq!(&*expected, &*output);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_many() {
|
2019-12-13 21:56:21 +01:00
|
|
|
let dir = tempfile::tempdir().unwrap();
|
2019-12-12 08:13:16 +01:00
|
|
|
let file1 = dir.path().join("file1");
|
|
|
|
fs::write(&file1, b"foo").unwrap();
|
|
|
|
let file2 = dir.path().join("file2");
|
|
|
|
fs::write(&file2, b"bar").unwrap();
|
2019-12-13 21:56:21 +01:00
|
|
|
|
2019-12-12 08:13:16 +01:00
|
|
|
let output = cmd!(b3sum_exe(), &file1, &file2).read().unwrap();
|
|
|
|
let foo_hash = blake3::hash(b"foo");
|
|
|
|
let bar_hash = blake3::hash(b"bar");
|
|
|
|
let expected = format!(
|
|
|
|
"{} {}\n{} {}",
|
|
|
|
foo_hash.to_hex(),
|
|
|
|
file1.to_string_lossy(),
|
|
|
|
bar_hash.to_hex(),
|
|
|
|
file2.to_string_lossy(),
|
|
|
|
);
|
|
|
|
assert_eq!(expected, output);
|
2019-12-13 21:56:21 +01:00
|
|
|
|
|
|
|
let output_no_names = cmd!(b3sum_exe(), "--no-names", &file1, &file2)
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
|
|
|
let expected_no_names = format!("{}\n{}", foo_hash.to_hex(), bar_hash.to_hex(),);
|
|
|
|
assert_eq!(expected_no_names, output_no_names);
|
2019-12-12 08:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_length() {
|
|
|
|
let mut buf = [0; 100];
|
|
|
|
blake3::Hasher::new()
|
|
|
|
.update(b"foo")
|
|
|
|
.finalize_xof()
|
|
|
|
.fill(&mut buf);
|
|
|
|
let expected = hex::encode(&buf[..]);
|
|
|
|
let output = cmd!(b3sum_exe(), "--length=100")
|
|
|
|
.stdin_bytes("foo")
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(&*expected, &*output);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-13 21:56:21 +01:00
|
|
|
fn test_keyed() {
|
2019-12-12 08:13:16 +01:00
|
|
|
let key = [42; blake3::KEY_LEN];
|
2019-12-13 21:56:21 +01:00
|
|
|
let f = tempfile::NamedTempFile::new().unwrap();
|
|
|
|
f.as_file().write_all(b"foo").unwrap();
|
|
|
|
f.as_file().flush().unwrap();
|
2019-12-12 08:13:16 +01:00
|
|
|
let expected = blake3::keyed_hash(&key, b"foo").to_hex();
|
2019-12-13 21:56:21 +01:00
|
|
|
let output = cmd!(b3sum_exe(), "--keyed", "--no-names", f.path())
|
|
|
|
.stdin_bytes(&key[..])
|
2019-12-12 08:13:16 +01:00
|
|
|
.read()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(&*expected, &*output);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_derive_key() {
|
2019-12-27 23:20:38 +01:00
|
|
|
let context = "BLAKE3 2019-12-28 10:28:41 example context";
|
2019-12-13 21:56:21 +01:00
|
|
|
let f = tempfile::NamedTempFile::new().unwrap();
|
2019-12-27 23:20:38 +01:00
|
|
|
f.as_file().write_all(b"key material").unwrap();
|
2019-12-13 21:56:21 +01:00
|
|
|
f.as_file().flush().unwrap();
|
2019-12-27 23:20:38 +01:00
|
|
|
let mut derive_key_out = [0; blake3::OUT_LEN];
|
|
|
|
blake3::derive_key(context, b"key material", &mut derive_key_out);
|
|
|
|
let expected = hex::encode(&derive_key_out);
|
|
|
|
let output = cmd!(b3sum_exe(), "--derive-key", context, "--no-names", f.path())
|
2019-12-12 08:13:16 +01:00
|
|
|
.read()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(&*expected, &*output);
|
|
|
|
}
|