exarch-core 0.12.0

Memory-safe archive extraction library with security validation
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
//! Meters the raw archive bytes a decoder pulls from its source and remembers
//! the source's own I/O faults.
//!
//! A [`SourceRead`] sits between the file and the decoder of a TAR stream. It
//! counts every byte it hands over into a [`SourceTap`], which lets a skipped
//! entry's decoded size be judged against the input that produced it, and it
//! records the first I/O error the source raised in a [`FaultSlot`]. The `tar`
//! crate reports a reader fault and a malformed header alike as an error from
//! its own call; the slot is what tells them apart, so a failing disk is never
//! reported as a corrupt archive.

use std::io;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;

use super::io_marker::IoMarker;
use super::tar_metadata_limit::budget_violation;
use crate::ArchiveError;
use crate::Malformation;
use crate::Violation;
use crate::copy::CopyError;
use crate::sync::lock_unpoisoned;

/// The decoder family that reads an entry's bytes, which decides how a failed
/// read is classified.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReadFamily {
    /// A TAR stream.
    Tar,
    /// A ZIP archive.
    Zip,
    /// A 7z archive.
    SevenZ,
    /// A reader none of whose errors is a decoder error: every read failure is
    /// an I/O failure of its source, as for a spool core wrote itself or a
    /// caller-supplied decoded stream.
    Opaque,
}

/// What a copy of one entry's bytes knows about its source, so a failed read
/// can be told from a failed write and a decoder error from a disk fault.
///
/// Passed through every
/// [`ExtractTarget::file`](super::target::ExtractTarget::file) to the copy that
/// reads the entry, which hands a failure to [`classify`](Self::classify).
#[derive(Debug, Clone, Copy)]
pub(crate) struct ReadFaults<'a> {
    tap: Option<&'a SourceTap>,
    family: ReadFamily,
    encrypted: Option<crate::EncryptionRefusal>,
}

impl<'a> ReadFaults<'a> {
    /// The faults of an entry read by `family`, with the source's tap when it
    /// has one.
    #[must_use]
    pub(crate) const fn new(family: ReadFamily, tap: Option<&'a SourceTap>) -> Self {
        Self {
            tap,
            family,
            encrypted: None,
        }
    }

    /// The faults of an entry that is decrypted while it is read: a decoder
    /// verdict on its bytes (checksum, truncation, malformed data) is then
    /// `reason`, since tampered or wrongly decrypted data fails that way.
    #[must_use]
    pub(crate) const fn decrypting(mut self, reason: crate::EncryptionRefusal) -> Self {
        self.encrypted = Some(reason);
        self
    }

    /// The decoder family of the entry being read.
    #[must_use]
    pub(crate) const fn family(self) -> ReadFamily {
        self.family
    }

    /// The faults of a TAR entry read from `source`.
    ///
    /// A stream whose source core cannot see has no way to tell a decoder
    /// error from a disk fault, so its read errors stay I/O errors.
    #[must_use]
    pub(crate) const fn of_tar(source: &'a TapSource) -> Self {
        match source {
            TapSource::Tapped(tap) | TapSource::Replay(tap) => {
                Self::new(ReadFamily::Tar, Some(tap))
            }
            #[cfg(test)]
            TapSource::Opaque => Self::new(ReadFamily::Opaque, None),
        }
    }

    pub(crate) fn pending_fault(self) -> Option<io::Error> {
        self.tap.and_then(SourceTap::fault)
    }

    /// Maps the failure of a copy to the error extraction reports.
    ///
    /// A failed write is always [`ArchiveError::Io`]: the destination is not
    /// the archive. A failed read is a budget refusal when the stream hit one,
    /// an I/O error when the source raised a fault, and otherwise a malformed
    /// archive: the decoder rejected the bytes it was given. A short entry
    /// after a source fault is that fault; a short TAR entry without one is a
    /// truncated archive; every other size mismatch and every overrun keeps
    /// the declared-size classification.
    #[must_use]
    pub(crate) fn classify(self, error: CopyError) -> ArchiveError {
        match error {
            CopyError::Read(error) => self.classify_read(error),
            CopyError::Short { declared, actual } => match self.pending_fault() {
                Some(fault) => ArchiveError::Io(fault),
                None if self.family == ReadFamily::Tar => {
                    ArchiveError::from(Malformation::TarEntryTruncated { declared, actual })
                }
                None => ArchiveError::from(CopyError::Short { declared, actual }),
            },
            other => ArchiveError::from(other),
        }
    }

