| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/mergegate" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | func TestEngineeringChangeFiles(t *testing.T) { |
| 17 | dir, git := factsRepo(t) |
| 18 | base := git("rev-parse", "HEAD") |
| 19 | git("mv", "README.md", "read me 日本語.md") |
| 20 | git("rm", "Sources/Pane.swift") |
| 21 | writeFile(t, dir, "new space.txt", "one\ntwo\n") |
| 22 | writeFile(t, dir, "assets/icon.png", "\x89PNG\x00changed") |
| 23 | git("add", "-A") |
| 24 | git("commit", "-qm", "candidate") |
| 25 | sha := git("rev-parse", "HEAD") |
| 26 | before := measure(t, dir, base, sha, "") |
| 27 | decision := func(f *wire.ChangeFacts) []byte { |
| 28 | b, err := json.Marshal(mergegate.Decide(mergegate.Inputs{Candidate: sha, Verdict: wire.VerdictApprove, Checks: wire.ChecksPassed, BaseChecks: true, Eligible: true, Change: f, Protocol: wire.ProtocolCurrent, Policy: mergegate.DefaultPolicy()})) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | return b |
| 33 | } |
| 34 | previous := string(decision(before)) |
| 35 | f := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, sha) |
| 36 | if f == nil || f.Base != base || f.SHA != sha || f.Files != 4 || f.Additions != 2 || f.Deletions != 1 || f.Binary != 1 || f.Truncated || len(f.Entries) != 4 { |
| 37 | t.Fatalf("facts: %+v", f) |
| 38 | } |
| 39 | entries := map[string]wire.ChangedFile{} |
| 40 | for _, e := range f.Entries { |
| 41 | entries[e.Path] = e |
| 42 | } |
| 43 | if e := entries["read me 日本語.md"]; e.Status != "renamed" || e.OldPath != "README.md" { |
| 44 | t.Fatalf("rename: %+v", e) |
| 45 | } |
| 46 | if e := entries["Sources/Pane.swift"]; e.Status != "deleted" || e.Deletions != 1 { |
| 47 | t.Fatalf("delete: %+v", e) |
| 48 | } |
| 49 | if e := entries["assets/icon.png"]; !e.Binary || e.Status != "modified" || e.Additions != 0 { |
| 50 | t.Fatalf("binary: %+v", e) |
| 51 | } |
| 52 | if e := entries["new space.txt"]; e.Status != "added" || e.Additions != 2 { |
| 53 | t.Fatalf("added: %+v", e) |
| 54 | } |
| 55 | _ = collectChangeDiffs(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), f, nil) |
| 56 | after := measure(t, dir, base, sha, "") |
| 57 | a, _ := json.Marshal(before) |
| 58 | b, _ := json.Marshal(after) |
| 59 | if string(a) != string(b) || previous != string(decision(after)) { |
| 60 | t.Fatal("presentation changed Review facts or gate decision") |
| 61 | } |
| 62 | if changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), sha, base) != nil { |
| 63 | t.Fatal("nonancestor accepted") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestChangeFilesBounds(t *testing.T) { |
| 68 | dir, git := factsRepo(t) |
| 69 | base := git("rev-parse", "HEAD") |
| 70 | for i := 0; i < wire.MaxChangeEntries+3; i++ { |
| 71 | writeFile(t, dir, fmt.Sprintf("many/%03d.txt", i), "one\n") |
| 72 | } |
| 73 | git("add", "-A") |
| 74 | git("commit", "-qm", "many") |
| 75 | f := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, git("rev-parse", "HEAD")) |
| 76 | if f == nil || len(f.Entries) != 200 || !f.Truncated || f.Files != 203 || f.Additions != 203 || f.Deletions != 0 { |
| 77 | t.Fatalf("bounded exact totals: %+v", f) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestChangeFilesByteBoundAndMalformedListings(t *testing.T) { |
| 82 | base, sha := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| 83 | var raw, num strings.Builder |
| 84 | for i := 0; i < 200; i++ { |
| 85 | name := fmt.Sprintf("%03d/%s", i, strings.Repeat("x", 3900)) |
| 86 | fmt.Fprintf(&raw, ":000000 100644 %s %s A\x00%s\x00", base, sha, name) |
| 87 | fmt.Fprintf(&num, "1\t0\t%s\x00", name) |
| 88 | } |
| 89 | f := changeFileListings(base, sha, raw.String(), num.String()) |
| 90 | if f == nil || !f.Truncated || len(f.Entries) >= 200 || len(f.Entries) == 0 || f.Files != 200 || f.Additions != 200 { |
| 91 | t.Fatalf("byte bound: %+v", f) |
| 92 | } |
| 93 | encoded, _ := json.Marshal(f) |
| 94 | if len(encoded) > wire.MaxChangeFilesBytes { |
| 95 | t.Fatalf("encoded %d bytes", len(encoded)) |
| 96 | } |
| 97 | for name, parts := range map[string][2]string{ |
| 98 | "bad-raw": {"not raw", num.String()}, |
| 99 | "bad-num": {raw.String(), "not numstat"}, |
| 100 | "missing-stat": {raw.String(), ""}, |
| 101 | "mismatched-path": {":000000 100644 " + base + " " + sha + " A\x00one\x00", "1\t0\ttwo\x00"}, |
| 102 | "traversal": {":000000 100644 " + base + " " + sha + " A\x00../secret\x00", "1\t0\t../secret\x00"}, |
| 103 | } { |
| 104 | t.Run(name, func(t *testing.T) { |
| 105 | if changeFileListings(base, sha, parts[0], parts[1]) != nil { |
| 106 | t.Fatal("malformed listing accepted") |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestChangeFilesClippedScanNeverInventsTotals(t *testing.T) { |
| 113 | dir := t.TempDir() |
| 114 | fake := filepath.Join(dir, "git") |
| 115 | // Exactly 4096 complete raw records fit the 1 MiB read bound. A final |
| 116 | // complete record is clipped, so parsing the prefix alone would look valid. |
| 117 | script := `#!/usr/bin/python3 |
| 118 | import sys |
| 119 | head=":000000 100644 "+"a"*40+" "+"b"*40+" A\0" |
| 120 | width=256-len(head)-1 |
| 121 | if "--raw" in sys.argv: |
| 122 | for i in range(4097): |
| 123 | name=str(i).zfill(width) |
| 124 | sys.stdout.buffer.write((head+name+"\0").encode()) |
| 125 | elif "--numstat" in sys.argv: |
| 126 | for i in range(4096): |
| 127 | sys.stdout.buffer.write(("1\t0\t"+str(i).zfill(width)+"\0").encode()) |
| 128 | ` |
| 129 | if err := os.WriteFile(fake, []byte(script), 0700); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | f := changeFiles(context.Background(), fake, dir, strings.Repeat("a", 40), strings.Repeat("b", 40)) |
| 133 | if f != nil { |
| 134 | t.Fatalf("clipped scan has invented totals: %+v", f) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestChangeFilesInitialCommit(t *testing.T) { |
| 139 | dir, git := factsRepo(t) |
| 140 | root := git("rev-parse", "HEAD") |
| 141 | f := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), "", root) |
| 142 | if f == nil || f.Base != "" || f.SHA != root || f.Files != 5 { |
| 143 | t.Fatalf("initial commit: %+v", f) |
| 144 | } |
| 145 | writeFile(t, dir, "later.txt", "later\n") |
| 146 | git("add", "-A") |
| 147 | git("commit", "-qm", "later") |
| 148 | if changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), "", git("rev-parse", "HEAD")) != nil { |
| 149 | t.Fatal("missing base on non-root commit was guessed") |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestChangeFilesFinalValidationAndTypeChange(t *testing.T) { |
| 154 | base, sha := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| 155 | raw := fmt.Sprintf(":100644 120000 %s %s T\x00link\x00", base, sha) |
| 156 | f := changeFileListings(base, sha, raw, "1\t1\tlink\x00") |
| 157 | if f == nil || len(f.Entries) != 1 || f.Entries[0].Status != wire.ChangeFileModified { |
| 158 | t.Fatalf("type change: %+v", f) |
| 159 | } |
| 160 | if changeFileListings(base, sha, raw+raw, "1\t1\tlink\x001\t1\tlink\x00") != nil { |
| 161 | t.Fatal("duplicate paths passed final validation") |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestChangeFilesInvalidCommitsNeverInvokeGit(t *testing.T) { |
| 166 | dir := t.TempDir() |
| 167 | fake := filepath.Join(dir, "git") |
| 168 | if err := os.WriteFile(fake, []byte("#!/bin/sh\n: > \"$0.called\"\n"), 0700); err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | good := strings.Repeat("a", 40) |
| 172 | for _, pair := range [][2]string{{good, strings.Repeat("b", 39)}, {strings.Repeat("b", 39), good}, {good, strings.Repeat("z", 40)}} { |
| 173 | if changeFiles(context.Background(), fake, dir, pair[0], pair[1]) != nil { |
| 174 | t.Fatal("invalid commit accepted") |
| 175 | } |
| 176 | if _, err := os.Stat(fake + ".called"); !os.IsNotExist(err) { |
| 177 | t.Fatal("git invoked for invalid commit") |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestChangeFilesPublishedOnlyByEngineering(t *testing.T) { |
| 183 | for _, specialist := range []string{wire.SpecialistEngineering, wire.SpecialistCopy} { |
| 184 | t.Run(specialist, func(t *testing.T) { |
| 185 | e, f, r, _ := hubRig(t) |
| 186 | r.Specialist, r.StepID, r.Branch = specialist, "s2", "equipo/tk1-another-fix" |
| 187 | base := gitOut(t, f.dir, "rev-parse", "main") |
| 188 | gitDir := e.tree(r, base).GitDir |
| 189 | gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) |
| 190 | tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") |
| 191 | r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "another fix") |
| 192 | r.CandidateSHA = base |
| 193 | rep := e.publish(context.Background(), r, wire.StepReport{}) |
| 194 | if (rep.ChangeFiles != nil) != (specialist == wire.SpecialistEngineering) { |
| 195 | t.Fatalf("%s facts: %+v", specialist, rep.ChangeFiles) |
| 196 | } |
| 197 | }) |
| 198 | } |
| 199 | } |
| 200 | |