github.com/Oyster-zx/tendermint@v0.34.24-fork/types/block_meta_test.go (about) 1 package types 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/tendermint/tendermint/crypto/tmhash" 9 tmrand "github.com/tendermint/tendermint/libs/rand" 10 ) 11 12 func TestBlockMeta_ToProto(t *testing.T) { 13 h := makeRandHeader() 14 bi := BlockID{Hash: h.Hash(), PartSetHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}} 15 16 bm := &BlockMeta{ 17 BlockID: bi, 18 BlockSize: 200, 19 Header: h, 20 NumTxs: 0, 21 } 22 23 tests := []struct { 24 testName string 25 bm *BlockMeta 26 expErr bool 27 }{ 28 {"success", bm, false}, 29 {"failure nil", nil, true}, 30 } 31 32 for _, tt := range tests { 33 tt := tt 34 t.Run(tt.testName, func(t *testing.T) { 35 pb := tt.bm.ToProto() 36 37 bm, err := BlockMetaFromProto(pb) 38 39 if !tt.expErr { 40 require.NoError(t, err, tt.testName) 41 require.Equal(t, tt.bm, bm, tt.testName) 42 } else { 43 require.Error(t, err, tt.testName) 44 } 45 }) 46 } 47 } 48 49 func TestBlockMeta_ValidateBasic(t *testing.T) { 50 h := makeRandHeader() 51 bi := BlockID{Hash: h.Hash(), PartSetHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}} 52 bi2 := BlockID{Hash: tmrand.Bytes(tmhash.Size), 53 PartSetHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}} 54 bi3 := BlockID{Hash: []byte("incorrect hash"), 55 PartSetHeader: PartSetHeader{Total: 123, Hash: []byte("incorrect hash")}} 56 57 bm := &BlockMeta{ 58 BlockID: bi, 59 BlockSize: 200, 60 Header: h, 61 NumTxs: 0, 62 } 63 64 bm2 := &BlockMeta{ 65 BlockID: bi2, 66 BlockSize: 200, 67 Header: h, 68 NumTxs: 0, 69 } 70 71 bm3 := &BlockMeta{ 72 BlockID: bi3, 73 BlockSize: 200, 74 Header: h, 75 NumTxs: 0, 76 } 77 78 tests := []struct { 79 name string 80 bm *BlockMeta 81 wantErr bool 82 }{ 83 {"success", bm, false}, 84 {"failure wrong blockID hash", bm2, true}, 85 {"failure wrong length blockID hash", bm3, true}, 86 } 87 for _, tt := range tests { 88 tt := tt 89 t.Run(tt.name, func(t *testing.T) { 90 if err := tt.bm.ValidateBasic(); (err != nil) != tt.wantErr { 91 t.Errorf("BlockMeta.ValidateBasic() error = %v, wantErr %v", err, tt.wantErr) 92 } 93 }) 94 } 95 }