From 376e1090a3f15223fba5e99da2f9f4ce39bc8445 Mon Sep 17 00:00:00 2001 From: Tobias Weingartner Date: Sun, 17 Apr 2016 13:17:42 -0700 Subject: [PATCH] Fix PrivateKeyBytes to error out and fail tests on error. --- caddy/https/crypto_test.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/caddy/https/crypto_test.go b/caddy/https/crypto_test.go index d1facd44..936f7349 100644 --- a/caddy/https/crypto_test.go +++ b/caddy/https/crypto_test.go @@ -8,6 +8,7 @@ import ( "crypto/rand" "crypto/rsa" "crypto/x509" + "errors" "os" "runtime" "testing" @@ -95,22 +96,25 @@ func TestSaveAndLoadECCPrivateKey(t *testing.T) { // PrivateKeysSame compares the bytes of a and b and returns true if they are the same. func PrivateKeysSame(a, b crypto.PrivateKey) bool { - return bytes.Equal(PrivateKeyBytes(a), PrivateKeyBytes(b)) + var abytes, bbytes []byte + var err error + + if abytes, err = PrivateKeyBytes(a); err != nil { + return false + } + if bbytes, err = PrivateKeyBytes(b); err != nil { + return false + } + return bytes.Equal(abytes, bbytes) } // PrivateKeyBytes returns the bytes of DER-encoded key. -func PrivateKeyBytes(key crypto.PrivateKey) []byte { - var keyBytes []byte +func PrivateKeyBytes(key crypto.PrivateKey) ([]byte, error) { switch key := key.(type) { case *rsa.PrivateKey: - keyBytes = x509.MarshalPKCS1PrivateKey(key) + return x509.MarshalPKCS1PrivateKey(key), nil case *ecdsa.PrivateKey: - var err error - var t *testing.T - keyBytes, err = x509.MarshalECPrivateKey(key) - if err != nil { - t.Error(err) - } + return x509.MarshalECPrivateKey(key) } - return keyBytes + return nil, errors.New("Bad juju") }