blazehash 0.2.3

Forensic file hasher — hashdeep for the modern era, BLAKE3 by default
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

#[test]
fn cli_version() {
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("blazehash"));
}

#[test]
fn cli_hash_single_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello world").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("HASHDEEP-1.0"))
        .stdout(predicate::str::contains("blake3"))
        .stdout(predicate::str::contains("test.txt"));
}

#[test]
fn cli_hash_directory_recursive() {
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("a.txt"), b"aaa").unwrap();
    let sub = dir.path().join("sub");
    fs::create_dir(&sub).unwrap();
    fs::write(sub.join("b.txt"), b"bbb").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-r")
        .arg(dir.path().to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("a.txt"))
        .stdout(predicate::str::contains("b.txt"));
}

#[test]
fn cli_multiple_algorithms() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-c")
        .arg("blake3,sha256")
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("size,blake3,sha256,filename"));
}

#[test]
fn cli_output_to_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello").unwrap();
    let output = dir.path().join("output.hash");

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-o")
        .arg(output.to_str().unwrap())
        .arg(file.to_str().unwrap())
        .assert()
        .success();

    let contents = fs::read_to_string(&output).unwrap();
    assert!(contents.contains("HASHDEEP-1.0"));
    assert!(contents.contains("test.txt"));
}

#[test]
fn cli_size_only_mode() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello world").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-s")
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("11"));
}

#[test]
fn cli_piecewise_hashing() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    // Write 2000 bytes to ensure multiple chunks at 1K chunk size
    let data = vec![0x42u8; 2000];
    fs::write(&file, &data).unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-p")
        .arg("1K")
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("test.txt"));
}

#[test]
fn cli_resume_flag() {
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("a.txt"), b"aaa").unwrap();
    fs::write(dir.path().join("b.txt"), b"bbb").unwrap();
    let manifest = dir.path().join("manifest.hash");

    // First run: hash only a.txt
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-o")
        .arg(manifest.to_str().unwrap())
        .arg(dir.path().join("a.txt").to_str().unwrap())
        .assert()
        .success();

    // Second run with --resume: should append b.txt but not re-hash a.txt
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("--resume")
        .arg("-o")
        .arg(manifest.to_str().unwrap())
        .arg("-r")
        .arg(dir.path().to_str().unwrap())
        .assert()
        .success();

    let contents = fs::read_to_string(&manifest).unwrap();
    assert!(contents.contains("a.txt"));
    assert!(contents.contains("b.txt"));
}

#[test]
fn cli_no_args_exits_ok() {
    // With MCP subcommand support, no args is valid (produces no output, exits 0)
    Command::cargo_bin("blazehash").unwrap().assert().success();
}

#[test]
fn cli_nonexistent_file_returns_error() {
    // blazehash exits 0 but produces no file entries for nonexistent paths
    let output = Command::cargo_bin("blazehash")
        .unwrap()
        .arg("/nonexistent/file.txt")
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    // Header is present but no data lines (no hash lines after the ## comments)
    assert!(stdout.contains("HASHDEEP-1.0"));
    // There should be no actual hash line (only header/comment lines start with % or #)
    let data_lines: Vec<&str> = stdout
        .lines()
        .filter(|l| !l.starts_with('%') && !l.starts_with('#') && !l.is_empty())
        .collect();
    assert!(
        data_lines.is_empty(),
        "expected no data lines, got: {data_lines:?}"
    );
}

#[test]
fn cli_size_only_directory() {
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("a.txt"), b"aaa").unwrap();
    fs::write(dir.path().join("b.txt"), b"bbbb").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-s")
        .arg("-r")
        .arg(dir.path().to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("3"))
        .stdout(predicate::str::contains("4"));
}

#[test]
fn cli_size_only_summary_stderr() {
    // -s should print a [+] summary line on stderr: file count, total size, time estimate
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("a.txt"), b"aaa").unwrap();
    fs::write(dir.path().join("b.txt"), b"bbbb").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-s")
        .arg("-r")
        .arg(dir.path().to_str().unwrap())
        .assert()
        .success()
        .stderr(predicate::str::contains("2 files"))
        .stderr(predicate::str::contains("est."));
}

#[test]
fn cli_size_only_single_file_summary() {
    // Single-file -s should also emit summary on stderr
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello world").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-s")
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stderr(predicate::str::contains("1 file"))
        .stderr(predicate::str::contains("est."));
}

