github.com/celestiaorg/celestia-node@v0.15.0-beta.1/header/headertest/serde_test.go (about) 1 package headertest 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/celestiaorg/celestia-node/header" 10 ) 11 12 func TestMarshalUnmarshalExtendedHeader(t *testing.T) { 13 in := RandExtendedHeader(t) 14 binaryData, err := in.MarshalBinary() 15 require.NoError(t, err) 16 17 out := &header.ExtendedHeader{} 18 err = out.UnmarshalBinary(binaryData) 19 require.NoError(t, err) 20 equalExtendedHeader(t, in, out) 21 22 // A custom JSON marshal/unmarshal is necessary which wraps the ValidatorSet with amino 23 // encoding, to be able to marshal the crypto.PubKey type back from JSON. 24 jsonData, err := in.MarshalJSON() 25 require.NoError(t, err) 26 27 out = &header.ExtendedHeader{} 28 err = out.UnmarshalJSON(jsonData) 29 require.NoError(t, err) 30 equalExtendedHeader(t, in, out) 31 } 32 33 func equalExtendedHeader(t *testing.T, in, out *header.ExtendedHeader) { 34 // ValidatorSet.totalVotingPower is not set (is a cached value that can be recomputed client side) 35 assert.Equal(t, in.ValidatorSet.Validators, out.ValidatorSet.Validators) 36 assert.Equal(t, in.ValidatorSet.Proposer, out.ValidatorSet.Proposer) 37 assert.True(t, in.DAH.Equals(out.DAH)) 38 // not the check for equality as time.Time is not serialized exactly 1:1 39 assert.NotZero(t, out.RawHeader) 40 assert.NotNil(t, out.Commit) 41 }