libfreemkv 0.31.10

Open source raw disc access library for optical drives
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//! Disc sector verification — read every sector and classify health.

use crate::disc::{Chapter, DiscTitle};
use crate::progress::{PassProgress, Progress};
use crate::sector::SectorSource;
use std::time::{Duration, Instant};

/// Pause before the single retry attempt on a failed sector, letting a
/// drive that briefly went NOT READY spin back up. Slept in
/// [`RETRY_POLL_INTERVAL`] increments so a cancel request (the progress
/// callback returning `false`) is observed within that interval rather
/// than after the full pause.
const RETRY_PAUSE: Duration = Duration::from_secs(2);

/// Polling cadence while sleeping out [`RETRY_PAUSE`].
const RETRY_POLL_INTERVAL: Duration = Duration::from_millis(250);

/// Sleep up to `total`, returning early with `false` if `cancelled`
/// observes a stop request mid-pause. Returns `true` if the full pause
/// elapsed without cancellation.
fn cancellable_pause(total: Duration, cancelled: &mut dyn FnMut() -> bool) -> bool {
    let deadline = Instant::now() + total;
    loop {
        if cancelled() {
            return false;
        }
        let now = Instant::now();
        if now >= deadline {
            return true;
        }
        std::thread::sleep(RETRY_POLL_INTERVAL.min(deadline - now));
    }
}

/// Health status of a single sector read.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SectorStatus {
    /// Read succeeded on first attempt, under 500ms
    Good,
    /// Read succeeded but took longer than expected (>500ms per sector)
    Slow,
    /// Read failed initially but succeeded on retry
    Recovered,
    /// Read failed all attempts — unrecoverable
    Bad,
}

/// A contiguous range of bad or slow sectors.
#[derive(Debug, Clone)]
pub struct SectorRange {
    pub start_lba: u32,
    pub count: u32,
    pub status: SectorStatus,
    /// Byte offset from start of title
    pub byte_offset: u64,
}

/// Result of a disc verification scan.
#[derive(Debug, Clone)]
pub struct VerifyResult {
    pub total_sectors: u64,
    pub good: u64,
    pub slow: u64,
    pub recovered: u64,
    pub bad: u64,
    /// Contiguous ranges of non-good sectors
    pub ranges: Vec<SectorRange>,
    pub elapsed_secs: f64,
}

impl VerifyResult {
    /// Percentage of sectors that are fully readable (good + slow + recovered).
    ///
    /// Returns `100.0` when there are no sectors. The subtraction is
    /// saturating so a caller-constructed `VerifyResult` with `bad` greater
    /// than `total_sectors` cannot wrap in release builds (it pins at 0%).
    pub fn readable_pct(&self) -> f64 {
        if self.total_sectors == 0 {
            return 100.0;
        }
        self.total_sectors.saturating_sub(self.bad) as f64 / self.total_sectors as f64 * 100.0
    }

    /// True if every sector read first-attempt clean — no bad, no
    /// recovered-on-retry, and no slow sectors. A `Recovered` or `Slow`
    /// sector means data was eventually returned but the surface is
    /// degraded, so `is_perfect()` is intentionally stricter than
    /// "fully readable" (which [`readable_pct`](Self::readable_pct)
    /// reports by counting recovered + slow as readable).
    pub fn is_perfect(&self) -> bool {
        self.bad == 0 && self.recovered == 0 && self.slow == 0
    }

    /// Map a bad sector range to a chapter timestamp.
    pub fn chapter_at_offset(
        chapters: &[Chapter],
        byte_offset: u64,
        duration_secs: f64,
        total_bytes: u64,
    ) -> Option<(usize, f64)> {
        if total_bytes == 0 || chapters.is_empty() {
            return None;
        }
        let time_secs = byte_offset as f64 / total_bytes as f64 * duration_secs;
        let mut chapter_idx = 0;
        for (i, ch) in chapters.iter().enumerate() {
            if ch.time_secs <= time_secs {
                chapter_idx = i;
            } else {
                break;
            }
        }
        Some((chapter_idx + 1, time_secs))
    }
}

