Skip to content

Commit 3f1a5d6

Browse files
committed
Refactor tpmkms simulator tests.
1 parent d378c7d commit 3f1a5d6

File tree

1 file changed

+11
-68
lines changed

1 file changed

+11
-68
lines changed

kms/tpmkms/tpmkms_simulator_test.go

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ func withKey(name string) newSimulatedTPMOption {
5959
}
6060
}
6161

62-
func newSimulatedTPM(t *testing.T, caps *tpmp.Capabilities, opts ...newSimulatedTPMOption) *tpmp.TPM {
62+
func newSimulatedTPM(t *testing.T, opts ...newSimulatedTPMOption) *tpmp.TPM {
6363
t.Helper()
6464
tmpDir := t.TempDir()
6565
tpmOpts := []tpmp.NewTPMOption{
6666
withSimulator(t),
6767
tpmp.WithStore(storage.NewDirstore(tmpDir)),
6868
}
6969

70-
if caps != nil {
71-
tpmOpts = append(tpmOpts, tpmp.WithCapabilities(caps))
72-
}
7370
tpm, err := tpmp.New(tpmOpts...)
7471

7572
require.NoError(t, err)
@@ -97,7 +94,7 @@ func withSimulator(t *testing.T) tpmp.NewTPMOption {
9794
}
9895

9996
func TestTPMKMS_CreateKey(t *testing.T) {
100-
tpmWithAK := newSimulatedTPM(t, nil, withAK("ak1"))
97+
tpmWithAK := newSimulatedTPM(t, withAK("ak1"))
10198
type fields struct {
10299
tpm *tpmp.TPM
103100
}
@@ -442,62 +439,8 @@ func TestTPMKMS_CreateKey(t *testing.T) {
442439
}
443440
}
444441

445-
func TestTPMKMS_CreateKey_Capabilities(t *testing.T) {
446-
tpmWithNoCaps := newSimulatedTPM(t, &tpmp.Capabilities{}, withAK("ak1"))
447-
type fields struct {
448-
tpm *tpmp.TPM
449-
}
450-
type args struct {
451-
req *apiv1.CreateKeyRequest
452-
}
453-
tests := []struct {
454-
name string
455-
fields fields
456-
args args
457-
assertFunc assert.ValueAssertionFunc
458-
expErr error
459-
}{
460-
{
461-
name: "fail/unsupported-algorithm",
462-
fields: fields{
463-
tpm: tpmWithNoCaps,
464-
},
465-
args: args{
466-
req: &apiv1.CreateKeyRequest{
467-
Name: "tpmkms:name=key1",
468-
SignatureAlgorithm: apiv1.SHA256WithRSA,
469-
Bits: 2048,
470-
},
471-
},
472-
assertFunc: func(tt assert.TestingT, i1 interface{}, i2 ...interface{}) bool {
473-
if assert.IsType(t, &apiv1.CreateKeyResponse{}, i1) {
474-
r, _ := i1.(*apiv1.CreateKeyResponse)
475-
return assert.Nil(t, r)
476-
}
477-
return false
478-
},
479-
expErr: errors.New(`signature algorithm "SHA256-RSA" not supported by the TPM device`),
480-
},
481-
}
482-
for _, tt := range tests {
483-
t.Run(tt.name, func(t *testing.T) {
484-
k := &TPMKMS{
485-
tpm: tt.fields.tpm,
486-
}
487-
got, err := k.CreateKey(tt.args.req)
488-
if tt.expErr != nil {
489-
assert.EqualError(t, err, tt.expErr.Error())
490-
return
491-
}
492-
493-
assert.NoError(t, err)
494-
assert.True(t, tt.assertFunc(t, got))
495-
})
496-
}
497-
}
498-
499442
func TestTPMKMS_DeleteKey(t *testing.T) {
500-
okTPM := newSimulatedTPM(t, nil,
443+
okTPM := newSimulatedTPM(t,
501444
withAK("ak1"), withAK("ak2"),
502445
withKey("key1"), withKey("key2"),
503446
)
@@ -564,7 +507,7 @@ func TestTPMKMS_DeleteKey(t *testing.T) {
564507
}
565508

566509
func TestTPMKMS_CreateSigner(t *testing.T) {
567-
tpmWithKey := newSimulatedTPM(t, nil, withKey("key1"))
510+
tpmWithKey := newSimulatedTPM(t, withKey("key1"))
568511

569512
key, err := tpmWithKey.GetKey(context.Background(), "key1")
570513
require.NoError(t, err)
@@ -725,7 +668,7 @@ func TestTPMKMS_CreateSigner(t *testing.T) {
725668
}
726669

727670
func TestTPMKMS_GetPublicKey(t *testing.T) {
728-
tpmWithKey := newSimulatedTPM(t, nil, withKey("key1"))
671+
tpmWithKey := newSimulatedTPM(t, withKey("key1"))
729672
_, err := tpmWithKey.CreateAK(context.Background(), "ak1")
730673
require.NoError(t, err)
731674
type fields struct {
@@ -866,7 +809,7 @@ func TestTPMKMS_GetPublicKey(t *testing.T) {
866809

867810
func TestTPMKMS_LoadCertificate(t *testing.T) {
868811
ctx := context.Background()
869-
tpm := newSimulatedTPM(t, nil)
812+
tpm := newSimulatedTPM(t)
870813
config := tpmp.CreateKeyConfig{
871814
Algorithm: "RSA",
872815
Size: 1024,
@@ -1031,7 +974,7 @@ func TestTPMKMS_LoadCertificate(t *testing.T) {
1031974

1032975
func TestTPMKMS_LoadCertificateChain(t *testing.T) {
1033976
ctx := context.Background()
1034-
tpm := newSimulatedTPM(t, nil)
977+
tpm := newSimulatedTPM(t)
1035978
config := tpmp.CreateKeyConfig{
1036979
Algorithm: "RSA",
1037980
Size: 1024,
@@ -1202,7 +1145,7 @@ func TestTPMKMS_LoadCertificateChain(t *testing.T) {
12021145

12031146
func TestTPMKMS_StoreCertificate(t *testing.T) {
12041147
ctx := context.Background()
1205-
tpm := newSimulatedTPM(t, nil)
1148+
tpm := newSimulatedTPM(t)
12061149
config := tpmp.CreateKeyConfig{
12071150
Algorithm: "RSA",
12081151
Size: 1024,
@@ -1372,7 +1315,7 @@ func TestTPMKMS_StoreCertificate(t *testing.T) {
13721315

13731316
func TestTPMKMS_StoreCertificateChain(t *testing.T) {
13741317
ctx := context.Background()
1375-
tpm := newSimulatedTPM(t, nil)
1318+
tpm := newSimulatedTPM(t)
13761319
config := tpmp.CreateKeyConfig{
13771320
Algorithm: "RSA",
13781321
Size: 1024,
@@ -1601,7 +1544,7 @@ func (c *customAttestationClient) Attest(context.Context) ([]*x509.Certificate,
16011544

16021545
func TestTPMKMS_CreateAttestation(t *testing.T) {
16031546
ctx := context.Background()
1604-
tpm := newSimulatedTPM(t, nil)
1547+
tpm := newSimulatedTPM(t)
16051548
eks, err := tpm.GetEKs(ctx)
16061549
require.NoError(t, err)
16071550
ek := getPreferredEK(eks)
@@ -2192,7 +2135,7 @@ func Test_hasValidIdentity(t *testing.T) {
21922135
identityRenewalPeriodPercentage: 60,
21932136
}
21942137
ctx := context.Background()
2195-
tpm := newSimulatedTPM(t, nil)
2138+
tpm := newSimulatedTPM(t)
21962139
eks, err := tpm.GetEKs(ctx)
21972140
require.NoError(t, err)
21982141
ek := getPreferredEK(eks)

0 commit comments

Comments
 (0)