-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_test.go
79 lines (67 loc) · 1.67 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
package dtls
import (
"crypto/tls"
"reflect"
"testing"
"github.com/pion/dtls/v2/pkg/crypto/selfsign"
)
func TestGetCertificate(t *testing.T) {
certificateWildcard, err := selfsign.GenerateSelfSignedWithDNS("*.test.test")
if err != nil {
t.Fatal(err)
}
certificateTest, err := selfsign.GenerateSelfSignedWithDNS("test.test", "www.test.test", "pop.test.test")
if err != nil {
t.Fatal(err)
}
certificateRandom, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatal(err)
}
cfg := &handshakeConfig{
localCertificates: []tls.Certificate{
certificateRandom,
certificateTest,
certificateWildcard,
},
}
testCases := []struct {
desc string
serverName string
expectedCertificate tls.Certificate
}{
{
desc: "Simple match in CN",
serverName: "test.test",
expectedCertificate: certificateTest,
},
{
desc: "Simple match in SANs",
serverName: "www.test.test",
expectedCertificate: certificateTest,
},
{
desc: "Wildcard match",
serverName: "foo.test.test",
expectedCertificate: certificateWildcard,
},
{
desc: "No match return first",
serverName: "foo.bar",
expectedCertificate: certificateRandom,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
cert, err := cfg.getCertificate(test.serverName)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cert.Leaf, test.expectedCertificate.Leaf) {
t.Fatalf("Certificate does not match: expected(%v) actual(%v)", test.expectedCertificate.Leaf, cert.Leaf)
}
})
}
}