isomage 2.1.0

Pure-Rust reader for ISO 9660, UDF, FAT, ext2/3/4, NTFS, HFS+, SquashFS, ZIP, TAR, and more. No unsafe, no runtime deps.
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
//! VHD (Virtual Hard Disk) container reader (`vhd` feature).
//!
//! VHD is Microsoft's virtual disk format, documented in the
//! "Virtual Hard Disk Image Format Specification" (Microsoft Open
//! Specification). This reader handles:
//!
//! - **Fixed VHDs**: virtual disk data starts at byte 0; the footer
//!   is the last 512 bytes of the file. Reported as a single extent
//!   with `file_location = Some(0)`.
//! - **Dynamic VHDs**: footer *copy* at byte 0 (the authoritative footer
//!   is the last 512 bytes), dynamic disk header at byte 512, Block
//!   Allocation Table follows. Data is fragmented across BAT-indexed
//!   blocks. Reported with `file_location = None`.
//!
//! **Differencing VHDs** (disk_type 4) are not supported and return
//! `Error::UnsupportedType(4)`. Parent resolution requires traversing
//! a locator chain that complicates the reader significantly and is
//! deferred to a later PR.
//!
//! ## Footer layout (512 bytes, big-endian multi-byte integers)
//!
//! ```text
//!  [0]   u8[8]  cookie          = b"conectix"
//!  [8]   u32    features
//! [12]   u32    file_format_version
//! [16]   u64    data_offset     // 0xFFFFFFFFFFFFFFFF for Fixed; 512 for Dynamic
//! [24]   u32    timestamp
//! [28]   u8[4]  creator_application
//! [32]   u32    creator_version
//! [36]   u8[4]  creator_host_os
//! [40]   u64    original_size
//! [48]   u64    current_size
//! [56]   u32    disk_geometry
//! [60]   u32    disk_type       // 2=Fixed, 3=Dynamic, 4=Differencing
//! [64]   u32    checksum        // 1's-complement of all bytes (checksum field = 0)
//! [68]   u8[16] unique_id
//! [84]   u8     saved_state
//! [85]   u8[427] reserved
//! ```
//!
//! ## Dynamic Disk Header layout (1024 bytes at offset 512)
//!
//! ```text
//!  [0]   u8[8]  cookie          = b"cxsparse"
//!  [8]   u64    data_offset     // reserved, 0xFFFFFFFFFFFFFFFF
//! [16]   u64    table_offset    // byte offset of the BAT
//! [24]   u32    header_version  // 0x00010000
//! [28]   u32    max_table_entries
//! [32]   u32    block_size      // default 0x200000 (2 MB)
//! [36]   u32    checksum
//! ```

use std::io::{self, Read, Seek, SeekFrom};

use crate::tree::TreeNode;

/// Magic bytes in a VHD footer at offset 0.
const FOOTER_COOKIE: &[u8; 8] = b"conectix";

/// Magic bytes in a Dynamic Disk Header at offset 0.
const DYN_HEADER_COOKIE: &[u8; 8] = b"cxsparse";

/// VHD footer size in bytes.
const FOOTER_SIZE: u64 = 512;

/// disk_type = 2: Fixed VHD.
const DISK_TYPE_FIXED: u32 = 2;

/// disk_type = 3: Dynamic VHD.
const DISK_TYPE_DYNAMIC: u32 = 3;

/// Sentinel BAT entry meaning "block not allocated".
/// Used in tests to build minimal Dynamic VHD images.
#[cfg(test)]
const BAT_ENTRY_UNUSED: u32 = 0xFFFF_FFFF;

// ── Error type ────────────────────────────────────────────────────────────────

