Files
logpile/internal/parser/archive_rar_test.go
T
Mikhail ChusavitinandClaude Opus 5.5 b4b15e58a2 feat(parser): linux-generic content-driven parser and RAR archive support
Field bundles from ad-hoc service scripts (first sample: Giga G894-SD3 .rar
with dmidecode/lscpu/lspci/nvme/lsblk output, no fixed layout). ADL-078:
- linux-generic: recognizes utility output by content, merges records by
  stable key, low detect confidence (20-30) below all vendor parsers
- pciids.LookupByDescription: resolve a textual lspci description back to
  vendor/device IDs and manufacturer/model
- archive: accept .rar via the in-repo internal/rar5 reader (RAR 5 / RAR 7,
  single volume, solid; encrypted, multi-volume, SFX and RAR 4.x rejected),
  same per-file and total limits as tar/zip
- UI: .rar in the upload accept list

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

129 lines
3.9 KiB
Go

package parser
import (
"bytes"
"encoding/binary"
"hash/crc32"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
type rarTestFile struct {
name string
content string
mtime time.Time
}
// buildStoredRAR5 assembles a minimal RAR 5.0 archive whose members use the
// "store" method, which is enough to exercise header walking and extraction
// without a RAR compressor.
func buildStoredRAR5(files []rarTestFile) []byte {
vint := func(v uint64) []byte {
var out []byte
for v >= 0x80 {
out = append(out, byte(v)|0x80)
v >>= 7
}
return append(out, byte(v))
}
block := func(body []byte) []byte {
sized := append(vint(uint64(len(body))), body...)
crc := make([]byte, 4)
binary.LittleEndian.PutUint32(crc, crc32.ChecksumIEEE(sized))
return append(crc, sized...)
}
fields := func(parts ...[]byte) []byte { return bytes.Join(parts, nil) }
u32 := func(v uint32) []byte {
out := make([]byte, 4)
binary.LittleEndian.PutUint32(out, v)
return out
}
archive := []byte("Rar!\x1a\x07\x01\x00")
// Main archive header: type 1, no header flags, no archive flags.
archive = append(archive, block(fields(vint(1), vint(0), vint(0)))...)
for _, f := range files {
data := []byte(f.content)
archive = append(archive, block(fields(
vint(2), // header type: file
vint(0x0002), // header flags: data area present
vint(uint64(len(data))), // data area size
vint(0x0002|0x0004), // file flags: mtime and CRC32 present
vint(uint64(len(data))), // unpacked size
vint(0x20), // attributes
u32(uint32(f.mtime.Unix())),
u32(crc32.ChecksumIEEE(data)),
vint(0), // compression info: store
vint(1), // host OS: Unix
vint(uint64(len(f.name))),
[]byte(f.name),
))...)
archive = append(archive, data...)
}
// End of archive header: type 5.
return append(archive, block(fields(vint(5), vint(0), vint(0)))...)
}
func TestExtractArchiveRAR(t *testing.T) {
mtime := time.Date(2026, 10, 2, 7, 37, 52, 0, time.UTC)
payload := buildStoredRAR5([]rarTestFile{
{name: "host01/System-info.txt", content: "System Information\n\tSerial Number: EXSN0001\n", mtime: mtime},
{name: "host01/service-log/lspci.txt", content: "00:00.0 Host bridge: Example Corp Device 0001\n", mtime: mtime},
})
check := func(t *testing.T, files []ExtractedFile, err error) {
t.Helper()
if err != nil {
t.Fatalf("extract rar: %v", err)
}
if len(files) != 2 {
t.Fatalf("expected 2 files, got %d: %+v", len(files), files)
}
if files[0].Path != "host01/System-info.txt" || files[1].Path != "host01/service-log/lspci.txt" {
t.Fatalf("unexpected paths: %q, %q", files[0].Path, files[1].Path)
}
if got := string(files[1].Content); got != "00:00.0 Host bridge: Example Corp Device 0001\n" {
t.Fatalf("unexpected content: %q", got)
}
if !files[0].ModTime.Equal(mtime) {
t.Fatalf("unexpected mtime: %v", files[0].ModTime)
}
}
t.Run("reader", func(t *testing.T) {
files, err := ExtractArchiveFromReader(bytes.NewReader(payload), "ER0001.RAR")
check(t, files, err)
})
t.Run("path", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "ER0001.rar")
if err := os.WriteFile(path, payload, 0o600); err != nil {
t.Fatalf("write rar: %v", err)
}
files, err := ExtractArchive(path)
check(t, files, err)
})
}
func TestExtractArchiveRAR_RejectsUnsupported(t *testing.T) {
cases := []struct {
name string
payload []byte
want string
}{
{name: "not a rar", payload: []byte("PK\x03\x04 not a rar"), want: "not a RAR archive"},
{name: "RAR 4.x", payload: []byte("Rar!\x1a\x07\x00\xcf\x90\x73\x00\x00\x0d\x00"), want: "RAR 4.x"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := ExtractArchiveFromReader(bytes.NewReader(tc.payload), "renamed.rar")
if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatalf("got error %v, want it to mention %q", err, tc.want)
}
})
}
}