Added 'Description()' to 'sshKeyPair'.

Also cleaned up tests.
This commit is contained in:
Stephen Fox 2019-02-03 09:17:18 -05:00
parent f233e54992
commit ad075ffac3
2 changed files with 68 additions and 30 deletions

View File

@ -90,6 +90,10 @@ type sshKeyPair interface {
// Bits returns the bits of entropy.
Bits() int
// Description returns a brief description of the key pair that
// is suitable for log messages or printing.
Description() string
// PrivateKeyPemBlock returns a slice of bytes representing
// the private key in ASN.1, DER format in a PEM block.
PrivateKeyPemBlock() []byte
@ -123,6 +127,10 @@ func (o defaultSshKeyPair) Bits() int {
return o.bits
}
func (o defaultSshKeyPair) Description() string {
return o.kind.String() + " " + strconv.Itoa(o.bits)
}
func (o defaultSshKeyPair) PrivateKeyPemBlock() []byte {
t := "UNKNOWN PRIVATE KEY"

View File

@ -3,27 +3,67 @@ package common
import (
"crypto/rand"
"errors"
"strconv"
"testing"
"golang.org/x/crypto/ssh"
)
// expected contains the data that the key pair should contain.
type expected struct {
kind sshKeyPairType
bits int
desc string
}
func (o expected) matches(kp sshKeyPair) error {
if o.kind.String() == "" {
return errors.New("expected kind's value cannot be empty")
}
if o.bits <= 0 {
return errors.New("expected bits' value cannot be less than or equal to 0")
}
if o.desc == "" {
return errors.New("expected description's value cannot be empty")
}
if kp.Type() != o.kind {
return errors.New("expected key pair type to be " +
o.kind.String() + " - got '" + kp.Type().String() + "'")
}
if kp.Bits() != o.bits {
return errors.New("expected key pair to be " +
strconv.Itoa(o.bits) + " bits - got " + strconv.Itoa(kp.Bits()))
}
expDescription := kp.Type().String() + " " + strconv.Itoa(o.bits)
if kp.Description() != expDescription {
return errors.New("expected key pair description to be '" +
expDescription + "' - got '" + kp.Description() + "'")
}
err := verifySshKeyPair(kp)
if err != nil {
return err
}
return nil
}
func TestDefaultSshKeyPairBuilder_Build_Default(t *testing.T) {
kp, err := newSshKeyPairBuilder().Build()
if err != nil {
t.Fatal(err.Error())
}
if kp.Type() != ecdsaSsh {
t.Fatal("Expected key pair type to be",
ecdsaSsh.String(), "- got", kp.Type())
}
if kp.Bits() != 521 {
t.Fatal("Expected key pair to be 521 bits - got", kp.Bits())
}
err = verifySshKeyPair(kp)
err = expected{
kind: ecdsaSsh,
bits: 521,
desc: "ecdsa 521",
}.matches(kp)
if err != nil {
t.Fatal(err.Error())
}
@ -35,16 +75,11 @@ func TestDefaultSshKeyPairBuilder_Build_EcdsaDefault(t *testing.T) {
t.Fatal(err.Error())
}
if kp.Type() != ecdsaSsh {
t.Fatal("Expected key pair type to be",
ecdsaSsh.String(), "- got", kp.Type())
}
if kp.Bits() != 521 {
t.Fatal("Expected key pair to be 521 bits - got", kp.Bits())
}
err = verifySshKeyPair(kp)
err = expected{
kind: ecdsaSsh,
bits: 521,
desc: "ecdsa 521",
}.matches(kp)
if err != nil {
t.Fatal(err.Error())
}
@ -56,16 +91,11 @@ func TestDefaultSshKeyPairBuilder_Build_RsaDefault(t *testing.T) {
t.Fatal(err.Error())
}
if kp.Type() != rsaSsh {
t.Fatal("Expected default key pair type to be",
rsaSsh.String(), "- got", kp.Type())
}
if kp.Bits() != 4096 {
t.Fatal("Expected key pair to be", 4096, "bits - got", kp.Bits())
}
err = verifySshKeyPair(kp)
err = expected{
kind: rsaSsh,
bits: 4096,
desc: "rsa 4096",
}.matches(kp)
if err != nil {
t.Fatal(err.Error())
}