-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_utils_test.go
62 lines (59 loc) · 1.41 KB
/
tls_utils_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
package aviation
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetClientTLSConfig(t *testing.T) {
for _, test := range []struct {
name string
ca string
usrCrt string
usrKey string
hasErr bool
}{
{
name: "CACertDNE",
ca: "DNE",
usrCrt: filepath.Join("testdata", "usr.crt"),
usrKey: filepath.Join("testdata", "usr.key"),
hasErr: true,
},
{
name: "UserCrtDNE",
ca: filepath.Join("testdata", "ca.crt"),
usrCrt: "DNE",
usrKey: "DNE",
hasErr: true,
},
{
name: "ClientConfig",
ca: filepath.Join("testdata", "ca.crt"),
usrCrt: filepath.Join("testdata", "user.crt"),
usrKey: filepath.Join("testdata", "user.key"),
},
} {
t.Run(test.name, func(t *testing.T) {
conf, err := GetClientTLSConfigFromFiles(test.ca, test.usrCrt, test.usrKey)
if test.hasErr {
assert.Error(t, err)
assert.Nil(t, conf)
} else {
assert.NoError(t, err)
expectedCrt, err := tls.LoadX509KeyPair(test.usrCrt, test.usrKey)
require.NoError(t, err)
require.Len(t, conf.Certificates, 1)
assert.Equal(t, expectedCrt, conf.Certificates[0])
ca, err := ioutil.ReadFile(test.ca)
require.NoError(t, err)
cp := x509.NewCertPool()
require.True(t, cp.AppendCertsFromPEM(ca))
assert.Equal(t, cp, conf.RootCAs)
}
})
}
}