go.crypto/ssh: some cleanup
Simplify MarshalAuthorizedKey by using the algoName func. Make the algoName func be very specific about supported key types in openssh certs. Generalize some of the commentary that previously mentioned specific key types. R=agl, dave CC=golang-dev https://golang.org/cl/6938067
This commit is contained in:
committed by
Adam Langley
parent
2da167fbbe
commit
d95b28330d
@@ -5,6 +5,11 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"crypto/dsa"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rsa"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -24,3 +29,49 @@ func TestSafeString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlgoNameSupported(t *testing.T) {
|
||||
supported := map[string]interface{}{
|
||||
KeyAlgoRSA: new(rsa.PublicKey),
|
||||
KeyAlgoDSA: new(dsa.PublicKey),
|
||||
KeyAlgoECDSA256: &ecdsa.PublicKey{Curve: elliptic.P256()},
|
||||
KeyAlgoECDSA384: &ecdsa.PublicKey{Curve: elliptic.P384()},
|
||||
KeyAlgoECDSA521: &ecdsa.PublicKey{Curve: elliptic.P521()},
|
||||
CertAlgoRSAv01: &OpenSSHCertV01{Key: new(rsa.PublicKey)},
|
||||
CertAlgoDSAv01: &OpenSSHCertV01{Key: new(dsa.PublicKey)},
|
||||
CertAlgoECDSA256v01: &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P256()}},
|
||||
CertAlgoECDSA384v01: &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P384()}},
|
||||
CertAlgoECDSA521v01: &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P521()}},
|
||||
}
|
||||
|
||||
for expected, key := range supported {
|
||||
actual := algoName(key)
|
||||
if expected != actual {
|
||||
t.Errorf("expected: %s, actual: %s", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestAlgoNameNotSupported(t *testing.T) {
|
||||
notSupported := []interface{}{
|
||||
&ecdsa.PublicKey{Curve: elliptic.P224()},
|
||||
&OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P224()}},
|
||||
}
|
||||
|
||||
panicTest := func(key interface{}) (algo string, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = errors.New(r.(string))
|
||||
}
|
||||
}()
|
||||
algo = algoName(key)
|
||||
return
|
||||
}
|
||||
|
||||
for _, unsupportedKey := range notSupported {
|
||||
if algo, err := panicTest(unsupportedKey); err == nil {
|
||||
t.Errorf("Expected a panic, Got: %s (for type %T)", algo, unsupportedKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user