Files
logpile/internal/parser/archive_zip_recover_test.go
T
Mikhail ChusavitinandClaude Opus 5.5 1cc2faa567 fix(archive): open zips with a missing or truncated central directory
A BEE-SP v14.23 blackbox (Dell PowerEdge R760) streamed from the live CD was
cut off inside the central directory: all 1920 members intact, no
end-of-central-directory record, so archive/zip rejected the whole file
("zip: not a valid zip file"). ADL-079:
- when archive/zip cannot open a .zip, read it front to back by local file
  headers; intact archives are unaffected
- streamed members (data descriptors) are delimited from the data itself,
  deflate and stored; every member is CRC32/size checked
- recovery stops at the first damaged, truncated or encrypted member and
  keeps the entries before it; the original error is returned when nothing
  could be read
- same 10 MB per-file / 1 GB total limits as the regular zip path

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 15:00:39 +03:00

193 lines
6.1 KiB
Go

package parser
import (
"archive/zip"
"bytes"
"hash/crc32"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
type zipTestFile struct {
name string
content string
method uint16
raw bool // sizes and CRC in the local header, no data descriptor
}
// buildTestZip returns the archive bytes and the offset where the central
// directory starts.
func buildTestZip(t *testing.T, modTime time.Time, files []zipTestFile) ([]byte, int) {
t.Helper()
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
for _, f := range files {
hdr := &zip.FileHeader{Name: f.name, Method: f.method, Modified: modTime}
if f.raw {
// CreateRaw writes the header as given: only the DOS time is set.
hdr = &zip.FileHeader{Name: f.name, Method: zip.Store}
hdr.SetModTime(modTime)
hdr.CRC32 = crc32.ChecksumIEEE([]byte(f.content))
hdr.CompressedSize64 = uint64(len(f.content))
hdr.UncompressedSize64 = uint64(len(f.content))
w, err := zw.CreateRaw(hdr)
if err != nil {
t.Fatalf("create raw %s: %v", f.name, err)
}
if _, err := w.Write([]byte(f.content)); err != nil {
t.Fatalf("write %s: %v", f.name, err)
}
continue
}
w, err := zw.CreateHeader(hdr)
if err != nil {
t.Fatalf("create %s: %v", f.name, err)
}
if _, err := w.Write([]byte(f.content)); err != nil {
t.Fatalf("write %s: %v", f.name, err)
}
}
if err := zw.Close(); err != nil {
t.Fatalf("close zip: %v", err)
}
centralDirOffset := bytes.Index(buf.Bytes(), []byte("PK\x01\x02"))
if centralDirOffset < 0 {
t.Fatal("central directory not found")
}
return buf.Bytes(), centralDirOffset
}
func recoverTestFiles() []zipTestFile {
return []zipTestFile{
{name: "README.md", content: strings.Repeat("streamed deflate member\n", 200), method: zip.Deflate},
{name: "export/", method: zip.Store, raw: true},
// Contains a data descriptor signature that must not end the member.
{name: "export/stored.bin", content: "head PK\x07\x08 middle PK\x03\x04 tail", method: zip.Store},
{name: "export/empty.txt", content: "", method: zip.Deflate},
{name: "export/raw.txt", content: "sizes in the local header", raw: true},
{name: "tasks/last.log", content: strings.Repeat("last member ", 50), method: zip.Deflate},
}
}
func assertRecoveredFiles(t *testing.T, got []ExtractedFile, want []zipTestFile, modTime time.Time) {
t.Helper()
var wantFiles []zipTestFile
for _, f := range want {
if !strings.HasSuffix(f.name, "/") {
wantFiles = append(wantFiles, f)
}
}
if len(got) != len(wantFiles) {
t.Fatalf("expected %d files, got %d", len(wantFiles), len(got))
}
for i, f := range wantFiles {
if got[i].Path != f.name {
t.Fatalf("file %d: expected path %q, got %q", i, f.name, got[i].Path)
}
if string(got[i].Content) != f.content {
t.Fatalf("file %s: content mismatch", f.name)
}
if !got[i].ModTime.Equal(modTime) {
t.Fatalf("file %s: expected mtime %v, got %v", f.name, modTime, got[i].ModTime)
}
}
}
func TestExtractArchiveFromReaderZIP_RecoversTruncatedCentralDirectory(t *testing.T) {
modTime := time.Date(2026, 10, 2, 16, 4, 12, 0, time.UTC)
want := recoverTestFiles()
data, centralDirOffset := buildTestZip(t, modTime, want)
// Sanity: the intact archive goes through archive/zip.
files, err := ExtractArchiveFromReader(bytes.NewReader(data), "blackbox.zip")
if err != nil {
t.Fatalf("extract intact zip: %v", err)
}
assertRecoveredFiles(t, files, want, modTime)
cuts := map[string]int{
"mid central directory": centralDirOffset + (len(data)-centralDirOffset)/2,
"central directory missing": centralDirOffset,
"end of central directory lost": len(data) - 10,
}
for name, cut := range cuts {
t.Run(name, func(t *testing.T) {
files, err := ExtractArchiveFromReader(bytes.NewReader(data[:cut]), "blackbox.zip")
if err != nil {
t.Fatalf("extract truncated zip: %v", err)
}
assertRecoveredFiles(t, files, want, modTime)
})
}
}
func TestExtractArchiveZIP_RecoversTruncatedCentralDirectoryFromPath(t *testing.T) {
modTime := time.Date(2026, 10, 2, 16, 4, 12, 0, time.UTC)
want := recoverTestFiles()
data, centralDirOffset := buildTestZip(t, modTime, want)
path := filepath.Join(t.TempDir(), "blackbox.zip")
if err := os.WriteFile(path, data[:centralDirOffset+20], 0o600); err != nil {
t.Fatalf("write zip: %v", err)
}
files, err := ExtractArchive(path)
if err != nil {
t.Fatalf("extract truncated zip: %v", err)
}
assertRecoveredFiles(t, files, want, modTime)
}
func TestExtractArchiveFromReaderZIP_TruncatedMemberKeepsEarlierEntries(t *testing.T) {
modTime := time.Date(2026, 10, 2, 16, 4, 12, 0, time.UTC)
want := recoverTestFiles()
data, centralDirOffset := buildTestZip(t, modTime, want)
// Cut inside the last member's compressed data.
files, err := ExtractArchiveFromReader(bytes.NewReader(data[:centralDirOffset-20]), "blackbox.zip")
if err != nil {
t.Fatalf("extract truncated zip: %v", err)
}
assertRecoveredFiles(t, files, want[:len(want)-1], modTime)
}
func TestExtractArchiveFromReaderZIP_CorruptMemberStopsRecovery(t *testing.T) {
modTime := time.Date(2026, 10, 2, 16, 4, 12, 0, time.UTC)
want := recoverTestFiles()
data, centralDirOffset := buildTestZip(t, modTime, want)
damaged := append([]byte(nil), data[:centralDirOffset]...)
at := bytes.Index(damaged, []byte("sizes in the local header"))
if at < 0 {
t.Fatal("raw member content not found")
}
damaged[at] ^= 0xff
files, err := ExtractArchiveFromReader(bytes.NewReader(damaged), "blackbox.zip")
if err != nil {
t.Fatalf("extract damaged zip: %v", err)
}
// Everything before export/raw.txt; the CRC mismatch ends recovery.
assertRecoveredFiles(t, files, want[:4], modTime)
}
func TestExtractArchiveFromReaderZIP_NotAZipStillFails(t *testing.T) {
for name, data := range map[string][]byte{
"garbage": []byte("this is not a zip archive at all"),
"empty": {},
"truncated first entry": []byte("PK\x03\x04\x14\x00\x08\x00\x08\x00"),
} {
t.Run(name, func(t *testing.T) {
_, err := ExtractArchiveFromReader(bytes.NewReader(data), "blackbox.zip")
if err == nil || !strings.Contains(err.Error(), "open zip") {
t.Fatalf("expected open zip error, got %v", err)
}
})
}
}