asdf-core 0.2.3

Core engine for reading and writing ASDF (Advanced Scientific Data Format) files
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
//! Reading an ASDF file: its tree, and the data in its binary blocks.

use alloc::borrow::Cow;
use std::path::{Path, PathBuf};

use asdf_yaml::{Document, parse_document};

use crate::block::header::CHECKSUM_SIZE;
use crate::compression::Compression;
use crate::error::{Result, err};
use crate::layout::{BlockLocation, Layout, scan};

/// Where a reader's bytes come from.
enum Source {
    /// A memory-mapped file. Block data is read straight out of the mapping,
    /// so a large array costs no copy until it is decompressed or converted.
    ///
    /// Absent under Miri, which cannot execute `mmap`: `Reader::open` reads
    /// the file whole there instead, so nothing would construct this and
    /// `dead_code` would fire.
    #[cfg(not(miri))]
    Mapped(memmap2::Mmap),
    /// An in-memory buffer.
    Owned(Vec<u8>),
}

impl core::ops::Deref for Source {
    type Target = [u8];
    fn deref(&self) -> &[u8] {
        match self {
            #[cfg(not(miri))]
            Source::Mapped(m) => m,
            Source::Owned(v) => v,
        }
    }
}

impl core::fmt::Debug for Source {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let kind = match self {
            #[cfg(not(miri))]
            Source::Mapped(_) => "Mapped",
            Source::Owned(_) => "Owned",
        };
        write!(f, "{kind}({} bytes)", self.len())
    }
}

/// The outcome of verifying a block's checksum.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ChecksumStatus {
    /// The header records no checksum; the all-zero value means "do not
    /// verify", so this is not a failure.
    Absent,
    /// The recorded digest matches.
    Valid,
    /// The recorded digest does not match.
    Invalid,
}

impl ChecksumStatus {
    /// Whether this status should be treated as a failure.
    ///
    /// An absent checksum is not one: the standard makes it optional.
    pub fn is_failure(self) -> bool {
        self == ChecksumStatus::Invalid
    }
}

/// The relative path an external `source` URI names, if it names a safe one.
///
/// Rejects anything that could escape the referring file's directory: an
/// absolute path, a `..` component, a Windows drive or UNC prefix, or a URI
/// with a scheme or authority. `file:` is not special-cased -- a relative
/// path is the only form the reference corpus uses and the only one worth
/// the risk.
fn external_relative_path(uri: &str) -> Result<PathBuf> {
    if uri.is_empty() {
        return Err(err!(InvalidArgument, "external source is an empty URI"));
    }
    if uri.contains("://") || uri.starts_with('/') || uri.starts_with('\\') {
        return Err(err!(
            InvalidArgument,
            "external source {uri:?} is not a relative path; only files beside the \
             referring one can be resolved"
        ));
    }

    // Percent-decoding is deliberately not done: a URI needing it is not one
    // the corpus produces, and decoding would reopen the escape it rejects.
    let path = Path::new(uri);
    for component in path.components() {
        use std::path::Component;
        match component {
            Component::Normal(_) | Component::CurDir => {}
            Component::ParentDir => {
                return Err(err!(
                    InvalidArgument,
                    "external source {uri:?} climbs out of the referring file's directory"
                ));
            }
            Component::RootDir | Component::Prefix(_) => {
                return Err(err!(InvalidArgument, "external source {uri:?} is not relative"));
            }
        }
    }
    Ok(path.to_path_buf())
}

/// An open ASDF file.
#[derive(Debug)]
pub struct Reader {
    source: Source,
    layout: Layout,
    /// Where the file came from, when it came from disk.
    ///
    /// Kept so that an array whose `source` names another file -- exploded
    /// form -- can be resolved relative to this one, as the standard says.
    path: Option<PathBuf>,
}

impl Reader {
    /// Open and scan a file from disk.
    ///
    /// The file is memory-mapped, so block data is not read until it is used.
    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();

