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