github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/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/hechain20/hechain/core/tx"
    13  	pkgtx "github.com/hechain20/hechain/pkg/tx"
    14  	"github.com/hechain20/hechain/protoutil"
    15  	"github.com/hyperledger/fabric-protos-go/common"
    16  	"github.com/hyperledger/fabric-protos-go/peer"
    17  
    18  	"github.com/pkg/errors"
    19  	"github.com/stretchr/testify/require"
    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  	require.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  		{
    41  			nil, &pkgtx.InvalidErr{
    42  				ActualErr:      errors.New("nil envelope payload"),
    43  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    44  			},
    45  		},
    46  		{
    47  			[]byte("bad env"), &pkgtx.InvalidErr{
    48  				ActualErr:      errors.New("error unmarshalling Envelope: unexpected EOF"),
    49  				ValidationCode: peer.TxValidationCode_INVALID_OTHER_REASON,
    50  			},
    51  		},
    52  		{
    53  			protoutil.MarshalOrPanic(&common.Envelope{Payload: []byte("bad payload"), Signature: []byte("signature")}), &pkgtx.InvalidErr{
    54  				ActualErr:      errors.New("error unmarshalling Payload: unexpected EOF"),
    55  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    56  			},
    57  		},
    58  		{
    59  			protoutil.MarshalOrPanic(&common.Envelope{Signature: []byte("signature")}), &pkgtx.InvalidErr{
    60  				ActualErr:      errors.New("nil envelope payload"),
    61  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    62  			},
    63  		},
    64  		{
    65  			protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Data: []byte("data")}), Signature: []byte("signature")}), &pkgtx.InvalidErr{
    66  				ActualErr:      errors.New("nil payload header"),
    67  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    68  			},
    69  		},
    70  		{
    71  			protoutil.MarshalOrPanic(&common.Envelope{Payload: protoutil.MarshalOrPanic(&common.Payload{Header: &common.Header{}}), Signature: []byte("signature")}), &pkgtx.InvalidErr{
    72  				ActualErr:      errors.New("nil payload channel header"),
    73  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    74  			},
    75  		},
    76  		{
    77  			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{
    78  				ActualErr:      errors.New("nil payload signature header"),
    79  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    80  			},
    81  		},
    82  		{
    83  			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{
    84  				ActualErr:      errors.New("error unmarshalling ChannelHeader: unexpected EOF"),
    85  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    86  			},
    87  		},
    88  		{
    89  			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{
    90  				ActualErr:      errors.New("error unmarshalling SignatureHeader: unexpected EOF"),
    91  				ValidationCode: peer.TxValidationCode_BAD_PAYLOAD,
    92  			},
    93  		},
    94  	}
    95  
    96  	for _, ic := range invalidConfigs {
    97  		_, _, err := f.CreateProcessor(ic.env)
    98  		require.Equal(t, err.Error(), ic.expectedErr.Error())
    99  	}
   100  
   101  	// 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
   102  	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")})
   103  
   104  	_, _, err := f.CreateProcessor(env)
   105  	require.Equal(t, err.Error(), "ValidationCode = UNKNOWN_TX_TYPE, ActualErr = invalid transaction type 1")
   106  }