Skip to content

Commit

Permalink
Test the nil raw data on the tx proto decode and ensure it doesnt crash
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Nov 4, 2024
1 parent b1941d1 commit 5785f4a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/dominant-strategies/go-quai/common"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestTransactionProtoEncodeDecode(t *testing.T) {
Expand Down Expand Up @@ -1065,3 +1066,35 @@ func TestCoinbaseTxEncodeDecode(t *testing.T) {

require.Equal(t, coinbaseTx.Hash(), tx.Hash())
}

func TestTxNilDecode(t *testing.T) {
// If empty data is sent from the wire or from rpc
// proto.Unmarshal should read it and not return any error
protoTransaction := new(ProtoTransaction)
err := proto.Unmarshal(nil, protoTransaction)
require.Equal(t, err, nil)

// This empty protoTransaction struct should not crash the proto decode
tx := Transaction{}
err = tx.ProtoDecode(protoTransaction, common.Location{0, 0})
require.NotEqual(t, err, nil)
}

func TestNilChainIDDecode(t *testing.T) {
// get an empty quai tx
quaiTx, _ := quaiTxData()

protoQuaiTx, err := quaiTx.ProtoEncode()
require.Equal(t, err, nil)

// set the proto quai tx chain id to nil
protoQuaiTx.ChainId = nil

tx := Transaction{}
err = tx.ProtoDecode(protoQuaiTx, common.Location{0, 0})

// This should print missing required field 'ChainId'
t.Log(err)

require.NotEqual(t, err, nil)
}

0 comments on commit 5785f4a

Please sign in to comment.