@ 18e20f574cf1ffbd092fda0b63e7b07430be77dc | history
package policy
import (
"crypto/x509"
"crypto/x509/pkix"
"errors"
"strings"
"testing"
"time"
"government/lisp2gov"
"government/ratify"
)
// fakeCert carries only what policy processing reads. Signatures and names
// are ratify's job, so these certs need neither.
func fakeCert(policies ...string) *x509.Certificate {
c := &x509.Certificate{Extensions: []pkix.Extension{{Id: oidCertificatePolicies}}}
for _, p := range policies {
if p == "any" {
p = "2.5.29.32.0"
}
c.Policies = append(c.Policies, mustOID(p))
}
return c
}
// path prepends a stand-in anchor, which Process skips.
func path(certs ...*x509.Certificate) []*x509.Certificate {
return append([]*x509.Certificate{{}}, certs...)
}
// levels prints each depth's policies, e.g. "any | 1.2.3 1.2.4 | 1.2.3".
func levels(g *Graph) string {
var parts []string
for _, level := range g.Levels {
var names []string
for _, n := range level {
name := n.Policy.String()
if n.Policy.Equal(AnyPolicy) {
name = "any"
}
names = append(names, name)
}
parts = append(parts, strings.Join(names, " "))
}
return strings.Join(parts, " | ")
}
func mustProcess(t *testing.T, certs []*x509.Certificate) *Graph {
t.Helper()
g, err := Process(certs, Options{})
if err != nil {
t.Fatal(err)
}
return g
}
func wantPolicyError(t *testing.T, name string, certs []*x509.Certificate, msg string) {
t.Helper()
_, err := Process(certs, Options{})
var e *Error
if !errors.As(err, &e) || e.Code != ErrPolicy || !strings.Contains(e.Msg, msg) {
t.Errorf("%s: error = %v, want %s containing %q", name, err, ErrPolicy, msg)
}
}
func TestSamePolicyHolds(t *testing.T) {
g := mustProcess(t, path(fakeCert("1.2.3"), fakeCert("1.2.3"), fakeCert("1.2.3")))
if got, want := levels(g), "any | 1.2.3 | 1.2.3 | 1.2.3"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
for d := 1; d <= 3; d++ {
n := g.Levels[d][0]
if len(n.Parents) != 1 || n.Parents[0] != g.Levels[d-1][0] {
t.Errorf("depth %d: parent is not the node above", d)
}
}
}
func TestDroppedPolicyFails(t *testing.T) {
wantPolicyError(t, "a cert asserts something else", path(fakeCert("1.2.3"), fakeCert("1.2.4"), fakeCert("1.2.3")),
"no policy holds at depth 2")
noExtension := &x509.Certificate{}
wantPolicyError(t, "a cert has no certificatePolicies", path(fakeCert("1.2.3"), noExtension),
"cert 2 has no certificatePolicies")
}
func TestPruneDeadEndBranch(t *testing.T) {
// 1.2.4 holds at depth 1 but nothing continues it at depth 2, so it is
// pruned: it does not hold along the whole path.
g := mustProcess(t, path(fakeCert("1.2.3", "1.2.4"), fakeCert("1.2.3"), fakeCert("1.2.3")))
if got, want := levels(g), "any | 1.2.3 | 1.2.3 | 1.2.3"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
if n := len(g.Levels[0][0].Children); n != 1 {
t.Errorf("root has %d children after pruning, want 1", n)
}
}
func TestPruningCascades(t *testing.T) {
// 1.2.4 survives cert 2 but not cert 3; its node at depth 2 goes, and
// then its now childless node at depth 1.
g := mustProcess(t, path(fakeCert("1.2.3", "1.2.4"), fakeCert("1.2.3", "1.2.4"), fakeCert("1.2.3")))
if got, want := levels(g), "any | 1.2.3 | 1.2.3 | 1.2.3"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
}
// TestAnyPolicyCopiesAtoms are the design-phase prototype's copyonly and
// nocopy chains: an atom listed only in cert 1 reaches depth 3 when certs 2
// and 3 assert anyPolicy, and is pruned away when they do not.
func TestAnyPolicyCopiesAtoms(t *testing.T) {
g := mustProcess(t, path(fakeCert("1.2.3"), fakeCert("any"), fakeCert("any")))
if got, want := levels(g), "any | 1.2.3 | 1.2.3 | 1.2.3"; got != want {
t.Errorf("copyonly: levels = %s, want %s", got, want)
}
wantPolicyError(t, "nocopy", path(fakeCert("1.2.3"), fakeCert("1.2.9"), fakeCert("1.2.9")), "no policy holds at depth 2")
}
func TestAnyPolicyAtDepthOneAdoptsAnything(t *testing.T) {
// In real PKI an intermediate asserting anyPolicy accepts whatever the
// next cert names: the policy has no node expecting it, so it goes
// under the anyPolicy node.
g := mustProcess(t, path(fakeCert("any"), fakeCert("1.2.3")))
if got, want := levels(g), "any | any | 1.2.3"; got != want {
t.Errorf("levels = %s, want %s", got, want)
}
}
var ratifyTime = time.Date(2026, 11, 14, 0, 0, 0, 0, time.UTC)
func TestCompiledDepthOnePrograms(t *testing.T) {
src := `1 "two" 3.0 x`
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)
}
g := mustProcess(t, certs)
if g.Depth() != 1 || len(g.Levels[1]) != 4 {
t.Fatalf("levels = %s", levels(g))
}
for i, n := range g.Levels[1] {
if !n.Policy.Equal(certs[1].Policies[i]) {
t.Errorf("node %d is %s, want %s", i, n.Policy, certs[1].Policies[i])
}
}
}