← Files ISA-L ZipARCHIVED FILE

native/cmd/isa-zip/main_test.go

3.91 KB · Nov 30, 2026 · 00:22 UTC

↓ Download file

package main

import (
	"archive/zip"
	"bytes"
	"os"
	"path/filepath"
	"testing"
)

func TestNativeZipRoundTrip(t *testing.T) {
	root := t.TempDir()
	src := filepath.Join(root, "payload")
	if err := os.Mkdir(src, 0755); err != nil {
		t.Fatal(err)
	}
	want := []byte("native Go + ISA-L C\n")
	if err := os.WriteFile(filepath.Join(src, "a.txt"), want, 0600); err != nil {
		t.Fatal(err)
	}
	out := filepath.Join(root, "test.zip")
	if err := pack([]string{out, src}); err != nil {
		t.Fatal(err)
	}
	f, err := os.Open(out)
	if err != nil {
		t.Fatal(err)
	}
	st, err := f.Stat()
	if err != nil {
		t.Fatal(err)
	}
	zr, err := zip.NewReader(f, st.Size())
	if err != nil {
		t.Fatal(err)
	}
	for _, entry := range zr.File {
		if !entry.FileInfo().IsDir() && entry.Method != zip.Deflate {
			t.Fatalf("method %d; want DEFLATE", entry.Method)
		}
	}
	dest := filepath.Join(root, "restored")
	if err = extract([]string{out, dest}); err != nil {
		t.Fatal(err)
	}
	got, err := os.ReadFile(filepath.Join(dest, "payload", "a.txt"))
	if err != nil {
		t.Fatal(err)
	}
	if string(got) != string(want) {
		t.Fatalf("got %q, want %q", got, want)
	}
	if err = f.Close(); err != nil {
		t.Fatal(err)
	}
}

func TestRejectTraversal(t *testing.T) {
	root := t.TempDir()
	archive := filepath.Join(root, "bad.zip")
	f, err := os.Create(archive)
	if err != nil {
		t.Fatal(err)
	}
	zw := zip.NewWriter(f)
	w, err := zw.Create("../escaped.txt")
	if err != nil {
		t.Fatal(err)
	}
	if _, err = w.Write([]byte("bad")); err != nil {
		t.Fatal(err)
	}
	if err = zw.Close(); err != nil {
		t.Fatal(err)
	}
	if err = f.Close(); err != nil {
		t.Fatal(err)
	}
	dest := filepath.Join(root, "out")
	if err = extract([]string{archive, dest}); err == nil {
		t.Fatal("expected traversal rejection")
	}
	if _, err = os.Stat(filepath.Join(root, "escaped.txt")); !os.IsNotExist(err) {
		t.Fatalf("traversal wrote outside destination: %v", err)
	}
}

func makeTestZip(t *testing.T, names ...string) []byte {
	t.Helper()
	var b bytes.Buffer
	z := zip.NewWriter(&b)
	for _, name := range names {
		w, err := z.CreateHeader(&zip.FileHeader{Name: name, Method: zip.Store})
		if err != nil {
			t.Fatal(err)
		}
		if _, err = w.Write([]byte("payload")); err != nil {
			t.Fatal(err)
		}
	}
	if err := z.Close(); err != nil {
		t.Fatal(err)
	}
	return b.Bytes()
}

func writeTestZip(t *testing.T, data []byte) string {
	t.Helper()
	p := filepath.Join(t.TempDir(), "test.zip")
	if err := os.WriteFile(p, data, 0600); err != nil {
		t.Fatal(err)
	}
	return p
}

func TestRejectBackslashPathAndKeepDestinationUntouched(t *testing.T) {
	archive := writeTestZip(t, makeTestZip(t, "first.txt", `..\escape.txt`))
	dest := filepath.Join(t.TempDir(), "out")
	if err := extract([]string{archive, dest}); err == nil {
		t.Fatal("expected backslash path rejection")
	}
	if _, err := os.Stat(filepath.Join(dest, "first.txt")); !os.IsNotExist(err) {
		t.Fatalf("partial extraction occurred: %v", err)
	}
}

func TestRejectCaseCollidingPathsBeforeWriting(t *testing.T) {
	archive := writeTestZip(t, makeTestZip(t, "abc.txt", "ABC.txt"))
	dest := filepath.Join(t.TempDir(), "out")
	if err := extract([]string{archive, dest}); err == nil {
		t.Fatal("expected case-collision rejection")
	}
	if _, err := os.Stat(filepath.Join(dest, "abc.txt")); !os.IsNotExist(err) {
		t.Fatalf("partial extraction occurred: %v", err)
	}
}

func TestCRCFailureLeavesNoCorruptDestination(t *testing.T) {
	data := makeTestZip(t, "result.txt")
	// The stored member payload follows its local header and filename.
	needle := []byte("payload")
	index := bytes.Index(data, needle)
	if index < 0 {
		t.Fatal("test payload not found in ZIP")
	}
	data[index] ^= 0x20
	archive := writeTestZip(t, data)
	dest := filepath.Join(t.TempDir(), "out")
	if err := extract([]string{archive, dest}); err == nil {
		t.Fatal("expected corrupt CRC rejection")
	}
	if _, err := os.Stat(filepath.Join(dest, "result.txt")); !os.IsNotExist(err) {
		t.Fatalf("corrupt output was retained: %v", err)
	}
}

SHA-256: 5b1f3c7bb19d15d40cfbfa6f6abd0ff01224f751350013a89bf363205d426db3