From ad075ffac39b69373aa3e756a0c68ded7c9cec70 Mon Sep 17 00:00:00 2001 From: Stephen Fox Date: Sun, 3 Feb 2019 09:17:18 -0500 Subject: [PATCH] Added 'Description()' to 'sshKeyPair'. Also cleaned up tests. --- builder/virtualbox/common/sshkeypair.go | 8 ++ builder/virtualbox/common/sshkeypair_test.go | 90 +++++++++++++------- 2 files changed, 68 insertions(+), 30 deletions(-) diff --git a/builder/virtualbox/common/sshkeypair.go b/builder/virtualbox/common/sshkeypair.go index dd926b9bc..8e323aee9 100644 --- a/builder/virtualbox/common/sshkeypair.go +++ b/builder/virtualbox/common/sshkeypair.go @@ -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" diff --git a/builder/virtualbox/common/sshkeypair_test.go b/builder/virtualbox/common/sshkeypair_test.go index 82cae75b8..b92fa385f 100644 --- a/builder/virtualbox/common/sshkeypair_test.go +++ b/builder/virtualbox/common/sshkeypair_test.go @@ -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()) }