@ 7eeb8a0214368f4491f26f4cf37bd7bf6dc67cd3 | history
package lisp2gov
import (
"crypto/x509"
"encoding/pem"
"strings"
"testing"
"time"
)
var (
issued = time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC)
never = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
)
func parseCRL(t *testing.T, data []byte) *x509.RevocationList {
t.Helper()
block, _ := pem.Decode(data)
if block == nil || block.Type != "X509 CRL" {
t.Fatalf("not an X509 CRL PEM block: %.40q", data)
}
crl, err := x509.ParseRevocationList(block.Bytes)
if err != nil {
t.Fatal(err)
}
return crl
}
func TestRepealEachDepth(t *testing.T) {
out, err := Compile("(+ 1 (* 2 3))", Options{})
if err != nil {
t.Fatal(err)
}
certs := parsePEM(t, out)
for depth := 1; depth < len(certs); depth++ {
data, err := Repeal(certs, depth, RepealOptions{Reason: Reasons["superseded"], At: issued, Issued: issued, NextUpdate: never})
if err != nil {
t.Fatalf("depth %d: %v", depth, err)
}
crl := parseCRL(t, data)
// The issuer of the repealed cert signs the CRL, not the anchor.
if err := crl.CheckSignatureFrom(certs[depth-1]); err != nil {
t.Errorf("depth %d: CRL does not verify against its issuer: %v", depth, err)
}
if depth > 1 {
if err := crl.CheckSignatureFrom(certs[0]); err == nil {
t.Errorf("depth %d: CRL verifies against the anchor too", depth)
}
}
if len(crl.RevokedCertificateEntries) != 1 {
t.Fatalf("depth %d: %d entries", depth, len(crl.RevokedCertificateEntries))
}
e := crl.RevokedCertificateEntries[0]
if e.SerialNumber.Cmp(certs[depth].SerialNumber) != 0 || e.ReasonCode != 4 || !e.RevocationTime.Equal(issued) {
t.Errorf("depth %d: entry serial %v reason %d time %v", depth, e.SerialNumber, e.ReasonCode, e.RevocationTime)
}
if !crl.ThisUpdate.Equal(issued) || !crl.NextUpdate.Equal(never) {
t.Errorf("depth %d: thisUpdate %v nextUpdate %v", depth, crl.ThisUpdate, crl.NextUpdate)
}
}
}
func TestRepealIsDeterministic(t *testing.T) {
out, _ := Compile("(+ 1 2)", Options{})
certs := parsePEM(t, out)
opts := RepealOptions{At: issued, Issued: issued, NextUpdate: never}
a, _ := Repeal(certs, 2, opts)
b, _ := Repeal(certs, 2, opts)
if string(a) != string(b) {
t.Error("two repeals with the same inputs differ")
}
}
func TestRepealRefusals(t *testing.T) {
out, _ := Compile("(+ 1 2)", Options{})
certs := parsePEM(t, out)
other := [32]byte{4}
ok := RepealOptions{At: issued, Issued: issued, NextUpdate: never}
tests := []struct {
name string
depth int
opts RepealOptions
want string
}{
{"depth 0 is the anchor", 0, ok, "depths 1 to 2"},
{"depth past the bottom", 3, ok, "depths 1 to 2"},
{"someone else's key", 1, RepealOptions{Seed: &other, At: issued, Issued: issued, NextUpdate: never}, "did not compile"},
{"stale before it is issued", 1, RepealOptions{At: issued, Issued: issued, NextUpdate: issued}, "go stale"},
}
for _, tt := range tests {
_, err := Repeal(certs, tt.depth, tt.opts)
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Errorf("%s: %v, want %q", tt.name, err, tt.want)
}
}
}
func TestReasonNames(t *testing.T) {
names := ReasonNames()
if names[0] != "unspecified" || names[len(names)-1] != "aACompromise" || len(names) != 10 {
t.Errorf("ReasonNames = %v", names)
}
}