cosmos-sdk/app/app_test.go

260 lines
7.3 KiB
Go
Raw Normal View History

package app
import (
"encoding/hex"
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
2017-03-23 17:51:50 -07:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types"
2017-07-03 12:34:08 -07:00
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/txs"
"github.com/tendermint/basecoin/types"
2017-07-03 12:34:08 -07:00
crypto "github.com/tendermint/go-crypto"
2017-05-01 07:03:54 -07:00
wire "github.com/tendermint/go-wire"
eyes "github.com/tendermint/merkleeyes/client"
2017-05-01 07:03:54 -07:00
"github.com/tendermint/tmlibs/log"
)
2017-04-13 18:33:39 -07:00
//--------------------------------------------------------
// test environment is a list of input and output accounts
type appTest struct {
t *testing.T
chainID string
app *Basecoin
accIn types.PrivAccount
accOut types.PrivAccount
2017-04-13 18:33:39 -07:00
}
func newAppTest(t *testing.T) *appTest {
at := &appTest{
t: t,
chainID: "test_chain_id",
}
at.reset()
return at
}
// make a tx sending 5mycoin from each accIn to accOut
2017-07-03 12:34:08 -07:00
func (at *appTest) getTx(seq int, coins types.Coins) basecoin.Tx {
addrIn := at.accIn.Account.PubKey.Address()
addrOut := at.accOut.Account.PubKey.Address()
in := []coin.TxInput{{Address: stack.SigPerm(addrIn), Coins: coins, Sequence: seq}}
out := []coin.TxOutput{{Address: stack.SigPerm(addrOut), Coins: coins}}
tx := coin.NewSendTx(in, out)
tx = txs.NewChain(at.chainID, tx)
stx := txs.NewMulti(tx)
txs.Sign(stx, at.accIn.PrivKey)
return stx.Wrap()
2017-04-13 18:33:39 -07:00
}
// set the account on the app through SetOption
func (at *appTest) acc2app(acc types.Account) {
accBytes, err := json.Marshal(acc)
require.Nil(at.t, err)
res := at.app.SetOption("coin/account", string(accBytes))
2017-04-13 18:33:39 -07:00
require.EqualValues(at.t, res, "Success")
}
// reset the in and out accs to be one account each with 7mycoin
func (at *appTest) reset() {
at.accIn = types.MakeAcc("input0")
at.accOut = types.MakeAcc("output0")
2017-04-13 18:33:39 -07:00
eyesCli := eyes.NewLocalClient("", 0)
// l := log.TestingLogger().With("module", "app"),
l := log.NewTMLogger(os.Stdout).With("module", "app")
l = log.NewTracingLogger(l)
at.app = NewBasecoin(
DefaultHandler(),
eyesCli,
l,
)
2017-04-13 18:33:39 -07:00
res := at.app.SetOption("base/chain_id", at.chainID)
require.EqualValues(at.t, res, "Success")
at.acc2app(at.accIn.Account)
at.acc2app(at.accOut.Account)
2017-04-13 18:33:39 -07:00
resabci := at.app.Commit()
require.True(at.t, resabci.IsOK(), resabci)
}
2017-07-03 12:34:08 -07:00
func getBalance(pk crypto.PubKey, state types.KVStore) (types.Coins, error) {
return getAddr(pk.Address(), state)
}
func getAddr(addr []byte, state types.KVStore) (types.Coins, error) {
actor := stack.SigPerm(addr)
acct, err := coin.NewAccountant("").GetAccount(state, actor)
return acct.Coins, err
}
2017-04-13 18:33:39 -07:00
// returns the final balance and expected balance for input and output accounts
2017-07-03 12:34:08 -07:00
func (at *appTest) exec(t *testing.T, tx basecoin.Tx, checkTx bool) (res abci.Result, diffIn, diffOut types.Coins) {
require := require.New(t)
2017-04-13 18:33:39 -07:00
2017-07-03 12:34:08 -07:00
initBalIn, err := getBalance(at.accIn.Account.PubKey, at.app.GetState())
require.Nil(err, "%+v", err)
initBalOut, err := getBalance(at.accOut.Account.PubKey, at.app.GetState())
require.Nil(err, "%+v", err)
2017-04-13 18:33:39 -07:00
2017-07-03 12:34:08 -07:00
txBytes := wire.BinaryBytes(tx)
2017-04-13 18:33:39 -07:00
if checkTx {
res = at.app.CheckTx(txBytes)
} else {
res = at.app.DeliverTx(txBytes)
}
2017-07-03 12:34:08 -07:00
endBalIn, err := getBalance(at.accIn.Account.PubKey, at.app.GetState())
require.Nil(err, "%+v", err)
endBalOut, err := getBalance(at.accOut.Account.PubKey, at.app.GetState())
require.Nil(err, "%+v", err)
return res, endBalIn.Minus(initBalIn), endBalOut.Minus(initBalOut)
2017-04-13 18:33:39 -07:00
}
//--------------------------------------------------------
func TestSetOption(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
eyesCli := eyes.NewLocalClient("", 0)
2017-07-03 12:34:08 -07:00
app := NewBasecoin(
DefaultHandler(),
eyesCli,
log.TestingLogger().With("module", "app"),
)
//testing ChainID
2017-03-28 13:32:55 -07:00
chainID := "testChain"
res := app.SetOption("base/chain_id", chainID)
assert.EqualValues(app.GetState().GetChainID(), chainID)
assert.EqualValues(res, "Success")
// make a nice account...
2017-07-03 12:34:08 -07:00
accIn := types.MakeAcc("input0").Account
accsInBytes, err := json.Marshal(accIn)
assert.Nil(err)
res = app.SetOption("coin/account", string(accsInBytes))
require.EqualValues(res, "Success")
2017-07-03 12:34:08 -07:00
// make sure it is set correctly, with some balance
2017-07-03 12:34:08 -07:00
coins, err := getBalance(accIn.PubKey, app.state)
require.Nil(err)
assert.Equal(accIn.Balance, coins)
// let's parse an account with badly sorted coins...
unsortAddr, err := hex.DecodeString("C471FB670E44D219EE6DF2FC284BE38793ACBCE1")
require.Nil(err)
unsortCoins := types.Coins{{"BTC", 789}, {"eth", 123}}
unsortAcc := `{
"pub_key": {
"type": "ed25519",
"data": "AD084F0572C116D618B36F2EB08240D1BAB4B51716CCE0E7734B89C8936DCE9A"
},
"coins": [
{
"denom": "eth",
"amount": 123
},
{
"denom": "BTC",
"amount": 789
}
]
}`
res = app.SetOption("coin/account", unsortAcc)
require.EqualValues(res, "Success")
2017-07-03 12:34:08 -07:00
coins, err = getAddr(unsortAddr, app.state)
require.Nil(err)
assert.True(coins.IsValid())
assert.Equal(unsortCoins, coins)
res = app.SetOption("base/dslfkgjdas", "")
assert.NotEqual(res, "Success")
res = app.SetOption("dslfkgjdas", "")
assert.NotEqual(res, "Success")
res = app.SetOption("dslfkgjdas/szfdjzs", "")
assert.NotEqual(res, "Success")
}
2017-04-13 18:33:39 -07:00
// Test CheckTx and DeliverTx with insufficient and sufficient balance
2017-03-23 17:51:50 -07:00
func TestTx(t *testing.T) {
assert := assert.New(t)
2017-03-28 13:32:55 -07:00
at := newAppTest(t)
2017-03-23 17:51:50 -07:00
//Bad Balance
at.accIn.Balance = types.Coins{{"mycoin", 2}}
at.acc2app(at.accIn.Account)
2017-07-03 12:34:08 -07:00
res, _, _ := at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), true)
2017-04-17 16:53:06 -07:00
assert.True(res.IsErr(), "ExecTx/Bad CheckTx: Expected error return from ExecTx, returned: %v", res)
2017-07-03 12:34:08 -07:00
res, diffIn, diffOut := at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), false)
2017-04-17 16:53:06 -07:00
assert.True(res.IsErr(), "ExecTx/Bad DeliverTx: Expected error return from ExecTx, returned: %v", res)
2017-07-03 12:34:08 -07:00
assert.True(diffIn.IsZero())
assert.True(diffOut.IsZero())
//Regular CheckTx
2017-03-28 13:32:55 -07:00
at.reset()
2017-07-03 12:34:08 -07:00
res, _, _ = at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), true)
2017-04-17 16:53:06 -07:00
assert.True(res.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", res)
//Regular DeliverTx
2017-03-28 13:32:55 -07:00
at.reset()
2017-07-03 12:34:08 -07:00
amt := types.Coins{{"mycoin", 3}}
res, diffIn, diffOut = at.exec(t, at.getTx(1, amt), false)
2017-04-17 16:53:06 -07:00
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
2017-07-03 12:34:08 -07:00
assert.Equal(amt.Negative(), diffIn)
assert.Equal(amt, diffOut)
2017-03-23 17:51:50 -07:00
}
2017-03-23 17:51:50 -07:00
func TestQuery(t *testing.T) {
assert := assert.New(t)
2017-03-28 13:32:55 -07:00
at := newAppTest(t)
2017-07-03 12:34:08 -07:00
res, _, _ := at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), false)
2017-04-17 16:53:06 -07:00
assert.True(res.IsOK(), "Commit, DeliverTx: Expected OK return from DeliverTx, Error: %v", res)
2017-03-28 13:32:55 -07:00
resQueryPreCommit := at.app.Query(abci.RequestQuery{
Path: "/account",
Data: at.accIn.Account.PubKey.Address(),
})
2017-03-28 13:32:55 -07:00
res = at.app.Commit()
assert.True(res.IsOK(), res)
2017-03-28 13:32:55 -07:00
resQueryPostCommit := at.app.Query(abci.RequestQuery{
Path: "/account",
Data: at.accIn.Account.PubKey.Address(),
})
assert.NotEqual(resQueryPreCommit, resQueryPostCommit, "Query should change before/after commit")
2017-03-23 17:51:50 -07:00
}
func TestSplitKey(t *testing.T) {
assert := assert.New(t)
prefix, suffix := splitKey("foo/bar")
assert.EqualValues("foo", prefix)
assert.EqualValues("bar", suffix)
prefix, suffix = splitKey("foobar")
assert.EqualValues("base", prefix)
assert.EqualValues("foobar", suffix)
prefix, suffix = splitKey("some/complex/issue")
assert.EqualValues("some", prefix)
assert.EqualValues("complex/issue", suffix)
}