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