Skip to content

Commit

Permalink
types: add test cases (#5747)
Browse files Browse the repository at this point in the history
Upgrade gomock dependency.
  • Loading branch information
Alessio Treglia authored Mar 4, 2020
1 parent b9ab834 commit da5c6ce
Show file tree
Hide file tree
Showing 16 changed files with 606 additions and 53 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ignore:
- "*.md"
- "*.rst"
- "**/*.pb.go"
- "types/*.pb.go"
- "tests/*"
- "tests/**/*"
- "x/**/*.pb.go"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/cosmos/ledger-cosmos-go v0.11.1
github.com/gogo/protobuf v1.3.1
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129
github.com/golang/mock v1.4.1
github.com/golang/protobuf v1.3.4
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekf
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 h1:tT8iWCYw4uOem71yYA3htfH+LNopJvcqZQshm56G5L4=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.1 h1:ocYkMQY5RrXTYgXl7ICpV0IXwlEQGwKIsery4gyXa1U=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -322,6 +322,7 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200107162124-548cf772de50/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down Expand Up @@ -368,3 +369,7 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
rsc.io/quote/v3 v3.1.0 h1:9JKUTTIUgS6kzR9mK1YuGKv6Nl+DijDNIc0ghT58FaY=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
44 changes: 14 additions & 30 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,8 @@ type AccAddress []byte

// AccAddressFromHex creates an AccAddress from a hex string.
func AccAddressFromHex(address string) (addr AccAddress, err error) {
if len(address) == 0 {
return addr, errors.New("decoding Bech32 address failed: must provide an address")
}

bz, err := hex.DecodeString(address)
if err != nil {
return nil, err
}

return AccAddress(bz), nil
bz, err := addressBytesFromHexString(address)
return AccAddress(bz), err
}

// VerifyAddressFormat verifies that the provided bytes form a valid address
Expand Down Expand Up @@ -265,16 +257,8 @@ type ValAddress []byte

// ValAddressFromHex creates a ValAddress from a hex string.
func ValAddressFromHex(address string) (addr ValAddress, err error) {
if len(address) == 0 {
return addr, errors.New("decoding Bech32 address failed: must provide an address")
}

bz, err := hex.DecodeString(address)
if err != nil {
return nil, err
}

return ValAddress(bz), nil
bz, err := addressBytesFromHexString(address)
return ValAddress(bz), err
}

// ValAddressFromBech32 creates a ValAddress from a Bech32 string.
Expand Down Expand Up @@ -420,16 +404,8 @@ type ConsAddress []byte

// ConsAddressFromHex creates a ConsAddress from a hex string.
func ConsAddressFromHex(address string) (addr ConsAddress, err error) {
if len(address) == 0 {
return addr, errors.New("decoding Bech32 address failed: must provide an address")
}

bz, err := hex.DecodeString(address)
if err != nil {
return nil, err
}

return ConsAddress(bz), nil
bz, err := addressBytesFromHexString(address)
return ConsAddress(bz), err
}

// ConsAddressFromBech32 creates a ConsAddress from a Bech32 string.
Expand Down Expand Up @@ -695,3 +671,11 @@ func GetFromBech32(bech32str, prefix string) ([]byte, error) {

return bz, nil
}

func addressBytesFromHexString(address string) ([]byte, error) {
if len(address) == 0 {
return nil, errors.New("decoding Bech32 address failed: must provide an address")
}

return hex.DecodeString(address)
}
80 changes: 80 additions & 0 deletions types/address_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package types_test

import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"strings"
"testing"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/crypto/ed25519"
Expand Down Expand Up @@ -37,6 +39,7 @@ func testMarshal(t *testing.T, original interface{}, res interface{}, marshal fu
}

func TestEmptyAddresses(t *testing.T) {
t.Parallel()
require.Equal(t, (types.AccAddress{}).String(), "")
require.Equal(t, (types.ValAddress{}).String(), "")
require.Equal(t, (types.ConsAddress{}).String(), "")
Expand Down Expand Up @@ -145,6 +148,9 @@ func TestRandBech32AccAddrConsistency(t *testing.T) {
err = (*types.AccAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
require.NotNil(t, err)
}

_, err := types.AccAddressFromHex("")
require.Equal(t, "decoding Bech32 address failed: must provide an address", err.Error())
}

func TestValAddr(t *testing.T) {
Expand All @@ -168,6 +174,7 @@ func TestValAddr(t *testing.T) {
res, err = types.ValAddressFromHex(str)
require.Nil(t, err)
require.Equal(t, acc, res)

}

for _, str := range invalidStrs {
Expand All @@ -180,6 +187,10 @@ func TestValAddr(t *testing.T) {
err = (*types.ValAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
require.NotNil(t, err)
}

// test empty string
_, err := types.ValAddressFromHex("")
require.Equal(t, "decoding Bech32 address failed: must provide an address", err.Error())
}

func TestConsAddress(t *testing.T) {
Expand Down Expand Up @@ -215,6 +226,10 @@ func TestConsAddress(t *testing.T) {
err = (*types.ConsAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
require.NotNil(t, err)
}

// test empty string
_, err := types.ConsAddressFromHex("")
require.Equal(t, "decoding Bech32 address failed: must provide an address", err.Error())
}

const letterBytes = "abcdefghijklmnopqrstuvwxyz"
Expand Down Expand Up @@ -410,3 +425,68 @@ func TestMustBech32ifyAddressBytes(t *testing.T) {
})
}
}

func TestAddressTypesEquals(t *testing.T) {
t.Parallel()
addr1 := secp256k1.GenPrivKey().PubKey().Address()
accAddr1 := types.AccAddress(addr1)
consAddr1 := types.ConsAddress(addr1)
valAddr1 := types.ValAddress(addr1)

addr2 := secp256k1.GenPrivKey().PubKey().Address()
accAddr2 := types.AccAddress(addr2)
consAddr2 := types.ConsAddress(addr2)
valAddr2 := types.ValAddress(addr2)

// equality
require.True(t, accAddr1.Equals(accAddr1))
require.True(t, consAddr1.Equals(consAddr1))
require.True(t, valAddr1.Equals(valAddr1))

// emptiness
require.True(t, types.AccAddress{}.Equals(types.AccAddress{}))
require.True(t, types.AccAddress{}.Equals(types.AccAddress(nil)))
require.True(t, types.AccAddress(nil).Equals(types.AccAddress{}))
require.True(t, types.AccAddress(nil).Equals(types.AccAddress(nil)))

require.True(t, types.ConsAddress{}.Equals(types.ConsAddress{}))
require.True(t, types.ConsAddress{}.Equals(types.ConsAddress(nil)))
require.True(t, types.ConsAddress(nil).Equals(types.ConsAddress{}))
require.True(t, types.ConsAddress(nil).Equals(types.ConsAddress(nil)))

require.True(t, types.ValAddress{}.Equals(types.ValAddress{}))
require.True(t, types.ValAddress{}.Equals(types.ValAddress(nil)))
require.True(t, types.ValAddress(nil).Equals(types.ValAddress{}))
require.True(t, types.ValAddress(nil).Equals(types.ValAddress(nil)))

require.False(t, accAddr1.Equals(accAddr2))
require.Equal(t, accAddr1.Equals(accAddr2), accAddr2.Equals(accAddr1))
require.False(t, consAddr1.Equals(consAddr2))
require.Equal(t, consAddr1.Equals(consAddr2), consAddr2.Equals(consAddr1))
require.False(t, valAddr1.Equals(valAddr2))
require.Equal(t, valAddr1.Equals(valAddr2), valAddr2.Equals(valAddr1))
}

func TestNilAddressTypesEmpty(t *testing.T) {
t.Parallel()
require.True(t, types.AccAddress(nil).Empty())
require.True(t, types.ConsAddress(nil).Empty())
require.True(t, types.ValAddress(nil).Empty())
}

func TestGetConsAddress(t *testing.T) {
t.Parallel()
pk := secp256k1.GenPrivKey().PubKey()
require.NotEqual(t, types.GetConsAddress(pk), pk.Address())
require.True(t, bytes.Equal(types.GetConsAddress(pk).Bytes(), pk.Address().Bytes()))
require.Panics(t, func() { types.GetConsAddress(crypto.PubKey(nil)) })
}

func TestGetFromBech32(t *testing.T) {
_, err := types.GetFromBech32("", "prefix")
require.Error(t, err)
require.Equal(t, "decoding Bech32 address failed: must provide an address", err.Error())
_, err = types.GetFromBech32("cosmos1qqqsyqcyq5rqwzqfys8f67", "x")
require.Error(t, err)
require.Equal(t, "invalid Bech32 prefix; expected x, got cosmos", err.Error())
}
47 changes: 47 additions & 0 deletions types/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package types_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestConfig_SetCoinType(t *testing.T) {
config := &sdk.Config{}
require.Equal(t, uint32(0), config.GetCoinType())
config.SetCoinType(99)
require.Equal(t, uint32(99), config.GetCoinType())

config.Seal()
require.Panics(t, func() { config.SetCoinType(99) })
}

func TestConfig_SetTxEncoder(t *testing.T) {
mockErr := errors.New("test")
config := &sdk.Config{}
require.Nil(t, config.GetTxEncoder())
encFunc := sdk.TxEncoder(func(tx sdk.Tx) ([]byte, error) { return nil, nil })
config.SetTxEncoder(encFunc)
_, err := config.GetTxEncoder()(sdk.Tx(nil))
require.Error(t, mockErr, err)

config.Seal()
require.Panics(t, func() { config.SetTxEncoder(encFunc) })
}

func TestConfig_SetFullFundraiserPath(t *testing.T) {
config := &sdk.Config{}
require.Equal(t, "", config.GetFullFundraiserPath())
config.SetFullFundraiserPath("test/path")
require.Equal(t, "test/path", config.GetFullFundraiserPath())

config.Seal()
require.Panics(t, func() { config.SetFullFundraiserPath("x/test/path") })
}

func TestKeyringServiceName(t *testing.T) {
require.Equal(t, sdk.DefaultKeyringServiceName, sdk.KeyringServiceName())
}
24 changes: 23 additions & 1 deletion types/context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -110,6 +111,7 @@ func TestContextWithCustom(t *testing.T) {
logger := NewMockLogger()
voteinfos := []abci.VoteInfo{{}}
meter := types.NewGasMeter(10000)
blockGasMeter := types.NewGasMeter(20000)
minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)}

ctx = types.NewContext(nil, header, ischeck, logger)
Expand All @@ -121,7 +123,8 @@ func TestContextWithCustom(t *testing.T) {
WithTxBytes(txbytes).
WithVoteInfos(voteinfos).
WithGasMeter(meter).
WithMinGasPrices(minGasPrices)
WithMinGasPrices(minGasPrices).
WithBlockGasMeter(blockGasMeter)
require.Equal(t, height, ctx.BlockHeight())
require.Equal(t, chainid, ctx.ChainID())
require.Equal(t, ischeck, ctx.IsCheckTx())
Expand All @@ -130,6 +133,25 @@ func TestContextWithCustom(t *testing.T) {
require.Equal(t, voteinfos, ctx.VoteInfos())
require.Equal(t, meter, ctx.GasMeter())
require.Equal(t, minGasPrices, ctx.MinGasPrices())
require.Equal(t, blockGasMeter, ctx.BlockGasMeter())

require.False(t, ctx.WithIsCheckTx(false).IsCheckTx())

// test IsReCheckTx
require.False(t, ctx.IsReCheckTx())
ctx = ctx.WithIsCheckTx(false)
ctx = ctx.WithIsReCheckTx(true)
require.True(t, ctx.IsCheckTx())
require.True(t, ctx.IsReCheckTx())

// test consensus param
require.Nil(t, ctx.ConsensusParams())
cp := &abci.ConsensusParams{}
require.Equal(t, cp, ctx.WithConsensusParams(cp).ConsensusParams())

// test inner context
newContext := context.WithValue(ctx.Context(), "key", "value")
require.NotEqual(t, ctx.Context(), ctx.WithContext(newContext).Context())
}

// Testing saving/loading of header fields to/from the context
Expand Down
32 changes: 32 additions & 0 deletions types/errors/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"io"
"strings"
"testing"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestABCInfo(t *testing.T) {
Expand Down Expand Up @@ -185,6 +188,8 @@ func TestRedact(t *testing.T) {
if err := Redact(serr); err == serr {
t.Error("reduct must not pass through a stdlib error")
}

require.Nil(t, Redact(nil))
}

func TestABCIInfoSerializeErr(t *testing.T) {
Expand Down Expand Up @@ -271,3 +276,30 @@ func (customErr) Codespace() string { return "extern" }
func (customErr) ABCICode() uint32 { return 999 }

func (customErr) Error() string { return "custom" }

func TestResponseCheckDeliverTx(t *testing.T) {
t.Parallel()
require.Equal(t, abci.ResponseCheckTx{
Codespace: "extern",
Code: 999,
Log: "custom",
GasWanted: int64(1),
GasUsed: int64(2),
}, ResponseCheckTx(customErr{}, 1, 2))
require.Equal(t, abci.ResponseDeliverTx{
Codespace: "extern",
Code: 999,
Log: "custom",
GasWanted: int64(1),
GasUsed: int64(2),
}, ResponseDeliverTx(customErr{}, 1, 2))
}

func TestQueryResult(t *testing.T) {
t.Parallel()
require.Equal(t, abci.ResponseQuery{
Codespace: "extern",
Code: 999,
Log: "custom",
}, QueryResult(customErr{}))
}
Loading

0 comments on commit da5c6ce

Please sign in to comment.