unit tests for parsePublicKey

This commit is contained in:
Dave Grijalva 2014-07-05 15:58:07 -07:00
parent 37329b525d
commit 1482919d15
2 changed files with 17 additions and 2 deletions

View File

@ -83,9 +83,9 @@ func (m *SigningMethodRS256) parsePublicKey(key []byte) (*rsa.PublicKey, error)
var parsedKey interface{} var parsedKey interface{}
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil { if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil { if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
return nil, err
} else {
parsedKey = cert.PublicKey parsedKey = cert.PublicKey
} else {
return nil, err
} }
} }

View File

@ -67,6 +67,7 @@ func TestRSAKeyParsing(t *testing.T) {
badKey := []byte("All your base are belong to key") badKey := []byte("All your base are belong to key")
method := GetSigningMethod("RS256").(*SigningMethodRS256) method := GetSigningMethod("RS256").(*SigningMethodRS256)
// Test parsePrivateKey
if _, e := method.parsePrivateKey(key); e != nil { if _, e := method.parsePrivateKey(key); e != nil {
t.Errorf("Failed to parse valid private key: %v", e) 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 { if k, e := method.parsePrivateKey(badKey); e == nil {
t.Errorf("Parsed invalid key as valid private key: %v", k) 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)
}
} }