        // Miri interprets rather than executes, so it has no `mmap`. Reading
        // the file whole is observably the same to every caller -- `Source`
        // hands out a `&[u8]` either way -- and it is what lets the FFI layer,
        // which is where the unsafe code actually lives, be checked at all.
        #[cfg(miri)]
        {
            let bytes = std::fs::read(path)?;
            let layout = scan(&bytes)?;
            return Ok(Self {
                source: Source::Owned(bytes),
                layout,
                path: Some(path.to_path_buf()),
            });
        }

        #[cfg(not(miri))]
        {
            let file = std::fs::File::open(path)?;
            Self::map(file, path)
        }
    }

    /// The memory-mapping half of [`Reader::open`], split out so the `miri`
    /// fallback above stays readable.
    #[cfg(not(miri))]
    fn map(file: std::fs::File, path: &Path) -> Result<Self> {
        // SAFETY: the only unsafe operation in the engine. Mapping is unsafe
        // because another process truncating the file can turn a later read
        // into SIGBUS. That hazard is inherent to memory-mapping and is the
        // same one libasdf accepts; ASDF files are written whole rather than
        // modified in place, so a concurrent truncation is not a case the
        // format contemplates. Mapping is what lets a multi-gigabyte array be
        // read without loading the file into memory, which is the point of
        // the format.
        #[allow(unsafe_code)]
        let mapped = unsafe { memmap2::Mmap::map(&file) }?;
        let layout = scan(&mapped)?;
        Ok(Self { source: Source::Mapped(mapped), layout, path: Some(path.to_path_buf()) })
    }

    /// Scan an in-memory file.
    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
        let layout = scan(&bytes)?;
        Ok(Self { source: Source::Owned(bytes), layout, path: None })
    }

    /// The path this file was opened from, if it came from disk.
    pub fn path(&self) -> Option<&Path> {
        self.path.as_deref()
    }

    /// The whole file's bytes.
    pub fn bytes(&self) -> &[u8] {
        &self.source
    }

    /// The scanned layout.
    pub fn layout(&self) -> &Layout {
        &self.layout
    }

    /// The YAML tree's text, if the file has a tree.
    pub fn tree_text(&self) -> Option<&str> {
        self.layout.tree_str(&self.source)
    }

    /// Parse the YAML tree.
    ///
    /// A file with no tree -- legitimate in exploded form -- yields `None`.
    pub fn tree(&self) -> Result<Option<Document>> {
        match self.tree_text() {
            None => Ok(None),
            Some(text) => Ok(Some(parse_document(text)?)),
        }
    }

    /// The number of binary blocks.
    pub fn block_count(&self) -> usize {
        self.layout.blocks.len()
    }

    /// A block's location and header.
    pub fn block(&self, index: usize) -> Result<&BlockLocation> {
        self.layout.blocks.get(index).ok_or_else(|| {
            err!(
                InvalidArgument,
                "block index {index} is out of range; the file has {} blocks",
                self.layout.blocks.len()
            )
        })
    }

    /// A block's bytes exactly as stored, without decompressing.
    ///
    /// For an uncompressed block this is the data itself; for a compressed
    /// one it is the compressed form.
    pub fn block_raw(&self, index: usize) -> Result<&[u8]> {
        let block = self.block(index)?;
        let start = usize::try_from(block.data_pos)
            .map_err(|_| err!(UnexpectedEof, "block {index} data offset overflows"))?;

        let len = if block.header.is_streamed() {
            // A streamed block runs to the end of the file; its size fields
            // are meaningless.
            self.source.len().saturating_sub(start)
        } else {
            usize::try_from(block.header.used_size)
                .map_err(|_| err!(UnexpectedEof, "block {index} used_size overflows"))?
        };

        // Checked, since both `start` and `len` come from the file and a
        // corrupt header can make the sum wrap.
        let end = start
            .checked_add(len)
            .ok_or_else(|| err!(UnexpectedEof, "block {index} size overflows"))?;
        self.source
            .get(start..end)
            .ok_or_else(|| err!(UnexpectedEof, "block {index} extends past the end of the file"))
    }

    /// The compression method a block uses.
    pub fn block_compression(&self, index: usize) -> Result<Compression> {
        Compression::from_name(self.block(index)?.header.compression_name())
    }

    /// A block's data, decompressed if necessary.
    ///
    /// An uncompressed block borrows straight from the file with no copy.
    pub fn block_data(&self, index: usize) -> Result<Cow<'_, [u8]>> {
        let raw = self.block_raw(index)?;
        let compression = self.block_compression(index)?;
        if compression == Compression::None {
            return Ok(Cow::Borrowed(raw));
        }
        let expected = usize::try_from(self.block(index)?.header.data_size)
            .map_err(|_| err!(UnexpectedEof, "block {index} data_size overflows"))?;
        Ok(Cow::Owned(compression.decompress(raw, expected)?))
    }

    /// Verify a block's MD5 checksum.
    ///
    /// The returned digest is what the data actually hashes to, which is
    /// useful for reporting a mismatch.
    ///
    /// # The Python asdf compatibility case
    ///
    /// For a *compressed* block, the specification means the checksum to
    /// cover the bytes as stored. Python asdf 5.x and earlier instead
    /// checksum the *uncompressed* data
    /// ([asdf#2015](https://github.com/asdf-format/asdf/issues/2015)).
    /// libasdf works around this by consulting the file's `asdf_library`
    /// metadata, and so do we: see [`Reader::has_python_checksum_bug`]. A
    /// compressed block whose stored bytes do not match is therefore retried
    /// against the decompressed bytes when the writer is known to be affected.
    pub fn verify_block_checksum(
        &self,
        index: usize,
    ) -> Result<(ChecksumStatus, [u8; CHECKSUM_SIZE])> {
        let header = &self.block(index)?.header;
        if !header.has_checksum() {
            return Ok((ChecksumStatus::Absent, [0; CHECKSUM_SIZE]));
        }
        let expected = header.checksum;

        let raw_digest = md5_of(self.block_raw(index)?);
        if raw_digest == expected {
            return Ok((ChecksumStatus::Valid, raw_digest));
        }

        // Only compressed blocks are affected, and only when the writer is
        // one of the versions known to be wrong.
        if self.block_compression(index)? != Compression::None && self.has_python_checksum_bug() {
            let decompressed = md5_of(&self.block_data(index)?);
            if decompressed == expected {
                return Ok((ChecksumStatus::Valid, decompressed));
            }
        }

        Ok((ChecksumStatus::Invalid, raw_digest))
    }

    /// Whether this file was written by a Python asdf version that
    /// checksums compressed blocks incorrectly.
    ///
    /// Matches libasdf's test: the `asdf_library` name is `asdf` and its
    /// major version is 5 or below.
    pub fn has_python_checksum_bug(&self) -> bool {
        const BUGGY_THROUGH_MAJOR: u32 = 5;

        let Ok(Some(doc)) = self.tree() else { return false };
        let Some(root) = doc.root() else { return false };
        let Some(library) = doc.mapping_get(root, "asdf_library") else {
            return false;
        };

        let name = doc
            .mapping_get(library, "name")
            .and_then(|id| doc.resolved(id).as_str().map(str::to_string));
        if name.as_deref() != Some("asdf") {
            return false;
        }

        doc.mapping_get(library, "version")
            .and_then(|id| doc.resolved(id).as_str().map(crate::Version::parse))
            .is_some_and(|v| v.major <= BUGGY_THROUGH_MAJOR)
    }
}

