oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package provider
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "golang.org/x/sys/unix"
13)
14
15const syncedAtFixture = "1795305600000"
16
17// writeRemoteSettings writes the files 2.1.312 leaves after an empty
18// managed-settings sync: the object, then the time of the sync.
19func writeRemoteSettings(t *testing.T, home, settings string, syncedAt bool) {
20 t.Helper()
21 writeFake(t, filepath.Join(home, "remote-settings.json"), settings)
22 if syncedAt {
23 writeFake(t, filepath.Join(home, "remote-settings.json.synced-at"), syncedAtFixture)
24 }
25}
26
27func TestClaudeEmptyRemoteSettingsAccepted(t *testing.T) {
28 for _, tc := range []struct {
29 name, settings string
30 syncedAt bool
31 }{
32 {"object", "{}", false}, {"object+synced-at", "{}", true}, {"newline", "{}\n", true},
33 {"surrounding", " \t\r\n{}\r\n \t", true}, {"at-cap", "{}" + strings.Repeat(" ", 62), true},
34 } {
35 t.Run(tc.name, func(t *testing.T) {
36 l := claudeRuntimeFixture(t)
37 writeRemoteSettings(t, l.runtime, tc.settings, tc.syncedAt)
38 if err := l.validateRuntimeEntries(); err != nil {
39 t.Fatal(err)
40 }
41 })
42 }
43}
44
45func TestClaudePolicyStampCap(t *testing.T) {
46 for _, size := range []int{225, 4096, 4097} {
47 l := claudeRuntimeFixture(t)
48 writeFake(t, filepath.Join(l.runtime, "policy-limits.json.stamp.json"), strings.Repeat("x", size))
49 err := l.validateRuntimeEntries()
50 if size <= 4096 && err != nil {
51 t.Fatalf("%d bytes: %v", size, err)
52 }
53 if size > 4096 && ContainmentReason(err) != "runtime_claude_policy_limits" {
54 t.Fatalf("%d bytes: %v", size, err)
55 }
56 }
57}
58
59// Anything but the empty object, as anything but a private regular file,
60// or from another CLI version stays refused, and the refusal names it.
61func TestClaudeRemoteSettingsOtherwiseRefused(t *testing.T) {
62 const settings, synced = "remote-settings.json", "remote-settings.json.synced-at"
63 for _, tc := range []struct {
64 name, target, kind, content string
65 syncedAt bool
66 want string
67 }{
68 {"permissions", settings, "file", `{"permissions":{}}`, true, "remote_settings"},
69 {"array", settings, "file", "[]", false, "remote_settings"},
70 {"null", settings, "file", "null", false, "remote_settings"},
71 {"empty-file", settings, "file", "", false, "remote_settings"},
72 {"inner-space", settings, "file", "{ }", false, "remote_settings"},
73 {"nul", settings, "file", "{}\x00", false, "remote_settings"},
74 {"oversize", settings, "file", "{}" + strings.Repeat(" ", 63), false, "remote_settings"},
75 {"directory", settings, "directory", "", false, "remote_settings"},
76 {"symlink", settings, "symlink", "", false, "remote_settings"},
77 {"hardlink", settings, "hardlink", "", false, "remote_settings"},
78 {"fifo", settings, "fifo", "", false, "remote_settings"},
79 {"mode", settings, "mode", "", false, "remote_settings"},
80 {"setuid", settings, "setuid", "", false, "remote_settings"},
81 {"version", settings, "version", "", true, "remote_settings"},
82 {"synced-alone", synced, "alone", "", false, "remote_settings_synced_at"},
83 {"synced-oversize", synced, "file", strings.Repeat("1", 33), false, "remote_settings_synced_at"},
84 {"synced-directory", synced, "directory", "", false, "remote_settings_synced_at"},
85 {"synced-symlink", synced, "symlink", "", false, "remote_settings_synced_at"},
86 {"synced-hardlink", synced, "hardlink", "", false, "remote_settings_synced_at"},
87 {"synced-fifo", synced, "fifo", "", false, "remote_settings_synced_at"},
88 {"synced-mode", synced, "mode", "", false, "remote_settings_synced_at"},
89 {"synced-setgid", synced, "setgid", "", false, "remote_settings_synced_at"},
90 {"synced-nested", "projects/" + synced, "file", syncedAtFixture, false, "remote_settings_synced_at"},
91 } {
92 t.Run(tc.name, func(t *testing.T) {
93 l := claudeRuntimeFixture(t)
94 must := func(err error) {
95 t.Helper()
96 if err != nil {
97 t.Fatal(err)
98 }
99 }
100 if tc.target == synced || strings.HasSuffix(tc.target, "/"+synced) {
101 if tc.kind != "alone" {
102 // A nested copy is refused even beside an accepted root pair.
103 writeRemoteSettings(t, l.runtime, "{}", tc.target != synced)
104 }
105 } else if tc.syncedAt {
106 writeFake(t, filepath.Join(l.runtime, synced), syncedAtFixture)
107 }
108 path := filepath.Join(l.runtime, tc.target)
109 must(os.MkdirAll(filepath.Dir(path), 0700))
110 valid := "{}"
111 if tc.target != settings {
112 valid = syncedAtFixture
113 }
114 switch tc.kind {
115 case "file":
116 writeFake(t, path, tc.content)
117 case "directory":
118 must(os.Mkdir(path, 0700))
119 case "symlink":
120 target := filepath.Join(t.TempDir(), "state")
121 writeFake(t, target, valid)
122 must(os.Symlink(target, path))
123 case "hardlink":
124 target := filepath.Join(t.TempDir(), "state")
125 writeFake(t, target, valid)
126 must(os.Link(target, path))
127 case "fifo":
128 must(unix.Mkfifo(path, 0600))
129 case "mode":
130 writeFake(t, path, valid)
131 must(os.Chmod(path, 0644))
132 case "setuid":
133 writeFake(t, path, valid)
134 must(os.Chmod(path, 0600|os.ModeSetuid))
135 case "setgid":
136 writeFake(t, path, valid)
137 must(os.Chmod(path, 0600|os.ModeSetgid))
138 case "version":
139 writeFake(t, path, valid)
140 l.source.Expected.ProviderVersion = "other"
141 case "alone":
142 writeFake(t, path, valid)
143 }
144 err := l.validateRuntimeEntries()
145 if !errors.Is(err, ErrContainment) || ContainmentReason(err) != "runtime_claude_"+tc.want {
146 t.Fatalf("want runtime_claude_%s: %v", tc.want, err)
147 }
148 })
149 }
150}
151
152// writeManagedTurn writes what 2.1.312 left in a runtime home during a
153// first turn on an org-managed account (production 2026-11-22, top-level
154// names and sizes captured), less the token-only scrub stubs.
155func writeManagedTurn(t *testing.T, home string) {
156 t.Helper()
157 for _, dir := range []string{"projects", "backups", "dev-mods", "seed-admin", "sessions", ".claude", ".config", ".npm", ".cache"} {
158 if err := os.MkdirAll(filepath.Join(home, dir), 0700); err != nil {
159 t.Fatal(err)
160 }
161 }
162 for name, size := range map[string]int{"policy-limits.json": 409, "policy-limits.json.signature.json": 3006, "policy-limits.json.stamp.json": 225} {
163 writeFake(t, filepath.Join(home, name), strings.Repeat("x", size))
164 }
165 writeRemoteSettings(t, home, "{}", true)
166}
167
168// Production 2026-11-22: a warm coordinator on an org-managed account
169// was refused before its second turn, runtime_claude_unexpected_entry:
170// unlisted. This is the top-level set captured from that runtime home.
171func TestTokenCopySecondTurnManagedAccount(t *testing.T) {
172 l := openTokenCopy(t, tokenFixture(t))
173 ctx := context.Background()
174 if err := l.CheckCopy(ctx); err != nil {
175 t.Fatalf("first turn: %v", err)
176 }
177 home := l.Binding().Home
178 scrubStubs(t, home, 0600, 0700)
179 writeManagedTurn(t, home)
180 if err := l.CheckCopy(ctx); err != nil {
181 t.Fatalf("second turn: %v", err)
182 }
183}
184
185// The same second turn on a subscription copy: the CLI's own credential
186// file is staged and no scrub stubs are written (those come only with a
187// token launch), and a stub there stays refused.
188func TestSubscriptionCopySecondTurnManagedAccount(t *testing.T) {
189 b, _ := accountFixture(t)
190 b.Provider = "claude"
191 b.Expected.ProviderVersion = ClaudeVersion
192 writeMetadata(t, b.Home, b.Expected)
193 writeFake(t, filepath.Join(b.Home, credentialName("claude")), `{}`)
194 l, err := OpenRuntimeCopy(context.Background(), b, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }, time.Second)
195 if err != nil {
196 t.Fatal(err)
197 }
198 t.Cleanup(func() { l.Close() })
199 ctx := context.Background()
200 if err = l.CheckCopy(ctx); err != nil {
201 t.Fatalf("first turn: %v", err)
202 }
203 home := l.Binding().Home
204 if _, err = os.Lstat(filepath.Join(home, credentialName("claude"))); err != nil {
205 t.Fatalf("subscription copy has no credential file: %v", err)
206 }
207 writeManagedTurn(t, home)
208 if err = l.CheckCopy(ctx); err != nil {
209 t.Fatalf("second turn: %v", err)
210 }
211 writeFake(t, filepath.Join(home, ".bashrc"), "")
212 if err = l.CheckCopy(ctx); ContainmentReason(err) != "runtime_claude_unexpected_entry:.bashrc" {
213 t.Fatalf("scrub stub on a subscription copy: %v", err)
214 }
215}
216
217// The account's own home never feeds these into the staged one.
218func TestClaudeRemoteSettingsNotStaged(t *testing.T) {
219 l := claudeRuntimeFixture(t)
220 names := []string{"policy-limits.json.stamp.json", "remote-settings.json", "remote-settings.json.synced-at"}
221 for _, name := range names {
222 writeFake(t, filepath.Join(l.source.Home, name), "{}")
223 }
224 preflightFixture(t, l)
225 for _, name := range names {
226 if _, err := os.Lstat(filepath.Join(l.runtime, name)); !os.IsNotExist(err) {
227 t.Fatalf("source state staged: %s: %v", name, err)
228 }
229 }
230}
231