Skip to content

Commit 9a33854

Browse files
committed
[FAB-3441] bccsp/signer test coverage
This change-set improves the test coverage of bccsp/signer to 100% Change-Id: I8f0e49ee4e47538b6ce156ca228bad2779c72de0 Signed-off-by: Angelo De Caro <[email protected]>
1 parent c5c4c51 commit 9a33854

File tree

8 files changed

+203
-129
lines changed

8 files changed

+203
-129
lines changed

bccsp/mocks/mocks.go

+90-7
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,109 @@ limitations under the License.
1616

1717
package mocks
1818

19-
import "github.com/hyperledger/fabric/bccsp"
19+
import (
20+
"crypto"
21+
"errors"
22+
"hash"
23+
"reflect"
2024

21-
type MockKey struct{}
25+
"github.com/hyperledger/fabric/bccsp"
26+
)
2227

23-
func (*MockKey) Bytes() ([]byte, error) {
28+
type MockBCCSP struct {
29+
SignArgKey bccsp.Key
30+
SignDigestArg []byte
31+
SignOptsArg bccsp.SignerOpts
32+
33+
SignValue []byte
34+
SignErr error
35+
36+
VerifyValue bool
37+
VerifyErr error
38+
}
39+
40+
func (*MockBCCSP) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
2441
panic("implement me")
2542
}
2643

27-
func (*MockKey) SKI() []byte {
44+
func (*MockBCCSP) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
2845
panic("implement me")
2946
}
3047

31-
func (*MockKey) Symmetric() bool {
48+
func (*MockBCCSP) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
3249
panic("implement me")
3350
}
3451

35-
func (*MockKey) Private() bool {
52+
func (*MockBCCSP) GetKey(ski []byte) (k bccsp.Key, err error) {
53+
panic("implement me")
54+
}
55+
56+
func (*MockBCCSP) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error) {
57+
panic("implement me")
58+
}
59+
60+
func (*MockBCCSP) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
61+
panic("implement me")
62+
}
63+
64+
func (b *MockBCCSP) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
65+
if !reflect.DeepEqual(b.SignArgKey, k) {
66+
return nil, errors.New("invalid key")
67+
}
68+
if !reflect.DeepEqual(b.SignDigestArg, digest) {
69+
return nil, errors.New("invalid digest")
70+
}
71+
if !reflect.DeepEqual(b.SignOptsArg, opts) {
72+
return nil, errors.New("invalid opts")
73+
}
74+
75+
return b.SignValue, b.SignErr
76+
}
77+
78+
func (b *MockBCCSP) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
79+
return b.VerifyValue, b.VerifyErr
80+
}
81+
82+
func (*MockBCCSP) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) {
3683
panic("implement me")
3784
}
3885

39-
func (*MockKey) PublicKey() (bccsp.Key, error) {
86+
func (*MockBCCSP) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
4087
panic("implement me")
4188
}
89+
90+
type MockKey struct {
91+
BytesValue []byte
92+
BytesErr error
93+
Symm bool
94+
PK bccsp.Key
95+
PKErr error
96+
}
97+
98+
func (m *MockKey) Bytes() ([]byte, error) {
99+
return m.BytesValue, m.BytesErr
100+
}
101+
102+
func (*MockKey) SKI() []byte {
103+
panic("implement me")
104+
}
105+
106+
func (m *MockKey) Symmetric() bool {
107+
return m.Symm
108+
}
109+
110+
func (*MockKey) Private() bool {
111+
panic("implement me")
112+
}
113+
114+
func (m *MockKey) PublicKey() (bccsp.Key, error) {
115+
return m.PK, m.PKErr
116+
}
117+
118+
type SignerOpts struct {
119+
HashFuncValue crypto.Hash
120+
}
121+
122+
func (o *SignerOpts) HashFunc() crypto.Hash {
123+
return o.HashFuncValue
124+
}

bccsp/pkcs11/impl_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,7 @@ func TestKeyImportFromX509ECDSAPublicKey(t *testing.T) {
10041004
},
10051005
}
10061006

