| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/cage" |
| 14 | ) |
| 15 | |
| 16 | func TestNetworkStatusImmediatelyAfterManualGrant(t *testing.T) { |
| 17 | for _, tc := range []struct{ failure, locationError string }{ |
| 18 | {"exit probe", ""}, |
| 19 | {"exit probe", "出口位置服务暂时不可用"}, |
| 20 | {"proxy core", "出口位置服务暂时不可用"}, |
| 21 | } { |
| 22 | t.Run(tc.failure+"/location-error="+tc.locationError, func(t *testing.T) { |
| 23 | s, gate := grantedToolUpdateServer(t) |
| 24 | s.setJobForTest(Job{}) |
| 25 | if tc.locationError != "" { |
| 26 | s.recordLocationProbeLocked(Location{IP: "192.0.2.1"}, errors.New(tc.locationError)) |
| 27 | } |
| 28 | s.recordLocationProbeLocked(Location{}, errors.New("出口 IP 暂时不可用")) |
| 29 | openBeforeFailure := gate.Status() |
| 30 | client, err := s.gateClient() |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | checkStatus := func(wantMode, wantError string) { |
| 35 | t.Helper() |
| 36 | response := httptest.NewRecorder() |
| 37 | s.status(response, httptest.NewRequest("GET", "/api/status", nil)) |
| 38 | var status struct { |
| 39 | NetworkError string `json:"network_error"` |
| 40 | Safety safetyState `json:"safety"` |
| 41 | } |
| 42 | if err := json.Unmarshal(response.Body.Bytes(), &status); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | errorMatches := status.NetworkError == wantError |
| 46 | if wantMode == cage.GateClosed && tc.failure == "exit probe" { |
| 47 | errorMatches = strings.HasPrefix(status.NetworkError, wantError+";") && strings.Contains(status.NetworkError, "自动重检") |
| 48 | } |
| 49 | if status.Safety.Gate == nil || status.Safety.Gate.Mode != wantMode || !errorMatches { |
| 50 | t.Fatalf("expected %s and %q, got %+v", wantMode, wantError, status) |
| 51 | } |
| 52 | if wantMode == cage.GateOpen && status.Safety.Error != "" { |
| 53 | t.Fatalf("open gate retained a safety error: %s", status.Safety.Error) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | issue := "代理出口探测超时" |
| 58 | if tc.failure == "proxy core" { |
| 59 | s.checkProxyHealth() |
| 60 | issue = "内置代理未就绪;外网保持关闭" |
| 61 | } else { |
| 62 | s.finishSafetyObservation(t.Context(), client, openBeforeFailure, "", issue, true) |
| 63 | } |
| 64 | s.publishGate(openBeforeFailure) |
| 65 | checkStatus(cage.GateClosed, issue) |
| 66 | |
| 67 | checking, err := client.BeginCheck(t.Context()) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | s.publishGate(checking) |
| 72 | checkStatus(cage.GateChecking, issue) |
| 73 | report := *checking.Report |
| 74 | report.ID, report.CheckID, report.CreatedAt = "manual-recheck", checking.CheckID, time.Now() |
| 75 | for i := range report.Checks { |
| 76 | report.Checks[i].CheckedAt = report.CreatedAt |
| 77 | } |
| 78 | opened, err := client.Grant(t.Context(), report) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | s.publishGate(opened) |
| 83 | checkStatus(cage.GateOpen, tc.locationError) |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestNetworkIntentDiagnosticLastsUntilDurableRepair(t *testing.T) { |
| 89 | s, gate := grantedToolUpdateServer(t) |
| 90 | path := filepath.Join(s.profile, "network-intent.json") |
| 91 | if err := os.WriteFile(path, []byte("{"), 0600); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | if err := s.restoreNetworkIntent(); err == nil { |
| 95 | t.Fatal("corrupt intent was accepted") |
| 96 | } |
| 97 | const locationFailure = "出口位置服务暂时不可用" |
| 98 | s.recordLocationProbeLocked(Location{IP: "192.0.2.1"}, errors.New(locationFailure)) |
| 99 | s.publishGate(gate.Status()) |
| 100 | if !s.networkPaused || s.currentNetworkErrorLocked() != s.networkIntentError || s.networkIntentError == "" { |
| 101 | t.Fatal("unrelated observations masked the invalid network intent") |
| 102 | } |
| 103 | if err := s.pauseNetworkNow("manual pause"); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if !s.networkPaused || gate.Status().Mode != cage.GateClosed || s.currentNetworkErrorLocked() != locationFailure { |
| 107 | t.Fatal("repairing the intent must clear only its diagnostic and preserve the pause") |
| 108 | } |
| 109 | if err := os.Remove(path); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if err := os.Mkdir(path, 0700); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | err := s.resumeNetworkIntentAt(s.safety.pauseVersion) |
| 116 | if err == nil || !s.networkPaused || s.networkIntentError == "" { |
| 117 | t.Fatal("failed persistence should keep the error and the pause") |
| 118 | } |
| 119 | if err := os.Remove(path); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if err := s.resumeNetworkIntentAt(s.safety.pauseVersion); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if s.networkPaused || s.currentNetworkErrorLocked() != locationFailure || gate.Status().Mode != cage.GateClosed { |
| 126 | t.Fatal("durable resume may clear the intent error, but cannot grant network access") |
| 127 | } |
| 128 | } |