gunk-dev/the-valley

record_test.go

source361d756c008emain's published tip history
24.5 KiB
1package main
2
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "the-valley/change"
13)
14
15const changeID = "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz"
16
17var recordRef = change.Ref("main", changeID)
18
19// records is a bare repository to push change records into, read the way
20// the hook reads one: through gitRepository, with real git objects.
21type records struct {
22 t *testing.T
23 dir string
24 p policy
25}
26
27func newRecords(t *testing.T) *records {
28 dir := t.TempDir()
29 gitIn(t, dir, "", "init", "--quiet", "--bare", dir)
30 t.Setenv("GIT_DIR", dir)
31 return &records{t: t, dir: dir, p: policy{
32 project: "project",
33 grants: grants{"operator": {grantRequest: true}},
34 repo: gitRepository{},
35 }}
36}
37
38func (r *records) git(stdin string, args ...string) string {
39 r.t.Helper()
40 return gitIn(r.t, r.dir, stdin, args...)
41}
42
43// source is a commit standing for a head a change is marked ready at.
44func (r *records) source(subject string) string {
45 empty := r.git("", "mktree")
46 return r.commit(empty, nil, subject)
47}
48
49// commit writes a commit object exactly as given, parents and all, which
50// git's own porcelain would not always write.
51func (r *records) commit(tree string, parents []string, message string) string {
52 r.t.Helper()
53 var b strings.Builder
54 fmt.Fprintf(&b, "tree %s\n", tree)
55 for _, p := range parents {
56 fmt.Fprintf(&b, "parent %s\n", p)
57 }
58 b.WriteString("author a <a@localhost> 1700000000 +0000\ncommitter a <a@localhost> 1700000000 +0000\n\n")
59 b.WriteString(message + "\n")
60 return r.git(b.String(), "hash-object", "-t", "commit", "-w", "--literally", "--stdin")
61}
62
63func (r *records) blob(content string) string {
64 return r.git(content, "hash-object", "-w", "--stdin")
65}
66
67// recordTree is a record commit's tree, holding the text as record.json.
68func (r *records) recordTree(text string) string {
69 return r.git(fmt.Sprintf("100644 blob %s\trecord.json\n", r.blob(text)), "mktree")
70}
71
72// write writes a record commit holding text, with the parents given.
73func (r *records) write(text string, parents ...string) string {
74 return r.commit(r.recordTree(text), parents, "a change record")
75}
76
77func encode(t *testing.T, rec change.Record) string {
78 t.Helper()
79 out, err := change.Encode(&rec)
80 if err != nil {
81 t.Fatal(err)
82 }
83 return string(out)
84}
85
86// first is the record that creates the change at head h, pushed by owner.
87func first(owner, h string) change.Record {
88 return change.Record{
89 Schema: change.Schema, ChangeID: changeID, Target: "main", Owner: owner,
90 State: change.Ready, Seq: 1, Active: 1,
91 Versions: []change.Version{
92 {N: 1, Head: change.HeadPrefix + h, Title: "the first", Branch: "topic", Uploader: owner},
93 },
94 }
95}
96
97// next is rec with a new version at head h, uploaded by who.
98func next(rec change.Record, who, h string) change.Record {
99 rec.Versions = append(append([]change.Version(nil), rec.Versions...), change.Version{
100 N: len(rec.Versions) + 1, Head: change.HeadPrefix + h, Title: "the next", Branch: "topic", Uploader: who,
101 })
102 rec.Active = len(rec.Versions)
103 rec.State = change.Ready
104 rec.Seq++
105 return rec
106}
107
108// withdrawn is rec, withdrawn.
109func withdrawn(rec change.Record) change.Record {
110 return moved(rec, change.Withdrawn)
111}
112
113// moved is rec moved to the state given and nothing more, as a record
114// whose one parent is the record it replaces moves it.
115func moved(rec change.Record, state string) change.Record {
116 rec.State = state
117 rec.Seq++
118 return rec
119}
120
121// expect decides one write and holds the answer to what is wanted: "" for
122// an allowed write, and otherwise a phrase the refusal has to hold.
123func (r *records) expect(what, who, ref, old, new, says string) {
124 r.t.Helper()
125 got := r.p.decide(who, update{old: old, new: new, ref: ref})
126 switch {
127 case says == "" && got != "":
128 r.t.Errorf("%s: refused: %s", what, got)
129 case says != "" && !strings.Contains(got, says):
130 r.t.Errorf("%s: got %q, want a refusal saying %q", what, got, says)
131 }
132}
133
134const zeroID = "0000000000000000000000000000000000000000"
135
136func TestCreatingAChangeRecord(t *testing.T) {
137 r := newRecords(t)
138 h := r.source("the head")
139 h2 := r.source("another head")
140 good := encode(t, first("alice", h))
141 create := func(what, who, text string, says string, parents ...string) {
142 t.Helper()
143 r.expect(what, who, recordRef, zeroID, r.write(text, parents...), says)
144 }
145 mutate := func(f func(*change.Record)) string {
146 rec := first("alice", h)
147 f(&rec)
148 return encode(t, rec)
149 }
150
151 create("the create row", "alice", good, "", h)
152
153 // Who.
154 create("an untagged key", "", good, "a key that names no principal", h)
155
156 // Parent structure.
157 create("no parent", "alice", good, "has 0 parents")
158 create("an extra parent", "alice", good, "has 2 parents", h, h2)
159 tree := r.git("", "mktree")
160 create("a tree as the head", "alice",
161 strings.Replace(good, h, tree, 1), "is a tree, and a head is a commit", tree)
162 create("a head the repository lacks", "alice",
163 strings.Replace(good, h, strings.Repeat("9", 40), 1), "does not hold", strings.Repeat("9", 40))
164
165 // Record content.
166 create("a head other than the parent", "alice", good, "the record commit's parent is "+h2, h2)
167 create("another uploader", "alice", mutate(func(c *change.Record) { c.Versions[0].Uploader = "bob" }),
168 "names bob as its uploader", h)
169 create("another owner", "alice", mutate(func(c *change.Record) { c.Owner = "bob" }),
170 "names bob as the owner", h)
171 create("a seq other than 1", "alice", mutate(func(c *change.Record) { c.Seq = 2 }), "seq is 2", h)
172 create("a withdrawn change", "alice", mutate(func(c *change.Record) { c.State = change.Withdrawn }),
173 "state is withdrawn", h)
174 create("two versions", "alice", encode(t, next(first("alice", h2), "alice", h)), "holds 2", h)
175 create("another target", "alice", mutate(func(c *change.Record) { c.Target = "release" }),
176 "names the target release", h)
177 create("another change", "alice", mutate(func(c *change.Record) { c.ChangeID = strings.Repeat("k", 32) }),
178 "names the change kkkk", h)
179 r.expect("a ref for another target", "alice", change.Ref("release", changeID), zeroID, r.write(good, h),
180 "refs/the-valley/changes/main/<change ID>")
181 r.expect("a ref that names no change", "alice", change.RefPrefix+"main/topic", zeroID, r.write(good, h),
182 "32 letters from k to z")
183
184 // Parsing.
185 create("a duplicate key", "alice", strings.Replace(good, `"seq": 1,`, `"seq": 1, "seq": 1,`, 1),
186 `names the key "seq" twice`, h)
187 create("a duplicate key in a version", "alice", strings.Replace(good, `"n": 1,`, `"n": 1, "n": 1,`, 1),
188 `names the key "n" twice`, h)
189 create("an unknown field", "alice", strings.Replace(good, `"seq": 1,`, `"seq": 1, "extra": 1,`, 1),
190 `"extra"`, h)
191 create("trailing data", "alice", good+"{}", "more after the value", h)
192 create("bytes that are not UTF-8", "alice", strings.Replace(good, "the first", "the \xff", 1),
193 "not valid UTF-8", h)
194
195 // Tree shape.
196 shaped := func(what, entries, says string) {
197 t.Helper()
198 commit := r.commit(r.git(entries, "mktree"), []string{h}, "a change record")
199 r.expect(what, "alice", recordRef, zeroID, commit, says)
200 }
201 blob := r.blob(good)
202 shaped("an empty tree", "", "its tree is empty")
203 shaped("a second entry", fmt.Sprintf("100644 blob %s\trecord.json\n100644 blob %s\tz\n", blob, blob),
204 "more than one entry")
205 shaped("another name", fmt.Sprintf("100644 blob %s\trecord.jsonx\n", blob), `"record.jsonx"`)
206 shaped("an executable", fmt.Sprintf("100755 blob %s\trecord.json\n", blob), "mode 100755")
207 shaped("a subtree", fmt.Sprintf("040000 tree %s\trecord.json\n", tree), "mode 40000")
208 r.expect("a tree as the record", "alice", recordRef, zeroID, r.recordTree(good),
209 "points at a tree, where a change record holds a commit")
210
211 // Bounds. Each object past its bound is refused on its type and size
212 // alone, before its content is asked for.
213 big := r.commit(r.recordTree(good), []string{h}, strings.Repeat("x", maxRecordCommit))
214 r.expect("an oversized commit", "alice", recordRef, zeroID, big, "more than the 4096")
215 long := r.commit(r.git(fmt.Sprintf("100644 blob %s\t%s\n", blob, strings.Repeat("r", maxRecordTree)), "mktree"),
216 []string{h}, "a change record")
217 r.expect("an oversized tree", "alice", recordRef, zeroID, long, "more than the 1024")
218 create("an oversized record.json", "alice", good+strings.Repeat(" ", maxRecordBlob), "more than the 65536", h)
219 objects, err := openObjects()
220 if err != nil {
221 t.Fatal(err)
222 }
223 objects.subject = "a change record"
224 _, err = readRecord(objects, big)
225 objects.close()
226 if err == nil || strings.Join(objects.requests, "; ") != "info "+big {
227 t.Errorf("an oversized commit: requests = %v, error = %v; want its info alone", objects.requests, err)
228 }
229
230 // A record commit naming a parent after its other headers names one
231 // git does not read, so it is refused rather than read two ways.
232 sneaky := r.git(fmt.Sprintf("tree %s\nparent %s\nauthor a <a@localhost> 1700000000 +0000\nparent %s\n\nx\n",
233 r.recordTree(good), h, h2), "hash-object", "-t", "commit", "-w", "--literally", "--stdin")
234 r.expect("a parent among the later headers", "alice", recordRef, zeroID, sneaky, "does not read as a parent")
235}
236
237func TestAddingAVersion(t *testing.T) {
238 r := newRecords(t)
239 h1, h2, h3 := r.source("one"), r.source("two"), r.source("three")
240 v1 := first("alice", h1)
241 old := r.write(encode(t, v1), h1)
242 update := func(what, who, text, says string, parents ...string) {
243 t.Helper()
244 r.expect(what, who, recordRef, old, r.write(text, parents...), says)
245 }
246 mutate := func(f func(*change.Record)) string {
247 rec := next(v1, "alice", h2)
248 f(&rec)
249 return encode(t, rec)
250 }
251 good := encode(t, next(v1, "alice", h2))
252
253 update("the new version row", "alice", good, "", old, h2)
254 update("the new version row, by the operator", "operator", encode(t, next(v1, "operator", h2)), "", old, h2)
255
256 // Who.
257 update("a principal that is neither owner nor operator", "mallory", encode(t, next(v1, "mallory", h2)),
258 "only the change's owner, alice, or a principal holding the request grant", old, h2)
259 update("an untagged key", "", good, "a key that names no principal", old, h2)
260
261 // Parent structure.
262 update("the parents in the wrong order", "alice", good, "has the record it replaces as its first parent", h2, old)
263 update("an extra parent", "alice", good, "has the record it replaces as its first parent", old, h2, h3)
264 update("a first parent that is not the old record", "alice", good, "as its first parent", h1, h2)
265 tree := r.git("", "mktree")
266 update("a tree as the head", "alice", strings.Replace(good, h2, tree, 1), "is a tree, and a head is a commit", old, tree)
267
268 // Record content.
269 update("a changed historical entry", "alice", mutate(func(c *change.Record) { c.Versions[0].Title = "rewritten" }),
270 "changes version 1", old, h2)
271 update("a historical entry with another uploader", "alice",
272 mutate(func(c *change.Record) { c.Versions[0].Uploader = "bob" }), "changes version 1", old, h2)
273 update("a skipped n", "alice", strings.Replace(good, `"n": 2`, `"n": 3`, 1), "no gap", old, h2)
274 update("seq not incremented", "alice", mutate(func(c *change.Record) { c.Seq = 1 }), "seq is 1", old, h2)
275 update("seq incremented twice", "alice", mutate(func(c *change.Record) { c.Seq = 3 }), "seq is 3", old, h2)
276 update("the owner changed", "alice", mutate(func(c *change.Record) { c.Owner = "bob" }),
277 "changes the owner from alice to bob", old, h2)
278 update("the target changed", "alice", mutate(func(c *change.Record) { c.Target = "release" }),
279 "names the target release", old, h2)
280 update("the change ID changed", "alice", mutate(func(c *change.Record) { c.ChangeID = strings.Repeat("k", 32) }),
281 "names the change kkkk", old, h2)
282 update("the active head again", "alice", encode(t, next(v1, "alice", h1)), "needs a new head", old, h1)
283 update("a head other than the second parent", "alice", good, "second parent is "+h3, old, h3)
284 update("another uploader", "alice", encode(t, next(v1, "bob", h2)), "names bob as its uploader", old, h2)
285 update("active left on the old version", "alice", mutate(func(c *change.Record) { c.Active = 1 }),
286 "active is 1", old, h2)
287 update("a new version that withdraws", "alice", mutate(func(c *change.Record) { c.State = change.Withdrawn }),
288 "state is withdrawn", old, h2)
289 update("two new versions", "alice", encode(t, next(next(v1, "alice", h3), "alice", h2)), "holds 3", old, h2)
290 update("no new version", "alice", encode(t, withdrawn(withdrawn(v1))), "adds one version", old, h2)
291}
292
293func TestWithdrawingAndComingBack(t *testing.T) {
294 r := newRecords(t)
295 h1, h2 := r.source("one"), r.source("two")
296 v1 := first("alice", h1)
297 ready := r.write(encode(t, v1), h1)
298 withdraw := func(what, who, text, says string, parents ...string) {
299 t.Helper()
300 r.expect(what, who, recordRef, ready, r.write(text, parents...), says)
301 }
302 mutate := func(f func(*change.Record)) string {
303 rec := withdrawn(v1)
304 f(&rec)
305 return encode(t, rec)
306 }
307 good := encode(t, withdrawn(v1))
308
309 withdraw("the withdraw row", "alice", good, "", ready)
310 withdraw("the withdraw row, by the operator", "operator", good, "", ready)
311
312 withdraw("a principal that is neither owner nor operator", "mallory", good, "only the change's owner", ready)
313 withdraw("an untagged key", "", good, "a key that names no principal", ready)
314 withdraw("an extra parent", "alice", good, "first parent", ready, h1, h2)
315 withdraw("the old record as a second parent", "alice", good, "a new version adds one version", ready, ready)
316 withdraw("no parent", "alice", good, "first parent")
317 withdraw("ready to ready without a new version", "alice", mutate(func(c *change.Record) { c.State = change.Ready }),
318 "the change is ready already", ready)
319 withdraw("seq not incremented", "alice", mutate(func(c *change.Record) { c.Seq = 1 }), "seq is 1", ready)
320 withdraw("the owner changed", "alice", mutate(func(c *change.Record) { c.Owner = "bob" }), "changes the owner", ready)
321 withdraw("a version changed", "alice", mutate(func(c *change.Record) { c.Versions[0].Title = "x" }),
322 "changes the versions", ready)
323 withdraw("the target changed", "alice", mutate(func(c *change.Record) { c.Target = "release" }),
324 "names the target release", ready)
325
326 // From withdrawn, the change comes back ready: at the version it was
327 // withdrawn at, or with a new one. It never comes back as a draft.
328 gone := withdrawn(v1)
329 old := r.write(encode(t, gone), ready)
330 again := func(what, who, text, says string, parents ...string) {
331 t.Helper()
332 r.expect(what, who, recordRef, old, r.write(text, parents...), says)
333 }
334 again("the re-ready row, at a new version", "alice", encode(t, next(gone, "alice", h2)), "", old, h2)
335 again("the re-ready row, at a new version, by the operator", "operator", encode(t, next(gone, "operator", h2)), "", old, h2)
336 again("the re-ready row, at the same version", "alice", encode(t, moved(gone, change.Ready)), "", old)
337 again("the re-ready row, at the same version, by the operator", "operator", encode(t, moved(gone, change.Ready)), "", old)
338 again("a new version at the same head", "alice", encode(t, next(gone, "alice", h1)), "needs a new head", old, h1)
339 again("withdrawn twice", "alice", encode(t, withdrawn(gone)), "withdrawn already", old)
340 again("withdrawn to draft", "alice", encode(t, moved(gone, change.Draft)), "never as a draft", old)
341 again("withdrawn to draft by the operator", "operator", encode(t, moved(gone, change.Draft)), "never as a draft", old)
342 again("a new version left withdrawn", "alice",
343 encode(t, func() change.Record { c := next(gone, "alice", h2); c.State = change.Withdrawn; return c }()),
344 "state is withdrawn", old, h2)
345 again("a new version left a draft", "alice",
346 encode(t, func() change.Record { c := next(gone, "alice", h2); c.State = change.Draft; return c }()),
347 "state is draft", old, h2)
348 again("re-ready by another principal", "mallory", encode(t, next(gone, "mallory", h2)), "only the change's owner", old, h2)
349 again("re-ready at the same version by another principal", "mallory", encode(t, moved(gone, change.Ready)),
350 "only the change's owner", old)
351 again("re-ready at the same version with seq not incremented", "alice",
352 encode(t, func() change.Record { c := moved(gone, change.Ready); c.Seq--; return c }()), "seq is 2", old)
353}
354
355func TestUnreadyingAndReadyingAgain(t *testing.T) {
356 r := newRecords(t)
357 h1, h2, h3 := r.source("one"), r.source("two"), r.source("three")
358 v2 := next(first("alice", h1), "alice", h2)
359 ready := r.write(encode(t, v2), r.write(encode(t, first("alice", h1)), h1), h2)
360 unready := func(what, who, text, says string, parents ...string) {
361 t.Helper()
362 r.expect(what, who, recordRef, ready, r.write(text, parents...), says)
363 }
364 mutate := func(rec change.Record, f func(*change.Record)) string {
365 f(&rec)
366 return encode(t, rec)
367 }
368 good := encode(t, moved(v2, change.Draft))
369
370 // Ready to draft changes the state and the seq, and nothing else.
371 unready("the unready row", "alice", good, "", ready)
372 unready("the unready row, by the operator", "operator", good, "", ready)
373 unready("unready by another principal", "mallory", good, "only the change's owner", ready)
374 unready("unready by an untagged key", "", good, "a key that names no principal", ready)
375 unready("unready to an earlier version", "alice", mutate(moved(v2, change.Draft), func(c *change.Record) { c.Active = 1 }),
376 "changes active from 2 to 1", ready)
377 unready("unready with seq not incremented", "alice", mutate(moved(v2, change.Draft), func(c *change.Record) { c.Seq-- }),
378 "seq is 2", ready)
379 unready("unready with a version changed", "alice",
380 mutate(moved(v2, change.Draft), func(c *change.Record) {
381 c.Versions = append([]change.Version(nil), c.Versions...)
382 c.Versions[1].Title = "x"
383 }),
384 "changes the versions", ready)
385 unready("unready with the owner changed", "alice", mutate(moved(v2, change.Draft), func(c *change.Record) { c.Owner = "bob" }),
386 "changes the owner", ready)
387 unready("unready with a new version", "alice", encode(t, func() change.Record { c := next(v2, "alice", h3); c.State = change.Draft; return c }()),
388 "state is draft", ready, h3)
389
390 // From draft, the change is readied again at the same version or a new
391 // one, or withdrawn.
392 draft := moved(v2, change.Draft)
393 old := r.write(good, ready)
394 again := func(what, who, text, says string, parents ...string) {
395 t.Helper()
396 r.expect(what, who, recordRef, old, r.write(text, parents...), says)
397 }
398 again("the re-ready row, at the same version", "alice", encode(t, moved(draft, change.Ready)), "", old)
399 again("the re-ready row, at the same version, by the operator", "operator", encode(t, moved(draft, change.Ready)), "", old)
400 again("the re-ready row, at a new version", "alice", encode(t, next(draft, "alice", h3)), "", old, h3)
401 again("the re-ready row, at a new version, by the operator", "operator", encode(t, next(draft, "operator", h3)), "", old, h3)
402 again("the withdraw row, from a draft", "alice", encode(t, withdrawn(draft)), "", old)
403 again("the withdraw row, from a draft, by the operator", "operator", encode(t, withdrawn(draft)), "", old)
404 again("re-ready by another principal", "mallory", encode(t, moved(draft, change.Ready)), "only the change's owner", old)
405 again("a new version by another principal", "mallory", encode(t, next(draft, "mallory", h3)), "only the change's owner", old, h3)
406 again("withdraw by another principal", "mallory", encode(t, withdrawn(draft)), "only the change's owner", old)
407 again("draft to draft", "alice", encode(t, moved(draft, change.Draft)), "the change is draft already", old)
408 again("a new version at the active head", "alice", encode(t, next(draft, "alice", h2)), "needs a new head", old, h2)
409 again("a new version left a draft", "alice",
410 encode(t, func() change.Record { c := next(draft, "alice", h3); c.State = change.Draft; return c }()),
411 "state is draft", old, h3)
412 again("re-ready at another version", "alice", mutate(moved(draft, change.Ready), func(c *change.Record) { c.Active = 1 }),
413 "changes active from 2 to 1", old)
414 again("re-ready with seq incremented twice", "alice", mutate(moved(draft, change.Ready), func(c *change.Record) { c.Seq++ }),
415 "seq is 5", old)
416 again("re-ready with a version changed", "alice",
417 mutate(moved(draft, change.Ready), func(c *change.Record) {
418 c.Versions = append([]change.Version(nil), c.Versions...)
419 c.Versions[0].Head = change.HeadPrefix + h3
420 }),
421 "changes the versions", old)
422}
423
424func TestOnlyTheOperatorDeletesARecord(t *testing.T) {
425 r := newRecords(t)
426 h := r.source("one")
427 old := r.write(encode(t, first("alice", h)), h)
428 r.expect("the delete row", "operator", recordRef, old, zeroID, "")
429 r.expect("a delete by the owner", "alice", recordRef, old, zeroID, "only a principal holding the request grant may delete")
430 r.expect("a delete by an untagged key", "", recordRef, old, zeroID, "a key that names no principal")
431}
432
433// The bounds about a push as a whole are checked by preReceive, before
434// any record is read.
435func TestAPushIsBoundInTheRecordsItWrites(t *testing.T) {
436 r := newRecords(t)
437 h := r.source("one")
438 dir := t.TempDir()
439 policyFile := filepath.Join(dir, "policy.json")
440 grantsFile := filepath.Join(dir, "grants")
441 if err := os.WriteFile(policyFile, []byte(`{"refs": [], "writers": [], "grants": {}}`), 0o644); err != nil {
442 t.Fatal(err)
443 }
444 if err := os.WriteFile(grantsFile, []byte("request operator\n"), 0o644); err != nil {
445 t.Fatal(err)
446 }
447 push := func(repo repository, who string, lines []string) (int, string) {
448 var stderr bytes.Buffer
449 code := preReceive([]string{"--project", "project", "--policy", policyFile, "--grants", grantsFile,
450 "--principal", who}, strings.NewReader(strings.Join(lines, "\n")+"\n"), &stderr, repo)
451 return code, stderr.String()
452 }
453 // ids makes n distinct change IDs, from an offset.
454 ids := func(from, n int) []string {
455 var out []string
456 for i := from; i < from+n; i++ {
457 id := []byte(strings.Repeat("k", 32))
458 for j, x := 31, i; x > 0; j, x = j-1, x/16 {
459 id[j] = byte('z' - x%16)
460 }
461 out = append(out, string(id))
462 }
463 return out
464 }
465
466 // 33 record updates are refused before any is read, or any ref looked
467 // up: this repository fails the test if one is.
468 var many []string
469 for _, id := range ids(0, maxRecordUpdates+1) {
470 many = append(many, zeroID+" "+h+" "+change.Ref("main", id))
471 }
472 code, says := push(unreadable{t}, "alice", many)
473 if code == 0 || !strings.Contains(says, "it updates 33 change records, and a push may update at most 32") {
474 t.Errorf("33 record updates: exit %d, %q", code, says)
475 }
476
477 // 32 are within the bound, and each is then read and decided.
478 var creates []string
479 for _, id := range ids(0, maxRecordUpdates) {
480 rec := first("alice", h)
481 rec.ChangeID = id
482 creates = append(creates, zeroID+" "+r.write(encode(t, rec), h)+" "+change.Ref("main", id))
483 }
484 if code, says := push(gitRepository{}, "alice", creates); code != 0 {
485 t.Errorf("32 record creates: exit %d, %q", code, says)
486 }
487
488 // Those were decided and never written. With 1999 records in place,
489 // one more fits and two more do not. Once the repository is full, a
490 // delete still goes through.
491 var refs strings.Builder
492 for _, id := range ids(maxRecordUpdates, maxRecords-1) {
493 fmt.Fprintf(&refs, "create %s %s\n", change.Ref("main", id), h)
494 }
495 r.git(refs.String(), "update-ref", "--stdin")
496 line := func(id string) string {
497 rec := first("alice", h)
498 rec.ChangeID = id
499 return zeroID + " " + r.write(encode(t, rec), h) + " " + change.Ref("main", id)
500 }
501 more := ids(maxRecordUpdates+maxRecords, 2)
502 code, says = push(gitRepository{}, "alice", []string{line(more[0]), line(more[1])})
503 if code == 0 || !strings.Contains(says, "holds 1999 change records, and this push would create 2 more, past the 2000") {
504 t.Errorf("two records past the bound: exit %d, %q", code, says)
505 }
506 if code, says := push(gitRepository{}, "alice", []string{line(more[0])}); code != 0 {
507 t.Errorf("the last record within the bound: exit %d, %q", code, says)
508 }
509 r.git("", "update-ref", change.Ref("main", more[0]), h)
510 code, says = push(gitRepository{}, "alice", []string{line(more[1])})
511 if code == 0 || !strings.Contains(says, "holds 2000 change records") {
512 t.Errorf("a record past a full repository: exit %d, %q", code, says)
513 }
514 if code, says := push(gitRepository{}, "operator", []string{h + " " + zeroID + " " + change.Ref("main", more[0])}); code != 0 {
515 t.Errorf("a delete from a full repository: exit %d, %q", code, says)
516 }
517}
518
519// unreadable is a repository whose objects must not be read.
520type unreadable struct{ t *testing.T }
521
522func (u unreadable) symbolic([]string) (map[string]bool, error) {
523 u.t.Error("the refs were read")
524 return nil, errors.New("unread")
525}
526func (u unreadable) notes(string) (map[string][]byte, error) {
527 u.t.Error("an object was read")
528 return nil, errors.New("unread")
529}
530func (u unreadable) record(string) (recordCommit, error) {
531 u.t.Error("a record was read")
532 return recordCommit{}, errors.New("unread")
533}
534func (u unreadable) kind(string) (string, error) {
535 u.t.Error("an object was read")
536 return "", errors.New("unread")
537}
538func (u unreadable) records() (int, error) {
539 u.t.Error("the records were counted")
540 return 0, errors.New("unread")
541}