| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "sync/atomic" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/sylvinus/agent-vm/internal/cage" |
| 13 | ) |
| 14 | |
| 15 | func TestLocationObservationRevokesUnsafeExitAndIgnoresPreviousRoute(t *testing.T) { |
| 16 | for _, condition := range []string{"mainland", "listed IP", "unknown", "previous route"} { |
| 17 | t.Run(condition, func(t *testing.T) { |
| 18 | s, gate := grantedToolUpdateServer(t) |
| 19 | cfg, _ := loadConfig(s.configPath) |
| 20 | loc := Location{IP: "8.8.8.8", CountryCode: "CN", CheckedAt: time.Now()} |
| 21 | s.locations = map[string]Location{"8.8.8.8": {IP: "8.8.8.8", CountryCode: "US", CheckedAt: time.Now()}} |
| 22 | var probeErr error |
| 23 | switch condition { |
| 24 | case "listed IP": |
| 25 | loc.IP, loc.CountryCode = "223.5.5.5", "US" |
| 26 | case "unknown": |
| 27 | loc.CountryCode, probeErr = "", errors.New("location unavailable") |
| 28 | case "previous route": |
| 29 | cfg.ProxyRevision = "previous-route" |
| 30 | } |
| 31 | if err := s.finishLocationProbe(cfg, loc, probeErr); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | want := cage.GateClosed |
| 35 | if condition == "previous route" { |
| 36 | want = cage.GateOpen |
| 37 | } |
| 38 | if gate.Status().Mode != want { |
| 39 | t.Fatalf("gate mode=%s want=%s", gate.Status().Mode, want) |
| 40 | } |
| 41 | if condition == "unknown" || condition == "mainland" { |
| 42 | if _, exists := s.locations["8.8.8.8"]; exists { |
| 43 | t.Fatal("stale country evidence survived a new rejection") |
| 44 | } |
| 45 | } |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestExitCountryCannotBeSkippedByStaticOrUnrestrictedMode(t *testing.T) { |
| 51 | for _, policy := range []cage.ExitPolicy{{Mode: "static", IP: "8.8.8.8"}, {Mode: "dynamic", Scope: "any"}} { |
| 52 | t.Run(policy.Mode, func(t *testing.T) { |
| 53 | s := testServer(t) |
| 54 | var calls atomic.Int32 |
| 55 | proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 56 | calls.Add(1) |
| 57 | w.WriteHeader(http.StatusServiceUnavailable) |
| 58 | })) |
| 59 | defer proxy.Close() |
| 60 | cfg := cage.DefaultConfig() |
| 61 | cfg.ProxyURL, cfg.ExitPolicy = proxy.URL, policy |
| 62 | if state, _ := s.checkExitPolicy(context.Background(), cfg, "8.8.8.8"); state != cage.CheckUnknown || calls.Load() == 0 { |
| 63 | t.Fatal("unavailable country lookup did not block or bypassed proxy") |
| 64 | } |
| 65 | s.locations = map[string]Location{"8.8.8.8": {IP: "8.8.8.8", CountryCode: "CN", CheckedAt: time.Now()}} |
| 66 | if state, _ := s.checkExitPolicy(context.Background(), cfg, "8.8.8.8"); state != cage.CheckFailed { |
| 67 | t.Fatal("mainland geolocation accepted") |
| 68 | } |
| 69 | s.locations["8.8.8.8"] = Location{IP: "8.8.8.8", CountryCode: "US", CheckedAt: time.Now()} |
| 70 | if state, _ := s.checkExitPolicy(context.Background(), cfg, "8.8.8.8"); state != cage.CheckPassed { |
| 71 | t.Fatal("verified non-mainland exit rejected") |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |