| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/cage" |
| 16 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 17 | "github.com/sylvinus/agent-vm/internal/host" |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | |
| 22 | func TestWindowsGuestSafetyWorkbench(t *testing.T) { |
| 23 | profile, executable := os.Getenv("CAGE_WINDOWS_GUEST_QA_PROFILE"), os.Getenv("CAGE_WINDOWS_GUEST_QA_EXECUTABLE") |
| 24 | if runtime.GOOS != "windows" || profile == "" || executable == "" { |
| 25 | t.Skip("requires an isolated paired Windows profile and current Cage executable") |
| 26 | } |
| 27 | binding, err := guestos.LoadWindowsProfile(profile) |
| 28 | if err != nil || binding == nil || binding.NetworkPairingFile == "" { |
| 29 | t.Fatal("paired packet-network profile unavailable") |
| 30 | } |
| 31 | s := testServer(t) |
| 32 | s.profile, s.configPath, s.exe, s.windowsGuest = profile, filepath.Join(profile, "config.json"), executable, binding |
| 33 | s.preferences = Preferences{Workspace: s.environmentDir(), Keyboard: "windows"} |
| 34 | s.lifetimeContext() |
| 35 | defer s.lifeCancel() |
| 36 | defer s.closeWindowsBrowser() |
| 37 | defer s.closeWindowsNetwork() |
| 38 | defer s.closeProjectShares() |
| 39 | if err = host.EnsurePrivateDir(s.environmentDir()); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | cfg, err := loadConfig(s.configPath) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if err = cfg.RequireManagedProxy(profile); err != nil { |
| 47 | t.Fatal("QA managed proxy configuration is not internally consistent") |
| 48 | } |
| 49 | ctx, cancel := context.WithTimeout(s.lifetimeContext(), 15*time.Second) |
| 50 | err = s.applyWindowsGuestSettings(ctx, cfg) |
| 51 | cancel() |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | server := httptest.NewServer(s.handler()) |
| 56 | defer server.Close() |
| 57 | s.origin = server.URL |
| 58 | request, _ := http.NewRequest("POST", server.URL+"/api/safety/check", strings.NewReader("{}")) |
| 59 | request.Header.Set("Origin", server.URL) |
| 60 | request.Header.Set("Content-Type", "application/json") |
| 61 | request.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 62 | request.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 63 | response, err := server.Client().Do(request) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | var job Job |
| 68 | err = json.NewDecoder(response.Body).Decode(&job) |
| 69 | response.Body.Close() |
| 70 | if err != nil || response.StatusCode != 200 || job.ID == "" { |
| 71 | t.Fatal("real safety job was not admitted") |
| 72 | } |
| 73 | deadline := time.Now().Add(190 * time.Second) |
| 74 | for s.jobs.snapshot().Running && time.Now().Before(deadline) { |
| 75 | time.Sleep(100 * time.Millisecond) |
| 76 | } |
| 77 | job = s.jobs.snapshot() |
| 78 | if job.Running { |
| 79 | s.jobs.cancelRunning() |
| 80 | t.Fatal("real safety job timed out") |
| 81 | } |
| 82 | s.mu.Lock() |
| 83 | report, gate := s.safety.Report, s.safety.Gate |
| 84 | s.mu.Unlock() |
| 85 | if report == nil { |
| 86 | t.Fatalf("real checks produced no report (job error: %s)", job.Error) |
| 87 | } |
| 88 | for _, check := range report.Checks { |
| 89 | t.Logf("actual check %s: %s", check.ID, check.State) |
| 90 | } |
| 91 | if job.Error != "" || gate == nil || gate.Mode != cage.GateOpen || gate.Report == nil { |
| 92 | t.Fatalf("real checks did not authorize the gate: %s", job.Error) |
| 93 | } |
| 94 | if err = report.Authorizes(gate.RunID, cfg.Fingerprint(), time.Now()); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | t.Log("Actual Windows workbench safety report authorized the selected proxy; no fabricated check or grant was used") |
| 98 | |
| 99 | |
| 100 | if os.Getenv("CAGE_WINDOWS_GUEST_QA_ACTIVATION_WINDOW") == "1" { |
| 101 | t.Log("Actual gate open; 45-second supervised QA activation window started") |
| 102 | until := time.Now().Add(45 * time.Second) |
| 103 | for time.Now().Before(until) { |
| 104 | s.monitorSafety() |
| 105 | time.Sleep(time.Second) |
| 106 | } |
| 107 | } |
| 108 | } |