| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/sylvinus/agent-vm/internal/host" |
| 10 | "golang.org/x/sys/windows" |
| 11 | ) |
| 12 | |
| 13 | func TestDesktopLocalAdministratorOwnedLock(t *testing.T) { |
| 14 | if !windows.GetCurrentProcessToken().IsElevated() { |
| 15 | t.Skip("requires an elevated Windows token") |
| 16 | } |
| 17 | for _, held := range []bool{false, true} { |
| 18 | name := "idle" |
| 19 | if held { |
| 20 | name = "live" |
| 21 | } |
| 22 | t.Run(name, func(t *testing.T) { |
| 23 | profile := t.TempDir() |
| 24 | if err := host.EnsurePrivateDir(profile); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | path := filepath.Join(profile, "control.lock") |
| 28 | lock, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | defer lock.Close() |
| 33 | admin, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION, admin, nil, nil, nil); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if err := host.CheckPrivateFile(path); err == nil { |
| 41 | t.Fatal("fixture is not an old group-owned lock") |
| 42 | } |
| 43 | if held { |
| 44 | if err := lockController(lock); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | defer unlockController(lock) |
| 48 | } |
| 49 | exe, _ := os.Executable() |
| 50 | st, err := NewDesktopLocalController(filepath.Join(profile, "config.json"), exe).Status(context.Background()) |
| 51 | if err != nil || st.ServiceRunning != held || st.Known == held { |
| 52 | t.Fatalf("lock ownership confused with liveness: %+v %v", st, err) |
| 53 | } |
| 54 | if !held { |
| 55 | if err := host.CheckPrivateFile(path); err != nil { |
| 56 | t.Fatalf("idle lock was not migrated to private current-user ownership: %v", err) |
| 57 | } |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |