| 1 | package note |
| 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 | if !s.Restricts("retired/attestations") || s.Restricts("operator/attestations") { |
| 38 | t.Errorf("the file restricts the wrong signers") |
| 39 | } |
| 40 | var none SignerProjects |
| 41 | if !none.Counts("laddie-worker/attestations", "elsewhere") || none.Restricts("laddie-worker/attestations") { |
| 42 | t.Errorf("with no file, a signer is restricted") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Only a space separates words. Any other whitespace, Unicode's included, |
| 47 | // is refused inside a word rather than read as a separator, so |
| 48 | // "guarded<no-break space>sealed" is never read as two projects. The host |
| 49 | // module refuses the same words (nix/checks/hosts.nix). |
| 50 | func TestAMalformedSignerProjectsFileDoesNotRead(t *testing.T) { |
| 51 | for _, c := range []struct{ file, says string }{ |
| 52 | {"w/attestations the-valley\nw/attestations cosmo\n", "line 2: w/attestations is named on an earlier line too"}, |
| 53 | {"w/attestations+0a1b2c3d the-valley\n", `line 1: signer name "w/attestations+0a1b2c3d" holds a plus`}, |
| 54 | {"w/attestations guarded\u00a0sealed\n", "holds 0xc2"}, |
| 55 | {"w/attestations guarded\u2003sealed\n", "holds 0xe2"}, |
| 56 | {"w/attestations\u3000the-valley\n", "holds 0xe3"}, |
| 57 | {"w/attestations\tthe-valley\n", "holds 0x09"}, |
| 58 | {"w/attestations the-valley\r\n", "holds 0x0d"}, |
| 59 | {"w\u00e9/attestations the-valley\n", "holds 0xc3"}, |
| 60 | } { |
| 61 | _, err := ParseSignerProjects([]byte(c.file)) |
| 62 | if err == nil || !strings.HasPrefix(err.Error(), "line ") || !strings.Contains(err.Error(), c.says) { |
| 63 | t.Errorf("%q: got %v, want an error saying %q", c.file, err, c.says) |
| 64 | } |
| 65 | } |
| 66 | } |