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.

132 lines
4.7 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. request := bcproto.StatusRequest{}
  49. assert.NoError(t, ValidateMsg(&request))
  50. }
  51. func TestBcStatusResponseMessageValidateBasic(t *testing.T) {
  52. testCases := []struct {
  53. testName string
  54. responseHeight int64
  55. expectErr bool
  56. }{
  57. {"Valid Response Message", 0, false},
  58. {"Valid Response Message", 1, false},
  59. {"Invalid Response Message", -1, true},
  60. }
  61. for _, tc := range testCases {
  62. tc := tc
  63. t.Run(tc.testName, func(t *testing.T) {
  64. response := bcproto.StatusResponse{Height: tc.responseHeight}
  65. assert.Equal(t, tc.expectErr, ValidateMsg(&response) != nil, "Validate Basic had an unexpected result")
  66. })
  67. }
  68. }
  69. func TestBlockchainMessageVectors(t *testing.T) {
  70. block := types.MakeBlock(int64(3), []types.Tx{types.Tx("Hello World")}, nil, nil)
  71. bpb, err := block.ToProto()
  72. require.NoError(t, err)
  73. testCases := []struct {
  74. testName string
  75. bmsg proto.Message
  76. expBytes []byte
  77. }{
  78. {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
  79. BlockRequest: &bcproto.BlockRequest{Height: 1}}}, []byte{0xa, 0x2, 0x8, 0x1}},
  80. {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
  81. BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}},
  82. []byte{0xa, 0xa, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  83. {"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{
  84. BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, []byte{0x1a, 0x6e, 0xa, 0x6c,
  85. 0xa, 0x37, 0xa, 0x0, 0x18, 0x3, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe,
  86. 0xff, 0xff, 0xff, 0x1, 0x2a, 0x2, 0x12, 0x0, 0x3a, 0x20, 0xc4, 0xda, 0x88, 0xe8,
  87. 0x76, 0x6, 0x2a, 0xa1, 0x54, 0x34, 0x0, 0xd5, 0xd, 0xe, 0xaa, 0xd, 0xac, 0x88, 0x9,
  88. 0x60, 0x57, 0x94, 0x9c, 0xfb, 0x7b, 0xca, 0x7f, 0x3a, 0x48, 0xc0, 0x4b, 0xf9, 0x12,
  89. 0x2f, 0xa, 0xb, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
  90. 0x12, 0x20, 0xc4, 0xda, 0x88, 0xe8, 0x76, 0x6, 0x2a, 0xa1, 0x54, 0x34, 0x0, 0xd5,
  91. 0xd, 0xe, 0xaa, 0xd, 0xac, 0x88, 0x9, 0x60, 0x57, 0x94, 0x9c, 0xfb, 0x7b, 0xca,
  92. 0x7f, 0x3a, 0x48, 0xc0, 0x4b, 0xf9, 0x1a, 0x0}},
  93. {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
  94. NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, []byte{0x12, 0x2, 0x8, 0x1}},
  95. {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
  96. NoBlockResponse: &bcproto.NoBlockResponse{Height: math.MaxInt64}}},
  97. []byte{0x12, 0xa, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  98. {"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{
  99. StatusRequest: &bcproto.StatusRequest{}}},
  100. []byte{0x22, 0x0}},
  101. {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
  102. StatusResponse: &bcproto.StatusResponse{Height: 1, Base: 2}}},
  103. []byte{0x2a, 0x4, 0x8, 0x1, 0x10, 0x2}},
  104. {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
  105. StatusResponse: &bcproto.StatusResponse{Height: math.MaxInt64, Base: math.MaxInt64}}},
  106. []byte{0x2a, 0x14, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x10,
  107. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
  108. }
  109. for _, tc := range testCases {
  110. tc := tc
  111. t.Run(tc.testName, func(t *testing.T) {
  112. bz, _ := proto.Marshal(tc.bmsg)
  113. require.Equal(t, tc.expBytes, bz)
  114. })
  115. }
  116. }