| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/google/uuid" |
| 10 | "github.com/sylvinus/agent-vm/internal/cage" |
| 11 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 12 | ) |
| 13 | |
| 14 | func TestWindowsInstallationActivationRejectsDifferentGuest(t *testing.T) { |
| 15 | s := testServer(t) |
| 16 | install := guestos.Installation{Version: 1, ID: uuid.New(), System: guestos.Windows11} |
| 17 | state := filepath.Join(s.profile, "state") |
| 18 | if err := os.MkdirAll(state, 0700); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | state, err := cage.StoragePhysicalPath(state) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | binding := guestos.WindowsProfile{Version: 1, VMID: uuid.New(), GUIUserSID: "S-1-5-21-1-2-3-1001", |
| 26 | InstanceDirectory: filepath.Join(state, "windows-"+strings.ReplaceAll(install.ID.String(), "-", "")), |
| 27 | RelayExecutable: filepath.Join(s.profile, "relay.exe"), PairingFile: filepath.Join(s.profile, "pairing.bin")} |
| 28 | if err := guestos.BindWindowsProfile(s.profile, binding); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | receipt := filepath.Join(s.profile, "windows-install-vm.txt") |
| 32 | if err := os.WriteFile(receipt, []byte(binding.VMID.String()), 0600); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | owner, err := s.installedWindowsVMOwner(install) |
| 36 | if err != nil || owner != "windows/"+binding.VMID.String()+"/Running" { |
| 37 | t.Fatalf("completed installation was not admitted: %q %v", owner, err) |
| 38 | } |
| 39 | if err := os.WriteFile(receipt, []byte(uuid.New().String()), 0600); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if _, err := s.installedWindowsVMOwner(install); err == nil { |
| 43 | t.Fatal("another VM's receipt permitted controller activation") |
| 44 | } |
| 45 | if err := os.WriteFile(receipt, []byte(binding.VMID.String()), 0600); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | install.ID = uuid.New() |
| 49 | if _, err := s.installedWindowsVMOwner(install); err == nil { |
| 50 | t.Fatal("another installation's disk permitted controller activation") |
| 51 | } |
| 52 | } |