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