94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package parser
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestExtractArchiveFromReaderTXT(t *testing.T) {
|
|
content := "loader_brand=\"XigmaNAS\"\nSystem uptime:\n"
|
|
files, err := ExtractArchiveFromReader(strings.NewReader(content), "xigmanas.txt")
|
|
if err != nil {
|
|
t.Fatalf("extract txt from reader: %v", err)
|
|
}
|
|
if len(files) != 1 {
|
|
t.Fatalf("expected 1 file, got %d", len(files))
|
|
}
|
|
if files[0].Path != "xigmanas.txt" {
|
|
t.Fatalf("expected filename xigmanas.txt, got %q", files[0].Path)
|
|
}
|
|
if string(files[0].Content) != content {
|
|
t.Fatalf("content mismatch")
|
|
}
|
|
}
|
|
|
|
func TestExtractArchiveTXT(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "sample.txt")
|
|
want := "plain text log"
|
|
if err := os.WriteFile(path, []byte(want), 0o600); err != nil {
|
|
t.Fatalf("write sample txt: %v", err)
|
|
}
|
|
|
|
files, err := ExtractArchive(path)
|
|
if err != nil {
|
|
t.Fatalf("extract txt file: %v", err)
|
|
}
|
|
if len(files) != 1 {
|
|
t.Fatalf("expected 1 file, got %d", len(files))
|
|
}
|
|
if files[0].Path != "sample.txt" {
|
|
t.Fatalf("expected sample.txt, got %q", files[0].Path)
|
|
}
|
|
if string(files[0].Content) != want {
|
|
t.Fatalf("content mismatch")
|
|
}
|
|
}
|
|
|
|
func TestExtractArchiveFromReaderTXT_TruncatedWhenTooLarge(t *testing.T) {
|
|
large := bytes.Repeat([]byte("a"), maxSingleFileSize+1024)
|
|
files, err := ExtractArchiveFromReader(bytes.NewReader(large), "huge.log")
|
|
if err != nil {
|
|
t.Fatalf("extract huge txt from reader: %v", err)
|
|
}
|
|
if len(files) != 1 {
|
|
t.Fatalf("expected 1 file, got %d", len(files))
|
|
}
|
|
|
|
f := files[0]
|
|
if !f.Truncated {
|
|
t.Fatalf("expected file to be marked as truncated")
|
|
}
|
|
if got := len(f.Content); got != maxSingleFileSize {
|
|
t.Fatalf("expected truncated size %d, got %d", maxSingleFileSize, got)
|
|
}
|
|
if f.TruncatedMessage == "" {
|
|
t.Fatalf("expected truncation message")
|
|
}
|
|
}
|
|
|
|
func TestIsSupportedArchiveFilename(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
want bool
|
|
}{
|
|
{name: "dump.tar.gz", want: true},
|
|
{name: "nvidia-bug-report-1651124000923.log.gz", want: true},
|
|
{name: "snapshot.zip", want: true},
|
|
{name: "report.log", want: true},
|
|
{name: "xigmanas.txt", want: true},
|
|
{name: "raw_export.json", want: false},
|
|
{name: "archive.bin", want: false},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
got := IsSupportedArchiveFilename(tc.name)
|
|
if got != tc.want {
|
|
t.Fatalf("IsSupportedArchiveFilename(%q)=%v, want %v", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
}
|