/// MD5 of a buffer.
fn md5_of(data: &[u8]) -> [u8; CHECKSUM_SIZE] {
    use md5::{Digest, Md5};
    let mut hasher = Md5::new();
    hasher.update(data);
    hasher.finalize().into()
}

/// Walking a tree to find every node carrying a given tag name.
///
/// The version suffix is ignored, so `core/ndarray-1.0.0` and
/// `core/ndarray-1.1.0` both match `core/ndarray`.
fn find_tagged(doc: &Document, name: &str) -> Vec<asdf_yaml::NodeId> {
    use asdf_yaml::NodeData;

    let mut out = Vec::new();
    let mut seen = std::collections::HashSet::new();
    let Some(root) = doc.root() else { return out };
    let mut stack = vec![root];

    while let Some(id) = stack.pop() {
        let resolved = doc.resolve(id);
        if !seen.insert(resolved) {
            continue;
        }
        if doc.tag_of(resolved).is_some_and(|t| t.split_version().0 == name) {
            out.push(resolved);
        }
        match &doc.node(resolved).data {
            NodeData::Sequence { items, .. } => stack.extend(items.iter().copied()),
            NodeData::Mapping { entries, .. } => {
                stack.extend(entries.iter().map(|e| e.value));
            }
            _ => {}
        }
    }
    out.sort();
    out
}

