177 lines
5.5 KiB
Go
177 lines
5.5 KiB
Go
package app
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
"testing/synctest"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/filelock"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
func TestArchiveActivationCanRetryWithoutImport(t *testing.T) {
|
|
dir := t.TempDir()
|
|
a := &App{cfg: &config.Config{DataRoot: dir}}
|
|
var archive bytes.Buffer
|
|
w := zip.NewWriter(&archive)
|
|
if err := w.AddFS(fstest.MapFS{"manifest.json": {Data: []byte(`{"commit":"candidate"}`)}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := w.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(dir, "runtime.zip")
|
|
if err := os.WriteFile(path, archive.Bytes(), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blockActivation := filepath.Join(dir, "reims-go.json")
|
|
if err := os.Mkdir(blockActivation, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inst, err := a.InstallRuntimeArchive(path)
|
|
var partial *i18n.CodedError
|
|
if !errors.As(err, &partial) || partial.Code != "E_RUNTIME_ACTIVATION" || inst == nil || inst.Active {
|
|
t.Fatalf("partial install = %+v, %v", inst, err)
|
|
}
|
|
list, err := rt.List(a.cfg)
|
|
if err != nil || len(list) != 1 || list[0].Commit != "candidate" || list[0].Active {
|
|
t.Fatalf("installed list = %+v, %v", list, err)
|
|
}
|
|
for _, item := range []string{path, blockActivation} {
|
|
if err := os.Remove(item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := a.UseRuntime(inst.Commit); err != nil || a.cfg.ActiveRuntime != "candidate" {
|
|
t.Fatalf("activation retry = %q, %v", a.cfg.ActiveRuntime, err)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeConfigFollowsExternalSelection(t *testing.T) {
|
|
dir := t.TempDir()
|
|
a := &App{cfg: &config.Config{DataRoot: dir, ActiveRuntime: "current"}}
|
|
stored := *a.cfg
|
|
stored.ActiveRuntime, stored.QEMUASLR = "backup", true
|
|
if err := config.Save("", &stored); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := a.runtimeConfig()
|
|
if got.ActiveRuntime != "backup" || !got.QEMUASLR {
|
|
t.Fatalf("desktop reads stale selection: %+v", got)
|
|
}
|
|
if err := a.SetSyncHostMemorySMBIOS(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saved, err := config.LoadExisting(filepath.Join(dir, "reims-go.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if saved.ActiveRuntime != "backup" || !saved.QEMUASLR || !saved.SyncHostMemorySMBIOS {
|
|
t.Fatalf("saving an unrelated setting lost the selection: %+v", saved)
|
|
}
|
|
}
|
|
|
|
func TestPreferencesDoNotOverwriteDamagedConfig(t *testing.T) {
|
|
dir := t.TempDir()
|
|
a := &App{cfg: &config.Config{DataRoot: dir}}
|
|
path := filepath.Join(dir, "reims-go.json")
|
|
if err := os.WriteFile(path, []byte("{"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.SetSyncHostMemorySMBIOS(true); err == nil {
|
|
t.Fatal("saved preferences over an unreadable selection")
|
|
}
|
|
if data, err := os.ReadFile(path); err != nil || string(data) != "{" {
|
|
t.Fatalf("damaged config replaced: %q, %v", data, err)
|
|
}
|
|
}
|
|
|
|
func TestConfigWaitDoesNotBlockApp(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
save func(*App) error
|
|
}{
|
|
{"ASLR", func(a *App) error { return a.SetQEMUASLR(true) }},
|
|
{"runtime", func(a *App) error { return a.UseRuntime("backup") }},
|
|
{"preference", func(a *App) error { return a.SetSyncHostMemorySMBIOS(true) }},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cfg := &config.Config{DataRoot: t.TempDir()}
|
|
if err := cfg.EnsureDirs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// All saves will stop before a registry write after the lock releases.
|
|
if err := os.WriteFile(filepath.Join(cfg.DataRoot, "reims-go.json"), []byte("{"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
release, err := filelock.Acquire(t.Context(), filepath.Join(cfg.RuntimesDir(), ".reims-go-runtime.lock"), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a := &App{cfg: cfg}
|
|
synctest.Test(t, func(t *testing.T) {
|
|
done := make(chan error, 1)
|
|
go func() { done <- tc.save(a) }()
|
|
synctest.Wait()
|
|
available := a.mu.TryLock()
|
|
if available {
|
|
a.mu.Unlock()
|
|
a.emitLog("test", "while waiting")
|
|
}
|
|
release()
|
|
if err := <-done; err == nil || !available {
|
|
t.Fatalf("global lock available = %v, save error = %v", available, err)
|
|
}
|
|
if a.cfg.QEMUASLR || a.cfg.ActiveRuntime != "" || a.cfg.SyncHostMemorySMBIOS {
|
|
t.Fatalf("failed save published %+v", a.cfg)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConcurrentSelectionAndPreferenceSurvive(t *testing.T) {
|
|
dir := t.TempDir()
|
|
a := &App{cfg: &config.Config{DataRoot: dir}}
|
|
synctest.Test(t, func(t *testing.T) {
|
|
resume := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
go func() {
|
|
results <- a.updateSelection(func(cfg *config.Config) error {
|
|
<-resume
|
|
// Model the registry operation returning and committing over
|
|
// the preferences saved while consent was pending.
|
|
saved, err := config.LoadExisting(filepath.Join(dir, "reims-go.json"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
saved.ActiveRuntime, saved.QEMUASLR = "backup", true
|
|
cfg.ActiveRuntime, cfg.QEMUASLR = saved.ActiveRuntime, saved.QEMUASLR
|
|
return config.Save("", saved)
|
|
})
|
|
}()
|
|
synctest.Wait()
|
|
if err := a.SetSyncHostMemorySMBIOS(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
close(resume)
|
|
if err := <-results; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saved, err := config.LoadExisting(filepath.Join(dir, "reims-go.json"))
|
|
if err != nil || saved.ActiveRuntime != "backup" || !saved.QEMUASLR || !saved.SyncHostMemorySMBIOS {
|
|
t.Fatalf("concurrent updates lost: %+v, %v", saved, err)
|
|
}
|
|
if a.cfg.ActiveRuntime != saved.ActiveRuntime || !a.cfg.QEMUASLR || !a.cfg.SyncHostMemorySMBIOS {
|
|
t.Fatalf("in-memory settings diverged: %+v", a.cfg)
|
|
}
|
|
})
|
|
}
|