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.

124 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. bpb, err := block.ToProto()
  74. require.NoError(t, err)
  75. testCases := []struct {
  76. testName string
  77. bmsg proto.Message
  78. expBytes string
  79. }{
  80. {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
  81. BlockRequest: &bcproto.BlockRequest{Height: 1}}}, "0a020801"},
  82. {"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
  83. BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}},
  84. "0a0a08ffffffffffffffff7f"},
  85. {"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{
  86. BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, "1ab3010ab0010a590a001803220b088092b8c398feffffff012a0212003a20c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf96a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855122f0a0b48656c6c6f20576f726c641220c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf91a221220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  87. {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
  88. NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, "12020801"},
  89. {"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
  90. NoBlockResponse: &bcproto.NoBlockResponse{Height: math.MaxInt64}}},
  91. "120a08ffffffffffffffff7f"},
  92. {"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{
  93. StatusRequest: &bcproto.StatusRequest{}}},
  94. "2200"},
  95. {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
  96. StatusResponse: &bcproto.StatusResponse{Height: 1, Base: 2}}},
  97. "2a0408011002"},
  98. {"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
  99. StatusResponse: &bcproto.StatusResponse{Height: math.MaxInt64, Base: math.MaxInt64}}},
  100. "2a1408ffffffffffffffff7f10ffffffffffffffff7f"},
  101. }
  102. for _, tc := range testCases {
  103. tc := tc
  104. t.Run(tc.testName, func(t *testing.T) {
  105. bz, _ := proto.Marshal(tc.bmsg)
  106. require.Equal(t, tc.expBytes, hex.EncodeToString(bz))
  107. })
  108. }
  109. }