github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/proto/tendermint/blocksync/message_test.go (about) 1 package blocksync_test 2 3 import ( 4 "encoding/hex" 5 math "math" 6 "testing" 7 8 "github.com/gogo/protobuf/proto" 9 "github.com/stretchr/testify/require" 10 11 bcproto "github.com/ari-anchor/sei-tendermint/proto/tendermint/blocksync" 12 "github.com/ari-anchor/sei-tendermint/types" 13 ) 14 15 func TestBlockRequest_Validate(t *testing.T) { 16 testCases := []struct { 17 testName string 18 requestHeight int64 19 expectErr bool 20 }{ 21 {"Valid Request Message", 0, false}, 22 {"Valid Request Message", 1, false}, 23 {"Invalid Request Message", -1, true}, 24 } 25 26 for _, tc := range testCases { 27 tc := tc 28 t.Run(tc.testName, func(t *testing.T) { 29 msg := &bcproto.Message{} 30 require.NoError(t, msg.Wrap(&bcproto.BlockRequest{Height: tc.requestHeight})) 31 32 require.Equal(t, tc.expectErr, msg.Validate() != nil) 33 }) 34 } 35 } 36 37 func TestNoBlockResponse_Validate(t *testing.T) { 38 testCases := []struct { 39 testName string 40 nonResponseHeight int64 41 expectErr bool 42 }{ 43 {"Valid Non-Response Message", 0, false}, 44 {"Valid Non-Response Message", 1, false}, 45 {"Invalid Non-Response Message", -1, true}, 46 } 47 48 for _, tc := range testCases { 49 tc := tc 50 t.Run(tc.testName, func(t *testing.T) { 51 msg := &bcproto.Message{} 52 require.NoError(t, msg.Wrap(&bcproto.NoBlockResponse{Height: tc.nonResponseHeight})) 53 54 require.Equal(t, tc.expectErr, msg.Validate() != nil) 55 }) 56 } 57 } 58 59 func TestStatusRequest_Validate(t *testing.T) { 60 msg := &bcproto.Message{} 61 require.NoError(t, msg.Wrap(&bcproto.StatusRequest{})) 62 require.NoError(t, msg.Validate()) 63 } 64 65 func TestStatusResponse_Validate(t *testing.T) { 66 testCases := []struct { 67 testName string 68 responseHeight int64 69 expectErr bool 70 }{ 71 {"Valid Response Message", 0, false}, 72 {"Valid Response Message", 1, false}, 73 {"Invalid Response Message", -1, true}, 74 } 75 76 for _, tc := range testCases { 77 tc := tc 78 t.Run(tc.testName, func(t *testing.T) { 79 msg := &bcproto.Message{} 80 require.NoError(t, msg.Wrap(&bcproto.StatusResponse{Height: tc.responseHeight})) 81 82 require.Equal(t, tc.expectErr, msg.Validate() != nil) 83 }) 84 } 85 } 86 87 func TestBlockchainMessageVectors(t *testing.T) { 88 block := types.MakeBlock(int64(3), []types.Tx{types.Tx("Hello World")}, nil, nil) 89 block.Version.Block = 11 // overwrite updated protocol version 90 91 bpb, err := block.ToProto() 92 require.NoError(t, err) 93 94 testCases := []struct { 95 testName string 96 bmsg proto.Message 97 expBytes string 98 }{ 99 {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{ 100 BlockRequest: &bcproto.BlockRequest{Height: 1}}}, "0a020801"}, 101 {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{ 102 BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}}, 103 "0a0a08ffffffffffffffff7f"}, 104 {"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{ 105 BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, "1a700a6e0a5b0a02080b1803220b088092b8c398feffffff012a0212003a20c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf96a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855120d0a0b48656c6c6f20576f726c641a00"}, 106 {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{ 107 NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, "12020801"}, 108 {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{ 109 NoBlockResponse: &bcproto.NoBlockResponse{Height: math.MaxInt64}}}, 110 "120a08ffffffffffffffff7f"}, 111 {"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{ 112 StatusRequest: &bcproto.StatusRequest{}}}, 113 "2200"}, 114 {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{ 115 StatusResponse: &bcproto.StatusResponse{Height: 1, Base: 2}}}, 116 "2a0408011002"}, 117 {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{ 118 StatusResponse: &bcproto.StatusResponse{Height: math.MaxInt64, Base: math.MaxInt64}}}, 119 "2a1408ffffffffffffffff7f10ffffffffffffffff7f"}, 120 } 121 122 for _, tc := range testCases { 123 tc := tc 124 t.Run(tc.testName, func(t *testing.T) { 125 bz, err := proto.Marshal(tc.bmsg) 126 require.NoError(t, err) 127 require.Equal(t, tc.expBytes, hex.EncodeToString(bz)) 128 }) 129 } 130 }