-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension_test.go
124 lines (114 loc) · 2.19 KB
/
extension_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
package smolcert
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidateKeyUsages(t *testing.T) {
testTable := []struct {
cert *Certificate
expectedKeyUsage KeyUsage
valid bool
}{
{
cert: &Certificate{
Extensions: []Extension{
Extension{
OID: OIDKeyUsage,
Critical: true,
Value: KeyUsageClientIdentification.ToBytes(),
},
},
},
expectedKeyUsage: KeyUsageClientIdentification,
valid: true,
},
{
cert: &Certificate{
Extensions: []Extension{
Extension{
OID: OIDKeyUsage,
Critical: true,
Value: KeyUsageClientIdentification.ToBytes(),
},
},
},
expectedKeyUsage: KeyUsageSignCert,
valid: false,
},
{
cert: &Certificate{
Extensions: []Extension{
Extension{
OID: OIDKeyUsage,
Critical: true,
Value: KeyUsageClientIdentification.ToBytes(),
},
},
},
expectedKeyUsage: KeyUsageServerIdentification,
valid: false,
},
{
cert: &Certificate{
Extensions: []Extension{},
},
expectedKeyUsage: KeyUsageClientIdentification,
valid: false,
},
}
for _, tt := range testTable {
err := RequiresExtension(tt.cert, OIDKeyUsage, ExpectKeyUsage(tt.expectedKeyUsage))
assert.Equal(t, err == nil, tt.valid)
}
}
func TestEnsureExtension(t *testing.T) {
testtable := []struct {
extensions []Extension
expectedCount int
ensured Extension
}{
{
extensions: []Extension{
{
OID: 12,
Critical: false,
},
{
OID: 13,
Critical: false,
},
},
expectedCount: 3,
ensured: Extension{
OID: 14,
},
},
{
extensions: []Extension{
{
OID: 12,
Critical: false,
},
{
OID: 13,
Critical: false,
},
},
expectedCount: 2,
ensured: Extension{
OID: 12,
},
},
{
extensions: []Extension{},
expectedCount: 1,
ensured: Extension{
OID: 14,
},
},
}
for i, tt := range testtable {
extensions := ensureExtension(tt.extensions, tt.ensured)
assert.Len(t, extensions, tt.expectedCount, "Test iteration %d", i)
}
}