aboutsummaryrefslogtreecommitdiff
path: root/b3sum
diff options
context:
space:
mode:
authorLeah Neukirchen <[email protected]>2025-02-28 17:10:39 +0100
committerLeah Neukirchen <[email protected]>2025-02-28 17:34:35 +0100
commitc721a95806173975d469a2260f57294fcc5a3dde (patch)
treee499186186c06a102123886c37e22767c9f7bf13 /b3sum
parentc3ad60d2b13d868d6da25c2587e915908c7066f4 (diff)
add tests
Adapted from https://github.com/BLAKE3-team/BLAKE3/pull/430/ by dbohdan.
Diffstat (limited to 'b3sum')
-rw-r--r--b3sum/tests/cli_tests.rs176
1 files changed, 176 insertions, 0 deletions
diff --git a/b3sum/tests/cli_tests.rs b/b3sum/tests/cli_tests.rs
index cd48ac7..8f3520c 100644
--- a/b3sum/tests/cli_tests.rs
+++ b/b3sum/tests/cli_tests.rs
@@ -16,6 +16,13 @@ fn test_hash_one() {
}
#[test]
+fn test_hash_one_tag() {
+ let expected = format!("BLAKE3 (-) = {}", blake3::hash(b"foo").to_hex());
+ let output = cmd!(b3sum_exe(), "--tag").stdin_bytes("foo").read().unwrap();
+ assert_eq!(&*expected, output);
+}
+
+#[test]
fn test_hash_one_raw() {
let expected = blake3::hash(b"foo").as_bytes().to_owned();
let output = cmd!(b3sum_exe(), "--raw")
@@ -56,6 +63,28 @@ fn test_hash_many() {
}
#[test]
+fn test_hash_many_tag() {
+ let dir = tempfile::tempdir().unwrap();
+ let file1 = dir.path().join("file1");
+ fs::write(&file1, b"foo").unwrap();
+ let file2 = dir.path().join("file2");
+ fs::write(&file2, b"bar").unwrap();
+
+ let output = cmd!(b3sum_exe(), "--tag", &file1, &file2).read().unwrap();
+ let foo_hash = blake3::hash(b"foo");
+ let bar_hash = blake3::hash(b"bar");
+ let expected = format!(
+ "BLAKE3 ({}) = {}\nBLAKE3 ({}) = {}",
+ // account for slash normalization on Windows
+ file1.to_string_lossy().replace("\\", "/"),
+ foo_hash.to_hex(),
+ file2.to_string_lossy().replace("\\", "/"),
+ bar_hash.to_hex(),
+ );
+ assert_eq!(expected, output);
+}
+
+#[test]
fn test_missing_files() {
let dir = tempfile::tempdir().unwrap();
let file1 = dir.path().join("file1");
@@ -521,6 +550,153 @@ fn test_check() {
}
#[test]
+fn test_check_tag() {
+ // Make a directory full of files, and make sure the b3sum output in that
+ // directory is what we expect.
+ let a_hash = blake3::hash(b"a").to_hex();
+ let b_hash = blake3::hash(b"b").to_hex();
+ let cd_hash = blake3::hash(b"cd").to_hex();
+ let dir = tempfile::tempdir().unwrap();
+ fs::write(dir.path().join("a"), b"a").unwrap();
+ fs::write(dir.path().join("b"), b"b").unwrap();
+ fs::create_dir(dir.path().join("c")).unwrap();
+ fs::write(dir.path().join("c/d"), b"cd").unwrap();
+ let output = cmd!(b3sum_exe(), "--tag", "a", "b", "c/d")
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let expected_checkfile = format!(
+ "BLAKE3 (a) = {}\n\
+ BLAKE3 (b) = {}\n\
+ BLAKE3 (c/d) = {}\n",
+ a_hash, b_hash, cd_hash,
+ );
+ assert_eq!(expected_checkfile, stdout);
+ assert_eq!("", stderr);
+
+ // Now use the output we just validated as a checkfile, passed to stdin.
+ let output = cmd!(b3sum_exe(), "--check")
+ .stdin_bytes(expected_checkfile.as_bytes())
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let expected_check_output = "\
+ a: OK\n\
+ b: OK\n\
+ c/d: OK\n";
+ assert_eq!(expected_check_output, stdout);
+ assert_eq!("", stderr);
+
+ // Check the same file, but with Windows-style newlines.
+ let windows_style = expected_checkfile.replace("\n", "\r\n");
+ let output = cmd!(b3sum_exe(), "--check")
+ .stdin_bytes(windows_style.as_bytes())
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let expected_check_output = "\
+ a: OK\n\
+ b: OK\n\
+ c/d: OK\n";
+ assert_eq!(expected_check_output, stdout);
+ assert_eq!("", stderr);
+
+ // Now pass the same checkfile twice on the command line just for fun.
+ let checkfile_path = dir.path().join("checkfile");
+ fs::write(&checkfile_path, &expected_checkfile).unwrap();
+ let output = cmd!(b3sum_exe(), "--check", &checkfile_path, &checkfile_path)
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let mut double_check_output = String::new();
+ double_check_output.push_str(&expected_check_output);
+ double_check_output.push_str(&expected_check_output);
+ assert_eq!(double_check_output, stdout);
+ assert_eq!("", stderr);
+
+ // Corrupt one of the files and check again.
+ fs::write(dir.path().join("b"), b"CORRUPTION").unwrap();
+ let output = cmd!(b3sum_exe(), "--check", &checkfile_path)
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .unchecked()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let expected_check_failure = "\
+ a: OK\n\
+ b: FAILED\n\
+ c/d: OK\n";
+ assert!(!output.status.success());
+ assert_eq!(expected_check_failure, stdout);
+ assert_eq!(
+ "b3sum: WARNING: 1 computed checksum did NOT match\n",
+ stderr,
+ );
+
+ // Delete one of the files and check again.
+ fs::remove_file(dir.path().join("b")).unwrap();
+ let open_file_error = fs::File::open(dir.path().join("b")).unwrap_err();
+ let output = cmd!(b3sum_exe(), "--check", &checkfile_path)
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .unchecked()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let expected_check_failure = format!(
+ "a: OK\n\
+ b: FAILED ({})\n\
+ c/d: OK\n",
+ open_file_error,
+ );
+ assert!(!output.status.success());
+ assert_eq!(expected_check_failure, stdout);
+ assert_eq!(
+ "b3sum: WARNING: 1 computed checksum did NOT match\n",
+ stderr,
+ );
+
+ // Confirm that --quiet suppresses the OKs but not the FAILEDs.
+ let output = cmd!(b3sum_exe(), "--check", "--quiet", &checkfile_path)
+ .dir(dir.path())
+ .stdout_capture()
+ .stderr_capture()
+ .unchecked()
+ .run()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ let expected_check_failure = format!("b: FAILED ({})\n", open_file_error);
+ assert!(!output.status.success());
+ assert_eq!(expected_check_failure, stdout);
+ assert_eq!(
+ "b3sum: WARNING: 1 computed checksum did NOT match\n",
+ stderr,
+ );
+}
+
+#[test]
fn test_check_invalid_characters() {
// Check that a null character in the path fails.
let output = cmd!(b3sum_exe(), "--check")