#[test]
fn cli_bare_mode_no_header() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello").unwrap();

    let output = Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-b")
        .arg(file.to_str().unwrap())
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(!stdout.contains("HASHDEEP"));
    assert!(!stdout.contains("%%%%"));
    assert!(stdout.contains("test.txt"));
}

#[test]
fn cli_jsonl_format() {
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("a.txt"), b"aaa").unwrap();
    fs::write(dir.path().join("b.txt"), b"bbb").unwrap();

    let output = Command::cargo_bin("blazehash")
        .unwrap()
        .arg("--format")
        .arg("jsonl")
        .arg(dir.path().join("a.txt").to_str().unwrap())
        .arg(dir.path().join("b.txt").to_str().unwrap())
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    let lines: Vec<&str> = stdout.lines().collect();
    assert_eq!(lines.len(), 2);
    // Each line should be valid JSON
    for line in &lines {
        let _: serde_json::Value = serde_json::from_str(line).unwrap();
    }
}

#[test]
fn cli_audit_shows_summary() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello world").unwrap();
    let manifest = dir.path().join("manifest.hash");

    // First hash
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-o")
        .arg(manifest.to_str().unwrap())
        .arg(file.to_str().unwrap())
        .assert()
        .success();

    // Audit
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-a")
        .arg("-k")
        .arg(manifest.to_str().unwrap())
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("Files matched: 1"))
        .stdout(predicate::str::contains("Files changed: 0"))
        .stdout(predicate::str::contains("Files missing: 0"));
}

#[test]
fn cli_audit_detects_changes() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"original content").unwrap();
    let manifest = dir.path().join("manifest.hash");

    // Hash original
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-o")
        .arg(manifest.to_str().unwrap())
        .arg(file.to_str().unwrap())
        .assert()
        .success();

    // Modify file
    fs::write(&file, b"modified content").unwrap();

    // Audit should detect change
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-a")
        .arg("-k")
        .arg(manifest.to_str().unwrap())
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("Files changed: 1"));
}

#[test]
fn cli_piecewise_bare_mode() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    let data = vec![0x42u8; 2000];
    fs::write(&file, &data).unwrap();

    let output = Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-p")
        .arg("1K")
        .arg("-b")
        .arg(file.to_str().unwrap())
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(!stdout.contains("HASHDEEP"));
    assert!(stdout.contains("test.txt"));
}

#[test]
fn cli_multiple_c_flags() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-c")
        .arg("blake3")
        .arg("-c")
        .arg("sha256")
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("blake3"))
        .stdout(predicate::str::contains("sha256"));
}

#[test]
fn cli_resume_without_output_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    fs::write(&file, b"hello").unwrap();

    // --resume without -o should still work (just doesn't actually resume)
    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("--resume")
        .arg(file.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("test.txt"));
}

// ── -o auto-naming ────────────────────────────────────────────────────────────

#[test]
fn cli_bare_o_derives_dirname_hash() {
    let dir = TempDir::new().unwrap();
    let evidence = dir.path().join("smith-2026");
    fs::create_dir_all(&evidence).unwrap();
    fs::write(evidence.join("file.txt"), b"data").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg("-r")
        .arg(&evidence)
        .arg("-o") // no filename — should auto-derive smith-2026.hash
        .current_dir(dir.path())
        .assert()
        .success();

    assert!(
        dir.path().join("smith-2026.hash").exists(),
        "smith-2026.hash not created"
    );
}

#[test]
fn cli_bare_o_falls_back_to_manifest_hash_for_dot() {
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("file.txt"), b"data").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg(".")
        .arg("-o")
        .current_dir(dir.path())
        .assert()
        .success();

    assert!(
        dir.path().join("manifest.hash").exists(),
        "manifest.hash not created"
    );
}

#[test]
fn cli_explicit_o_filename_unchanged() {
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("file.txt"), b"data").unwrap();
    let out = dir.path().join("custom.hash");

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg(dir.path())
        .arg("-o")
        .arg(&out)
        .assert()
        .success();

    assert!(out.exists(), "custom.hash not created");
}

#[test]
fn cli_nsrl_bloom_rejected() {
    let dir = TempDir::new().unwrap();
    let bloom = dir.path().join("nsrl.bloom");
    fs::write(&bloom, b"fake").unwrap();
    fs::write(dir.path().join("file.txt"), b"data").unwrap();

    Command::cargo_bin("blazehash")
        .unwrap()
        .arg(dir.path())
        .arg("--nsrl")
        .arg(&bloom)
        .assert()
        .failure()
        .stderr(predicate::str::is_match("(?i)bloom|not supported|sqlite").unwrap());
}