-
Notifications
You must be signed in to change notification settings - Fork 36
/
certinfo_test.go
85 lines (76 loc) · 2.25 KB
/
certinfo_test.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package certinfo
import (
"bytes"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"testing"
)
type InputType int
const (
tCertificate InputType = iota
tCertificateRequest
)
// Compares a PEM-encoded certificate to a refernce file.
func testPair(t *testing.T, certFile, refFile string, inputType InputType) {
// Read and parse the certificate
pemData, err := ioutil.ReadFile(certFile)
if err != nil {
t.Fatal(err)
}
block, rest := pem.Decode([]byte(pemData))
if block == nil || len(rest) > 0 {
t.Fatal("Certificate decoding error")
}
var result string
switch inputType {
case tCertificate:
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
t.Fatal(err)
}
result, err = CertificateText(cert)
if err != nil {
t.Fatal(err)
}
case tCertificateRequest:
cert, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
t.Fatal(err)
}
result, err = CertificateRequestText(cert)
if err != nil {
t.Fatal(err)
}
}
resultData := []byte(result)
// Read the reference output
refData, err := ioutil.ReadFile(refFile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(resultData, refData) {
t.Logf("'%s' did not match reference '%s'\n", certFile, refFile)
t.Errorf("Dump follows:\n%s\n", result)
}
}
// Test the root CA certificate
func TestCertInfoRoot(t *testing.T) {
testPair(t, "test_certs/root1.cert.pem", "test_certs/root1.cert.text", tCertificate)
testPair(t, "test_certs/root1.csr.pem", "test_certs/root1.csr.text", tCertificateRequest)
}
// Test the leaf (user) RSA certificate
func TestCertInfoLeaf1(t *testing.T) {
testPair(t, "test_certs/leaf1.cert.pem", "test_certs/leaf1.cert.text", tCertificate)
testPair(t, "test_certs/leaf1.csr.pem", "test_certs/leaf1.csr.text", tCertificateRequest)
}
// Test the leaf (user) DSA certificate
func TestCertInfoLeaf2(t *testing.T) {
testPair(t, "test_certs/leaf2.cert.pem", "test_certs/leaf2.cert.text", tCertificate)
testPair(t, "test_certs/leaf2.csr.pem", "test_certs/leaf2.csr.text", tCertificateRequest)
}
// Test the leaf (user) ECDSA certificate
func TestCertInfoLeaf3(t *testing.T) {
testPair(t, "test_certs/leaf3.cert.pem", "test_certs/leaf3.cert.text", tCertificate)
testPair(t, "test_certs/leaf3.csr.pem", "test_certs/leaf3.csr.text", tCertificateRequest)
}