1007-
cryptoSigner := &signer.CryptoSigner{}
1008-
err = cryptoSigner.Init(currentBCCSP, k)
1007+
cryptoSigner, err := signer.New(currentBCCSP, k)
10091008
if err != nil {
10101009
t.Fatalf("Failed initializing CyrptoSigner [%s]", err)
10111010
}
@@ -1815,8 +1814,7 @@ func TestKeyImportFromX509RSAPublicKey(t *testing.T) {
18151814
},
18161815
}
18171816

1818-
cryptoSigner := &signer.CryptoSigner{}
1819-
err = cryptoSigner.Init(currentBCCSP, k)
1817+
cryptoSigner, err := signer.New(currentBCCSP, k)
18201818
if err != nil {
18211819
t.Fatalf("Failed initializing CyrptoSigner [%s]", err)
18221820
}

bccsp/signer/signer.go

+15-29
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,49 @@ import (
2525
"github.com/hyperledger/fabric/bccsp/utils"
2626
)
2727

28-
// CryptoSigner is the BCCSP-based implementation of a crypto.Signer
29-
type CryptoSigner struct {
28+
// bccspCryptoSigner is the BCCSP-based implementation of a crypto.Signer
29+
type bccspCryptoSigner struct {
3030
csp bccsp.BCCSP
3131
key bccsp.Key
3232
pk interface{}
3333
}
3434

35-
// Init initializes this CryptoSigner.
36-
func (s *CryptoSigner) Init(csp bccsp.BCCSP, key bccsp.Key) error {
35+
// New returns a new BCCSP-based crypto.Signer
36+
// for the given BCCSP instance and key.
37+
func New(csp bccsp.BCCSP, key bccsp.Key) (crypto.Signer, error) {
3738
// Validate arguments
3839
if csp == nil {
39-
return errors.New("Invalid BCCSP. Nil.")
40+
return nil, errors.New("bccsp instance must be different from nil.")
4041
}
4142
if key == nil {
42-
return errors.New("Invalid Key. Nil.")
43+
return nil, errors.New("key must be different from nil.")
4344
}
4445
if key.Symmetric() {
45-
return errors.New("Invalid Key. Symmetric.")
46+
return nil, errors.New("key must be asymmetric.")
4647
}
4748

4849
// Marshall the bccsp public key as a crypto.PublicKey
4950
pub, err := key.PublicKey()
5051
if err != nil {
51-
return fmt.Errorf("Failed getting public key [%s]", err)
52+
return nil, fmt.Errorf("failed getting public key [%s]", err)
5253
}
5354

5455
raw, err := pub.Bytes()
5556
if err != nil {
56-
return fmt.Errorf("Failed marshalling public key [%s]", err)
57+
return nil, fmt.Errorf("failed marshalling public key [%s]", err)
5758
}
5859

5960
pk, err := utils.DERToPublicKey(raw)
6061
if err != nil {
61-
return fmt.Errorf("Failed marshalling public key [%s]", err)
62+
return nil, fmt.Errorf("failed marshalling der to public key [%s]", err)
6263
}
6364

64-
// Init fields
65-
s.csp = csp
66-
s.key = key
67-
s.pk = pk
68-
69-
return nil
70-
65+
return &bccspCryptoSigner{csp, key, pk}, nil
7166
}
7267

7368
// Public returns the public key corresponding to the opaque,
7469
// private key.
75-
func (s *CryptoSigner) Public() crypto.PublicKey {
70+
func (s *bccspCryptoSigner) Public() crypto.PublicKey {
7671
return s.pk
7772
}
7873

@@ -89,15 +84,6 @@ func (s *CryptoSigner) Public() crypto.PublicKey {
8984
// Note that when a signature of a hash of a larger message is needed,
9085
// the caller is responsible for hashing the larger message and passing
9186
// the hash (as digest) and the hash function (as opts) to Sign.
92-
func (s *CryptoSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
93-
if opts == nil {
94-
return s.csp.Sign(s.key, digest, nil)
95-
}
96-
97-
so, ok := opts.(bccsp.SignerOpts)
98-
if !ok {
99-
return nil, errors.New("Invalid opts type. Expecting bccsp.SignerOpts")
100-
}
101-
102-
return s.csp.Sign(s.key, digest, so)
87+
func (s *bccspCryptoSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
88+
return s.csp.Sign(s.key, digest, opts)
10389
}

0 commit comments

Comments
 (0)