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