You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.2 KiB

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