@ 2dbe47969d09d4856796bbf662b4e11c74a7f4fe | history
package policy
import (
"crypto/x509"
"strconv"
"strings"
"testing"
"government/ast"
"government/lisp2gov"
"government/ratify"
)
// mapping adds policyMappings to a fake cert, as "issuer>subject" pairs.
func mapping(c *x509.Certificate, pairs ...string) *x509.Certificate {
for _, pair := range pairs {
from, to, _ := strings.Cut(pair, ">")
if from == "any" {
from = "2.5.29.32.0"
}
if to == "any" {
to = "2.5.29.32.0"
}
c.PolicyMappings = append(c.PolicyMappings, x509.PolicyMapping{IssuerDomainPolicy: mustOID(from), SubjectDomainPolicy: mustOID(to)})
}
return c
}
// label names a GOVERNMENT node the way the design's worked example does:
// "+#2", "1#3", "(...)#4".
func label(t *testing.T, n *Node) string {
t.Helper()
_, id, text, err := ast.DecodeOID(n.Policy)
if err != nil {
t.Fatalf("%s: %v", n.Policy, err)
}
return text + "#" + strconv.FormatUint(id, 10)
}
func compiledGraph(t *testing.T, src string) *Graph {
t.Helper()
out, err := lisp2gov.Compile(src, lisp2gov.Options{})
if err != nil {
t.Fatal(err)
}
certs, err := ratify.Ratify(out, ratify.Options{At: ratifyTime})
if err != nil {
t.Fatal(err)
}
return mustProcess(t, certs)
}
// TestWorkedExample checks the design's worked example. The ′ marks in the
// design are the validator's copies: +#2 and 1#3 appear again at depth 3,
// each the only child of itself at depth 2.
func TestWorkedExample(t *testing.T) {
g := compiledGraph(t, "(+ 1 (* 2 3))")
want := []string{
"(...)#1",
"+#2 <- (...)#1 | 1#3 <- (...)#1 | (...)#4 <- (...)#1",
"+#2 <- +#2 | 1#3 <- 1#3 | *#5 <- (...)#4 | 2#6 <- (...)#4 | 3#7 <- (...)#4",
}
if g.Depth() != 3 {
t.Fatalf("depth %d, want 3", g.Depth())
}
for d := 1; d <= 3; d++ {
var nodes []string
for _, n := range g.Levels[d] {
s := label(t, n)
if d > 1 {
var parents []string
for _, p := range n.Parents {
parents = append(parents, label(t, p))
}
s += " <- " + strings.Join(parents, ", ")
}
nodes = append(nodes, s)
}
if got := strings.Join(nodes, " | "); got != want[d-1] {
t.Errorf("depth %d:\n got %s\n want %s", d, got, want[d-1])
}
}
// Nothing is pruned: every branch reaches depth 3.
if n := len(g.Levels[1][0].Children); n != 3 {
t.Errorf("(...)#1 has %d children, want 3", n)
}
}
func TestChildWithTwoParents(t *testing.T) {
// Both 1.2.3 and 1.2.4 map to 1.2.5. RFC 5280's tree would copy 1.2.5
// under each; RFC 9618's graph makes one node with two parents, which
// for GOVERNMENT is a shared subexpression.
g := mustProcess(t, path(
mapping(fakeCert("1.2.3", "1.2.4"), "1.2.3>1.2.5", "1.2.4>1.2.5"),
fakeCert("any"),
))
if got, want := levels(g), "any | 1.2.3 1.2.4 | 1.2.5"; got != want {
t.Fatalf("levels = %s, want %s", got, want)
}
child := g.Levels[2][0]
if len(child.Parents) != 2 || child.Parents[0] != g.Levels[1][0] || child.Parents[1] != g.Levels[1][1] {
t.Errorf("1.2.5 has %d parents, want both depth-1 nodes", len(child.Parents))
}
}
func TestMappingReplacesExpectedSet(t *testing.T) {
// Once 1.2.3 maps to 1.2.4, the next cert may no longer continue
// 1.2.3 itself: anyPolicy creates 1.2.4, not a copy of 1.2.3.
g := mustProcess(t, path(mapping(fakeCert("1.2.3"), "1.2.3>1.2.4"), fakeCert("any")))
if got, want := levels(g), "any | 1.2.3 | 1.2.4"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
wantPolicyError(t, "asserting the issuer's own policy after mapping it",
path(mapping(fakeCert("1.2.3"), "1.2.3>1.2.4"), fakeCert("1.2.3")), "no policy holds at depth 2")
}
func TestMappingsIgnoredWhereRFCIgnoresThem(t *testing.T) {
// Mappings in the last cert are never processed (step 29 will reject
// them), and a mapping from a policy with no node does nothing.
g := mustProcess(t, path(mapping(fakeCert("1.2.3"), "1.2.9>1.2.4"), mapping(fakeCert("any"), "1.2.3>1.2.4")))
if got, want := levels(g), "any | 1.2.3 | 1.2.3"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
}
func TestMappingUnderAnyPolicy(t *testing.T) {
// Cert 1 asserts only anyPolicy and maps 1.2.3 to 1.2.4. RFC 5280
// 6.1.4 (b)(1) then creates 1.2.3 under the root as if cert 1 had named
// it, expecting 1.2.4; the unused anyPolicy node is pruned.
g := mustProcess(t, path(mapping(fakeCert("any"), "1.2.3>1.2.4"), fakeCert("1.2.4")))
if got, want := levels(g), "any | 1.2.3 | 1.2.4"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
}
func TestAnyPolicyCannotBeMapped(t *testing.T) {
for _, pair := range []string{"any>1.2.4", "1.2.3>any"} {
wantPolicyError(t, pair, path(mapping(fakeCert("1.2.3"), pair), fakeCert("any")), "maps anyPolicy")
}
}