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.

107 lines
3.6 KiB

  1. package statesync
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/gogo/protobuf/proto"
  6. "github.com/stretchr/testify/require"
  7. ssproto "github.com/tendermint/tendermint/proto/tendermint/statesync"
  8. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  9. )
  10. func TestValidateMsg(t *testing.T) {
  11. testcases := map[string]struct {
  12. msg proto.Message
  13. valid bool
  14. }{
  15. "nil": {nil, false},
  16. "unrelated": {&tmproto.Block{}, false},
  17. "ChunkRequest valid": {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, true},
  18. "ChunkRequest 0 height": {&ssproto.ChunkRequest{Height: 0, Format: 1, Index: 1}, false},
  19. "ChunkRequest 0 format": {&ssproto.ChunkRequest{Height: 1, Format: 0, Index: 1}, true},
  20. "ChunkRequest 0 chunk": {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 0}, true},
  21. "ChunkResponse valid": {
  22. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{1}},
  23. true},
  24. "ChunkResponse 0 height": {
  25. &ssproto.ChunkResponse{Height: 0, Format: 1, Index: 1, Chunk: []byte{1}},
  26. false},
  27. "ChunkResponse 0 format": {
  28. &ssproto.ChunkResponse{Height: 1, Format: 0, Index: 1, Chunk: []byte{1}},
  29. true},
  30. "ChunkResponse 0 chunk": {
  31. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 0, Chunk: []byte{1}},
  32. true},
  33. "ChunkResponse empty body": {
  34. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{}},
  35. true},
  36. "ChunkResponse nil body": {
  37. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: nil},
  38. false},
  39. "ChunkResponse missing": {
  40. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true},
  41. true},
  42. "ChunkResponse missing with empty": {
  43. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{}},
  44. true},
  45. "ChunkResponse missing with body": {
  46. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{1}},
  47. false},
  48. "SnapshotsRequest valid": {&ssproto.SnapshotsRequest{}, true},
  49. "SnapshotsResponse valid": {
  50. &ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{1}},
  51. true},
  52. "SnapshotsResponse 0 height": {
  53. &ssproto.SnapshotsResponse{Height: 0, Format: 1, Chunks: 2, Hash: []byte{1}},
  54. false},
  55. "SnapshotsResponse 0 format": {
  56. &ssproto.SnapshotsResponse{Height: 1, Format: 0, Chunks: 2, Hash: []byte{1}},
  57. true},
  58. "SnapshotsResponse 0 chunks": {
  59. &ssproto.SnapshotsResponse{Height: 1, Format: 1, Hash: []byte{1}},
  60. false},
  61. "SnapshotsResponse no hash": {
  62. &ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{}},
  63. false},
  64. }
  65. for name, tc := range testcases {
  66. tc := tc
  67. t.Run(name, func(t *testing.T) {
  68. err := validateMsg(tc.msg)
  69. if tc.valid {
  70. require.NoError(t, err)
  71. } else {
  72. require.Error(t, err)
  73. }
  74. })
  75. }
  76. }
  77. //nolint:lll // ignore line length
  78. func TestStateSyncVectors(t *testing.T) {
  79. testCases := []struct {
  80. testName string
  81. msg proto.Message
  82. expBytes string
  83. }{
  84. {"SnapshotsRequest", &ssproto.SnapshotsRequest{}, "0a00"},
  85. {"SnapshotsResponse", &ssproto.SnapshotsResponse{Height: 1, Format: 2, Chunks: 3, Hash: []byte("chuck hash"), Metadata: []byte("snapshot metadata")}, "1225080110021803220a636875636b20686173682a11736e617073686f74206d65746164617461"},
  86. {"ChunkRequest", &ssproto.ChunkRequest{Height: 1, Format: 2, Index: 3}, "1a06080110021803"},
  87. {"ChunkResponse", &ssproto.ChunkResponse{Height: 1, Format: 2, Index: 3, Chunk: []byte("it's a chunk")}, "2214080110021803220c697427732061206368756e6b"},
  88. }
  89. for _, tc := range testCases {
  90. tc := tc
  91. bz := mustEncodeMsg(tc.msg)
  92. require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName)
  93. }
  94. }