@@ -13,41 +13,40 @@ import (
1313"crypto/rsa"
1414)
1515
16- func TestECDSACreateKey (t * testing.T ) {
17- tpm ,err := simulator .OpenSimulator ()
18- if err != nil {
19- t .Fatal (err )
20- }
21- defer tpm .Close ()
22- k ,err := CreateKey (tpm ,tpm2 .TPMAlgECDSA , []byte ("" ))
23- if err != nil {
24- t .Fatalf ("failed key import: %v" ,err )
25- }
26-
27- // Test if we can load the key
28- // signer/signer_test.go tests the signing of the key
29- _ ,err = LoadKey (tpm ,k )
30- if err != nil {
31- t .Fatalf ("failed loading key: %v" ,err )
16+ func TestCreateKey (t * testing.T ) {
17+ cases := []struct {
18+ text string
19+ alg tpm2.TPMAlgID
20+ }{
21+ {
22+ text :"ecdsa" ,
23+ alg :tpm2 .TPMAlgECDSA ,
24+ },
25+ {
26+ text :"rsa" ,
27+ alg :tpm2 .TPMAlgRSA ,
28+ },
3229}
33- }
3430
35- func TestRSACreateKey (t * testing.T ) {
3631tpm ,err := simulator .OpenSimulator ()
3732if err != nil {
3833t .Fatal (err )
3934}
4035defer tpm .Close ()
41- k ,err := CreateKey (tpm ,tpm2 .TPMAlgRSA , []byte ("" ))
42- if err != nil {
43- t .Fatalf ("failed key import: %v" ,err )
44- }
4536
46- // Test if we can load the key
47- // signer/signer_test.go tests the signing of the key
48- _ ,err = LoadKey (tpm ,k )
49- if err != nil {
50- t .Fatalf ("failed loading key: %v" ,err )
37+ for _ ,c := range cases {
38+ t .Run (c .text ,func (t * testing.T ) {
39+ k ,err := CreateKey (tpm ,c .alg , []byte ("" ), []byte ("" ))
40+ if err != nil {
41+ t .Fatalf ("failed key import: %v" ,err )
42+ }
43+
44+ // Test if we can load the key
45+ // signer/signer_test.go tests the signing of the key
46+ if _ ,err = LoadKey (tpm ,k );err != nil {
47+ t .Fatalf ("failed loading key: %v" ,err )
48+ }
49+ })
5150}
5251}
5352
@@ -63,9 +62,11 @@ func mustPrivate(data []byte) tpm2.TPM2BPrivate {
6362
6463func TestMarshalling (t * testing.T ) {
6564cases := []struct {
66- k * Key
65+ text string
66+ k * Key
6767}{
6868{
69+ text :"ecdsa/haspin" ,
6970k :& Key {
7071Version :1 ,
7172PIN :HasPIN ,
@@ -75,6 +76,7 @@ func TestMarshalling(t *testing.T) {
7576},
7677},
7778{
79+ text :"ecdsa/nopin" ,
7880k :& Key {
7981Version :1 ,
8082PIN :NoPIN ,
@@ -84,6 +86,18 @@ func TestMarshalling(t *testing.T) {
8486},
8587},
8688{
89+ text :"ecdsa/comment" ,
90+ k :& Key {
91+ Version :1 ,
92+ PIN :HasPIN ,
93+ Type :tpm2 .TPMAlgECDSA ,
94+ Public :mustPublic ([]byte ("public" )),
95+ Private :mustPrivate ([]byte ("private" )),
96+ Comment : []byte ("This is a comment" ),
97+ },
98+ },
99+ {
100+ text :"rsa/haspin" ,
87101k :& Key {
88102Version :1 ,
89103PIN :HasPIN ,
@@ -93,6 +107,7 @@ func TestMarshalling(t *testing.T) {
93107},
94108},
95109{
110+ text :"rsa/nopin" ,
96111k :& Key {
97112Version :1 ,
98113PIN :NoPIN ,
@@ -101,18 +116,31 @@ func TestMarshalling(t *testing.T) {
101116Private :mustPrivate ([]byte ("private" )),
102117},
103118},
119+ {
120+ text :"rsa/comment" ,
121+ k :& Key {
122+ Version :1 ,
123+ PIN :HasPIN ,
124+ Type :tpm2 .TPMAlgRSA ,
125+ Public :mustPublic ([]byte ("public" )),
126+ Private :mustPrivate ([]byte ("private" )),
127+ Comment : []byte ("This is a comment" ),
128+ },
129+ },
104130}
105131
106132for _ ,c := range cases {
107- b := EncodeKey (c .k )
108- k ,err := DecodeKey (b )
109- if err != nil {
110- t .Fatalf ("test failed: %v" ,err )
111- }
112-
113- if ! reflect .DeepEqual (k ,c .k ) {
114- t .Fatalf ("keys are not the same" )
115- }
133+ t .Run (c .text ,func (t * testing.T ) {
134+ b := EncodeKey (c .k )
135+ k ,err := DecodeKey (b )
136+ if err != nil {
137+ t .Fatalf ("test failed: %v" ,err )
138+ }
139+
140+ if ! reflect .DeepEqual (k ,c .k ) {
141+ t .Fatalf ("keys are not the same" )
142+ }
143+ })
116144}
117145}
118146
@@ -127,7 +155,7 @@ func TestECDSAImportKey(t *testing.T) {
127155t .Fatal (err )
128156}
129157defer tpm .Close ()
130- k ,err := ImportKey (tpm ,* pk , []byte ("" ))
158+ k ,err := ImportKey (tpm ,* pk , []byte ("" ), [] byte ( "" ) )
131159if err != nil {
132160t .Fatalf ("failed key import: %v" ,err )
133161}
@@ -151,7 +179,7 @@ func TestRSAImportKey(t *testing.T) {
151179t .Fatal (err )
152180}
153181defer tpm .Close ()
154- k ,err := ImportKey (tpm ,* pk , []byte ("" ))
182+ k ,err := ImportKey (tpm ,* pk , []byte ("" ), [] byte ( "" ) )
155183if err != nil {
156184t .Fatalf ("failed key import: %v" ,err )
157185}