gunk-dev/the-valley

lookup_test.go

source40b311d8f0demain's published tip history
8.7 KiB
1package main
2
3// Every ref the integrator reads by name is read by that exact name. git
4// rev-parse, and every git command given a ref name, goes on to try other
5// names for a ref the repository lacks: refs/tags/<name>, then
6// refs/heads/<name>. A pusher can make such a branch or tag. Each test
7// here leaves a ref missing, makes the ref git would take for it, and
8// shows the integrator treats the ref as missing.
9
10import (
11 "context"
12 "os"
13 "path/filepath"
14 "strings"
15 "testing"
16
17 "the-valley/change"
18 "the-valley/composed"
19 "the-valley/integrator/verdict"
20)
21
22// A request named for a change with no record is refused, whatever a
23// branch named like its record says.
24func TestAMissingRecordIsNoRecord(t *testing.T) {
25 s := newScratchRepo(t)
26 base := s.git("rev-parse", "HEAD")
27 head := s.commit("head", base)
28 ref := change.Ref("main", readyID)
29 s.recordText("refs/heads/"+ref, recordJSON(t, readyID, change.Ready, head), head)
30
31 in := &integrator{repo: s.repo}
32 idx, err := in.readRecords()
33 if err != nil {
34 t.Fatal(err)
35 }
36 r := request{ref: requestPrefix + "/main/" + change.RequestName(readyID), change: change.RequestName(readyID), target: "refs/heads/main", head: head}
37 outcome, why, err := in.recordRefusal(r, idx, base)
38 if err != nil {
39 t.Fatal(err)
40 }
41 if outcome != refusedNotReady || !strings.Contains(why, ref+" holds no record of it") {
42 t.Fatalf("with refs/heads/%s ready and %s missing, the request was refused %q (%s)", ref, ref, outcome, why)
43 }
44}
45
46// A request whose target is missing is not judged, and a withdrawn change
47// does not void it, whatever a branch named like the target holds.
48func TestAMissingTargetIsNoTarget(t *testing.T) {
49 s := newScratchRepo(t)
50 base := s.git("rev-parse", "HEAD")
51 head := s.commit("head", base)
52 s.git("update-ref", "refs/heads/refs/heads/gone", base)
53 data, err := change.Encode(&change.Record{
54 Schema: change.Schema, ChangeID: draftID, Target: "gone", Owner: "owner", State: change.Withdrawn, Seq: 2, Active: 1,
55 Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "a version", Branch: "topic", Uploader: "owner"}},
56 })
57 if err != nil {
58 t.Fatal(err)
59 }
60 s.recordText(change.Ref("gone", draftID), string(data), head)
61 name := change.RequestName(draftID)
62 ref := requestPrefix + "/gone/" + name
63 s.git("update-ref", ref, head)
64
65 in := &integrator{repo: s.repo, out: devNull(t)}
66 idx, err := in.readRecords()
67 if err != nil {
68 t.Fatal(err)
69 }
70 r := request{ref: ref, change: name, target: "refs/heads/gone", head: head}
71 if kept := in.voidWithdrawn([]request{r}, idx); len(kept) != 1 {
72 t.Errorf("with refs/heads/gone missing, the withdrawn change voided its request")
73 }
74 if _, there := gitTry(s.repo, "rev-parse", "--verify", "--quiet", ref); !there {
75 t.Errorf("with refs/heads/gone missing, the request was deleted")
76 }
77 if err := in.handle(r, idx, inputs{digest: "i"}); err == nil || err.Error() != "no target refs/heads/gone to integrate onto" {
78 t.Errorf("with refs/heads/gone missing, the request was judged: %v", err)
79 }
80}
81
82// A request with no memo has none, whatever a tag named like its memo
83// holds. A tag may hold a blob, as a memo does; a branch may not.
84func TestAMissingMemoIsNoMemo(t *testing.T) {
85 s := newScratchRepo(t)
86 r := request{ref: requestPrefix + "/main/topic", change: "topic", target: "refs/heads/main", head: s.git("rev-parse", "HEAD")}
87 in := &integrator{repo: s.repo}
88 planted := s.input("planted land\n", "hash-object", "-w", "--stdin")
89 s.git("update-ref", "refs/tags/"+in.memoRef(r), planted)
90 if memo, err := in.readMemo(r); err != nil || memo != "" {
91 t.Fatalf("with %s missing, the memo read %q (%v)", in.memoRef(r), memo, err)
92 }
93 if err := in.writeMemo(r, "written"); err != nil {
94 t.Fatal(err)
95 }
96 if memo, err := in.readMemo(r); err != nil || memo != "written" {
97 t.Fatalf("the memo written reads %q (%v)", memo, err)
98 }
99}
100
101// The floor is read at its ref by that exact name. An instance repository
102// with no refs/heads/main has no floor, whatever a branch named
103// refs/heads/refs/heads/main carries.
104func TestAMissingFloorRefIsNoFloor(t *testing.T) {
105 s := newScratchRepo(t)
106 dir := filepath.Join(s.repo, "policy", "instance")
107 if err := os.MkdirAll(dir, 0o755); err != nil {
108 t.Fatal(err)
109 }
110 if err := os.WriteFile(filepath.Join(dir, "floor.cue"), []byte("package verification\n"), 0o644); err != nil {
111 t.Fatal(err)
112 }
113 s.git("add", "policy")
114 s.git("commit", "--quiet", "-m", "the floor")
115 floor := composed.Floor{Repo: s.repo, Ref: "refs/heads/main", Dir: "policy/instance"}
116 read, done, err := floor.Open(context.Background(), "valley-integrator-floor")
117 done()
118 if err != nil || read == "" {
119 t.Fatalf("the floor at main did not read: %v", err)
120 }
121
122 s.git("update-ref", "refs/heads/refs/heads/main", "refs/heads/main")
123 s.git("update-ref", "-d", "refs/heads/main")
124 _, done, err = floor.Open(context.Background(), "valley-integrator-floor")
125 done()
126 if err == nil || !strings.Contains(err.Error(), "has no refs/heads/main: "+composed.Floorless) {
127 t.Fatalf("with refs/heads/main missing, the floor read returned %v", err)
128 }
129}
130
131// The signers publication is written when there is none yet, whatever a
132// branch named refs/heads/refs/the-valley/signers holds. Read through
133// that branch, the publication's compare-and-swap would expect a ref that
134// is not there, and no publication could ever be written.
135func TestAMissingPublicationIsWritten(t *testing.T) {
136 s := newScratchRepo(t)
137 s.git("update-ref", "refs/heads/"+signersRef, s.git("rev-parse", "HEAD"))
138 known := filepath.Join(t.TempDir(), "known-signers")
139 if err := os.WriteFile(known, nil, 0o644); err != nil {
140 t.Fatal(err)
141 }
142 stdout, stderr := os.Stdout, os.Stderr
143 os.Stdout, os.Stderr = devNull(t), devNull(t)
144 err := publishSigners([]string{"--instance-repo", s.repo, "--known-signers", known, "--name", "valley-host/attestations"})
145 os.Stdout, os.Stderr = stdout, stderr
146 if err != nil {
147 t.Fatalf("with %s missing, the publication failed: %v", signersRef, err)
148 }
149 if _, found, err := resolveRef(s.repo, signersRef); err != nil || !found {
150 t.Fatalf("no publication at %s (%v)", signersRef, err)
151 }
152}
153
154// Evidence is read through the objects its refs held when they were
155// listed. A ref deleted after the listing is read as it was listed, never
156// through a branch named like it.
157func TestEvidenceDeletedAfterListingIsReadAsListed(t *testing.T) {
158 s := newScratchRepo(t)
159 head := s.git("rev-parse", "HEAD")
160 subject := strings.Repeat("ab", 32)
161 ref := attestationPrefix + "/" + subject + "/" + strings.Repeat("cd", 32)
162 noteTree := func(note string) string {
163 blob := s.input(note, "hash-object", "-w", "--stdin")
164 dir := s.input("100644 blob "+blob+"\t"+statementNote+"\n", "mktree")
165 return s.input("040000 tree "+dir+"\ttree-ok\n", "mktree")
166 }
167 listed := "predicate.check.name tree-ok\npredicate.result pass\n\nthe listed signature\n"
168 planted := "predicate.check.name tree-ok\npredicate.result pass\n\nthe planted signature\n"
169 s.git("update-ref", ref, noteTree(listed))
170 plantedCommit := s.git("commit-tree", noteTree(planted), "-m", "planted")
171
172 // attest is not under test: every note reads as signed by nobody the
173 // verifier holds, and collectEvidence returns it as read.
174 attest := filepath.Join(t.TempDir(), "attest")
175 if err := os.WriteFile(attest, []byte("#!/bin/sh\necho 'attest: "+unknownSigner+"'\nexit 1\n"), 0o755); err != nil {
176 t.Fatal(err)
177 }
178 in := &integrator{repo: s.repo, attest: attest, evidenceListed: func() {
179 s.git("update-ref", "-d", ref)
180 s.git("update-ref", "refs/heads/"+ref, plantedCommit)
181 }}
182 _, notes, err := in.collectEvidence(&worktree{repo: s.repo, dir: s.repo}, head, subject)
183 if err != nil {
184 t.Fatal(err)
185 }
186 if got := string(notes["tree-ok"]); got != listed {
187 t.Fatalf("with %s deleted after it was listed, and refs/heads/%s planted, the note read is %q", ref, ref, got)
188 }
189}
190
191// A request that is gone when the integrator lands did not move, whatever
192// a branch named like it holds: the landing's transaction neither deletes
193// it nor says it moved.
194func TestAGoneRequestDidNotMove(t *testing.T) {
195 s := newScratchRepo(t)
196 head := s.git("rev-parse", "HEAD")
197 other := s.commit("other", head)
198 ref := requestPrefix + "/main/topic"
199 s.git("update-ref", "refs/heads/"+ref, other)
200
201 in := &integrator{repo: s.repo, signerName: "integrator"}
202 r := request{ref: ref, change: "topic", target: "refs/heads/main", head: head}
203 tx, err := in.prepareLanding(verdict.Change{Target: r.target}, r, head, landing{commit: head},
204 landedEvidence{refs: map[string]map[string]string{}}, head+" "+head+" 0000000000000000")
205 if err != nil {
206 t.Fatal(err)
207 }
208 for _, u := range tx.updates {
209 if u.ref == ref {
210 t.Errorf("with %s gone and refs/heads/%s planted, the transaction updates it: %+v", ref, ref, u)
211 }
212 }
213 if tx.requestNow != "" {
214 t.Errorf("with %s gone and refs/heads/%s planted, the landing says it moved to %s", ref, ref, tx.requestNow)
215 }
216}