From b9c737eb37a63d66f7616f77935944f15eda3c87 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 16 Oct 2023 22:31:46 +1100 Subject: [PATCH] litonlylzma: add TestUsrBinFoocatCompat --- lib/litonlylzma/litonlylzma.go | 12 +++++++ lib/litonlylzma/litonlylzma_test.go | 54 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/lib/litonlylzma/litonlylzma.go b/lib/litonlylzma/litonlylzma.go index 591c2ed6..5d3b40f9 100644 --- a/lib/litonlylzma/litonlylzma.go +++ b/lib/litonlylzma/litonlylzma.go @@ -655,6 +655,18 @@ func encodeXz(dst []byte, src []byte) (appendedDst []byte, retErr error) { srcChunk, remaining = remaining, nil } + // Emit 3 or 6 bytes of LZMA2 header. The LZMA2 file format also allows + // for chunks to depend on previous chunks. But, as said above, we + // produce independent chunks (favoring simplicity over maximizing + // compression ratio). We emit one of: + // + // 00000001 U U - Uncompressed, reset dic, need reset state and set new prop + // 111uuuuu U U P P S - LZMA, reset state + set new prop, reset dic + // + // "U U" or "uuuuu U U" is a 16-bit or 21-bit (unpacked_size - 1), "P + // P" is a uint16_t (packed_size - 1) and S holds the LZMA properties. + // + // https://github.com/jljusten/LZMA-SDK/blob/781863cdf592da3e97420f50de5dac056ad352a5/C/Lzma2Dec.c#L17-L23 rawLZMA = encodeRaw(rawLZMA[:0], srcChunk) if (len(srcChunk) + 3) <= (len(rawLZMA) + 6) { dst = append(dst, diff --git a/lib/litonlylzma/litonlylzma_test.go b/lib/litonlylzma/litonlylzma_test.go index cfd3dc99..ebca4ccc 100644 --- a/lib/litonlylzma/litonlylzma_test.go +++ b/lib/litonlylzma/litonlylzma_test.go @@ -13,6 +13,7 @@ package litonlylzma import ( "bytes" "os" + "os/exec" "testing" ) @@ -65,3 +66,56 @@ func TestRoundTripOneThousand0x00s(tt *testing.T) { testRoundTrip(tt, oneThousan func TestRoundTripOneThousand0xFFs(tt *testing.T) { testRoundTrip(tt, oneThousand0xFFs) } func TestRoundTripPiTxt(tt *testing.T) { testRoundTrip(tt, "pi.txt") } func TestRoundTripRomeoTxt(tt *testing.T) { testRoundTrip(tt, "romeo.txt") } + +func TestUsrBinFoocatCompat(tt *testing.T) { + testCases := [...]struct { + fileFormat FileFormat + command string + }{ + {FileFormatLZMA, "/usr/bin/lzcat"}, + {FileFormatXz, "/usr/bin/xzcat"}, + } + + // Not every system has "/usr/bin/{lzcat,xzcat}" commands. + for _, tc := range testCases { + if _, err := os.Stat(tc.command); err != nil { + tt.Skipf("%v", err) + } + } + + pi, err := os.ReadFile("../../test/data/pi.txt") + if err != nil { + tt.Fatalf("ReadFile: %v", err) + } else if len(pi) != 100003 { + tt.Fatalf("ReadFile: unexpected pi.txt file length") + } + + lengths := [...]int{0, 1, 10, 100, 1e3, 1e4, 1e5} + + for _, tc := range testCases { + for _, length := range lengths { + original := pi[:length] + + compressed, err := tc.fileFormat.Encode(nil, original) + if err != nil { + tt.Fatalf("%v, length=%d: Encode: %v", tc.fileFormat, length, err) + } + + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + + cmd := exec.Command(tc.command) + cmd.Stdin = bytes.NewReader(compressed) + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + tt.Fatalf("%v, length=%d: Run: %v\nstderr:\n%s", tc.fileFormat, length, err, stderr.Bytes()) + } + + recovered := stdout.Bytes() + if !bytes.Equal(original, recovered) { + tt.Fatalf("%v, length=%d: round trip produced different bytes", tc.fileFormat, length) + } + } + } +}