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