-
Notifications
You must be signed in to change notification settings - Fork 22
/
autocert_test.go
191 lines (168 loc) · 4.48 KB
/
autocert_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package acme
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
)
func TestWhitelistHosts(t *testing.T) {
w := WhitelistHosts("hello")
if err := w("no"); err == nil {
t.Fatal("expected error, got none")
}
if err := w("hello"); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestAutoCert_HTTPHandler(t *testing.T) {
a := AutoCert{}
handler := a.HTTPHandler(nil)
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Result().StatusCode != http.StatusMovedPermanently {
t.Fatalf("expected status %d, got: %d", http.StatusMovedPermanently, w.Result().StatusCode)
}
}
func TestAutoCert_GetCertificate(t *testing.T) {
tests := []struct {
ac AutoCert
helo *tls.ClientHelloInfo
err bool
errStr string
}{
{
ac: AutoCert{},
helo: &tls.ClientHelloInfo{},
err: true,
errStr: "missing",
},
{
ac: AutoCert{},
helo: &tls.ClientHelloInfo{ServerName: "simple"},
err: true,
errStr: "count invalid",
},
{
ac: AutoCert{},
helo: &tls.ClientHelloInfo{ServerName: `inva.lid\`},
err: true,
errStr: "invalid character",
},
{
ac: AutoCert{},
helo: &tls.ClientHelloInfo{ServerName: `inva.lid/`},
err: true,
errStr: "invalid character",
},
{
ac: AutoCert{HostCheck: WhitelistHosts("no.no")},
helo: &tls.ClientHelloInfo{ServerName: `va.lid`},
err: true,
errStr: "not whitelisted",
},
}
for k := range tests {
_, err := tests[k].ac.GetCertificate(tests[k].helo)
if tests[k].err && err == nil {
t.Fatalf("expected error, got none")
}
if !tests[k].err && err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if !strings.Contains(err.Error(), tests[k].errStr) {
t.Fatalf("missing %q in error: %v", tests[k].errStr, err)
}
}
}
func TestAutoCert_getDirectoryURL(t *testing.T) {
ac := AutoCert{}
if dir := ac.getDirectoryURL(); dir != LetsEncryptStaging {
t.Fatalf("Expected staging url, got: %s", dir)
}
ac.DirectoryURL = "blah"
if dir := ac.getDirectoryURL(); dir != "blah" {
t.Fatalf("expected blah, got: %s", dir)
}
}
func TestAutoCert_Cache(t *testing.T) {
ac := AutoCert{}
data := []byte{1, 2, 3}
ac.putCache(data, "hello", "world")
if b := ac.getCache("hello", "world"); !reflect.DeepEqual(data, b) {
t.Fatalf("expected: %+v, got: %+v", data, b)
}
if b := ac.getCache("non", "existent"); b != nil {
t.Fatalf("expected: nil, got: %+v", b)
}
}
func TestAutoCert_Cache2(t *testing.T) {
ac := AutoCert{CacheDir: os.TempDir()}
data := []byte{1, 2, 3}
ctx := ac.putCache(data, "hello", "world")
<-ctx.Done()
ac2 := AutoCert{CacheDir: os.TempDir()}
if b := ac2.getCache("hello", "world"); !reflect.DeepEqual(data, b) {
t.Fatalf("expected: %+v, got: %+v", data, b)
}
ac3 := AutoCert{CacheDir: "fake"}
if b := ac3.getCache("hello", "world"); b != nil {
t.Fatalf("expected: nil, got: %+v", b)
}
}
func TestAutoCert_checkHost(t *testing.T) {
ac := AutoCert{}
if err := ac.checkHost("ok"); err != nil {
t.Fatalf("expected nil, got: %v", err)
}
ac2 := AutoCert{HostCheck: WhitelistHosts("host")}
if err := ac2.checkHost("ok"); err == nil {
t.Fatal("expected error, got: nil")
}
}
func TestAutoCert_getExistingCert(t *testing.T) {
ac := AutoCert{}
if cert, _ := ac.getExistingCert("fake"); cert != nil {
t.Fatalf("expected nil cert, got: %+v", cert)
}
}
func TestAutoCert_GetCertificate2(t *testing.T) {
root := fetchRoot()
doPost := func(name string, req interface{}) {
reqJSON, err := json.Marshal(req)
if err != nil {
panic(fmt.Sprintf("error marshalling boulder %s: %v", name, err))
}
if _, err := http.Post("http://localhost:8055/"+name, "application/json", bytes.NewReader(reqJSON)); err != nil {
panic(fmt.Sprintf("error posting boulder %s: %v", name, err))
}
}
ac := AutoCert{
DirectoryURL: testClient.Directory().URL,
Options: []OptionFunc{WithInsecureSkipVerify()},
RootCert: string(root),
PreUpdateChallengeHook: func(account Account, challenge Challenge) {
addReq := struct {
Token string `json:"token"`
Content string `json:"content"`
}{
Token: challenge.Token,
Content: challenge.KeyAuthorization,
}
doPost("add-http01", addReq)
},
}
cert, err := ac.GetCertificate(&tls.ClientHelloInfo{ServerName: randString() + ".com"})
if err != nil {
t.Fatalf("error getting certificate: %v", err)
}
if cert == nil {
t.Fatalf("NO CERT")
}
}