github.com/number571/tendermint@v0.34.11-gost/proto/tendermint/statesync/message_test.go (about) 1 package statesync_test 2 3 import ( 4 "encoding/hex" 5 "testing" 6 7 proto "github.com/gogo/protobuf/proto" 8 "github.com/stretchr/testify/require" 9 10 ssproto "github.com/number571/tendermint/proto/tendermint/statesync" 11 tmproto "github.com/number571/tendermint/proto/tendermint/types" 12 ) 13 14 func TestValidateMsg(t *testing.T) { 15 testcases := map[string]struct { 16 msg proto.Message 17 validMsg bool 18 valid bool 19 }{ 20 "nil": {nil, false, false}, 21 "unrelated": {&tmproto.Block{}, false, false}, 22 23 "ChunkRequest valid": {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, true, true}, 24 "ChunkRequest 0 height": {&ssproto.ChunkRequest{Height: 0, Format: 1, Index: 1}, true, false}, 25 "ChunkRequest 0 format": {&ssproto.ChunkRequest{Height: 1, Format: 0, Index: 1}, true, true}, 26 "ChunkRequest 0 chunk": {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 0}, true, true}, 27 28 "ChunkResponse valid": { 29 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{1}}, 30 true, 31 true, 32 }, 33 "ChunkResponse 0 height": { 34 &ssproto.ChunkResponse{Height: 0, Format: 1, Index: 1, Chunk: []byte{1}}, 35 true, 36 false, 37 }, 38 "ChunkResponse 0 format": { 39 &ssproto.ChunkResponse{Height: 1, Format: 0, Index: 1, Chunk: []byte{1}}, 40 true, 41 true, 42 }, 43 "ChunkResponse 0 chunk": { 44 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 0, Chunk: []byte{1}}, 45 true, 46 true, 47 }, 48 "ChunkResponse empty body": { 49 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{}}, 50 true, 51 true, 52 }, 53 "ChunkResponse nil body": { 54 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: nil}, 55 true, 56 false, 57 }, 58 "ChunkResponse missing": { 59 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true}, 60 true, 61 true, 62 }, 63 "ChunkResponse missing with empty": { 64 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{}}, 65 true, 66 true, 67 }, 68 "ChunkResponse missing with body": { 69 &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{1}}, 70 true, 71 false, 72 }, 73 74 "SnapshotsRequest valid": {&ssproto.SnapshotsRequest{}, true, true}, 75 76 "SnapshotsResponse valid": { 77 &ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{1}}, 78 true, 79 true, 80 }, 81 "SnapshotsResponse 0 height": { 82 &ssproto.SnapshotsResponse{Height: 0, Format: 1, Chunks: 2, Hash: []byte{1}}, 83 true, 84 false, 85 }, 86 "SnapshotsResponse 0 format": { 87 &ssproto.SnapshotsResponse{Height: 1, Format: 0, Chunks: 2, Hash: []byte{1}}, 88 true, 89 true, 90 }, 91 "SnapshotsResponse 0 chunks": { 92 &ssproto.SnapshotsResponse{Height: 1, Format: 1, Hash: []byte{1}}, 93 true, 94 false, 95 }, 96 "SnapshotsResponse no hash": { 97 &ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{}}, 98 true, 99 false, 100 }, 101 } 102 103 for name, tc := range testcases { 104 tc := tc 105 t.Run(name, func(t *testing.T) { 106 msg := new(ssproto.Message) 107 108 if tc.validMsg { 109 require.NoError(t, msg.Wrap(tc.msg)) 110 } else { 111 require.Error(t, msg.Wrap(tc.msg)) 112 } 113 114 if tc.valid { 115 require.NoError(t, msg.Validate()) 116 } else { 117 require.Error(t, msg.Validate()) 118 } 119 }) 120 } 121 } 122 123 func TestStateSyncVectors(t *testing.T) { 124 testCases := []struct { 125 testName string 126 msg proto.Message 127 expBytes string 128 }{ 129 { 130 "SnapshotsRequest", 131 &ssproto.SnapshotsRequest{}, 132 "0a00", 133 }, 134 { 135 "SnapshotsResponse", 136 &ssproto.SnapshotsResponse{ 137 Height: 1, 138 Format: 2, 139 Chunks: 3, 140 Hash: []byte("chuck hash"), 141 Metadata: []byte("snapshot metadata"), 142 }, 143 "1225080110021803220a636875636b20686173682a11736e617073686f74206d65746164617461", 144 }, 145 { 146 "ChunkRequest", 147 &ssproto.ChunkRequest{ 148 Height: 1, 149 Format: 2, 150 Index: 3, 151 }, 152 "1a06080110021803", 153 }, 154 { 155 "ChunkResponse", 156 &ssproto.ChunkResponse{ 157 Height: 1, 158 Format: 2, 159 Index: 3, 160 Chunk: []byte("it's a chunk"), 161 }, 162 "2214080110021803220c697427732061206368756e6b", 163 }, 164 } 165 166 for _, tc := range testCases { 167 tc := tc 168 169 msg := new(ssproto.Message) 170 require.NoError(t, msg.Wrap(tc.msg)) 171 172 bz, err := msg.Marshal() 173 require.NoError(t, err) 174 require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName) 175 } 176 }