181 lines
4.5 KiB
Go
181 lines
4.5 KiB
Go
package types_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
var invalidStrs = []string{
|
|
"",
|
|
"hello, world!",
|
|
"0xAA",
|
|
"AAA",
|
|
types.Bech32PrefixAccAddr + "AB0C",
|
|
types.Bech32PrefixAccPub + "1234",
|
|
types.Bech32PrefixValAddr + "5678",
|
|
types.Bech32PrefixValPub + "BBAB",
|
|
types.Bech32PrefixConsAddr + "FF04",
|
|
types.Bech32PrefixConsPub + "6789",
|
|
}
|
|
|
|
func testMarshal(t *testing.T, original interface{}, res interface{}, marshal func() ([]byte, error), unmarshal func([]byte) error) {
|
|
bz, err := marshal()
|
|
require.Nil(t, err)
|
|
err = unmarshal(bz)
|
|
require.Nil(t, err)
|
|
require.Equal(t, original, res)
|
|
}
|
|
|
|
func TestRandBech32PubkeyConsistency(t *testing.T) {
|
|
var pub ed25519.PubKeyEd25519
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
rand.Read(pub[:])
|
|
|
|
mustBech32AccPub := types.MustBech32ifyAccPub(pub)
|
|
bech32AccPub, err := types.Bech32ifyAccPub(pub)
|
|
require.Nil(t, err)
|
|
require.Equal(t, bech32AccPub, mustBech32AccPub)
|
|
|
|
mustBech32ValPub := types.MustBech32ifyValPub(pub)
|
|
bech32ValPub, err := types.Bech32ifyValPub(pub)
|
|
require.Nil(t, err)
|
|
require.Equal(t, bech32ValPub, mustBech32ValPub)
|
|
|
|
mustBech32ConsPub := types.MustBech32ifyConsPub(pub)
|
|
bech32ConsPub, err := types.Bech32ifyConsPub(pub)
|
|
require.Nil(t, err)
|
|
require.Equal(t, bech32ConsPub, mustBech32ConsPub)
|
|
|
|
mustAccPub := types.MustGetAccPubKeyBech32(bech32AccPub)
|
|
accPub, err := types.GetAccPubKeyBech32(bech32AccPub)
|
|
require.Nil(t, err)
|
|
require.Equal(t, accPub, mustAccPub)
|
|
|
|
mustValPub := types.MustGetValPubKeyBech32(bech32ValPub)
|
|
valPub, err := types.GetValPubKeyBech32(bech32ValPub)
|
|
require.Nil(t, err)
|
|
require.Equal(t, valPub, mustValPub)
|
|
|
|
mustConsPub := types.MustGetConsPubKeyBech32(bech32ConsPub)
|
|
consPub, err := types.GetConsPubKeyBech32(bech32ConsPub)
|
|
require.Nil(t, err)
|
|
require.Equal(t, consPub, mustConsPub)
|
|
|
|
require.Equal(t, valPub, accPub)
|
|
require.Equal(t, valPub, consPub)
|
|
}
|
|
}
|
|
|
|
func TestRandBech32AccAddrConsistency(t *testing.T) {
|
|
var pub ed25519.PubKeyEd25519
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
rand.Read(pub[:])
|
|
|
|
acc := types.AccAddress(pub.Address())
|
|
res := types.AccAddress{}
|
|
|
|
testMarshal(t, &acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
|
testMarshal(t, &acc, &res, acc.Marshal, (&res).Unmarshal)
|
|
|
|
str := acc.String()
|
|
res, err := types.AccAddressFromBech32(str)
|
|
require.Nil(t, err)
|
|
require.Equal(t, acc, res)
|
|
|
|
str = hex.EncodeToString(acc)
|
|
res, err = types.AccAddressFromHex(str)
|
|
require.Nil(t, err)
|
|
require.Equal(t, acc, res)
|
|
}
|
|
|
|
for _, str := range invalidStrs {
|
|
_, err := types.AccAddressFromHex(str)
|
|
require.NotNil(t, err)
|
|
|
|
_, err = types.AccAddressFromBech32(str)
|
|
require.NotNil(t, err)
|
|
|
|
err = (*types.AccAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
|
require.NotNil(t, err)
|
|
}
|
|
}
|
|
|
|
func TestValAddr(t *testing.T) {
|
|
var pub ed25519.PubKeyEd25519
|
|
|
|
for i := 0; i < 20; i++ {
|
|
rand.Read(pub[:])
|
|
|
|
acc := types.ValAddress(pub.Address())
|
|
res := types.ValAddress{}
|
|
|
|
testMarshal(t, &acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
|
testMarshal(t, &acc, &res, acc.Marshal, (&res).Unmarshal)
|
|
|
|
str := acc.String()
|
|
res, err := types.ValAddressFromBech32(str)
|
|
require.Nil(t, err)
|
|
require.Equal(t, acc, res)
|
|
|
|
str = hex.EncodeToString(acc)
|
|
res, err = types.ValAddressFromHex(str)
|
|
require.Nil(t, err)
|
|
require.Equal(t, acc, res)
|
|
}
|
|
|
|
for _, str := range invalidStrs {
|
|
_, err := types.ValAddressFromHex(str)
|
|
require.NotNil(t, err)
|
|
|
|
_, err = types.ValAddressFromBech32(str)
|
|
require.NotNil(t, err)
|
|
|
|
err = (*types.ValAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
|
require.NotNil(t, err)
|
|
}
|
|
}
|
|
|
|
func TestConsAddress(t *testing.T) {
|
|
var pub ed25519.PubKeyEd25519
|
|
|
|
for i := 0; i < 20; i++ {
|
|
rand.Read(pub[:])
|
|
|
|
acc := types.ConsAddress(pub.Address())
|
|
res := types.ConsAddress{}
|
|
|
|
testMarshal(t, &acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
|
testMarshal(t, &acc, &res, acc.Marshal, (&res).Unmarshal)
|
|
|
|
str := acc.String()
|
|
res, err := types.ConsAddressFromBech32(str)
|
|
require.Nil(t, err)
|
|
require.Equal(t, acc, res)
|
|
|
|
str = hex.EncodeToString(acc)
|
|
res, err = types.ConsAddressFromHex(str)
|
|
require.Nil(t, err)
|
|
require.Equal(t, acc, res)
|
|
}
|
|
|
|
for _, str := range invalidStrs {
|
|
_, err := types.ConsAddressFromHex(str)
|
|
require.NotNil(t, err)
|
|
|
|
_, err = types.ConsAddressFromBech32(str)
|
|
require.NotNil(t, err)
|
|
|
|
err = (*types.ConsAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
|
require.NotNil(t, err)
|
|
}
|
|
}
|