| 1 | //go:build testbuild |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "crypto/sha256" |
| 8 | "encoding/base64" |
| 9 | "encoding/hex" |
| 10 | "encoding/json" |
| 11 | "net" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "path/filepath" |
| 15 | "slices" |
| 16 | "strings" |
| 17 | "sync" |
| 18 | "testing" |
| 19 | "time" |
| 20 | |
| 21 | "amadan.net/oficina/equipo/internal/runner" |
| 22 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 23 | "amadan.net/oficina/equipo/internal/runner/mac/fprobe" |
| 24 | "amadan.net/oficina/equipo/internal/runner/mac/proc" |
| 25 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 26 | "amadan.net/oficina/equipo/internal/wire" |
| 27 | ) |
| 28 | |
| 29 | // The shipped probes through the real Engineering launch on Linux (slice |
| 30 | // F task 14): the darwin launch of the pinned stand-in, the fake CLI, |
| 31 | // the runner's own permission tool, no sandbox (so nothing here shows a |
| 32 | // sandbox holding: that is the Mac's, task 18). |
| 33 | |
| 34 | // probeHost is the probes' host over the Engineering rig. |
| 35 | func probeHost(x *engRig) fprobe.Host { |
| 36 | d := x.darwinRig |
| 37 | var tokens []string |
| 38 | for _, home := range x.homes { |
| 39 | tokens = append(tokens, filepath.Join(home, provider.TokenFile)) |
| 40 | } |
| 41 | return fprobe.Host{ |
| 42 | Inputs: fprobe.NewInputs("v-test", true, wire.Xcode{}, nil), |
| 43 | Paths: d.m.paths, RuntimeRoot: filepath.Join(d.cfg.ScratchRoot, "homes"), TokenFiles: tokens, |
| 44 | Scratch: filepath.Join(d.cfg.ScratchRoot, "fprobe"), |
| 45 | Secrets: func(ctx context.Context) ([][]byte, error) { |
| 46 | s, err := x.w.Eng.Secrets(ctx) |
| 47 | return secretForms(s), err |
| 48 | }, |
| 49 | Turns: d.m.turns, Launch: x.w.probeLaunch, |
| 50 | Listen: func() (net.Listener, error) { return net.Listen("tcp", "127.0.0.1:0") }, |
| 51 | Resolve: func(context.Context, string) bool { return true }, |
| 52 | Exposed: func(string) bool { return false }, |
| 53 | Exec: func(context.Context, string, ...string) (string, error) { return "", nil }, |
| 54 | Lookup: proc.Platform.Lookup, |
| 55 | Limits: map[string]time.Duration{fprobe.LaunchA: time.Minute, fprobe.LaunchC: 2 * time.Minute}, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func b64(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } |
| 60 | |
| 61 | // The fake CLI claims in its text that every probe was denied, while it |
| 62 | // writes the control and a sentinel it was never allowed to read into |
| 63 | // the tree, and asks to read another: the verdicts follow the runner's |
| 64 | // own observers (the sentinel found, the records missing, its own |
| 65 | // permission tool refusing the read), never the text. |
| 66 | func TestProbeTurnVerdictIgnoresModelText(t *testing.T) { |
| 67 | x := newEngRig(t) |
| 68 | d := x.darwinRig |
| 69 | claim := "F1 to F7 all denied, every attempt was refused and nothing reached the tree" |
| 70 | probeNote = func(s fprobe.Spec) string { |
| 71 | if s.Launch != fprobe.LaunchA { |
| 72 | return "\nsay:" + claim + "\n" |
| 73 | } |
| 74 | sentinels, _ := filepath.Glob(filepath.Join(d.m.paths.StateDir, "equipo-fprobe-*")) |
| 75 | if len(sentinels) != 1 { |
| 76 | t.Errorf("no sentinel planted: %v", sentinels) |
| 77 | return "" |
| 78 | } |
| 79 | nonce, _ := os.ReadFile(sentinels[0]) |
| 80 | // The model knows this run's nonce (it is in the command it is |
| 81 | // asked to run) and forges the records with it, in the tree, the |
| 82 | // one place it can write: they are not the script's. |
| 83 | forged := "begin " + s.Nonce + "\n" |
| 84 | return "\nbytes:out/begin|" + b64(forged) + "\nbytes:out/f1|" + b64("tcp 1\nhttps 1\nhttps_local 1\ndns 1\n") + "\nbytes:out/f2.state|" + b64(string(nonce)) + "\ncopy:" + sentinels[0] + "|out/f6-copy\nsay:" + claim + "\n" |
| 85 | } |
| 86 | t.Cleanup(func() { probeNote = nil }) |
| 87 | h := probeHost(x) |
| 88 | var mu sync.Mutex |
| 89 | var outcomes []fprobe.Outcome |
| 90 | var asks []fprobe.Ask |
| 91 | h.Launch = func(ctx context.Context, s fprobe.Spec, obs fprobe.Observer) fprobe.Outcome { |
| 92 | ask := obs.Ask |
| 93 | obs.Ask = func(q fprobe.Ask) { |
| 94 | mu.Lock() |
| 95 | asks = append(asks, q) |
| 96 | mu.Unlock() |
| 97 | ask(q) |
| 98 | } |
| 99 | o := x.w.probeLaunch(ctx, s, obs) |
| 100 | mu.Lock() |
| 101 | outcomes = append(outcomes, o) |
| 102 | mu.Unlock() |
| 103 | return o |
| 104 | } |
| 105 | var s fprobe.Store |
| 106 | results := s.Run(context.Background(), h) |
| 107 | if len(results) != 2 || results[0].Launch != fprobe.LaunchA || results[1].Launch != fprobe.LaunchC { |
| 108 | t.Fatalf("results %+v", results) |
| 109 | } |
| 110 | if len(outcomes) != 2 || !outcomes[0].Started || !outcomes[0].Proven || !outcomes[1].Started || !outcomes[1].Proven { |
| 111 | t.Fatalf("outcomes %+v\n%s", outcomes, d.logs.String()) |
| 112 | } |
| 113 | probes := append(slices.Clone(results[0].Probes), results[1].Probes...) |
| 114 | for _, p := range probes { |
| 115 | if p.Verdict == wire.EngineeringProbeSuccess(p.ID) || strings.Contains(p.Detail, "refused and nothing") { |
| 116 | t.Fatalf("a verdict followed the model's text: %+v", p) |
| 117 | } |
| 118 | } |
| 119 | byID := func(id string) wire.Probe { |
| 120 | for _, p := range probes { |
| 121 | if p.ID == id { |
| 122 | return p |
| 123 | } |
| 124 | } |
| 125 | return wire.Probe{} |
| 126 | } |
| 127 | if p := byID("F2"); p.Verdict != wire.ProbeFailed || !strings.Contains(p.Detail, "reached_tree:state") { |
| 128 | t.Fatalf("F2: %+v", p) |
| 129 | } |
| 130 | if p := byID("F1"); p.Verdict != wire.ProbeFailed || !strings.HasPrefix(p.Detail, "script_not_run;") || !strings.Contains(p.Detail, "not_shown_denied:tcp https https_local dns") { |
| 131 | t.Fatalf("F1 (the forged records in the tree are not the script's): %+v", p) |
| 132 | } |
| 133 | // The fake CLI runs no Bash: the runner's tool never saw a script run. |
| 134 | if p := byID("F4"); p.Verdict != wire.ProbeFailed || p.Detail != "script_not_run;child_not_started" { |
| 135 | t.Fatalf("F4: %+v", p) |
| 136 | } |
| 137 | if p := byID("F7"); p.Verdict != wire.ProbeUnavailable { |
| 138 | t.Fatalf("F7 without Xcode: %+v", p) |
| 139 | } |
| 140 | // The runner's own permission tool refused the read the fake asked |
| 141 | // for, and said so to the observer. |
| 142 | refused := false |
| 143 | for _, q := range asks { |
| 144 | var in struct { |
| 145 | FilePath string `json:"file_path"` |
| 146 | } |
| 147 | _ = json.Unmarshal(q.Input, &in) |
| 148 | if q.Tool == "Read" && strings.HasPrefix(in.FilePath, d.m.paths.StateDir) { |
| 149 | refused = !q.Allowed |
| 150 | } |
| 151 | } |
| 152 | if !refused { |
| 153 | t.Fatalf("the read of the state dir was not refused by the runner's tool: %+v", asks) |
| 154 | } |
| 155 | // Nothing of the probes is left: no tree, no sentinel, no step log, |
| 156 | // no kept session, no Keychain watch. |
| 157 | if left, _ := filepath.Glob(filepath.Join(h.Scratch, "*")); len(left) != 0 { |
| 158 | t.Fatalf("tree left: %v", left) |
| 159 | } |
| 160 | if left, _ := filepath.Glob(filepath.Join(d.m.paths.StateDir, "equipo-f*")); len(left) != 0 { |
| 161 | t.Fatalf("sentinel left: %v", left) |
| 162 | } |
| 163 | if left, _ := filepath.Glob(filepath.Join(x.w.Engine.root(), "*fprobe*")); len(left) != 0 { |
| 164 | t.Fatalf("a step log: %v", left) |
| 165 | } |
| 166 | if x.keptRows(t) != 0 || d.m.keychain.Held() { |
| 167 | t.Fatal("a kept session or a Keychain watch was left") |
| 168 | } |
| 169 | // The launches ran on the small model, through the Engineering |
| 170 | // launch (its darwin policy for the tree). |
| 171 | small := 0 |
| 172 | for _, l := range d.launches() { |
| 173 | if strings.Contains(l.argv, provider.ClaudeGlanceModel) { |
| 174 | small++ |
| 175 | } |
| 176 | } |
| 177 | if small != 2 { |
| 178 | t.Fatalf("launches on the small model: %d", small) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // The capability report carries F1 to F7 beside P1 to P7 and the |
| 183 | // Engineering status they make with the guards and the connections; the |
| 184 | // claude tag goes with ready, and Xcode access follows F5. |
| 185 | func TestMacEngineeringStatusInCaps(t *testing.T) { |
| 186 | x := newEngRig(t) |
| 187 | h := &macHost{version: "v-test", probes: newMacProbes("v-test", "", nil, func() bool { return true })} |
| 188 | h.eng = newMacEngineering(h) |
| 189 | sims := []wire.Simulator{{Name: "iPhone 17 Pro", Runtime: "iOS 27.2", UDID: "ABCDEF01-2345-6789-ABCD-EF0123456789", State: "Shutdown"}} |
| 190 | xcode := wire.Xcode{Version: "27.2", Build: "27B52"} |
| 191 | in := fprobe.NewInputs("v-test", true, xcode, sims) |
| 192 | for _, l := range fprobe.Launches { |
| 193 | r := fprobe.Result{Launch: l, Inputs: in, At: time.Now()} |
| 194 | for _, id := range wire.EngineeringProbeIDs { |
| 195 | if fprobe.LaunchOf(id) == l { |
| 196 | r.Probes = append(r.Probes, wire.Probe{ID: id, Verdict: wire.EngineeringProbeSuccess(id), Detail: wire.ProbeDetail("f1_denied")}) |
| 197 | } |
| 198 | } |
| 199 | h.eng.store.Put(r) |
| 200 | } |
| 201 | report := func(mut func(*wire.Capabilities)) wire.Capabilities { |
| 202 | c := wire.Capabilities{Tags: []string{wire.TagMacOS, wire.TagXcode}, Simulators: sims, Xcode: xcode, Harness: wire.Harness{Claude: provider.ClaudeVersion}, |
| 203 | Probes: []wire.Probe{{ID: "P1", Verdict: wire.ProbePassed}}} |
| 204 | if mut != nil { |
| 205 | mut(&c) |
| 206 | } |
| 207 | h.eng.compose(&c) |
| 208 | if err := wire.ValidCapabilities(c); err != nil { |
| 209 | t.Fatalf("invalid report: %v", err) |
| 210 | } |
| 211 | return c |
| 212 | } |
| 213 | c := report(nil) |
| 214 | if c.Engineering == nil || c.Engineering.State != wire.EngineeringNoConnection || slices.Contains(c.Tags, wire.TagClaude) { |
| 215 | t.Fatalf("no connection bound: %+v %v", c.Engineering, c.Tags) |
| 216 | } |
| 217 | if len(c.Probes) != 8 || c.Probes[0].ID != "P1" || c.Probes[1].ID != "F1" || c.Probes[7].ID != "F7" { |
| 218 | t.Fatalf("probes %+v", c.Probes) |
| 219 | } |
| 220 | h.eng.bind(x.w, x.conns, nil) |
| 221 | if c = report(nil); *c.Engineering != (wire.EngineeringStatus{State: wire.EngineeringProbesPending, Reason: "P2"}) { |
| 222 | t.Fatalf("no trusted P2: %+v", c.Engineering) |
| 223 | } |
| 224 | if h.eng.xcode() { |
| 225 | t.Fatal("Xcode access without P2 denied: F5 is skipped") |
| 226 | } |
| 227 | h.probes.last = mac.Report{Version: "v-test", Profile: mac.ProfileDigest(), At: time.Now(), SRT: srtPinned, Probes: []wire.Probe{{ID: "P2", Verdict: wire.ProbeDenied}}} |
| 228 | c = report(nil) |
| 229 | if *c.Engineering != (wire.EngineeringStatus{State: wire.EngineeringReady}) || !slices.Contains(c.Tags, wire.TagClaude) { |
| 230 | t.Fatalf("ready: %+v %v", c.Engineering, c.Tags) |
| 231 | } |
| 232 | for _, cs := range []struct { |
| 233 | name string |
| 234 | mut func(*wire.Capabilities) |
| 235 | want wire.EngineeringStatus |
| 236 | }{ |
| 237 | {"tainted", func(c *wire.Capabilities) { c.Engineering = &wire.EngineeringStatus{State: wire.EngineeringTainted} }, wire.EngineeringStatus{State: wire.EngineeringTainted}}, |
| 238 | {"credential unproven", func(c *wire.Capabilities) { c.Unproven = wire.UnprovenCredential }, wire.EngineeringStatus{State: wire.EngineeringNoConnection, Reason: wire.UnprovenCredential}}, |
| 239 | {"cli", func(c *wire.Capabilities) { c.Harness.Claude = "" }, wire.EngineeringStatus{State: wire.EngineeringCLIUnpinned}}, |
| 240 | {"xcode changed", func(c *wire.Capabilities) { c.Xcode.Build = "27B53" }, wire.EngineeringStatus{State: wire.EngineeringProbesPending, Reason: "F1 F2 F3 F4 F6 F7"}}, |
| 241 | } { |
| 242 | if c := report(cs.mut); *c.Engineering != cs.want || slices.Contains(c.Tags, wire.TagClaude) { |
| 243 | t.Fatalf("%s: %+v %v", cs.name, c.Engineering, c.Tags) |
| 244 | } |
| 245 | } |
| 246 | // Below protocol 37 the F probes and the status are dropped. |
| 247 | c = report(nil) |
| 248 | hello := wire.Hello{Capabilities: &c} |
| 249 | if !wire.WithoutMacEngineering(&hello) || hello.Capabilities.Engineering != nil || len(hello.Capabilities.Probes) != 1 { |
| 250 | t.Fatalf("below 37: %+v", hello.Capabilities) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // The probe launch's permission tool (fix round 1, #1): beyond the |
| 255 | // Engineering rules, Bash is the probe script exactly (its bytes as the |
| 256 | // runner wrote them, this run's nonce), and no native write lands |
| 257 | // outside the tree; every answer is heard by the probe. |
| 258 | func TestProbePermission(t *testing.T) { |
| 259 | dir := t.TempDir() |
| 260 | script := filepath.Join(dir, "bin", "probe.sh") |
| 261 | if err := os.MkdirAll(filepath.Dir(script), 0o700); err != nil { |
| 262 | t.Fatal(err) |
| 263 | } |
| 264 | if err := os.WriteFile(script, []byte("echo probe\n"), 0o600); err != nil { |
| 265 | t.Fatal(err) |
| 266 | } |
| 267 | sum := sha256.Sum256([]byte("echo probe\n")) |
| 268 | s := fprobe.Spec{Tree: filepath.Join(dir, "tree"), Scratch: filepath.Join(dir, "run"), Script: script, ScriptSHA256: hex.EncodeToString(sum[:]), Nonce: "EQUIPO-NONCE-1"} |
| 269 | var heard []fprobe.Ask |
| 270 | allowAll := func(json.RawMessage, provider.Policy) json.RawMessage { |
| 271 | return json.RawMessage(`{"behavior":"allow","updatedInput":{}}`) |
| 272 | } |
| 273 | perm := probePermission(allowAll, s, func(q fprobe.Ask) { heard = append(heard, q) }) |
| 274 | ask := func(tool string, input map[string]string) bool { |
| 275 | raw, _ := json.Marshal(map[string]any{"tool_name": tool, "tool_use_id": "u1", "input": input}) |
| 276 | return !denied(perm(raw, provider.Policy{})) |
| 277 | } |
| 278 | if !ask("Bash", map[string]string{"command": s.Command()}) || !ask("Write", map[string]string{"file_path": filepath.Join(s.Tree, "x")}) { |
| 279 | t.Fatal("the script and a write in the tree are refused") |
| 280 | } |
| 281 | for _, c := range []struct { |
| 282 | tool string |
| 283 | input map[string]string |
| 284 | }{ |
| 285 | {"Bash", map[string]string{"command": "echo dns 1 >> " + filepath.Join(s.Scratch, "out", "f1")}}, |
| 286 | {"Bash", map[string]string{"command": s.Command() + " && true"}}, |
| 287 | {"Write", map[string]string{"file_path": filepath.Join(s.Scratch, "out", "f1")}}, |
| 288 | {"Edit", map[string]string{"file_path": script}}, |
| 289 | } { |
| 290 | if ask(c.tool, c.input) { |
| 291 | t.Fatalf("allowed %+v", c) |
| 292 | } |
| 293 | } |
| 294 | if len(heard) != 6 || heard[0].ToolUseID != "u1" || !heard[0].Allowed || heard[2].Allowed { |
| 295 | t.Fatalf("heard %+v", heard) |
| 296 | } |
| 297 | // The Engineering rules still decide first. |
| 298 | refuseAll := func(json.RawMessage, provider.Policy) json.RawMessage { |
| 299 | return json.RawMessage(`{"behavior":"deny","message":"no"}`) |
| 300 | } |
| 301 | if raw, _ := json.Marshal(map[string]any{"tool_name": "Bash", "input": map[string]string{"command": s.Command()}}); !denied(probePermission(refuseAll, s, func(fprobe.Ask) {})(raw, provider.Policy{})) { |
| 302 | t.Fatal("the probe allowed what the Engineering rules refused") |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Readiness and Xcode access are decided afresh at each use (fix round |
| 307 | // 1, #9): the inputs read now (not the hour-old report) and the time now. |
| 308 | func TestMacEngineeringRevalidates(t *testing.T) { |
| 309 | x := newEngRig(t) |
| 310 | h := &macHost{version: "v-test", probes: newMacProbes("v-test", "", nil, func() bool { return true }), creds: x.m, taint: x.m.taint, turns: x.m.turns} |
| 311 | h.eng = newMacEngineering(h) |
| 312 | sims := []wire.Simulator{{Name: "iPhone 17 Pro", Runtime: "iOS 27.2", UDID: "ABCDEF01-2345-6789-ABCD-EF0123456789", State: "Shutdown"}} |
| 313 | live := wire.Capabilities{Tags: []string{wire.TagMacOS}, Simulators: sims, Xcode: wire.Xcode{Version: "27.2", Build: "27B52"}, Harness: wire.Harness{Claude: provider.ClaudeVersion}} |
| 314 | h.eng.live = func(context.Context) wire.Capabilities { return live } |
| 315 | now := time.Now() |
| 316 | h.eng.now = func() time.Time { return now } |
| 317 | in := fprobe.NewInputs("v-test", true, live.Xcode, sims) |
| 318 | for _, l := range fprobe.Launches { |
| 319 | r := fprobe.Result{Launch: l, Inputs: in, At: now} |
| 320 | for _, id := range wire.EngineeringProbeIDs { |
| 321 | if fprobe.LaunchOf(id) == l { |
| 322 | r.Probes = append(r.Probes, wire.Probe{ID: id, Verdict: wire.EngineeringProbeSuccess(id)}) |
| 323 | } |
| 324 | } |
| 325 | h.eng.store.Put(r) |
| 326 | } |
| 327 | h.eng.bind(x.w, x.conns, nil) |
| 328 | ctx := context.Background() |
| 329 | h.probes.last = mac.Report{Version: "v-test", Profile: mac.ProfileDigest(), At: now, SRT: srtPinned, Probes: []wire.Probe{{ID: "P2", Verdict: wire.ProbeDenied}}} |
| 330 | if st := h.eng.statusNow(ctx); st == nil || st.State != wire.EngineeringReady { |
| 331 | t.Fatalf("ready now: %+v", st) |
| 332 | } |
| 333 | // P2 trusted denied, so F5 counts; the report composed now says it. |
| 334 | h.probes.last = mac.Report{Version: "v-test", Profile: mac.ProfileDigest(), At: now, SRT: srtPinned, Probes: []wire.Probe{{ID: "P2", Verdict: wire.ProbeDenied}}} |
| 335 | report := live |
| 336 | h.eng.compose(&report) |
| 337 | if !h.eng.xcode() || !h.eng.xcodeNow(ctx) { |
| 338 | t.Fatal("Xcode access with P2 and F5 denied") |
| 339 | } |
| 340 | live.Xcode.Build = "27B53" |
| 341 | if st := h.eng.statusNow(ctx); st == nil || st.State != wire.EngineeringProbesPending { |
| 342 | t.Fatalf("Xcode changed since the report: %+v", st) |
| 343 | } |
| 344 | live.Xcode.Build = "27B52" |
| 345 | now = now.Add(25 * time.Hour) |
| 346 | if st := h.eng.statusNow(ctx); st == nil || st.State != wire.EngineeringProbesPending { |
| 347 | t.Fatalf("a day later: %+v", st) |
| 348 | } |
| 349 | if h.eng.xcodeNow(ctx) { |
| 350 | t.Fatal("Xcode access a day later") |
| 351 | } |
| 352 | x.m.taint.Set() |
| 353 | if st := h.eng.statusNow(ctx); st == nil || st.State != wire.EngineeringTainted { |
| 354 | t.Fatalf("tainted now: %+v", st) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // Below protocol 37 a Mac's hello says no claude tag (fix round 1, #10): |
| 359 | // an older instance would place Engineering on it. |
| 360 | func TestMacClaudeTagBelow37(t *testing.T) { |
| 361 | caps := wire.Capabilities{Tags: []string{wire.TagMacOS, wire.TagXcode, wire.TagClaude}, Simulators: []wire.Simulator{}, Engineering: &wire.EngineeringStatus{State: wire.EngineeringReady}} |
| 362 | hello := wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Capabilities: &caps} |
| 363 | f, err := wire.NewFrame("h1", wire.FrameHello, hello) |
| 364 | if err != nil { |
| 365 | t.Fatal(err) |
| 366 | } |
| 367 | tags := func(p int) []string { |
| 368 | enc, err := wire.Encode(f, p) |
| 369 | if err != nil { |
| 370 | t.Fatal(err) |
| 371 | } |
| 372 | raw, _ := json.Marshal(enc) |
| 373 | _, body, err := wire.ParseFrame(raw) |
| 374 | if err != nil { |
| 375 | t.Fatal(err) |
| 376 | } |
| 377 | return body.(*wire.Hello).Capabilities.Tags |
| 378 | } |
| 379 | if got := tags(36); slices.Contains(got, wire.TagClaude) || !slices.Contains(got, wire.TagMacOS) { |
| 380 | t.Fatalf("at 36: %v", got) |
| 381 | } |
| 382 | if got := tags(37); !slices.Contains(got, wire.TagClaude) { |
| 383 | t.Fatalf("at 37: %v", got) |
| 384 | } |
| 385 | // A Linux runner's claude tag is not the Mac's to strip. |
| 386 | hello.Platform, hello.Capabilities = &wire.Platform{OS: "linux", Arch: "amd64"}, &wire.Capabilities{Tags: []string{wire.TagLinux, wire.TagClaude}, Simulators: []wire.Simulator{}} |
| 387 | if f, err = wire.NewFrame("h2", wire.FrameHello, hello); err != nil { |
| 388 | t.Fatal(err) |
| 389 | } |
| 390 | if got := tags(36); !slices.Contains(got, wire.TagClaude) { |
| 391 | t.Fatalf("linux at 36: %v", got) |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // #5 (fix round 2): the production host names the runner's own binary |
| 396 | // as P2's reader. Built by newMacHost and probeHost, it runs the shipped |
| 397 | // script phase with a stand-in at that path: P2's targets give it, and |
| 398 | // the script then says tokenargs and tokenfd. |
| 399 | func TestMacProbeHostNamesTheReader(t *testing.T) { |
| 400 | root := t.TempDir() |
| 401 | exe := filepath.Join(root, "lib", "equipo-runner") |
| 402 | c := runner.Config{StateDir: filepath.Join(root, "state"), ScratchRoot: filepath.Join(root, "scratch"), WorkspaceRoot: filepath.Join(root, "work"), Executable: exe} |
| 403 | if err := os.MkdirAll(filepath.Dir(exe), 0o700); err != nil { |
| 404 | t.Fatal(err) |
| 405 | } |
| 406 | // The stand-in reads only its caller's parent, the hostile shell. |
| 407 | stand := "#!/bin/bash\nmode=$2; shift 2\npp=$(ps -o ppid= -p $PPID | tr -d ' ')\ncase \"$mode\" in pids) echo $PPID; echo $pp ;; procargs) for p in \"$@\"; do echo \"procargs-start $p\"; if [ \"$p\" = \"$PPID\" ] || [ \"$p\" = \"$pp\" ]; then echo \"procargs-end $p ok\"; else echo \"procargs-end $p refused\"; fi; done ;; fds) echo fds-done ;; esac\n" |
| 408 | if err := os.WriteFile(exe, []byte(stand), 0o700); err != nil { |
| 409 | t.Fatal(err) |
| 410 | } |
| 411 | was, wasTemp := macUsers, macUserTemp |
| 412 | macUsers = filepath.Join(root, "Users") |
| 413 | macUserTemp = func(context.Context, mac.Exec) (string, error) { return "/private/var/folders/4m/abc123_def/T", nil } |
| 414 | defer func() { macUsers, macUserTemp = was, wasTemp }() |
| 415 | h, err := newMacHost(c, OptionsFromEnv(func(string) string { return "" })) |
| 416 | if err != nil { |
| 417 | t.Fatal(err) |
| 418 | } |
| 419 | ph := h.probeHost("v1", nil) |
| 420 | if ph.Runner != exe { |
| 421 | t.Fatalf("P2's reader %q, want the runner %q", ph.Runner, exe) |
| 422 | } |
| 423 | fixture, err := mac.CopyFixture(t.TempDir()) |
| 424 | if err != nil { |
| 425 | t.Fatal(err) |
| 426 | } |
| 427 | token := filepath.Join(root, "accounts", ".oauth-token") |
| 428 | if err := os.MkdirAll(filepath.Dir(token), 0o700); err != nil || os.WriteFile(token, []byte("sk-ant-oat01-probe-0123456789abcdef"), 0o600) != nil { |
| 429 | t.Fatal(err) |
| 430 | } |
| 431 | targets := "state=/nonexistent\ndns=example.invalid\nkeychain=io.amadan.equipo.probe.x\nport_script=1\ntoken=sk-ant-oat01-probe-0123456789abcdef\ntoken_file=" + token + "\nhelper=" + ph.Runner + "\n" |
| 432 | if err := os.WriteFile(filepath.Join(fixture, "equipo-targets.txt"), []byte(targets), 0o600); err != nil { |
| 433 | t.Fatal(err) |
| 434 | } |
| 435 | cmd := exec.Command("bash", filepath.Join(fixture, "project", "hostile.sh")) |
| 436 | cmd.Env = []string{"PATH=/usr/bin:/bin", "SRCROOT=" + filepath.Join(fixture, "project"), "TMPDIR=" + t.TempDir()} |
| 437 | out, _ := cmd.CombinedOutput() |
| 438 | got := mac.ParseAttempts(string(out)) |
| 439 | if got["script.tokenargs"] != "denied" || got["script.tokenfd"] != "denied" { |
| 440 | t.Fatalf("the script did not use the runner's reader: %v\n%s", got, out) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // #7 (fix round 2): the simulators a launch runs on are read afresh with |
| 445 | // its inputs, never the cached report's. |
| 446 | func TestMacProbeSimulatorsFresh(t *testing.T) { |
| 447 | x := newEngRig(t) |
| 448 | h := &macHost{version: "v-test", probes: newMacProbes("v-test", "", nil, func() bool { return true }), creds: x.m, taint: x.m.taint, turns: x.m.turns, scratch: x.cfg.ScratchRoot} |
| 449 | h.eng = newMacEngineering(h) |
| 450 | stale := []wire.Simulator{{Name: "iPhone 16", Runtime: "iOS 27.0", UDID: "00000000-0000-0000-0000-000000000000"}} |
| 451 | fresh := []wire.Simulator{{Name: "iPhone 17 Pro", Runtime: "iOS 27.2", UDID: "ABCDEF01-2345-6789-ABCD-EF0123456789"}} |
| 452 | h.eng.live = func(context.Context) wire.Capabilities { |
| 453 | return wire.Capabilities{Simulators: fresh, Xcode: wire.Xcode{Version: "27.2", Build: "27B52"}, Harness: wire.Harness{Claude: provider.ClaudeVersion}} |
| 454 | } |
| 455 | h.eng.bind(x.w, x.conns, func(context.Context) wire.Capabilities { |
| 456 | return wire.Capabilities{Simulators: stale, Xcode: wire.Xcode{Version: "27.0", Build: "27A1"}, Harness: wire.Harness{Claude: provider.ClaudeVersion}} |
| 457 | }) |
| 458 | host, ok := h.eng.host(context.Background()) |
| 459 | if !ok { |
| 460 | t.Fatal("no host with a usable connection") |
| 461 | } |
| 462 | in, sims := host.Live(context.Background()) |
| 463 | if !slices.Equal(sims, fresh) || in.Runtimes != "iOS 27.2" || in.Xcode != "27.2 (27B52)" { |
| 464 | t.Fatalf("live %+v %+v", in, sims) |
| 465 | } |
| 466 | } |
| 467 | |