blazehash 0.2.4

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
#![cfg(feature = "gpu")]

use blazehash::gpu::config::{GpuConfig, GpuConfigState};
use tempfile::TempDir;

fn temp_config_path() -> (TempDir, std::path::PathBuf) {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("config.toml");
    (dir, path)
}

#[test]
fn test_config_no_file_returns_none() {
    let (_dir, path) = temp_config_path();
    let config = GpuConfig::load(&path);
    assert!(config.is_none(), "no config file → no config");
}

#[test]
fn test_config_write_and_load_roundtrip() {
    let (_dir, path) = temp_config_path();
    let cfg = GpuConfig {
        device: "NVIDIA RTX 3090".to_string(),
        calibrated: "2026-04-09".to_string(),
        threshold_single_mb: 48,
        threshold_multi_mb: 3,
        gpu_enabled: true,
    };
    cfg.save(&path).unwrap();
    let loaded = GpuConfig::load(&path).unwrap();
    assert_eq!(loaded.device, "NVIDIA RTX 3090");
    assert_eq!(loaded.threshold_single_mb, 48);
    assert!(loaded.gpu_enabled);
}

#[test]
fn test_config_corrupted_returns_none() {
    let (_dir, path) = temp_config_path();
    std::fs::write(&path, b"not valid toml {{{{").unwrap();
    let config = GpuConfig::load(&path);
    assert!(config.is_none(), "corrupted config → treat as missing");
}

#[test]
fn test_state_no_config_triggers_calibration() {
    let (_dir, path) = temp_config_path();
    let state = GpuConfigState::resolve(None, Some("NVIDIA RTX 3090"), &path);
    assert_eq!(state, GpuConfigState::NeedsCalibration);
}

#[test]
fn test_state_same_device_enabled_returns_use_thresholds() {
    let (_dir, path) = temp_config_path();
    let cfg = GpuConfig {
        device: "NVIDIA RTX 3090".to_string(),
        calibrated: "2026-04-09".to_string(),
        threshold_single_mb: 48,
        threshold_multi_mb: 3,
        gpu_enabled: true,
    };
    cfg.save(&path).unwrap();
    let state = GpuConfigState::resolve(GpuConfig::load(&path), Some("NVIDIA RTX 3090"), &path);
    assert_eq!(
        state,
        GpuConfigState::UseThresholds {
            single_mb: 48,
            multi_mb: 3
        }
    );
}

#[test]
fn test_state_same_device_disabled_returns_skip() {
    let (_dir, path) = temp_config_path();
    let cfg = GpuConfig {
        device: "Intel UHD 630".to_string(),
        calibrated: "2026-04-09".to_string(),
        threshold_single_mb: 999,
        threshold_multi_mb: 999,
        gpu_enabled: false,
    };
    cfg.save(&path).unwrap();
    let state = GpuConfigState::resolve(GpuConfig::load(&path), Some("Intel UHD 630"), &path);
    assert_eq!(state, GpuConfigState::Skip);
}

#[test]
fn test_state_different_device_triggers_calibration() {
    let (_dir, path) = temp_config_path();
    let cfg = GpuConfig {
        device: "Old GPU".to_string(),
        calibrated: "2026-04-09".to_string(),
        threshold_single_mb: 48,
        threshold_multi_mb: 3,
        gpu_enabled: true,
    };
    cfg.save(&path).unwrap();
    let state = GpuConfigState::resolve(GpuConfig::load(&path), Some("New GPU"), &path);
    assert_eq!(state, GpuConfigState::NeedsCalibration);
}

#[test]
fn test_state_no_gpu_adapter_returns_skip_leaves_config() {
    let (_dir, path) = temp_config_path();
    let cfg = GpuConfig {
        device: "NVIDIA RTX 3090".to_string(),
        calibrated: "2026-04-09".to_string(),
        threshold_single_mb: 48,
        threshold_multi_mb: 3,
        gpu_enabled: true,
    };
    cfg.save(&path).unwrap();
    // GPU absent (adapter = None) — config should be untouched
    let state = GpuConfigState::resolve(GpuConfig::load(&path), None, &path);
    assert_eq!(state, GpuConfigState::Skip);
    // Config still on disk, unmodified
    let reloaded = GpuConfig::load(&path).unwrap();
    assert_eq!(reloaded.device, "NVIDIA RTX 3090");
}

#[test]
fn test_no_calibrate_flag_returns_conservative_defaults() {
    let state = GpuConfigState::resolve_no_calibrate(Some("NVIDIA RTX 3090"));
    assert_eq!(
        state,
        GpuConfigState::UseThresholds {
            single_mb: blazehash::gpu::config::DEFAULT_THRESHOLD_SINGLE_MB,
            multi_mb: blazehash::gpu::config::DEFAULT_THRESHOLD_MULTI_MB,
        }
    );
}

