types: Hash invoked for nil Data and Header should not panic

Fixes https://github.com/tendermint/tendermint/issues/1298
Fixes https://github.com/tendermint/tendermint/issues/1299

Found while writing tests in https://github.com/tendermint/tendermint/pull/1300
This commit is contained in:
Emmanuel T Odeke
2018-03-10 21:44:05 -08:00
parent 3d5c05e4e6
commit 8723c91db9
2 changed files with 18 additions and 1 deletions

View File

@ -3,7 +3,9 @@ package types
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
)
@ -73,3 +75,15 @@ func makeBlockID(hash string, partSetSize int, partSetHash string) BlockID {
}
}
var nilBytes []byte
func TestNilHeaderHashDoesntCrash(t *testing.T) {
assert.Equal(t, []byte((*Header)(nil).Hash()), nilBytes)
assert.Equal(t, []byte((new(Header)).Hash()), nilBytes)
}
func TestNilDataHashDoesntCrash(t *testing.T) {
assert.Equal(t, []byte((*Data)(nil).Hash()), nilBytes)
assert.Equal(t, []byte(new(Data).Hash()), nilBytes)
}