forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_meta_test.go
97 lines (83 loc) · 2.02 KB
/
block_meta_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
package types
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/cometbft/cometbft/crypto/tmhash"
cmtrand "github.com/cometbft/cometbft/internal/rand"
)
func TestBlockMeta_ToProto(t *testing.T) {
h := makeRandHeader()
bi := BlockID{Hash: h.Hash(), PartSetHeader: PartSetHeader{Total: 123, Hash: cmtrand.Bytes(tmhash.Size)}}
bm := &BlockMeta{
BlockID: bi,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
tests := []struct {
testName string
bm *BlockMeta
expErr bool
}{
{"success", bm, false},
{"failure nil", nil, true},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
pb := tt.bm.ToProto()
bm, err := BlockMetaFromTrustedProto(pb)
if !tt.expErr {
require.NoError(t, err, tt.testName)
require.Equal(t, tt.bm, bm, tt.testName)
} else {
require.Error(t, err, tt.testName)
}
})
}
}
func TestBlockMeta_ValidateBasic(t *testing.T) {
h := makeRandHeader()
bi := BlockID{Hash: h.Hash(), PartSetHeader: PartSetHeader{Total: 123, Hash: cmtrand.Bytes(tmhash.Size)}}
bi2 := BlockID{
Hash: cmtrand.Bytes(tmhash.Size),
PartSetHeader: PartSetHeader{Total: 123, Hash: cmtrand.Bytes(tmhash.Size)},
}
bi3 := BlockID{
Hash: []byte("incorrect hash"),
PartSetHeader: PartSetHeader{Total: 123, Hash: []byte("incorrect hash")},
}
bm := &BlockMeta{
BlockID: bi,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
bm2 := &BlockMeta{
BlockID: bi2,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
bm3 := &BlockMeta{
BlockID: bi3,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
tests := []struct {
name string
bm *BlockMeta
wantErr bool
}{
{"success", bm, false},
{"failure wrong blockID hash", bm2, true},
{"failure wrong length blockID hash", bm3, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.bm.ValidateBasic(); (err != nil) != tt.wantErr {
t.Errorf("BlockMeta.ValidateBasic() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}