#[test]
fn test_backend_detect_returns_option() {
    // On headless CI with no GPU, returns None gracefully.
    // On a machine with a GPU, returns Some.
    // Either way: must not panic.
    let backend = blazehash::gpu::backend::GpuBackend::detect();
    if let Some(b) = backend {
        assert!(!b.adapter_name().is_empty());
    }
}

#[test]
fn test_backend_skips_software_renderers() {
    // If detect() returns Some, the adapter must not be a known SW renderer.
    let backend = blazehash::gpu::backend::GpuBackend::detect();
    if let Some(b) = backend {
        let name = b.adapter_name().to_lowercase();
        assert!(!name.contains("warp"), "WARP is a software renderer");
        assert!(
            !name.contains("llvmpipe"),
            "llvmpipe is a software renderer"
        );
        assert!(
            !name.contains("software"),
            "software renderer must be skipped"
        );
    }
}

#[test]
fn test_gpu_sha256_empty_input() {
    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        eprintln!("No GPU — skipping GPU sha256 test");
        return;
    };
    let hasher = blazehash::gpu::sha256::GpuSha256::new(&backend);
    let result = hasher.hash(b"");
    assert_eq!(
        result,
        "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    );
}

#[test]
fn test_gpu_sha256_abc() {
    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        return;
    };
    let hasher = blazehash::gpu::sha256::GpuSha256::new(&backend);
    let result = hasher.hash(b"abc");
    assert_eq!(
        result,
        "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
    );
}

#[test]
fn test_gpu_sha256_matches_cpu_for_various_sizes() {
    use sha2::{Digest, Sha256};

    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        return;
    };
    let hasher = blazehash::gpu::sha256::GpuSha256::new(&backend);

    for size in [0usize, 1, 55, 56, 63, 64, 128, 1023, 4096] {
        let data: Vec<u8> = (0..size).map(|i| (i % 251) as u8).collect();
        let gpu_result = hasher.hash(&data);
        let cpu_result = hex::encode(Sha256::digest(&data));
        assert_eq!(gpu_result, cpu_result, "mismatch at size={size}");
    }
}

#[test]
fn test_gpu_sha256_large_file_matches_cpu() {
    use sha2::{Digest, Sha256};

    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        return;
    };
    let hasher = blazehash::gpu::sha256::GpuSha256::new(&backend);

    let data = vec![0x42u8; 1024 * 1024]; // 1 MiB
    let gpu_result = hasher.hash(&data);
    let cpu_result = hex::encode(Sha256::digest(&data));
    assert_eq!(gpu_result, cpu_result);
}

#[test]
fn test_gpu_md5_empty_input() {
    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        eprintln!("No GPU — skipping GPU md5 test");
        return;
    };
    let hasher = blazehash::gpu::md5::GpuMd5::new(&backend);
    let result = hasher.hash(b"");
    // MD5("") = d41d8cd98f00b204e9800998ecf8427e
    assert_eq!(result, "d41d8cd98f00b204e9800998ecf8427e");
}

#[test]
fn test_gpu_md5_abc() {
    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        return;
    };
    let hasher = blazehash::gpu::md5::GpuMd5::new(&backend);
    let result = hasher.hash(b"abc");
    // MD5("abc") = 900150983cd24fb0d6963f7d28e17f72
    assert_eq!(result, "900150983cd24fb0d6963f7d28e17f72");
}

#[test]
fn test_gpu_md5_matches_cpu_for_various_sizes() {
    use md5::{Digest, Md5};

    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        return;
    };
    let hasher = blazehash::gpu::md5::GpuMd5::new(&backend);

    for size in [0usize, 1, 55, 56, 63, 64, 128, 1023, 4096] {
        let data: Vec<u8> = (0..size).map(|i| (i % 251) as u8).collect();
        let gpu_result = hasher.hash(&data);
        let cpu_result = hex::encode(Md5::digest(&data));
        assert_eq!(gpu_result, cpu_result, "mismatch at size={size}");
    }
}

#[test]
fn test_gpu_md5_large_file_matches_cpu() {
    use md5::{Digest, Md5};

    let Some(backend) = blazehash::gpu::backend::GpuBackend::detect() else {
        return;
    };
    let hasher = blazehash::gpu::md5::GpuMd5::new(&backend);

    let data = vec![0x42u8; 1024 * 1024]; // 1 MiB
    let gpu_result = hasher.hash(&data);
    let cpu_result = hex::encode(Md5::digest(&data));
    assert_eq!(gpu_result, cpu_result);
}

// ---- Task 13: GPU threshold decision function ----

