@ bf971275d54b7235006309f4e84fd06723f4f70e | history
package ratify
import (
"bytes"
"crypto/x509"
"encoding/asn1"
"errors"
"strings"
"testing"
"time"
)
func date(s string) time.Time {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return t
}
// windowed builds a chain where every cert is valid for 2027, except the
// depths in overrides, which get their own window.
func windowed(t *testing.T, overrides map[int][2]string) []*x509.Certificate {
return testChain(t, 3, func(d int, tmpl *x509.Certificate) {
tmpl.NotBefore, tmpl.NotAfter = date("2027-01-01T00:00:00Z"), date("2027-12-31T00:00:00Z")
if w, ok := overrides[d]; ok {
tmpl.NotBefore, tmpl.NotAfter = date(w[0]), date(w[1])
}
})
}
func TestValidityWindow(t *testing.T) {
chain := windowed(t, nil)
tests := []struct {
at string
code string
msg string
}{
{"2027-06-01T00:00:00Z", "", ""},
{"2027-01-01T00:00:00Z", "", ""}, // both ends are inside the window
{"2027-12-31T00:00:00Z", "", ""},
{"2026-12-31T23:59:59Z", ErrNotInForce, "takes effect 2027-01-01T00:00:00Z"},
{"2027-12-31T00:00:01Z", ErrLapsed, "expired 2027-12-31T00:00:00Z"},
}
for _, tt := range tests {
checkCode(t, "at "+tt.at, CheckValidity(chain, date(tt.at)), tt.code, tt.msg)
}
}
func TestValidityIsTheOverlap(t *testing.T) {
// Depth 2 starts late and depth 1 ends early, so the program is in
// force only from March to September.
chain := windowed(t, map[int][2]string{
1: {"2027-01-01T00:00:00Z", "2027-09-30T00:00:00Z"},
2: {"2027-03-01T00:00:00Z", "2027-12-31T00:00:00Z"},
})
checkCode(t, "February", CheckValidity(chain, date("2027-02-01T00:00:00Z")), ErrNotInForce, "takes effect 2027-03-01T00:00:00Z (depth 2)")
checkCode(t, "June", CheckValidity(chain, date("2027-06-01T00:00:00Z")), "", "")
checkCode(t, "October", CheckValidity(chain, date("2027-10-01T00:00:00Z")), ErrLapsed, "expired 2027-09-30T00:00:00Z (depth 1)")
}
func TestValidityNeverInForce(t *testing.T) {
chain := windowed(t, map[int][2]string{
1: {"2027-01-01T00:00:00Z", "2027-03-01T00:00:00Z"},
3: {"2027-06-01T00:00:00Z", "2027-12-31T00:00:00Z"},
})
checkCode(t, "disjoint windows", CheckValidity(chain, date("2027-04-01T00:00:00Z")), ErrLapsed, "never in force: depth 1 expires 2027-03-01T00:00:00Z, before depth 3 takes effect")
}
// TestTimeEncodings shows RFC 5280's two time formats: UTCTime (two-digit
// year) up to 2049, GeneralizedTime (four-digit year) from 2050. That is
// why a 9999-12-31 expiry can be written at all.
func TestTimeEncodings(t *testing.T) {
tests := []struct {
notAfter string
tag byte
}{
{"2049-12-31T23:59:59Z", asn1.TagUTCTime},
{"2050-01-01T00:00:00Z", asn1.TagGeneralizedTime},
{"9999-12-31T23:59:59Z", asn1.TagGeneralizedTime},
}
for _, tt := range tests {
chain := windowed(t, map[int][2]string{1: {"2027-01-01T00:00:00Z", tt.notAfter}})
c := chain[1]
if !c.NotAfter.Equal(date(tt.notAfter)) {
t.Errorf("%s: parsed back as %s", tt.notAfter, c.NotAfter)
}
// Validity ::= SEQUENCE { notBefore Time, notAfter Time }; find it
// by its encoded notBefore, a UTCTime for 2027.
notBefore := []byte("270101000000Z")
i := bytes.Index(c.RawTBSCertificate, notBefore)
if i < 0 {
t.Fatal("notBefore not found")
}
if got := c.RawTBSCertificate[i+len(notBefore)]; got != byte(tt.tag) {
t.Errorf("%s: notAfter tag %d, want %d", tt.notAfter, got, tt.tag)
}
}
if err := CheckValidity(windowed(t, map[int][2]string{1: {"2027-01-01T00:00:00Z", "9999-12-31T23:59:59Z"}}), date("2027-06-01T00:00:00Z")); err != nil {
t.Error(err)
}
}
func checkCode(t *testing.T, name string, err error, code, msg string) {
t.Helper()
if code == "" {
if err != nil {
t.Errorf("%s: unexpected %v", name, err)
}
return
}
var e *Error
if !errors.As(err, &e) || e.Code != code || !strings.Contains(e.Msg, msg) {
t.Errorf("%s: error = %v, want %s containing %q", name, err, code, msg)
}
}