forked from globalsign/hvclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_info_test.go
313 lines (287 loc) · 8.44 KB
/
cert_info_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright (c) 2019-2021 GMO GlobalSign Pte. Ltd.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hvclient_test
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/globalsign/hvclient"
"github.com/globalsign/hvclient/internal/testhelpers"
)
const testPEM = `-----BEGIN C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lnbi5jb20vY2FjZXJ0L2dzbnBodmNhZGVtb3NoYTJnMy5jcnQwRAYDVR0fBD0w
OzA5oDegNYYzaHR0cDovL2NybC5nbG9iYWxzaWduLmNvbS9jYS9nc25waHZjYWRl
bW9zaGEyZzMuY3JsMA0GCSqGSIb3DQEBCwUAA4IBAQA9u+JNLBTwqUBj7X1BxUyG
QuHRyOeiVzY4YRNWplW3tkwzizX0UZruLoMyJ9DQqNNWYGALrynaEKDcVnVZnzRf
BLX1C08WVb37EHYeu/wyh3bhu3sSRmbjuYxduyDDdhye3urbHUkMDjCjGuE6r45l
pzODa/yMds7Dt/s0GzE6kGGqeAXcZiT9CNiTKfgSykGPQmI5MSVxWRu6eXZKQ9M1
awPX0kDyz7BCnA7g6TgOMSLpIpmS+sk0gPbn0L8aSbsr+rVXeuvflMvX6s+83O90
C5KcdB2n5alI+o4AXFf5Gv4L4isofggpTtUNioHZvXCJE0jpuhgZmcGUWvJe6GfH
-----END CERTIFICATE-----`
func TestCertInfoEqual(t *testing.T) {
t.Parallel()
var testcases = []struct {
name string
first hvclient.CertInfo
second hvclient.CertInfo
want bool
}{
{
name: "All/Equal",
first: hvclient.CertInfo{
PEM: "some PEM",
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
Status: hvclient.StatusIssued,
UpdatedAt: time.Date(2021, 6, 21, 18, 43, 30, 0, time.UTC),
},
second: hvclient.CertInfo{
PEM: "some PEM",
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
Status: hvclient.StatusIssued,
UpdatedAt: time.Date(2021, 6, 21, 18, 43, 30, 0, time.UTC),
},
want: true,
},
{
name: "PEM",
first: hvclient.CertInfo{
PEM: "some PEM",
},
second: hvclient.CertInfo{
PEM: "some other PEM",
},
want: false,
},
{
name: "X509/Value",
first: hvclient.CertInfo{
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
},
second: hvclient.CertInfo{
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
},
want: true,
},
{
name: "X509/Inequal",
first: hvclient.CertInfo{
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
},
second: hvclient.CertInfo{
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_ica_cert.pem"),
},
want: false,
},
{
name: "X509/FirstNil",
first: hvclient.CertInfo{
X509: nil,
},
second: hvclient.CertInfo{
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
},
want: false,
},
{
name: "X509/SecondNil",
first: hvclient.CertInfo{
X509: testhelpers.MustGetCertFromFile(t, "testdata/test_cert.pem"),
},
second: hvclient.CertInfo{
X509: nil,
},
want: false,
},
{
name: "Status",
first: hvclient.CertInfo{
Status: hvclient.StatusIssued,
},
second: hvclient.CertInfo{
Status: hvclient.StatusRevoked,
},
want: false,
},
{
name: "UpdatedAt",
first: hvclient.CertInfo{
UpdatedAt: time.Date(2021, 6, 21, 18, 43, 30, 0, time.UTC),
},
second: hvclient.CertInfo{
UpdatedAt: time.Date(2021, 7, 21, 18, 43, 30, 0, time.UTC),
},
want: false,
},
}
for _, tc := range testcases {
var tc = tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := tc.first.Equal(tc.second); got != tc.want {
t.Fatalf("got %t, want %t", got, tc.want)
}
})
}
}
func TestCertInfoMarshalJSON(t *testing.T) {
t.Parallel()
var testcases = []struct {
name string
info hvclient.CertInfo
want []byte
err error
}{
{
name: "Issued",
info: hvclient.CertInfo{
PEM: testPEM,
Status: hvclient.StatusIssued,
UpdatedAt: time.Unix(1477958400, 0),
},
want: []byte(fmt.Sprintf(`{"certificate":"%s","status":"ISSUED","updated_at":1477958400}`,
strings.Replace(testPEM, "\n", "\\n", -1))),
},
{
name: "Revoked",
info: hvclient.CertInfo{
PEM: testPEM,
Status: hvclient.StatusRevoked,
UpdatedAt: time.Unix(1477958400, 0),
},
want: []byte(fmt.Sprintf(`{"certificate":"%s","status":"REVOKED","updated_at":1477958400}`,
strings.Replace(testPEM, "\n", "\\n", -1))),
},
{
name: "BadStatus",
info: hvclient.CertInfo{
PEM: testPEM,
Status: hvclient.CertStatus(0),
UpdatedAt: time.Unix(1477958400, 0),
},
err: errors.New("invalid status"),
},
}
for _, tc := range testcases {
var tc = tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var got, err = json.Marshal(tc.info)
if (err == nil) != (tc.err == nil) {
t.Fatalf("got error %v, want %v", err, tc.err)
}
if !bytes.Equal(got, tc.want) {
t.Errorf("got %s, want %s", string(got), string(tc.want))
}
})
}
}
func TestCertInfoUnmarshalJSON(t *testing.T) {
t.Parallel()
var testcases = []struct {
name string
data []byte
want hvclient.CertInfo
err error
}{
{
name: "Issued",
data: []byte(fmt.Sprintf(`{"certificate":"%s","status":"ISSUED","updated_at":1477958400}`,
strings.Replace(testPEM, "\n", "\\n", -1))),
want: hvclient.CertInfo{
PEM: testPEM,
X509: testhelpers.MustParseCert(t, testPEM),
Status: hvclient.StatusIssued,
UpdatedAt: time.Unix(1477958400, 0),
},
},
{
name: "Revoked",
data: []byte(fmt.Sprintf(`{"certificate":"%s","status":"REVOKED","updated_at":1477958400}`,
strings.Replace(testPEM, "\n", "\\n", -1))),
want: hvclient.CertInfo{
PEM: testPEM,
X509: testhelpers.MustParseCert(t, testPEM),
Status: hvclient.StatusRevoked,
UpdatedAt: time.Unix(1477958400, 0),
},
},
{
name: "BadStatusValue",
data: []byte(fmt.Sprintf(`{"certificate":"%s","status":"BAD STATUS","updated_at":1477958400}`,
strings.Replace(testPEM, "\n", "\\n", -1))),
err: errors.New("bad status value"),
},
{
name: "BadStatusType",
data: []byte(fmt.Sprintf(`{"certificate":"%s","status":1234,"updated_at":1477958400}`,
strings.Replace(testPEM, "\n", "\\n", -1))),
err: errors.New("bad status type"),
},
{
name: "BadPEM",
data: []byte(`{"certificate":"BAD PEM","status":"ISSUED","updated_at":1477958400}`),
err: errors.New("invalid PEM"),
},
{
name: "EmptyPEM",
data: []byte(`{"certificate":"","status":"ISSUED","updated_at":1477958400}`),
err: errors.New("missing PEM"),
},
{
name: "InvalidCertificate",
data: []byte(fmt.Sprintf(`{"certificate":"%s","status":"ISSUED","updated_at":1477958400}`,
strings.Replace(strings.Replace(testPEM, "\n", "\\n", -1), "M", "N", -1))),
err: errors.New("invalid certificate"),
},
}
for _, tc := range testcases {
var tc = tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var got hvclient.CertInfo
var err = json.Unmarshal(tc.data, &got)
if (err == nil) != (tc.err == nil) {
t.Fatalf("got error %v, want %v", err, tc.err)
}
if !got.Equal(tc.want) {
t.Errorf("got %v, want %v", got, tc.want)
}
})
}
}
func TestCertStatusStringInvalidValue(t *testing.T) {
t.Parallel()
var want = "ERROR: UNKNOWN STATUS"
if got := hvclient.CertStatus(0).String(); got != want {
t.Errorf("got %s, want %s", got, want)
}
}