Skip to content

Commit

Permalink
key_test: Add tests for comments
Browse files Browse the repository at this point in the history
Signed-off-by: Morten Linderud <morten@linderud.pw>
  • Loading branch information
Foxboron committed Aug 6, 2023
1 parent 1708452 commit f89b613
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ func mustPrivate(data []byte) tpm2.TPM2BPrivate {

func TestMarshalling(t *testing.T) {
cases := []struct {
k *Key
text string
k *Key
}{
{
text: "ecdsa/haspin",
k: &Key{
Version: 1,
PIN: HasPIN,
Expand All @@ -69,6 +71,7 @@ func TestMarshalling(t *testing.T) {
},
},
{
text: "ecdsa/nopin",
k: &Key{
Version: 1,
PIN: NoPIN,
Expand All @@ -78,6 +81,18 @@ func TestMarshalling(t *testing.T) {
},
},
{
text: "ecdsa/comment",
k: &Key{
Version: 1,
PIN: HasPIN,
Type: tpm2.TPMAlgECDSA,
Public: mustPublic([]byte("public")),
Private: mustPrivate([]byte("private")),
Comment: []byte("This is a comment"),
},
},
{
text: "rsa/haspin",
k: &Key{
Version: 1,
PIN: HasPIN,
Expand All @@ -87,6 +102,7 @@ func TestMarshalling(t *testing.T) {
},
},
{
text: "rsa/nopin",
k: &Key{
Version: 1,
PIN: NoPIN,
Expand All @@ -95,17 +111,30 @@ func TestMarshalling(t *testing.T) {
Private: mustPrivate([]byte("private")),
},
},
{
text: "rsa/comment",
k: &Key{
Version: 1,
PIN: HasPIN,
Type: tpm2.TPMAlgRSA,
Public: mustPublic([]byte("public")),
Private: mustPrivate([]byte("private")),
Comment: []byte("This is a comment"),
},
},
}

for _, c := range cases {
b := EncodeKey(c.k)
k, err := DecodeKey(b)
if err != nil {
t.Fatalf("test failed: %v", err)
}
t.Run(c.text, func(t *testing.T) {
b := EncodeKey(c.k)
k, err := DecodeKey(b)
if err != nil {
t.Fatalf("test failed: %v", err)
}

if !reflect.DeepEqual(k, c.k) {
t.Fatalf("keys are not the same")
}
if !reflect.DeepEqual(k, c.k) {
t.Fatalf("keys are not the same")
}
})
}
}

0 comments on commit f89b613

Please sign in to comment.