From 1482919d15c7cbc3c48db6132a5b30d1a9c79bcc Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Sat, 5 Jul 2014 15:58:07 -0700 Subject: [PATCH] unit tests for parsePublicKey --- rs256.go | 4 ++-- rs256_test.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/rs256.go b/rs256.go index 33b26cc..c4ec6c1 100644 --- a/rs256.go +++ b/rs256.go @@ -83,9 +83,9 @@ func (m *SigningMethodRS256) parsePublicKey(key []byte) (*rsa.PublicKey, error) var parsedKey interface{} if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil { if cert, err := x509.ParseCertificate(block.Bytes); err == nil { - return nil, err - } else { parsedKey = cert.PublicKey + } else { + return nil, err } } diff --git a/rs256_test.go b/rs256_test.go index a0fbd5a..f2de1e0 100644 --- a/rs256_test.go +++ b/rs256_test.go @@ -67,6 +67,7 @@ func TestRSAKeyParsing(t *testing.T) { badKey := []byte("All your base are belong to key") method := GetSigningMethod("RS256").(*SigningMethodRS256) + // Test parsePrivateKey if _, e := method.parsePrivateKey(key); e != nil { t.Errorf("Failed to parse valid private key: %v", e) } @@ -78,4 +79,18 @@ func TestRSAKeyParsing(t *testing.T) { if k, e := method.parsePrivateKey(badKey); e == nil { t.Errorf("Parsed invalid key as valid private key: %v", k) } + + // Test parsePublicKey + if _, e := method.parsePublicKey(pubKey); e != nil { + t.Errorf("Failed to parse valid public key: %v", e) + } + + if k, e := method.parsePublicKey(key); e == nil { + t.Errorf("Parsed private key as valid public key: %v", k) + } + + if k, e := method.parsePublicKey(badKey); e == nil { + t.Errorf("Parsed invalid key as valid private key: %v", k) + } + }