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