parent:
856d84d1b545be4936dea4e0fad4488a38d1e0ba
nmyk <nick@nmyk.io>
2026-11-14T21:46:12-05:00
add golden and stress tests, make some optimizations
diff --git a/DESIGN.md b/DESIGN.md
index c63ae17681c0df2df398b8e4d52898c70c9e8e4b..f10dd4bc60d98d7e8eba2c74cdbac1f8be07f12b 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -101,7 +101,7 @@ | 9 | One child OID mapped from several parents gets several parents (9618) or is duplicated (5280) | Shared subexpressions | The compiler **shares** identical lists at the same depth by default (`--no-share` keeps copies); the decoder and evaluator work on the DAG directly, never unfolding it |
**Strict check.** Plain RFC processing forgives mistakes by deleting code. After policy processing, the interpreter also verifies that certs 2..n assert `anyPolicy` and cert 1 does not, that every explicitly asserted policy in cert *i* is a node at depth *i*, that every mapping in cert *i* produced an edge, and that the program's distinct node ids are exactly 1..N with no gaps. A failure is a ratification error naming the orphaned OID. This turns silent data loss into a compile-style error.
-**Denial-of-service guard.** Policy processing has caused real DoS bugs, e.g. Go's CVE-2026-32281 (inefficient policy validation, fixed in Go 1.25.9 / 1.26.2). The graph form keeps work linear in mappings, and the interpreter caps nodes (`--max-nodes`, default 1,000,000).
+**Denial-of-service guard.** Policy processing has caused real DoS bugs, e.g. Go's CVE-2026-32281 (inefficient policy validation, fixed in Go 1.25.9 / 1.26.2). The graph form keeps work linear in mappings, and the interpreter caps nodes (`--max-nodes`, default 1,000,000), checking the cap as each node is made rather than after. A cap on nodes is not a cap on time, so each cert's work is also kept linear in its level: parents are indexed by the policies they expect, nodes by policy for mappings, and pruning stops at the first level where nothing died. The worst case the cap allows is cheap to reach: every atom above the bottom is copied down once per cert, so a 400 KB file with 6,000 top-level atoms beside one 200-deep form asks for 1.2 million nodes. It is refused in under two seconds.
## OID encoding
@@ -404,11 +404,11 @@
| Category | Form | Checks |
| --- | --- | --- |
| Language | `testdata/*.gov.lisp` | All semantics: arithmetic, comparisons, truthiness, `if`, `quote`, lists, `define`, closures, scoping, recursion, higher-order functions, strings and Unicode |
-| Shape stress | `testdata/*.gov.lisp` | 200-deep nesting (201 certs), 1,000-argument calls, wide `quote`d lists, shared subexpressions |
+| Shape stress | `stress_test.go`, generated | 200-deep nesting (201 certs), 1,000-argument calls, 1,000-element `quote`d lists, 1,000 top-level forms, all through the whole pipeline; `--max-nodes` exactly at and one below a program's graph size; a small file asking for a million-node graph, refused quickly. Shared subexpressions are in `lisp2gov/share_test.go` |
| Round trip | Go test over every `testdata` file | `gov2lisp(lisp2gov(src))` equals the normalized source, comments included |
| Ratification | Go table test that mutates a valid chain | Flipped signature byte, reordered certs, dropped mapping, orphaned policy, mapping in cert n, `anyPolicy` missing from a cert 2..n or asserted in cert 1, a list node with no elements, unknown critical extension; each expects a specific error |
| Civic | Go table test | Runs before, inside and after a `--not-before`/`--not-after` window; repeal of each depth; scheduled repeal; stale CRL; CRL signed by the wrong key; `--anchor` pin across a re-ratified version; notes over 200 characters; default-key warning |
-| Golden | One checked-in `.gov` | Byte equality, proving determinism |
+| Golden | `lisp2gov/testdata/golden.gov` and `golden.crl`, from `golden.gov.lisp` | Byte equality with every option fixed (seed, author, dates), so any change to the encoding fails, reported as the block and DER byte where it starts; the program uses every literal kind, a split note, a dotted pair and a shared node, and must also run. `go test ./lisp2gov -update` accepts a deliberate change |
| External oracle (optional) | `openssl verify -policy_check -explicit_policy -policy 2.5.29.32.0`, plus `-crl_check` | OpenSSL accepts every valid program, rejects broken ones, and agrees on repeals. Without `-policy 2.5.29.32.0` OpenSSL treats the acceptable set as empty |
Ratification tests are Go rather than Lisp because a Lisp source cannot express a broken certificate.
diff --git a/lisp2gov/golden_test.go b/lisp2gov/golden_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..5ee4a669bc4b1eb6c77b89827f7db0e56ce834ea
--- /dev/null
+++ b/lisp2gov/golden_test.go
@@ -0,0 +1,173 @@
+package lisp2gov
+
+import (
+ "bytes"
+ "encoding/pem"
+ "fmt"
+ "os"
+ "testing"
+ "time"
+
+ "government/eval"
+ "government/internal/lisptest"
+ "government/program"
+)
+
+// The golden program is built with every option fixed, so its bytes
+// depend on nothing but the compiler.
+var (
+ goldenSeed = [32]byte{'g', 'o', 'l', 'd', 'e', 'n'}
+ goldenOpts = Options{
+ Name: "golden",
+ Seed: &goldenSeed,
+ Author: "Ada Lovelace <ada@example.org>",
+ NotBefore: time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC),
+ NotAfter: time.Date(2037, 1, 1, 0, 0, 0, 0, time.UTC),
+ }
+ goldenRepeal = RepealOptions{
+ Seed: &goldenSeed,
+ Reason: 4, // superseded
+ At: time.Date(2030, 6, 1, 0, 0, 0, 0, time.UTC),
+ LastUpdate: time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC),
+ NextUpdate: time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC),
+ }
+)
+
+// TestGolden compiles testdata/golden.gov.lisp, which uses every part of
+// the encoding, and repeals it, and compares both results byte for byte
+// with the checked-in files. Anyone can rebuild them and get the same
+// bytes, so any difference is a change to the format: either a bug, or a
+// deliberate change to accept with go test ./lisp2gov -update.
+func TestGolden(t *testing.T) {
+ src, err := os.ReadFile("testdata/golden.gov.lisp")
+ if err != nil {
+ t.Fatal(err)
+ }
+ gov, err := Compile(string(src), goldenOpts)
+ if err != nil {
+ t.Fatal(err)
+ }
+ certs := parsePEM(t, gov)
+ crl, err := Repeal(certs, goldenRepeal)
+ if err != nil {
+ t.Fatal(err)
+ }
+ compareGolden(t, "testdata/golden.gov", gov)
+ compareGolden(t, "testdata/golden.crl", crl)
+
+ // The golden program must also be a working one, so the file stays
+ // worth inspecting.
+ if len(certs) < 4 {
+ t.Errorf("golden program has %d certs; it should be deep enough to have mappings", len(certs))
+ }
+ p, err := program.Load(gov, program.Options{At: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ f, err := lisptest.Parse("testdata/golden.gov.lisp", string(src))
+ if err != nil {
+ t.Fatal(err)
+ }
+ compiled, err := f.WithForms(p.Forms)
+ if err != nil {
+ t.Fatal(err)
+ }
+ lisptest.Run(t, compiled, eval.NewTopLevel())
+}
+
+func compareGolden(t *testing.T, path string, got []byte) {
+ t.Helper()
+ if *update {
+ if err := os.WriteFile(path, got, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ want, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("%v (run go test ./lisp2gov -update to create it)", err)
+ }
+ if diff := firstDifference(want, got); diff != "" {
+ t.Errorf("%s: %s (if the change is intended, run go test ./lisp2gov -update)", path, diff)
+ }
+}
+
+// firstDifference describes where two PEM files first differ: which block,
+// and which byte of its DER, so a failure points at a field rather than at
+// a line of base64.
+func firstDifference(want, got []byte) string {
+ if bytes.Equal(want, got) {
+ return ""
+ }
+ for block := 0; ; block++ {
+ w, wrest := pem.Decode(want)
+ g, grest := pem.Decode(got)
+ switch {
+ case w == nil && g == nil:
+ return "the PEM text differs outside its blocks"
+ case w == nil:
+ return fmt.Sprintf("block %d is new", block)
+ case g == nil:
+ return fmt.Sprintf("block %d is missing", block)
+ }
+ if w.Type != g.Type {
+ return fmt.Sprintf("block %d is a %s, want a %s", block, g.Type, w.Type)
+ }
+ if !bytes.Equal(w.Bytes, g.Bytes) {
+ i := 0
+ for i < len(w.Bytes) && i < len(g.Bytes) && w.Bytes[i] == g.Bytes[i] {
+ i++
+ }
+ return fmt.Sprintf("block %d (%s) differs from DER byte %d of %d (now %d bytes long)",
+ block, w.Type, i, len(w.Bytes), len(g.Bytes))
+ }
+ want, got = wrest, grest
+ }
+}
+
+// TestFirstDifference checks that a change to any one byte is caught and
+// placed: the golden comparison is only as good as this.
+func TestFirstDifference(t *testing.T) {
+ gov, err := Compile("(+ 1 (* 2 3))", Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if d := firstDifference(gov, gov); d != "" {
+ t.Errorf("identical files: %s", d)
+ }
+ blocks := pemBlocks(gov)
+ for b, der := range blocks {
+ for _, i := range []int{0, len(der) / 2, len(der) - 1} {
+ changed := make([][]byte, len(blocks))
+ copy(changed, blocks)
+ changed[b] = bytes.Clone(der)
+ changed[b][i] ^= 1
+ want := fmt.Sprintf("block %d (CERTIFICATE) differs from DER byte %d of", b, i)
+ if d := firstDifference(gov, encodeBlocks(changed)); !bytes.HasPrefix([]byte(d), []byte(want)) {
+ t.Errorf("flipping byte %d of block %d: %q", i, b, d)
+ }
+ }
+ }
+ if d := firstDifference(gov, encodeBlocks(blocks[:len(blocks)-1])); d != fmt.Sprintf("block %d is missing", len(blocks)-1) {
+ t.Errorf("dropped block: %q", d)
+ }
+}
+
+func pemBlocks(data []byte) [][]byte {
+ var out [][]byte
+ for {
+ var b *pem.Block
+ b, data = pem.Decode(data)
+ if b == nil {
+ return out
+ }
+ out = append(out, b.Bytes)
+ }
+}
+
+func encodeBlocks(blocks [][]byte) []byte {
+ var out []byte
+ for _, b := range blocks {
+ out = append(out, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: b})...)
+ }
+ return out
+}
diff --git a/lisp2gov/testdata/golden.crl b/lisp2gov/testdata/golden.crl
new file mode 100644
index 0000000000000000000000000000000000000000..f8a1c54c958a4a36ce252bc5041cc4b33608ebe5
--- /dev/null
+++ b/lisp2gov/testdata/golden.crl
@@ -0,0 +1,8 @@
+-----BEGIN X509 CRL-----
+MIH5MIGsAgEBMAUGAytlcDAoMQ8wDQYDVQQKEwZnb2xkZW4xFTATBgNVBAMTDEFk
+YSBMb3ZlbGFjZRcNMzAwMTAxMDAwMDAwWhgPOTk5OTEyMzEyMzU5NTlaMCIwIAIB
+AhcNMzAwNjAxMDAwMDAwWjAMMAoGA1UdFQQDCgEEoDIwMDAfBgNVHSMEGDAWgBT0
+x7zVfeMS3S9afdgx541mlTcELDANBgNVHRQEBgIEcNvYgDAFBgMrZXADQQDfVaO9
+v+yCQMVk2ZlS3P+mm1f5Lpkjs5mGdT0zvHmlFls1atUJMKXRMr+KX1558K7EUyRk
++jC9kaW/o386emMC
+-----END X509 CRL-----
diff --git a/lisp2gov/testdata/golden.gov b/lisp2gov/testdata/golden.gov
new file mode 100644
index 0000000000000000000000000000000000000000..dfa994fd44c9d233ec1fd532263dcf39cb9702a3
--- /dev/null
+++ b/lisp2gov/testdata/golden.gov
@@ -0,0 +1,165 @@
+-----BEGIN CERTIFICATE-----
+MIIBfjCCATCgAwIBAgIBATAFBgMrZXAwKDEPMA0GA1UEChMGZ29sZGVuMRUwEwYD
+VQQDEwxBZGEgTG92ZWxhY2UwHhcNMjcwMTAxMDAwMDAwWhcNMzcwMTAxMDAwMDAw
+WjAoMQ8wDQYDVQQKEwZnb2xkZW4xFTATBgNVBAMTDEFkYSBMb3ZlbGFjZTAqMAUG
+AytlcAMhALxY0aR9YK9CjAUuaKISxHa5X5TNPUKW2fxxcXS6T7Qbo38wfTAOBgNV
+HQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQU9Me81X3jEt0v
+Wn3YMeeNZpU3BCwwHwYDVR0jBBgwFoAU9Me81X3jEt0vWn3YMeeNZpU3BCwwGgYD
+VR0RBBMwEYEPYWRhQGV4YW1wbGUub3JnMAUGAytlcANBAGdhWiWy0kHTpfBh0X0I
+uOE/XXUHGk74IVgjapd6YJLxBTtJouCJ3BRuOfqQj2ezwGoUkdmpCPKwkeEb8CBI
+PgM=
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+MIIK0TCCCoOgAwIBAgIBAjAFBgMrZXAwKDEPMA0GA1UEChMGZ29sZGVuMRUwEwYD
+VQQDEwxBZGEgTG92ZWxhY2UwHhcNMjcwMTAxMDAwMDAwWhcNMzcwMTAxMDAwMDAw
+WjAuMQ8wDQYDVQQKEwZnb2xkZW4xGzAZBgNVBAMTEkdPVkVSTk1FTlQgRGVwdGgg
+MTAqMAUGAytlcAMhADQK9c3JHY3bGo7AlpRXQ4I7Kn/gxmmoUH6JYz+jjgiwo4IJ
+yjCCCcYwDgYDVR0PAQH/BAQDAgEG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Ph7ul8QQASAEGFmmOxcHuh7u6hfOG
+tODPh7ul8QQCSTEwMAYWaY7Fwe6Hu7qF84a04M+Hu6XxBABIAQYWaY7Fwe6Hu7qF
+84a04M+Hu6XxBAJKMjAwBhZpjsXB7oe7uoXzhrTgz4e7pfEEAEgBBhZpjsXB7oe7
+uoXzhrTgz4e7pfEEAkszMAUGAytlcANBAHxEAVuUDqiZKfwip1QpVVU56anbu5bp
+0pZQuhVttXuRKw3cph2oKidEwDBTVdKAy6htMDHbZiU66F0MAh0m1Qk=
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+MIIBfjCCATCgAwIBAgIBBjAFBgMrZXAwLjEPMA0GA1UEChMGZ29sZGVuMRswGQYD
+VQQDExJHT1ZFUk5NRU5UIERlcHRoIDQwHhcNMjcwMTAxMDAwMDAwWhcNMzcwMTAx
+MDAwMDAwWjAuMQ8wDQYDVQQKEwZnb2xkZW4xGzAZBgNVBAMTEkdPVkVSTk1FTlQg
+RGVwdGggNTAqMAUGAytlcAMhACUi4btq4Ba8yZHRU7irPuB9a6vmAVDSMBxaaSHT
+HrISo3MwcTAOBgNVHQ8BAf8EBAMCB4AwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQU
+L+baPI+I+fxP+aJ5EGZnWIyhhsswHwYDVR0jBBgwFoAUpK4/FyOQ3NS6gRbY1+k2
+7D+1XFEwEQYDVR0gBAowCDAGBgRVHSAAMAUGAytlcANBAD9jNBPOmQFG+asyQu5S
+0fS5TncYHRjyr9E2QVA2SlDeg0RfYrAC6HB9VsGdcpjt9h6TPWHrgiMnl6XTue5u
+8gk=
+-----END CERTIFICATE-----
diff --git a/lisp2gov/testdata/golden.gov.lisp b/lisp2gov/testdata/golden.gov.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..0f108d65a3037619ef132dddcd44d274e009e210
--- /dev/null
+++ b/lisp2gov/testdata/golden.gov.lisp
@@ -0,0 +1,24 @@
+; golden.gov.lisp: one program that uses every part of the encoding, so a
+; change to any byte the compiler writes shows up as a diff of golden.gov.
+; This note is long on purpose: past 200 characters it no longer fits one
+; RFC 5280 explicitText, and has to be split across several user notices,
+; then joined back in order when the file is decoded.
+
+(define square (lambda (n) (* n n))) ; a builtin, a symbol, a lambda
+(square 12) ;=> 144
+
+; Every literal kind: integers, floats, strings with escapes and code
+; points outside ASCII, booleans and the empty list.
+'(-42 0 3.25 -0.5 1.0e10 "tab\there" "\u{1F3DB} capitol" "" T F ()) ;=> (-42 0 3.25 -0.5 10000000000.0 "tab\there" "🏛 capitol" "" T F ())
+
+; A dotted pair, and a dot followed by a list, which reads as a longer list.
+'(a . b) ;=> (a . b)
+'(a . (b c)) ;=> (a b c)
+
+; (square (+ 1 2)) appears twice at the same depth, so it is one shared node.
+(+ (square (+ 1 2))
+ (- (square (+ 1 2)) 1)) ;=> 17
+
+(if (< 1 2) "yes" "no") ;=> "yes"
+(car (cdr '(1 2 3))) ;=> 2
+(strcat "GOVERN" "MENT") ;=> "GOVERNMENT"
diff --git a/policy/graph.go b/policy/graph.go
index feee6a7ceb5d72a1a83a6ca6c01c927f0b809946..e4d92a06cd4c4597c57606cdfd8b0f83dd8518bc 100644
--- a/policy/graph.go
+++ b/policy/graph.go
@@ -41,10 +41,6 @@ // it named the policy itself. Nodes made by anyPolicy have none.
Note string
}
-func (n *Node) expects(p x509.OID) bool {
- return slices.ContainsFunc(n.Expected, p.Equal)
-}
-
// Graph is the valid policy graph after the whole path. Levels[0] holds the
// root, an anyPolicy node; Levels[i] holds the nodes made by cert i.
type Graph struct {
@@ -74,7 +70,7 @@ }
func (g *Graph) spend(depth int) error {
if g.budget == 0 {
- return errorf(ErrTooLarge, "the policy graph needs more than its node budget at depth %d (see -max-nodes)", depth)
+ return errorf(ErrTooLarge, "the policy graph needs more than its node budget at depth %d (see --max-nodes)", depth)
}
g.budget--
return nil
@@ -135,15 +131,17 @@ level = append(level, n)
byPolicy[key(p)] = n
return nil
}
- expecting := func(p x509.OID) []*Node {
- var from []*Node
- for _, parent := range parents {
- if parent.expects(p) {
- from = append(from, parent)
- }
+ // Which parents expect each policy, built once: looking each policy up
+ // by scanning every parent would make a wide level cost its width
+ // squared, a denial of service the node budget alone does not stop.
+ expectedBy := map[string][]*Node{}
+ for _, parent := range parents {
+ for _, p := range parent.Expected {
+ k := key(p)
+ expectedBy[k] = append(expectedBy[k], parent)
}
- return from
}
+ expecting := func(p x509.OID) []*Node { return expectedBy[key(p)] }
// (d)(1): each policy the cert names goes under every node that
// expects it, or failing that under an anyPolicy node. A policy with
@@ -199,20 +197,36 @@ }
// prune is (d)(3): delete every node at depth i-1 or above that has no
// children, repeatedly, since each deletion can leave its parents
-// childless. Going up one level at a time does it in one pass.
+// childless. Going up one level at a time does it in one pass, and the
+// pass stops at the first level where nothing died: after the previous
+// cert's prune only the newest level can hold childless nodes, so the
+// levels above it are untouched unless a deletion reaches them. Each
+// affected parent's children are filtered once, so the cost is the number
+// of edges removed, not deletions times siblings or certs times levels.
func (g *Graph) prune(i int) {
for d := i - 1; d >= 0; d-- {
kept := g.Levels[d][:0]
+ dead := map[*Node]bool{}
for _, n := range g.Levels[d] {
if len(n.Children) > 0 {
kept = append(kept, n)
- continue
+ } else {
+ dead[n] = true
}
+ }
+ g.Levels[d] = kept
+ if len(dead) == 0 {
+ return
+ }
+ done := map[*Node]bool{}
+ for n := range dead {
for _, parent := range n.Parents {
- parent.Children = slices.DeleteFunc(parent.Children, func(c *Node) bool { return c == n })
+ if !done[parent] {
+ done[parent] = true
+ parent.Children = slices.DeleteFunc(parent.Children, func(c *Node) bool { return dead[c] })
+ }
}
}
- g.Levels[d] = kept
}
}
@@ -254,15 +268,17 @@ }
}
level := g.Levels[i]
+ byPolicy := map[string][]*Node{}
+ for _, n := range level {
+ k := key(n.Policy)
+ byPolicy[k] = append(byPolicy[k], n)
+ }
for _, p := range issuers {
- found := false
- for _, n := range level {
- if n.Policy.Equal(p) {
- n.Expected = mapped[key(p)]
- found = true
- }
+ nodes := byPolicy[key(p)]
+ for _, n := range nodes {
+ n.Expected = mapped[key(p)]
}
- if found {
+ if len(nodes) > 0 {
continue
}
// (b)(1), second half: with no node for P but an anyPolicy node at
diff --git a/stress_test.go b/stress_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..d591e46ad8718f9fdb51eb14e1e7c7bc27115b80
--- /dev/null
+++ b/stress_test.go
@@ -0,0 +1,151 @@
+package government_test
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "reflect"
+ "strings"
+ "testing"
+ "time"
+
+ "government/lisp2gov"
+ "government/policy"
+ "government/program"
+ "government/ratify"
+ "government/sexpr"
+)
+
+// deep nests (+ 1 ...) until the program is depth certs deep: each list
+// puts its elements one cert lower, and the innermost 0 sits at the bottom.
+func deep(depth int) string {
+ return strings.Repeat("(+ 1 ", depth-1) + "0" + strings.Repeat(")", depth-1)
+}
+
+func numbers(n int) string {
+ var b strings.Builder
+ for i := 1; i <= n; i++ {
+ fmt.Fprintf(&b, " %d", i)
+ }
+ return b.String()
+}
+
+// stress runs src through everything: compile, ratify, policy processing,
+// decode, evaluate. It checks the decoded forms are the source's and
+// returns the program and what it printed.
+func stress(t *testing.T, src string, opts program.Options) (*program.Program, string, []byte) {
+ t.Helper()
+ data, err := lisp2gov.Compile(src, lisp2gov.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ opts.At = time.Now()
+ p, err := program.Load(data, opts)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want, err := sexpr.ReadAll(src)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(p.Forms, want) {
+ t.Error("the program decodes to different forms")
+ }
+ var out bytes.Buffer
+ if err := p.Run(&out); err != nil {
+ t.Fatal(p.Describe(err))
+ }
+ return p, out.String(), data
+}
+
+func TestStressDeep(t *testing.T) {
+ start := time.Now()
+ p, out, data := stress(t, deep(200), program.Options{})
+ if p.Depth() != 200 || len(p.Certs) != 201 {
+ t.Errorf("depth %d in %d certs, want 200 in 201", p.Depth(), len(p.Certs))
+ }
+ if out != "199\n" {
+ t.Errorf("printed %q", out)
+ }
+ t.Logf("200 deep: %d bytes, %d nodes, %v", len(data), p.Nodes, time.Since(start))
+}
+
+func TestStressWide(t *testing.T) {
+ tests := []struct {
+ name, src, want string
+ }{
+ {"1,000 arguments", "(+" + numbers(1000) + ")", "500500\n"},
+ {"1,000-element quoted list", "(car (cdr '(" + numbers(1000) + ")))", "2\n"},
+ {"1,000 top-level forms", numbers(1000), strings.ReplaceAll(strings.TrimSpace(numbers(1000)), " ", "\n") + "\n"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ start := time.Now()
+ p, out, data := stress(t, tt.src, program.Options{})
+ if out != tt.want {
+ t.Errorf("printed %.40q...", out)
+ }
+ t.Logf("%d bytes, %d nodes, depth %d, %v", len(data), p.Nodes, p.Depth(), time.Since(start))
+ })
+ }
+}
+
+// graphSize is how many nodes policy processing makes for a program,
+// counting the validator's copies: the number --max-nodes limits.
+func graphSize(t *testing.T, data []byte) int {
+ t.Helper()
+ certs, err := ratify.Ratify(data, ratify.Options{At: time.Now()})
+ if err != nil {
+ t.Fatal(err)
+ }
+ g, err := policy.Program(certs, policy.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ n := 0
+ for _, level := range g.Levels {
+ n += len(level)
+ }
+ return n - 1 // the anyPolicy root is not made under the budget
+}
+
+// TestMaxNodesIsExact: a budget of exactly the graph's size is enough, and
+// one less is refused.
+func TestMaxNodesIsExact(t *testing.T) {
+ // Not "42": its graph is one node, and a budget of 0 means the default.
+ for _, src := range []string{"(+ 1 (* 2 3))", deep(50), "(+" + numbers(100) + ")"} {
+ data, err := lisp2gov.Compile(src, lisp2gov.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ n := graphSize(t, data)
+ if _, err := program.Load(data, program.Options{At: time.Now(), MaxNodes: n}); err != nil {
+ t.Errorf("%.20s: budget %d refused: %v", src, n, err)
+ }
+ _, err = program.Load(data, program.Options{At: time.Now(), MaxNodes: n - 1})
+ var pe *policy.Error
+ if !errors.As(err, &pe) || pe.Code != policy.ErrTooLarge {
+ t.Errorf("%.20s: budget %d of %d: %v, want E-TOO-LARGE", src, n-1, n, err)
+ }
+ }
+}
+
+// TestSmallFileLargeGraph is the attack the budget is for: every atom
+// above the bottom is copied down once per cert, so a few thousand
+// top-level atoms beside one deep form ask for millions of graph nodes
+// from a file of a few hundred kilobytes. The default budget must refuse
+// it, and quickly, without building the graph first.
+func TestSmallFileLargeGraph(t *testing.T) {
+ src := numbers(6000) + "\n" + deep(200)
+ data, err := lisp2gov.Compile(src, lisp2gov.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ start := time.Now()
+ _, err = program.Load(data, program.Options{At: time.Now()})
+ var pe *policy.Error
+ if !errors.As(err, &pe) || pe.Code != policy.ErrTooLarge {
+ t.Fatalf("%v, want E-TOO-LARGE", err)
+ }
+ t.Logf("%d-byte file asking for about %d nodes refused in %v: %v", len(data), 6000*200, time.Since(start), err)
+}