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.

150 lines
5.5 KiB

  1. package blockchain
  2. import (
  3. "math"
  4. "testing"
  5. "github.com/gogo/protobuf/proto"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. bcproto "github.com/tendermint/tendermint/proto/tendermint/blockchain"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. func TestBcBlockRequestMessageValidateBasic(t *testing.T) {
  12. testCases := []struct {
  13. testName string
  14. requestHeight int64
  15. expectErr bool
  16. }{
  17. {"Valid Request Message", 0, false},
  18. {"Valid Request Message", 1, false},
  19. {"Invalid Request Message", -1, true},
  20. }
  21. for _, tc := range testCases {
  22. tc := tc
  23. t.Run(tc.testName, func(t *testing.T) {
  24. request := bcproto.BlockRequest{Height: tc.requestHeight}
  25. assert.Equal(t, tc.expectErr, ValidateMsg(&request) != nil, "Validate Basic had an unexpected result")
  26. })
  27. }
  28. }
  29. func TestBcNoBlockResponseMessageValidateBasic(t *testing.T) {
  30. testCases := []struct {
  31. testName string
  32. nonResponseHeight int64
  33. expectErr bool
  34. }{
  35. {"Valid Non-Response Message", 0, false},
  36. {"Valid Non-Response Message", 1, false},
  37. {"Invalid Non-Response Message", -1, true},
  38. }
  39. for _, tc := range testCases {
  40. tc := tc
  41. t.Run(tc.testName, func(t *testing.T) {
  42. nonResponse := bcproto.NoBlockResponse{Height: tc.nonResponseHeight}
  43. assert.Equal(t, tc.expectErr, ValidateMsg(&nonResponse) != nil, "Validate Basic had an unexpected result")
  44. })
  45. }
  46. }
  47. func TestBcStatusRequestMessageValidateBasic(t *testing.T) {
  48. testCases := []struct {
  49. testName string
  50. requestHeight int64
  51. expectErr bool
  52. }{
  53. {"Valid Request Message", 0, false},
  54. {"Valid Request Message", 1, false},
  55. {"Invalid Request Message", -1, true},
  56. }
  57. for _, tc := range testCases {
  58. tc := tc
  59. t.Run(tc.testName, func(t *testing.T) {
  60. request := bcproto.StatusRequest{Height: tc.requestHeight}
  61. assert.Equal(t, tc.expectErr, ValidateMsg(&request) != nil, "Validate Basic had an unexpected result")
  62. })
  63. }
  64. }
  65. func TestBcStatusResponseMessageValidateBasic(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. for _, tc := range testCases {
  76. tc := tc
  77. t.Run(tc.testName, func(t *testing.T) {
  78. response := bcproto.StatusResponse{Height: tc.responseHeight}
  79. assert.Equal(t, tc.expectErr, ValidateMsg(&response) != nil, "Validate Basic had an unexpected result")
  80. })
  81. }
  82. }
  83. func TestBlockchainMessageVectors(t *testing.T) {
  84. block := types.MakeBlock(int64(3), []types.Tx{types.Tx("Hello World")}, nil, nil)
  85. bpb, err := block.ToProto()
  86. require.NoError(t, err)
  87. testCases := []struct {
  88. testName string
  89. bmsg proto.Message
  90. expBytes []byte
  91. }{
  92. {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
  93. BlockRequest: &bcproto.BlockRequest{Height: 1}}}, []byte{0xa, 0x2, 0x8, 0x1}},
  94. {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
  95. BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}},
  96. []byte{0xa, 0xa, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  97. {"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{
  98. BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, []byte{0x1a, 0x6e, 0xa, 0x6c,
  99. 0xa, 0x37, 0xa, 0x0, 0x18, 0x3, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe,
  100. 0xff, 0xff, 0xff, 0x1, 0x2a, 0x2, 0x12, 0x0, 0x3a, 0x20, 0xc4, 0xda, 0x88, 0xe8,
  101. 0x76, 0x6, 0x2a, 0xa1, 0x54, 0x34, 0x0, 0xd5, 0xd, 0xe, 0xaa, 0xd, 0xac, 0x88, 0x9,
  102. 0x60, 0x57, 0x94, 0x9c, 0xfb, 0x7b, 0xca, 0x7f, 0x3a, 0x48, 0xc0, 0x4b, 0xf9, 0x12,
  103. 0x2f, 0xa, 0xb, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
  104. 0x12, 0x20, 0xc4, 0xda, 0x88, 0xe8, 0x76, 0x6, 0x2a, 0xa1, 0x54, 0x34, 0x0, 0xd5,
  105. 0xd, 0xe, 0xaa, 0xd, 0xac, 0x88, 0x9, 0x60, 0x57, 0x94, 0x9c, 0xfb, 0x7b, 0xca,
  106. 0x7f, 0x3a, 0x48, 0xc0, 0x4b, 0xf9, 0x1a, 0x0}},
  107. {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
  108. NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, []byte{0x12, 0x2, 0x8, 0x1}},
  109. {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
  110. NoBlockResponse: &bcproto.NoBlockResponse{Height: math.MaxInt64}}},
  111. []byte{0x12, 0xa, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  112. {"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{
  113. StatusRequest: &bcproto.StatusRequest{Height: 1, Base: 2}}},
  114. []byte{0x22, 0x4, 0x8, 0x1, 0x10, 0x2}},
  115. {"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{
  116. StatusRequest: &bcproto.StatusRequest{Height: math.MaxInt64, Base: math.MaxInt64}}},
  117. []byte{0x22, 0x14, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
  118. 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  119. {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
  120. StatusResponse: &bcproto.StatusResponse{Height: 1, Base: 2}}},
  121. []byte{0x2a, 0x4, 0x8, 0x1, 0x10, 0x2}},
  122. {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
  123. StatusResponse: &bcproto.StatusResponse{Height: math.MaxInt64, Base: math.MaxInt64}}},
  124. []byte{0x2a, 0x14, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x10,
  125. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  126. }
  127. for _, tc := range testCases {
  128. tc := tc
  129. t.Run(tc.testName, func(t *testing.T) {
  130. bz, _ := proto.Marshal(tc.bmsg)
  131. require.Equal(t, tc.expBytes, bz)
  132. })
  133. }
  134. }