impl Reader {
    /// Resolve an ndarray's `source` to a block index in this file.
    fn block_index_for(&self, source: &crate::core::ndarray::Source) -> Option<usize> {
        match source {
            crate::core::ndarray::Source::Block(i) => Some(*i),
            crate::core::ndarray::Source::LastBlock => self.block_count().checked_sub(1),
            _ => None,
        }
    }

    /// Resolve an external array `source` and read the data it names.
    ///
    /// The standard makes `source` a URI relative to the file's own, and
    /// exploded form writes one array per file with the data in block 0.
    ///
    /// Resolution is deliberately narrow. The URI must be a relative path
    /// with no `..` component and no scheme, so a file can only reach others
    /// beneath its own directory: a tree is untrusted input, and following an
    /// arbitrary path out of it would let a crafted file name anything on the
    /// machine. A file read from memory has no directory to resolve against
    /// and so resolves nothing.
    pub fn external_block(&self, uri: &str) -> Result<Vec<u8>> {
        let Some(base) = self.path.as_deref().and_then(Path::parent) else {
            return Err(err!(
                InvalidArgument,
                "external source {uri:?} cannot be resolved: this file was not read from disk"
            ));
        };
        let relative = external_relative_path(uri)?;
        let target = base.join(relative);

        // The lexical check above stops `..` and absolute paths, but a
        // symlink is neither: `data.bin -> /etc/shadow` is a clean relative
        // name that `File::open` follows straight out of the directory.
        // Resolving both sides and comparing is the only check that sees it.
        let resolved = target.canonicalize().map_err(|e| {
            err!(InvalidArgument, "external source {uri:?} ({}): {e}", target.display())
        })?;
        let root = base.canonicalize().unwrap_or_else(|_| base.to_path_buf());
        if !resolved.starts_with(&root) {
            return Err(err!(
                InvalidArgument,
                "external source {uri:?} resolves to {}, outside the referring file's \
                 directory {}",
                resolved.display(),
                root.display()
            ));
        }

        let referenced = Reader::open(&resolved).map_err(|e| {
            err!(InvalidArgument, "external source {uri:?} ({}): {e}", target.display())
        })?;
        if referenced.block_count() == 0 {
            return Err(err!(InvalidArgument, "external source {uri:?} has no blocks"));
        }
        Ok(referenced.block_data(0)?.into_owned())
    }

