Files
Reims-Go/internal/runtime/selection_test.go
T

131 lines
4.8 KiB
Go

package runtime
import (
"context"
"errors"
"os"
"path/filepath"
"runtime"
"slices"
"testing"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/host"
)
func TestSelectionFollowsRuntimeAndToggle(t *testing.T) {
cfg := &config.Config{DataRoot: t.TempDir()}
var targets []string
sync := func(_ context.Context, root, path string) error {
if root != cfg.RuntimesDir() {
t.Fatalf("unexpected runtimes root %q", root)
}
targets = append(targets, path)
return nil
}
selectRuntime := func(client *config.Config, commit string, toggle *bool) {
t.Helper()
if err := saveSelection(context.Background(), client, commit, toggle, sync); err != nil {
t.Fatal(err)
}
}
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
backup := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "backup")}
writeFakeRuntime(t, current.Dir, Manifest{Commit: "current"})
writeFakeRuntime(t, backup.Dir, Manifest{Commit: "backup"})
on, off := true, false
selectRuntime(cfg, "current", nil) // An older config has ASLR off.
stale := *cfg
selectRuntime(cfg, "", &on)
selectRuntime(&stale, "backup", nil) // CLI picks up the desktop preference.
selectRuntime(cfg, "current", nil)
selectRuntime(cfg, "", &off)
selectRuntime(&stale, "backup", nil)
want := []string{current.QemuPath(), backup.QemuPath(), current.QemuPath(), ""}
if !slices.Equal(targets, want) {
t.Fatalf("synced paths = %q, want %q", targets, want)
}
saved, err := config.Load(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || saved.QEMUASLR || saved.ActiveRuntime != "backup" {
t.Fatalf("saved selection = %+v, %v", saved, err)
}
}
func TestSelectionFailureKeepsPreviousRuntime(t *testing.T) {
cfg := &config.Config{DataRoot: t.TempDir(), ActiveRuntime: "old", QEMUASLR: true}
for _, commit := range []string{"old", "new"} {
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), commit), Manifest{Commit: commit})
}
if err := config.Save("", cfg); err != nil {
t.Fatal(err)
}
calls := 0
err := saveSelection(context.Background(), cfg, "new", nil, func(context.Context, string, string) error {
calls++
return host.ErrElevationDeclined
})
if !errors.Is(err, host.ErrElevationDeclined) || calls != 1 {
t.Fatalf("declined activation: calls = %d, error = %v", calls, err)
}
// Prevent the atomic config write after a successful host update.
if err := os.Mkdir(filepath.Join(cfg.DataRoot, "reims-go.json.tmp"), 0o755); err != nil {
t.Fatal(err)
}
var applied []string
err = saveSelection(context.Background(), cfg, "new", nil, func(_ context.Context, _, path string) error {
applied = append(applied, path)
return nil
})
old := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "old")}
next := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "new")}
if err == nil || !slices.Equal(applied, []string{next.QemuPath(), old.QemuPath()}) {
t.Fatalf("rollback paths = %v, error = %v", applied, err)
}
saved, err := config.Load(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || cfg.ActiveRuntime != "old" || saved.ActiveRuntime != "old" || !saved.QEMUASLR {
t.Fatalf("failed selection was committed: %+v, %v", saved, err)
}
}
func TestLaunchPolicyRejectsSupersededRuntime(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Windows launch policy")
}
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "backup", QEMUASLR: true}
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
writeFakeRuntime(t, current.Dir, Manifest{Commit: "current"})
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), "backup"), Manifest{Commit: "backup"})
if err := config.Save("", &cfg); err != nil {
t.Fatal(err)
}
stale := cfg
stale.ActiveRuntime, stale.QEMUASLR = "current", false
if err := CheckLaunchASLR(t.Context(), stale, current.QemuPath()); err == nil {
t.Fatal("launch would reapply policy to the superseded runtime")
}
}
func TestNonWindowsIgnoresSavedQEMUASLR(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("non-Windows host")
}
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "current", QEMUASLR: true}
for _, commit := range []string{"current", "backup"} {
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), commit), Manifest{Commit: commit})
}
if err := config.Save("", &cfg); err != nil {
t.Fatal(err)
}
if err := Activate(t.Context(), &cfg, "backup"); err != nil {
t.Fatal(err)
}
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
if err := CheckLaunchASLR(t.Context(), cfg, current.QemuPath()); err != nil {
t.Fatalf("Windows policy blocked launch on %s: %v", runtime.GOOS, err)
}
saved, err := config.LoadExisting(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || !saved.QEMUASLR || saved.ActiveRuntime != "backup" {
t.Fatalf("lost runtime selection or portable preference: %+v, %v", saved, err)
}
}