128 lines
3.7 KiB
Go
128 lines
3.7 KiB
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/synctest"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/filelock"
|
|
)
|
|
|
|
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.updateConfig(nil, func(cfg *config.Config) error {
|
|
<-resume
|
|
cfg.ActiveRuntime, cfg.QEMUASLR = "backup", true
|
|
return config.Save("", cfg)
|
|
})
|
|
}()
|
|
synctest.Wait()
|
|
started := make(chan struct{})
|
|
go func() {
|
|
close(started)
|
|
results <- a.SetSyncHostMemorySMBIOS(true)
|
|
}()
|
|
<-started
|
|
close(resume)
|
|
for range 2 {
|
|
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)
|
|
}
|
|
})
|
|
}
|