mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 23:02:16 +00:00
Make block_test.go more table driven (#2526)
This commit is contained in:
parent
32e274cff0
commit
fd1b8598bc
@ -1,14 +1,13 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/crypto"
|
|
||||||
"github.com/tendermint/tendermint/crypto/tmhash"
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
||||||
cmn "github.com/tendermint/tendermint/libs/common"
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
)
|
)
|
||||||
@ -45,51 +44,37 @@ func TestBlockValidateBasic(t *testing.T) {
|
|||||||
ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
|
ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
|
||||||
evList := []Evidence{ev}
|
evList := []Evidence{ev}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
testName string
|
||||||
|
malleateBlock func(*Block)
|
||||||
|
expErr bool
|
||||||
|
}{
|
||||||
|
{"Make Block", func(blk *Block) {}, false},
|
||||||
|
{"Make Block w/ proposer Addr", func(blk *Block) { blk.ProposerAddress = valSet.GetProposer().Address }, false},
|
||||||
|
{"Increase NumTxs", func(blk *Block) { blk.NumTxs++ }, true},
|
||||||
|
{"Remove 1/2 the commits", func(blk *Block) {
|
||||||
|
blk.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
|
||||||
|
blk.LastCommit.hash = nil // clear hash or change wont be noticed
|
||||||
|
}, true},
|
||||||
|
{"Remove LastCommitHash", func(blk *Block) { blk.LastCommitHash = []byte("something else") }, true},
|
||||||
|
{"Tampered Data", func(blk *Block) {
|
||||||
|
blk.Data.Txs[0] = Tx("something else")
|
||||||
|
blk.Data.hash = nil // clear hash or change wont be noticed
|
||||||
|
}, true},
|
||||||
|
{"Tampered DataHash", func(blk *Block) {
|
||||||
|
blk.DataHash = cmn.RandBytes(len(blk.DataHash))
|
||||||
|
}, true},
|
||||||
|
{"Tampered EvidenceHash", func(blk *Block) {
|
||||||
|
blk.EvidenceHash = []byte("something else")
|
||||||
|
}, true},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
block := MakeBlock(h, txs, commit, evList)
|
block := MakeBlock(h, txs, commit, evList)
|
||||||
require.NotNil(t, block)
|
tc.malleateBlock(block)
|
||||||
block.ProposerAddress = valSet.GetProposer().Address
|
assert.Equal(t, tc.expErr, block.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
||||||
|
})
|
||||||
// proper block must pass
|
}
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// tamper with NumTxs
|
|
||||||
block = MakeBlock(h, txs, commit, evList)
|
|
||||||
block.NumTxs++
|
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
// remove 1/2 the commits
|
|
||||||
block = MakeBlock(h, txs, commit, evList)
|
|
||||||
block.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
|
|
||||||
block.LastCommit.hash = nil // clear hash or change wont be noticed
|
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
// tamper with LastCommitHash
|
|
||||||
block = MakeBlock(h, txs, commit, evList)
|
|
||||||
block.LastCommitHash = []byte("something else")
|
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
// tamper with data
|
|
||||||
block = MakeBlock(h, txs, commit, evList)
|
|
||||||
block.Data.Txs[0] = Tx("something else")
|
|
||||||
block.Data.hash = nil // clear hash or change wont be noticed
|
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
// tamper with DataHash
|
|
||||||
block = MakeBlock(h, txs, commit, evList)
|
|
||||||
block.DataHash = cmn.RandBytes(len(block.DataHash))
|
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
// tamper with evidence
|
|
||||||
block = MakeBlock(h, txs, commit, evList)
|
|
||||||
block.EvidenceHash = []byte("something else")
|
|
||||||
err = block.ValidateBasic()
|
|
||||||
require.Error(t, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBlockHash(t *testing.T) {
|
func TestBlockHash(t *testing.T) {
|
||||||
@ -161,7 +146,11 @@ func TestBlockString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeBlockIDRandom() BlockID {
|
func makeBlockIDRandom() BlockID {
|
||||||
blockHash, blockPartsHeader := crypto.CRandBytes(tmhash.Size), PartSetHeader{123, crypto.CRandBytes(tmhash.Size)}
|
blockHash := make([]byte, tmhash.Size)
|
||||||
|
partSetHash := make([]byte, tmhash.Size)
|
||||||
|
rand.Read(blockHash)
|
||||||
|
rand.Read(partSetHash)
|
||||||
|
blockPartsHeader := PartSetHeader{123, partSetHash}
|
||||||
return BlockID{blockHash, blockPartsHeader}
|
return BlockID{blockHash, blockPartsHeader}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,28 +200,25 @@ func TestCommit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommitValidateBasic(t *testing.T) {
|
func TestCommitValidateBasic(t *testing.T) {
|
||||||
commit := randCommit()
|
testCases := []struct {
|
||||||
assert.NoError(t, commit.ValidateBasic())
|
testName string
|
||||||
|
malleateCommit func(*Commit)
|
||||||
// nil precommit is OK
|
expectErr bool
|
||||||
commit = randCommit()
|
}{
|
||||||
commit.Precommits[0] = nil
|
{"Random Commit", func(com *Commit) {}, false},
|
||||||
assert.NoError(t, commit.ValidateBasic())
|
{"Nil precommit", func(com *Commit) { com.Precommits[0] = nil }, false},
|
||||||
|
{"Incorrect signature", func(com *Commit) { com.Precommits[0].Signature = []byte{0} }, false},
|
||||||
// tamper with types
|
{"Incorrect type", func(com *Commit) { com.Precommits[0].Type = VoteTypePrevote }, true},
|
||||||
commit = randCommit()
|
{"Incorrect height", func(com *Commit) { com.Precommits[0].Height = int64(100) }, true},
|
||||||
commit.Precommits[0].Type = VoteTypePrevote
|
{"Incorrect round", func(com *Commit) { com.Precommits[0].Round = 100 }, true},
|
||||||
assert.Error(t, commit.ValidateBasic())
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
// tamper with height
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
commit = randCommit()
|
com := randCommit()
|
||||||
commit.Precommits[0].Height = int64(100)
|
tc.malleateCommit(com)
|
||||||
assert.Error(t, commit.ValidateBasic())
|
assert.Equal(t, tc.expectErr, com.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
||||||
|
})
|
||||||
// tamper with round
|
}
|
||||||
commit = randCommit()
|
|
||||||
commit.Precommits[0].Round = 100
|
|
||||||
assert.Error(t, commit.ValidateBasic())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaxHeaderBytes(t *testing.T) {
|
func TestMaxHeaderBytes(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user