-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
79 lines (72 loc) · 1.86 KB
/
Copy pathconfig_test.go
File metadata and controls
79 lines (72 loc) · 1.86 KB
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
// Copyright (c) 2024, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package easytls
import (
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig_ApplyTo(t *testing.T) {
t.Run("VerifyClient", func(t *testing.T) {
var conf tls.Config
require.NoError(t, Config{VerifyClient: true}.ApplyTo(&conf, 0))
assert.Equal(t, tls.RequireAndVerifyClientCert, conf.ClientAuth)
})
t.Run("InsecureSkipVerify", func(t *testing.T) {
var conf tls.Config
require.NoError(t, Config{InsecureSkipVerify: true}.ApplyTo(&conf, 0))
assert.True(t, conf.InsecureSkipVerify)
})
}
func TestValidateCA(t *testing.T) {
tests := map[string]struct {
cert *x509.Certificate
wantValid bool
wantErr []error
}{
"nil": {},
"not marked as ca": {
cert: &x509.Certificate{
KeyUsage: x509.KeyUsageCertSign,
},
wantErr: []error{ErrNotMarkedAsCA},
},
"missing cert sign": {
cert: &x509.Certificate{
IsCA: true,
},
wantErr: []error{ErrMissingCertSign},
},
"CACertificate": {
cert: CACertificate(pkix.Name{}),
wantValid: true,
},
"ServerCertificate": {
cert: ServerCertificate("localhost"),
wantValid: false,
wantErr: []error{ErrNotMarkedAsCA, ErrMissingCertSign},
},
"ClientCertificate": {
cert: ClientCertificate(),
wantValid: false,
wantErr: []error{ErrNotMarkedAsCA, ErrMissingCertSign},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
haveValid, haveErr := ValidateCA(tc.cert)
assert.Equal(t, tc.wantValid, haveValid)
if tc.wantErr == nil {
assert.NoError(t, haveErr)
} else {
for _, wantErr := range tc.wantErr {
assert.ErrorIs(t, haveErr, wantErr)
}
}
})
}
}