-
Notifications
You must be signed in to change notification settings - Fork 20
/
util_test.go
266 lines (227 loc) · 6.7 KB
/
util_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
package applepay
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/hex"
"math/big"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestExtractMerchantHash(t *testing.T) {
Convey("Nil certificates are rejected", t, func() {
hash, err := extractMerchantHash(tls.Certificate{})
Convey("hash should be nil", func() {
So(hash, ShouldBeNil)
})
Convey("err should be correct", func() {
So(err.Error(), ShouldEqual, "nil certificate")
})
})
Convey("Invalid certificates are rejected", t, func() {
cert := tls.Certificate{
Certificate: [][]byte{
[]byte("not a valid certificate"),
},
}
hash, err := extractMerchantHash(cert)
Convey("hash should be nil", func() {
So(hash, ShouldBeNil)
})
Convey("err should be correct", func() {
So(err.Error(), ShouldStartWith, "certificate parsing error")
})
})
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
Convey("Certificates without the extension return an error", t, func() {
tpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
}
cert, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, &key.PublicKey, key)
hash, err := extractMerchantHash(tls.Certificate{Certificate: [][]byte{cert}})
Convey("hash should be nil", func() {
So(hash, ShouldBeNil)
})
Convey("err should be correct", func() {
So(err.Error(), ShouldStartWith, "error finding the hash extension")
})
})
Convey("Certificates with an hash too short return an error", t, func() {
tpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
ExtraExtensions: []pkix.Extension{
{
Id: merchantIDHashOID,
Value: []byte("@.hash too short"),
},
},
}
cert, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, &key.PublicKey, key)
hash, err := extractMerchantHash(tls.Certificate{Certificate: [][]byte{cert}})
Convey("hash should be nil", func() {
So(hash, ShouldBeNil)
})
Convey("err should be correct", func() {
So(err.Error(), ShouldEqual, "invalid hash length")
})
})
Convey("Certificates with an invalid hexadecimal hash return an error", t, func() {
tpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
ExtraExtensions: []pkix.Extension{
{
Id: merchantIDHashOID,
Value: []byte("@.this string is the correct length but it's not valid hexadecimal"),
},
},
}
cert, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, &key.PublicKey, key)
hash, err := extractMerchantHash(tls.Certificate{Certificate: [][]byte{cert}})
Convey("hash should be nil", func() {
So(hash, ShouldBeNil)
})
Convey("err should be correct", func() {
So(err.Error(), ShouldStartWith, "invalid hash hex")
})
})
Convey("Certificates with a correctly encoded hash return the right value", t, func() {
tpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
ExtraExtensions: []pkix.Extension{
{
Id: merchantIDHashOID,
Value: []byte("@.cc7614e23cd0e6b5b758c7519977a73624dc4395eb19c3fdb6dcdfbb47158cfd"),
},
},
}
cert, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, &key.PublicKey, key)
hash, err := extractMerchantHash(tls.Certificate{Certificate: [][]byte{cert}})
Convey("hash should be correct", func() {
expectedHash, _ := hex.DecodeString("cc7614e23cd0e6b5b758c7519977a73624dc4395eb19c3fdb6dcdfbb47158cfd")
So(hash, ShouldResemble, expectedHash)
})
Convey("err should be nil", func() {
So(err, ShouldBeNil)
})
})
}
func TestExtractExtension(t *testing.T) {
Convey("Nil certificates return an error", t, func() {
res, err := extractExtension(nil, merchantIDHashOID)
Convey("value is nil", func() {
So(res, ShouldBeNil)
})
Convey("err is correct", func() {
So(err.Error(), ShouldEqual, "nil certificate")
})
})
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
Convey("Inexisting extensions return an error", t, func() {
tpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
ExtraExtensions: []pkix.Extension{
{
Id: mustParseASN1ObjectIdentifier("1.2.3.4.5"),
Value: []byte("@.test"),
}, {
Id: mustParseASN1ObjectIdentifier("1.2.3.4.6"),
Value: []byte("@.test2"),
},
},
}
certBytes, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, &key.PublicKey, key)
cert, _ := x509.ParseCertificate(certBytes)
res, err := extractExtension(cert, merchantIDHashOID)
Convey("value is nil", func() {
So(res, ShouldBeNil)
})
Convey("err is correct", func() {
So(err.Error(), ShouldEqual, "extension not found")
})
})
Convey("Valid extensions work", t, func() {
tpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
ExtraExtensions: []pkix.Extension{
{
Id: mustParseASN1ObjectIdentifier("1.2.3.4.5"),
Value: []byte("@.test"),
}, {
Id: merchantIDHashOID,
Value: []byte("@.correct value"),
},
},
}
certBytes, _ := x509.CreateCertificate(rand.Reader, tpl, tpl, &key.PublicKey, key)
cert, _ := x509.ParseCertificate(certBytes)
res, err := extractExtension(cert, merchantIDHashOID)
Convey("value is correct", func() {
So(res, ShouldResemble, []byte("@.correct value"))
})
Convey("err is nil", func() {
So(err, ShouldBeNil)
})
})
}
func TestMustParseASN1ObjectIdentifier(t *testing.T) {
Convey("Invalid OIDs panic", t, func() {
var oid asn1.ObjectIdentifier
recovered := false
func() {
defer func() {
if r := recover(); r != nil {
recovered = true
}
}()
oid = mustParseASN1ObjectIdentifier("1.2.3.invalid")
}()
Convey("panic recovered", func() {
So(recovered, ShouldBeTrue)
})
Convey("oid is nil", func() {
So(oid, ShouldBeNil)
})
})
Convey("Valid OIDs do not panic", t, func() {
var oid asn1.ObjectIdentifier
recovered := false
func() {
defer func() {
if r := recover(); r != nil {
recovered = true
}
}()
oid = mustParseASN1ObjectIdentifier("1.2.840.113635.100.6.32")
}()
Convey("no panic recovered", func() {
So(recovered, ShouldBeFalse)
})
Convey("oid is correct", func() {
So(oid, ShouldResemble, asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 6, 32})
})
})
}
func TestParseASN1ObjectIdentifier(t *testing.T) {
Convey("Invalid OIDs are not parsed", t, func() {
oid, err := parseASN1ObjectIdentifier("1.2.3.invalid")
Convey("oid is nil", func() {
So(oid, ShouldBeNil)
})
Convey("err is not nil", func() {
So(err, ShouldNotBeNil)
})
})
Convey("Valid OIDs are parsed correctly", t, func() {
oid, err := parseASN1ObjectIdentifier("1.2.840.113635.100.6.32")
Convey("oid is correct", func() {
So(oid, ShouldResemble, asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 6, 32})
})
Convey("err is nil", func() {
So(err, ShouldBeNil)
})
})
}