    /// Classifies the error of a read that is not part of a copy, such as
    /// draining the rest of an entry.
    #[must_use]
    pub(crate) fn classify_read(self, error: io::Error) -> ArchiveError {
        let verdict = classify_decoder_read(error, self.pending_fault(), self.family);
        if self.family == ReadFamily::Zip && matches!(verdict, ReadVerdict::DeclaredSizeOverrun(_))
        {
            // The entry's caller knows the declared size and types it as a
            // security violation.
            return ArchiveError::Io(DeclaredSizeCeiling.into_io());
        }
        match verdict.into_decoder_error() {
            Ok(error) => self.unreadable(error),
            Err(verdict) => verdict,
        }
    }

    /// Classifies the error of reading a stored symlink target: a source
    /// fault is I/O, anything else leaves the target unreadable.
    #[must_use]
    pub(crate) fn classify_symlink_read(self, error: io::Error) -> ArchiveError {
        match classify_decoder_read(error, self.pending_fault(), self.family).into_decoder_error() {
            Ok(error) if self.encrypted.is_some() => self.unreadable(error),
            Ok(error) => ArchiveError::from(Malformation::SymlinkTargetUnreadable {
                detail: error.to_string().into(),
            }),
            Err(verdict) => verdict,
        }
    }

    fn unreadable(self, error: io::Error) -> ArchiveError {
        if let Some(reason) = self.encrypted {
            return Violation::Encrypted { reason, name: None }.into();
        }
        let detail: Box<str> = error.to_string().into();
        match self.family {
            ReadFamily::Opaque => ArchiveError::Io(error),
            ReadFamily::Tar => ArchiveError::from(Malformation::TarEntryUnreadable { detail }),
            ReadFamily::Zip => ArchiveError::from(Malformation::ZipEntryDataUnreadable { detail }),
            ReadFamily::SevenZ => {
                ArchiveError::from(Malformation::SevenZEntryDataUnreadable { detail })
            }
        }
    }
}

/// What a failed decoder read means, decided once for extract, list and
/// verify.
///
/// Every variant but [`Budget`](Self::Budget) keeps the error that was read,
/// so a front end can quote its detail.
#[derive(Debug)]
pub(crate) enum ReadVerdict {
    /// A decoder memory, member-count or compression-ratio refusal.
    Budget(ArchiveError),
    /// The source's own I/O failure: a recorded fault, or an errno no decoder
    /// raises.
    SourceFault(io::Error),
    /// The entry's checksum did not match its bytes.
    Checksum(io::Error),
    /// The entry decoded past its declared size.
    DeclaredSizeOverrun(io::Error),
    /// The stream ended before the entry did.
    Truncated(io::Error),
    /// The decoder rejected the bytes it was given.
    Malformed(io::Error),
}

impl ReadVerdict {
    /// The error of a decoder verdict, or the finished [`ArchiveError`] of a
    /// budget refusal or source fault.
    fn into_decoder_error(self) -> Result<io::Error, ArchiveError> {
        match self {
            Self::Budget(error) => Err(error),
            Self::SourceFault(error) => Err(ArchiveError::Io(error)),
            Self::Checksum(error)
            | Self::DeclaredSizeOverrun(error)
            | Self::Truncated(error)
            | Self::Malformed(error) => Ok(error),
        }
    }
}

