| 1 | package gitguard |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "os" |
| 9 | "runtime" |
| 10 | "slices" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/sylvinus/agent-vm/internal/mounts" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | func (b *box) scan(volumes ...string) *Result { |
| 21 | b.t.Helper() |
| 22 | b.useEnv() |
| 23 | return Scan(context.Background(), Shares(b.proj, volumes), b.home) |
| 24 | } |
| 25 | |
| 26 | func (b *box) newScan(volumes ...string) *scan { |
| 27 | b.useEnv() |
| 28 | shares := Shares(b.proj, volumes) |
| 29 | return &scan{ctx: context.Background(), shares: shares, proj: shares[0], home: b.home, seen: map[string]bool{}} |
| 30 | } |
| 31 | |
| 32 | func risksText(r *Result) string { return strings.Join(r.Risks(mounts.BaseNames), "\n") } |
| 33 | |
| 34 | |
| 35 | |
| 36 | func TestScanOddPaths(t *testing.T) { |
| 37 | if runtime.GOOS == "windows" { |
| 38 | t.Skip("no newline in Windows paths") |
| 39 | } |
| 40 | for _, name := range []string{"pro\nj", "pro\tj", "pro\n"} { |
| 41 | b := newBox(t) |
| 42 | b.proj = b.home + "/" + name |
| 43 | os.MkdirAll(b.proj, 0o755) |
| 44 | b.git(b.proj, "init", "-q") |
| 45 | b.git(b.proj, "config", "core.hooksPath", ".githooks") |
| 46 | b.git(b.proj, "config", "core.fsmonitor", "tools/mon.sh") |
| 47 | r := b.scan() |
| 48 | if len(r.Hooks) != 1 || r.Hooks[0] != ".githooks\t.githooks" { |
| 49 | t.Errorf("%q: hooks %q", name, r.Hooks) |
| 50 | } |
| 51 | if got := risksText(r); got != "core.fsmonitor = tools/mon.sh" { |
| 52 | t.Errorf("%q: risks %q", name, got) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | b := newBox(t) |
| 57 | b.git(b.proj, "config", "core.hooksPath", "hk\tx/h") |
| 58 | b.git(b.proj, "config", "--add", "core.hooksPath", "hk\nx/h") |
| 59 | r := b.scan() |
| 60 | for _, h := range r.Hooks { |
| 61 | if strings.ContainsAny(h[:strings.LastIndexByte(h, '\t')], "\t\n") || !strings.HasSuffix(h, "\t") { |
| 62 | t.Errorf("hooks line %q", h) |
| 63 | } |
| 64 | } |
| 65 | if len(r.Hooks) == 0 { |
| 66 | t.Error("a hooks folder with a tab in its name was not listed") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | func TestScanGitFailures(t *testing.T) { |
| 72 | |
| 73 | b := newBox(t) |
| 74 | cfg := b.proj + "/.git/config" |
| 75 | old, _ := os.ReadFile(cfg) |
| 76 | os.WriteFile(cfg, append(old, "[core\n"...), 0o644) |
| 77 | got := risksText(b.scan()) |
| 78 | if !strings.Contains(got, "could not check the repository of the project: ") || !strings.Contains(got, "bad config") { |
| 79 | t.Errorf("bad repository config:\n%s", got) |
| 80 | } |
| 81 | |
| 82 | |
| 83 | b = newBox(t) |
| 84 | b.write(b.home+"/.gitconfig", "[core\n") |
| 85 | got = risksText(b.scan()) |
| 86 | if !strings.Contains(got, "could not check git's own config: ") { |
| 87 | t.Errorf("bad global config:\n%s", got) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | func TestScanGitTimeout(t *testing.T) { |
| 93 | if runtime.GOOS == "windows" { |
| 94 | t.Skip("shell script for git") |
| 95 | } |
| 96 | b := newBox(t) |
| 97 | fake := t.TempDir() |
| 98 | os.WriteFile(fake+"/git", []byte("#!/bin/sh\nexec sleep 60\n"), 0o755) |
| 99 | t.Setenv("PATH", fake+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 100 | old := gitTimeout |
| 101 | gitTimeout = 300 * time.Millisecond |
| 102 | t.Cleanup(func() { gitTimeout = old }) |
| 103 | start := time.Now() |
| 104 | got := risksText(b.scan()) |
| 105 | if d := time.Since(start); d > 20*time.Second { |
| 106 | t.Errorf("took %v", d) |
| 107 | } |
| 108 | if !strings.Contains(got, "could not check the repository of the project: git did not answer in time") { |
| 109 | t.Errorf("risks:\n%s", got) |
| 110 | } |
| 111 | if st := BareState(context.Background()); st != BareUnavailable { |
| 112 | t.Errorf("BareState with a hung git: %s", st) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | func TestScanOptionValues(t *testing.T) { |
| 119 | b := newBox(t) |
| 120 | b.git(b.proj, "config", "core.sshCommand", "ssh -o ProxyCommand=tools/proxy.sh") |
| 121 | b.git(b.proj, "config", "remote.o.uploadpack", "git-upload-pack --exec=./up.sh") |
| 122 | b.git(b.proj, "config", "core.pager", `less -o "ProxyCommand=./pager.sh arg"`) |
| 123 | b.git(b.proj, "config", "core.askPass", "env X=tools/ask.sh askpass") |
| 124 | |
| 125 | b.git(b.proj, "config", "credential.helper", "store --file=y --path=/usr/bin/true") |
| 126 | b.git(b.proj, "config", "alias.p", "!git push --repo=https://example.com/x") |
| 127 | b.git(b.proj, "config", "core.editor", "vi -o StrictHostKeyChecking=no") |
| 128 | got := risksText(b.scan()) |
| 129 | want := []string{ |
| 130 | "core.sshcommand = ssh -o ProxyCommand=tools/proxy.sh", |
| 131 | "remote.o.uploadpack = git-upload-pack --exec=./up.sh", |
| 132 | `core.pager = less -o "ProxyCommand=./pager.sh arg"`, |
| 133 | "core.askpass = env X=tools/ask.sh askpass", |
| 134 | } |
| 135 | lines := strings.Split(got, "\n") |
| 136 | slices.Sort(lines) |
| 137 | slices.Sort(want) |
| 138 | if strings.Join(lines, "\n") != strings.Join(want, "\n") { |
| 139 | t.Errorf("risks:\n%s\nwant:\n%s", got, strings.Join(want, "\n")) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | func TestScanHookHardLink(t *testing.T) { |
| 146 | if runtime.GOOS == "windows" { |
| 147 | t.Skip("not checked on Windows") |
| 148 | } |
| 149 | b := newBox(t) |
| 150 | b.write(b.proj+"/scripts/pre-commit", "#!/bin/sh\n") |
| 151 | b.write(b.proj+"/.git/hooks/post-commit", "#!/bin/sh\n") |
| 152 | b.write(b.proj+"/scripts/sample", "#!/bin/sh\n") |
| 153 | b.write(b.proj+"/scripts/hidden", "#!/bin/sh\n") |
| 154 | for hook, file := range map[string]string{"pre-commit": "pre-commit", "pre-push.sample": "sample", ".dot": "hidden"} { |
| 155 | if err := os.Link(b.proj+"/scripts/"+file, b.proj+"/.git/hooks/"+hook); err != nil { |
| 156 | t.Skip("no hard links here:", err) |
| 157 | } |
| 158 | } |
| 159 | got := risksText(b.scan()) |
| 160 | if got != "hook pre-commit has other hard links: one may be in a share" { |
| 161 | t.Errorf("risks:\n%s", got) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | func TestScanHooksPathVariants(t *testing.T) { |
| 167 | nfd := "e\xcc\x81cole/hooks" |
| 168 | for _, c := range []struct { |
| 169 | value string |
| 170 | want string |
| 171 | }{ |
| 172 | {".githooks", ".githooks\t.githooks"}, |
| 173 | {"./.githooks/", ".githooks\t.githooks"}, |
| 174 | {"tools/../.githooks", ".githooks\t.githooks"}, |
| 175 | {"~/proj/.githooks", ".githooks\t.githooks"}, |
| 176 | {"PROJ/.githooks", ".githooks\t.githooks"}, |
| 177 | {".GITHOOKS", ".GITHOOKS\t.GITHOOKS"}, |
| 178 | {".", ".\t."}, |
| 179 | {"../outside", ""}, |
| 180 | {"~/other/hooks", ""}, |
| 181 | {nfd, nfd + "\t" + nfd}, |
| 182 | {"ev\xe2\x80\xaeil/hooks", "ev?il/hooks\t"}, |
| 183 | {"zero\xe2\x80\x8bwidth/hooks", "zero?width/hooks\t"}, |
| 184 | {"my hooks/x", "my hooks/x\tmy hooks/x"}, |
| 185 | {".git/hooks", ""}, |
| 186 | {".GIT/hooks", ""}, |
| 187 | } { |
| 188 | b := newBox(t) |
| 189 | v := strings.ReplaceAll(c.value, "PROJ", b.proj) |
| 190 | if v == nfd { |
| 191 | b.setRaw("core", "hooksPath", v) |
| 192 | } else { |
| 193 | b.git(b.proj, "config", "core.hooksPath", v) |
| 194 | } |
| 195 | r := b.scan() |
| 196 | got := strings.Join(r.Hooks, "\n") |
| 197 | if got != c.want { |
| 198 | t.Errorf("core.hooksPath = %q: hooks %q, want %q", c.value, got, c.want) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | b := newBox(t) |
| 204 | b.setRaw("core", "hooksPath", nfd) |
| 205 | if names := b.scan().Names(); !slices.Contains(names, "e\xcc\x81cole") { |
| 206 | t.Errorf("names: %q", names) |
| 207 | } |
| 208 | b = newBox(t) |
| 209 | b.git(b.proj, "config", "core.hooksPath", "ev\xe2\x80\xaeil/hooks") |
| 210 | for _, n := range b.scan().Names() { |
| 211 | if strings.ContainsRune(n, 0x202e) { |
| 212 | t.Errorf("a bidi override in the names: %q", n) |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | func TestRisksNameSpellings(t *testing.T) { |
| 220 | b := newBox(t) |
| 221 | b.git(b.proj, "config", "core.fsmonitor", "Tools/run.sh") |
| 222 | b.setRaw("core", "pager", "e\xcc\x81cole/run.sh") |
| 223 | r := b.scan() |
| 224 | got := strings.Join(r.Risks([]string{".git", ".hg", "tools", "\xc3\xa9cole"}), "\n") |
| 225 | if strings.Contains(got, "fsmonitor") { |
| 226 | t.Errorf("Tools/ is not under the name tools:\n%s", got) |
| 227 | } |
| 228 | if !strings.Contains(got, "core.pager") { |
| 229 | t.Errorf("the decomposed spelling is taken for the composed name:\n%s", got) |
| 230 | } |
| 231 | if got := strings.Join(r.Risks([]string{".git", ".hg", "tools", "e\xcc\x81cole"}), "\n"); strings.Contains(got, "core.pager") { |
| 232 | t.Errorf("the name as spelled does not cover it:\n%s", got) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | func TestScanBareRepo(t *testing.T) { |
| 239 | b := newBox(t) |
| 240 | for _, d := range []string{"mirror.git", "deep/er/bare.git"} { |
| 241 | p := b.proj + "/" + d |
| 242 | os.MkdirAll(p, 0o755) |
| 243 | b.git(p, "init", "-q", "--bare") |
| 244 | b.git(p, "config", "core.pager", "tools/p.sh") |
| 245 | b.git(p, "config", "core.hooksPath", "hk") |
| 246 | } |
| 247 | r := b.scan() |
| 248 | for _, want := range []string{"mirror.git/hk\tmirror.git/hk", "deep/er/bare.git/hk\tdeep/er/bare.git/hk"} { |
| 249 | if !slices.Contains(r.Hooks, want) { |
| 250 | t.Errorf("hooks %q lack %q", r.Hooks, want) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | |
| 255 | |
| 256 | got := risksText(r) |
| 257 | for _, want := range []string{"config file mirror.git/config", "config file deep/er/bare.git/config"} { |
| 258 | if !strings.Contains(got, want) { |
| 259 | t.Errorf("risks lack %q:\n%s", want, got) |
| 260 | } |
| 261 | } |
| 262 | if strings.Count(got, "core.pager = tools/p.sh") != 1 { |
| 263 | t.Errorf("risks:\n%s", got) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | func TestReposWalk(t *testing.T) { |
| 271 | b := newBox(t) |
| 272 | for _, d := range []string{ |
| 273 | "node_modules/pkg", "a/node_modules/x", "a/b", "a/b/c/d", "l1/l2/l3/l4/l5/l6", "m1/m2/m3/m4/m5/m6/m7", |
| 274 | } { |
| 275 | os.MkdirAll(b.proj+"/"+d, 0o755) |
| 276 | b.git(b.proj+"/"+d, "init", "-q") |
| 277 | } |
| 278 | s := b.newScan() |
| 279 | var got []string |
| 280 | for _, r := range s.repos(s.proj) { |
| 281 | got = append(got, strings.TrimPrefix(strings.TrimPrefix(r.at, s.proj), "/")) |
| 282 | } |
| 283 | sort.Strings(got) |
| 284 | want := []string{"", "a/b", "a/b/c/d", "l1/l2/l3/l4/l5/l6", "node_modules/pkg"} |
| 285 | if strings.Join(got, ",") != strings.Join(want, ",") { |
| 286 | t.Errorf("repositories %q, want %q", got, want) |
| 287 | } |
| 288 | if len(s.risks) != 0 { |
| 289 | t.Errorf("risks: %+v", s.risks) |
| 290 | } |
| 291 | |
| 292 | old := maxFolders |
| 293 | maxFolders = 3 |
| 294 | t.Cleanup(func() { maxFolders = old }) |
| 295 | s = b.newScan() |
| 296 | s.repos(s.proj) |
| 297 | if got := risksText(&Result{risks: s.risks}); !strings.Contains(got, "repositories in the project: stopped looking after 3 folders") { |
| 298 | t.Errorf("risks:\n%s", got) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestVisible(t *testing.T) { |
| 303 | for in, want := range map[string]string{ |
| 304 | "core.editor = vi": "core.editor = vi", |
| 305 | "été 日本 😀": "été 日本 😀", |
| 306 | "a\x1b[2Jb": "a?[2Jb", |
| 307 | "ab": "a?b", |
| 308 | "abc": "a?b?c", |
| 309 | "nb sp": "nb?sp", |
| 310 | "a
b": "a?b", |
| 311 | "a\U000e0041b": "a?b", |
| 312 | "a\U0000feffb": "a?b", |
| 313 | "tab\there\nnl": "tab?here?nl", |
| 314 | "\u009b31m": "?31m", |
| 315 | "private\U000f0000": "private?", |
| 316 | } { |
| 317 | if got := visible(in); got != want { |
| 318 | t.Errorf("visible(%q) = %q, want %q", in, got, want) |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func TestBareStateScopes(t *testing.T) { |
| 324 | b := newBox(t) |
| 325 | b.useEnv() |
| 326 | if st := BareState(context.Background()); st != BareUnset && st != BareOld { |
| 327 | t.Errorf("unset: %s", st) |
| 328 | } |
| 329 | b.write(b.home+"/.gitconfig", "[safe]\n\tbareRepository = explicit\n") |
| 330 | if st := BareState(context.Background()); st != BareOK && st != BareOld { |
| 331 | t.Errorf("explicit in the global config: %s", st) |
| 332 | } |
| 333 | b.write(b.home+"/.gitconfig", "[safe]\n\tbareRepository = all\n") |
| 334 | if st := BareState(context.Background()); st != BareUnset && st != BareOld { |
| 335 | t.Errorf("all: %s", st) |
| 336 | } |
| 337 | } |