@ c491ab51ca7fcb046a61286346455e3aaca25ae3 | history
package lisp2gov
import (
"bytes"
"encoding/pem"
"errors"
"os"
"path/filepath"
"testing"
"government/internal/lisptest"
"government/sexpr"
)
func TestCompileTestdata(t *testing.T) {
files, err := lisptest.Files()
if err != nil || len(files) == 0 {
t.Fatalf("no testdata: %v", err)
}
for _, file := range files {
src, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
a, err := Compile(string(src), Options{Name: filepath.Base(file)})
if err != nil {
t.Errorf("%s: %v", file, err)
continue
}
b, _ := Compile(string(src), Options{Name: filepath.Base(file)})
if !bytes.Equal(a, b) {
t.Errorf("%s: two compiles differ", file)
}
certs := parsePEM(t, a)
for i := 1; i < len(certs); i++ {
if err := certs[i].CheckSignatureFrom(certs[i-1]); err != nil {
t.Errorf("%s: cert %d: %v", file, i, err)
}
}
}
}
func TestCompileDepth(t *testing.T) {
for _, tt := range []struct {
src string
certs int
}{
{"42", 2},
{"(+ 1 2)", 3},
{"(+ 1 (* 2 3))", 4},
{"1 (a (b (c)))", 5},
} {
out, err := Compile(tt.src, Options{})
if err != nil {
t.Fatal(err)
}
if got := len(parsePEM(t, out)); got != tt.certs {
t.Errorf("%s: %d certs, want %d", tt.src, got, tt.certs)
}
}
}
func TestCompileOptions(t *testing.T) {
a, _ := Compile("(+ 1 2)", Options{Name: "a"})
b, _ := Compile("(+ 1 2)", Options{Name: "b"})
if bytes.Equal(a, b) {
t.Error("name has no effect")
}
if got := parsePEM(t, b)[2].Subject.Organization[0]; got != "b" {
t.Errorf("O = %q, want b", got)
}
seed := [32]byte{7}
c, _ := Compile("(+ 1 2)", Options{Name: "a", Seed: &seed})
if bytes.Equal(a, c) {
t.Error("seed has no effect")
}
if err := parsePEM(t, c)[1].CheckSignatureFrom(parsePEM(t, a)[0]); err == nil {
t.Error("a cert from one seed verifies under another seed's anchor")
}
}
func TestCompileHeaderCannotInjectBlocks(t *testing.T) {
src := "; -----BEGIN CERTIFICATE-----\n; AAAA\n; -----END CERTIFICATE-----\n\"\n-----BEGIN CERTIFICATE-----\n\"\n"
out, err := Compile(src, Options{IncludeSource: true})
if err != nil {
t.Fatal(err)
}
// The program is one string atom: anchor plus one cert, nothing more.
if got := len(parsePEM(t, out)); got != 2 {
t.Errorf("%d PEM blocks, want 2", got)
}
}
func TestCompileErrors(t *testing.T) {
_, err := Compile("(+ 1", Options{})
var se *sexpr.SyntaxError
if !errors.As(err, &se) {
t.Errorf("unfinished form: error %v is not a *sexpr.SyntaxError", err)
}
if _, err := Compile("; only a comment\n", Options{}); err == nil {
t.Error("a program with no forms compiled")
}
}
func TestCompileSourceIsOptIn(t *testing.T) {
src := "; secret plan\n(+ 1 2)\n"
without, err := Compile(src, Options{Name: "p"})
if err != nil {
t.Fatal(err)
}
if bytes.Contains(without, []byte("secret plan")) || bytes.Contains(without, []byte("(+ 1 2)")) {
t.Errorf("source in output by default:\n%.80s", without)
}
with, err := Compile(src, Options{Name: "p", IncludeSource: true})
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(with, []byte(" ; secret plan\n (+ 1 2)\n")) {
t.Errorf("source missing with IncludeSource:\n%.120s", with)
}
// The header is the only difference; the certs are the same.
a, b := parsePEM(t, without), parsePEM(t, with)
for i := range a {
if !bytes.Equal(a[i].Raw, b[i].Raw) {
t.Errorf("cert %d differs", i)
}
}
}
func TestNoComments(t *testing.T) {
src := "; the answer, as a sum\n(+ 40 ; most of it\n 2)\n"
with, err := Compile(src, Options{})
if err != nil {
t.Fatal(err)
}
without, err := Compile(src, Options{NoComments: true})
if err != nil {
t.Fatal(err)
}
for _, c := range []string{"the answer, as a sum", "most of it"} {
if !bytes.Contains(blocksDER(t, with), []byte(c)) {
t.Errorf("%q missing from the normal build", c)
}
if bytes.Contains(blocksDER(t, without), []byte(c)) {
t.Errorf("%q still in the --no-comments build", c)
}
}
uncommented, _ := Compile("(+ 40\n 2)\n", Options{})
if !bytes.Equal(without, uncommented) {
t.Error("dropping comments should give the same bytes as a source without them")
}
if _, err := Compile(src, Options{NoComments: true, IncludeSource: true}); err == nil {
t.Error("NoComments with IncludeSource was accepted")
}
}
// blocksDER joins the DER of every PEM block, where the notes live as text.
func blocksDER(t *testing.T, data []byte) []byte {
t.Helper()
var out []byte
for {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
return out
}
out = append(out, block.Bytes...)
}
}