ipfrs-storage 0.2.0

Storage backends and block management for IPFRS content-addressed system
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Storage block integrity verifier using FNV-1a checksums.
//!
//! Tracks verification results and produces detailed reports for
//! auditing and self-healing workflows.

use std::collections::HashMap;

// ---------------------------------------------------------------------------
// FNV-1a checksum (64-bit)
// ---------------------------------------------------------------------------

#[inline]
fn fnv1a_checksum(data: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for &byte in data {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

// ---------------------------------------------------------------------------
// VerificationResult
// ---------------------------------------------------------------------------

/// The outcome of verifying a single storage block.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum VerificationResult {
    /// Checksum matches — block is intact.
    Ok,
    /// Checksum mismatch — block content has changed since registration.
    Corrupted {
        /// The FNV-1a checksum recorded at write time.
        expected: u64,
        /// The FNV-1a checksum computed from the current content.
        actual: u64,
    },
    /// Block was not found in the registry, or content was absent.
    Missing,
}

// ---------------------------------------------------------------------------
// BlockRecord
// ---------------------------------------------------------------------------

/// Metadata and last verification state for a single registered block.
#[derive(Clone, Debug)]
pub struct BlockRecord {
    /// Unique block identifier assigned by the verifier.
    pub block_id: u64,
    /// Content Identifier (CID) string for this block.
    pub cid: String,
    /// FNV-1a checksum of the block content at registration time.
    pub expected_checksum: u64,
    /// Size of the block content in bytes.
    pub size_bytes: u64,
    /// Logical tick at which the last verification was performed, if any.
    pub verified_at_tick: Option<u64>,
    /// Result of the most recent verification, if any.
    pub last_result: Option<VerificationResult>,
}

// ---------------------------------------------------------------------------
// VerificationReport
// ---------------------------------------------------------------------------

/// Summary of a batch verification run.
#[derive(Clone, Debug)]
pub struct VerificationReport {
    /// Total number of blocks checked in this batch.
    pub total_checked: usize,
    /// Number of blocks whose checksums matched.
    pub ok_count: usize,
    /// Number of blocks with checksum mismatches.
    pub corrupted_count: usize,
    /// Number of missing blocks (not registered or content absent).
    pub missing_count: usize,
    /// Block IDs of corrupted blocks, sorted ascending.
    pub corrupted_ids: Vec<u64>,
    /// Block IDs of missing blocks, sorted ascending.
    pub missing_ids: Vec<u64>,
}

impl VerificationReport {
    /// Returns `true` when no corruption or missing blocks were found.
    #[inline]
    pub fn is_clean(&self) -> bool {
        self.corrupted_count == 0 && self.missing_count == 0
    }
}

// ---------------------------------------------------------------------------
// VerifierStats
// ---------------------------------------------------------------------------

/// Cumulative statistics across all verification operations.
#[derive(Clone, Debug, Default)]
pub struct VerifierStats {
    /// Total number of distinct blocks currently registered.
    pub total_blocks_registered: usize,
    /// Total number of individual block verifications performed.
    pub total_verifications_run: u64,
    /// Total verifications that returned `Ok`.
    pub total_ok: u64,
    /// Total verifications that returned `Corrupted`.
    pub total_corrupted: u64,
    /// Total verifications that returned `Missing`.
    pub total_missing: u64,
}

// ---------------------------------------------------------------------------
// StorageBlockVerifier
// ---------------------------------------------------------------------------

/// Verifies storage block integrity using FNV-1a checksums.
///
/// Tracks per-block verification history and accumulates lifetime statistics
/// suitable for auditing and self-healing pipelines.
pub struct StorageBlockVerifier {
    records: HashMap<u64, BlockRecord>,
    next_block_id: u64,
    stats: VerifierStats,
}

impl StorageBlockVerifier {
    /// Creates a new, empty verifier.
    pub fn new() -> Self {
        Self {
            records: HashMap::new(),
            next_block_id: 0,
            stats: VerifierStats::default(),
        }
    }

    /// Registers a block, computing its FNV-1a checksum from `content`.
    ///
    /// Returns the newly assigned `block_id`.
    pub fn register(&mut self, cid: String, content: &[u8]) -> u64 {
        let block_id = self.next_block_id;
        self.next_block_id += 1;

        let expected_checksum = fnv1a_checksum(content);
        let record = BlockRecord {
            block_id,
            cid,
            expected_checksum,
            size_bytes: content.len() as u64,
            verified_at_tick: None,
            last_result: None,
        };

        self.records.insert(block_id, record);
        self.stats.total_blocks_registered += 1;

        block_id
    }

    /// Verifies a single block against its registered checksum.
    ///
    /// - If `block_id` is unknown → `Missing` (stats updated, **no** record mutation).
    /// - If `content` is `None` → `Missing` (record updated).
    /// - Otherwise computes the FNV-1a checksum and returns `Ok` or `Corrupted`.
    ///
    /// Always increments `stats.total_verifications_run`.
    pub fn verify_block(
        &mut self,
        block_id: u64,
        content: Option<&[u8]>,
        current_tick: u64,
    ) -> VerificationResult {
        // Block not registered at all.
        if !self.records.contains_key(&block_id) {
            self.stats.total_missing += 1;
            self.stats.total_verifications_run += 1;
            return VerificationResult::Missing;
        }

        self.stats.total_verifications_run += 1;

        let content_bytes = match content {
            None => {
                self.stats.total_missing += 1;
                let record = self
                    .records
                    .get_mut(&block_id)
                    .expect("key existence checked above");
                record.verified_at_tick = Some(current_tick);
                record.last_result = Some(VerificationResult::Missing);
                return VerificationResult::Missing;
            }
            Some(bytes) => bytes,
        };

        let record = self
            .records
            .get_mut(&block_id)
            .expect("key existence checked above");

        let actual = fnv1a_checksum(content_bytes);
        let result = if actual == record.expected_checksum {
            self.stats.total_ok += 1;
            VerificationResult::Ok
        } else {
            self.stats.total_corrupted += 1;
            VerificationResult::Corrupted {
                expected: record.expected_checksum,
                actual,
            }
        };

        record.verified_at_tick = Some(current_tick);
        record.last_result = Some(result.clone());

        result
    }

    /// Verifies a batch of `(block_id, content)` pairs and returns a summary report.
    ///
    /// `corrupted_ids` and `missing_ids` in the returned report are sorted ascending.
    pub fn verify_batch(
        &mut self,
        batch: Vec<(u64, Option<Vec<u8>>)>,
        current_tick: u64,
    ) -> VerificationReport {
        let mut ok_count = 0usize;
        let mut corrupted_count = 0usize;
        let mut missing_count = 0usize;
        let mut corrupted_ids: Vec<u64> = Vec::new();
        let mut missing_ids: Vec<u64> = Vec::new();

        let total_checked = batch.len();

        for (block_id, content) in batch {
            let result = self.verify_block(block_id, content.as_deref(), current_tick);
            match result {
                VerificationResult::Ok => ok_count += 1,
                VerificationResult::Corrupted { .. } => {
                    corrupted_count += 1;
                    corrupted_ids.push(block_id);
                }
                VerificationResult::Missing => {
                    missing_count += 1;
                    missing_ids.push(block_id);
                }
            }
        }

        corrupted_ids.sort_unstable();
        missing_ids.sort_unstable();

        VerificationReport {
            total_checked,
            ok_count,
            corrupted_count,
            missing_count,
            corrupted_ids,
            missing_ids,
        }
    }

    /// Returns a reference to the record for `block_id`, if it exists.
    pub fn get_record(&self, block_id: u64) -> Option<&BlockRecord> {
        self.records.get(&block_id)
    }

    /// Returns a reference to the cumulative statistics.
    pub fn stats(&self) -> &VerifierStats {
        &self.stats
    }

    /// Returns block IDs that have never been verified, sorted ascending.
    pub fn unverified_blocks(&self) -> Vec<u64> {
        let mut ids: Vec<u64> = self
            .records
            .values()
            .filter(|r| r.verified_at_tick.is_none())
            .map(|r| r.block_id)
            .collect();
        ids.sort_unstable();
        ids
    }

    /// Returns block IDs whose most recent verification was `Corrupted`, sorted ascending.
    pub fn corrupted_blocks(&self) -> Vec<u64> {
        let mut ids: Vec<u64> = self
            .records
            .values()
            .filter(|r| matches!(&r.last_result, Some(VerificationResult::Corrupted { .. })))
            .map(|r| r.block_id)
            .collect();
        ids.sort_unstable();
        ids
    }
}

impl Default for StorageBlockVerifier {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // Helper
    // -----------------------------------------------------------------------

    fn make_verifier() -> StorageBlockVerifier {
        StorageBlockVerifier::new()
    }

    fn expected_checksum(data: &[u8]) -> u64 {
        fnv1a_checksum(data)
    }

    // -----------------------------------------------------------------------
    // register
    // -----------------------------------------------------------------------

    #[test]
    fn test_register_computes_correct_checksum() {
        let mut v = make_verifier();
        let content = b"hello world";
        let id = v.register("cid-1".into(), content);
        let record = v.get_record(id).expect("record must exist");
        assert_eq!(record.expected_checksum, expected_checksum(content));
    }

    #[test]
    fn test_register_stores_size() {
        let mut v = make_verifier();
        let content = b"abcde";
        let id = v.register("cid-size".into(), content);
        let record = v.get_record(id).expect("record must exist");
        assert_eq!(record.size_bytes, 5);
    }

    #[test]
    fn test_register_stores_cid() {
        let mut v = make_verifier();
        let id = v.register("my-cid".into(), b"data");
        let record = v.get_record(id).expect("record must exist");
        assert_eq!(record.cid, "my-cid");
    }

    #[test]
    fn test_register_increments_block_id() {
        let mut v = make_verifier();
        let id0 = v.register("cid-0".into(), b"a");
        let id1 = v.register("cid-1".into(), b"b");
        let id2 = v.register("cid-2".into(), b"c");
        assert_eq!(id0, 0);
        assert_eq!(id1, 1);
        assert_eq!(id2, 2);
    }

    #[test]
    fn test_register_increments_stats() {
        let mut v = make_verifier();
        v.register("a".into(), b"x");
        v.register("b".into(), b"y");
        assert_eq!(v.stats().total_blocks_registered, 2);
    }

    #[test]
    fn test_register_no_verified_at_tick() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        let record = v.get_record(id).expect("record must exist");
        assert!(record.verified_at_tick.is_none());
        assert!(record.last_result.is_none());
    }

    // -----------------------------------------------------------------------
    // verify_block — Ok
    // -----------------------------------------------------------------------

    #[test]
    fn test_verify_block_ok_when_content_matches() {
        let mut v = make_verifier();
        let content = b"consistent data";
        let id = v.register("cid".into(), content);
        let result = v.verify_block(id, Some(content), 1);
        assert_eq!(result, VerificationResult::Ok);
    }

    #[test]
    fn test_verify_block_ok_updates_record() {
        let mut v = make_verifier();
        let content = b"data";
        let id = v.register("cid".into(), content);
        v.verify_block(id, Some(content), 42);
        let record = v.get_record(id).expect("record must exist");
        assert_eq!(record.verified_at_tick, Some(42));
        assert_eq!(record.last_result, Some(VerificationResult::Ok));
    }

    // -----------------------------------------------------------------------
    // verify_block — Corrupted
    // -----------------------------------------------------------------------

    #[test]
    fn test_verify_block_corrupted_when_content_changed() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"original");
        let result = v.verify_block(id, Some(b"modified"), 1);
        assert!(matches!(result, VerificationResult::Corrupted { .. }));
    }

    #[test]
    fn test_verify_block_corrupted_expected_and_actual() {
        let mut v = make_verifier();
        let original = b"original";
        let modified = b"modified!!";
        let id = v.register("cid".into(), original);
        let expected_cs = expected_checksum(original);
        let actual_cs = expected_checksum(modified);
        let result = v.verify_block(id, Some(modified), 1);
        assert_eq!(
            result,
            VerificationResult::Corrupted {
                expected: expected_cs,
                actual: actual_cs,
            }
        );
    }

    #[test]
    fn test_verify_block_corrupted_updates_record() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"a");
        v.verify_block(id, Some(b"b"), 7);
        let record = v.get_record(id).expect("record must exist");
        assert_eq!(record.verified_at_tick, Some(7));
        assert!(matches!(
            record.last_result,
            Some(VerificationResult::Corrupted { .. })
        ));
    }

    // -----------------------------------------------------------------------
    // verify_block — Missing
    // -----------------------------------------------------------------------

    #[test]
    fn test_verify_block_missing_when_not_registered() {
        let mut v = make_verifier();
        let result = v.verify_block(999, Some(b"some data"), 1);
        assert_eq!(result, VerificationResult::Missing);
    }

    #[test]
    fn test_verify_block_missing_when_content_none() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        let result = v.verify_block(id, None, 5);
        assert_eq!(result, VerificationResult::Missing);
    }

    #[test]
    fn test_verify_block_none_updates_record() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        v.verify_block(id, None, 10);
        let record = v.get_record(id).expect("record must exist");
        assert_eq!(record.verified_at_tick, Some(10));
        assert_eq!(record.last_result, Some(VerificationResult::Missing));
    }

    // -----------------------------------------------------------------------
    // stats accumulation
    // -----------------------------------------------------------------------

    #[test]
    fn test_stats_total_verifications_run() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        v.verify_block(id, Some(b"data"), 1);
        v.verify_block(id, Some(b"wrong"), 2);
        v.verify_block(id, None, 3);
        v.verify_block(9999, Some(b"x"), 4); // missing id
        assert_eq!(v.stats().total_verifications_run, 4);
    }

    #[test]
    fn test_stats_total_ok() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        v.verify_block(id, Some(b"data"), 1);
        v.verify_block(id, Some(b"data"), 2);
        assert_eq!(v.stats().total_ok, 2);
    }

    #[test]
    fn test_stats_total_corrupted() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        v.verify_block(id, Some(b"bad"), 1);
        v.verify_block(id, Some(b"worse"), 2);
        assert_eq!(v.stats().total_corrupted, 2);
    }

    #[test]
    fn test_stats_total_missing() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        v.verify_block(id, None, 1); // missing (None)
        v.verify_block(9999, Some(b"x"), 2); // missing (unknown id)
        assert_eq!(v.stats().total_missing, 2);
    }

    #[test]
    fn test_stats_accumulate_across_calls() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"hello");
        v.verify_block(id, Some(b"hello"), 1); // ok
        v.verify_block(id, Some(b"world"), 2); // corrupted
        v.verify_block(id, None, 3); // missing
        let s = v.stats();
        assert_eq!(s.total_ok, 1);
        assert_eq!(s.total_corrupted, 1);
        assert_eq!(s.total_missing, 1);
        assert_eq!(s.total_verifications_run, 3);
    }

    // -----------------------------------------------------------------------
    // verify_batch
    // -----------------------------------------------------------------------

    #[test]
    fn test_verify_batch_report_counts() {
        let mut v = make_verifier();
        let id_ok = v.register("ok".into(), b"good");
        let id_bad = v.register("bad".into(), b"original");
        let id_reg = v.register("none".into(), b"data");

        let batch = vec![
            (id_ok, Some(b"good".to_vec())),
            (id_bad, Some(b"corrupted".to_vec())),
            (id_reg, None),
            (9999, Some(b"x".to_vec())), // unregistered → missing
        ];

        let report = v.verify_batch(batch, 1);
        assert_eq!(report.total_checked, 4);
        assert_eq!(report.ok_count, 1);
        assert_eq!(report.corrupted_count, 1);
        assert_eq!(report.missing_count, 2);
    }

    #[test]
    fn test_verify_batch_sorted_ids() {
        let mut v = make_verifier();
        // Register three blocks; ids will be 0, 1, 2.
        let id0 = v.register("c0".into(), b"a");
        let id1 = v.register("c1".into(), b"b");
        let id2 = v.register("c2".into(), b"c");

        // Submit in reverse order with corrupted/missing content.
        let batch = vec![
            (id2, Some(b"z".to_vec())), // corrupted
            (id0, Some(b"z".to_vec())), // corrupted
            (id1, None),                // missing
        ];
        let report = v.verify_batch(batch, 1);
        assert_eq!(report.corrupted_ids, vec![id0, id2]);
        assert_eq!(report.missing_ids, vec![id1]);
    }

    // -----------------------------------------------------------------------
    // is_clean
    // -----------------------------------------------------------------------

    #[test]
    fn test_report_is_clean_true() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        let report = v.verify_batch(vec![(id, Some(b"data".to_vec()))], 1);
        assert!(report.is_clean());
    }

    #[test]
    fn test_report_is_clean_false_when_corrupted() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        let report = v.verify_batch(vec![(id, Some(b"wrong".to_vec()))], 1);
        assert!(!report.is_clean());
    }

    #[test]
    fn test_report_is_clean_false_when_missing() {
        let mut v = make_verifier();
        let id = v.register("cid".into(), b"data");
        let report = v.verify_batch(vec![(id, None)], 1);
        assert!(!report.is_clean());
    }

    // -----------------------------------------------------------------------
    // unverified_blocks
    // -----------------------------------------------------------------------

    #[test]
    fn test_unverified_blocks_returns_all_initially() {
        let mut v = make_verifier();
        v.register("a".into(), b"1");
        v.register("b".into(), b"2");
        v.register("c".into(), b"3");
        let unverified = v.unverified_blocks();
        assert_eq!(unverified, vec![0, 1, 2]);
    }

    #[test]
    fn test_unverified_blocks_excludes_verified() {
        let mut v = make_verifier();
        let id0 = v.register("a".into(), b"1");
        v.register("b".into(), b"2");
        v.verify_block(id0, Some(b"1"), 1);
        let unverified = v.unverified_blocks();
        assert_eq!(unverified, vec![1]);
    }

    #[test]
    fn test_unverified_blocks_empty_when_all_verified() {
        let mut v = make_verifier();
        let id0 = v.register("a".into(), b"x");
        let id1 = v.register("b".into(), b"y");
        v.verify_block(id0, Some(b"x"), 1);
        v.verify_block(id1, Some(b"y"), 1);
        assert!(v.unverified_blocks().is_empty());
    }

    // -----------------------------------------------------------------------
    // corrupted_blocks
    // -----------------------------------------------------------------------

    #[test]
    fn test_corrupted_blocks_returns_only_corrupted() {
        let mut v = make_verifier();
        let id0 = v.register("a".into(), b"original");
        let id1 = v.register("b".into(), b"good");
        let id2 = v.register("c".into(), b"also original");

        v.verify_block(id0, Some(b"changed"), 1); // corrupted
        v.verify_block(id1, Some(b"good"), 1); // ok
        v.verify_block(id2, Some(b"also changed"), 1); // corrupted

        let corrupted = v.corrupted_blocks();
        assert_eq!(corrupted, vec![id0, id2]);
    }

    #[test]
    fn test_corrupted_blocks_empty_initially() {
        let mut v = make_verifier();
        v.register("a".into(), b"x");
        assert!(v.corrupted_blocks().is_empty());
    }

    #[test]
    fn test_corrupted_blocks_not_including_missing() {
        let mut v = make_verifier();
        let id = v.register("a".into(), b"data");
        v.verify_block(id, None, 1); // missing, not corrupted
        assert!(v.corrupted_blocks().is_empty());
    }

    // -----------------------------------------------------------------------
    // batch with mixed results
    // -----------------------------------------------------------------------

    #[test]
    fn test_batch_mixed_results_all_categories() {
        let mut v = make_verifier();
        let id_ok = v.register("ok".into(), b"abc");
        let id_bad = v.register("bad".into(), b"xyz");
        let id_none = v.register("none".into(), b"zzz");

        let batch = vec![
            (id_ok, Some(b"abc".to_vec())),
            (id_bad, Some(b"XYZ-TAMPERED".to_vec())),
            (id_none, None),
            (42, Some(b"phantom".to_vec())), // unknown block id
        ];

        let report = v.verify_batch(batch, 100);
        assert_eq!(report.total_checked, 4);
        assert_eq!(report.ok_count, 1);
        assert_eq!(report.corrupted_count, 1);
        assert_eq!(report.missing_count, 2);
        assert!(!report.is_clean());

        // Stats should reflect totals
        let s = v.stats();
        assert_eq!(s.total_ok, 1);
        assert_eq!(s.total_corrupted, 1);
        assert_eq!(s.total_missing, 2);
        assert_eq!(s.total_verifications_run, 4);
    }

    #[test]
    fn test_empty_batch_produces_clean_report() {
        let mut v = make_verifier();
        let report = v.verify_batch(vec![], 1);
        assert_eq!(report.total_checked, 0);
        assert!(report.is_clean());
    }

    #[test]
    fn test_fnv1a_deterministic() {
        let data = b"determinism test";
        let a = fnv1a_checksum(data);
        let b = fnv1a_checksum(data);
        assert_eq!(a, b);
    }

    #[test]
    fn test_fnv1a_different_inputs_differ() {
        let a = fnv1a_checksum(b"alpha");
        let b = fnv1a_checksum(b"beta");
        assert_ne!(a, b);
    }

    #[test]
    fn test_get_record_returns_none_for_unknown() {
        let v = make_verifier();
        assert!(v.get_record(0).is_none());
    }
}