github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/handlers/endorsement/plugin/plugin_test.go (about) 1 /* 2 Copyright Hitachi, Ltd. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package main_test 8 9 import ( 10 "errors" 11 "testing" 12 13 "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/hyperledger/fabric/core/endorser/mocks" 15 mocks2 "github.com/hyperledger/fabric/core/handlers/endorsement/builtin/mocks" 16 plgn "github.com/hyperledger/fabric/core/handlers/endorsement/plugin" 17 "github.com/stretchr/testify/assert" 18 "github.com/stretchr/testify/mock" 19 ) 20 21 func TestEndorsementPlugin(t *testing.T) { 22 factory := plgn.NewPluginFactory() 23 plugin := factory.New() 24 dependency := &struct{}{} 25 err := plugin.Init(dependency) 26 assert.EqualError(t, err, "could not find SigningIdentityFetcher in dependencies") 27 28 sif := &mocks.SigningIdentityFetcher{} 29 err1 := plugin.Init(sif) 30 assert.NoError(t, err1) 31 32 // For each test, mock methods are called only once. Check it for them. 33 // SigningIdentity fails 34 sif.On("SigningIdentityForRequest", mock.Anything).Return(nil, errors.New("signingIdentityForRequestReturnsError")).Once() 35 endorsement2, prepBytes2, err2 := plugin.Endorse(nil, nil) 36 assert.Nil(t, endorsement2) 37 assert.Nil(t, prepBytes2) 38 assert.EqualError(t, err2, "failed fetching signing identity: signingIdentityForRequestReturnsError") 39 40 // Serializing the identity fails 41 sid := &mocks2.SigningIdentity{} 42 sid.On("Serialize").Return(nil, errors.New("serializeReturnsError")).Once() 43 sif.On("SigningIdentityForRequest", mock.Anything).Return(sid, nil).Once() 44 endorsement3, prepBytes3, err3 := plugin.Endorse(nil, nil) 45 assert.Nil(t, endorsement3) 46 assert.Nil(t, prepBytes3) 47 assert.EqualError(t, err3, "could not serialize the signing identity: serializeReturnsError") 48 49 // Signing fails 50 sid.On("Serialize").Return([]byte("Endorser4"), nil).Once() 51 sid.On("Sign", mock.Anything).Return(nil, errors.New("signReturnsError")).Once() 52 sif.On("SigningIdentityForRequest", mock.Anything).Return(sid, nil).Once() 53 endorsement4, prepBytes4, err4 := plugin.Endorse([]byte("prpBytes4"), nil) 54 assert.Nil(t, endorsement4) 55 assert.Nil(t, prepBytes4) 56 assert.EqualError(t, err4, "could not sign the proposal response payload: signReturnsError") 57 58 // Success 59 sid.On("Serialize").Return([]byte("Endorser5"), nil).Once() 60 sid.On("Sign", mock.Anything).Return([]byte("Signature5"), nil).Once() 61 sif.On("SigningIdentityForRequest", mock.Anything).Return(sid, nil).Once() 62 endorsement5, prpBytes5, err5 := plugin.Endorse([]byte("prpBytes5"), nil) 63 expected5 := &peer.Endorsement{Signature: []byte("Signature5"), Endorser: []byte("Endorser5")} 64 assert.NoError(t, err5) 65 assert.Equal(t, expected5, endorsement5) 66 assert.Equal(t, []byte("prpBytes5"), prpBytes5) 67 }