| 1 | package main |
| 2 | |
| 3 | // Whether a signed approval authorizes a recorded request, read from real |
| 4 | // scratch repositories: notes signed with throwaway keys, and record |
| 5 | // commits written as loose objects, as the push hook admits them or as no |
| 6 | // hook would. |
| 7 | |
| 8 | import ( |
| 9 | "bytes" |
| 10 | "compress/zlib" |
| 11 | "context" |
| 12 | "crypto/ed25519" |
| 13 | "crypto/sha1" |
| 14 | "crypto/sha256" |
| 15 | "encoding/hex" |
| 16 | "errors" |
| 17 | "fmt" |
| 18 | "os" |
| 19 | "path/filepath" |
| 20 | "slices" |
| 21 | "strings" |
| 22 | "testing" |
| 23 | "time" |
| 24 | |
| 25 | "the-valley/approval" |
| 26 | "the-valley/change" |
| 27 | "the-valley/note" |
| 28 | ) |
| 29 | |
| 30 | var approvalInstance = strings.Repeat("5f", 32) |
| 31 | |
| 32 | func approvalKey(label string) approval.Key { |
| 33 | seed := sha256.Sum256([]byte("integrator approval test key " + label)) |
| 34 | return approval.NewKey(ed25519.NewKeyFromSeed(seed[:])) |
| 35 | } |
| 36 | |
| 37 | // listing is an approval list naming each key under principal. |
| 38 | func listing(principal string, keys ...approval.Key) []byte { |
| 39 | var b strings.Builder |
| 40 | b.WriteString("# approval keys\n") |
| 41 | for _, k := range keys { |
| 42 | b.WriteString(note.NewVerifierKey(principal, k.Public()).String() + "\n") |
| 43 | } |
| 44 | return []byte(b.String()) |
| 45 | } |
| 46 | |
| 47 | // written is a record commit and the record it holds. |
| 48 | type written struct { |
| 49 | oid string |
| 50 | r *change.Record |
| 51 | } |
| 52 | |
| 53 | // entry is one tree entry: its mode as a tree object spells it, its name |
| 54 | // and its object. |
| 55 | type entry struct{ mode, name, id string } |
| 56 | |
| 57 | type approvalFixture struct { |
| 58 | t *testing.T |
| 59 | repo string |
| 60 | empty string // the empty tree |
| 61 | base string // the commit every head is made on |
| 62 | heads int |
| 63 | operator approval.Key |
| 64 | } |
| 65 | |
| 66 | func newApprovalFixture(t *testing.T) *approvalFixture { |
| 67 | f := &approvalFixture{t: t, repo: t.TempDir(), operator: approvalKey("operator")} |
| 68 | if _, err := git(f.repo, "init", "--quiet"); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | f.empty = f.tree() |
| 72 | f.base = f.commit("base", f.empty) |
| 73 | return f |
| 74 | } |
| 75 | |
| 76 | // object writes body as a loose object of kind, whether or not git would |
| 77 | // write it, and returns its ID. |
| 78 | func (f *approvalFixture) object(kind string, body []byte) string { |
| 79 | f.t.Helper() |
| 80 | header := fmt.Sprintf("%s %d\x00", kind, len(body)) |
| 81 | sum := sha1.Sum(append([]byte(header), body...)) |
| 82 | id := hex.EncodeToString(sum[:]) |
| 83 | path := filepath.Join(f.repo, ".git", "objects", id[:2], id[2:]) |
| 84 | if _, err := os.Stat(path); err == nil { |
| 85 | return id |
| 86 | } |
| 87 | var b bytes.Buffer |
| 88 | w := zlib.NewWriter(&b) |
| 89 | w.Write([]byte(header)) |
| 90 | w.Write(body) |
| 91 | w.Close() |
| 92 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 93 | f.t.Fatal(err) |
| 94 | } |
| 95 | if err := os.WriteFile(path, b.Bytes(), 0o444); err != nil { |
| 96 | f.t.Fatal(err) |
| 97 | } |
| 98 | return id |
| 99 | } |
| 100 | |
| 101 | func (f *approvalFixture) tree(entries ...entry) string { |
| 102 | var b bytes.Buffer |
| 103 | for _, e := range entries { |
| 104 | raw, err := hex.DecodeString(e.id) |
| 105 | if err != nil { |
| 106 | f.t.Fatal(err) |
| 107 | } |
| 108 | b.WriteString(e.mode + " " + e.name + "\x00") |
| 109 | b.Write(raw) |
| 110 | } |
| 111 | return f.object("tree", b.Bytes()) |
| 112 | } |
| 113 | |
| 114 | // commit is a commit of tree on parents, with fixed dates, so equal |
| 115 | // content on equal parents is one commit. |
| 116 | func (f *approvalFixture) commit(message, tree string, parents ...string) string { |
| 117 | var b strings.Builder |
| 118 | b.WriteString("tree " + tree + "\n") |
| 119 | for _, p := range parents { |
| 120 | b.WriteString("parent " + p + "\n") |
| 121 | } |
| 122 | b.WriteString("author t <t@localhost> 0 +0000\ncommitter t <t@localhost> 0 +0000\n\n" + message + "\n") |
| 123 | return f.object("commit", []byte(b.String())) |
| 124 | } |
| 125 | |
| 126 | // head is a new commit on the base, unlike every other head. |
| 127 | func (f *approvalFixture) head() string { |
| 128 | f.heads++ |
| 129 | return f.commit(fmt.Sprintf("head %d", f.heads), f.empty, f.base) |
| 130 | } |
| 131 | |
| 132 | func (f *approvalFixture) blob(text string) string { return f.object("blob", []byte(text)) } |
| 133 | |
| 134 | // recordTree is a tree holding text as record.json. |
| 135 | func (f *approvalFixture) recordTree(text string) string { |
| 136 | return f.tree(entry{"100644", "record.json", f.blob(text)}) |
| 137 | } |
| 138 | |
| 139 | func (f *approvalFixture) encode(r *change.Record) string { |
| 140 | f.t.Helper() |
| 141 | data, err := change.Encode(r) |
| 142 | if err != nil { |
| 143 | f.t.Fatal(err) |
| 144 | } |
| 145 | return string(data) |
| 146 | } |
| 147 | |
| 148 | func (f *approvalFixture) write(r *change.Record, parents ...string) written { |
| 149 | return written{oid: f.commit("a change record", f.recordTree(f.encode(r)), parents...), r: r} |
| 150 | } |
| 151 | |
| 152 | func version(n int, head string) change.Version { |
| 153 | return change.Version{N: n, Head: change.HeadPrefix + head, Title: "a version", Branch: "topic", Uploader: "owner"} |
| 154 | } |
| 155 | |
| 156 | func clone(r *change.Record) *change.Record { |
| 157 | c := *r |
| 158 | c.Versions = slices.Clone(r.Versions) |
| 159 | return &c |
| 160 | } |
| 161 | |
| 162 | func headOf(v change.Version) string { return strings.TrimPrefix(v.Head, change.HeadPrefix) } |
| 163 | |
| 164 | // created is a change's first record, ready at head, as the hook's create |
| 165 | // row admits it. |
| 166 | func (f *approvalFixture) created(head string) written { |
| 167 | return f.write(&change.Record{ |
| 168 | Schema: change.Schema, ChangeID: readyID, Target: "main", Owner: "owner", |
| 169 | State: change.Ready, Seq: 1, Versions: []change.Version{version(1, head)}, Active: 1, |
| 170 | }, head) |
| 171 | } |
| 172 | |
| 173 | // adding is prev's record with a version at head added, as the hook's |
| 174 | // new-version row admits it, unwritten. |
| 175 | func adding(prev written, head string) *change.Record { |
| 176 | r := clone(prev.r) |
| 177 | r.Versions = append(r.Versions, version(len(r.Versions)+1, head)) |
| 178 | r.Active, r.State, r.Seq = len(r.Versions), change.Ready, r.Seq+1 |
| 179 | return r |
| 180 | } |
| 181 | |
| 182 | func (f *approvalFixture) versioned(prev written, head string) written { |
| 183 | return f.write(adding(prev, head), prev.oid, head) |
| 184 | } |
| 185 | |
| 186 | // moved changes prev's state, as the hook's unready, re-ready and withdraw |
| 187 | // rows admit it. |
| 188 | func (f *approvalFixture) moved(prev written, state string) written { |
| 189 | r := clone(prev.r) |
| 190 | r.State, r.Seq = state, r.Seq+1 |
| 191 | return f.write(r, prev.oid) |
| 192 | } |
| 193 | |
| 194 | // approvalStatement approves version n of the record signed. |
| 195 | func approvalStatement(signed written, n int) approval.Statement { |
| 196 | return approval.Statement{ |
| 197 | Instance: approvalInstance, Project: "app", Target: "main", Change: readyID, |
| 198 | Version: n, Head: signed.r.Versions[n-1].Head, Record: change.HeadPrefix + signed.oid, |
| 199 | Principal: "operator", Issued: time.Date(2026, 11, 6, 0, 43, 26, 0, time.UTC), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func (f *approvalFixture) sign(s approval.Statement, k approval.Key) []byte { |
| 204 | f.t.Helper() |
| 205 | n, err := approval.Sign(s, k) |
| 206 | if err != nil { |
| 207 | f.t.Fatal(err) |
| 208 | } |
| 209 | return n |
| 210 | } |
| 211 | |
| 212 | // approved is the query for a request at the head of version n of signed, |
| 213 | // with the operator's approval of it, and signed as the current record. |
| 214 | func (f *approvalFixture) approved(signed written, n int) approvalQuery { |
| 215 | s := approvalStatement(signed, n) |
| 216 | return approvalQuery{ |
| 217 | note: f.object("blob", f.sign(s, f.operator)), record: signed.oid, |
| 218 | instance: approvalInstance, project: "app", target: "main", changeID: readyID, |
| 219 | head: strings.TrimPrefix(s.Head, change.HeadPrefix), |
| 220 | authorityName: approvalSignersFile, authority: listing("operator", f.operator), |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // signedFor is the operator's approval of version n of a record held as |
| 225 | // the commit oid, whatever that commit holds. |
| 226 | func (f *approvalFixture) signedFor(oid string, r *change.Record, n int) approvalQuery { |
| 227 | return f.approved(written{oid: oid, r: r}, n) |
| 228 | } |
| 229 | |
| 230 | // outcome unpacks a result, holding it to exactly one of three: authorized |
| 231 | // with proofs, refused, or an error. |
| 232 | func outcome(t *testing.T, r approvalResult) ([]proof, *approvalRefusal, error) { |
| 233 | t.Helper() |
| 234 | proofs, ok := r.Authorized() |
| 235 | decided := 0 |
| 236 | for _, b := range []bool{ok, r.Refusal() != nil, r.Err() != nil} { |
| 237 | if b { |
| 238 | decided++ |
| 239 | } |
| 240 | } |
| 241 | if decided != 1 || ok != (len(proofs) > 0) { |
| 242 | t.Fatalf("authorized %v with %d proofs, refusal %v, err %v", ok, len(proofs), r.Refusal(), r.Err()) |
| 243 | } |
| 244 | return proofs, r.Refusal(), r.Err() |
| 245 | } |
| 246 | |
| 247 | func verify(f *approvalFixture, q approvalQuery) ([]proof, *approvalRefusal, error) { |
| 248 | f.t.Helper() |
| 249 | return outcome(f.t, verifyApproval(context.Background(), f.repo, q)) |
| 250 | } |
| 251 | |
| 252 | func TestAnApprovalAuthorizesTheRequestItsHistoryLeadsTo(t *testing.T) { |
| 253 | for _, c := range []struct { |
| 254 | name string |
| 255 | setup func(f *approvalFixture) (q approvalQuery, walked []string) |
| 256 | }{ |
| 257 | {"the current record is the signed one", func(f *approvalFixture) (approvalQuery, []string) { |
| 258 | c1 := f.created(f.head()) |
| 259 | return f.approved(c1, 1), []string{c1.oid} |
| 260 | }}, |
| 261 | {"versions were added since the signed record", func(f *approvalFixture) (approvalQuery, []string) { |
| 262 | c1 := f.created(f.head()) |
| 263 | c2 := f.versioned(c1, f.head()) |
| 264 | c3 := f.versioned(c2, f.head()) |
| 265 | q := f.approved(c1, 1) |
| 266 | q.record = c3.oid |
| 267 | return q, []string{c3.oid, c2.oid, c1.oid} |
| 268 | }}, |
| 269 | {"the approval came after a withdrawal and a fresh ready", func(f *approvalFixture) (approvalQuery, []string) { |
| 270 | c1 := f.created(f.head()) |
| 271 | c3 := f.moved(f.moved(c1, change.Withdrawn), change.Ready) |
| 272 | c4 := f.versioned(c3, f.head()) |
| 273 | q := f.approved(c3, 1) |
| 274 | q.record = c4.oid |
| 275 | return q, []string{c4.oid, c3.oid} |
| 276 | }}, |
| 277 | {"the approved head is at two versions, and the approval names the later", func(f *approvalFixture) (approvalQuery, []string) { |
| 278 | h1 := f.head() |
| 279 | c3 := f.versioned(f.versioned(f.created(h1), f.head()), h1) |
| 280 | return f.approved(c3, 3), []string{c3.oid} |
| 281 | }}, |
| 282 | {"the approved head is at two versions, and the approval names the earlier", func(f *approvalFixture) (approvalQuery, []string) { |
| 283 | h1 := f.head() |
| 284 | c3 := f.versioned(f.versioned(f.created(h1), f.head()), h1) |
| 285 | return f.approved(c3, 1), []string{c3.oid} |
| 286 | }}, |
| 287 | {"the policy requires hardware, and the hardware list holds the key", func(f *approvalFixture) (approvalQuery, []string) { |
| 288 | c1 := f.created(f.head()) |
| 289 | q := f.approved(c1, 1) |
| 290 | q.hardware, q.authorityName = true, approvalSignersHardwareFile |
| 291 | return q, []string{c1.oid} |
| 292 | }}, |
| 293 | {"the walk is as long as the record bounds allow", func(f *approvalFixture) (approvalQuery, []string) { |
| 294 | // Each step adds a version, and the versions a record holds |
| 295 | // are bounded by change.MaxRecordBlob before change.MaxVersions. |
| 296 | h1, h2 := f.head(), f.head() |
| 297 | other := map[string]string{h1: h2, h2: h1} |
| 298 | c1 := f.created(h1) |
| 299 | walked, cur := []string{c1.oid}, c1 |
| 300 | for { |
| 301 | head := other[headOf(cur.r.Last())] |
| 302 | next := adding(cur, head) |
| 303 | if len(f.encode(next)) > change.MaxRecordBlob { |
| 304 | break |
| 305 | } |
| 306 | cur = f.write(next, cur.oid, head) |
| 307 | walked = append([]string{cur.oid}, walked...) |
| 308 | } |
| 309 | if len(walked) < 100 { |
| 310 | f.t.Fatalf("the longest walk is %d records", len(walked)) |
| 311 | } |
| 312 | f.t.Logf("the longest walk reads %d records", len(walked)) |
| 313 | q := f.approved(c1, 1) |
| 314 | q.record = cur.oid |
| 315 | return q, walked |
| 316 | }}, |
| 317 | } { |
| 318 | t.Run(c.name, func(t *testing.T) { |
| 319 | f := newApprovalFixture(t) |
| 320 | q, walked := c.setup(f) |
| 321 | authority := bytes.Clone(q.authority) |
| 322 | proofs, refusal, err := verify(f, q) |
| 323 | if err != nil || refusal != nil { |
| 324 | t.Fatalf("refused: %v %v", refusal, err) |
| 325 | } |
| 326 | if len(proofs) != 1 || proofs[0].kind != proofSignedNote { |
| 327 | t.Fatalf("proofs = %+v, want one signed note", proofs) |
| 328 | } |
| 329 | p := proofs[0].signedNote |
| 330 | q.authority[0] ^= 0xff |
| 331 | body, err := git(f.repo, "cat-file", "blob", q.note) |
| 332 | if err != nil { |
| 333 | t.Fatal(err) |
| 334 | } |
| 335 | s, err := approval.ParseNote([]byte(body)) |
| 336 | if err != nil { |
| 337 | t.Fatal(err) |
| 338 | } |
| 339 | switch { |
| 340 | case string(p.note) != body: |
| 341 | t.Errorf("the proof holds the note %q, and the blob is %q", p.note, body) |
| 342 | case p.statement != s: |
| 343 | t.Errorf("the proof holds the statement %+v, and the note %+v", p.statement, s) |
| 344 | case p.authorityName != q.authorityName || !bytes.Equal(p.authority, authority): |
| 345 | t.Errorf("the proof holds the list %s %q, and the query gave %s %q", p.authorityName, p.authority, q.authorityName, authority) |
| 346 | case !slices.Equal(p.records, walked): |
| 347 | t.Errorf("the walk read %d records %q, want %d", len(p.records), p.records, len(walked)) |
| 348 | } |
| 349 | }) |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | func TestAnApprovalIsRefusedWithItsReason(t *testing.T) { |
| 354 | missing := strings.Repeat("ab", 20) |
| 355 | for _, c := range []struct { |
| 356 | name string |
| 357 | want approvalReason |
| 358 | setup func(f *approvalFixture) approvalQuery |
| 359 | }{ |
| 360 | // The note. |
| 361 | {"the note is not in the repository", approvalInvalid, func(f *approvalFixture) approvalQuery { |
| 362 | q := f.approved(f.created(f.head()), 1) |
| 363 | q.note = missing |
| 364 | return q |
| 365 | }}, |
| 366 | {"the note is a commit", approvalInvalid, func(f *approvalFixture) approvalQuery { |
| 367 | q := f.approved(f.created(f.head()), 1) |
| 368 | q.note = f.base |
| 369 | return q |
| 370 | }}, |
| 371 | {"the note is past the bound", approvalInvalid, func(f *approvalFixture) approvalQuery { |
| 372 | q := f.approved(f.created(f.head()), 1) |
| 373 | q.note = f.blob(strings.Repeat("x", approval.MaxNote+1)) |
| 374 | return q |
| 375 | }}, |
| 376 | {"the note is an attestation signed with an approval key", approvalInvalid, func(f *approvalFixture) approvalQuery { |
| 377 | q := f.approved(f.created(f.head()), 1) |
| 378 | seed := sha256.Sum256([]byte("integrator approval test key operator")) |
| 379 | n, err := note.Sign([]byte(note.StatementType+"\nsubject.primary valley-tree-v1\n"), "operator", ed25519.NewKeyFromSeed(seed[:])) |
| 380 | if err != nil { |
| 381 | f.t.Fatal(err) |
| 382 | } |
| 383 | q.note = f.object("blob", n) |
| 384 | return q |
| 385 | }}, |
| 386 | {"the note carries a second signature", approvalInvalid, func(f *approvalFixture) approvalQuery { |
| 387 | c1 := f.created(f.head()) |
| 388 | q := f.approved(c1, 1) |
| 389 | seed := sha256.Sum256([]byte("a second signer")) |
| 390 | n, err := note.AddSignature(f.sign(approvalStatement(c1, 1), f.operator), "other", ed25519.NewKeyFromSeed(seed[:])) |
| 391 | if err != nil { |
| 392 | f.t.Fatal(err) |
| 393 | } |
| 394 | q.note = f.object("blob", n) |
| 395 | return q |
| 396 | }}, |
| 397 | {"the signed text was altered", approvalInvalid, func(f *approvalFixture) approvalQuery { |
| 398 | c1 := f.created(f.head()) |
| 399 | q := f.approved(c1, 1) |
| 400 | q.note = f.object("blob", bytes.Replace(f.sign(approvalStatement(c1, 1), f.operator), []byte("00:43:26Z"), []byte("00:43:27Z"), 1)) |
| 401 | return q |
| 402 | }}, |
| 403 | |
| 404 | // The list. |
| 405 | {"the key is not listed", keyNotListed, func(f *approvalFixture) approvalQuery { |
| 406 | q := f.approved(f.created(f.head()), 1) |
| 407 | q.authority = listing("operator", approvalKey("another")) |
| 408 | return q |
| 409 | }}, |
| 410 | {"the key is listed under another principal", keyNotListed, func(f *approvalFixture) approvalQuery { |
| 411 | q := f.approved(f.created(f.head()), 1) |
| 412 | q.authority = listing("alice", f.operator) |
| 413 | return q |
| 414 | }}, |
| 415 | {"the list is empty", keyNotListed, func(f *approvalFixture) approvalQuery { |
| 416 | q := f.approved(f.created(f.head()), 1) |
| 417 | q.authority = nil |
| 418 | return q |
| 419 | }}, |
| 420 | {"the list holds only comments", keyNotListed, func(f *approvalFixture) approvalQuery { |
| 421 | q := f.approved(f.created(f.head()), 1) |
| 422 | q.authority = listing("operator") |
| 423 | return q |
| 424 | }}, |
| 425 | {"the policy requires hardware, and only the general list holds the key", keyNotListed, func(f *approvalFixture) approvalQuery { |
| 426 | q := f.approved(f.created(f.head()), 1) |
| 427 | q.hardware, q.authorityName, q.authority = true, approvalSignersHardwareFile, listing("operator", approvalKey("hardware")) |
| 428 | return q |
| 429 | }}, |
| 430 | {"the policy requires hardware, and the general list is given", authorityInvalid, func(f *approvalFixture) approvalQuery { |
| 431 | q := f.approved(f.created(f.head()), 1) |
| 432 | q.hardware = true |
| 433 | return q |
| 434 | }}, |
| 435 | {"the policy requires no hardware, and the hardware list is given", authorityInvalid, func(f *approvalFixture) approvalQuery { |
| 436 | q := f.approved(f.created(f.head()), 1) |
| 437 | q.authorityName = approvalSignersHardwareFile |
| 438 | return q |
| 439 | }}, |
| 440 | {"the list does not read", authorityInvalid, func(f *approvalFixture) approvalQuery { |
| 441 | q := f.approved(f.created(f.head()), 1) |
| 442 | q.authority = append(listing("operator", f.operator), "not a key\n"...) |
| 443 | return q |
| 444 | }}, |
| 445 | |
| 446 | // The scope and the request. |
| 447 | {"another instance", scopeMismatch, func(f *approvalFixture) approvalQuery { |
| 448 | q := f.approved(f.created(f.head()), 1) |
| 449 | q.instance = strings.Repeat("6e", 32) |
| 450 | return q |
| 451 | }}, |
| 452 | {"another project", scopeMismatch, func(f *approvalFixture) approvalQuery { |
| 453 | q := f.approved(f.created(f.head()), 1) |
| 454 | q.project = "site" |
| 455 | return q |
| 456 | }}, |
| 457 | {"another target", scopeMismatch, func(f *approvalFixture) approvalQuery { |
| 458 | q := f.approved(f.created(f.head()), 1) |
| 459 | q.target = "release" |
| 460 | return q |
| 461 | }}, |
| 462 | {"another change", scopeMismatch, func(f *approvalFixture) approvalQuery { |
| 463 | q := f.approved(f.created(f.head()), 1) |
| 464 | q.changeID = draftID |
| 465 | return q |
| 466 | }}, |
| 467 | {"the request files another head", requestMismatch, func(f *approvalFixture) approvalQuery { |
| 468 | c2 := f.versioned(f.created(f.head()), f.head()) |
| 469 | q := f.approved(c2, 1) |
| 470 | q.head = headOf(c2.r.Versions[1]) |
| 471 | return q |
| 472 | }}, |
| 473 | {"the approved head is at two versions, and the approval names one between", requestMismatch, func(f *approvalFixture) approvalQuery { |
| 474 | h1 := f.head() |
| 475 | c3 := f.versioned(f.versioned(f.created(h1), f.head()), h1) |
| 476 | s := approvalStatement(c3, 3) |
| 477 | s.Version = 2 |
| 478 | q := f.approved(c3, 3) |
| 479 | q.note = f.object("blob", f.sign(s, f.operator)) |
| 480 | return q |
| 481 | }}, |
| 482 | {"the signed and the current record give the version different heads", requestMismatch, func(f *approvalFixture) approvalQuery { |
| 483 | c1 := f.created(f.head()) |
| 484 | q := f.approved(c1, 1) |
| 485 | h := f.head() |
| 486 | r := adding(c1, h) |
| 487 | r.Versions[0] = version(1, f.head()) |
| 488 | q.record = f.write(r, c1.oid, h).oid |
| 489 | return q |
| 490 | }}, |
| 491 | {"the approved head is a blob", requestMismatch, func(f *approvalFixture) approvalQuery { |
| 492 | return f.approved(f.created(f.blob("not a commit\n")), 1) |
| 493 | }}, |
| 494 | {"the approved head is a tree", requestMismatch, func(f *approvalFixture) approvalQuery { |
| 495 | return f.approved(f.created(f.empty), 1) |
| 496 | }}, |
| 497 | {"the approved head is not in the repository", requestMismatch, func(f *approvalFixture) approvalQuery { |
| 498 | return f.approved(f.created(missing), 1) |
| 499 | }}, |
| 500 | |
| 501 | // Readiness. |
| 502 | {"the signed record is a draft", notReady, func(f *approvalFixture) approvalQuery { |
| 503 | return f.approved(f.moved(f.created(f.head()), change.Draft), 1) |
| 504 | }}, |
| 505 | {"the current record is withdrawn", notReady, func(f *approvalFixture) approvalQuery { |
| 506 | c1 := f.created(f.head()) |
| 507 | q := f.approved(c1, 1) |
| 508 | q.record = f.moved(c1, change.Withdrawn).oid |
| 509 | return q |
| 510 | }}, |
| 511 | {"the change was withdrawn and readied again since the approval", notReady, func(f *approvalFixture) approvalQuery { |
| 512 | c1 := f.created(f.head()) |
| 513 | q := f.approved(c1, 1) |
| 514 | q.record = f.versioned(f.moved(f.moved(c1, change.Withdrawn), change.Ready), f.head()).oid |
| 515 | return q |
| 516 | }}, |
| 517 | {"the change was a draft since the approval", notReady, func(f *approvalFixture) approvalQuery { |
| 518 | c1 := f.created(f.head()) |
| 519 | q := f.approved(c1, 1) |
| 520 | q.record = f.versioned(f.moved(c1, change.Draft), f.head()).oid |
| 521 | return q |
| 522 | }}, |
| 523 | {"a replacement ref stands a ready record in for the current one", notReady, func(f *approvalFixture) approvalQuery { |
| 524 | c1 := f.created(f.head()) |
| 525 | q := f.approved(c1, 1) |
| 526 | q.record = f.moved(c1, change.Draft).oid |
| 527 | if _, err := git(f.repo, "update-ref", "refs/replace/"+q.record, f.versioned(c1, f.head()).oid); err != nil { |
| 528 | f.t.Fatal(err) |
| 529 | } |
| 530 | return q |
| 531 | }}, |
| 532 | |
| 533 | // The signed record. |
| 534 | {"the signed record is not in the repository", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 535 | c1 := f.created(f.head()) |
| 536 | return f.signedFor(missing, c1.r, 1) |
| 537 | }}, |
| 538 | {"the signed record is a tree", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 539 | c1 := f.created(f.head()) |
| 540 | return f.signedFor(f.recordTree(f.encode(c1.r)), c1.r, 1) |
| 541 | }}, |
| 542 | {"the signed record's tree is a blob", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 543 | h := f.head() |
| 544 | c1 := f.created(h) |
| 545 | return f.signedFor(f.commit("a change record", f.blob(f.encode(c1.r)), h), c1.r, 1) |
| 546 | }}, |
| 547 | {"the signed record's record.json is a tree", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 548 | h := f.head() |
| 549 | c1 := f.created(h) |
| 550 | return f.signedFor(f.commit("a change record", f.tree(entry{"100644", "record.json", f.empty}), h), c1.r, 1) |
| 551 | }}, |
| 552 | {"the signed record's tree holds a second file", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 553 | h := f.head() |
| 554 | c1 := f.created(h) |
| 555 | blob := f.blob(f.encode(c1.r)) |
| 556 | return f.signedFor(f.commit("a change record", f.tree(entry{"100644", "record.json", blob}, entry{"100644", "zz.json", blob}), h), c1.r, 1) |
| 557 | }}, |
| 558 | {"the signed record's record.json is executable", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 559 | h := f.head() |
| 560 | c1 := f.created(h) |
| 561 | return f.signedFor(f.commit("a change record", f.tree(entry{"100755", "record.json", f.blob(f.encode(c1.r))}), h), c1.r, 1) |
| 562 | }}, |
| 563 | {"the signed record's record.json is a symlink", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 564 | h := f.head() |
| 565 | c1 := f.created(h) |
| 566 | return f.signedFor(f.commit("a change record", f.tree(entry{"120000", "record.json", f.blob(f.encode(c1.r))}), h), c1.r, 1) |
| 567 | }}, |
| 568 | {"the signed record's record.json is a submodule", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 569 | h := f.head() |
| 570 | c1 := f.created(h) |
| 571 | return f.signedFor(f.commit("a change record", f.tree(entry{"160000", "record.json", h}), h), c1.r, 1) |
| 572 | }}, |
| 573 | {"the signed record's record.json is a directory", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 574 | h := f.head() |
| 575 | c1 := f.created(h) |
| 576 | return f.signedFor(f.commit("a change record", f.tree(entry{"40000", "record.json", f.recordTree(f.encode(c1.r))}), h), c1.r, 1) |
| 577 | }}, |
| 578 | {"the signed record's file is at another path", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 579 | h := f.head() |
| 580 | c1 := f.created(h) |
| 581 | return f.signedFor(f.commit("a change record", f.tree(entry{"100644", "record.jsn", f.blob(f.encode(c1.r))}), h), c1.r, 1) |
| 582 | }}, |
| 583 | {"the signed record's record.json does not read", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 584 | h := f.head() |
| 585 | c1 := f.created(h) |
| 586 | return f.signedFor(f.commit("a change record", f.recordTree("{}\n"), h), c1.r, 1) |
| 587 | }}, |
| 588 | {"the signed record's record.json repeats a field", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 589 | h := f.head() |
| 590 | c1 := f.created(h) |
| 591 | text := strings.Replace(f.encode(c1.r), `"state": "ready",`, `"state": "ready", "state": "ready",`, 1) |
| 592 | return f.signedFor(f.commit("a change record", f.recordTree(text), h), c1.r, 1) |
| 593 | }}, |
| 594 | {"the signed record's record.json holds an unknown field", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 595 | h := f.head() |
| 596 | c1 := f.created(h) |
| 597 | text := strings.Replace(f.encode(c1.r), `"state": "ready",`, `"state": "ready", "approved": true,`, 1) |
| 598 | return f.signedFor(f.commit("a change record", f.recordTree(text), h), c1.r, 1) |
| 599 | }}, |
| 600 | {"the signed record has three parents", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 601 | h := f.head() |
| 602 | c1 := f.created(h) |
| 603 | return f.signedFor(f.commit("a change record", f.recordTree(f.encode(c1.r)), h, f.head(), f.head()), c1.r, 1) |
| 604 | }}, |
| 605 | {"the signed record is another change's", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 606 | h := f.head() |
| 607 | c1 := f.created(h) |
| 608 | other := clone(c1.r) |
| 609 | other.ChangeID = draftID |
| 610 | return f.signedFor(f.write(other, h).oid, c1.r, 1) |
| 611 | }}, |
| 612 | |
| 613 | // The walk. |
| 614 | {"the current record is older than the signed one", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 615 | c1 := f.created(f.head()) |
| 616 | q := f.approved(f.versioned(c1, f.head()), 1) |
| 617 | q.record = c1.oid |
| 618 | return q |
| 619 | }}, |
| 620 | {"the current record descends from another history", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 621 | h1 := f.head() |
| 622 | c1 := f.created(h1) |
| 623 | q := f.approved(c1, 1) |
| 624 | r := clone(c1.r) |
| 625 | r.Owner = "alice" |
| 626 | other := f.write(r, h1) |
| 627 | q.record = f.versioned(f.versioned(other, f.head()), f.head()).oid |
| 628 | return q |
| 629 | }}, |
| 630 | {"a ready record follows the signed one with one parent", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 631 | c1 := f.created(f.head()) |
| 632 | q := f.approved(c1, 1) |
| 633 | r := clone(c1.r) |
| 634 | r.Seq++ |
| 635 | q.record = f.write(r, c1.oid).oid |
| 636 | return q |
| 637 | }}, |
| 638 | {"an update replaces a version rather than adding one", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 639 | c2 := f.versioned(f.created(f.head()), f.head()) |
| 640 | q := f.approved(c2, 1) |
| 641 | h := f.head() |
| 642 | r := clone(c2.r) |
| 643 | r.Versions[1], r.Seq = version(2, h), r.Seq+1 |
| 644 | q.record = f.write(r, c2.oid, h).oid |
| 645 | return q |
| 646 | }}, |
| 647 | {"an update deletes a version", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 648 | c2 := f.versioned(f.created(f.head()), f.head()) |
| 649 | q := f.approved(c2, 1) |
| 650 | r := clone(c2.r) |
| 651 | r.Versions, r.Active, r.Seq = r.Versions[:1], 1, r.Seq+1 |
| 652 | q.record = f.write(r, c2.oid, headOf(r.Versions[0])).oid |
| 653 | return q |
| 654 | }}, |
| 655 | {"an update alters an earlier version", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 656 | c2 := f.versioned(f.created(f.head()), f.head()) |
| 657 | q := f.approved(c2, 2) |
| 658 | h := f.head() |
| 659 | r := adding(c2, h) |
| 660 | r.Versions[0].Title = "another title" |
| 661 | q.record = f.write(r, c2.oid, h).oid |
| 662 | return q |
| 663 | }}, |
| 664 | {"an update changes the owner", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 665 | c1 := f.created(f.head()) |
| 666 | q := f.approved(c1, 1) |
| 667 | h := f.head() |
| 668 | r := adding(c1, h) |
| 669 | r.Owner = "alice" |
| 670 | q.record = f.write(r, c1.oid, h).oid |
| 671 | return q |
| 672 | }}, |
| 673 | {"an update changes the target", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 674 | h1 := f.head() |
| 675 | c1 := f.created(h1) |
| 676 | q := f.approved(c1, 1) |
| 677 | r := clone(c1.r) |
| 678 | r.Target = "release" |
| 679 | elsewhere := f.write(r, h1) |
| 680 | h := f.head() |
| 681 | back := adding(elsewhere, h) |
| 682 | back.Target = "main" |
| 683 | q.record = f.write(back, elsewhere.oid, h).oid |
| 684 | return q |
| 685 | }}, |
| 686 | {"an update skips a seq", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 687 | c1 := f.created(f.head()) |
| 688 | q := f.approved(c1, 1) |
| 689 | h := f.head() |
| 690 | r := adding(c1, h) |
| 691 | r.Seq++ |
| 692 | q.record = f.write(r, c1.oid, h).oid |
| 693 | return q |
| 694 | }}, |
| 695 | {"an update leaves the new version inactive", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 696 | c1 := f.created(f.head()) |
| 697 | q := f.approved(c1, 1) |
| 698 | h := f.head() |
| 699 | r := adding(c1, h) |
| 700 | r.Active = 1 |
| 701 | q.record = f.write(r, c1.oid, h).oid |
| 702 | return q |
| 703 | }}, |
| 704 | {"an update's second parent is not its new version's head", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 705 | c1 := f.created(f.head()) |
| 706 | q := f.approved(c1, 1) |
| 707 | q.record = f.write(adding(c1, f.head()), c1.oid, f.head()).oid |
| 708 | return q |
| 709 | }}, |
| 710 | {"an update's new version's head is a blob", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 711 | c1 := f.created(f.head()) |
| 712 | q := f.approved(c1, 1) |
| 713 | q.record = f.versioned(c1, f.blob("not a commit\n")).oid |
| 714 | return q |
| 715 | }}, |
| 716 | {"the signed record is reached only through a second parent", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 717 | c1 := f.created(f.head()) |
| 718 | signed := f.versioned(c1, f.head()) |
| 719 | sibling := f.versioned(c1, f.head()) |
| 720 | q := f.approved(signed, 1) |
| 721 | q.record = f.versioned(sibling, signed.oid).oid |
| 722 | return q |
| 723 | }}, |
| 724 | {"the current record's first parent is not in the repository", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 725 | c1 := f.created(f.head()) |
| 726 | q := f.approved(c1, 1) |
| 727 | h := f.head() |
| 728 | q.record = f.write(adding(c1, h), missing, h).oid |
| 729 | return q |
| 730 | }}, |
| 731 | {"the current record's record.json is past the bound", historyInvalid, func(f *approvalFixture) approvalQuery { |
| 732 | c1 := f.created(f.head()) |
| 733 | q := f.approved(c1, 1) |
| 734 | h := f.head() |
| 735 | padded := f.encode(adding(c1, h)) + strings.Repeat(" ", change.MaxRecordBlob) |
| 736 | q.record = f.commit("a change record", f.recordTree(padded), c1.oid, h) |
| 737 | return q |
| 738 | }}, |
| 739 | } { |
| 740 | t.Run(c.name, func(t *testing.T) { |
| 741 | f := newApprovalFixture(t) |
| 742 | proofs, refusal, err := verify(f, c.setup(f)) |
| 743 | switch { |
| 744 | case err != nil: |
| 745 | t.Fatalf("err = %v, want the refusal %s", err, c.want) |
| 746 | case refusal == nil: |
| 747 | t.Fatalf("authorized with %+v, want the refusal %s", proofs, c.want) |
| 748 | case refusal.reason != c.want: |
| 749 | t.Fatalf("refused with %v, want %s", refusal, c.want) |
| 750 | } |
| 751 | t.Log(refusal) |
| 752 | }) |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // An object past its bound is refused on its size: its content is never |
| 757 | // asked for. |
| 758 | func TestARecordObjectPastItsBoundIsNotRead(t *testing.T) { |
| 759 | f := newApprovalFixture(t) |
| 760 | h := f.head() |
| 761 | text := recordJSON(t, readyID, change.Ready, h) |
| 762 | blob := f.blob(text) |
| 763 | tree := f.recordTree(text) |
| 764 | bigCommit := f.commit(strings.Repeat("x", change.MaxRecordCommit), tree, h) |
| 765 | bigTree := f.tree(entry{"100644", strings.Repeat("r", change.MaxRecordTree), blob}) |
| 766 | treeCommit := f.commit("a change record", bigTree, h) |
| 767 | bigBlob := f.blob(text + strings.Repeat(" ", change.MaxRecordBlob)) |
| 768 | blobTree := f.tree(entry{"100644", "record.json", bigBlob}) |
| 769 | blobCommit := f.commit("a change record", blobTree, h) |
| 770 | for _, c := range []struct { |
| 771 | name, commit string |
| 772 | asked []string |
| 773 | }{ |
| 774 | {"the commit", bigCommit, []string{"info " + bigCommit}}, |
| 775 | {"the tree", treeCommit, []string{"info " + treeCommit, "contents " + treeCommit, "info " + bigTree}}, |
| 776 | {"record.json", blobCommit, []string{"info " + blobCommit, "contents " + blobCommit, "info " + blobTree, "contents " + blobTree, "info " + bigBlob}}, |
| 777 | } { |
| 778 | t.Run(c.name, func(t *testing.T) { |
| 779 | objects, err := openObjectReader(context.Background(), f.repo) |
| 780 | if err != nil { |
| 781 | t.Fatal(err) |
| 782 | } |
| 783 | defer objects.close() |
| 784 | _, err = readRecordCommit(objects, c.commit, "main", readyID) |
| 785 | if !errors.As(err, new(objectRefusal)) { |
| 786 | t.Fatalf("err = %v, want a refusal", err) |
| 787 | } |
| 788 | if !slices.Equal(objects.asked, c.asked) { |
| 789 | t.Errorf("asked %q, want %q", objects.asked, c.asked) |
| 790 | } |
| 791 | }) |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // git reads the repository it is given, whatever GIT_ variables the |
| 796 | // process holds. |
| 797 | func TestAnApprovalIsReadFromItsRepositoryWhateverTheEnvironmentSays(t *testing.T) { |
| 798 | f := newApprovalFixture(t) |
| 799 | q := f.approved(f.created(f.head()), 1) |
| 800 | other := t.TempDir() |
| 801 | if _, err := git(other, "init", "--quiet"); err != nil { |
| 802 | t.Fatal(err) |
| 803 | } |
| 804 | t.Setenv("GIT_DIR", filepath.Join(other, ".git")) |
| 805 | t.Setenv("GIT_OBJECT_DIRECTORY", filepath.Join(other, ".git", "objects")) |
| 806 | t.Setenv("GIT_CONFIG_COUNT", "1") |
| 807 | t.Setenv("GIT_CONFIG_KEY_0", "core.useReplaceRefs") |
| 808 | t.Setenv("GIT_CONFIG_VALUE_0", "true") |
| 809 | if proofs, refusal, err := verify(f, q); err != nil || refusal != nil || len(proofs) != 1 { |
| 810 | t.Fatalf("proofs %+v, refusal %v, err %v", proofs, refusal, err) |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // What decides nothing is an error: a query that names no object, a |
| 815 | // repository git cannot read, and a context that has ended. |
| 816 | func TestAnApprovalThatCannotBeCheckedIsAnError(t *testing.T) { |
| 817 | f := newApprovalFixture(t) |
| 818 | q := f.approved(f.created(f.head()), 1) |
| 819 | bad := q |
| 820 | bad.record = "main" |
| 821 | if _, refusal, err := verify(f, bad); err == nil || refusal != nil { |
| 822 | t.Errorf("a record named by a ref: refusal %v, err %v", refusal, err) |
| 823 | } |
| 824 | if _, refusal, err := outcome(t, verifyApproval(context.Background(), t.TempDir()+"/absent", q)); err == nil || refusal != nil { |
| 825 | t.Errorf("a repository that does not exist: refusal %v, err %v", refusal, err) |
| 826 | } |
| 827 | ctx, cancel := context.WithCancel(context.Background()) |
| 828 | cancel() |
| 829 | if _, refusal, err := outcome(t, verifyApproval(ctx, f.repo, q)); !errors.Is(err, context.Canceled) || refusal != nil { |
| 830 | t.Errorf("an ended context: refusal %v, err %v", refusal, err) |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | // An object the repository lacks is missing, even when its configuration |
| 835 | // names a promisor remote that holds it. |
| 836 | func TestAnApprovalIsNeverFetched(t *testing.T) { |
| 837 | f := newApprovalFixture(t) |
| 838 | q := f.approved(f.created(f.head()), 1) |
| 839 | remote := t.TempDir() |
| 840 | for _, args := range [][]string{ |
| 841 | {"init", "--quiet", "--bare", remote}, |
| 842 | {"push", "--quiet", remote, q.note + ":refs/tags/approval", f.base + ":refs/heads/main"}, |
| 843 | {"config", "core.repositoryformatversion", "1"}, |
| 844 | {"config", "extensions.partialClone", "origin"}, |
| 845 | {"config", "remote.origin.url", remote}, |
| 846 | {"config", "remote.origin.promisor", "true"}, |
| 847 | } { |
| 848 | if _, err := git(f.repo, args...); err != nil { |
| 849 | t.Fatal(err) |
| 850 | } |
| 851 | } |
| 852 | note := q.note |
| 853 | if err := os.Remove(filepath.Join(f.repo, ".git", "objects", note[:2], note[2:])); err != nil { |
| 854 | t.Fatal(err) |
| 855 | } |
| 856 | _, refusal, err := verify(f, q) |
| 857 | if err != nil || refusal == nil || refusal.reason != approvalInvalid { |
| 858 | t.Fatalf("refusal %v, err %v, want the note missing", refusal, err) |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | // Authorized is true only as verifyApproval's success sets it. |
| 863 | func TestOnlyASuccessfulCheckAuthorizes(t *testing.T) { |
| 864 | for name, r := range map[string]approvalResult{ |
| 865 | "the zero value": {}, |
| 866 | "proofs alone": {proofs: []proof{{kind: proofSignedNote}}}, |
| 867 | "authorized with no proof": {authorized: true}, |
| 868 | "a refusal": {refusal: &approvalRefusal{reason: notReady}}, |
| 869 | "an error": {err: errors.New("git failed")}, |
| 870 | } { |
| 871 | if proofs, ok := r.Authorized(); ok || proofs != nil { |
| 872 | t.Errorf("%s: authorized %v with %v", name, ok, proofs) |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | // An error from the operating system keeps its identity, and its text is |
| 878 | // quoted: a path cannot put a newline or an escape into a message. |
| 879 | func TestAnOperatingSystemErrorIsQuoted(t *testing.T) { |
| 880 | f := newApprovalFixture(t) |
| 881 | q := f.approved(f.created(f.head()), 1) |
| 882 | repo := filepath.Join(t.TempDir(), "a\nb\x1b[31m") |
| 883 | _, refusal, err := outcome(t, verifyApproval(context.Background(), repo, q)) |
| 884 | var pathErr *os.PathError |
| 885 | switch { |
| 886 | case err == nil || refusal != nil: |
| 887 | t.Fatalf("refusal %v, err %v, want an error", refusal, err) |
| 888 | case !errors.As(err, &pathErr) || pathErr.Path != repo: |
| 889 | t.Errorf("the error %v does not unwrap to the path error", err) |
| 890 | case strings.ContainsAny(err.Error(), "\n\x1b"): |
| 891 | t.Errorf("the error %q holds the path unquoted", err.Error()) |
| 892 | } |
| 893 | } |