github.com/koko1123/flow-go-1@v0.29.6/network/codec/roundTripHeader_test.go (about)

     1  package codec_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/koko1123/flow-go-1/model/messages"
    10  	"github.com/koko1123/flow-go-1/network"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  )
    13  
    14  // roundTripHeaderViaCodec tests encoding and then decoding (AKA round-
    15  // trip) an example message to see if the decoded message matches the
    16  // original encoded message. Why? Both JSON and CBOR require helper
    17  // functions (i.e. MarshalJSON() & MarshalCBOR()) which properly export
    18  // time in the proper format and zone, otherwise running nodes will fail
    19  // due to signature validation failures due to using the incorrectly
    20  // serialized time. When CBOR was first added without the assicated
    21  // helper function then all the unit tests passed but the nodes failed
    22  // as described above. Therefore these functions were added to help the
    23  // next developer who wants to add a new serialization format :-)
    24  func roundTripHeaderViaCodec(t *testing.T, codec network.Codec) {
    25  	block := unittest.BlockFixture()
    26  	message := messages.NewBlockProposal(&block)
    27  	encoded, err := codec.Encode(message)
    28  	assert.NoError(t, err)
    29  	decodedInterface, err := codec.Decode(encoded)
    30  	assert.NoError(t, err)
    31  	decoded := decodedInterface.(*messages.BlockProposal)
    32  	decodedBlock := decoded.Block.ToInternal()
    33  	assert.Equal(t, block.Header.ProposerSigData, decodedBlock.Header.ProposerSigData)
    34  	messageHeader := fmt.Sprintf("- .Header=%+v\n", block.Header)
    35  	decodedHeader := fmt.Sprintf("- .Header=%+v\n", decodedBlock.Header)
    36  	assert.Equal(t, messageHeader, decodedHeader)
    37  }
    38  
    39  func TestRoundTripHeaderViaCBOR(t *testing.T) {
    40  	codec := unittest.NetworkCodec()
    41  	roundTripHeaderViaCodec(t, codec)
    42  }