# Test Case: DEFLATE and ZSTD Partial References

**Test ID**: TC143-deflate-zstd-partial-reference

**G5 fixture contract**: Use [G5-MANUAL-SETUP.md](G5-MANUAL-SETUP.md).
Its generated paths, source comparisons, structured corruption offsets, and
cleanup rules supersede placeholder examples in this file.
**Category**: Compression
**Priority**: Critical
**Regression**: Non-LZ4 partial-reference decoding

## Objective
Verify full and partial-reference decoding for raw DEFLATE and ZSTD streams,
including exact full-file SHA256, cross-extent reads, random reads, and
corrupted-stream `EIO` behavior.

## Preconditions
- FreeBSD 15 guest.
- ZSTDIO-enabled repo22 module for the ZSTD cases.
- Deterministic full-index fixtures whose layout parser proves:
  - the full file points at a complete compressed stream;
  - the partial file reuses that physical stream;
  - the reused HEAD record has `Z_EROFS_LI_PARTIAL_REF` (`0x8000`) set;
  - the reused first NONHEAD record has the complete source pcluster's
    `D0_CBLKCNT`, not the partial file's original compressed-block count;
  - the partial file's logical length is shorter than the source stream's
    decompressed length.
- Corrupted copies produced only after locating the target compressed extent;
  do not use a blind image offset.

## Layout Proof
```sh
dump.erofs -s deflate-partial-ref.erofs
dump.erofs --path=/a.dat -e deflate-partial-ref.erofs
dump.erofs --path=/b.dat -e deflate-partial-ref.erofs
dump.erofs -s zstd-partial-ref.erofs
dump.erofs --path=/a.dat -e zstd-partial-ref.erofs
dump.erofs --path=/b.dat -e zstd-partial-ref.erofs
```

The byte-level fixture record must assert the original NID, map-header offset,
HEAD record, pblk, and checksum before setting the partial-reference bit or
redirecting a pblk.

## Test Steps
1. Mount the DEFLATE image and hash `/a.dat` and `/b.dat` completely.
2. Read a range crossing a known DEFLATE logical extent boundary.
3. Read another non-aligned random range and compare it byte-for-byte with the
   source.
4. Repeat steps 1-3 for ZSTD.
5. Mount each corrupted copy and read the targeted file through a small C
   errno probe.
6. Compare pre/post `dmesg` and active `vmstat -m` EROFS allocations.

The G5 DEFLATE partial is 100000 bytes because erofs-utils 1.8.6 splits the
1 MiB DEFLATE source according to the 32 KiB DEFLATE window. It reuses the
first 17-block source pcluster and uses offsets 65500/2048 and 90000/4096.
The ZSTD partial is 700000 bytes and uses these checks:

```sh
dd if=/mnt/repo22/b.dat of=/tmp/guest bs=1 skip=122900 count=512 status=none
dd if=source-b.dat of=/tmp/source bs=1 skip=122900 count=512 status=none
cmp /tmp/guest /tmp/source

dd if=/mnt/repo22/b.dat of=/tmp/guest bs=1 skip=524287 count=4097 status=none
dd if=source-prefix-b.dat of=/tmp/source bs=1 skip=524287 count=4097 status=none
cmp /tmp/guest /tmp/source
```

## Expected Results
- Full DEFLATE and ZSTD streams require complete output and stream completion.
- Partial references succeed after producing the requested logical prefix;
  they do not require the reused source frame to finish.
- Complete SHA256 and every boundary/random byte comparison match.
- Targeted corruption returns read errno 5 (`EIO`), with no leaked active
  allocation, panic, or trap.
- `control.bin` in each corrupted image remains byte-identical to its source.

## Cleanup
Unmount each image, detach every md unit, remove temporary range outputs, and
unload the module by its `kldstat` ID.
