| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "github.com/sylvinus/agent-vm/internal/cage" |
| 7 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func TestMacIPv6ScopedRoutesRequireCompleteLiveEvidence(t *testing.T) { |
| 13 | base := guestObservation{IPv6DefaultRoutes: 1, IPv6UnscopedAbsent: true, IPv6LinkSent: true, IPv6LinkInterface: "en0", |
| 14 | IPv6Unbound: macIPv6Probe{Result: "no-route"}, IPv6Scoped: []macIPv6Probe{{Name: "utun0", RouteVerified: true, BoundIndex: 7, Result: "timeout"}}} |
| 15 | before := cage.GateStatus{Mode: cage.GateChecking, RunID: "selected-vm", CheckID: "current-check"} |
| 16 | after := before |
| 17 | after.PolicyVerified = true |
| 18 | after.IPv6LinkProbeCheck = before.CheckID |
| 19 | if !macOSIPv6LinkVerified(base, before, after) { |
| 20 | t.Fatal("live matching IPv6 drop proof rejected") |
| 21 | } |
| 22 | for _, change := range []func(*guestObservation, *cage.GateStatus){ |
| 23 | func(_ *guestObservation, a *cage.GateStatus) { a.IPv6LinkProbeCheck = "old-check" }, |
| 24 | func(_ *guestObservation, a *cage.GateStatus) { a.RunID = "another-vm" }, |
| 25 | func(b *guestObservation, _ *cage.GateStatus) { b.IPv6Scoped[0].GlobalSources = 1 }, |
| 26 | func(b *guestObservation, _ *cage.GateStatus) { b.IPv6Scoped[0].Result = "connected" }, |
| 27 | func(b *guestObservation, _ *cage.GateStatus) { b.IPv6Scoped[0].BoundIndex = 0 }, |
| 28 | func(b *guestObservation, _ *cage.GateStatus) { b.IPv6UnscopedAbsent = false }, |
| 29 | func(b *guestObservation, _ *cage.GateStatus) { b.IPv6DefaultRoutes++ }, |
| 30 | } { |
| 31 | v, a := base, after |
| 32 | v.IPv6Scoped = append([]macIPv6Probe(nil), base.IPv6Scoped...) |
| 33 | change(&v, &a) |
| 34 | if macOSIPv6LinkVerified(v, before, a) { |
| 35 | t.Fatal("accepted incomplete interface or ingress observations") |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestMacOSStaticExitRequiresThreeMatchingSuccessfulSamples(t *testing.T) { |
| 41 | cfg := cage.DefaultConfig() |
| 42 | host := "8.8.8.8" |
| 43 | ok := networkObservation{Status: 200, IP: host} |
| 44 | sample := macOSStaticCandidate(cfg, "this-run", host, ok, ok) |
| 45 | if sample == nil || sample.IP != host || sample.Run != "this-run" || sample.Fingerprint != cfg.Fingerprint() { |
| 46 | t.Fatal("missing verified candidate") |
| 47 | } |
| 48 | for _, bad := range []networkObservation{{Status: 503, IP: host}, {Status: 200, IP: "8.8.4.4"}, {Status: 200}} { |
| 49 | if macOSStaticCandidate(cfg, "this-run", host, bad, ok) != nil || macOSStaticCandidate(cfg, "this-run", host, ok, bad) != nil { |
| 50 | t.Fatal("accepted different or incomplete egress samples") |
| 51 | } |
| 52 | } |
| 53 | cfg.ExitPolicy = cage.ExitPolicy{Mode: "static", IP: host} |
| 54 | if macOSStaticCandidate(cfg, "this-run", host, ok, ok) != nil { |
| 55 | t.Fatal("offered the existing binding again") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestMacOSWorkloadStartsNetworkSupervision(t *testing.T) { |
| 60 | s := testServer(t) |
| 61 | s.macosGuest = &guestos.MacOSProfile{Instance: "mac", User: "guestdev"} |
| 62 | checked := make(chan struct{}, 1) |
| 63 | previous := physicalProxyInterface |
| 64 | physicalProxyInterface = func(ctx context.Context) (string, error) { |
| 65 | select { |
| 66 | case checked <- struct{}{}: |
| 67 | default: |
| 68 | } |
| 69 | return "", errors.New("network disconnected") |
| 70 | } |
| 71 | defer func() { s.closeBackground(); physicalProxyInterface = previous }() |
| 72 | s.startMonitors() |
| 73 | select { |
| 74 | case <-checked: |
| 75 | case <-time.After(7 * time.Second): |
| 76 | t.Fatal("the running macOS workbench never started its real network monitor") |
| 77 | } |
| 78 | } |