| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/cage" |
| 12 | ) |
| 13 | |
| 14 | func TestGuestConnectionRequiresSuccessfulMatchingManifest(t *testing.T) { |
| 15 | required := cage.RequiredGuestManifest() |
| 16 | for _, name := range []string{"current", "missing", "old-revision", "old-protocol", "broken"} { |
| 17 | t.Run(name, func(t *testing.T) { |
| 18 | manifest := required |
| 19 | if name == "old-revision" { |
| 20 | manifest.Revision = "previous" |
| 21 | } |
| 22 | if name == "old-protocol" { |
| 23 | manifest.Protocol++ |
| 24 | } |
| 25 | encoded, _ := json.Marshal(manifest) |
| 26 | if name == "missing" { |
| 27 | encoded = []byte("null") |
| 28 | } |
| 29 | if name == "broken" { |
| 30 | encoded = []byte("{") |
| 31 | } |
| 32 | credentials, current, err := parseGuestConnection(append([]byte("{\"token\":\"guest-test\"}\npassword\n"), encoded...)) |
| 33 | if name == "current" { |
| 34 | if err != nil || current == nil || credentials.Token != "guest-test" || credentials.VNCPassword != "password" { |
| 35 | t.Fatal(current, err) |
| 36 | } |
| 37 | } else if !errors.Is(err, errGuestUpdateRequired) { |
| 38 | t.Fatalf("stale guest accepted: %v", err) |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func grantedToolUpdateServer(t *testing.T) (*Server, *cage.EgressGate) { |
| 45 | t.Helper() |
| 46 | s, gate := interfaceMonitorServer(t) |
| 47 | grantTestGate(t, gate) |
| 48 | s.ready = true |
| 49 | required := cage.RequiredGuestManifest() |
| 50 | s.guestManifest = &required |
| 51 | s.setJobForTest(Job{ID: "tools", Kind: "environment-tools", Running: true}) |
| 52 | s.publishGate(gate.Status()) |
| 53 | return s, gate |
| 54 | } |
| 55 | |
| 56 | func grantTestGate(t *testing.T, gate *cage.EgressGate) { |
| 57 | t.Helper() |
| 58 | state := gate.Status() |
| 59 | report := cage.SafetyReport{Policy: cage.SafetyPolicyVersion, ID: "tool-update-test", RunID: state.RunID, CheckID: state.CheckID, Configuration: state.Configuration, CreatedAt: time.Now()} |
| 60 | for _, id := range []string{"egress_policy", "proxy_transport", "dns_transport", "guest_settings", "browser_settings", "filesystem_boundary", "management_boundary", "fail_closed", "udp_policy", "ipv6_policy", "exit_consistency"} { |
| 61 | report.Checks = append(report.Checks, cage.SafetyCheck{ID: id, Title: id, Critical: true, State: cage.CheckPassed, Evidence: "synthetic isolated fixture", CheckedAt: report.CreatedAt}) |
| 62 | } |
| 63 | if err := gate.Grant(report); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestToolUpdateLeaseHonorsPauseStopAndConfigurationAtRenewal(t *testing.T) { |
| 69 | for _, jobKind := range []string{"environment-tools", "claude-desktop-install"} { |
| 70 | t.Run(jobKind, func(t *testing.T) { |
| 71 | for _, condition := range []string{"allowed", "paused", "stop", "configuration-changed"} { |
| 72 | t.Run(condition, func(t *testing.T) { |
| 73 | s, gate := grantedToolUpdateServer(t) |
| 74 | s.setJobForTest(Job{ID: "tools", Kind: jobKind, Running: true}) |
| 75 | client, err := s.gateClient() |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | before := gate.Status() |
| 80 | |
| 81 | |
| 82 | for !time.Now().Add(cage.GateLease).After(before.Until) { |
| 83 | time.Sleep(time.Millisecond) |
| 84 | } |
| 85 | switch condition { |
| 86 | case "paused": |
| 87 | if err := s.pauseNetworkNow("test user pause"); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | case "stop": |
| 91 | s.setJobForTest(Job{ID: "stop", Kind: "stop", Running: true}) |
| 92 | case "configuration-changed": |
| 93 | cfg, err := loadConfig(s.configPath) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | cfg.Timezone = "Etc/UTC" |
| 98 | if cfg.Fingerprint() == before.Configuration { |
| 99 | cfg.Timezone = "Asia/Tokyo" |
| 100 | } |
| 101 | if err := writePrivate(s.configPath, cfg); err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | } |
| 105 | s.finishSafetyMonitor(context.Background(), client, before, "192.0.2.1", "") |
| 106 | after := gate.Status() |
| 107 | switch condition { |
| 108 | case "allowed": |
| 109 | if after.Mode != cage.GateOpen || !after.Until.After(before.Until) { |
| 110 | t.Fatal("tool update lease was not renewed") |
| 111 | } |
| 112 | case "paused", "configuration-changed": |
| 113 | if after.Mode != cage.GateClosed { |
| 114 | t.Fatal("denied network was renewed") |
| 115 | } |
| 116 | case "stop": |
| 117 | if after.Until != before.Until { |
| 118 | t.Fatal("stopping job allowed a lease renewal") |
| 119 | } |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | }) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestToolUpdateDoesNotRestorePausedOrExpiredNetworking(t *testing.T) { |
| 128 | for _, paused := range []bool{true, false} { |
| 129 | s, gate := grantedToolUpdateServer(t) |
| 130 | if paused { |
| 131 | if err := s.pauseNetworkNow("manual"); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | } else { |
| 135 | gate.Close("expired") |
| 136 | } |
| 137 | s.mu.Lock() |
| 138 | allowed := s.toolsNetworkAllowedLocked() |
| 139 | s.mu.Unlock() |
| 140 | if !allowed { |
| 141 | t.Fatal("business network pause blocked maintenance") |
| 142 | } |
| 143 | if gate.Status().Mode != cage.GateClosed { |
| 144 | t.Fatal("tool update restored closed network") |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestEnvironmentVersionRoutesRequireHostSession(t *testing.T) { |
| 150 | s := testServer(t) |
| 151 | for _, path := range []string{"/api/environment/versions", "/api/environment/update", "/api/environment/tools"} { |
| 152 | method := "POST" |
| 153 | if strings.HasSuffix(path, "versions") { |
| 154 | method = "GET" |
| 155 | } |
| 156 | if w := request(s, method, path, s.origin, "{}", false); w.Code != 401 { |
| 157 | t.Fatalf("untrusted version action accepted: %d", w.Code) |
| 158 | } |
| 159 | } |
| 160 | w := request(s, "GET", "/api/environment/versions", s.origin, "", true) |
| 161 | if w.Code != 200 || !strings.Contains(w.Body.String(), `"status":"stopped"`) { |
| 162 | t.Fatal(w.Code, w.Body.String()) |
| 163 | } |
| 164 | } |