| 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 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestDeriverOutputIsRead(t *testing.T) { |
| 14 | out := `abc1234..def5678: 3 changed path(s) |
| 15 | policy: instance + policy |
| 16 | classes matched: code, prose |
| 17 | |
| 18 | required checks: |
| 19 | code-build mandatory code |
| 20 | prose-format default prose, knowledge |
| 21 | |
| 22 | unclassified: 1 path(s) matched no class |
| 23 | odd.txt |
| 24 | ` |
| 25 | d, err := parseDerived(out) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if !d.classes["code"] || !d.classes["prose"] || len(d.classes) != 2 { |
| 30 | t.Fatalf("classes = %v", d.classes) |
| 31 | } |
| 32 | if got := strings.Join(d.requires["prose-format"], "+"); got != "prose+knowledge" { |
| 33 | t.Fatalf("prose-format required by %q", got) |
| 34 | } |
| 35 | if got := strings.Join(d.requires["code-build"], "+"); got != "code" { |
| 36 | t.Fatalf("code-build required by %q", got) |
| 37 | } |
| 38 | if len(d.requires) != 2 { |
| 39 | t.Fatalf("required = %v", d.requires) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestValidityWindowsAreReadAsThePolicyWritesThem(t *testing.T) { |
| 44 | for _, c := range []struct { |
| 45 | in string |
| 46 | want time.Duration |
| 47 | }{ |
| 48 | {"", 0}, |
| 49 | {"30s", 30 * time.Second}, |
| 50 | {"15m", 15 * time.Minute}, |
| 51 | {"6h", 6 * time.Hour}, |
| 52 | {"2d", 48 * time.Hour}, |
| 53 | } { |
| 54 | got, err := parseWindow(c.in) |
| 55 | if err != nil || got != c.want { |
| 56 | t.Errorf("parseWindow(%q) = %v, %v; want %v", c.in, got, err, c.want) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestARefusalIsReadAsTheSentenceItIs(t *testing.T) { |
| 62 | // attest reports what it checked, then refuses, then elaborates. The |
| 63 | // sentence is the refusal; the elaboration is not. |
| 64 | out := `note /tmp/x.note |
| 65 | text ok (411 bytes signed) |
| 66 | attest: tree digest mismatch: the statement is about a different tree |
| 67 | recorded aaaa |
| 68 | found bbbb |
| 69 | ` |
| 70 | if got := refusalLine(out); got != "tree digest mismatch: the statement is about a different tree" { |
| 71 | t.Fatalf("refusalLine = %q", got) |
| 72 | } |
| 73 | if got := refusalLine("something with no prefix at all\n"); got != "something with no prefix at all" { |
| 74 | t.Fatalf("refusalLine fallback = %q", got) |
| 75 | } |
| 76 | } |