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