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.

83 lines
2.7 KiB

  1. package statesync
  2. import (
  3. "testing"
  4. "github.com/gogo/protobuf/proto"
  5. "github.com/stretchr/testify/require"
  6. ssproto "github.com/tendermint/tendermint/proto/statesync"
  7. tmproto "github.com/tendermint/tendermint/proto/types"
  8. )
  9. func TestValidateMsg(t *testing.T) {
  10. testcases := map[string]struct {
  11. msg proto.Message
  12. valid bool
  13. }{
  14. "nil": {nil, false},
  15. "unrelated": {&tmproto.Block{}, false},
  16. "ChunkRequest valid": {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, true},
  17. "ChunkRequest 0 height": {&ssproto.ChunkRequest{Height: 0, Format: 1, Index: 1}, false},
  18. "ChunkRequest 0 format": {&ssproto.ChunkRequest{Height: 1, Format: 0, Index: 1}, true},
  19. "ChunkRequest 0 chunk": {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 0}, true},
  20. "ChunkResponse valid": {
  21. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{1}},
  22. true},
  23. "ChunkResponse 0 height": {
  24. &ssproto.ChunkResponse{Height: 0, Format: 1, Index: 1, Chunk: []byte{1}},
  25. false},
  26. "ChunkResponse 0 format": {
  27. &ssproto.ChunkResponse{Height: 1, Format: 0, Index: 1, Chunk: []byte{1}},
  28. true},
  29. "ChunkResponse 0 chunk": {
  30. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 0, Chunk: []byte{1}},
  31. true},
  32. "ChunkResponse empty body": {
  33. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{}},
  34. true},
  35. "ChunkResponse nil body": {
  36. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: nil},
  37. false},
  38. "ChunkResponse missing": {
  39. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true},
  40. true},
  41. "ChunkResponse missing with empty": {
  42. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{}},
  43. true},
  44. "ChunkResponse missing with body": {
  45. &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{1}},
  46. false},
  47. "SnapshotsRequest valid": {&ssproto.SnapshotsRequest{}, true},
  48. "SnapshotsResponse valid": {
  49. &ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{1}},
  50. true},
  51. "SnapshotsResponse 0 height": {
  52. &ssproto.SnapshotsResponse{Height: 0, Format: 1, Chunks: 2, Hash: []byte{1}},
  53. false},
  54. "SnapshotsResponse 0 format": {
  55. &ssproto.SnapshotsResponse{Height: 1, Format: 0, Chunks: 2, Hash: []byte{1}},
  56. true},
  57. "SnapshotsResponse 0 chunks": {
  58. &ssproto.SnapshotsResponse{Height: 1, Format: 1, Hash: []byte{1}},
  59. false},
  60. "SnapshotsResponse no hash": {
  61. &ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{}},
  62. false},
  63. }
  64. for name, tc := range testcases {
  65. tc := tc
  66. t.Run(name, func(t *testing.T) {
  67. err := validateMsg(tc.msg)
  68. if tc.valid {
  69. require.NoError(t, err)
  70. } else {
  71. require.Error(t, err)
  72. }
  73. })
  74. }
  75. }