| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "golang.org/x/sys/unix" |
| 11 | ) |
| 12 | |
| 13 | func TestClaudePrivateWriteStaging(t *testing.T) { |
| 14 | for _, name := range []string{".cc-writes", ".claude/bridge-spawn"} { |
| 15 | t.Run(name, func(t *testing.T) { |
| 16 | for _, kind := range []string{"empty", "file", "child", "link", "mode", "version"} { |
| 17 | t.Run(kind, func(t *testing.T) { |
| 18 | l := &AccountLease{source: Binding{Provider: "claude", Expected: Identity{ProviderVersion: ClaudeVersion}}, runtime: t.TempDir()} |
| 19 | path := filepath.Join(l.runtime, name) |
| 20 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | switch kind { |
| 24 | case "file": |
| 25 | if err := os.WriteFile(path, nil, 0600); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | case "link": |
| 29 | if err := os.Symlink(t.TempDir(), path); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | default: |
| 33 | if err := os.Mkdir(path, 0700); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | } |
| 37 | if kind == "child" { |
| 38 | if err := os.WriteFile(filepath.Join(path, "settings.json.tmp.12345678"), nil, 0600); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | } |
| 42 | if kind == "mode" { |
| 43 | if err := os.Chmod(path, 0777); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | } |
| 47 | if kind == "version" { |
| 48 | l.source.Expected.ProviderVersion = "other" |
| 49 | } |
| 50 | err := l.validateRuntimeEntries() |
| 51 | if (kind == "empty") != (err == nil) { |
| 52 | t.Fatalf("%s: %v", kind, err) |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestClaudeGitIgnoreState(t *testing.T) { |
| 61 | for _, kind := range []string{"observed", "private", "same-size-content", "empty", "extra-pattern", "missing-newline", "mode", "directory-mode", "version", "child", "symlink", "hardlink", "directory", "fifo", "setuid", "setgid"} { |
| 62 | t.Run(kind, func(t *testing.T) { |
| 63 | l := &AccountLease{source: Binding{Provider: "claude", Expected: Identity{ProviderVersion: ClaudeVersion}}, runtime: t.TempDir()} |
| 64 | must := func(err error) { |
| 65 | t.Helper() |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | } |
| 70 | dir := filepath.Join(l.runtime, ".config/git") |
| 71 | must(os.MkdirAll(dir, 0775)) |
| 72 | must(os.Chmod(dir, 0775)) |
| 73 | path := filepath.Join(dir, "ignore") |
| 74 | content := "**/.claude/.cc-writes/\n" |
| 75 | switch kind { |
| 76 | case "same-size-content": |
| 77 | content = strings.Repeat("x", len(content)) |
| 78 | case "empty": |
| 79 | content = "" |
| 80 | case "extra-pattern": |
| 81 | content += "*\n" |
| 82 | case "missing-newline": |
| 83 | content = strings.TrimSuffix(content, "\n") |
| 84 | } |
| 85 | must(os.WriteFile(path, []byte(content), 0664)) |
| 86 | must(os.Chmod(path, 0664)) |
| 87 | switch kind { |
| 88 | case "private": |
| 89 | must(os.Chmod(dir, 0700)) |
| 90 | must(os.Chmod(path, 0600)) |
| 91 | case "mode": |
| 92 | must(os.Chmod(path, 0666)) |
| 93 | case "directory-mode": |
| 94 | must(os.Chmod(dir, 0777)) |
| 95 | case "version": |
| 96 | l.source.Expected.ProviderVersion = "other" |
| 97 | case "child": |
| 98 | must(os.WriteFile(filepath.Join(dir, "config"), nil, 0600)) |
| 99 | case "symlink": |
| 100 | must(os.Remove(path)) |
| 101 | must(os.Symlink("/workspace/ignore", path)) |
| 102 | case "hardlink": |
| 103 | must(os.Link(path, filepath.Join(t.TempDir(), "link"))) |
| 104 | case "directory": |
| 105 | must(os.Remove(path)) |
| 106 | must(os.Mkdir(path, 0664)) |
| 107 | case "fifo": |
| 108 | must(os.Remove(path)) |
| 109 | must(unix.Mkfifo(path, 0664)) |
| 110 | case "setuid": |
| 111 | must(os.Chmod(path, 0664|os.ModeSetuid)) |
| 112 | case "setgid": |
| 113 | must(os.Chmod(path, 0664|os.ModeSetgid)) |
| 114 | } |
| 115 | err := l.validateRuntimeEntries() |
| 116 | if (kind == "observed" || kind == "private") != (err == nil) { |
| 117 | t.Fatalf("%s: %v", kind, err) |
| 118 | } |
| 119 | if err != nil && !errors.Is(err, ErrContainment) { |
| 120 | t.Fatalf("wrong refusal: %v", err) |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestClaudeGitIgnoreReadback(t *testing.T) { |
| 127 | for _, change := range []string{"unchanged", "missing", "replaced", "mode", "size", "hardlink", "symlink", "parent-symlink", "unreadable"} { |
| 128 | t.Run(change, func(t *testing.T) { |
| 129 | must := func(err error) { |
| 130 | t.Helper() |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | } |
| 135 | l := &AccountLease{runtime: t.TempDir()} |
| 136 | dir := filepath.Join(l.runtime, ".config/git") |
| 137 | must(os.MkdirAll(dir, 0700)) |
| 138 | path := filepath.Join(dir, "ignore") |
| 139 | content := []byte("**/.claude/.cc-writes/\n") |
| 140 | must(os.WriteFile(path, content, 0600)) |
| 141 | info, err := os.Lstat(path) |
| 142 | must(err) |
| 143 | switch change { |
| 144 | case "missing": |
| 145 | must(os.Remove(path)) |
| 146 | case "replaced": |
| 147 | must(os.Rename(path, path+".old")) |
| 148 | must(os.WriteFile(path, content, 0600)) |
| 149 | case "mode": |
| 150 | must(os.Chmod(path, 0664)) |
| 151 | case "size": |
| 152 | must(os.WriteFile(path, append(content, '\n'), 0600)) |
| 153 | case "hardlink": |
| 154 | must(os.Link(path, filepath.Join(t.TempDir(), "link"))) |
| 155 | case "symlink": |
| 156 | must(os.Rename(path, path+".old")) |
| 157 | must(os.Symlink(path+".old", path)) |
| 158 | case "parent-symlink": |
| 159 | must(os.Rename(dir, dir+".old")) |
| 160 | must(os.Symlink(dir+".old", dir)) |
| 161 | case "unreadable": |
| 162 | must(os.Chmod(path, 0000)) |
| 163 | } |
| 164 | if got := l.claudeGitIgnore(info); got != (change == "unchanged") { |
| 165 | t.Fatalf("%s accepted=%v", change, got) |
| 166 | } |
| 167 | }) |
| 168 | } |
| 169 | } |
| 170 | |