github.com/MetalBlockchain/subnet-evm@v0.4.9/plugin/evm/message/message_test.go (about)

     1  // (c) 2019-2021, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package message
     5  
     6  import (
     7  	"encoding/base64"
     8  	"testing"
     9  
    10  	"github.com/MetalBlockchain/metalgo/utils"
    11  	"github.com/MetalBlockchain/metalgo/utils/units"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  // TestMarshalTxs asserts that the structure or serialization logic hasn't changed, primarily to
    17  // ensure compatibility with the network.
    18  func TestMarshalTxs(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	base64EthTxGossip := "AAAAAAAAAAAABGJsYWg="
    22  	msg := []byte("blah")
    23  	builtMsg := TxsGossip{
    24  		Txs: msg,
    25  	}
    26  	builtMsgBytes, err := BuildGossipMessage(Codec, builtMsg)
    27  	assert.NoError(err)
    28  	assert.Equal(base64EthTxGossip, base64.StdEncoding.EncodeToString(builtMsgBytes))
    29  
    30  	parsedMsgIntf, err := ParseGossipMessage(Codec, builtMsgBytes)
    31  	assert.NoError(err)
    32  
    33  	parsedMsg, ok := parsedMsgIntf.(TxsGossip)
    34  	assert.True(ok)
    35  
    36  	assert.Equal(msg, parsedMsg.Txs)
    37  }
    38  
    39  func TestTxsTooLarge(t *testing.T) {
    40  	assert := assert.New(t)
    41  
    42  	builtMsg := TxsGossip{
    43  		Txs: utils.RandomBytes(1024 * units.KiB),
    44  	}
    45  	_, err := BuildGossipMessage(Codec, builtMsg)
    46  	assert.Error(err)
    47  }
    48  
    49  func TestParseGibberish(t *testing.T) {
    50  	assert := assert.New(t)
    51  
    52  	randomBytes := utils.RandomBytes(256 * units.KiB)
    53  	_, err := ParseGossipMessage(Codec, randomBytes)
    54  	assert.Error(err)
    55  }