跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

rooted_test.go

691 行 · 21.1 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "fmt"
7 "io"
8 "os"
9 "path/filepath"
10 "runtime"
11 "sync"
12 "testing"
13 "time"
14
15 "github.com/lima-vm/sshocker/pkg/util"
16 "github.com/pkg/sftp"
17)
18
19var (
20 zwnj = string(rune(0x200c)) // ignored by HFS+
21 zwj = string(rune(0x200d))
22 bom = string(rune(0xfeff))
23)
24
25// setupRooted serves <tmp>/root with ".git" read-only, and returns a client
26// that plays the role of a compromised guest sending arbitrary requests.
27// It also creates the symlinks "gitlink" -> ".git" and "configlink" -> ".git/config".
28func setupRooted(t *testing.T, readonly bool) (*sftp.Client, string) {
29 t.Helper()
30 c, root, _ := setupRootedHandlers(t, readonly)
31 return c, root
32}
33
34func setupRootedHandlers(t *testing.T, readonly bool) (*sftp.Client, string, *rootedHandlers) {
35 t.Helper()
36 tmp := t.TempDir()
37 root := filepath.Join(tmp, "root")
38 for _, d := range []string{filepath.Join(root, ".git", "hooks"), filepath.Join(root, "src"), filepath.Join(tmp, "outside")} {
39 if err := os.MkdirAll(d, 0o755); err != nil {
40 t.Fatal(err)
41 }
42 }
43 for _, f := range []string{filepath.Join(root, ".git", "config"), filepath.Join(root, "src", "main.go"), filepath.Join(tmp, "outside", "secret")} {
44 if err := os.WriteFile(f, []byte("orig"), 0o644); err != nil {
45 t.Fatal(err)
46 }
47 }
48 if err := os.Symlink(".git", filepath.Join(root, "gitlink")); err != nil {
49 t.Fatal(err)
50 }
51 if err := os.Symlink(filepath.Join(".git", "config"), filepath.Join(root, "configlink")); err != nil {
52 t.Fatal(err)
53 }
54 c2sR, c2sW := io.Pipe()
55 s2cR, s2cW := io.Pipe()
56 srv, h, err := newRootedServer(&util.RWC{ReadCloser: c2sR, WriteCloser: s2cW}, root, readonly, []string{".git"})
57 if err != nil {
58 t.Fatal(err)
59 }
60 done := make(chan struct{})
61 go func() {
62 defer close(done)
63 _ = srv.Serve()
64 _ = h.Close()
65 }()
66 client, err := sftp.NewClientPipe(s2cR, c2sW)
67 if err != nil {
68 t.Fatal(err)
69 }
70 t.Cleanup(func() {
71 srv.Close()
72 client.Close()
73 <-done
74 })
75 return client, root, h
76}
77
78func assertUnchanged(t *testing.T, root string) {
79 t.Helper()
80 fi, err := os.Lstat(filepath.Join(root, ".git"))
81 if err != nil || !fi.IsDir() {
82 t.Fatalf(".git is no longer a directory: %v, %v", fi, err)
83 }
84 b, err := os.ReadFile(filepath.Join(root, ".git", "config"))
85 if err != nil || string(b) != "orig" {
86 t.Fatalf(".git/config changed: %q, %v", b, err)
87 }
88 wantPerm := os.FileMode(0o644)
89 if runtime.GOOS == "windows" {
90 wantPerm = 0o666
91 }
92 fi, err = os.Stat(filepath.Join(root, ".git", "config"))
93 if err != nil || fi.Mode().Perm() != wantPerm {
94 t.Fatalf(".git/config mode changed: %v, %v", fi, err)
95 }
96 b, err = os.ReadFile(filepath.Join(filepath.Dir(root), "outside", "secret"))
97 if err != nil || string(b) != "orig" {
98 t.Fatalf("outside/secret changed: %q, %v", b, err)
99 }
100 for dir, n := range map[string]int{".git": 2, ".git/hooks": 0, "../outside": 1} {
101 entries, err := os.ReadDir(filepath.Join(root, dir))
102 if err != nil || len(entries) != n {
103 t.Fatalf("%s entries changed: %v, %v", dir, entries, err)
104 }
105 }
106}
107
108func TestRootedAllowsNormalOperations(t *testing.T) {
109 c, root := setupRooted(t, false)
110 p := func(s string) string { return filepath.Join(root, s) }
111
112 f, err := c.Create(p("src/new.go"))
113 if err != nil {
114 t.Fatal(err)
115 }
116 if _, err := f.Write([]byte("hello")); err != nil {
117 t.Fatal(err)
118 }
119 if err := f.Chmod(0o600); err != nil {
120 t.Fatal(err)
121 }
122 f.Close()
123 if err := c.Truncate(p("src/new.go"), 2); err != nil {
124 t.Fatal(err)
125 }
126 mtime := time.Unix(1_000_000_000, 0)
127 if err := c.Chtimes(p("src/new.go"), mtime, mtime); err != nil {
128 t.Fatal(err)
129 }
130 if err := c.Mkdir(p("dir")); err != nil {
131 t.Fatal(err)
132 }
133 if err := c.PosixRename(p("src/new.go"), p("dir/renamed.go")); err != nil {
134 t.Fatal(err)
135 }
136 if err := c.Rename(p("dir/renamed.go"), p("src/main.go")); err == nil {
137 t.Fatal("Rename must not replace an existing file")
138 }
139 names := []string{"dir/hardlink", "dir/renamed.go"}
140 statName, wantPerm := "dir/renamed.go", os.FileMode(0o666) // Windows keeps only the owner write bit
141 if runtime.GOOS != "windows" {
142 if err := c.Symlink("renamed.go", p("dir/link")); err != nil {
143 t.Fatal(err)
144 }
145 if target, err := c.ReadLink(p("dir/link")); err != nil || target != "renamed.go" {
146 t.Fatalf("ReadLink: %q, %v", target, err)
147 }
148 names = append(names, "dir/link")
149 statName, wantPerm = "dir/link", 0o600
150 }
151 fi, err := c.Stat(p(statName))
152 if err != nil {
153 t.Fatal(err)
154 }
155 if fi.Size() != 2 || fi.Mode().Perm() != wantPerm || !fi.ModTime().Equal(mtime) {
156 t.Fatalf("unexpected stat: size=%d mode=%v mtime=%v", fi.Size(), fi.Mode(), fi.ModTime())
157 }
158 if err := c.Link(p("dir/renamed.go"), p("dir/hardlink")); err != nil {
159 t.Fatal(err)
160 }
161 entries, err := c.ReadDir(p("dir"))
162 if err != nil || len(entries) != len(names) {
163 t.Fatalf("ReadDir: %v, %v", entries, err)
164 }
165 if _, err := c.StatVFS(root); err != nil {
166 t.Fatal(err)
167 }
168 for _, s := range names {
169 if err := c.Remove(p(s)); err != nil {
170 t.Fatal(err)
171 }
172 }
173 if err := c.RemoveDirectory(p("dir")); err != nil {
174 t.Fatal(err)
175 }
176 // Reading the protected directory, also through an in-root symlink, is allowed.
177 rf, err := c.Open(p("configlink"))
178 if err != nil {
179 t.Fatal(err)
180 }
181 b, err := io.ReadAll(rf)
182 rf.Close()
183 if err != nil || string(b) != "orig" {
184 t.Fatalf("read .git/config: %q, %v", b, err)
185 }
186 assertUnchanged(t, root)
187}
188
189func TestRootedChmodWithoutReadPermission(t *testing.T) {
190 if runtime.GOOS == "windows" {
191 t.Skip("mode bits other than the owner write bit are not supported on Windows")
192 }
193 if os.Geteuid() == 0 {
194 t.Skip("root bypasses permission checks")
195 }
196 c, root := setupRooted(t, false)
197 for _, s := range []string{"src/main.go", "src"} {
198 p := filepath.Join(root, s)
199 if err := os.Chmod(p, 0); err != nil {
200 t.Fatal(err)
201 }
202 t.Cleanup(func() { _ = os.Chmod(p, 0o755) })
203 if err := c.Chmod(p, 0o755); err != nil {
204 t.Fatalf("%s: %v", s, err)
205 }
206 if fi, err := os.Stat(p); err != nil || fi.Mode().Perm() != 0o755 {
207 t.Fatalf("%s: %v, %v", s, fi, err)
208 }
209 }
210}
211
212// SSH_FXF_APPEND has the bit of SSH_FILEXFER_ATTR_PERMISSIONS: an append open used to
213// panic the server, and with it the process serving the mount.
214func TestRootedOpenAppend(t *testing.T) {
215 c, root := setupRooted(t, false)
216 for _, s := range []string{"src/main.go", "src/new.go"} {
217 f, err := c.OpenFile(filepath.Join(root, s), os.O_WRONLY|os.O_APPEND|os.O_CREATE)
218 if err != nil {
219 t.Fatalf("%s: %v", s, err)
220 }
221 // Offset 0: with SSH_FXF_APPEND, writes go to the end whatever the client sends.
222 if _, err := f.Write([]byte("+")); err != nil {
223 t.Fatalf("%s: %v", s, err)
224 }
225 f.Close()
226 }
227 for s, want := range map[string]string{"src/main.go": "orig+", "src/new.go": "+"} {
228 if b, err := os.ReadFile(filepath.Join(root, s)); err != nil || string(b) != want {
229 t.Fatalf("%s: %q, %v", s, b, err)
230 }
231 }
232}
233
234// The host grows the file after the client learned its size: an append must not overwrite that.
235func TestRootedAppendAfterHostWrite(t *testing.T) {
236 c, root := setupRooted(t, false)
237 p := filepath.Join(root, "src", "main.go")
238 f, err := c.OpenFile(p, os.O_WRONLY|os.O_APPEND)
239 if err != nil {
240 t.Fatal(err)
241 }
242 defer f.Close()
243 if _, err := f.WriteAt([]byte("-guest1"), 4); err != nil {
244 t.Fatal(err)
245 }
246 hf, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, 0)
247 if err != nil {
248 t.Fatal(err)
249 }
250 if _, err := hf.WriteString("-host"); err != nil {
251 t.Fatal(err)
252 }
253 hf.Close()
254 if _, err := f.WriteAt([]byte("-guest2"), 11); err != nil {
255 t.Fatal(err)
256 }
257 if b, err := os.ReadFile(p); err != nil || string(b) != "orig-guest1-host-guest2" {
258 t.Fatalf("%q, %v", b, err)
259 }
260}
261
262// Attributes shorter than their flags announce: an error, not a panic of the process serving the mount.
263func TestRootedSetstatShortAttributes(t *testing.T) {
264 _, root, h := setupRootedHandlers(t, false)
265 for _, rel := range []string{"src/main.go", ".git/config"} {
266 for _, flags := range []uint32{0x1, 0x2, 0x4, 0x8} {
267 r := sftp.NewRequest("Setstat", filepath.Join(root, rel))
268 r.Flags = flags
269 if err := h.Filecmd(r); err == nil {
270 t.Fatalf("%s, flags %#x: no error", rel, flags)
271 }
272 }
273 }
274 r := sftp.NewRequest("Mkdir", filepath.Join(root, "src", "dir"))
275 r.Flags = 0x4
276 if err := h.Filecmd(r); err != nil {
277 t.Fatal(err)
278 }
279 assertUnchanged(t, root)
280}
281
282// FSTAT and FSETSTAT apply to the open file, not to whatever is now at the path it was opened with.
283func TestRootedFstatFsetstatAfterRename(t *testing.T) {
284 c, root := setupRooted(t, false)
285 p := func(s string) string { return filepath.Join(root, s) }
286 f, err := c.Create(p("src/a"))
287 if err != nil {
288 t.Fatal(err)
289 }
290 defer f.Close()
291 if _, err := f.Write([]byte("hello")); err != nil {
292 t.Fatal(err)
293 }
294 if err := c.PosixRename(p("src/a"), p("src/b")); err != nil {
295 t.Fatal(err)
296 }
297 if err := os.WriteFile(p("src/a"), []byte("other file"), 0o644); err != nil {
298 t.Fatal(err)
299 }
300 if fi, err := f.Stat(); err != nil || fi.Size() != 5 {
301 t.Fatalf("Stat: %v, %v", fi, err)
302 }
303 if err := f.Truncate(2); err != nil {
304 t.Fatal(err)
305 }
306 if err := f.Chmod(0o600); err != nil {
307 t.Fatal(err)
308 }
309 if b, err := os.ReadFile(p("src/b")); err != nil || string(b) != "he" {
310 t.Fatalf("src/b: %q, %v", b, err)
311 }
312 if b, err := os.ReadFile(p("src/a")); err != nil || string(b) != "other file" {
313 t.Fatalf("src/a: %q, %v", b, err)
314 }
315 if runtime.GOOS != "windows" {
316 if fi, err := os.Stat(p("src/b")); err != nil || fi.Mode().Perm() != 0o600 {
317 t.Fatalf("src/b: %v, %v", fi, err)
318 }
319 if fi, err := os.Stat(p("src/a")); err != nil || fi.Mode().Perm() != 0o644 {
320 t.Fatalf("src/a: %v, %v", fi, err)
321 }
322 }
323}
324
325// A handle opened for reading may point into a read-only name through a symlink:
326// FSETSTAT on it must keep going through the path checks.
327func TestRootedFsetstatReadHandle(t *testing.T) {
328 c, root := setupRooted(t, false)
329 f, err := c.Open(filepath.Join(root, "configlink"))
330 if err != nil {
331 t.Fatal(err)
332 }
333 defer f.Close()
334 if err := f.Truncate(0); err == nil {
335 t.Fatal("Truncate succeeded")
336 }
337 if err := f.Chmod(0o777); err == nil {
338 t.Fatal("Chmod succeeded")
339 }
340 assertUnchanged(t, root)
341}
342
343// FSETSTAT on a read handle applies only while the handle's file is still at its path.
344func TestRootedReadHandleSetstatAfterRename(t *testing.T) {
345 c, root := setupRooted(t, false)
346 p := func(s string) string { return filepath.Join(root, s) }
347 f, err := c.Open(p("src/main.go"))
348 if err != nil {
349 t.Fatal(err)
350 }
351 defer f.Close()
352 if err := c.PosixRename(p("src/main.go"), p("src/moved.go")); err != nil {
353 t.Fatal(err)
354 }
355 if err := os.WriteFile(p("src/main.go"), []byte("other file"), 0o644); err != nil {
356 t.Fatal(err)
357 }
358 if err := f.Chmod(0o400); err == nil {
359 t.Fatal("Chmod through the handle changed the file now at its path")
360 }
361 if fi, err := os.Stat(p("src/main.go")); err != nil || fi.Mode().Perm()&0o200 == 0 {
362 t.Fatalf("src/main.go: %v, %v", fi, err)
363 }
364}
365
366// Directories are listed as the client asks, and the handle's descriptor is released on close.
367func TestRootedListLarge(t *testing.T) {
368 c, root := setupRooted(t, false)
369 dir := filepath.Join(root, "src", "many")
370 if err := os.Mkdir(dir, 0o755); err != nil {
371 t.Fatal(err)
372 }
373 const n = 1234
374 for i := 0; i < n; i++ {
375 if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("f%04d", i)), nil, 0o644); err != nil {
376 t.Fatal(err)
377 }
378 }
379 before := openFDs()
380 for i := 0; i < 3; i++ {
381 fis, err := c.ReadDir(dir)
382 if err != nil || len(fis) != n {
383 t.Fatalf("ReadDir: %d entries, %v", len(fis), err)
384 }
385 seen := make(map[string]bool, n)
386 for _, fi := range fis {
387 seen[fi.Name()] = true
388 }
389 if len(seen) != n {
390 t.Fatalf("%d distinct entries", len(seen))
391 }
392 }
393 if after := openFDs(); after > before {
394 t.Fatalf("descriptors: %d before, %d after", before, after)
395 }
396}
397
398// openFDs counts the descriptors of this process, or returns 0 where it cannot.
399func openFDs() int {
400 fis, err := os.ReadDir("/proc/self/fd")
401 if err != nil {
402 return 0
403 }
404 return len(fis)
405}
406
407func TestRootedMaxHandles(t *testing.T) {
408 c, root := setupRooted(t, false)
409 p := filepath.Join(root, "src", "main.go")
410 files := make([]*sftp.File, 0, maxHandles)
411 defer func() {
412 for _, f := range files {
413 f.Close()
414 }
415 }()
416 for i := 0; i < maxHandles; i++ {
417 f, err := c.Open(p)
418 if err != nil {
419 t.Fatalf("handle %d: %v", i, err)
420 }
421 files = append(files, f)
422 }
423 if f, err := c.Open(p); err == nil {
424 f.Close()
425 t.Fatal("one handle over the limit")
426 }
427 files[0].Close()
428 files = files[1:]
429 f, err := c.Open(p)
430 if err != nil {
431 t.Fatal(err)
432 }
433 files = append(files, f)
434}
435
436func TestReverseSSHFSCloseNotStarted(t *testing.T) {
437 if err := (&ReverseSSHFS{}).Close(); err != nil {
438 t.Fatal(err)
439 }
440}
441
442// sshfs sends its open flags with the permissions alone as attributes.
443func TestRootedOpenMode(t *testing.T) {
444 if runtime.GOOS == "windows" {
445 t.Skip("mode bits other than the owner write bit are not supported on Windows")
446 }
447 _, root, h := setupRootedHandlers(t, false)
448 const write, appendFlag, creat = 0x2, 0x4, 0x8
449 r := sftp.NewRequest("Put", filepath.Join(root, "src", "run.sh"))
450 r.Flags = write | appendFlag | creat
451 r.Attrs = []byte{0, 0, 0o1, 0o355} // 0o755
452 f, err := h.openFile(r)
453 if err != nil {
454 t.Fatal(err)
455 }
456 f.Close()
457 if fi, err := os.Stat(filepath.Join(root, "src", "run.sh")); err != nil || fi.Mode().Perm() != 0o755 {
458 t.Fatalf("%v, %v", fi, err)
459 }
460}
461
462func TestRootedDeniesProtectedWrites(t *testing.T) {
463 type env struct {
464 c *sftp.Client
465 root string
466 }
467 p := func(e env, s string) string { return filepath.Join(e.root, s) }
468 open := func(e env, s string, flags int) error {
469 f, err := e.c.OpenFile(p(e, s), flags)
470 if err == nil {
471 f.Close()
472 }
473 return err
474 }
475 create := func(e env, s string) error { return open(e, s, os.O_RDWR|os.O_CREATE|os.O_TRUNC) }
476 withOutsideLink := func(f func(e env) error) func(e env) error {
477 return func(e env) error {
478 if err := os.Symlink(filepath.Join(e.root, "..", "outside"), p(e, "out")); err != nil {
479 panic(err)
480 }
481 return f(e)
482 }
483 }
484 attempts := []struct {
485 name string
486 f func(e env) error
487 }{
488 {"create hook", func(e env) error { return create(e, ".git/hooks/pre-commit") }},
489 {"open config for write", func(e env) error { return open(e, ".git/config", os.O_WRONLY) }},
490 // On a case-insensitive file system, these would name the existing .git.
491 {"upper case", func(e env) error { return e.c.Mkdir(p(e, "src/.GIT")) }},
492 {"hfs ignorable", func(e env) error { return e.c.Mkdir(p(e, "src/.g"+zwnj+"it")) }},
493 {"nested repo", func(e env) error { return e.c.Mkdir(p(e, "src/.git")) }},
494 {"nested gitfile", func(e env) error { return create(e, "src/.git") }},
495 {"via dir symlink", func(e env) error { return create(e, "gitlink/hooks/pre-commit") }},
496 {"via file symlink", func(e env) error { return open(e, "configlink", os.O_WRONLY|os.O_TRUNC) }},
497 {"hardlink from config", func(e env) error { return e.c.Link(p(e, ".git/config"), p(e, "src/config")) }},
498 {"hardlink into hooks", func(e env) error { return e.c.Link(p(e, "src/main.go"), p(e, ".git/hooks/x")) }},
499 {"symlink in hooks", func(e env) error { return e.c.Symlink("/bin/sh", p(e, ".git/hooks/x")) }},
500 {"rename .git away", func(e env) error { return e.c.PosixRename(p(e, ".git"), p(e, "old")) }},
501 {"rename into .git", func(e env) error { return e.c.PosixRename(p(e, "src/main.go"), p(e, ".git/hooks/x")) }},
502 {"rename to nested gitfile", func(e env) error { return e.c.PosixRename(p(e, "src/main.go"), p(e, "src/.git")) }},
503 {"remove config", func(e env) error { return e.c.Remove(p(e, ".git/config")) }},
504 {"rmdir hooks", func(e env) error { return e.c.RemoveDirectory(p(e, ".git/hooks")) }},
505 {"chmod config", func(e env) error { return e.c.Chmod(p(e, ".git/config"), 0o777) }},
506 {"chmod via symlink", func(e env) error { return e.c.Chmod(p(e, "configlink"), 0o777) }},
507 {"truncate config", func(e env) error { return e.c.Truncate(p(e, ".git/config"), 0) }},
508 {"truncate via symlink", func(e env) error { return e.c.Truncate(p(e, "configlink"), 0) }},
509 {"chtimes config", func(e env) error { return e.c.Chtimes(p(e, ".git/config"), time.Unix(0, 0), time.Unix(0, 0)) }},
510 {"write outside", func(e env) error { return create(e, "../outside/secret") }},
511 {"read outside", func(e env) error { return open(e, "../outside/secret", os.O_RDONLY) }},
512 {"write through outside symlink", withOutsideLink(func(e env) error { return create(e, "out/secret") })},
513 {"read through outside symlink", withOutsideLink(func(e env) error { return open(e, "out/secret", os.O_RDONLY) })},
514 }
515 for _, a := range attempts {
516 t.Run(a.name, func(t *testing.T) {
517 c, root := setupRooted(t, false)
518 if err := a.f(env{c, root}); err == nil {
519 t.Error("expected an error")
520 }
521 assertUnchanged(t, root)
522 })
523 }
524}
525
526// TestRootedNoopTimes checks the utimes request used by Lima's mountInotify.
527func TestRootedNoopTimes(t *testing.T) {
528 c, root := setupRooted(t, false)
529 config := filepath.Join(root, ".git", "config")
530 mtime := time.Unix(1_000_000_000, 0)
531 if err := os.Chtimes(config, mtime, mtime); err != nil {
532 t.Fatal(err)
533 }
534 before := ctime(t, config)
535 time.Sleep(10 * time.Millisecond)
536 if err := c.Chtimes(config, mtime, mtime); err != nil {
537 t.Fatalf("utimes to the current mtime: %v", err)
538 }
539 after, err := os.Lstat(config)
540 if err != nil {
541 t.Fatal(err)
542 }
543 if afterCtime := ctime(t, config); before != afterCtime || !after.ModTime().Equal(mtime) {
544 t.Fatalf("file was modified: ctime %v -> %v, mtime %v", before, afterCtime, after.ModTime())
545 }
546 for _, times := range [][2]time.Time{
547 {mtime, mtime.Add(time.Second)},
548 {mtime.Add(time.Second), mtime},
549 {mtime.Add(time.Second), mtime.Add(time.Second)},
550 } {
551 if err := c.Chtimes(config, times[0], times[1]); err == nil {
552 t.Errorf("utimes to %v: expected an error", times)
553 }
554 }
555 if err := c.Chtimes(filepath.Join(root, ".git", "missing"), mtime, mtime); err == nil {
556 t.Error("utimes on a missing file: expected an error")
557 }
558 assertUnchanged(t, root)
559}
560
561// TestRootedNoopRemoval checks the removal used to relay host deletions.
562func TestRootedNoopRemoval(t *testing.T) {
563 c, root, h := setupRootedHandlers(t, false)
564 p := func(s string) string { return filepath.Join(root, s) }
565
566 // The path was created again on the host before the guest removed it.
567 h.expectRemove(p("src/main.go"))
568 if err := c.Remove(p("src/main.go")); err != nil {
569 t.Fatal(err)
570 }
571 if _, err := os.Stat(p("src/main.go")); err != nil {
572 t.Fatalf("the file was removed: %v", err)
573 }
574 // The token is consumed.
575 if err := c.Remove(p("src/main.go")); err != nil {
576 t.Fatal(err)
577 }
578 if _, err := os.Stat(p("src/main.go")); err == nil {
579 t.Fatal("the file was not removed")
580 }
581
582 h.expectRemove(p("src"))
583 if err := c.RemoveDirectory(p("src")); err != nil {
584 t.Fatal(err)
585 }
586 if _, err := os.Stat(p("src")); err != nil {
587 t.Fatalf("the directory was removed: %v", err)
588 }
589
590 // A git lock file deleted on the host.
591 h.expectRemove(p(".git/index.lock"))
592 if err := c.Remove(p(".git/index.lock")); err != nil {
593 t.Fatal(err)
594 }
595 if err := c.Remove(p(".git/index.lock")); err == nil {
596 t.Fatal("expected an error")
597 }
598 h.expectRemove(p(".git/config"))
599 if err := c.Remove(p(".git/config")); err != nil {
600 t.Fatal(err)
601 }
602
603 // An expired token.
604 h.expectRemove(p(".git/config"))
605 h.now = func() time.Time { return time.Now().Add(noopRemovalTTL + time.Second) }
606 if err := c.Remove(p(".git/config")); err == nil {
607 t.Fatal("expected an error")
608 }
609 assertUnchanged(t, root)
610}
611
612func TestRootedReadonly(t *testing.T) {
613 c, root := setupRooted(t, true)
614 if _, err := c.Create(filepath.Join(root, "src", "new.go")); err == nil {
615 t.Error("expected an error")
616 }
617 if err := c.Mkdir(filepath.Join(root, "dir")); err == nil {
618 t.Error("expected an error")
619 }
620 if _, err := c.Stat(filepath.Join(root, "src", "main.go")); err != nil {
621 t.Error(err)
622 }
623}
624
625// TestRootedSymlinkSwapRace swaps a directory for a symlink to .git
626// while the client keeps writing into it.
627func TestRootedSymlinkSwapRace(t *testing.T) {
628 c, root := setupRooted(t, false)
629 d := filepath.Join(root, "d")
630 stop := make(chan struct{})
631 var wg sync.WaitGroup
632 wg.Add(1)
633 go func() {
634 defer wg.Done()
635 for {
636 select {
637 case <-stop:
638 return
639 default:
640 }
641 _ = os.RemoveAll(d)
642 _ = os.Mkdir(d, 0o755)
643 _ = os.RemoveAll(d)
644 _ = os.Symlink(".git", d)
645 }
646 }()
647 var created int
648 deadline := time.Now().Add(2 * time.Second)
649 for time.Now().Before(deadline) {
650 if f, err := c.Create(filepath.Join(d, "pwn")); err == nil {
651 f.Close()
652 created++
653 }
654 _ = c.Mkdir(filepath.Join(d, "pwndir"))
655 _ = c.Symlink("x", filepath.Join(d, "pwnlink"))
656 _ = c.Chmod(filepath.Join(d, "config"), 0o777)
657 _ = c.Truncate(filepath.Join(d, "config"), 0)
658 _ = c.Remove(filepath.Join(d, "config"))
659 }
660 close(stop)
661 wg.Wait()
662 if created == 0 {
663 t.Fatal("no write reached d while it was a directory, the race was not exercised")
664 }
665 assertUnchanged(t, root)
666}
667
668func TestSameName(t *testing.T) {
669 for _, s := range []string{".git", ".GIT", ".Git", ".g" + zwnj + "it", bom + ".git", ".git" + zwj} {
670 if !sameName(s, ".git") {
671 t.Errorf("%q should match .git", s)
672 }
673 }
674 for _, s := range []string{".gitignore", "git", ".git.", "x.git", ".gi"} {
675 if sameName(s, ".git") {
676 t.Errorf("%q should not match .git", s)
677 }
678 }
679 // Names APFS takes as one: either normalization, full case folding.
680 for _, c := range [][2]string{
681 {"café", "café"},
682 {"CAFÉ", "café"},
683 {"glaß", "glass"},
684 {"GLASS", "glaß"},
685 {"K", "k"}, // KELVIN SIGN
686 } {
687 if !sameName(c[0], c[1]) {
688 t.Errorf("%q should match %q", c[0], c[1])
689 }
690 }
691}