2022-11-07 16:50:25 +00:00
|
|
|
package types_test
|
2021-01-06 20:56:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-03 18:06:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/tests"
|
2022-11-07 16:50:25 +00:00
|
|
|
"github.com/evmos/ethermint/types"
|
2021-01-06 20:56:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsEmptyHash(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
hash string
|
|
|
|
expEmpty bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty string", "", true,
|
|
|
|
},
|
|
|
|
{
|
2021-09-03 18:06:36 +00:00
|
|
|
"zero hash", common.Hash{}.String(), true,
|
2021-01-06 20:56:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2021-09-03 18:06:36 +00:00
|
|
|
"non-empty hash", common.BytesToHash([]byte{1, 2, 3, 4}).String(), false,
|
2021-01-06 20:56:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2022-11-07 16:50:25 +00:00
|
|
|
require.Equal(t, tc.expEmpty, types.IsEmptyHash(tc.hash), tc.name)
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsZeroAddress(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
address string
|
|
|
|
expEmpty bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty string", "", true,
|
|
|
|
},
|
|
|
|
{
|
2021-09-03 18:06:36 +00:00
|
|
|
"zero address", common.Address{}.String(), true,
|
2021-01-06 20:56:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2021-09-03 18:06:36 +00:00
|
|
|
"non-empty address", common.BytesToAddress([]byte{1, 2, 3, 4}).String(), false,
|
2021-01-06 20:56:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2022-11-07 16:50:25 +00:00
|
|
|
require.Equal(t, tc.expEmpty, types.IsZeroAddress(tc.address), tc.name)
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 09:48:11 +00:00
|
|
|
|
|
|
|
func TestValidateAddress(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
address string
|
|
|
|
expError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty string", "", true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid address", "0x", true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero address", common.Address{}.String(), false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid address", tests.GenerateAddress().Hex(), false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2022-11-07 16:50:25 +00:00
|
|
|
err := types.ValidateAddress(tc.address)
|
2021-09-28 09:48:11 +00:00
|
|
|
|
|
|
|
if tc.expError {
|
|
|
|
require.Error(t, err, tc.name)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:51:28 +00:00
|
|
|
func TestValidateNonZeroAddress(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
address string
|
|
|
|
expError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty string", "", true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid address", "0x", true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero address", common.Address{}.String(), true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid address", tests.GenerateAddress().Hex(), false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2022-11-07 16:50:25 +00:00
|
|
|
err := types.ValidateNonZeroAddress(tc.address)
|
2022-04-07 10:51:28 +00:00
|
|
|
|
|
|
|
if tc.expError {
|
|
|
|
require.Error(t, err, tc.name)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 09:48:11 +00:00
|
|
|
func TestSafeInt64(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
value uint64
|
|
|
|
expError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no overflow", 10, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"overflow", 18446744073709551615, true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2022-11-07 16:50:25 +00:00
|
|
|
value, err := types.SafeInt64(tc.value)
|
2021-09-28 09:48:11 +00:00
|
|
|
if tc.expError {
|
|
|
|
require.Error(t, err, tc.name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err, tc.name)
|
|
|
|
require.Equal(t, int64(tc.value), value, tc.name)
|
|
|
|
}
|
|
|
|
}
|