-
Notifications
You must be signed in to change notification settings - Fork 0
/
adssl_test.go
121 lines (102 loc) · 2.45 KB
/
adssl_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
package adssl
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestPrintKubeSecret(t *testing.T) {
var output bytes.Buffer
c := Certificate{
PrivateKeyString: "key",
Result: "result",
CaCert: "ca cert",
}
PrintKubeSecret(&output, c)
want := `apiVersion: v1
kind: Secret
name: tls-secret
data:
ca.crt: Y2EgY2VydA==
tls.key: a2V5
tls.crt: cmVzdWx0
`
if output.String() != want {
t.Errorf("want %q, got %q", want, output.String())
}
}
func TestNew(t *testing.T) {
srv := serverMock()
defer srv.Close()
s := Server{
Endpoint: srv.URL,
Username: "user",
Password: "pass",
}
r := Request{
CommonName: "foo.bar",
Country: "US",
Province: "New Jersey",
Locality: "Weehawken",
DNSNames: "boo.bar",
IPAddresses: "1.2.3.4",
}
testCase := struct {
CaCert, Result string
}{
CaCert: "-----BEGIN CERTIFICATE-----CA-----END CERTIFICATE-----",
Result: "-----BEGIN CERTIFICATE-----666-----END CERTIFICATE-----",
}
got, err := New(s, r)
if err != nil {
t.Fatal(err)
}
if got.CaCert != testCase.CaCert {
t.Errorf("got %q, want %q", got.CaCert, testCase.CaCert)
}
if got.Result != testCase.Result {
t.Errorf("got %q, want %q", got.Result, testCase.Result)
}
}
func TestGenerateCertFromCsr(t *testing.T) {
srv := serverMock()
defer srv.Close()
c := Certificate{
CertificateRequest: "this is a CSR request",
}
s := Server{
Endpoint: srv.URL,
Username: "user",
Password: "pass",
}
want := "-----BEGIN CERTIFICATE-----666-----END CERTIFICATE-----"
got, err := GenerateCertFromCsr(s, c)
if err != nil {
t.Fatal(err)
}
if got.Result != want {
t.Errorf("want %q, got %q", want, got.Result)
}
}
func serverMock() *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc("/certsrv/certcarc.asp", certcarcMock)
handler.HandleFunc("/certsrv/certfnsh.asp", certfnshMock)
handler.HandleFunc("/certsrv/certnew.cer", certnewMock)
srv := httptest.NewServer(handler)
return srv
}
func certcarcMock(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("nRenewals=111;"))
}
func certfnshMock(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("certnew.cer?ReqID=666&Enc=b64"))
}
func certnewMock(w http.ResponseWriter, r *http.Request) {
switch r.FormValue("ReqID") {
case "CACert":
w.Write([]byte("-----BEGIN CERTIFICATE-----CA-----END CERTIFICATE-----"))
case "666":
w.Write([]byte("-----BEGIN CERTIFICATE-----666-----END CERTIFICATE-----"))
}
}