use blazehash::algorithm::Algorithm;
use blazehash::gpu::threshold::{should_use_gpu, GPU_ALGOS};

#[test]
fn test_gpu_algos_excludes_blake3() {
    assert!(
        !GPU_ALGOS.contains(&Algorithm::Blake3),
        "BLAKE3 must not be in GPU_ALGOS"
    );
    assert!(
        GPU_ALGOS.contains(&Algorithm::Sha256),
        "SHA-256 must be in GPU_ALGOS"
    );
    assert!(
        GPU_ALGOS.contains(&Algorithm::Md5),
        "MD5 must be in GPU_ALGOS"
    );
}

#[test]
fn test_should_use_gpu_skip_state_always_false() {
    let state = GpuConfigState::Skip;
    assert!(!should_use_gpu(100, &[Algorithm::Sha256], &state));
    assert!(!should_use_gpu(100, &[Algorithm::Md5], &state));
}

#[test]
fn test_should_use_gpu_needs_calibration_always_false() {
    let state = GpuConfigState::NeedsCalibration;
    assert!(!should_use_gpu(100, &[Algorithm::Sha256], &state));
}

#[test]
fn test_should_use_gpu_blake3_only_always_false() {
    let state = GpuConfigState::UseThresholds {
        single_mb: 10,
        multi_mb: 3,
    };
    // BLAKE3 is not GPU-eligible — never use GPU even if file is huge
    assert!(!should_use_gpu(1000, &[Algorithm::Blake3], &state));
}

#[test]
fn test_should_use_gpu_single_algo_below_threshold() {
    let state = GpuConfigState::UseThresholds {
        single_mb: 48,
        multi_mb: 3,
    };
    // 10 MB < 48 MB threshold — use CPU
    assert!(!should_use_gpu(10, &[Algorithm::Sha256], &state));
}

#[test]
fn test_should_use_gpu_single_algo_above_threshold() {
    let state = GpuConfigState::UseThresholds {
        single_mb: 48,
        multi_mb: 3,
    };
    // 100 MB > 48 MB threshold — use GPU
    assert!(should_use_gpu(100, &[Algorithm::Sha256], &state));
}

#[test]
fn test_should_use_gpu_multi_algo_below_threshold() {
    let state = GpuConfigState::UseThresholds {
        single_mb: 48,
        multi_mb: 3,
    };
    // 1 MB < 3 MB multi threshold with 2 GPU-eligible algos — use CPU
    assert!(!should_use_gpu(
        1,
        &[Algorithm::Sha256, Algorithm::Md5],
        &state
    ));
}

#[test]
fn test_should_use_gpu_multi_algo_above_threshold() {
    let state = GpuConfigState::UseThresholds {
        single_mb: 48,
        multi_mb: 3,
    };
    // 5 MB > 3 MB multi threshold with 2 GPU-eligible algos — use GPU
    assert!(should_use_gpu(
        5,
        &[Algorithm::Sha256, Algorithm::Md5],
        &state
    ));
}

#[test]
fn test_should_use_gpu_mixed_algos_counts_only_gpu_eligible() {
    let state = GpuConfigState::UseThresholds {
        single_mb: 48,
        multi_mb: 3,
    };
    // Blake3 + Sha256 + Md5 — only 2 are GPU-eligible (Sha256, Md5)
    // 2 eligible algos → multi path: 5 MB > 3 MB → GPU
    assert!(should_use_gpu(
        5,
        &[Algorithm::Blake3, Algorithm::Sha256, Algorithm::Md5],
        &state
    ));
}

// ---- Task 14: GPU integration in hash_file ----

#[test]
fn test_gpu_hash_file_sha256_matches_cpu() {
    use blazehash::algorithm::Algorithm;
    use blazehash::hash::hash_file;
    use std::io::Write;
    use tempfile::NamedTempFile;

    // Only run this test if a GPU is available
    if blazehash::gpu::backend::GpuBackend::detect().is_none() {
        eprintln!("No GPU — skipping hash_file GPU integration test");
        return;
    }

    let mut f = NamedTempFile::new().unwrap();
    // 1 MiB file — verify correctness regardless of which path is taken.
    f.write_all(&vec![0x77u8; 1024 * 1024]).unwrap();
    f.flush().unwrap();

    let result1 = hash_file(f.path(), &[Algorithm::Sha256], false, false).unwrap();
    // Call again — must produce identical result (GPU or CPU, both correct)
    let result2 = hash_file(f.path(), &[Algorithm::Sha256], false, false).unwrap();
    assert_eq!(
        result1.hashes[&Algorithm::Sha256],
        result2.hashes[&Algorithm::Sha256],
        "hash_file must be deterministic"
    );
    assert_eq!(result1.hashes[&Algorithm::Sha256].len(), 64);
}