    /// Parse the tree with every block-backed `core/ndarray` replaced by its
    /// data inline.
    ///
    /// This is the transformation the ASDF Standard's reference corpus asks
    /// for before comparing a file against its expected YAML. An array whose
    /// data lives in another file -- exploded form's external `source` -- is
    /// resolved through [`Reader::external_block`], which needs this file to
    /// have been read from disk.
    ///
    /// Returns the transformed tree and the paths of any arrays that could
    /// not be inlined.
    pub fn tree_inlined(&self) -> Result<Option<(Document, Vec<String>)>> {
        use crate::core::elements::{decode_all, inline_ndarray};
        use crate::core::ndarray::Ndarray;

        let Some(mut doc) = self.tree()? else { return Ok(None) };
        let mut skipped = Vec::new();

        for id in find_tagged(&doc, "core/ndarray") {
            let nd = match Ndarray::parse(&doc, id) {
                Ok(nd) => nd,
                Err(e) => {
                    skipped.push(format!("{id:?}: {e}"));
                    continue;
                }
            };

            // Inline data is already where it needs to be.
            if matches!(nd.source, crate::core::ndarray::Source::Inline(_)) {
                continue;
            }

            // An external source names another file; its first block holds
            // the data, which is how exploded form is written.
            let data = if let crate::core::ndarray::Source::External(uri) = &nd.source {
                match self.external_block(uri) {
                    Ok(bytes) => Cow::Owned(bytes),
                    Err(e) => {
                        skipped.push(format!("{id:?}: {e}"));
                        continue;
                    }
                }
            } else {
                let Some(index) = self.block_index_for(&nd.source) else {
                    skipped.push(format!("{id:?}: data is outside this file ({:?})", nd.source));
                    continue;
                };
                match self.block_data(index) {
                    Ok(d) => d,
                    Err(e) => {
                        skipped.push(format!("{id:?}: block {index}: {e}"));
                        continue;
                    }
                }
            };

            let shape = match nd.resolved_shape(Some(data.len() as u64)) {
                Ok(s) => s,
                Err(e) => {
                    skipped.push(format!("{id:?}: {e}"));
                    continue;
                }
            };

            match decode_all(&nd, &shape, &data) {
                Ok(elements) => inline_ndarray(&mut doc, id, &elements, &shape)?,
                Err(e) => skipped.push(format!("{id:?}: {e}")),
            }
        }
        Ok(Some((doc, skipped)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::block::header::BlockHeader;
    use crate::layout::write_block_index;

    /// Build a file with one block, optionally compressed and checksummed.
    fn build(payload: &[u8], compression: Compression, checksum_over: Option<&[u8]>) -> Vec<u8> {
        let stored = compression.compress(payload).unwrap();
        let mut buf = Vec::new();
        buf.extend_from_slice(b"#ASDF 1.0.0\n#ASDF_STANDARD 1.6.0\n");
        buf.extend_from_slice(
            b"%YAML 1.1\n%TAG ! tag:stsci.edu:asdf/\n--- !core/asdf-1.1.0\nx: 1\n...\n",
        );

        let mut header = BlockHeader {
            allocated_size: stored.len() as u64,
            used_size: stored.len() as u64,
            data_size: payload.len() as u64,
            ..Default::default()
        };
        header.set_compression(compression.name()).unwrap();
        if let Some(over) = checksum_over {
            header.checksum = md5_of(over);
        }

        let offset = buf.len() as u64;
        header.write(&mut buf);
        buf.extend_from_slice(&stored);
        buf.extend_from_slice(&write_block_index(&[offset]));
        buf
    }

    #[test]
    fn external_source_uris_may_not_escape_the_directory() {
        // The only shape the standard's exploded form uses.
        assert!(external_relative_path("exploded0000.asdf").is_ok());
        assert!(external_relative_path("data/block0.asdf").is_ok());
        assert!(external_relative_path("./here.asdf").is_ok());

        // Everything that could reach outside the referring file's tree.
        for bad in [
            "",
            "/etc/passwd",
            "../secrets.asdf",
            "data/../../secrets.asdf",
            "file:///etc/passwd",
            "https://example.invalid/x.asdf",
        ] {
            assert!(
                external_relative_path(bad).is_err(),
                "{bad:?} should be rejected as an external source"
            );
        }
    }

    #[test]
    fn a_memory_backed_file_resolves_no_external_sources() {
        let file = build(b"whatever", Compression::None, None);
        let r = Reader::from_bytes(file);
        let r = r.unwrap();
        assert!(r.path().is_none());
        // There is no directory to resolve against, so this must fail rather
        // than guess at the working directory.
        assert!(r.external_block("other.asdf").is_err());
    }

    #[test]
    fn an_external_source_is_read_from_the_neighbouring_file() {
        let dir = std::env::temp_dir().join(format!("asdf-exploded-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();

        // The data file: one block holding four little-endian int32s.
        let payload: Vec<u8> = [1i32, 2, 3, 4].iter().flat_map(|v| v.to_le_bytes()).collect();
        let data_file = dir.join("holder0000.asdf");
        std::fs::write(&data_file, build(&payload, Compression::None, None)).unwrap();

        // The referring file: a tree naming it, and no blocks of its own.
        let mut buf = Vec::new();
        buf.extend_from_slice(b"#ASDF 1.0.0\n#ASDF_STANDARD 1.6.0\n");
        buf.extend_from_slice(b"%YAML 1.1\n%TAG ! tag:stsci.edu:asdf/\n--- !core/asdf-1.1.0\n");
        buf.extend_from_slice(
            b"data: !core/ndarray-1.1.0\n  source: holder0000.asdf\n  \
              datatype: int32\n  byteorder: little\n  shape: [4]\n",
        );
        buf.extend_from_slice(b"...\n");
        let referring = dir.join("holder.asdf");
        std::fs::write(&referring, buf).unwrap();

        let r = Reader::open(&referring).unwrap();
        assert_eq!(r.block_count(), 0, "the referring file holds no blocks itself");
        assert_eq!(r.external_block("holder0000.asdf").unwrap(), payload);

        // And inlining follows the reference through to real values.
        let (doc, skipped) = r.tree_inlined().unwrap().unwrap();
        assert!(skipped.is_empty(), "nothing should be left un-inlined: {skipped:?}");
        let root = doc.root().unwrap();
        let array = doc.mapping_get(root, "data").unwrap();
        let values = doc.mapping_get(array, "data").unwrap();
        let items = doc.sequence_items(values).unwrap();
        let read: Vec<&str> = items.iter().map(|i| doc.resolved(*i).as_str().unwrap()).collect();
        assert_eq!(read, ["1", "2", "3", "4"]);
        // `source` is replaced, as it is for an internal block.
        assert!(doc.mapping_get(array, "source").is_none());

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn a_missing_external_file_is_reported_not_silently_skipped() {
        let dir =
            std::env::temp_dir().join(format!("asdf-exploded-missing-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let referring = dir.join("dangling.asdf");
        let mut buf = Vec::new();
        buf.extend_from_slice(b"#ASDF 1.0.0\n#ASDF_STANDARD 1.6.0\n");
        buf.extend_from_slice(b"%YAML 1.1\n%TAG ! tag:stsci.edu:asdf/\n--- !core/asdf-1.1.0\n");
        buf.extend_from_slice(
            b"data: !core/ndarray-1.1.0\n  source: nowhere0000.asdf\n  \
              datatype: int32\n  byteorder: little\n  shape: [4]\n",
        );
        buf.extend_from_slice(b"...\n");
        std::fs::write(&referring, buf).unwrap();

        let r = Reader::open(&referring).unwrap();
        let (_, skipped) = r.tree_inlined().unwrap().unwrap();
        assert_eq!(skipped.len(), 1);
        assert!(skipped[0].contains("nowhere0000.asdf"), "{skipped:?}");

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn reads_tree_and_block_data() {
        let payload = b"hello block data".to_vec();
        let file = build(&payload, Compression::None, None);
        let r = Reader::from_bytes(file).unwrap();

        assert_eq!(r.block_count(), 1);
        assert_eq!(&*r.block_data(0).unwrap(), &payload[..]);

        let doc = r.tree().unwrap().unwrap();
        let root = doc.root().unwrap();
        assert!(doc.mapping_get(root, "x").is_some());
    }

    #[test]
    fn uncompressed_data_is_borrowed_not_copied() {
        let file = build(b"borrow me", Compression::None, None);
        let r = Reader::from_bytes(file).unwrap();
        assert!(matches!(r.block_data(0).unwrap(), Cow::Borrowed(_)));
    }

    #[test]
    fn compressed_data_round_trips() {
        let payload = vec![7u8; 4096];
        for c in crate::compression::available() {
            let file = build(&payload, c, None);
            let r = Reader::from_bytes(file).unwrap();
            assert_eq!(r.block_compression(0).unwrap(), c);
            assert_eq!(&*r.block_data(0).unwrap(), &payload[..], "{c:?}");
            // The raw form is the compressed bytes.
            assert!(r.block_raw(0).unwrap().len() < payload.len(), "{c:?}");
        }
    }

    #[test]
    fn valid_checksums_verify() {
        let payload = b"checksum me".to_vec();
        let file = build(&payload, Compression::None, Some(&payload));
        let r = Reader::from_bytes(file).unwrap();
        let (status, _) = r.verify_block_checksum(0).unwrap();
        assert_eq!(status, ChecksumStatus::Valid);
    }

    #[test]
    fn invalid_checksums_are_reported() {
        let payload = b"checksum me".to_vec();
        let file = build(&payload, Compression::None, Some(b"something else"));
        let r = Reader::from_bytes(file).unwrap();
        let (status, computed) = r.verify_block_checksum(0).unwrap();
        assert_eq!(status, ChecksumStatus::Invalid);
        assert_eq!(computed, md5_of(&payload), "the digest of the real data is reported");
    }

    #[test]
    fn an_absent_checksum_is_not_a_failure() {
        let file = build(b"no checksum", Compression::None, None);
        let r = Reader::from_bytes(file).unwrap();
        let (status, _) = r.verify_block_checksum(0).unwrap();
        assert_eq!(status, ChecksumStatus::Absent);
        assert!(!status.is_failure());
    }

    #[cfg(feature = "zlib")]
    #[test]
    fn compressed_checksums_cover_the_stored_bytes() {
        // What the specification means: the digest is over the data as stored.
        let payload = vec![3u8; 2048];
        let stored = Compression::Zlib.compress(&payload).unwrap();
        let file = build(&payload, Compression::Zlib, Some(&stored));
        let r = Reader::from_bytes(file).unwrap();
        assert_eq!(r.verify_block_checksum(0).unwrap().0, ChecksumStatus::Valid);
    }

    /// Python asdf 5.x and earlier checksum the *uncompressed* data for a
    /// compressed block. libasdf detects those writers from `asdf_library`
    /// and verifies against the decompressed bytes instead; so do we.
    #[cfg(feature = "zlib")]
    #[test]
    fn the_python_checksum_bug_is_worked_around() {
        let payload = vec![9u8; 2048];
        let stored = Compression::Zlib.compress(&payload).unwrap();

        let make = |library_version: &str| {
            let mut buf = Vec::new();
            buf.extend_from_slice(b"#ASDF 1.0.0\n#ASDF_STANDARD 1.6.0\n");
            buf.extend_from_slice(b"%YAML 1.1\n%TAG ! tag:stsci.edu:asdf/\n--- !core/asdf-1.1.0\n");
            buf.extend_from_slice(
                format!(
                    "asdf_library: !core/software-1.0.0 {{name: asdf, version: {library_version}}}\n"
                )
                .as_bytes(),
            );
            buf.extend_from_slice(b"...\n");

            let mut header = BlockHeader {
                allocated_size: stored.len() as u64,
                used_size: stored.len() as u64,
                data_size: payload.len() as u64,
                // The bug: digest taken over the *uncompressed* payload.
                checksum: md5_of(&payload),
                ..Default::default()
            };
            header.set_compression("zlib").unwrap();
            header.write(&mut buf);
            buf.extend_from_slice(&stored);
            buf
        };

        // A writer known to be affected: accepted via the workaround.
        let r = Reader::from_bytes(make("4.1.0")).unwrap();
        assert!(r.has_python_checksum_bug());
        assert_eq!(
            r.verify_block_checksum(0).unwrap().0,
            ChecksumStatus::Valid,
            "an affected writer's checksum should verify against the uncompressed data"
        );

        // A writer past the fix: the same file is genuinely invalid.
        let r = Reader::from_bytes(make("6.0.0")).unwrap();
        assert!(!r.has_python_checksum_bug());
        assert_eq!(
            r.verify_block_checksum(0).unwrap().0,
            ChecksumStatus::Invalid,
            "the workaround must not apply to writers that are not affected"
        );
    }

    #[test]
    fn out_of_range_block_indices_error() {
        let file = build(b"one block", Compression::None, None);
        let r = Reader::from_bytes(file).unwrap();
        assert!(r.block(1).is_err());
        assert!(r.block_data(99).is_err());
    }

    #[test]
    fn a_file_without_a_tree_reads_cleanly() {
        let mut buf = Vec::new();
        buf.extend_from_slice(b"#ASDF 1.0.0\n#ASDF_STANDARD 1.6.0\n");
        let header =
            BlockHeader { allocated_size: 4, used_size: 4, data_size: 4, ..Default::default() };
        header.write(&mut buf);
        buf.extend_from_slice(b"data");

        let r = Reader::from_bytes(buf).unwrap();
        assert!(r.tree().unwrap().is_none());
        assert_eq!(&*r.block_data(0).unwrap(), b"data");
    }
}