github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/signer/signer_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package signer 17 18 import ( 19 "crypto/ecdsa" 20 "crypto/elliptic" 21 "crypto/rand" 22 "errors" 23 "testing" 24 25 "github.com/hyperledger/fabric/bccsp/mocks" 26 "github.com/hyperledger/fabric/bccsp/utils" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestInitFailures(t *testing.T) { 31 _, err := New(nil, &mocks.MockKey{}) 32 assert.Error(t, err) 33 34 _, err = New(&mocks.MockBCCSP{}, nil) 35 assert.Error(t, err) 36 37 _, err = New(&mocks.MockBCCSP{}, &mocks.MockKey{Symm: true}) 38 assert.Error(t, err) 39 40 _, err = New(&mocks.MockBCCSP{}, &mocks.MockKey{PKErr: errors.New("No PK")}) 41 assert.Error(t, err) 42 assert.Equal(t, "failed getting public key: No PK", err.Error()) 43 44 _, err = New(&mocks.MockBCCSP{}, &mocks.MockKey{PK: &mocks.MockKey{BytesErr: errors.New("No bytes")}}) 45 assert.Error(t, err) 46 assert.Equal(t, "failed marshalling public key: No bytes", err.Error()) 47 48 _, err = New(&mocks.MockBCCSP{}, &mocks.MockKey{PK: &mocks.MockKey{BytesValue: []byte{0, 1, 2, 3}}}) 49 assert.Error(t, err) 50 } 51 52 func TestInit(t *testing.T) { 53 k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 54 assert.NoError(t, err) 55 pkRaw, err := utils.PublicKeyToDER(&k.PublicKey) 56 assert.NoError(t, err) 57 58 signer, err := New(&mocks.MockBCCSP{}, &mocks.MockKey{PK: &mocks.MockKey{BytesValue: pkRaw}}) 59 assert.NoError(t, err) 60 assert.NotNil(t, signer) 61 62 // Test public key 63 R, S, err := ecdsa.Sign(rand.Reader, k, []byte{0, 1, 2, 3}) 64 assert.NoError(t, err) 65 66 assert.True(t, ecdsa.Verify(signer.Public().(*ecdsa.PublicKey), []byte{0, 1, 2, 3}, R, S)) 67 } 68 69 func TestPublic(t *testing.T) { 70 pk := &mocks.MockKey{} 71 signer := &bccspCryptoSigner{pk: pk} 72 73 pk2 := signer.Public() 74 assert.NotNil(t, pk, pk2) 75 } 76 77 func TestSign(t *testing.T) { 78 expectedSig := []byte{0, 1, 2, 3, 4} 79 expectedKey := &mocks.MockKey{} 80 expectedDigest := []byte{0, 1, 2, 3, 4, 5} 81 expectedOpts := &mocks.SignerOpts{} 82 83 signer := &bccspCryptoSigner{ 84 key: expectedKey, 85 csp: &mocks.MockBCCSP{ 86 SignArgKey: expectedKey, SignDigestArg: expectedDigest, SignOptsArg: expectedOpts, 87 SignValue: expectedSig}} 88 signature, err := signer.Sign(nil, expectedDigest, expectedOpts) 89 assert.NoError(t, err) 90 assert.Equal(t, expectedSig, signature) 91 92 signer = &bccspCryptoSigner{ 93 key: expectedKey, 94 csp: &mocks.MockBCCSP{ 95 SignArgKey: expectedKey, SignDigestArg: expectedDigest, SignOptsArg: expectedOpts, 96 SignErr: errors.New("no signature")}} 97 _, err = signer.Sign(nil, expectedDigest, expectedOpts) 98 assert.Error(t, err) 99 assert.Equal(t, err.Error(), "no signature") 100 101 signer = &bccspCryptoSigner{ 102 key: nil, 103 csp: &mocks.MockBCCSP{SignArgKey: expectedKey, SignDigestArg: expectedDigest, SignOptsArg: expectedOpts}} 104 _, err = signer.Sign(nil, expectedDigest, expectedOpts) 105 assert.Error(t, err) 106 assert.Equal(t, err.Error(), "invalid key") 107 108 signer = &bccspCryptoSigner{ 109 key: expectedKey, 110 csp: &mocks.MockBCCSP{SignArgKey: expectedKey, SignDigestArg: expectedDigest, SignOptsArg: expectedOpts}} 111 _, err = signer.Sign(nil, nil, expectedOpts) 112 assert.Error(t, err) 113 assert.Equal(t, err.Error(), "invalid digest") 114 115 signer = &bccspCryptoSigner{ 116 key: expectedKey, 117 csp: &mocks.MockBCCSP{SignArgKey: expectedKey, SignDigestArg: expectedDigest, SignOptsArg: expectedOpts}} 118 _, err = signer.Sign(nil, expectedDigest, nil) 119 assert.Error(t, err) 120 assert.Equal(t, err.Error(), "invalid opts") 121 }