-
Notifications
You must be signed in to change notification settings - Fork 22
/
certificate_test.go
148 lines (140 loc) · 3.79 KB
/
certificate_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package acme
import (
"bytes"
"encoding/pem"
"net/http"
"strings"
"testing"
)
func Test_decodeCertificateChain(t *testing.T) {
account, order, _ := makeOrderFinalised(t, nil)
tests := []struct {
name string
body func() []byte
resp func() *http.Response
expectsError bool
errorStr string
}{
{
name: "invalid certificate",
body: func() []byte {
var b []byte
block := &pem.Block{
Type: "MESSAGE",
Headers: map[string]string{
"Animal": "Gopher",
},
Bytes: []byte("test"),
}
if err := pem.Encode(bytes.NewBuffer(b), block); err != nil {
t.Fatal(err)
}
return b
},
resp: func() *http.Response {
return nil
},
},
{
name: "invalid link",
body: func() []byte {
return nil
},
resp: func() *http.Response {
return &http.Response{
Header: map[string][]string{
"Link": {`<http://bogus.fakedomain>; rel="up"`},
},
}
},
expectsError: true,
errorStr: "bogus.fakedomain",
},
{
name: "valid link",
body: func() []byte {
return nil
},
resp: func() *http.Response {
return &http.Response{
Header: map[string][]string{
"Link": {`<` + order.Certificate + `>; rel="up"`},
},
}
},
},
}
for i, ct := range tests {
body := ct.body()
resp := ct.resp()
_, err := testClient.decodeCertificateChain(body, resp, account)
if ct.expectsError && err == nil {
t.Errorf("decodeCertificateChain test %d %q expected error, got none", i, ct.name)
}
if !ct.expectsError && err != nil {
t.Errorf("decodeCertificateChain test %d %q expected no error, got: %v", i, ct.name, err)
}
if err != nil && ct.errorStr != "" && !strings.Contains(err.Error(), ct.errorStr) {
t.Errorf("AccoudecodeCertificateChainntKeyChange test %d %q error doesnt contain %q: %s", i, ct.name, ct.errorStr, err.Error())
}
}
}
func TestClient_FetchCertificates(t *testing.T) {
account, order, _ := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
certs, err := testClient.FetchCertificates(account, order.Certificate)
if err != nil {
t.Fatalf("expeceted no error, got: %v", err)
}
if len(certs) == 0 {
t.Fatal("no certs returned")
}
for _, d := range order.Identifiers {
if err := certs[0].VerifyHostname(d.Value); err != nil {
t.Fatalf("cert not verified for %s: %v - %+v", d, err, certs[0])
}
}
}
func TestClient_FetchAllCertificates(t *testing.T) {
account, order, _ := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
certs, err := testClient.FetchAllCertificates(account, order.Certificate)
if err != nil {
t.Fatalf("expeceted no error, got: %v", err)
}
if len(certs) == 1 {
t.Skip("no alternative root certificates")
}
}
func TestClient_RevokeCertificate(t *testing.T) {
// test revoking cert with cert key
account, order, privKey := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
certs, err := testClient.FetchCertificates(account, order.Certificate)
if err != nil {
t.Fatalf("expeceted no error, got: %v", err)
}
if err := testClient.RevokeCertificate(account, certs[0], privKey, ReasonUnspecified); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestClient_RevokeCertificate2(t *testing.T) {
// test revoking cert with account key
account, order, _ := makeOrderFinalised(t, nil)
if order.Certificate == "" {
t.Fatalf("no certificate: %+v", order)
}
certs, err := testClient.FetchCertificates(account, order.Certificate)
if err != nil {
t.Fatalf("expeceted no error, got: %v", err)
}
if err := testClient.RevokeCertificate(account, certs[0], account.PrivateKey, ReasonUnspecified); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}