| 1 | package main |
| 2 | |
| 3 | // The policy bridge, where this program is coupled to two others: the |
| 4 | // deriver's output, and the window a policy writes down. Both are pinned |
| 5 | // here rather than assumed. |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "reflect" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | func TestDeriverOutputIsRead(t *testing.T) { |
| 17 | out := `abc1234..def5678: 3 changed path(s) |
| 18 | policy: instance + policy |
| 19 | classes matched: code, prose |
| 20 | |
| 21 | required checks: |
| 22 | code-build mandatory code |
| 23 | prose-format default prose, knowledge |
| 24 | |
| 25 | unclassified: 1 path(s) matched no class |
| 26 | odd.txt |
| 27 | ` |
| 28 | d, err := parseDerived(out) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if !d.classes["code"] || !d.classes["prose"] || len(d.classes) != 2 { |
| 33 | t.Fatalf("classes = %v", d.classes) |
| 34 | } |
| 35 | if got := strings.Join(d.requires["prose-format"], "+"); got != "prose+knowledge" { |
| 36 | t.Fatalf("prose-format required by %q", got) |
| 37 | } |
| 38 | if got := strings.Join(d.requires["code-build"], "+"); got != "code" { |
| 39 | t.Fatalf("code-build required by %q", got) |
| 40 | } |
| 41 | if len(d.requires) != 2 { |
| 42 | t.Fatalf("required = %v", d.requires) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // The deriver ends its report with what each owed check costs. Those lines |
| 47 | // are for an author and owe nothing, so a report carrying them reads as |
| 48 | // exactly the report without them — with an unclassified block between |
| 49 | // the table and the summary, and with the summary straight after the table. |
| 50 | func TestTheCostSummaryChangesNothingRead(t *testing.T) { |
| 51 | table := `abc1234..def5678: 2 changed path(s) |
| 52 | policy: instance + policy |
| 53 | classes matched: code, prose |
| 54 | |
| 55 | required checks: |
| 56 | code-build mandatory code |
| 57 | vm-test default code |
| 58 | prose-format default prose, knowledge |
| 59 | ` |
| 60 | unclassified := ` |
| 61 | unclassified: 1 path(s) matched no class |
| 62 | odd.txt |
| 63 | ` |
| 64 | summary := ` |
| 65 | run before pushing: code-build, prose-format |
| 66 | left to the operator's [a]sk: vm-test |
| 67 | ` |
| 68 | for _, before := range []string{table, table + unclassified} { |
| 69 | want, err := parseDerived(before) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if len(want.requires) != 3 || len(want.classes) != 2 { |
| 74 | t.Fatalf("the report without a summary reads as %+v", want) |
| 75 | } |
| 76 | got, err := parseDerived(before + summary) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if !reflect.DeepEqual(got, want) { |
| 81 | t.Errorf("with the cost summary, the report reads as %+v, not %+v", got, want) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestValidityWindowsAreReadAsThePolicyWritesThem(t *testing.T) { |
| 87 | for _, c := range []struct { |
| 88 | in string |
| 89 | want time.Duration |
| 90 | }{ |
| 91 | {"", 0}, |
| 92 | {"30s", 30 * time.Second}, |
| 93 | {"15m", 15 * time.Minute}, |
| 94 | {"6h", 6 * time.Hour}, |
| 95 | {"2d", 48 * time.Hour}, |
| 96 | } { |
| 97 | got, err := parseWindow(c.in) |
| 98 | if err != nil || got != c.want { |
| 99 | t.Errorf("parseWindow(%q) = %v, %v; want %v", c.in, got, err, c.want) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Each layer's absence defaults in the safe direction of that layer's job, |
| 105 | // and the two directions are opposite (dcr-f41f718). Both are pinned here, |
| 106 | // because the value of the rule is that neither is the other. |
| 107 | |
| 108 | func TestATargetWithNoProjectLayerIsJudgedUnderTheFloorAlone(t *testing.T) { |
| 109 | tip := &worktree{dir: t.TempDir()} |
| 110 | p := policySource{projectDir: "policy/project"} |
| 111 | done, err := p.openProject(tip) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | defer done() |
| 116 | if p.note == "" { |
| 117 | t.Fatal("the pass said nothing about composing the floor alone") |
| 118 | } |
| 119 | // What the deriver and the composer are handed is a layer that adds |
| 120 | // nothing, not a directory that is not there. |
| 121 | docs, err := filepath.Glob(filepath.Join(p.project, "*.cue")) |
| 122 | if err != nil || len(docs) != 1 { |
| 123 | t.Fatalf("the resolved project layer holds %v (%v)", docs, err) |
| 124 | } |
| 125 | if body, err := os.ReadFile(docs[0]); err != nil || strings.TrimSpace(string(body)) != "package verification" { |
| 126 | t.Fatalf("the layer that adds nothing reads %q (%v)", body, err) |
| 127 | } |
| 128 | done() |
| 129 | if _, err := os.Stat(p.project); !os.IsNotExist(err) { |
| 130 | t.Fatalf("the pass left its scratch layer behind: %v", err) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestATargetsOwnProjectLayerIsUsedWhereItIs(t *testing.T) { |
| 135 | tip := &worktree{dir: t.TempDir()} |
| 136 | own := filepath.Join(tip.dir, "policy/project") |
| 137 | if err := os.MkdirAll(own, 0o755); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | if err := os.WriteFile(filepath.Join(own, "policy.cue"), []byte("package verification\n"), 0o644); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | p := policySource{projectDir: "policy/project"} |
| 144 | done, err := p.openProject(tip) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | defer done() |
| 149 | if p.project != own { |
| 150 | t.Fatalf("the project layer resolved to %s, not to %s", p.project, own) |
| 151 | } |
| 152 | if p.note != "" { |
| 153 | t.Fatalf("an ordinary composition said %q", p.note) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestAFloorThatIsNotThereIsRefused(t *testing.T) { |
| 158 | // The materialized source, which is the one a test can drive without a |
| 159 | // repository. The repository source refuses the same way, and the e2e |
| 160 | // observes it there. |
| 161 | _, done, err := instanceLayer{path: t.TempDir()}.open() |
| 162 | defer done() |
| 163 | if err == nil { |
| 164 | t.Fatal("a floor directory with no documents in it was accepted") |
| 165 | } |
| 166 | if !strings.Contains(err.Error(), floorless) { |
| 167 | t.Fatalf("the refusal reads %q", err) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func TestARefusalIsReadAsTheSentenceItIs(t *testing.T) { |
| 172 | // attest reports what it checked, then refuses, then elaborates. The |
| 173 | // sentence is the refusal; the elaboration is not. |
| 174 | out := `note /tmp/x.note |
| 175 | text ok (411 bytes signed) |
| 176 | attest: tree digest mismatch: the statement is about a different tree |
| 177 | recorded aaaa |
| 178 | found bbbb |
| 179 | ` |
| 180 | if got := refusalLine(out); got != "tree digest mismatch: the statement is about a different tree" { |
| 181 | t.Fatalf("refusalLine = %q", got) |
| 182 | } |
| 183 | if got := refusalLine("something with no prefix at all\n"); got != "something with no prefix at all" { |
| 184 | t.Fatalf("refusalLine fallback = %q", got) |
| 185 | } |
| 186 | } |