/// Reasons `detect` or `detect_and_parse` can fail.
#[derive(Debug)]
pub enum Error {
    /// File is shorter than one VHD footer (512 bytes).
    TooShort,
    /// Footer cookie is not `b"conectix"`.
    BadMagic,
    /// Footer checksum does not match the computed 1's-complement sum.
    BadChecksum,
    /// `disk_type` is not 2 (Fixed) or 3 (Dynamic). The value is included
    /// for diagnostic purposes.
    UnsupportedType(u32),
    /// Dynamic Disk Header cookie is not `b"cxsparse"`.
    BadDynamicHeader,
    /// Underlying I/O error.
    Io(io::Error),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::TooShort => write!(f, "VHD file is shorter than 512 bytes (no footer)"),
            Error::BadMagic => write!(f, "VHD footer cookie b\"conectix\" not found"),
            Error::BadChecksum => write!(f, "VHD footer checksum mismatch"),
            Error::UnsupportedType(t) => write!(
                f,
                "VHD disk_type {t} is not supported (only Fixed=2, Dynamic=3)"
            ),
            Error::BadDynamicHeader => {
                write!(f, "VHD Dynamic Disk Header cookie b\"cxsparse\" not found")
            }
            Error::Io(e) => write!(f, "VHD I/O error: {e}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(e) => Some(e),
            _ => None,
        }
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Io(e)
    }
}

// ── Footer ────────────────────────────────────────────────────────────────────

/// Parsed VHD footer fields we actually use.
struct Footer {
    /// `data_offset` field: 0xFFFF_FFFF_FFFF_FFFF for Fixed, 512 for Dynamic.
    data_offset: u64,
    /// `current_size`: virtual disk size in bytes.
    current_size: u64,
    /// `disk_type`: 2=Fixed, 3=Dynamic, 4=Differencing, …
    disk_type: u32,
}

/// Compute the 1's-complement checksum of a 512-byte footer buffer,
/// with the checksum field (bytes 64..68) treated as zero.
///
/// From the VHD spec: sum all bytes, treating the checksum bytes as 0,
/// then take the 1's complement (bitwise NOT) of the low 32 bits.
fn verify_checksum(buf: &[u8; 512]) -> bool {
    let stored = u32::from_be_bytes([buf[64], buf[65], buf[66], buf[67]]);

    let mut sum: u32 = 0;
    for (i, &b) in buf.iter().enumerate() {
        if (64..68).contains(&i) {
            // Treat checksum field as 0 during computation.
            continue;
        }
        sum = sum.wrapping_add(b as u32);
    }
    let computed = !sum;
    computed == stored
}

/// Read and validate the VHD footer at `offset` from the start of the file.
fn read_footer<R: Read + Seek>(r: &mut R, offset: u64) -> Result<Footer, Error> {
    r.seek(SeekFrom::Start(offset))?;
    let mut buf = [0u8; 512];
    r.read_exact(&mut buf).map_err(|e| {
        if e.kind() == io::ErrorKind::UnexpectedEof {
            Error::TooShort
        } else {
            Error::Io(e)
        }
    })?;

    // Check magic.
    if &buf[0..8] != FOOTER_COOKIE {
        return Err(Error::BadMagic);
    }

    // Verify 1's-complement checksum.
    if !verify_checksum(&buf) {
        return Err(Error::BadChecksum);
    }

    let data_offset = u64::from_be_bytes([
        buf[16], buf[17], buf[18], buf[19], buf[20], buf[21], buf[22], buf[23],
    ]);
    let current_size = u64::from_be_bytes([
        buf[48], buf[49], buf[50], buf[51], buf[52], buf[53], buf[54], buf[55],
    ]);
    let disk_type = u32::from_be_bytes([buf[60], buf[61], buf[62], buf[63]]);

    Ok(Footer {
        data_offset,
        current_size,
        disk_type,
    })
}

// ── Detection ─────────────────────────────────────────────────────────────────

/// Detect whether `r` is a VHD image by checking the footer magic.
///
/// Seeks to `file_end - 512`, reads 8 bytes, checks for `b"conectix"`.
/// Restores the stream position on both success and error paths.
///
/// Returns `Ok(())` if detection succeeds.
pub fn detect<R: Read + Seek>(r: &mut R) -> Result<(), Error> {
    let pos = r.stream_position()?;
    let result = detect_inner(r);
    let _ = r.seek(SeekFrom::Start(pos));
    result
}

