1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
package x509util
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"testing"
)
func TestCreateCertificateRequest(t *testing.T) {
r := rand.Reader
priv, err := rsa.GenerateKey(r, 1024)
if err != nil {
t.Fatal(err)
}
template := CertificateRequest{
CertificateRequest: x509.CertificateRequest{
Subject: pkix.Name{
CommonName: "test.acme.co",
Country: []string{"US"},
},
},
ChallengePassword: "foobar",
}
derBytes, err := CreateCertificateRequest(r, &template, priv)
if err != nil {
t.Fatal(err)
}
out, err := x509.ParseCertificateRequest(derBytes)
if err != nil {
t.Fatalf("failed to create certificate request: %s", err)
}
if err := out.CheckSignature(); err != nil {
t.Errorf("failed to check certificate request signature: %s", err)
}
challenge, err := ParseChallengePassword(derBytes)
if err != nil {
t.Fatalf("failed to parse challengePassword attribute: %s", err)
}
if have, want := challenge, template.ChallengePassword; have != want {
t.Errorf("have %s, want %s", have, want)
}
}
|