| 1 | package gitguard |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | "unicode" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | func FuzzHops(f *testing.F) { |
| 15 | for _, seed := range [][]byte{ |
| 16 | {0, 1, 2, 3, 4, 5, 6, 7, 8}, |
| 17 | {4, 4, 4, 4, 1, 2}, |
| 18 | {2, 3, 9, 1, 0, 5, 11, 7, 4}, |
| 19 | {7, 6, 5, 4, 8, 8, 0, 10}, |
| 20 | } { |
| 21 | f.Add(seed) |
| 22 | } |
| 23 | f.Fuzz(func(t *testing.T, in []byte) { |
| 24 | if len(in) > 24 { |
| 25 | return |
| 26 | } |
| 27 | next := func() int { |
| 28 | if len(in) == 0 { |
| 29 | return 0 |
| 30 | } |
| 31 | b := in[0] |
| 32 | in = in[1:] |
| 33 | return int(b) |
| 34 | } |
| 35 | root, err := filepath.EvalSymlinks(t.TempDir()) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | root = filepath.ToSlash(root) |
| 40 | sentinel := root + "/sentinel" |
| 41 | for _, d := range []string{"d0/d0", "d1/d0", "d2", "d3", "sentinel"} { |
| 42 | if err := os.MkdirAll(root+"/"+d, 0o755); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | } |
| 46 | targets := []string{"d0", "../d1", "l1", root + "/d2", "/", ".", "..", "l0", root + "/l3/d0", "d0/d0", "missing", "../l2"} |
| 47 | var links []string |
| 48 | for _, l := range []string{"l0", "l1", "l2", "l3", "d0/l0", "d1/l1"} { |
| 49 | p := root + "/" + l |
| 50 | if err := os.Symlink(targets[next()%len(targets)], p); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | links = append(links, p) |
| 54 | } |
| 55 | comps := []string{"d0", "d1", "d2", "d3", "l0", "l1", "l2", "l3", "..", ".", "x"} |
| 56 | p := root |
| 57 | for n := next()%5 + 1; n > 0; n-- { |
| 58 | p += "/" + comps[next()%len(comps)] |
| 59 | } |
| 60 | want, err := filepath.EvalSymlinks(p) |
| 61 | if err != nil { |
| 62 | return |
| 63 | } |
| 64 | got := hops(p) |
| 65 | if last := got[len(got)-1]; filepath.Clean(last) != filepath.Clean(want) { |
| 66 | t.Fatalf("hops(%q) ends at %q, the path leads to %q (%q)", p, last, want, got) |
| 67 | } |
| 68 | for _, l := range links { |
| 69 | old, _ := os.Readlink(l) |
| 70 | os.Remove(l) |
| 71 | os.Symlink(sentinel, l) |
| 72 | moved, err := filepath.EvalSymlinks(p) |
| 73 | os.Remove(l) |
| 74 | os.Symlink(old, l) |
| 75 | if (err != nil || moved != want) && !slices.Contains(got, l) { |
| 76 | t.Fatalf("%q goes through %q, which hops leaves out: %q", p, l, got) |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | func TestHooksName(t *testing.T) { |
| 85 | for rel, want := range map[string]string{ |
| 86 | ".husky/_": ".husky", |
| 87 | "tools/h": "tools", |
| 88 | "R&D/hooks": "R&D", |
| 89 | ".": "", |
| 90 | `a"b/x`: "", |
| 91 | "a\xffb/x": "", |
| 92 | "a\u2028b/x": "", |
| 93 | } { |
| 94 | if got, _ := HooksName(rel); got != want { |
| 95 | t.Errorf("HooksName(%q) = %q, want %q", rel, got, want) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func FuzzVisible(f *testing.F) { |
| 101 | for _, s := range []string{"core.editor = vi", "a\x1b[2Jb", "\u009b31m", "x\ny", "\xff\x9b", "a\xe2\x80\xaeb", "x\xe2\x80\x8by", "\xe2\x81\xa6z", "\xf3\xa0\x81\x81"} { |
| 102 | f.Add(s) |
| 103 | } |
| 104 | f.Fuzz(func(t *testing.T, s string) { |
| 105 | for _, r := range visible(s) { |
| 106 | |
| 107 | if unicode.IsControl(r) || unicode.Is(unicode.Cf, r) || unicode.Is(unicode.Z, r) && r != ' ' || unicode.Is(unicode.Co, r) { |
| 108 | t.Fatalf("%U left in %q", r, visible(s)) |
| 109 | } |
| 110 | } |
| 111 | }) |
| 112 | } |