235 lines
5.9 KiB
Go
235 lines
5.9 KiB
Go
package runtime
|
|
|
|
import (
|
|
"archive/tar"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
type tarEntry struct {
|
|
name string
|
|
typeflag byte
|
|
body string
|
|
linkname string
|
|
mode int64
|
|
}
|
|
|
|
func writeTestTarZst(t *testing.T, archivePath string, entries []tarEntry) {
|
|
t.Helper()
|
|
f, err := os.Create(archivePath)
|
|
if err != nil {
|
|
t.Fatalf("create archive: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
zw, err := zstd.NewWriter(f)
|
|
if err != nil {
|
|
t.Fatalf("new zstd writer: %v", err)
|
|
}
|
|
tw := tar.NewWriter(zw)
|
|
|
|
for _, e := range entries {
|
|
tf := e.typeflag
|
|
if tf == 0 {
|
|
tf = tar.TypeReg
|
|
}
|
|
mode := e.mode
|
|
if mode == 0 {
|
|
if tf == tar.TypeDir {
|
|
mode = 0o755
|
|
} else {
|
|
mode = 0o644
|
|
}
|
|
}
|
|
hdr := &tar.Header{
|
|
Name: e.name,
|
|
Typeflag: tf,
|
|
Mode: mode,
|
|
Linkname: e.linkname,
|
|
}
|
|
if tf == tar.TypeReg {
|
|
hdr.Size = int64(len(e.body))
|
|
}
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
t.Fatalf("write tar header %s: %v", e.name, err)
|
|
}
|
|
if tf == tar.TypeReg {
|
|
if _, err := tw.Write([]byte(e.body)); err != nil {
|
|
t.Fatalf("write tar body %s: %v", e.name, err)
|
|
}
|
|
}
|
|
}
|
|
if err := tw.Close(); err != nil {
|
|
t.Fatalf("close tar writer: %v", err)
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatalf("close zstd writer: %v", err)
|
|
}
|
|
}
|
|
|
|
func sha256Hex(s string) string {
|
|
sum := sha256.Sum256([]byte(s))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func TestInstallFromArchiveTarZstPureGo(t *testing.T) {
|
|
dataRoot := t.TempDir()
|
|
cfg := &config.Config{DataRoot: dataRoot}
|
|
|
|
ctlBody := "fake-reims-ctl-binary"
|
|
qemuBody := "fake-qemu-binary"
|
|
m := Manifest{
|
|
SchemaVersion: 2,
|
|
Name: "reims-runtime-win-x64",
|
|
Commit: "3731afd",
|
|
BuiltAt: "2026-11-29T00:00:00Z",
|
|
SHA256: map[string]string{
|
|
"bin/reims-ctl.exe": sha256Hex(ctlBody),
|
|
"qemu/bin/qemu-system-x86_64.exe": sha256Hex(qemuBody),
|
|
},
|
|
}
|
|
manifestBytes, err := json.Marshal(m)
|
|
if err != nil {
|
|
t.Fatalf("marshal manifest: %v", err)
|
|
}
|
|
|
|
archivePath := filepath.Join(t.TempDir(), "reims-runtime-win-x64-3731afd.tar.zst")
|
|
writeTestTarZst(t, archivePath, []tarEntry{
|
|
{name: "./", typeflag: tar.TypeDir},
|
|
{name: "reims-runtime-win-x64-3731afd/", typeflag: tar.TypeDir},
|
|
{name: "reims-runtime-win-x64-3731afd/manifest.json", body: string(manifestBytes), mode: 0o444},
|
|
{name: "reims-runtime-win-x64-3731afd/bin/reims-ctl.exe", body: ctlBody, mode: 0o755},
|
|
{name: "reims-runtime-win-x64-3731afd/qemu/bin/qemu-system-x86_64.exe", body: qemuBody, mode: 0o755},
|
|
})
|
|
|
|
// Isolate PATH so the test proves no external tar or zstd binary is invoked.
|
|
t.Setenv("PATH", "")
|
|
|
|
inst, err := InstallFromArchive(cfg, archivePath)
|
|
if err != nil {
|
|
t.Fatalf("InstallFromArchive failed: %v", err)
|
|
}
|
|
if inst.Commit != "3731afd" {
|
|
t.Fatalf("Commit = %q, want 3731afd", inst.Commit)
|
|
}
|
|
gotCtl, err := os.ReadFile(filepath.Join(inst.Dir, "bin", "reims-ctl.exe"))
|
|
if err != nil {
|
|
t.Fatalf("read extracted reims-ctl.exe: %v", err)
|
|
}
|
|
if string(gotCtl) != ctlBody {
|
|
t.Fatalf("extracted reims-ctl.exe = %q, want %q", string(gotCtl), ctlBody)
|
|
}
|
|
gotQemu, err := os.ReadFile(filepath.Join(inst.Dir, "qemu", "bin", "qemu-system-x86_64.exe"))
|
|
if err != nil {
|
|
t.Fatalf("read extracted qemu: %v", err)
|
|
}
|
|
if string(gotQemu) != qemuBody {
|
|
t.Fatalf("extracted qemu = %q, want %q", string(gotQemu), qemuBody)
|
|
}
|
|
}
|
|
|
|
func TestExtractTarZstRejectsTraversalAndEscapes(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
entries []tarEntry
|
|
}{
|
|
{
|
|
name: "dotdot_file",
|
|
entries: []tarEntry{
|
|
{name: "../escape.txt", body: "bad"},
|
|
},
|
|
},
|
|
{
|
|
name: "nested_dotdot",
|
|
entries: []tarEntry{
|
|
{name: "a/b/../../../escape.txt", body: "bad"},
|
|
},
|
|
},
|
|
{
|
|
name: "backslash_dotdot",
|
|
entries: []tarEntry{
|
|
{name: `sub\..\..\escape.txt`, body: "bad"},
|
|
},
|
|
},
|
|
{
|
|
name: "absolute_unix",
|
|
entries: []tarEntry{
|
|
{name: "/etc/passwd", body: "bad"},
|
|
},
|
|
},
|
|
{
|
|
name: "symlink_escape",
|
|
entries: []tarEntry{
|
|
{name: "sub/link", typeflag: tar.TypeSymlink, linkname: "../../outside.txt"},
|
|
},
|
|
},
|
|
{
|
|
name: "symlink_absolute",
|
|
entries: []tarEntry{
|
|
{name: "link", typeflag: tar.TypeSymlink, linkname: "/tmp/outside.txt"},
|
|
},
|
|
},
|
|
{
|
|
name: "hardlink_escape",
|
|
entries: []tarEntry{
|
|
{name: "hard", typeflag: tar.TypeLink, linkname: "../outside.txt"},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
archivePath := filepath.Join(t.TempDir(), tc.name+".tar.zst")
|
|
writeTestTarZst(t, archivePath, tc.entries)
|
|
destDir := filepath.Join(t.TempDir(), "dest")
|
|
if err := os.MkdirAll(destDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := extractTarZst(archivePath, destDir)
|
|
if err == nil {
|
|
t.Fatalf("expected error for %s, got nil", tc.name)
|
|
}
|
|
if !strings.Contains(err.Error(), "illegal tar") {
|
|
t.Fatalf("unexpected error for %s: %v", tc.name, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractTarZstCorruptArchive(t *testing.T) {
|
|
archivePath := filepath.Join(t.TempDir(), "corrupt.tar.zst")
|
|
if err := os.WriteFile(archivePath, []byte("not a valid zstd stream"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := extractTarZst(archivePath, t.TempDir()); err == nil {
|
|
t.Fatal("expected error for corrupt archive, got nil")
|
|
}
|
|
}
|
|
|
|
func TestExtractTarZstHardlinkWithinDest(t *testing.T) {
|
|
archivePath := filepath.Join(t.TempDir(), "hardlink.tar.zst")
|
|
writeTestTarZst(t, archivePath, []tarEntry{
|
|
{name: "bundle/a.txt", body: "hello"},
|
|
{name: "bundle/b.txt", typeflag: tar.TypeLink, linkname: "bundle/a.txt"},
|
|
})
|
|
destDir := t.TempDir()
|
|
if err := extractTarZst(archivePath, destDir); err != nil {
|
|
t.Fatalf("extractTarZst failed: %v", err)
|
|
}
|
|
got, err := os.ReadFile(filepath.Join(destDir, "bundle", "b.txt"))
|
|
if err != nil {
|
|
t.Fatalf("read hardlinked file: %v", err)
|
|
}
|
|
if string(got) != "hello" {
|
|
t.Fatalf("hardlink content = %q, want hello", string(got))
|
|
}
|
|
}
|