Skip to content

Commit

Permalink
add test function for ParseRSAPrivateKey
Browse files Browse the repository at this point in the history
  • Loading branch information
shibumi committed Jun 26, 2020
1 parent f54d2e5 commit 849dddb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion in_toto/keylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func ParseRSAPrivateKeyFromPEM(pemBytes []byte) (*rsa.PrivateKey, error) {
// Should we handle it / fail / say something about it?
data, _ := pem.Decode(pemBytes)
if data == nil {
return nil, fmt.Errorf("Could not find a Private key PEM block")
return nil, fmt.Errorf("Could not find a private key PEM block")
}

priv, err := x509.ParsePKCS1PrivateKey(data.Bytes)
Expand Down
26 changes: 26 additions & 0 deletions in_toto/keylib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ yMxdI/24LUOOQ71cHW3ITIDImm6I8KmrXFM2NewTARKfAgMBAAE=
}
}

func TestParseRSAPrivateKeyFromPEM(t *testing.T) {
// Test parsing errors:
// - Missing pem headers,
// - Missing pem body
// We only support RSA private keys, therefore we don't need to check for other keys.
// Other keys should fail at ParsePKCS1 stage already.
invalidRSA := []string{
"not a PEM block",
`-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----`,
}
expectedErrors := []string{
"Could not find a private key PEM block",
"truncated",
}

for i := 0; i < len(invalidRSA); i++ {
result, err := ParseRSAPrivateKeyFromPEM([]byte(invalidRSA[i]))
if err == nil || !strings.Contains(err.Error(), expectedErrors[i]) {
t.Errorf("ParseRSAPrivateKeyFromPEM returned (%p, %s), expected '%s'"+
" error", result, err, expectedErrors[i])
}
}
}

func TestLoadRSAPublicKey(t *testing.T) {
// Test loading valid public rsa key from pem-formatted file
var key Key
Expand Down

0 comments on commit 849dddb

Please sign in to comment.