fn detect_inner<R: Read + Seek>(r: &mut R) -> Result<(), Error> {
    // Find file length.
    let file_len = r.seek(SeekFrom::End(0))?;
    if file_len < FOOTER_SIZE {
        return Err(Error::TooShort);
    }

    // Seek to footer.
    let footer_offset = file_len - FOOTER_SIZE;
    r.seek(SeekFrom::Start(footer_offset))?;

    // Read only the 8-byte cookie for detection.
    let mut cookie = [0u8; 8];
    match r.read_exact(&mut cookie) {
        Ok(()) => {}
        Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => return Err(Error::TooShort),
        Err(e) => return Err(Error::Io(e)),
    }

    if &cookie != FOOTER_COOKIE {
        return Err(Error::BadMagic);
    }

    Ok(())
}

// ── Tree building ─────────────────────────────────────────────────────────────

/// Parse the VHD at `r` and return a [`TreeNode`] tree.
///
/// The tree always has the shape:
///
/// ```text
/// / (dir)
/// └── disk.img (file, size = virtual disk size)
/// ```
///
/// For Fixed VHDs, `disk.img` has `file_location = Some(0)` because
/// the raw disk data occupies bytes `0..current_size` of the file.
///
/// For Dynamic VHDs, `disk.img` has `file_location = None` because
/// data is scattered across BAT-indexed blocks. The virtual size is
/// still reported correctly in `file_length`.
pub fn detect_and_parse<R: Read + Seek>(r: &mut R) -> Result<TreeNode, Error> {
    // Find file length and read footer from the end.
    let file_len = r.seek(SeekFrom::End(0))?;
    if file_len < FOOTER_SIZE {
        return Err(Error::TooShort);
    }
    let footer_offset = file_len - FOOTER_SIZE;
    let footer = read_footer(r, footer_offset)?;

    match footer.disk_type {
        DISK_TYPE_FIXED => parse_fixed(footer.current_size, file_len),
        DISK_TYPE_DYNAMIC => parse_dynamic(r, &footer, file_len),
        other => Err(Error::UnsupportedType(other)),
    }
}

/// Build the tree for a Fixed VHD. Data occupies bytes 0..current_size.
fn parse_fixed(current_size: u64, file_len: u64) -> Result<TreeNode, Error> {
    // Validate that current_size doesn't exceed the available data region.
    // A fixed VHD is: [disk data: current_size bytes] [footer: 512 bytes].
    let data_region = file_len.saturating_sub(FOOTER_SIZE);
    if current_size > data_region {
        return Err(Error::TooShort);
    }

    let mut root = TreeNode::new_directory("/".to_string());

    // For a Fixed VHD the raw disk data starts at byte 0 and is
    // current_size bytes long, followed immediately by the 512-byte footer.
    let disk_node =
        TreeNode::new_file_with_location("disk.img".to_string(), current_size, 0, current_size);

    root.add_child(disk_node);
    root.calculate_directory_size();
    Ok(root)
}

/// Build the tree for a Dynamic VHD. Data is fragmented across BAT blocks;
/// we report the virtual size but set `file_location = None`.
fn parse_dynamic<R: Read + Seek>(
    r: &mut R,
    footer: &Footer,
    file_len: u64,
) -> Result<TreeNode, Error> {
    // The Dynamic Disk Header lives at data_offset (= 512 for standard VHDs).
    // Validate data_offset before seeking to avoid seeking past EOF on corrupt images.
    let dyn_header_offset = footer.data_offset;
    if dyn_header_offset > file_len.saturating_sub(1024) {
        return Err(Error::TooShort);
    }

    // Read 1024 bytes of the Dynamic Disk Header.
    r.seek(SeekFrom::Start(dyn_header_offset))?;
    let mut hdr = [0u8; 1024];
    match r.read_exact(&mut hdr) {
        Ok(()) => {}
        Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => return Err(Error::TooShort),
        Err(e) => return Err(Error::Io(e)),
    }

    // Verify the Dynamic Disk Header cookie.
    if &hdr[0..8] != DYN_HEADER_COOKIE {
        return Err(Error::BadDynamicHeader);
    }

    // For Dynamic VHDs we report the virtual size without mapping blocks.
    let current_size = footer.current_size;

    let mut root = TreeNode::new_directory("/".to_string());

    // file_location = None: data is scattered across BAT blocks.
    let mut disk_node = TreeNode::new_file("disk.img".to_string(), current_size);
    disk_node.file_length = Some(current_size);

    root.add_child(disk_node);
    root.calculate_directory_size();
    Ok(root)
}

