-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata_test.go
143 lines (133 loc) · 3.03 KB
/
metadata_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
// SPDX-FileCopyrightText: 2020 Google LLC
// SPDX-License-Identifier: Apache-2.0
package piv
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMetadata(t *testing.T) {
tests := []struct {
name string
slot Slot
policy Key
importKey bool
}{
{
"EC/P256/Generated",
SlotAuthentication,
Key{AlgECCP256, PINPolicyNever, TouchPolicyNever},
false,
},
{
"EC/P384/Generated",
SlotAuthentication,
Key{AlgECCP384, PINPolicyNever, TouchPolicyNever},
false,
},
{
"RSA/1024/Generated",
SlotAuthentication,
Key{AlgRSA1024, PINPolicyNever, TouchPolicyNever},
false,
},
{
"RSA/2048/Generated",
SlotAuthentication,
Key{AlgRSA2048, PINPolicyNever, TouchPolicyNever},
false,
},
{
"EC/P256/Imported",
SlotAuthentication,
Key{AlgECCP256, PINPolicyNever, TouchPolicyNever},
true,
},
{
"EC/P384/Imported",
SlotAuthentication,
Key{AlgECCP384, PINPolicyNever, TouchPolicyNever},
true,
},
{
"RSA/1024/Imported",
SlotAuthentication,
Key{AlgRSA1024, PINPolicyNever, TouchPolicyNever},
true,
},
{
"RSA/2048/Imported",
SlotAuthentication,
Key{AlgRSA2048, PINPolicyNever, TouchPolicyNever},
true,
},
{
"PINPolicy/Once",
SlotAuthentication,
Key{AlgECCP256, PINPolicyOnce, TouchPolicyNever},
false,
},
{
"PINPolicy/Always",
SlotAuthentication,
Key{AlgECCP256, PINPolicyAlways, TouchPolicyNever},
false,
},
{
"TouchPolicy/Always",
SlotAuthentication,
Key{AlgECCP256, PINPolicyNever, TouchPolicyAlways},
false,
},
{
"TouchPolicy/Cached",
SlotAuthentication,
Key{AlgECCP256, PINPolicyNever, TouchPolicyCached},
false,
},
{
"SlotSignature",
SlotSignature,
Key{AlgECCP256, PINPolicyNever, TouchPolicyCached},
false,
},
{
"SlotCardAuthentication",
SlotCardAuthentication,
Key{AlgECCP256, PINPolicyNever, TouchPolicyCached},
false,
},
{
"SlotKeyManagement",
SlotKeyManagement,
Key{AlgECCP256, PINPolicyNever, TouchPolicyCached},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
withCard(t, true, false, SupportsMetadata, func(t *testing.T, c *Card) {
want := &Metadata{
Algorithm: test.policy.Algorithm,
PINPolicy: test.policy.PINPolicy,
TouchPolicy: test.policy.TouchPolicy,
}
if test.importKey {
key := testKey(t, test.policy.Algorithm.algType(), test.policy.Algorithm.bits())
err := c.SetPrivateKeyInsecure(DefaultManagementKey, test.slot, key, test.policy)
require.NoError(t, err, "importing key")
want.Origin = OriginImported
want.PublicKey = key.Public()
} else {
pub, err := c.GenerateKey(DefaultManagementKey, test.slot, test.policy)
require.NoError(t, err, "Failed to generate key")
want.Origin = OriginGenerated
want.PublicKey = pub
}
got, err := c.Metadata(test.slot)
require.NoError(t, err, "Failed to get key metadata")
assert.Equal(t, want, got)
})
})
}
}