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.

106 lines
3.4 KiB

  1. package statesync
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestSnapshotsRequestMessage_ValidateBasic(t *testing.T) {
  7. testcases := map[string]struct {
  8. msg *snapshotsRequestMessage
  9. valid bool
  10. }{
  11. "nil": {nil, false},
  12. "valid": {&snapshotsRequestMessage{}, true},
  13. }
  14. for name, tc := range testcases {
  15. tc := tc
  16. t.Run(name, func(t *testing.T) {
  17. err := tc.msg.ValidateBasic()
  18. if tc.valid {
  19. require.NoError(t, err)
  20. } else {
  21. require.Error(t, err)
  22. }
  23. })
  24. }
  25. }
  26. func TestSnapshotsResponseMessage_ValidateBasic(t *testing.T) {
  27. testcases := map[string]struct {
  28. msg *snapshotsResponseMessage
  29. valid bool
  30. }{
  31. "nil": {nil, false},
  32. "valid": {&snapshotsResponseMessage{Height: 1, Format: 1, Chunks: 2, Hash: []byte{1}}, true},
  33. "0 height": {&snapshotsResponseMessage{Height: 0, Format: 1, Chunks: 2, Hash: []byte{1}}, false},
  34. "0 format": {&snapshotsResponseMessage{Height: 1, Format: 0, Chunks: 2, Hash: []byte{1}}, true},
  35. "0 chunks": {&snapshotsResponseMessage{Height: 1, Format: 1, Hash: []byte{1}}, false},
  36. "no hash": {&snapshotsResponseMessage{Height: 1, Format: 1, Chunks: 2, Hash: []byte{}}, false},
  37. }
  38. for name, tc := range testcases {
  39. tc := tc
  40. t.Run(name, func(t *testing.T) {
  41. err := tc.msg.ValidateBasic()
  42. if tc.valid {
  43. require.NoError(t, err)
  44. } else {
  45. require.Error(t, err)
  46. }
  47. })
  48. }
  49. }
  50. func TestChunkRequestMessage_ValidateBasic(t *testing.T) {
  51. testcases := map[string]struct {
  52. msg *chunkRequestMessage
  53. valid bool
  54. }{
  55. "nil": {nil, false},
  56. "valid": {&chunkRequestMessage{Height: 1, Format: 1, Index: 1}, true},
  57. "0 height": {&chunkRequestMessage{Height: 0, Format: 1, Index: 1}, false},
  58. "0 format": {&chunkRequestMessage{Height: 1, Format: 0, Index: 1}, true},
  59. "0 chunk": {&chunkRequestMessage{Height: 1, Format: 1, Index: 0}, true},
  60. }
  61. for name, tc := range testcases {
  62. tc := tc
  63. t.Run(name, func(t *testing.T) {
  64. err := tc.msg.ValidateBasic()
  65. if tc.valid {
  66. require.NoError(t, err)
  67. } else {
  68. require.Error(t, err)
  69. }
  70. })
  71. }
  72. }
  73. func TestChunkResponseMessage_ValidateBasic(t *testing.T) {
  74. testcases := map[string]struct {
  75. msg *chunkResponseMessage
  76. valid bool
  77. }{
  78. "nil message": {nil, false},
  79. "valid": {&chunkResponseMessage{Height: 1, Format: 1, Index: 1, Chunk: []byte{1}}, true},
  80. "0 height": {&chunkResponseMessage{Height: 0, Format: 1, Index: 1, Chunk: []byte{1}}, false},
  81. "0 format": {&chunkResponseMessage{Height: 1, Format: 0, Index: 1, Chunk: []byte{1}}, true},
  82. "0 chunk": {&chunkResponseMessage{Height: 1, Format: 1, Index: 0, Chunk: []byte{1}}, true},
  83. "empty body": {&chunkResponseMessage{Height: 1, Format: 1, Index: 1, Chunk: []byte{}}, true},
  84. "nil body": {&chunkResponseMessage{Height: 1, Format: 1, Index: 1, Chunk: nil}, false},
  85. "missing": {&chunkResponseMessage{Height: 1, Format: 1, Index: 1, Missing: true}, true},
  86. "missing with empty": {&chunkResponseMessage{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{}}, true},
  87. "missing with body": {&chunkResponseMessage{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{1}}, false},
  88. }
  89. for name, tc := range testcases {
  90. tc := tc
  91. t.Run(name, func(t *testing.T) {
  92. err := tc.msg.ValidateBasic()
  93. if tc.valid {
  94. require.NoError(t, err)
  95. } else {
  96. require.Error(t, err)
  97. }
  98. })
  99. }
  100. }