/// Decides what the failed read `error` means: the one chain behind
/// extraction, listing and verification.
///
/// A budget refusal wins, then the source's recorded `fault`, then any errno
/// (a decoder never raises one, so it is the disk's), then the decoder's own
/// checksum, declared-size and truncation errors.
pub(crate) fn classify_decoder_read(
    error: io::Error,
    fault: Option<io::Error>,
    family: ReadFamily,
) -> ReadVerdict {
    if let Some(violation) = budget_violation(&error) {
        return ReadVerdict::Budget(violation);
    }
    if let Some(fault) = fault {
        return ReadVerdict::SourceFault(fault);
    }
    if error.raw_os_error().is_some() {
        return ReadVerdict::SourceFault(error);
    }
    if is_checksum_failure(&error, family) {
        return ReadVerdict::Checksum(error);
    }
    if zip_crate_failure(&error, family) == Some(ZipCrateFailure::DeclaredSizeExceeded) {
        return ReadVerdict::DeclaredSizeOverrun(error);
    }
    if error.kind() == io::ErrorKind::UnexpectedEof {
        return ReadVerdict::Truncated(error);
    }
    ReadVerdict::Malformed(error)
}

/// Core's own replay failure: the replayed bytes do not match the entry's
/// CRC-32. Found by downcast, never by message.
#[derive(Debug, thiserror::Error)]
#[error("Invalid checksum")]
pub(crate) struct ReplayChecksumMismatch;

impl IoMarker for ReplayChecksumMismatch {}

/// An entry decoded past its declared size, typed so the entry's caller can
/// raise a security violation.
#[derive(Debug, thiserror::Error)]
#[error("entry data exceeds its declared size")]
pub(crate) struct DeclaredSizeCeiling;

impl IoMarker for DeclaredSizeCeiling {}

/// A failure the `zip` crate reports as a plain-text `io::Error`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ZipCrateFailure {
    InvalidChecksum,
    DeclaredSizeExceeded,
}

/// The only place core recognizes the `zip` crate's own read errors, which it
/// raises as untyped `InvalidData` strings ("Invalid checksum" and "File is
/// larger than its declared uncompressed size" in zip 9.4).
///
/// Only a [`ReadFamily::Zip`] read is looked at: another decoder's message that
/// happens to contain the same words is not a ZIP verdict. Matches keywords
/// rather than the exact text so that a rewording within the supported range
/// keeps its meaning; `zip_crate_contract_holds` drives the real crate and
/// fails loudly when a release changes either error.
fn zip_crate_failure(error: &io::Error, family: ReadFamily) -> Option<ZipCrateFailure> {
    if family != ReadFamily::Zip
        || error.kind() != io::ErrorKind::InvalidData
        || error.get_ref().is_none()
    {
        return None;
    }
    let message = error.to_string();
    let has = |needle: &str| {
        message
            .as_bytes()
            .windows(needle.len())
            .any(|window| window.eq_ignore_ascii_case(needle.as_bytes()))
    };
    if has("declared") && has("size") {
        Some(ZipCrateFailure::DeclaredSizeExceeded)
    } else if has("checksum") {
        Some(ZipCrateFailure::InvalidChecksum)
    } else {
        None
    }
}

/// Whether `error` is the checksum failure sevenz-rust2 raises for an entry
/// whose data was read to its end.
pub(crate) fn is_sevenz_checksum(error: &io::Error) -> bool {
    error
        .get_ref()
        .and_then(|inner| inner.downcast_ref::<sevenz_rust2::Error>())
        .is_some_and(|e| matches!(e, sevenz_rust2::Error::ChecksumVerificationFailed))
}

fn is_checksum_failure(error: &io::Error, family: ReadFamily) -> bool {
    ReplayChecksumMismatch::find(error).is_some()
        || is_sevenz_checksum(error)
        || zip_crate_failure(error, family) == Some(ZipCrateFailure::InvalidChecksum)
}

/// Reports a source `fault` as [`ArchiveError::Io`] in place of a verdict on
/// the bytes the failing read would have delivered: a malformed archive, or a
/// corrupt or truncated stream at the end of a TAR.
///
/// A budget, security or ambiguity refusal is about bytes that were read, and
/// stays as it is.
pub(crate) fn prefer_fault(error: ArchiveError, fault: Option<io::Error>) -> ArchiveError {
    use crate::error::TarTrailerRefusal;
    use crate::error::TrailerCorruption;
    match (&error, fault) {
        (
            ArchiveError::InvalidArchive(_)
            | ArchiveError::TarTrailer(
                TarTrailerRefusal::Truncated
                | TarTrailerRefusal::Corrupt {
                    cause: TrailerCorruption::Decoder,
                    ..
                },
            ),
            Some(fault),
        ) => ArchiveError::Io(fault),
        _ => error,
    }
}

