| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "sync/atomic" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/cage" |
| 16 | "github.com/sylvinus/agent-vm/internal/managedproxy" |
| 17 | "golang.org/x/crypto/ssh" |
| 18 | ) |
| 19 | |
| 20 | func TestMacOSLocaleKeepsSecondaryLanguages(t *testing.T) { |
| 21 | if runtime.GOOS != "darwin" { |
| 22 | t.Skip("uses native macOS defaults") |
| 23 | } |
| 24 | domain := filepath.Join(t.TempDir(), "regional-preferences") |
| 25 | run := func(name string, args ...string) string { |
| 26 | t.Helper() |
| 27 | out, err := exec.Command(name, args...).CombinedOutput() |
| 28 | if err != nil { |
| 29 | t.Fatalf("preference operation %s: %v, %s", name, err, out) |
| 30 | } |
| 31 | return strings.TrimSpace(string(out)) |
| 32 | } |
| 33 | apply := func(locale string) { run("/bin/bash", "-c", macOSLocaleScript, "cage-locale", domain, locale) } |
| 34 | apply("ja_JP") |
| 35 | if got := run("/usr/bin/defaults", "read", domain, "AppleLocale"); got != "ja_JP" { |
| 36 | t.Fatalf("new preferences: %q", got) |
| 37 | } |
| 38 | run("/usr/bin/defaults", "write", domain, "AppleLanguages", "-array", "fr-FR", "en-US", "ja-JP") |
| 39 | apply("en_US") |
| 40 | apply("en_US") |
| 41 | if got := run("/usr/bin/plutil", "-extract", "AppleLanguages", "json", "-o", "-", domain+".plist"); got != `["en-US","fr-FR","ja-JP"]` { |
| 42 | t.Fatalf("updated preferences: %q", got) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestMacOSRegionWaitsForPreferenceAndRuntime(t *testing.T) { |
| 47 | var applied atomic.Bool |
| 48 | var samples atomic.Int32 |
| 49 | g := newMacWorkspaceGuest(t, func(ch ssh.Channel, command string) (uint32, bool) { |
| 50 | if strings.Contains(command, "cage-locale") && strings.Contains(command, "NSGlobalDomain") && strings.Contains(command, "en_US") { |
| 51 | applied.Store(true) |
| 52 | return 0, true |
| 53 | } |
| 54 | if strings.Contains(command, "result.system_timezone") && applied.Load() { |
| 55 | zone, language := "America/Los_Angeles", "en-US" |
| 56 | switch samples.Add(1) { |
| 57 | case 1: |
| 58 | zone = "GMT" |
| 59 | case 2: |
| 60 | language = "fr-FR" |
| 61 | } |
| 62 | fmt.Fprintf(ch, `{"tz":%q,"system_timezone":"America/Los_Angeles","system_locale":"en_US","system_language":%q,"process_locale":"en-US"}`, zone, language) |
| 63 | return 0, true |
| 64 | } |
| 65 | return 1, true |
| 66 | }) |
| 67 | cfg := cage.DefaultConfig() |
| 68 | cfg.Timezone = "America/Los_Angeles" |
| 69 | ctx, cancel := context.WithTimeout(t.Context(), 8*time.Second) |
| 70 | defer cancel() |
| 71 | if err := g.s.syncMacOSRegion(ctx, cfg); err != nil || samples.Load() != 3 { |
| 72 | t.Fatalf("startup observations=%d, err=%v", samples.Load(), err) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestMacOSStartAndControllerReconnectRequireFreshNetworkCheck(t *testing.T) { |
| 77 | if runtime.GOOS != "darwin" { |
| 78 | t.Skip("native Mac startup uses the host shell and OpenSSH") |
| 79 | } |
| 80 | for _, tc := range []struct { |
| 81 | name string |
| 82 | start, paused, setup bool |
| 83 | }{ |
| 84 | {"cold start", true, false, false}, |
| 85 | {"cold start retains pause", true, true, false}, |
| 86 | {"first setup starts offline", true, false, true}, |
| 87 | {"controller adopts running guest", false, false, false}, |
| 88 | {"controller adopts paused guest", false, true, false}, |
| 89 | {"controller adopts unfinished setup", false, false, true}, |
| 90 | } { |
| 91 | t.Run(tc.name, func(t *testing.T) { |
| 92 | g := newMacWorkspaceGuest(t, func(_ ssh.Channel, command string) (uint32, bool) { |
| 93 | return 0, strings.Contains(command, "uname -s") |
| 94 | }) |
| 95 | s := g.s |
| 96 | s.ready = false |
| 97 | if tc.setup { |
| 98 | s.macosGuest.SetupID = "awaiting-first-setup" |
| 99 | } |
| 100 | if err := writePrivate(filepath.Join(s.profile, "network-intent.json"), networkIntent{Version: 1, Paused: tc.paused, PauseVersion: 7}); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | if err := s.restoreNetworkIntent(); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | cfg, err := loadConfig(s.configPath) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | gate, err := cage.NewEgressGate(cfg) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | stop, err := cage.StartGateControl(s.configPath, s.instanceDir(), gate) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | defer stop() |
| 119 | if err = gate.BeginCheck(); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | grantTestGate(t, gate) |
| 123 | |
| 124 | |
| 125 | store, err := managedproxy.Load(s.profile) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | store.Selected = "" |
| 130 | if err = store.Save(s.profile); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | checkFailure := cfg.RequireManagedProxy(s.profile) |
| 134 | if checkFailure == nil { |
| 135 | t.Fatal("fixture still has a selected exit") |
| 136 | } |
| 137 | wantCheck := !tc.paused && !tc.setup |
| 138 | if tc.start { |
| 139 | marker := filepath.Join(s.instanceDir(), "cage-running.json") |
| 140 | if err := os.Remove(marker); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | s.exe = filepath.Join(t.TempDir(), "cage") |
| 144 | script := fmt.Sprintf("#!/bin/sh\n[ \"$*\" = 'guest-os macos start' ] || exit 1\nprintf '{\"pid\":%d}' > '%s'\n", os.Getpid(), marker) |
| 145 | if err := os.WriteFile(s.exe, []byte(script), 0700); err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | s.setJobForTest(Job{ID: "starting", Kind: "start", Running: true}) |
| 149 | err := s.startMacOSGuest() |
| 150 | if wantCheck && (err == nil || err.Error() != checkFailure.Error()) || !wantCheck && err != nil { |
| 151 | t.Fatalf("start result = %v, want fresh check = %t", err, wantCheck) |
| 152 | } |
| 153 | } else { |
| 154 | s.op.Lock() |
| 155 | err := s.attachMacOSGuest() |
| 156 | job := s.jobs.snapshot() |
| 157 | s.op.Unlock() |
| 158 | defer s.jobs.close() |
| 159 | if err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | if (job.Running && job.Kind == "network-recovery") != wantCheck { |
| 163 | t.Fatalf("controller reconnect job = %+v, want fresh check = %t", job, wantCheck) |
| 164 | } |
| 165 | if wantCheck { |
| 166 | if result := awaitJobIdle(t, &s.jobs); result.Error != checkFailure.Error() { |
| 167 | t.Fatalf("reconnect completed without the expected proxy check: %+v", result) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if state := gate.Status(); !s.ready || state.Mode != cage.GateClosed || !time.Now().After(state.Until) || s.networkPaused != tc.paused || s.safety.pauseVersion != 7 { |
| 172 | t.Fatalf("ready=%t mode=%s paused=%t generation=%d", s.ready, state.Mode, s.networkPaused, s.safety.pauseVersion) |
| 173 | } |
| 174 | }) |
| 175 | } |
| 176 | } |