github.com/yimialmonte/fabric@v2.1.1+incompatible/core/tx/processor_factory_test.go (about) 1 /* 2 Copyright State Street Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package tx_test 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-protos-go/common" 13 "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/hyperledger/fabric/core/tx" 15 pkgtx "github.com/hyperledger/fabric/pkg/tx" 16 "github.com/hyperledger/fabric/protoutil" 17 18 "github.com/pkg/errors" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func TestCreateProcessor(t *testing.T) { 23 f := &tx.ProcessorFactory{} 24 25 invalidType := int32(-1) 26 27 env := protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{ChannelId: "myc", TxId: "tid", Type: invalidType}), SignatureHeader: protoutil.MarshalOrPanic(&common.SignatureHeader{Creator: []byte("creator"), Nonce: []byte("nonce")})}}), Signature: []byte("signature")}) 28 29 _, _, err := f.CreateProcessor(env) 30 assert.Equal(t, err.Error(), "ValidationCode = UNKNOWN_TX_TYPE, ActualErr = invalid transaction type -1") 31 } 32 33 func TestBasicTxValidity(t *testing.T) { 34 f := &tx.ProcessorFactory{} 35 36 invalidConfigs := []struct { 37 env []byte 38 expectedErr *pkgtx.InvalidErr 39 }{ 40 {nil, &pkgtx.InvalidErr{ 41 ActualErr: errors.New("nil envelope payload"), 42 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 43 }, 44 {[]byte("bad env"), &pkgtx.InvalidErr{ 45 ActualErr: errors.New("error unmarshaling Envelope: unexpected EOF"), 46 ValidationCode: peer.TxValidationCode_INVALID_OTHER_REASON}, 47 }, 48 {protoutil.MarshalOrPanic(&common.Envelope{Payload: []byte("bad payload"), Signature: []byte("signature")}), &pkgtx.InvalidErr{ 49 ActualErr: errors.New("error unmarshaling Payload: unexpected EOF"), 50 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 51 }, 52 {protoutil.MarshalOrPanic(&common.Envelope{Signature: []byte("signature")}), &pkgtx.InvalidErr{ 53 ActualErr: errors.New("nil envelope payload"), 54 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 55 }, 56 {protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Data: []byte("data")}), Signature: []byte("signature")}), &pkgtx.InvalidErr{ 57 ActualErr: errors.New("nil payload header"), 58 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 59 }, 60 {protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{}}), Signature: []byte("signature")}), &pkgtx.InvalidErr{ 61 ActualErr: errors.New("nil payload channel header"), 62 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 63 }, 64 {protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{ChannelId: "myc", TxId: "tid"})}}), Signature: []byte("signature")}), &pkgtx.InvalidErr{ 65 ActualErr: errors.New("nil payload signature header"), 66 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 67 }, 68 {protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{ChannelHeader: []byte("bad channel header"), SignatureHeader: protoutil.MarshalOrPanic(&common.SignatureHeader{Creator: []byte("creator"), Nonce: []byte("nonce")})}}), Signature: []byte("signature")}), &pkgtx.InvalidErr{ 69 ActualErr: errors.New("error unmarshaling ChannelHeader: unexpected EOF"), 70 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 71 }, 72 {protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{ChannelId: "myc", TxId: "tid"}), SignatureHeader: []byte("bad sig hdr")}}), Signature: []byte("signature")}), &pkgtx.InvalidErr{ 73 ActualErr: errors.New("error unmarshaling SignatureHeader: unexpected EOF"), 74 ValidationCode: peer.TxValidationCode_BAD_PAYLOAD}, 75 }, 76 } 77 78 for _, ic := range invalidConfigs { 79 _, _, err := f.CreateProcessor(ic.env) 80 assert.Equal(t, err.Error(), ic.expectedErr.Error()) 81 } 82 83 // NOTE: common.HeaderType_CONFIG is a valid type and this should succeed when we populate ProcessorFactory (and signifies successful validation). Till then, a negative test 84 env := protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{ChannelId: "myc", TxId: "tid", Type: int32(common.HeaderType_CONFIG)}), SignatureHeader: protoutil.MarshalOrPanic(&common.SignatureHeader{Creator: []byte("creator"), Nonce: []byte("nonce")})}}), Signature: []byte("signature")}) 85 86 _, _, err := f.CreateProcessor(env) 87 assert.Equal(t, err.Error(), "ValidationCode = UNKNOWN_TX_TYPE, ActualErr = invalid transaction type 1") 88 }