@ 37b1f5914992b1c2410e634b80cb352b72e7f793 | history
package lisp2gov
import (
"bytes"
"errors"
"os"
"path/filepath"
"testing"
"government/sexpr"
)
func TestCompileTestdata(t *testing.T) {
files, err := filepath.Glob("../testdata/*.gov.lisp")
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)
}
}
}