| 1 | package main |
| 2 | |
| 3 | // The evidence a landing writes stays within every bound the push hook |
| 4 | // reads an attestation ref within (change.AttestationDepth and the bounds |
| 5 | // beside it), or nothing lands. Each case drives land over a real |
| 6 | // repository, with attest and cue stood in for: what they would say about |
| 7 | // a statement is not under test here. |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | |
| 16 | "the-valley/change" |
| 17 | "the-valley/integrator/verdict" |
| 18 | ) |
| 19 | |
| 20 | // landingWith sets up a request for a head on main's tip, whose check |
| 21 | // "build" holds note, and lands it. stored is notes the integrator's ref |
| 22 | // for the attested tree already holds, by path, as earlier landings of |
| 23 | // that tree left them. It returns the error land gave, and whether the |
| 24 | // target, the request and every record ref are as they were before. |
| 25 | func landingWith(t *testing.T, note string, stored map[string]string) (err error, untouched bool) { |
| 26 | t.Helper() |
| 27 | s := newScratchRepo(t) |
| 28 | tip := s.git("rev-parse", "HEAD") |
| 29 | head := s.commit("head", tip) |
| 30 | landed := s.commit("landed", tip) |
| 31 | keyHash := "cdcdcdcd" |
| 32 | attested, digest := strings.Repeat("ab", 32), strings.Repeat("ef", 32) |
| 33 | original := attestationPrefix + "/" + attested + "/" + keyHash |
| 34 | if len(stored) > 0 { |
| 35 | blobs := map[string]string{} |
| 36 | for path, content := range stored { |
| 37 | blobs[path] = s.input(content, "hash-object", "-w", "--stdin") |
| 38 | } |
| 39 | in := &integrator{repo: s.repo} |
| 40 | tree, err := in.writeTree(blobs) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | s.git("update-ref", original, tree) |
| 45 | } |
| 46 | r := request{ref: requestPrefix + "/main/topic", change: "topic", target: "refs/heads/main", head: head} |
| 47 | s.git("update-ref", r.ref, head) |
| 48 | |
| 49 | // attest renders a statement as it is given, and signs by adding a |
| 50 | // line, as a countersignature adds one. cue passes every statement. |
| 51 | bin := t.TempDir() |
| 52 | attest, cue := filepath.Join(bin, "attest"), filepath.Join(bin, "cue") |
| 53 | script := "#!/bin/sh\ncat\nif [ \"$1\" = sign ]; then echo '- the integrator signature'; fi\n" |
| 54 | if err := os.WriteFile(attest, []byte(script), 0o755); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if err := os.WriteFile(cue, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | |
| 61 | refs := func() string { return s.git("for-each-ref", "--format=%(refname) %(objectname)") } |
| 62 | before := refs() |
| 63 | in := &integrator{repo: s.repo, out: devNull(t), attest: attest, cue: cue, signerName: "integrator", keyHash: keyHash} |
| 64 | ch := verdict.Change{ID: "topic", Target: r.target, Attested: attested} |
| 65 | v := verdict.Verdict{Outcome: verdict.Land, Checks: []verdict.CheckVerdict{{Name: "build", Transferred: true, Rule: "unmoved"}}} |
| 66 | l := landing{commit: landed, digest: digest, notes: map[string][]byte{"build": []byte(note)}} |
| 67 | _, err = in.land(ch, v, r, tip, l, head+" "+head+" 0000000000000000", newStopwatch()) |
| 68 | return err, refs() == before |
| 69 | } |
| 70 | |
| 71 | // statement is a note for check of exactly size bytes. |
| 72 | func statement(check string, size int) string { |
| 73 | text := fmt.Sprintf("predicate.check.name %s\npredicate.result pass\n\n", check) |
| 74 | return text + strings.Repeat("x", size-len(text)-1) + "\n" |
| 75 | } |
| 76 | |
| 77 | func TestEvidenceWithinTheBoundsLands(t *testing.T) { |
| 78 | stored := map[string]string{} |
| 79 | for i := 0; i < 15; i++ { |
| 80 | check := fmt.Sprintf("c%02d", i) |
| 81 | stored[check+"/"+statementNote] = statement(check, 63<<10) |
| 82 | } |
| 83 | if err, untouched := landingWith(t, statement("build", 20<<10), stored); err != nil || untouched { |
| 84 | t.Fatalf("evidence within every bound: land says %v, and the refs moved: %v", err, !untouched) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestANoteOverTheNoteBoundLandsNothing(t *testing.T) { |
| 89 | // The note fits, and the integrator's signature takes it over. |
| 90 | err, untouched := landingWith(t, statement("build", change.AttestationNoteBytes), nil) |
| 91 | if err == nil || !strings.Contains(err.Error(), "nothing landed") || !strings.Contains(err.Error(), "in one note") { |
| 92 | t.Fatalf("a countersigned note over %d bytes: land says %v", change.AttestationNoteBytes, err) |
| 93 | } |
| 94 | if !untouched { |
| 95 | t.Fatal("a countersigned note over the note bound: a ref moved") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestMergedEvidenceOverTheByteBoundLandsNothing(t *testing.T) { |
| 100 | // Each note fits, and so does what the ref held, and the two together |
| 101 | // are past the bound on the bytes of the whole tree. |
| 102 | stored := map[string]string{} |
| 103 | for i := 0; i < 16; i++ { |
| 104 | check := fmt.Sprintf("c%02d", i) |
| 105 | stored[check+"/"+statementNote] = statement(check, 63<<10) |
| 106 | } |
| 107 | err, untouched := landingWith(t, statement("build", 20<<10), stored) |
| 108 | if err == nil || !strings.Contains(err.Error(), "nothing landed") || !strings.Contains(err.Error(), "in all") { |
| 109 | t.Fatalf("merged evidence over %d bytes: land says %v", change.AttestationBytes, err) |
| 110 | } |
| 111 | if !untouched { |
| 112 | t.Fatal("merged evidence over the byte bound: a ref moved") |
| 113 | } |
| 114 | } |