/// An I/O error as the source raised it: kind, errno and message.
#[derive(Debug, Clone)]
struct RecordedFault {
    kind: io::ErrorKind,
    raw_os_error: Option<i32>,
    message: String,
}

impl RecordedFault {
    fn of(error: &io::Error) -> Self {
        Self {
            kind: error.kind(),
            raw_os_error: error.raw_os_error(),
            message: error.to_string(),
        }
    }

    /// Rebuilds the error, keeping the errno when the source gave one.
    fn rebuild(&self) -> io::Error {
        self.raw_os_error.map_or_else(
            || io::Error::new(self.kind, self.message.clone()),
            io::Error::from_raw_os_error,
        )
    }
}

/// The first I/O error a source raised, shared between the reader that saw it
/// and the code that classifies a failure further up the stack.
///
/// An [`io::ErrorKind::Interrupted`] error is a retry request, not a fault,
/// and is never recorded. Cheap to clone; every clone sees the same slot.
#[derive(Debug, Clone, Default)]
pub(crate) struct FaultSlot(Arc<Mutex<Option<RecordedFault>>>);

impl FaultSlot {
    fn lock(&self) -> MutexGuard<'_, Option<RecordedFault>> {
        lock_unpoisoned(&self.0)
    }

    /// Remembers `error` unless a fault is already held or it is a retry
    /// request.
    pub(crate) fn record(&self, error: &io::Error) {
        if error.kind() == io::ErrorKind::Interrupted {
            return;
        }
        self.lock().get_or_insert_with(|| RecordedFault::of(error));
    }

    /// The recorded fault, rebuilt as an [`io::Error`], leaving it in place.
    #[must_use]
    pub(crate) fn get(&self) -> Option<io::Error> {
        self.lock().as_ref().map(RecordedFault::rebuild)
    }

    /// Forgets the recorded fault: the stream read on successfully, so it is
    /// aligned again and a later failure is not the fault's doing.
    pub(crate) fn clear(&self) {
        self.lock().take();
    }
}

/// Shared view of the raw bytes read from an archive source and of the faults
/// it raised.
///
/// Cheap to clone; every clone observes the same counter and slot.
#[derive(Debug, Clone, Default)]
pub(crate) struct SourceTap {
    consumed: Arc<AtomicU64>,
    fault: FaultSlot,
}

impl SourceTap {
    /// A tap over an existing fault `slot`, for a source that records its own
    /// faults.
    #[must_use]
    pub(crate) fn over(slot: FaultSlot) -> Self {
        Self {
            consumed: Arc::default(),
            fault: slot,
        }
    }

    /// Raw bytes read from the source so far.
    #[must_use]
    pub(crate) fn consumed(&self) -> u64 {
        self.consumed.load(Ordering::Relaxed)
    }

    /// The I/O fault the source raised, if one is pending.
    #[must_use]
    pub(crate) fn fault(&self) -> Option<io::Error> {
        self.fault.get()
    }
}

/// Read wrapper that records the bytes it returns and the faults it raises into
/// a [`SourceTap`].
///
/// Never alters, buffers or reorders the bytes, and returns every error
/// unchanged.
pub(crate) struct SourceRead<R> {
    inner: R,
    tap: SourceTap,
}

impl<R> SourceRead<R> {
    /// Wraps `inner`, recording into `tap`.
    pub(crate) const fn new(inner: R, tap: SourceTap) -> Self {
        Self { inner, tap }
    }
}

impl<R: Read> Read for SourceRead<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self.inner.read(buf) {
            Ok(n) => {
                if n > 0 {
                    self.tap.fault.clear();
                }
                self.tap.consumed.fetch_add(n as u64, Ordering::Relaxed);
                Ok(n)
            }
            Err(e) => {
                self.tap.fault.record(&e);
                Err(e)
            }
        }
    }
}

impl<R: Seek> Seek for SourceRead<R> {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        self.inner
            .seek(pos)
            .inspect_err(|error| self.tap.fault.record(error))
    }
}

