github.com/number571/tendermint@v0.34.11-gost/proto/tendermint/blockchain/message_test.go (about) 1 package blockchain_test 2 3 import ( 4 "encoding/hex" 5 math "math" 6 "testing" 7 8 proto "github.com/gogo/protobuf/proto" 9 "github.com/stretchr/testify/require" 10 11 bcproto "github.com/number571/tendermint/proto/tendermint/blockchain" 12 "github.com/number571/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 // nolint:lll 88 func TestBlockchainMessageVectors(t *testing.T) { 89 block := types.MakeBlock(int64(3), []types.Tx{types.Tx("Hello World")}, nil, nil) 90 block.Version.Block = 11 // overwrite updated protocol version 91 92 bpb, err := block.ToProto() 93 require.NoError(t, err) 94 95 testCases := []struct { 96 testName string 97 bmsg proto.Message 98 expBytes string 99 }{ 100 {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{ 101 BlockRequest: &bcproto.BlockRequest{Height: 1}}}, "0a020801"}, 102 {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{ 103 BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}}, 104 "0a0a08ffffffffffffffff7f"}, 105 {"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{ 106 BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, "1a700a6e0a5b0a02080b1803220b088092b8c398feffffff012a0212003a20c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf96a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855120d0a0b48656c6c6f20576f726c641a00"}, 107 {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{ 108 NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, "12020801"}, 109 {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{ 110 NoBlockResponse: &bcproto.NoBlockResponse{Height: math.MaxInt64}}}, 111 "120a08ffffffffffffffff7f"}, 112 {"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{ 113 StatusRequest: &bcproto.StatusRequest{}}}, 114 "2200"}, 115 {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{ 116 StatusResponse: &bcproto.StatusResponse{Height: 1, Base: 2}}}, 117 "2a0408011002"}, 118 {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{ 119 StatusResponse: &bcproto.StatusResponse{Height: math.MaxInt64, Base: math.MaxInt64}}}, 120 "2a1408ffffffffffffffff7f10ffffffffffffffff7f"}, 121 } 122 123 for _, tc := range testCases { 124 tc := tc 125 t.Run(tc.testName, func(t *testing.T) { 126 bz, err := proto.Marshal(tc.bmsg) 127 require.NoError(t, err) 128 require.Equal(t, tc.expBytes, hex.EncodeToString(bz)) 129 }) 130 } 131 }