Skip to content

Commit

Permalink
[FAB-11217] Increase test coverage for signer.go
Browse files Browse the repository at this point in the history
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 <daisuke.iizuka.ag@hitachi.com>
  • Loading branch information
Daisuke IIZUKA authored and denyeart committed Jul 29, 2018
1 parent 7134f6e commit 9ec119e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/common/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 13 additions & 3 deletions cmd/common/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions cmd/common/signer/testdata/signer/empty_private_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----

0 comments on commit 9ec119e

Please sign in to comment.