| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "the-valley/change" |
| 14 | ) |
| 15 | |
| 16 | var ref = Ref("main", changeID) |
| 17 | |
| 18 | // as is a principal that may write reviews under any name. |
| 19 | func as(principal string) Pusher { |
| 20 | return Pusher{Principal: principal, MayReview: func(string) bool { return true }} |
| 21 | } |
| 22 | |
| 23 | // appendAs builds rec on the head of w's history, checks it as the hook |
| 24 | // will, as pusher, and moves the ref, and returns the new head. |
| 25 | func (w *world) appendAs(rec *Record, pusher Pusher) string { |
| 26 | w.t.Helper() |
| 27 | old := w.refAt(ref) |
| 28 | commit, err := Build(w.repo(), old, rec, soon()) |
| 29 | if err != nil { |
| 30 | w.t.Fatal(err) |
| 31 | } |
| 32 | if err := CheckAppend(w.repo(), Update{Ref: ref, Old: old, New: commit}, pusher, w.against()); err != nil { |
| 33 | w.t.Fatalf("CheckAppend: %v", err) |
| 34 | } |
| 35 | w.setRef(ref, commit) |
| 36 | return commit |
| 37 | } |
| 38 | |
| 39 | // A history keeps every review in order, each commit on the one before it |
| 40 | // and the record it binds, and a withdrawal on the one before it alone. It |
| 41 | // keeps the records it binds once the change's own record ref is deleted |
| 42 | // and the repository pruned. |
| 43 | func TestAHistoryKeepsEveryReviewInOrder(t *testing.T) { |
| 44 | w := newWorld(t) |
| 45 | s := w.story() |
| 46 | r1 := w.verdict(s, "alice", RequestChanges) |
| 47 | r2 := w.verdict(s, "bob", Approve) |
| 48 | r3 := withdrawal(r1, "alice") |
| 49 | c1 := w.appendAs(r1, as("alice")) |
| 50 | c2 := w.appendAs(r2, as("bob")) |
| 51 | c3 := w.appendAs(r3, as("alice")) |
| 52 | |
| 53 | h, err := ReadHistory(w.repo(), ref, c3, soon()) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | got := []Entry{} |
| 58 | for _, e := range h.Entries { |
| 59 | got = append(got, Entry{Commit: e.Commit, Parents: e.Parents}) |
| 60 | } |
| 61 | want := []Entry{{c1, []string{s.rec2}, nil}, {c2, []string{c1, s.rec2}, nil}, {c3, []string{c2}, nil}} |
| 62 | if !reflect.DeepEqual(got, want) || h.Head() != c3 { |
| 63 | t.Fatalf("the history is %+v, want %+v", got, want) |
| 64 | } |
| 65 | for i, r := range []*Record{r1, r2, r3} { |
| 66 | got, _ := Encode(h.Entries[i].Review) |
| 67 | want, _ := Encode(r) |
| 68 | if !bytes.Equal(got, want) { |
| 69 | t.Fatalf("review %d reads back as %s, want %s", i, got, want) |
| 70 | } |
| 71 | } |
| 72 | if e, ok := h.Withdrawal(r1.ReviewID); !ok || e.Commit != c3 { |
| 73 | t.Fatalf("the withdrawal of the first review: %+v, %v", e, ok) |
| 74 | } |
| 75 | if _, ok := h.Withdrawal(r2.ReviewID); ok { |
| 76 | t.Fatal("the second review reads as withdrawn") |
| 77 | } |
| 78 | if e, ok := h.Find(r2.ReviewID); !ok || e.Commit != c2 { |
| 79 | t.Fatalf("the second review: %+v, %v", e, ok) |
| 80 | } |
| 81 | |
| 82 | // The change's record ref goes, and with it every other way to reach |
| 83 | // its records. The history keeps them, and the heads they name. |
| 84 | dangling := w.blob("nothing reaches this\n") |
| 85 | w.git(nil, "update-ref", "-d", change.Ref("main", changeID)) |
| 86 | w.git(nil, "gc", "--quiet", "--prune=now") |
| 87 | if _, err := w.run(nil, "cat-file", "-e", dangling); err == nil { |
| 88 | t.Fatal("gc pruned nothing, so the test shows nothing") |
| 89 | } |
| 90 | for _, kept := range []string{s.rec2, s.rec1, s.head2, s.head1} { |
| 91 | if _, err := w.run(nil, "cat-file", "-e", kept); err != nil { |
| 92 | t.Fatalf("%s was pruned with the record ref: %v", kept, err) |
| 93 | } |
| 94 | } |
| 95 | if _, err := ReadHistory(w.repo(), ref, c3, soon()); err != nil { |
| 96 | t.Fatalf("the history after the record ref went: %v", err) |
| 97 | } |
| 98 | // No record now, so no new judgment; a withdrawal needs none. |
| 99 | r4 := w.verdict(s, "carol", Approve) |
| 100 | r4.Binding.Record = change.HeadPrefix + s.rec2 |
| 101 | c4, err := Build(w.repo(), c3, r4, soon()) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | err = CheckAppend(w.repo(), Update{Ref: ref, Old: c3, New: c4}, as("carol"), w.against()) |
| 106 | if err == nil || !strings.Contains(err.Error(), "holds no record of the change") { |
| 107 | t.Fatalf("a judgment of a change with no record: %v", err) |
| 108 | } |
| 109 | w.appendAs(withdrawal(r2, "bob"), as("bob")) |
| 110 | } |
| 111 | |
| 112 | // Each case is an update the hook must refuse, and CheckAppend refuses it, |
| 113 | // saying why. |
| 114 | func TestCheckAppendRefusesWhatIsNoAppend(t *testing.T) { |
| 115 | w := newWorld(t) |
| 116 | s := w.story() |
| 117 | r1 := w.verdict(s, "alice", RequestChanges) |
| 118 | r2 := w.verdict(s, "bob", Approve) |
| 119 | c1 := w.appendAs(r1, as("alice")) |
| 120 | c2 := w.appendAs(r2, as("bob")) |
| 121 | withdrawn := w.appendAs(withdrawal(r1, "alice"), as("alice")) |
| 122 | w.setRef(ref, c2) |
| 123 | |
| 124 | encode := func(r *Record) []byte { |
| 125 | data, err := Encode(r) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | return data |
| 130 | } |
| 131 | // commit writes a review commit by hand, past every check Build makes. |
| 132 | commit := func(r *Record, parents ...string) string { return w.oneFile(File, encode(r), parents...) } |
| 133 | edited := func(edit func(*Record)) *Record { |
| 134 | r := w.verdict(s, "carol", Approve) |
| 135 | edit(r) |
| 136 | return r |
| 137 | } |
| 138 | unknown := withdrawal(r2, "bob") |
| 139 | *unknown.Withdraws = id1 |
| 140 | renamed := withdrawal(r2, "bob") |
| 141 | renamed.Review = "operator" |
| 142 | extra := w.git([]byte("100644 blob "+w.blob(string(encode(r2)))+"\t"+File+"\n100644 blob "+w.blob("x")+"\textra\n"), "mktree") |
| 143 | big := w.oneFile(File, []byte(strings.Repeat(" ", MaxRecord+1)), c2, s.rec2) |
| 144 | |
| 145 | cases := []struct { |
| 146 | name string |
| 147 | update Update |
| 148 | pusher Pusher |
| 149 | against func(*Against) |
| 150 | says string |
| 151 | }{ |
| 152 | {name: "a delete", update: Update{ref, c2, strings.Repeat("0", 40)}, says: "no push deletes one"}, |
| 153 | {name: "a ref that is no history's", update: Update{RefPrefix + "main/" + id1, c2, c2}, says: "a review history's ref is"}, |
| 154 | {name: "a key that names no principal", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: Pusher{}, says: "named principal"}, |
| 155 | {name: "a review under a name the pusher may not write", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: Pusher{Principal: "carol", MayReview: func(name string) bool { return name == "operator" }}, says: "carol may not write reviews under that name"}, |
| 156 | {name: "a review by a pusher who may write none", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: Pusher{Principal: "carol"}, says: "carol may not write reviews under that name"}, |
| 157 | {name: "a withdrawal under a name the pusher may not write", update: Update{ref, c2, commit(withdrawal(r2, "bob"), c2)}, pusher: Pusher{Principal: "bob"}, says: "bob may not write reviews under that name"}, |
| 158 | {name: "a review by someone else", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: as("dave"), says: "names carol as the reviewer, and dave is pushing it"}, |
| 159 | {name: "an ID the history holds", update: Update{ref, c2, commit(edited(func(r *Record) { r.ReviewID = r1.ReviewID }), c2, s.rec2)}, says: "is the ID of the review in " + c1}, |
| 160 | {name: "a review of another change", update: Update{ref, c2, commit(edited(func(r *Record) { r.ChangeID = otherID }), c2, s.rec2)}, says: "a review of the change " + otherID}, |
| 161 | {name: "a first review on a history", update: Update{ref, c2, commit(edited(func(r *Record) {}), s.rec2)}, says: "as the first review commit does"}, |
| 162 | {name: "a review on an older head", update: Update{ref, c2, commit(edited(func(r *Record) {}), c1, s.rec2)}, says: "its first parent is " + c1}, |
| 163 | {name: "a second history", update: Update{ref, "", commit(edited(func(r *Record) {}), c1, s.rec2)}, says: "it starts the history"}, |
| 164 | {name: "a judgment that does not keep its record", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.head2)}, says: "as its last parent"}, |
| 165 | {name: "a judgment with three parents", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec1, s.rec2)}, says: "has 3 parents"}, |
| 166 | {name: "a tree with another file", update: Update{ref, c2, w.commitTree(extra, c2, s.rec2)}, says: "holds something other than review.json alone"}, |
| 167 | {name: "a review of more than 64 KiB", update: Update{ref, c2, big}, says: "more than the 65536 it may be"}, |
| 168 | {name: "a blob", update: Update{ref, c2, w.blob("x")}, says: "is a blob, where a commit is expected"}, |
| 169 | {name: "a history that does not read", update: Update{ref, s.head2, commit(edited(func(r *Record) {}), s.head2, s.rec2)}, says: "the history it appends to does not read"}, |
| 170 | {name: "a binding that does not verify", update: Update{ref, c2, commit(edited(func(r *Record) { r.Binding.Delta = digest }), c2, s.rec2)}, says: "its binding does not verify"}, |
| 171 | {name: "a change with no record", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, against: func(a *Against) { a.RecordTip = "" }, says: "holds no record of the change"}, |
| 172 | {name: "a withdrawal of someone else's review", update: Update{ref, c2, commit(withdrawal(r1, "bob"), c2)}, pusher: as("bob"), says: "only its reviewer or an operator"}, |
| 173 | {name: "a withdrawal of a review the history lacks", update: Update{ref, c2, commit(unknown, c2)}, pusher: as("bob"), says: "holds no review with that ID"}, |
| 174 | {name: "a withdrawal under another name", update: Update{ref, c2, commit(renamed, c2)}, pusher: as("bob"), says: "under the review name operator"}, |
| 175 | {name: "a withdrawal twice", update: Update{ref, withdrawn, commit(withdrawal(r1, "alice"), withdrawn)}, pusher: as("alice"), says: "withdrew already"}, |
| 176 | {name: "a withdrawal with the record as a parent", update: Update{ref, c2, commit(withdrawal(r2, "bob"), c2, s.rec2)}, pusher: as("bob"), says: "where a withdrawal has one"}, |
| 177 | {name: "a withdrawal that starts a history", update: Update{ref, "", commit(withdrawal(r2, "bob"), s.rec2)}, pusher: as("bob"), says: "holds no review with that ID"}, |
| 178 | } |
| 179 | for _, c := range cases { |
| 180 | t.Run(c.name, func(t *testing.T) { |
| 181 | pusher := c.pusher |
| 182 | if pusher.Principal == "" && !strings.Contains(c.says, "named principal") { |
| 183 | pusher = as("carol") |
| 184 | } |
| 185 | a := w.against() |
| 186 | if c.against != nil { |
| 187 | c.against(&a) |
| 188 | } |
| 189 | err := CheckAppend(w.repo(), c.update, pusher, a) |
| 190 | if err == nil { |
| 191 | t.Fatal("it was admitted") |
| 192 | } |
| 193 | if !strings.Contains(err.Error(), c.says) { |
| 194 | t.Fatalf("the refusal does not say %q: %v", c.says, err) |
| 195 | } |
| 196 | }) |
| 197 | } |
| 198 | // A withdrawal is final: none withdraws it. |
| 199 | w.setRef(ref, withdrawn) |
| 200 | h, err := ReadHistory(w.repo(), ref, withdrawn, soon()) |
| 201 | if err != nil { |
| 202 | t.Fatal(err) |
| 203 | } |
| 204 | final := withdrawal(h.Entries[2].Review, "alice") |
| 205 | err = CheckAppend(w.repo(), Update{ref, withdrawn, commit(final, withdrawn)}, as("alice"), w.against()) |
| 206 | if err == nil || !strings.Contains(err.Error(), "a withdrawal is final") { |
| 207 | t.Fatalf("a withdrawal of a withdrawal: %v", err) |
| 208 | } |
| 209 | // An operator may withdraw anyone's review, under any name, and needs |
| 210 | // no grant of the name to do it; to judge a version, they need one. |
| 211 | w.appendAs(withdrawal(r2, "operator"), Pusher{Principal: "operator", Operator: true}) |
| 212 | judged := w.verdict(s, "operator", Approve) |
| 213 | err = CheckAppend(w.repo(), Update{ref, w.refAt(ref), commit(judged, w.refAt(ref), s.rec2)}, Pusher{Principal: "operator", Operator: true}, w.against()) |
| 214 | if err == nil || !strings.Contains(err.Error(), "operator may not write reviews under that name") { |
| 215 | t.Fatalf("an operator's judgment under a name they may not write: %v", err) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Two clients append to one history at once. The second's publish fails |
| 220 | // on its stale lease; it reads the new head and appends again, with the |
| 221 | // same review. Building that review once more finds it recorded. |
| 222 | func TestAppendsAreCompareAndSwap(t *testing.T) { |
| 223 | origin := newWorld(t) |
| 224 | s := origin.story() |
| 225 | a, b := origin.clone(), origin.clone() |
| 226 | ra := a.verdict(s, "alice", RequestChanges) |
| 227 | rb := b.verdict(s, "bob", Approve) |
| 228 | |
| 229 | ca, err := Build(a.repo(), "", ra, soon()) |
| 230 | if err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | cb, err := Build(b.repo(), "", rb, soon()) |
| 234 | if err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | if err := Publish(a.repo(), "origin", ref, "", ca, soon()); err != nil { |
| 238 | t.Fatal(err) |
| 239 | } |
| 240 | if err := Publish(b.repo(), "origin", ref, "", cb, soon()); !errors.Is(err, ErrStale) { |
| 241 | t.Fatalf("a publish on a stale lease: %v", err) |
| 242 | } |
| 243 | b.git(nil, "fetch", "--quiet", "origin", "+"+ref+":"+ref) |
| 244 | head := b.refAt(ref) |
| 245 | if head != ca { |
| 246 | t.Fatalf("the fetched head is %s, where %s published %s", head, "alice", ca) |
| 247 | } |
| 248 | cb, err = Build(b.repo(), head, rb, soon()) |
| 249 | if err != nil { |
| 250 | t.Fatal(err) |
| 251 | } |
| 252 | if err := CheckAppend(b.repo(), Update{ref, head, cb}, as("bob"), b.against()); err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | if err := Publish(b.repo(), "origin", ref, head, cb, soon()); err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | if again, err := Build(b.repo(), cb, rb, soon()); !errors.Is(err, ErrRecorded) || again != cb { |
| 259 | t.Fatalf("building a recorded review again: %s, %v", again, err) |
| 260 | } |
| 261 | changed := *rb |
| 262 | changed.Summary = "Another judgment under the same ID." |
| 263 | if _, err := Build(b.repo(), cb, &changed, soon()); err == nil || errors.Is(err, ErrRecorded) { |
| 264 | t.Fatalf("another review under a recorded ID: %v", err) |
| 265 | } |
| 266 | if err := Publish(a.repo(), "origin", ref, ca, ca, soon()); !errors.Is(err, ErrStale) { |
| 267 | t.Fatalf("a publish on a lease of an older head: %v", err) |
| 268 | } |
| 269 | // A withdrawal, which has no notes, is found recorded too. |
| 270 | wb := withdrawal(rb, "bob") |
| 271 | cw, err := Build(b.repo(), cb, wb, soon()) |
| 272 | if err != nil { |
| 273 | t.Fatal(err) |
| 274 | } |
| 275 | if err := Publish(b.repo(), "origin", ref, cb, cw, soon()); err != nil { |
| 276 | t.Fatal(err) |
| 277 | } |
| 278 | if again, err := Build(b.repo(), cw, wb, soon()); !errors.Is(err, ErrRecorded) || again != cw { |
| 279 | t.Fatalf("building a recorded withdrawal again: %s, %v", again, err) |
| 280 | } |
| 281 | h, err := ReadHistory(origin.repo(), ref, origin.refAt(ref), soon()) |
| 282 | if err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | if len(h.Entries) != 3 || h.Entries[0].Review.ReviewID != ra.ReviewID || h.Entries[1].Review.ReviewID != rb.ReviewID { |
| 286 | t.Fatalf("origin's history holds %d reviews", len(h.Entries)) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // A history holds at most MaxHistory reviews: reading a longer one fails, |
| 291 | // and so does appending to a full one. |
| 292 | func TestAHistoryIsBounded(t *testing.T) { |
| 293 | w := newWorld(t) |
| 294 | s := w.story() |
| 295 | r := w.verdict(s, "alice", Approve) |
| 296 | var stream strings.Builder |
| 297 | for k := 1; k <= MaxHistory+1; k++ { |
| 298 | id, err := NewID() |
| 299 | if err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | r.ReviewID = id |
| 303 | data, err := Encode(r) |
| 304 | if err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | fmt.Fprintf(&stream, "commit refs/heads/scratch\nmark :%d\ncommitter t <t@localhost> 0 +0000\ndata 1\nr\n", k) |
| 308 | if k == 1 { |
| 309 | fmt.Fprintf(&stream, "from %s\n", s.rec2) |
| 310 | } else { |
| 311 | fmt.Fprintf(&stream, "from :%d\nmerge %s\n", k-1, s.rec2) |
| 312 | } |
| 313 | fmt.Fprintf(&stream, "deleteall\nM 100644 inline %s\ndata %d\n%s\n", File, len(data), data) |
| 314 | } |
| 315 | marks := filepath.Join(t.TempDir(), "marks") |
| 316 | w.git([]byte(stream.String()), "fast-import", "--quiet", "--export-marks="+marks) |
| 317 | exported, err := os.ReadFile(marks) |
| 318 | if err != nil { |
| 319 | t.Fatal(err) |
| 320 | } |
| 321 | commits := map[string]string{} |
| 322 | for _, line := range strings.Split(strings.TrimSpace(string(exported)), "\n") { |
| 323 | mark, oid, _ := strings.Cut(line, " ") |
| 324 | commits[mark] = oid |
| 325 | } |
| 326 | full, past := commits[fmt.Sprintf(":%d", MaxHistory)], commits[fmt.Sprintf(":%d", MaxHistory+1)] |
| 327 | h, err := ReadHistory(w.repo(), ref, full, soon()) |
| 328 | if err != nil || len(h.Entries) != MaxHistory { |
| 329 | t.Fatalf("a history of %d reviews: %v", MaxHistory, err) |
| 330 | } |
| 331 | if _, err := ReadHistory(w.repo(), ref, past, soon()); err == nil || !strings.Contains(err.Error(), "more than the 512") { |
| 332 | t.Fatalf("a history of %d reviews: %v", MaxHistory+1, err) |
| 333 | } |
| 334 | err = CheckAppend(w.repo(), Update{ref, full, past}, as("alice"), w.against()) |
| 335 | if err == nil || !strings.Contains(err.Error(), "the most a history may") { |
| 336 | t.Fatalf("an append to a full history: %v", err) |
| 337 | } |
| 338 | } |