| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/google/uuid" |
| 9 | "github.com/sylvinus/agent-vm/internal/cage" |
| 10 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 11 | ) |
| 12 | |
| 13 | func TestProxyChangeClosesBoundWindowsGate(t *testing.T) { |
| 14 | profile := t.TempDir() |
| 15 | instance := t.TempDir() |
| 16 | binding := guestos.WindowsProfile{Version: 1, VMID: uuid.New(), GUIUserSID: "S-1-5-21-1-2-3-1001", InstanceDirectory: instance, RelayExecutable: filepath.Join(profile, "relay.exe"), PairingFile: filepath.Join(profile, "pairing.bin")} |
| 17 | if err := guestos.BindWindowsProfile(profile, binding); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | gate, err := cage.NewEgressGate(cage.DefaultConfig()) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | stop, err := cage.StartGateControl(filepath.Join(profile, "config.json"), instance, gate) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | defer stop() |
| 29 | if err := closeManagedGates(profile); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if gate.Status().Mode != cage.GateClosed || gate.Status().Reason != "代理核心停止或重启,等待重新检测" { |
| 33 | t.Fatal("CLI proxy lifecycle missed the bound Windows gate") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestProxyChangeRejectsMalformedWindowsBinding(t *testing.T) { |
| 38 | profile := t.TempDir() |
| 39 | if err := os.WriteFile(guestos.WindowsProfilePath(profile), []byte(`{"version":1}`), 0600); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if err := closeManagedGates(profile); err == nil { |
| 43 | t.Fatal("proxy mutation ignored an invalid native guest binding") |
| 44 | } |
| 45 | } |