From 9ec119e5d2fc0bf5a2223515f441e1e64ff19c99 Mon Sep 17 00:00:00 2001 From: Daisuke IIZUKA Date: Wed, 18 Jul 2018 06:25:44 +0000 Subject: [PATCH] [FAB-11217] Increase test coverage for signer.go Increase test coverage to 93.8%. And, fix error message to omit ":", because errors.Wrap() concatenates messages with ":". Change-Id: I37a21239286121638ccee1057aff2fffea8382f6 Signed-off-by: Daisuke IIZUKA --- cmd/common/signer/signer.go | 4 ++-- cmd/common/signer/signer_test.go | 16 +++++++++++++--- .../signer/testdata/signer/empty_private_key | 2 ++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 cmd/common/signer/testdata/signer/empty_private_key diff --git a/cmd/common/signer/signer.go b/cmd/common/signer/signer.go index ff330cb6b3d..710537104f9 100644 --- a/cmd/common/signer/signer.go +++ b/cmd/common/signer/signer.go @@ -79,11 +79,11 @@ func loadPrivateKey(file string) (*ecdsa.PrivateKey, error) { } bl, _ := pem.Decode(b) if bl == nil { - return nil, errors.Errorf("%s: wrong PEM encoding", file) + return nil, errors.Errorf("failed to decode PEM block from %s", file) } key, err := x509.ParsePKCS8PrivateKey(bl.Bytes) if err != nil { - return nil, errors.WithStack(err) + return nil, errors.Wrapf(err, "failed to parse private key from %s", file) } return key.(*ecdsa.PrivateKey), nil } diff --git a/cmd/common/signer/signer_test.go b/cmd/common/signer/signer_test.go index fb9bac5c569..962098b411d 100644 --- a/cmd/common/signer/signer_test.go +++ b/cmd/common/signer/signer_test.go @@ -41,7 +41,7 @@ func TestSignerBadConfig(t *testing.T) { } signer, err := NewSigner(conf) - assert.Contains(t, err.Error(), "open testdata/signer/non_existent_cert: no such file or directory") + assert.EqualError(t, err, "open testdata/signer/non_existent_cert: no such file or directory") assert.Nil(t, signer) conf = Config{ @@ -51,7 +51,7 @@ func TestSignerBadConfig(t *testing.T) { } signer, err = NewSigner(conf) - assert.Contains(t, err.Error(), "open testdata/signer/non_existent_cert: no such file or directory") + assert.EqualError(t, err, "open testdata/signer/non_existent_cert: no such file or directory") assert.Nil(t, signer) conf = Config{ @@ -61,6 +61,16 @@ func TestSignerBadConfig(t *testing.T) { } signer, err = NewSigner(conf) - assert.Contains(t, err.Error(), "testdata/signer/broken_private_key: wrong PEM encoding") + assert.EqualError(t, err, "failed to decode PEM block from testdata/signer/broken_private_key") + assert.Nil(t, signer) + + conf = Config{ + MSPID: "SampleOrg", + IdentityPath: filepath.Join("testdata", "signer", "cert.pem"), + KeyPath: filepath.Join("testdata", "signer", "empty_private_key"), + } + + signer, err = NewSigner(conf) + assert.EqualError(t, err, "failed to parse private key from testdata/signer/empty_private_key: asn1: syntax error: sequence truncated") assert.Nil(t, signer) } diff --git a/cmd/common/signer/testdata/signer/empty_private_key b/cmd/common/signer/testdata/signer/empty_private_key new file mode 100644 index 00000000000..f5518e2571b --- /dev/null +++ b/cmd/common/signer/testdata/signer/empty_private_key @@ -0,0 +1,2 @@ +-----BEGIN PRIVATE KEY----- +-----END PRIVATE KEY-----