/// The raw source behind a TAR byte stream, as the read budget sees it.
#[derive(Debug, Clone)]
pub(crate) enum TapSource {
    /// Core built the stream and metered its input.
    Tapped(SourceTap),
    /// Core replays the decoded stream a list pass spooled to a file: the
    /// spool's read faults are recorded, but its bytes are already decoded, so
    /// they say nothing about the input that produced them.
    Replay(SourceTap),
    /// The caller supplied an already-decoded reader; core cannot see how much
    /// input produced it or which errors came from its source, so it records
    /// no faults and its I/O errors keep their existing classification.
    #[cfg(test)]
    Opaque,
}

impl TapSource {
    /// The I/O fault the source raised, if one is pending.
    #[must_use]
    pub(crate) fn fault(&self) -> Option<io::Error> {
        match self {
            Self::Tapped(tap) | Self::Replay(tap) => tap.fault(),
            #[cfg(test)]
            Self::Opaque => None,
        }
    }
}

/// Real `zip` crate archives and read errors, for tests that pin how the
/// crate's own failures are classified.
#[cfg(test)]
pub(crate) mod zip_fixtures {
    use std::io;

    pub(crate) fn stored_zip(data: &[u8]) -> Vec<u8> {
        let mut writer = zip::write::ZipWriter::new(io::Cursor::new(Vec::new()));
        let options = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        writer.start_file("a", options).unwrap();
        io::Write::write_all(&mut writer, data).unwrap();
        writer.finish().unwrap().into_inner()
    }

    pub(crate) fn forged_size_zip() -> Vec<u8> {
        let mut bytes = stored_zip(&[b'z'; 64]);
        for (signature, offset) in [
            ([0x50, 0x4B, 0x03, 0x04], 22),
            ([0x50, 0x4B, 0x01, 0x02], 24),
        ] {
            let at = bytes.windows(4).position(|w| w == signature).unwrap();
            bytes[at + offset..at + offset + 4].copy_from_slice(&8u32.to_le_bytes());
        }
        bytes
    }

    pub(crate) fn bad_crc_zip() -> Vec<u8> {
        let mut bytes = stored_zip(b"MARKER");
        let at = bytes.windows(6).position(|w| w == b"MARKER").unwrap();
        bytes[at] ^= 1;
        bytes
    }

    pub(crate) fn zip_read_error(bytes: &[u8]) -> io::Error {
        let mut archive = zip::ZipArchive::new(io::Cursor::new(bytes.to_vec())).unwrap();
        let mut entry = archive.by_index(0).unwrap();
        io::copy(&mut entry, &mut io::sink()).unwrap_err()
    }
}

#[cfg(test)]
mod tests {
    use std::assert_matches;

    use super::zip_fixtures::bad_crc_zip;
    use super::zip_fixtures::forged_size_zip;
    use super::zip_fixtures::zip_read_error;
    use super::*;

    struct Scripted(Vec<io::Result<usize>>);

