@@ -37,17 +37,19 @@ import (
37
37
"go.step.sm/crypto/tpm/tss2"
38
38
)
39
39
40
- type newSimulatedTPMOption func ( t * testing. T , tpm * tpmp. TPM )
40
+ type newSimulatedTPMOption any
41
41
42
- func withAK (name string ) newSimulatedTPMOption {
42
+ type newSimulatedTPMPreparerOption func (t * testing.T , tpm * tpmp.TPM )
43
+
44
+ func withAK (name string ) newSimulatedTPMPreparerOption {
43
45
return func (t * testing.T , tpm * tpmp.TPM ) {
44
46
t .Helper ()
45
47
_ , err := tpm .CreateAK (context .Background (), name )
46
48
require .NoError (t , err )
47
49
}
48
50
}
49
51
50
- func withKey (name string ) newSimulatedTPMOption {
52
+ func withKey (name string ) newSimulatedTPMPreparerOption {
51
53
return func (t * testing.T , tpm * tpmp.TPM ) {
52
54
t .Helper ()
53
55
config := tpmp.CreateKeyConfig {
@@ -59,20 +61,38 @@ func withKey(name string) newSimulatedTPMOption {
59
61
}
60
62
}
61
63
64
+ func withCapabilities (caps * tpmp.Capabilities ) tpmp.NewTPMOption {
65
+ return tpmp .WithCapabilities (caps )
66
+ }
67
+
62
68
func newSimulatedTPM (t * testing.T , opts ... newSimulatedTPMOption ) * tpmp.TPM {
63
69
t .Helper ()
70
+
64
71
tmpDir := t .TempDir ()
65
72
tpmOpts := []tpmp.NewTPMOption {
66
73
withSimulator (t ),
67
74
tpmp .WithStore (storage .NewDirstore (tmpDir )),
68
75
}
69
76
70
- tpm , err := tpmp .New (tpmOpts ... )
77
+ var preparers []newSimulatedTPMPreparerOption
78
+ for _ , opt := range opts {
79
+ switch o := opt .(type ) {
80
+ case tpmp.NewTPMOption :
81
+ tpmOpts = append (tpmOpts , o )
82
+ case newSimulatedTPMPreparerOption :
83
+ preparers = append (preparers , o )
84
+ default :
85
+ require .Fail (t , "invalid TPM option type provided" , `TPM option type "%T"` , o )
86
+ }
87
+ }
71
88
89
+ tpm , err := tpmp .New (tpmOpts ... )
72
90
require .NoError (t , err )
73
- for _ , applyTo := range opts {
91
+
92
+ for _ , applyTo := range preparers {
74
93
applyTo (t , tpm )
75
94
}
95
+
76
96
return tpm
77
97
}
78
98
@@ -93,6 +113,60 @@ func withSimulator(t *testing.T) tpmp.NewTPMOption {
93
113
return tpmp .WithSimulator (sim )
94
114
}
95
115
116
+ func TestTPMKMS_CreateKey_Capabilities (t * testing.T ) {
117
+ tpmWithNoCaps := newSimulatedTPM (t , withCapabilities (& tpmp.Capabilities {}))
118
+ type fields struct {
119
+ tpm * tpmp.TPM
120
+ }
121
+ type args struct {
122
+ req * apiv1.CreateKeyRequest
123
+ }
124
+ tests := []struct {
125
+ name string
126
+ fields fields
127
+ args args
128
+ assertFunc assert.ValueAssertionFunc
129
+ expErr error
130
+ }{
131
+ {
132
+ name : "fail/unsupported-algorithm" ,
133
+ fields : fields {
134
+ tpm : tpmWithNoCaps ,
135
+ },
136
+ args : args {
137
+ req : & apiv1.CreateKeyRequest {
138
+ Name : "tpmkms:name=key1" ,
139
+ SignatureAlgorithm : apiv1 .SHA256WithRSA ,
140
+ Bits : 2048 ,
141
+ },
142
+ },
143
+ assertFunc : func (tt assert.TestingT , i1 interface {}, i2 ... interface {}) bool {
144
+ if assert .IsType (t , & apiv1.CreateKeyResponse {}, i1 ) {
145
+ r , _ := i1 .(* apiv1.CreateKeyResponse )
146
+ return assert .Nil (t , r )
147
+ }
148
+ return false
149
+ },
150
+ expErr : errors .New (`signature algorithm "SHA256-RSA" not supported by the TPM device` ),
151
+ },
152
+ }
153
+ for _ , tt := range tests {
154
+ t .Run (tt .name , func (t * testing.T ) {
155
+ k := & TPMKMS {
156
+ tpm : tt .fields .tpm ,
157
+ }
158
+ got , err := k .CreateKey (tt .args .req )
159
+ if tt .expErr != nil {
160
+ assert .EqualError (t , err , tt .expErr .Error ())
161
+ return
162
+ }
163
+
164
+ assert .NoError (t , err )
165
+ assert .True (t , tt .assertFunc (t , got ))
166
+ })
167
+ }
168
+ }
169
+
96
170
func TestTPMKMS_CreateKey (t * testing.T ) {
97
171
tpmWithAK := newSimulatedTPM (t , withAK ("ak1" ))
98
172
type fields struct {
0 commit comments