From 8149bf517f1a03a186847e07372196ef00d97a5f Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 16 Jun 2020 19:45:15 +0200 Subject: [PATCH] types: add tests for blockmeta (#5013) ## Description add tests for to/from proto for blockmeta and add validate basic test case. Closes: #XXX --- types/block_meta_test.go | 93 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/types/block_meta_test.go b/types/block_meta_test.go index 882ff8573..3de643210 100644 --- a/types/block_meta_test.go +++ b/types/block_meta_test.go @@ -1,7 +1,94 @@ package types -import "testing" +import ( + "testing" -func TestBlockMetaValidateBasic(t *testing.T) { - // TODO + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/tmhash" + tmrand "github.com/tendermint/tendermint/libs/rand" +) + +func TestBlockMeta_ToProto(t *testing.T) { + h := makeRandHeader() + bi := BlockID{Hash: h.Hash(), PartsHeader: PartSetHeader{Total: 123, Hash: tmrand.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 { + tt := tt + t.Run(tt.testName, func(t *testing.T) { + pb := tt.bm.ToProto() + + bm, err := BlockMetaFromProto(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(), PartsHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}} + bi2 := BlockID{Hash: tmrand.Bytes(tmhash.Size), + PartsHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}} + bi3 := BlockID{Hash: []byte("incorrect hash"), + PartsHeader: 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 { + tt := tt + 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) + } + }) + } }