    impl Read for Scripted {
        fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
            self.0.remove(0)
        }
    }

    #[test]
    fn counts_bytes_across_clones() {
        let tap = SourceTap::default();
        let mut reader = SourceRead::new(&b"abcdef"[..], tap.clone());
        let mut buf = [0u8; 4];
        assert_eq!(reader.read(&mut buf).unwrap(), 4);
        assert_eq!(tap.consumed(), 4);
        assert_eq!(reader.read(&mut buf).unwrap(), 2);
        assert_eq!(tap.consumed(), 6);
        assert_eq!(reader.read(&mut buf).unwrap(), 0);
        assert_eq!(tap.consumed(), 6);
    }

    #[test]
    fn keeps_the_errno_of_the_first_fault() {
        let tap = SourceTap::default();
        let mut reader = SourceRead::new(
            Scripted(vec![
                Err(io::Error::from_raw_os_error(5)),
                Err(io::Error::from_raw_os_error(28)),
            ]),
            tap.clone(),
        );
        let mut buf = [0u8; 4];
        reader.read(&mut buf).unwrap_err();
        reader.read(&mut buf).unwrap_err();
        assert_eq!(tap.fault().unwrap().raw_os_error(), Some(5));
    }

    #[test]
    fn rebuilds_a_fault_without_errno_from_kind_and_message() {
        let tap = SourceTap::default();
        let mut reader = SourceRead::new(
            Scripted(vec![Err(io::Error::new(io::ErrorKind::TimedOut, "slow"))]),
            tap.clone(),
        );
        reader.read(&mut [0u8; 4]).unwrap_err();
        let fault = tap.fault().unwrap();
        assert_eq!(fault.kind(), io::ErrorKind::TimedOut);
        assert_eq!(fault.raw_os_error(), None);
        assert!(fault.to_string().contains("slow"));
    }

    #[test]
    fn a_retry_request_is_not_a_fault() {
        let tap = SourceTap::default();
        let mut reader = SourceRead::new(
            Scripted(vec![Err(io::ErrorKind::Interrupted.into())]),
            tap.clone(),
        );
        reader.read(&mut [0u8; 4]).unwrap_err();
        assert!(tap.fault().is_none());
    }

    fn tap_with_fault() -> SourceTap {
        let tap = SourceTap::default();
        tap.fault.record(&io::Error::from_raw_os_error(5));
        tap
    }

    fn decoder_error() -> io::Error {
        io::Error::new(io::ErrorKind::InvalidData, "lzma data error")
    }

    #[test]
    fn a_decoder_error_without_a_source_fault_is_a_malformed_entry() {
        let tap = SourceTap::default();
        for (family, expected) in [
            (
                ReadFamily::Tar,
                Malformation::TarEntryUnreadable {
                    detail: "lzma data error".into(),
                },
            ),
            (
                ReadFamily::Zip,
                Malformation::ZipEntryDataUnreadable {
                    detail: "lzma data error".into(),
                },
            ),
            (
                ReadFamily::SevenZ,
                Malformation::SevenZEntryDataUnreadable {
                    detail: "lzma data error".into(),
                },
            ),
        ] {
            let error =
                ReadFaults::new(family, Some(&tap)).classify(CopyError::Read(decoder_error()));
            assert_matches!(error, ArchiveError::InvalidArchive(ref m) if *m == expected, "{family:?}");
        }
    }

    #[test]
    fn a_recorded_source_fault_wins_over_the_decoder_verdict() {
        let tap = tap_with_fault();
        for family in [ReadFamily::Tar, ReadFamily::Zip, ReadFamily::SevenZ] {
            let error =
                ReadFaults::new(family, Some(&tap)).classify(CopyError::Read(decoder_error()));
            assert_matches!(error, ArchiveError::Io(ref e) if e.raw_os_error() == Some(5), "{family:?}");
        }
    }

    #[test]
    fn an_errno_is_the_disks_even_without_a_recorded_fault() {
        let error = ReadFaults::new(ReadFamily::Zip, None)
            .classify(CopyError::Read(io::Error::from_raw_os_error(28)));
        assert_matches!(error, ArchiveError::Io(ref e) if e.raw_os_error() == Some(28));
    }

    #[test]
    fn an_opaque_reader_keeps_every_read_error_as_io() {
        let error =
            ReadFaults::new(ReadFamily::Opaque, None).classify(CopyError::Read(decoder_error()));
        assert_matches!(error, ArchiveError::Io(ref e) if e.kind() == io::ErrorKind::InvalidData);
    }

    #[test]
    fn a_budget_refusal_wins_over_a_recorded_fault() {
        let exceeded =
            io::Error::other(super::super::tar_metadata_limit::DecoderMemoryExceeded { max: 7 });
        let tap = tap_with_fault();
        let error =
            ReadFaults::new(ReadFamily::Tar, Some(&tap)).classify(CopyError::Read(exceeded));
        assert_matches!(error, ArchiveError::QuotaExceeded { .. });
    }

    #[test]
    fn the_zip_size_ceiling_is_left_for_the_entry_to_type() {
        let ceiling = io::Error::new(io::ErrorKind::InvalidData, "ceiling");
        let error = ReadFaults::new(ReadFamily::Zip, None).classify(CopyError::Read(ceiling));
        assert_matches!(error, ArchiveError::InvalidArchive(_));

        let ceiling = zip_read_error(&forged_size_zip());
        let error = ReadFaults::new(ReadFamily::Zip, None).classify(CopyError::Read(ceiling));
        assert_matches!(error, ArchiveError::Io(ref e) if DeclaredSizeCeiling::find(e).is_some());
    }

    #[test]
    fn zip_crate_contract_holds() {
        assert_eq!(
            zip_crate_failure(&zip_read_error(&bad_crc_zip()), ReadFamily::Zip),
            Some(ZipCrateFailure::InvalidChecksum),
            "the zip crate changed its checksum error; update zip_crate_failure"
        );
        assert_eq!(
            zip_crate_failure(&zip_read_error(&forged_size_zip()), ReadFamily::Zip),
            Some(ZipCrateFailure::DeclaredSizeExceeded),
            "the zip crate changed its declared-size error; update zip_crate_failure"
        );
    }

    #[test]
    fn only_a_zip_read_gets_the_zip_text_verdicts() {
        let texts = [
            "File is larger than its declared uncompressed size",
            "Invalid checksum",
        ];
        for family in [ReadFamily::Tar, ReadFamily::SevenZ, ReadFamily::Opaque] {
            for text in texts {
                let error = io::Error::new(io::ErrorKind::InvalidData, text);
                assert_matches!(
                    classify_decoder_read(error, None, family),
                    ReadVerdict::Malformed(_),
                    "{family:?}: {text}"
                );
            }
        }
        for text in texts {
            let error = io::Error::new(io::ErrorKind::InvalidData, text);
            assert_matches!(
                ReadFaults::new(ReadFamily::Tar, None).classify(CopyError::Read(error)),
                ArchiveError::InvalidArchive(_)
            );
        }
    }

    #[test]
    fn the_verdict_chain_orders_its_causes() {
        let exceeded =
            io::Error::other(super::super::tar_metadata_limit::DecoderMemoryExceeded { max: 7 });
        assert_matches!(
            classify_decoder_read(
                exceeded,
                Some(io::Error::from_raw_os_error(5)),
                ReadFamily::Zip
            ),
            ReadVerdict::Budget(_)
        );
        assert_matches!(
            classify_decoder_read(
                zip_read_error(&bad_crc_zip()),
                Some(io::Error::other("eio")),
                ReadFamily::Zip
            ),
            ReadVerdict::SourceFault(_)
        );
        assert_matches!(
            classify_decoder_read(io::Error::from_raw_os_error(28), None, ReadFamily::Zip),
            ReadVerdict::SourceFault(_)
        );
        assert_matches!(
            classify_decoder_read(zip_read_error(&bad_crc_zip()), None, ReadFamily::Zip),
            ReadVerdict::Checksum(_)
        );
        assert_matches!(
            classify_decoder_read(ReplayChecksumMismatch.into_io(), None, ReadFamily::Tar),
            ReadVerdict::Checksum(_)
        );
        assert_matches!(
            classify_decoder_read(
                io::Error::other(sevenz_rust2::Error::ChecksumVerificationFailed),
                None,
                ReadFamily::SevenZ
            ),
            ReadVerdict::Checksum(_)
        );
        assert_matches!(
            classify_decoder_read(zip_read_error(&forged_size_zip()), None, ReadFamily::Zip),
            ReadVerdict::DeclaredSizeOverrun(_)
        );
        assert_matches!(
            classify_decoder_read(io::ErrorKind::UnexpectedEof.into(), None, ReadFamily::Tar),
            ReadVerdict::Truncated(_)
        );
        assert_matches!(
            classify_decoder_read(decoder_error(), None, ReadFamily::Tar),
            ReadVerdict::Malformed(_)
        );
    }

    #[test]
    fn a_plain_error_with_checksum_text_is_not_a_zip_failure_without_a_payload() {
        let bare = io::Error::from(io::ErrorKind::InvalidData);
        assert_eq!(zip_crate_failure(&bare, ReadFamily::Zip), None);
    }

    #[test]
    fn a_failed_write_is_io_whatever_the_source_did() {
        let tap = tap_with_fault();
        let error = ReadFaults::new(ReadFamily::Tar, Some(&tap))
            .classify(CopyError::Write(io::Error::from_raw_os_error(28)));
        assert_matches!(error, ArchiveError::Io(ref e) if e.raw_os_error() == Some(28));
    }

    #[test]
    fn a_short_tar_entry_is_truncated_unless_the_source_failed() {
        let short = || CopyError::Short {
            declared: 10,
            actual: 4,
        };
        let clean = SourceTap::default();
        assert_matches!(
            ReadFaults::new(ReadFamily::Tar, Some(&clean)).classify(short()),
            ArchiveError::InvalidArchive(Malformation::TarEntryTruncated {
                declared: 10,
                actual: 4
            })
        );
        let faulty = tap_with_fault();
        for family in [ReadFamily::Tar, ReadFamily::Zip, ReadFamily::SevenZ] {
            assert_matches!(
                ReadFaults::new(family, Some(&faulty)).classify(short()),
                ArchiveError::Io(ref e) if e.raw_os_error() == Some(5),
                "{family:?}"
            );
        }
    }

    #[test]
    fn a_short_zip_or_7z_entry_and_every_overrun_keep_the_declared_size_verdict() {
        for family in [ReadFamily::Zip, ReadFamily::SevenZ, ReadFamily::Opaque] {
            let faults = ReadFaults::new(family, None);
            assert_matches!(
                faults.classify(CopyError::Short {
                    declared: 10,
                    actual: 4
                }),
                ArchiveError::SecurityViolation { .. },
                "{family:?}"
            );
        }
        for family in [ReadFamily::Tar, ReadFamily::Zip, ReadFamily::SevenZ] {
            assert_matches!(
                ReadFaults::new(family, None).classify(CopyError::Overrun { declared: 3 }),
                ArchiveError::SecurityViolation { .. },
                "{family:?}"
            );
        }
    }

    #[test]
    fn a_tar_source_picks_its_tap_and_an_opaque_one_reads_as_io() {
        let tap = tap_with_fault();
        let replay = TapSource::Replay(tap);
        assert_matches!(
            ReadFaults::of_tar(&replay).classify(CopyError::Read(decoder_error())),
            ArchiveError::Io(_)
        );
        assert_matches!(
            ReadFaults::of_tar(&TapSource::Opaque).classify(CopyError::Read(decoder_error())),
            ArchiveError::Io(_)
        );
        assert_matches!(
            ReadFaults::of_tar(&TapSource::Tapped(SourceTap::default()))
                .classify(CopyError::Read(decoder_error())),
            ArchiveError::InvalidArchive(_)
        );
    }

    #[test]
    fn a_symlink_target_read_is_io_after_a_source_fault_and_unreadable_otherwise() {
        let faulty = tap_with_fault();
        assert_matches!(
            ReadFaults::new(ReadFamily::Zip, Some(&faulty)).classify_symlink_read(decoder_error()),
            ArchiveError::Io(_)
        );
        let clean = SourceTap::default();
        assert_matches!(
            ReadFaults::new(ReadFamily::Zip, Some(&clean)).classify_symlink_read(decoder_error()),
            ArchiveError::InvalidArchive(Malformation::SymlinkTargetUnreadable { .. })
        );
    }

    #[test]
    fn a_failed_seek_is_a_recorded_fault() {
        struct NoSeek;
        impl Read for NoSeek {
            fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
                Ok(0)
            }
        }
        impl Seek for NoSeek {
            fn seek(&mut self, _: SeekFrom) -> io::Result<u64> {
                Err(io::Error::from_raw_os_error(5))
            }
        }
        let tap = SourceTap::default();
        let mut reader = SourceRead::new(NoSeek, tap.clone());
        reader.seek(SeekFrom::Start(1)).unwrap_err();
        assert_eq!(tap.fault().unwrap().raw_os_error(), Some(5));
    }

    #[test]
    fn a_later_successful_read_clears_the_fault() {
        let tap = SourceTap::default();
        let mut reader = SourceRead::new(
            Scripted(vec![Err(io::Error::from_raw_os_error(5)), Ok(3), Ok(0)]),
            tap.clone(),
        );
        let mut buf = [0u8; 4];
        reader.read(&mut buf).unwrap_err();
        assert!(tap.fault().is_some());
        assert_eq!(reader.read(&mut buf).unwrap(), 3);
        assert!(tap.fault().is_none());
    }
}