// ── Unit tests ────────────────────────────────────────────────────────────────

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

    // ── Minimal Fixed VHD builder ─────────────────────────────────────────
    //
    // Layout:
    //   [0..512]     512 bytes of "disk data" (all zeros)
    //   [512..1024]  512-byte Fixed VHD footer
    //
    // This is a degenerate 512-byte Fixed VHD — just enough to satisfy
    // the parser. Real Fixed VHDs have current_size = file_size - 512.

    fn build_footer(disk_type: u32, current_size: u64, data_offset: u64) -> [u8; 512] {
        let mut buf = [0u8; 512];

        // Cookie.
        buf[0..8].copy_from_slice(FOOTER_COOKIE);

        // features = 0x00000002 (reserved bit must be set per spec).
        buf[8..12].copy_from_slice(&2u32.to_be_bytes());

        // file_format_version = 0x00010000.
        buf[12..16].copy_from_slice(&0x0001_0000u32.to_be_bytes());

        // data_offset.
        buf[16..24].copy_from_slice(&data_offset.to_be_bytes());

        // timestamp = 0.
        buf[24..28].copy_from_slice(&0u32.to_be_bytes());

        // creator_application = "test".
        buf[28..32].copy_from_slice(b"test");

        // creator_version = 0.
        buf[32..36].copy_from_slice(&0u32.to_be_bytes());

        // creator_host_os = "Wi2k".
        buf[36..40].copy_from_slice(b"Wi2k");

        // original_size.
        buf[40..48].copy_from_slice(&current_size.to_be_bytes());

        // current_size.
        buf[48..56].copy_from_slice(&current_size.to_be_bytes());

        // disk_geometry = 0 (we don't validate it).
        buf[56..60].copy_from_slice(&0u32.to_be_bytes());

        // disk_type.
        buf[60..64].copy_from_slice(&disk_type.to_be_bytes());

        // Compute and insert checksum (checksum field = 0 during computation).
        // buf[64..68] is already 0.
        let mut sum: u32 = 0;
        for &b in buf.iter() {
            sum = sum.wrapping_add(b as u32);
        }
        let checksum: u32 = !sum;
        buf[64..68].copy_from_slice(&checksum.to_be_bytes());

        buf
    }

    /// Build a minimal Fixed VHD: 512 bytes of data + 512-byte footer.
    fn build_fixed_vhd(data_size: u64) -> Vec<u8> {
        let data_offset = 0xFFFF_FFFF_FFFF_FFFFu64; // Fixed VHD sentinel.
        let footer = build_footer(DISK_TYPE_FIXED, data_size, data_offset);
        let mut image = vec![0u8; data_size as usize];
        image.extend_from_slice(&footer);
        image
    }

    /// Build a minimal Dynamic VHD: footer-copy at 0, dynamic disk header at 512.
    fn build_dynamic_vhd(virtual_size: u64) -> Vec<u8> {
        // data_offset = 512 (offset of Dynamic Disk Header).
        let footer = build_footer(DISK_TYPE_DYNAMIC, virtual_size, 512);

        // Dynamic Disk Header (1024 bytes).
        let mut dyn_hdr = [0u8; 1024];
        dyn_hdr[0..8].copy_from_slice(DYN_HEADER_COOKIE);

        // data_offset (reserved): 0xFFFFFFFFFFFFFFFF.
        dyn_hdr[8..16].copy_from_slice(&0xFFFF_FFFF_FFFF_FFFFu64.to_be_bytes());

        // table_offset: BAT starts at 512 + 1024 = 1536.
        let table_offset: u64 = 512 + 1024;
        dyn_hdr[16..24].copy_from_slice(&table_offset.to_be_bytes());

        // header_version = 0x00010000.
        dyn_hdr[24..28].copy_from_slice(&0x0001_0000u32.to_be_bytes());

        // max_table_entries: ceil(virtual_size / block_size).
        let block_size: u32 = 0x0020_0000; // 2 MB default.
        let max_table_entries = virtual_size.div_ceil(block_size as u64) as u32;
        dyn_hdr[28..32].copy_from_slice(&max_table_entries.to_be_bytes());

        // block_size.
        dyn_hdr[32..36].copy_from_slice(&block_size.to_be_bytes());

        // Checksum for dyn header (field at [36..40], compute over [0..1024]).
        // buf[36..40] already 0.
        let mut sum: u32 = 0;
        for &b in dyn_hdr.iter() {
            sum = sum.wrapping_add(b as u32);
        }
        let hdr_checksum: u32 = !sum;
        dyn_hdr[36..40].copy_from_slice(&hdr_checksum.to_be_bytes());

        // BAT: all entries = 0xFFFFFFFF (no blocks allocated).
        let bat_entries = max_table_entries as usize;
        let mut bat = vec![0xFFu8; bat_entries * 4];
        // Ensure all entries are 0xFFFFFFFF.
        for i in 0..bat_entries {
            bat[i * 4..i * 4 + 4].copy_from_slice(&BAT_ENTRY_UNUSED.to_be_bytes());
        }

        // Layout: footer (512) + dyn_header (1024) + BAT + footer-copy at end.
        let mut image: Vec<u8> = Vec::new();
        image.extend_from_slice(&footer); // Copy of footer at byte 0.
        image.extend_from_slice(&dyn_hdr);
        image.extend_from_slice(&bat);
        image.extend_from_slice(&footer); // Footer at end.
        image
    }

    // ── Detection tests ───────────────────────────────────────────────────

    #[test]
    fn detect_fixed_vhd_ok() {
        let img = build_fixed_vhd(512);
        let mut c = Cursor::new(&img);
        assert!(detect(&mut c).is_ok(), "should detect fixed VHD");
    }

    #[test]
    fn detect_dynamic_vhd_ok() {
        let img = build_dynamic_vhd(2 * 1024 * 1024);
        let mut c = Cursor::new(&img);
        assert!(detect(&mut c).is_ok(), "should detect dynamic VHD");
    }

    #[test]
    fn detect_restores_position() {
        let img = build_fixed_vhd(512);
        let mut c = Cursor::new(&img);
        c.seek(SeekFrom::Start(7)).unwrap();
        detect(&mut c).unwrap();
        assert_eq!(
            c.stream_position().unwrap(),
            7,
            "detect() must restore stream position"
        );
    }

    #[test]
    fn detect_rejects_bad_magic() {
        let img = vec![0u8; 1024];
        let mut c = Cursor::new(&img);
        assert!(
            matches!(detect(&mut c), Err(Error::BadMagic)),
            "all-zeros should fail with BadMagic"
        );
    }

    #[test]
    fn detect_rejects_too_short() {
        let img = vec![0u8; 256];
        let mut c = Cursor::new(&img);
        assert!(
            matches!(detect(&mut c), Err(Error::TooShort)),
            "256-byte image should fail with TooShort"
        );
    }

    // ── Checksum tests ────────────────────────────────────────────────────

    #[test]
    fn checksum_valid_footer_passes() {
        let img = build_fixed_vhd(512);
        // Footer is the last 512 bytes.
        let footer_slice: &[u8; 512] = img[img.len() - 512..].try_into().unwrap();
        assert!(
            verify_checksum(footer_slice),
            "freshly built footer checksum should pass"
        );
    }

    #[test]
    fn checksum_corrupted_footer_fails() {
        let img = build_fixed_vhd(512);
        let mut patched = img.clone();
        let footer_start = patched.len() - 512;
        patched[footer_start + 10] ^= 0xFF; // Flip bits in the features field (byte 10).
        let footer_slice: &[u8; 512] = patched[footer_start..].try_into().unwrap();
        assert!(
            !verify_checksum(footer_slice),
            "corrupted footer should fail checksum"
        );
    }

    #[test]
    fn bad_checksum_returns_error() {
        let mut img = build_fixed_vhd(512);
        // Corrupt checksum bytes in the footer (bytes 64..68 from footer start).
        let footer_start = img.len() - 512;
        img[footer_start + 64] ^= 0xFF;
        let mut c = Cursor::new(&img);
        // detect_and_parse calls read_footer which checks the checksum.
        let result = detect_and_parse(&mut c);
        assert!(
            matches!(result, Err(Error::BadChecksum)),
            "corrupted checksum should yield BadChecksum"
        );
    }

    // ── Parse tests ───────────────────────────────────────────────────────

    #[test]
    fn fixed_vhd_tree_shape() {
        let img = build_fixed_vhd(512);
        let mut c = Cursor::new(&img);
        let root = detect_and_parse(&mut c).expect("parse fixed VHD");
        assert_eq!(root.name, "/");
        assert!(root.is_directory);
        assert_eq!(root.children.len(), 1);
        let child = &root.children[0];
        assert_eq!(child.name, "disk.img");
        assert!(!child.is_directory);
    }

    #[test]
    fn fixed_vhd_file_location_is_zero() {
        let data_size: u64 = 512;
        let img = build_fixed_vhd(data_size);
        let mut c = Cursor::new(&img);
        let root = detect_and_parse(&mut c).expect("parse fixed VHD");
        let disk = &root.children[0];
        assert_eq!(
            disk.file_location,
            Some(0),
            "fixed VHD disk.img should have file_location=Some(0)"
        );
        assert_eq!(
            disk.file_length,
            Some(data_size),
            "fixed VHD disk.img should have file_length=Some(current_size)"
        );
        assert_eq!(disk.size, data_size);
    }

    #[test]
    fn dynamic_vhd_tree_shape() {
        let virtual_size: u64 = 10 * 1024 * 1024; // 10 MB
        let img = build_dynamic_vhd(virtual_size);
        let mut c = Cursor::new(&img);
        let root = detect_and_parse(&mut c).expect("parse dynamic VHD");
        assert_eq!(root.name, "/");
        assert!(root.is_directory);
        assert_eq!(root.children.len(), 1);
        let child = &root.children[0];
        assert_eq!(child.name, "disk.img");
        assert!(!child.is_directory);
    }

    #[test]
    fn dynamic_vhd_no_file_location() {
        let virtual_size: u64 = 10 * 1024 * 1024;
        let img = build_dynamic_vhd(virtual_size);
        let mut c = Cursor::new(&img);
        let root = detect_and_parse(&mut c).expect("parse dynamic VHD");
        let disk = &root.children[0];
        assert_eq!(
            disk.file_location, None,
            "dynamic VHD disk.img should have file_location=None (fragmented BAT)"
        );
        assert_eq!(
            disk.file_length,
            Some(virtual_size),
            "dynamic VHD disk.img should report virtual size in file_length"
        );
    }

    #[test]
    fn unsupported_differencing_type_returns_error() {
        // disk_type = 4 (Differencing).
        const DISK_TYPE_DIFFERENCING: u32 = 4;
        let data_offset = 512u64;
        let footer = build_footer(DISK_TYPE_DIFFERENCING, 1024 * 1024, data_offset);

        // Build a minimal image: 512 bytes data + footer at end.
        let mut img = vec![0u8; 512];
        img.extend_from_slice(&footer);

        let mut c = Cursor::new(&img);
        let result = detect_and_parse(&mut c);
        assert!(
            matches!(result, Err(Error::UnsupportedType(4))),
            "differencing VHD should return UnsupportedType(4)"
        );
    }

    // ── Error Display / source ────────────────────────────────────────────────

    #[test]
    fn error_display_too_short() {
        let msg = format!("{}", Error::TooShort);
        assert!(msg.contains("512") || msg.contains("short"), "got: {msg}");
    }

    #[test]
    fn error_display_bad_magic() {
        let msg = format!("{}", Error::BadMagic);
        assert!(
            msg.contains("conectix") || msg.contains("cookie"),
            "got: {msg}"
        );
    }

    #[test]
    fn error_display_bad_checksum() {
        let msg = format!("{}", Error::BadChecksum);
        assert!(
            msg.contains("checksum") || msg.contains("sum"),
            "got: {msg}"
        );
    }

    #[test]
    fn error_display_unsupported_type() {
        let msg = format!("{}", Error::UnsupportedType(5));
        assert!(msg.contains('5'), "got: {msg}");
    }

    #[test]
    fn error_display_bad_dynamic_header() {
        let msg = format!("{}", Error::BadDynamicHeader);
        assert!(
            msg.contains("cxsparse") || msg.contains("dynamic"),
            "got: {msg}"
        );
    }

    #[test]
    fn error_display_io() {
        let io = io::Error::other("disk");
        let msg = format!("{}", Error::Io(io));
        assert!(msg.contains("disk"), "got: {msg}");
    }

    #[test]
    fn error_source_io() {
        use std::error::Error as StdError;
        assert!(Error::Io(io::Error::other("s")).source().is_some());
    }

    #[test]
    fn error_source_non_io() {
        use std::error::Error as StdError;
        assert!(Error::TooShort.source().is_none());
        assert!(Error::BadMagic.source().is_none());
        assert!(Error::BadChecksum.source().is_none());
        assert!(Error::UnsupportedType(2).source().is_none());
        assert!(Error::BadDynamicHeader.source().is_none());
    }

    #[test]
    fn error_from_io_error() {
        let io = io::Error::other("disk read failed");
        let e = Error::from(io);
        assert!(matches!(e, Error::Io(_)));
    }

    #[test]
    fn read_footer_too_short_returns_error() {
        // 200 bytes: seek to 0 succeeds, but read_exact(512) gets UnexpectedEof → TooShort.
        let data = vec![0u8; 200];
        let mut c = Cursor::new(data);
        assert!(matches!(read_footer(&mut c, 0), Err(Error::TooShort)));
    }

    #[test]
    fn read_footer_bad_magic_returns_error() {
        // 512 bytes of zeros: cookie is all-zeros, not b"conectix" → BadMagic.
        let data = vec![0u8; 512];
        let mut c = Cursor::new(data);
        assert!(matches!(read_footer(&mut c, 0), Err(Error::BadMagic)));
    }

    #[test]
    fn parse_fixed_current_size_exceeds_data_region_returns_error() {
        // current_size=1000 on a 512-byte file: data_region = 512 - 512 = 0; 1000 > 0 → TooShort.
        let footer = build_footer(DISK_TYPE_FIXED, 1000, 0xFFFF_FFFF_FFFF_FFFFu64);
        let mut img = vec![0u8; 0];
        img.extend_from_slice(&footer);
        let mut c = Cursor::new(&img);
        assert!(matches!(detect_and_parse(&mut c), Err(Error::TooShort)));
    }

    #[test]
    fn parse_dynamic_data_offset_too_large_returns_error() {
        // Build dynamic footer with data_offset = 0xFFFF_FFFF_FFFF_FFFF (Fixed sentinel) —
        // dyn_header_offset = 0xFFFF_FFFF_FFFF_FFFF, which exceeds file_len - 1024 → TooShort.
        let footer = build_footer(DISK_TYPE_DYNAMIC, 1024 * 1024, 0xFFFF_FFFF_FFFF_FFFFu64);
        let mut img = vec![0u8; 512];
        img.extend_from_slice(&footer);
        let mut c = Cursor::new(&img);
        assert!(matches!(detect_and_parse(&mut c), Err(Error::TooShort)));
    }

    #[test]
    fn parse_dynamic_bad_dyn_header_cookie_returns_error() {
        // Valid dynamic footer at data_offset=512, but dyn header is all-zeros (wrong cookie).
        let footer = build_footer(DISK_TYPE_DYNAMIC, 1024 * 1024, 512);
        let dyn_hdr = [0u8; 1024]; // wrong cookie: not b"cxsparse"
        let mut img: Vec<u8> = Vec::new();
        img.extend_from_slice(&footer); // copy at byte 0
        img.extend_from_slice(&dyn_hdr); // dyn header at 512
        img.extend_from_slice(&footer); // authoritative footer at end
        let mut c = Cursor::new(&img);
        assert!(matches!(
            detect_and_parse(&mut c),
            Err(Error::BadDynamicHeader)
        ));
    }
}