From c2edd9ac1484f2e262a949d2ce4efbe288e49295 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 30 Jun 2017 19:44:35 +0200 Subject: [PATCH] Start testing the sendtx validation --- modules/coin/tx.go | 14 +++++-- modules/coin/tx_test.go | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 modules/coin/tx_test.go diff --git a/modules/coin/tx.go b/modules/coin/tx.go index 935b34bc8..7f5be7165 100644 --- a/modules/coin/tx.go +++ b/modules/coin/tx.go @@ -18,14 +18,17 @@ type TxInput struct { } func (txIn TxInput) ValidateBasic() error { - // TODO: knowledge of app-specific codings? if txIn.Address.App == "" { return errors.InvalidAddress() } + // TODO: knowledge of app-specific codings? + if len(txIn.Address.Address) == 0 { + return errors.InvalidAddress() + } if !txIn.Coins.IsValid() { return errors.InvalidCoins() } - if txIn.Coins.IsZero() { + if !txIn.Coins.IsPositive() { return errors.InvalidCoins() } if txIn.Sequence <= 0 { @@ -55,14 +58,17 @@ type TxOutput struct { } func (txOut TxOutput) ValidateBasic() error { - // TODO: knowledge of app-specific codings? if txOut.Address.App == "" { return errors.InvalidAddress() } + // TODO: knowledge of app-specific codings? + if len(txOut.Address.Address) == 0 { + return errors.InvalidAddress() + } if !txOut.Coins.IsValid() { return errors.InvalidCoins() } - if txOut.Coins.IsZero() { + if !txOut.Coins.IsPositive() { return errors.InvalidCoins() } return nil diff --git a/modules/coin/tx_test.go b/modules/coin/tx_test.go new file mode 100644 index 000000000..69780eecf --- /dev/null +++ b/modules/coin/tx_test.go @@ -0,0 +1,85 @@ +package coin + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tendermint/basecoin" + "github.com/tendermint/basecoin/types" +) + +// 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 ( + zeroCoin = types.Coin{"zeros", 0} + plusCoin = types.Coin{"plus", 23} + negCoin = types.Coin{"neg", -42} +) + +var coins = []struct { + coins types.Coins + valid bool +}{ + {types.Coins{}, false}, + {types.Coins{zeroCoin}, false}, + {types.Coins{plusCoin}, true}, + {types.Coins{negCoin}, false}, + {types.Coins{plusCoin, plusCoin}, false}, + {types.Coins{plusCoin, zeroCoin}, false}, + {types.Coins{negCoin, plusCoin}, false}, +} + +func TestTxValidateInput(t *testing.T) { + assert := assert.New(t) + + seqs := []struct { + seq int + valid bool + }{ + {-3, false}, + {0, false}, + {1, true}, + {6571265735, true}, + } + + for i, act := range actors { + for j, coin := range coins { + for k, seq := range seqs { + input := NewTxInput(act.actor, coin.coins, seq.seq) + err := input.ValidateBasic() + if act.valid && coin.valid && seq.valid { + assert.Nil(err, "%d,%d,%d: %+v", i, j, k, err) + } else { + assert.NotNil(err, "%d,%d,%d", i, j, k) + } + } + } + } +} + +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) + } + } + } +}