/// Verify all sectors in a title's extents.
///
/// Reads in batches for speed, falling back to single-sector reads on a
/// batch failure. Each sector that fails its single read gets one retry
/// after a [`RETRY_PAUSE`] cool-down (cancellable — see below).
///
/// `batch_sectors` is the read granularity in 2048-byte sectors; a value
/// of `0` is treated as `1` (a `0` batch would make no forward progress).
///
/// `on_progress` is both the progress sink and the cancellation channel:
/// returning `false` from [`Progress::report`] requests an early stop. The
/// stop is honoured between sectors *and* mid-retry-pause, so a verify of a
/// disc with many bad sectors stays responsive instead of blocking the full
/// pause per sector. Timing classification uses a 500 ms-per-sector
/// threshold to mark a successful-but-slow read as [`SectorStatus::Slow`].
pub fn verify_title(
    reader: &mut dyn SectorSource,
    title: &DiscTitle,
    batch_sectors: u16,
    on_progress: Option<&dyn Progress>,
) -> VerifyResult {
    let start = Instant::now();
    let mut good: u64 = 0;
    let mut slow: u64 = 0;
    let mut recovered: u64 = 0;
    let mut bad: u64 = 0;
    let mut ranges: Vec<SectorRange> = Vec::new();
    let mut sectors_done: u64 = 0;
    let mut byte_offset: u64 = 0;

    // A zero batch size would never advance `offset` -> infinite loop.
    // Clamp to at least one sector per read.
    let batch_sectors = batch_sectors.max(1);

    let total_sectors: u64 = title.extents.iter().map(|e| e.sector_count as u64).sum();
    let mut buf = vec![0u8; batch_sectors as usize * 2048];

    'outer: for ext in &title.extents {
        let mut offset: u32 = 0;
        while offset < ext.sector_count {
            let remaining = ext.sector_count - offset;
            let count = remaining.min(batch_sectors as u32) as u16;
            // `start_lba + offset` stays within the extent's own LBA span by
            // construction, but a crafted/corrupt extent can push the sum past
            // u32::MAX; saturate rather than wrap (release) or panic (debug).
            let lba = ext.start_lba.saturating_add(offset);
            let bytes = count as usize * 2048;

            let batch_start = Instant::now();
            let batch_ok = reader
                .read_sectors(lba, count, &mut buf[..bytes], false)
                .is_ok();
            let batch_ms = batch_start.elapsed().as_millis();

            if batch_ok {
                // Batch succeeded — classify by speed
                let status = if batch_ms > count as u128 * 500 {
                    SectorStatus::Slow
                } else {
                    SectorStatus::Good
                };

                match status {
                    SectorStatus::Good => good += count as u64,
                    SectorStatus::Slow => {
                        slow += count as u64;
                        // Merge with the previous range when contiguous and the
                        // same status, mirroring the per-sector path so a run of
                        // slow batches coalesces into one range instead of one
                        // entry per batch.
                        let merged = ranges.last_mut().is_some_and(|last| {
                            if last.status == SectorStatus::Slow
                                && last.start_lba.saturating_add(last.count) == lba
                            {
                                last.count = last.count.saturating_add(count as u32);
                                true
                            } else {
                                false
                            }
                        });
                        if !merged {
                            ranges.push(SectorRange {
                                start_lba: lba,
                                count: count as u32,
                                status: SectorStatus::Slow,
                                byte_offset,
                            });
                        }
                    }
                    _ => {}
                }

                sectors_done += count as u64;
                if let Some(cb) = on_progress {
                    let pp = crate::progress::PassProgress {
                        kind: crate::progress::PassKind::Verify,
                        work_done: sectors_done,
                        work_total: total_sectors,
                        bytes_good_total: (good + slow + recovered) * 2048,
                        bytes_unreadable_total: bad * 2048,
                        bytes_pending_total: 0,
                        bytes_total_disc: total_sectors * 2048,
                        disc_duration_secs: Some(title.duration_secs),
                        bytes_bad_in_main_title: 0,
                        main_title_duration_secs: Some(title.duration_secs),
                        main_title_size_bytes: Some(total_sectors * 2048),
                    };
                    if !cb.report(&pp) {
                        break 'outer;
                    }
                }
            } else {
                // Batch failed — test each sector individually
                for i in 0..count {
                    let sector_lba = lba.saturating_add(i as u32);
                    let sector_offset = i as usize * 2048;
                    let sector_byte_offset = byte_offset + i as u64 * 2048;

                    let s1 = Instant::now();
                    let first_ok = reader
                        .read_sectors(
                            sector_lba,
                            1,
                            &mut buf[sector_offset..sector_offset + 2048],
                            false,
                        )
                        .is_ok();
                    let s1_ms = s1.elapsed().as_millis();

                    let status = if first_ok && s1_ms <= 500 {
                        good += 1;
                        SectorStatus::Good
                    } else if first_ok {
                        slow += 1;
                        SectorStatus::Slow
                    } else {
                        // Retry once more after a brief cool-down. The pause is
                        // cancellable via the progress callback so a long run of
                        // bad sectors doesn't pin the thread for 2s each with no
                        // way to stop. If cancelled mid-pause, skip the retry and
                        // count the sector as bad, then bail out of the scan.
                        let cancelled_during_pause = !cancellable_pause(RETRY_PAUSE, &mut || {
                            on_progress.is_some_and(|cb| {
                                !cb.report(&PassProgress {
                                    kind: crate::progress::PassKind::Verify,
                                    work_done: sectors_done,
                                    work_total: total_sectors,
                                    bytes_good_total: (good + slow + recovered) * 2048,
                                    bytes_unreadable_total: bad * 2048,
                                    bytes_pending_total: 0,
                                    bytes_total_disc: total_sectors * 2048,
                                    disc_duration_secs: Some(title.duration_secs),
                                    bytes_bad_in_main_title: 0,
                                    main_title_duration_secs: Some(title.duration_secs),
                                    main_title_size_bytes: Some(total_sectors * 2048),
                                })
                            })
                        });
                        if cancelled_during_pause {
                            bad += 1;
                            break 'outer;
                        }
                        if reader
                            .read_sectors(
                                sector_lba,
                                1,
                                &mut buf[sector_offset..sector_offset + 2048],
                                false,
                            )
                            .is_ok()
                        {
                            recovered += 1;
                            SectorStatus::Recovered
                        } else {
                            bad += 1;
                            SectorStatus::Bad
                        }
                    };

                    if status != SectorStatus::Good {
                        // Merge with previous range if contiguous and same status
                        if let Some(last) = ranges.last_mut() {
                            if last.status == status
                                && last.start_lba.saturating_add(last.count) == sector_lba
                            {
                                last.count += 1;
                            } else {
                                ranges.push(SectorRange {
                                    start_lba: sector_lba,
                                    count: 1,
                                    status,
                                    byte_offset: sector_byte_offset,
                                });
                            }
                        } else {
                            ranges.push(SectorRange {
                                start_lba: sector_lba,
                                count: 1,
                                status,
                                byte_offset: sector_byte_offset,
                            });
                        }
                    }

                    sectors_done += 1;
                    if let Some(cb) = on_progress {
                        let pp = crate::progress::PassProgress {
                            kind: crate::progress::PassKind::Verify,
                            work_done: sectors_done,
                            work_total: total_sectors,
                            bytes_good_total: (good + slow + recovered) * 2048,
                            bytes_unreadable_total: bad * 2048,
                            bytes_pending_total: 0,
                            bytes_total_disc: total_sectors * 2048,
                            disc_duration_secs: Some(title.duration_secs),
                            bytes_bad_in_main_title: 0,
                            main_title_duration_secs: Some(title.duration_secs),
                            main_title_size_bytes: Some(total_sectors * 2048),
                        };
                        if !cb.report(&pp) {
                            break 'outer;
                        }
                    }
                }
            }

            offset += count as u32;
            byte_offset += bytes as u64;
        }
    }

    VerifyResult {
        total_sectors,
        good,
        slow,
        recovered,
        bad,
        ranges,
        elapsed_secs: start.elapsed().as_secs_f64(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::disc::{ContentFormat, DiscTitle, Extent};
    use crate::error::Error;

    /// Synthetic source: every read succeeds, filling the buffer with zeros.
    struct AlwaysGood;
    impl SectorSource for AlwaysGood {
        fn read_sectors(
            &mut self,
            _lba: u32,
            count: u16,
            buf: &mut [u8],
            _recovery: bool,
        ) -> Result<usize, Error> {
            let n = count as usize * 2048;
            buf[..n].fill(0);
            Ok(n)
        }
    }

    /// Synthetic source: every read fails.
    struct AlwaysBad;
    impl SectorSource for AlwaysBad {
        fn read_sectors(
            &mut self,
            _lba: u32,
            _count: u16,
            _buf: &mut [u8],
            _recovery: bool,
        ) -> Result<usize, Error> {
            Err(Error::ScsiError {
                opcode: 0x28,
                status: crate::scsi::SCSI_STATUS_CHECK_CONDITION,
                sense: None,
            })
        }
    }

    fn title_with_extent(start_lba: u32, sector_count: u32) -> DiscTitle {
        DiscTitle {
            playlist: String::new(),
            playlist_id: 0,
            duration_secs: 0.0,
            size_bytes: 0,
            clips: Vec::new(),
            streams: Vec::new(),
            chapters: Vec::new(),
            extents: vec![Extent {
                start_lba,
                sector_count,
            }],
            content_format: ContentFormat::BdTs,
            codec_privates: Vec::new(),
        }
    }

    #[test]
    fn batch_zero_does_not_hang() {
        // A zero batch size must be clamped to 1 internally; otherwise the
        // inner `while offset < sector_count` loop never advances. The
        // assertion only matters because the test returns at all.
        let title = title_with_extent(0, 4);
        let mut src = AlwaysGood;
        let r = verify_title(&mut src, &title, 0, None);
        assert_eq!(r.total_sectors, 4);
        assert_eq!(r.good, 4);
        assert!(r.is_perfect());
    }

    #[test]
    fn all_good_is_perfect() {
        let title = title_with_extent(100, 10);
        let mut src = AlwaysGood;
        let r = verify_title(&mut src, &title, 4, None);
        assert_eq!(r.good, 10);
        assert_eq!(r.bad, 0);
        assert!(r.ranges.is_empty());
        assert_eq!(r.readable_pct(), 100.0);
    }

    #[test]
    fn cancel_during_retry_pause_stops_promptly() {
        // AlwaysBad forces the single-sector retry path, which pauses before
        // its retry. The progress callback requests stop on its first call,
        // so the pause must return early and the scan must bail out rather
        // than sleeping RETRY_PAUSE * sector_count. A generous wall-clock
        // bound (well under a single full pause) proves cancellation worked.
        let title = title_with_extent(0, 8);
        let mut src = AlwaysBad;
        let cb = |_p: &PassProgress| false; // always request stop
        let started = Instant::now();
        let r = verify_title(&mut src, &title, 4, Some(&cb));
        assert!(
            started.elapsed() < RETRY_PAUSE,
            "verify did not honour cancel during retry pause"
        );
        // Cancelled mid-first-bad-sector: exactly one sector counted bad.
        assert_eq!(r.bad, 1);
    }

    #[test]
    fn readable_pct_saturates_on_inconsistent_counts() {
        let r = VerifyResult {
            total_sectors: 10,
            good: 0,
            slow: 0,
            recovered: 0,
            bad: 9999, // impossible in practice; must not wrap
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        assert_eq!(r.readable_pct(), 0.0);
    }

    #[test]
    fn high_lba_extent_does_not_overflow() {
        // An extent whose start_lba + offset would exceed u32::MAX must
        // saturate, not panic in debug builds.
        let title = title_with_extent(u32::MAX - 1, 4);
        let mut src = AlwaysGood;
        let r = verify_title(&mut src, &title, 2, None);
        assert_eq!(r.total_sectors, 4);
    }

    #[test]
    fn no_progress_callback_still_completes_on_bad_sectors() {
        // Without a callback there is no cancel signal, so each bad sector
        // takes the full RETRY_PAUSE. Keep the extent tiny (1 sector) so the
        // test stays fast while still exercising the bad+retry path.
        let title = title_with_extent(0, 1);
        let mut src = AlwaysBad;
        let r = verify_title(&mut src, &title, 1, None);
        assert_eq!(r.bad, 1);
        assert_eq!(r.ranges.len(), 1);
        assert_eq!(r.ranges[0].status, SectorStatus::Bad);
    }

    // ── New comprehensive tests ────────────────────────────────────────────────

    /// A SectorSource that fails exactly `fail_count` calls, then succeeds.
    struct FailFirst {
        remaining_fails: usize,
    }
    impl SectorSource for FailFirst {
        fn read_sectors(
            &mut self,
            _lba: u32,
            count: u16,
            buf: &mut [u8],
            _recovery: bool,
        ) -> Result<usize, Error> {
            if self.remaining_fails > 0 {
                self.remaining_fails -= 1;
                Err(Error::ScsiError {
                    opcode: 0x28,
                    status: crate::scsi::SCSI_STATUS_CHECK_CONDITION,
                    sense: None,
                })
            } else {
                let n = count as usize * 2048;
                buf[..n].fill(0);
                Ok(n)
            }
        }
    }

    fn title_with_two_extents(s1: u32, c1: u32, s2: u32, c2: u32) -> DiscTitle {
        DiscTitle {
            playlist: String::new(),
            playlist_id: 0,
            duration_secs: 0.0,
            size_bytes: 0,
            clips: Vec::new(),
            streams: Vec::new(),
            chapters: Vec::new(),
            extents: vec![
                Extent {
                    start_lba: s1,
                    sector_count: c1,
                },
                Extent {
                    start_lba: s2,
                    sector_count: c2,
                },
            ],
            content_format: ContentFormat::BdTs,
            codec_privates: Vec::new(),
        }
    }

    /// readable_pct on a fully-good result is 100.0 (not NaN or < 100).
    /// Mutation: changing the formula to `(total - bad - 1) / total` makes this fail.
    #[test]
    fn readable_pct_all_good_is_exactly_100() {
        let r = VerifyResult {
            total_sectors: 100,
            good: 100,
            slow: 0,
            recovered: 0,
            bad: 0,
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        assert_eq!(r.readable_pct(), 100.0);
    }

    /// readable_pct with zero total_sectors must return 100.0, not NaN/inf
    /// from a division by zero.
    /// Spec: the doc-comment explicitly states "Returns 100.0 when there are no sectors."
    /// Mutation: removing the `if self.total_sectors == 0` guard makes this panic or NaN.
    #[test]
    fn readable_pct_empty_disc_is_100() {
        let r = VerifyResult {
            total_sectors: 0,
            good: 0,
            slow: 0,
            recovered: 0,
            bad: 0,
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        assert_eq!(r.readable_pct(), 100.0);
    }

    /// readable_pct counts slow and recovered as readable (not bad).
    /// Spec: doc says "good + slow + recovered" are readable.
    /// Mutation: omitting `slow` or `recovered` from the numerator reduces the result.
    #[test]
    fn readable_pct_counts_slow_and_recovered_as_readable() {
        // 6 of 10 sectors are "lost" (bad); 4 are readable (slow + recovered).
        let r = VerifyResult {
            total_sectors: 10,
            good: 0,
            slow: 2,
            recovered: 2,
            bad: 6,
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        // saturating_sub(6) = 4; 4/10 = 40.0
        let got = r.readable_pct();
        assert!((got - 40.0).abs() < 1e-9, "expected 40.0, got {got}");
    }

    /// is_perfect() is false when there are slow sectors, even if bad == 0.
    /// Spec: doc says "no bad, no recovered-on-retry, and no slow sectors".
    /// Mutation: removing `self.slow == 0` from is_perfect() makes this pass when it should fail.
    #[test]
    fn is_perfect_false_when_slow_present() {
        let r = VerifyResult {
            total_sectors: 10,
            good: 9,
            slow: 1,
            recovered: 0,
            bad: 0,
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        assert!(!r.is_perfect());
    }

    /// is_perfect() is false when there are recovered sectors.
    /// Mutation: removing `self.recovered == 0` from is_perfect() makes this fail.
    #[test]
    fn is_perfect_false_when_recovered_present() {
        let r = VerifyResult {
            total_sectors: 10,
            good: 9,
            slow: 0,
            recovered: 1,
            bad: 0,
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        assert!(!r.is_perfect());
    }

    /// is_perfect() is false when there are bad sectors.
    /// Mutation: removing `self.bad == 0` from is_perfect() makes this fail.
    #[test]
    fn is_perfect_false_when_bad_present() {
        let r = VerifyResult {
            total_sectors: 5,
            good: 4,
            slow: 0,
            recovered: 0,
            bad: 1,
            ranges: Vec::new(),
            elapsed_secs: 0.0,
        };
        assert!(!r.is_perfect());
    }

    /// chapter_at_offset returns None when chapters is empty.
    /// Mutation: removing the `chapters.is_empty()` check makes this return Some.
    #[test]
    fn chapter_at_offset_empty_chapters_returns_none() {
        let result = VerifyResult::chapter_at_offset(&[], 1024, 120.0, 100_000);
        assert!(result.is_none());
    }

    /// chapter_at_offset returns None when total_bytes == 0 (division-by-zero guard).
    /// Mutation: removing the `total_bytes == 0` guard makes this panic or NaN.
    #[test]
    fn chapter_at_offset_zero_total_bytes_returns_none() {
        use crate::disc::Chapter;
        let chapters = vec![Chapter {
            time_secs: 0.0,
            name: String::new(),
        }];
        let result = VerifyResult::chapter_at_offset(&chapters, 1024, 120.0, 0);
        assert!(result.is_none());
    }

    /// chapter_at_offset maps byte offset to the correct chapter index (1-based).
    /// Spec: time_secs = byte_offset / total_bytes * duration; chapter = last chapter
    ///       whose time_secs <= computed time.
    /// Mutation: off-by-one on chapter_idx (using `i` instead of `chapter_idx`) changes result.
    #[test]
    fn chapter_at_offset_selects_correct_chapter() {
        use crate::disc::Chapter;
        // 3 chapters at 0s, 30s, 60s; disc is 120s, 120,000 bytes.
        let chapters = vec![
            Chapter {
                time_secs: 0.0,
                name: String::new(),
            },
            Chapter {
                time_secs: 30.0,
                name: String::new(),
            },
            Chapter {
                time_secs: 60.0,
                name: String::new(),
            },
        ];
        let total_bytes: u64 = 120_000;
        let duration = 120.0_f64;

        // Byte 90,000 → time 90.0s → chapter 3 (index 2, 1-based = 3).
        let (ch, t) =
            VerifyResult::chapter_at_offset(&chapters, 90_000, duration, total_bytes).unwrap();
        assert_eq!(ch, 3, "expected chapter 3, got {ch}");
        assert!((t - 90.0).abs() < 1e-6, "expected t≈90.0, got {t}");

        // Byte 0 → time 0s → chapter 1 (first chapter).
        let (ch0, _) =
            VerifyResult::chapter_at_offset(&chapters, 0, duration, total_bytes).unwrap();
        assert_eq!(ch0, 1);
    }

    /// A contiguous sequence of bad sectors in one extent must be coalesced
    /// into a single SectorRange (not one entry per sector).
    /// Mutation: removing the range-merge logic produces many small ranges.
    #[test]
    fn contiguous_bad_sectors_coalesce_into_one_range() {
        // 4-sector extent, all bad. Batch size 1 forces per-sector path.
        // With cancel-on-second-bad we'd stop early; we must let all 4 sectors
        // run to check coalescing. Use no callback so they all complete.
        // But AlwaysBad + no callback is slow (RETRY_PAUSE per sector × 4).
        // Limit to 2 sectors to keep wall-clock reasonable.
        let title = title_with_extent(100, 2);
        let mut src = AlwaysBad;
        // no callback → full 2 × RETRY_PAUSE, but only 2 sectors
        let r = verify_title(&mut src, &title, 1, None);
        // Both sectors are bad and contiguous; they must merge into one range.
        assert_eq!(r.bad, 2);
        assert_eq!(
            r.ranges.len(),
            1,
            "contiguous bad sectors must coalesce: got {} ranges",
            r.ranges.len()
        );
        assert_eq!(r.ranges[0].start_lba, 100);
        assert_eq!(r.ranges[0].count, 2);
        assert_eq!(r.ranges[0].status, SectorStatus::Bad);
    }

    /// A batch that fails, then each individual sector succeeds on the first
    /// per-sector read, is counted as Good (not Recovered).
    /// Mutation: counting per-sector-first-ok as Recovered instead of Good flips this.
    #[test]
    fn batch_fail_then_per_sector_ok_counts_as_good() {
        // First read fails (batch), then all per-sector reads succeed.
        let mut src = FailFirst { remaining_fails: 1 };
        let title = title_with_extent(0, 2);
        let r = verify_title(&mut src, &title, 2, None);
        // Both sectors should be Good (or Slow if the read was slow — but
        // FailFirst returns instantly, so they should be Good).
        assert_eq!(r.bad, 0);
        assert_eq!(r.recovered, 0);
        // Good ≥ 1: at least one sector was read clean on the per-sector path.
        assert!(r.good >= 1, "expected some good sectors, got {}", r.good);
    }

    /// A recovered sector (batch fails, per-sector fails, retry succeeds)
    /// appears in ranges with SectorStatus::Recovered.
    /// This is exercised by FailFirst with remaining_fails=2: batch fails (1),
    /// first per-sector attempt fails (2), retry succeeds (0 remaining).
    /// Mutation: mapping recovered to Good removes the Recovered range entry.
    #[test]
    fn recovered_sector_appears_in_ranges_as_recovered() {
        // Batch read of 1 sector: fail once (batch), fail once (per-sector),
        // then succeed (retry). That yields Recovered status.
        let mut src = FailFirst { remaining_fails: 2 };
        let title = title_with_extent(0, 1);
        // Provide a callback that never cancels so the retry pause can run.
        // But we want the test to be fast — FailFirst returns immediately,
        // so the cancel-during-pause path won't be hit. However, we still
        // sleep RETRY_PAUSE here (2s). Use no-cancel callback.
        let noop_cb = |_: &crate::progress::PassProgress| true;
        let r = verify_title(&mut src, &title, 1, Some(&noop_cb));
        assert_eq!(r.recovered, 1, "sector should be Recovered");
        assert_eq!(r.bad, 0);
        assert_eq!(r.ranges.len(), 1);
        assert_eq!(r.ranges[0].status, SectorStatus::Recovered);
    }

    /// total_sectors sums across multiple extents correctly.
    /// Mutation: only summing the first extent makes total wrong.
    #[test]
    fn total_sectors_sums_multiple_extents() {
        let title = title_with_two_extents(0, 3, 100, 5);
        let mut src = AlwaysGood;
        let r = verify_title(&mut src, &title, 4, None);
        assert_eq!(r.total_sectors, 8, "expected 3+5=8 sectors");
        assert_eq!(r.good, 8);
    }

    /// SectorRange byte_offset is zero for the very first sector of the first extent.
    /// Mutation: starting byte_offset at 2048 instead of 0 shifts all offsets.
    #[test]
    fn sector_range_byte_offset_starts_at_zero_for_first_sector() {
        // AlwaysBad with 1 sector: the range for LBA 0 must have byte_offset=0.
        let title = title_with_extent(0, 1);
        let mut src = AlwaysBad;
        let r = verify_title(&mut src, &title, 1, None);
        assert_eq!(r.ranges.len(), 1);
        assert_eq!(
            r.ranges[0].byte_offset, 0,
            "first sector byte_offset must be 0"
        );
    }

    /// SectorRange byte_offset for the second sector is 2048.
    /// Spec: sector size = 2048 bytes on Blu-ray (ISO 9660 / ECMA-119 §7).
    /// Mutation: computing offset as `i * 2049` changes the second sector offset.
    #[test]
    fn sector_range_byte_offset_increments_by_2048() {
        // Force the second sector to be bad: fail twice (first two reads
        // attempt LBA 0 in batch, then individually). That is complex.
        // Simpler: use a source that only fails on LBA 1.
        struct BadOnLba1;
        impl SectorSource for BadOnLba1 {
            fn read_sectors(
                &mut self,
                lba: u32,
                count: u16,
                buf: &mut [u8],
                _recovery: bool,
            ) -> Result<usize, Error> {
                if lba == 1 && count == 1 {
                    Err(Error::ScsiError {
                        opcode: 0x28,
                        status: crate::scsi::SCSI_STATUS_CHECK_CONDITION,
                        sense: None,
                    })
                } else if lba == 0 && count == 2 {
                    // Batch of 2 fails when it includes LBA 1 — simulate batch
                    // failure so we fall to per-sector.
                    Err(Error::ScsiError {
                        opcode: 0x28,
                        status: crate::scsi::SCSI_STATUS_CHECK_CONDITION,
                        sense: None,
                    })
                } else {
                    let n = count as usize * 2048;
                    buf[..n].fill(0);
                    Ok(n)
                }
            }
        }
        // Extent: LBA 0..1 (2 sectors), batch size 2 so we get a batch attempt first.
        let title = title_with_extent(0, 2);
        let mut src = BadOnLba1;
        let noop_cb = |_: &crate::progress::PassProgress| true;
        let r = verify_title(&mut src, &title, 2, Some(&noop_cb));
        // We expect exactly one bad sector at LBA 1.
        assert_eq!(r.bad, 1);
        let bad_range = r
            .ranges
            .iter()
            .find(|rng| rng.status == SectorStatus::Bad)
            .unwrap();
        // LBA 1 is the second sector → byte_offset = 1 * 2048 = 2048.
        assert_eq!(
            bad_range.byte_offset, 2048,
            "second sector byte_offset must be 2048, got {}",
            bad_range.byte_offset
        );
    }

    /// cancellable_pause returns true when it completes the full duration
    /// without cancellation.
    /// Mutation: returning `false` unconditionally makes callers think they were cancelled.
    #[test]
    fn cancellable_pause_returns_true_when_not_cancelled() {
        // Use a very short duration so the test runs fast.
        let tiny = Duration::from_millis(10);
        let result = cancellable_pause(tiny, &mut || false);
        assert!(result, "not-cancelled pause must return true");
    }

    /// cancellable_pause returns false immediately when the callback requests cancel.
    /// Mutation: ignoring the callback return and sleeping the full duration breaks this.
    #[test]
    fn cancellable_pause_returns_false_when_cancelled() {
        let long = Duration::from_secs(60);
        let started = Instant::now();
        let result = cancellable_pause(long, &mut || true);
        assert!(!result, "cancelled pause must return false");
        // Must return well within the first poll interval, not after 60s.
        assert!(started.elapsed() < Duration::from_secs(1));
    }
}