| 1 | package main |
| 2 | |
| 3 | // The signer projects file: which signers it restricts, to what, and the |
| 4 | // files it refuses to read. Whether a restricted signer's note is then |
| 5 | // accepted or called unknown is integrator-e2e's, over the real attest. |
| 6 | |
| 7 | import ( |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestASignerCountsOnlyInTheProjectsListedForIt(t *testing.T) { |
| 13 | s, err := parseSignerProjects([]byte(`# workers |
| 14 | laddie-worker/attestations the-valley cosmo |
| 15 | |
| 16 | retired/attestations |
| 17 | `)) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | for _, c := range []struct { |
| 22 | name, project string |
| 23 | counts bool |
| 24 | }{ |
| 25 | {"laddie-worker/attestations", "the-valley", true}, |
| 26 | {"laddie-worker/attestations", "cosmo", true}, |
| 27 | {"laddie-worker/attestations", "elsewhere", false}, |
| 28 | // A signer with no project counts nowhere. |
| 29 | {"retired/attestations", "the-valley", false}, |
| 30 | // A signer the file does not name is not restricted. |
| 31 | {"operator/attestations", "elsewhere", true}, |
| 32 | } { |
| 33 | if got := s.counts(c.name, c.project); got != c.counts { |
| 34 | t.Errorf("%s counts in %s: %v, want %v", c.name, c.project, got, c.counts) |
| 35 | } |
| 36 | } |
| 37 | var none signerProjects |
| 38 | if !none.counts("laddie-worker/attestations", "elsewhere") { |
| 39 | t.Errorf("with no file, a signer is restricted") |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestAMalformedSignerProjectsFileDoesNotRead(t *testing.T) { |
| 44 | for _, c := range []struct{ file, says string }{ |
| 45 | {"w/attestations the-valley\nw/attestations cosmo\n", "line 2: w/attestations is named on an earlier line too"}, |
| 46 | {"w/attestations+0a1b2c3d the-valley\n", "line 1: signer name \"w/attestations+0a1b2c3d\" holds a plus"}, |
| 47 | {"\xff the-valley\n", "line 1: signer name \"\\xff\" is not valid utf-8"}, |
| 48 | } { |
| 49 | _, err := parseSignerProjects([]byte(c.file)) |
| 50 | if err == nil || !strings.Contains(err.Error(), c.says) { |
| 51 | t.Errorf("%q: got %v, want an error saying %q", c.file, err, c.says) |
| 52 | } |
| 53 | } |
| 54 | if _, err := readSignerProjects(t.TempDir() + "/missing"); err == nil { |
| 55 | t.Errorf("a file that is not there was read") |
| 56 | } |
| 57 | } |