cosmos-sdk/modules/coin/tx_test.go

191 lines
4.5 KiB
Go
Raw Normal View History

2017-06-30 10:44:35 -07:00
package coin
import (
"testing"
"github.com/stretchr/testify/assert"
2017-06-30 11:26:17 -07:00
"github.com/stretchr/testify/require"
2017-07-06 05:59:45 -07:00
2017-06-30 11:26:17 -07:00
"github.com/tendermint/go-wire/data"
2017-07-06 05:59:45 -07:00
"github.com/tendermint/basecoin"
2017-06-30 10:44:35 -07:00
)
// these are some constructs for the test cases
var actors = []struct {
actor basecoin.Actor
valid bool
}{
{basecoin.Actor{}, false},
{basecoin.Actor{App: "fooz"}, false},
{basecoin.Actor{Address: []byte{1, 2, 3, 4}}, false},
{basecoin.Actor{App: "fooz", Address: []byte{1, 2, 3, 4}}, true},
{basecoin.Actor{ChainID: "dings", App: "fooz", Address: []byte{1, 2, 3, 4}}, true},
{basecoin.Actor{ChainID: "dat", App: "fooz"}, false},
}
var (
2017-07-06 05:59:45 -07:00
zeroCoin = Coin{"zeros", 0}
plusCoin = Coin{"plus", 23}
negCoin = Coin{"neg", -42}
2017-06-30 10:44:35 -07:00
)
var coins = []struct {
2017-07-06 05:59:45 -07:00
coins Coins
2017-06-30 10:44:35 -07:00
valid bool
}{
2017-07-06 05:59:45 -07:00
{Coins{}, false},
{Coins{zeroCoin}, false},
{Coins{plusCoin}, true},
{Coins{negCoin}, false},
{Coins{plusCoin, plusCoin}, false},
{Coins{plusCoin, zeroCoin}, false},
{Coins{negCoin, plusCoin}, false},
2017-06-30 10:44:35 -07:00
}
func TestTxValidateInput(t *testing.T) {
assert := assert.New(t)
for i, act := range actors {
for j, coin := range coins {
2017-07-12 09:54:07 -07:00
input := NewTxInput(act.actor, coin.coins)
err := input.ValidateBasic()
if act.valid && coin.valid {
assert.Nil(err, "%d,%d: %+v", i, j, err)
} else {
assert.NotNil(err, "%d,%d", i, j)
2017-06-30 10:44:35 -07:00
}
}
}
}
func TestTxValidateOutput(t *testing.T) {
assert := assert.New(t)
for i, act := range actors {
for j, coin := range coins {
input := NewTxOutput(act.actor, coin.coins)
err := input.ValidateBasic()
if act.valid && coin.valid {
assert.Nil(err, "%d,%d: %+v", i, j, err)
} else {
assert.NotNil(err, "%d,%d", i, j)
}
}
}
}
2017-06-30 11:00:27 -07:00
func TestTxValidateTx(t *testing.T) {
assert := assert.New(t)
addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}}
addr2 := basecoin.Actor{App: "coin", Address: []byte{3, 4}, ChainID: "over-there"}
addr3 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
noAddr := basecoin.Actor{}
2017-07-06 05:59:45 -07:00
noCoins := Coins{}
someCoins := Coins{{"atom", 123}}
moreCoins := Coins{{"atom", 124}}
otherCoins := Coins{{"btc", 15}}
2017-06-30 11:00:27 -07:00
bothCoins := someCoins.Plus(otherCoins)
2017-07-06 05:59:45 -07:00
minusCoins := Coins{{"eth", -34}}
2017-06-30 11:00:27 -07:00
// cases: all valid (one), all valid (multi)
// no input, no outputs, invalid inputs, invalid outputs
// totals don't match
cases := []struct {
valid bool
tx basecoin.Tx
2017-06-30 11:00:27 -07:00
}{
// 0-2. valid cases
{true, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins)},
[]TxOutput{NewTxOutput(addr2, someCoins)},
)},
{true, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins), NewTxInput(addr2, otherCoins)},
[]TxOutput{NewTxOutput(addr3, bothCoins)},
)},
{true, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, bothCoins)},
[]TxOutput{NewTxOutput(addr2, someCoins), NewTxOutput(addr3, otherCoins)},
)},
2017-06-30 11:00:27 -07:00
// 3-4. missing cases
{false, NewSendTx(
nil,
[]TxOutput{NewTxOutput(addr2, someCoins)},
)},
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins)},
nil,
)},
2017-06-30 11:00:27 -07:00
2017-07-12 09:54:07 -07:00
// 5-7. invalid inputs
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(noAddr, someCoins)},
[]TxOutput{NewTxOutput(addr2, someCoins)},
)},
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, noCoins)},
[]TxOutput{NewTxOutput(addr2, noCoins)},
)},
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, minusCoins)},
[]TxOutput{NewTxOutput(addr2, minusCoins)},
)},
2017-06-30 11:00:27 -07:00
2017-07-12 09:54:07 -07:00
// 8-10. totals don't match
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins)},
[]TxOutput{NewTxOutput(addr2, moreCoins)},
)},
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins), NewTxInput(addr2, minusCoins)},
[]TxOutput{NewTxOutput(addr3, someCoins)},
)},
{false, NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins), NewTxInput(addr2, moreCoins)},
[]TxOutput{NewTxOutput(addr3, bothCoins)},
)},
2017-06-30 11:00:27 -07:00
}
for i, tc := range cases {
err := tc.tx.ValidateBasic()
if tc.valid {
assert.Nil(err, "%d: %+v", i, err)
} else {
assert.NotNil(err, "%d", i)
}
}
2017-06-30 11:26:17 -07:00
}
func TestTxSerializeTx(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}}
addr2 := basecoin.Actor{App: "coin", Address: []byte{3, 4}}
2017-07-06 05:59:45 -07:00
someCoins := Coins{{"atom", 123}}
2017-06-30 11:26:17 -07:00
send := NewSendTx(
2017-07-12 09:54:07 -07:00
[]TxInput{NewTxInput(addr1, someCoins)},
[]TxOutput{NewTxOutput(addr2, someCoins)},
)
2017-06-30 11:26:17 -07:00
js, err := data.ToJSON(send)
require.Nil(err)
var tx basecoin.Tx
err = data.FromJSON(js, &tx)
require.Nil(err)
assert.Equal(send, tx)
bin, err := data.ToWire(send)
require.Nil(err)
var tx2 basecoin.Tx
err = data.FromWire(bin, &tx2)
require.Nil(err)
assert.Equal(send, tx2)
2017-06-30 11:00:27 -07:00
}