diff --git a/cmd/commands/utils_test.go b/cmd/commands/utils_test.go index c530b04a14..f16927d486 100644 --- a/cmd/commands/utils_test.go +++ b/cmd/commands/utils_test.go @@ -1,12 +1,40 @@ package commands import ( + "encoding/hex" "testing" "github.com/stretchr/testify/assert" "github.com/tendermint/basecoin/types" ) +func TestHex(t *testing.T) { + + //test isHex + hexNoPrefix := hex.EncodeToString([]byte("foobar")) + hexWPrefix := "0x" + hexNoPrefix + str := "foobar" + strWPrefix := "0xfoobar" + + //define the list of coin tests + var testList = []struct { + testPass bool + errMsg string + }{ + {isHex(hexWPrefix), "isHex not identifying hex with 0x prefix"}, + {!isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix"}, + {!isHex(str), "isHex shouldn't identify non-hex string"}, + {!isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix"}, + {StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass, tl.errMsg) + } + +} + //Test the parse coin and parse coins functionality func TestParse(t *testing.T) { @@ -26,15 +54,27 @@ func TestParse(t *testing.T) { return coin } - //testing ParseCoin Function - assert.True(t, types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin") - assert.True(t, types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins") - assert.True(t, types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins") + //define the list of coin tests + var testList = []struct { + testPass bool + errMsg string + }{ + //testing ParseCoin Function + {types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin"}, + {types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins"}, + {types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins"}, - //testing ParseCoins Function - assert.True(t, types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), "parseCoins doesn't parse a single coin") - assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")), - "parseCoins doesn't properly parse two coins") - assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")), - "parseCoins doesn't properly parse two coins which use spaces") + //testing ParseCoins Function + {types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), + "parseCoins doesn't parse a single coin"}, + {types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")), + "parseCoins doesn't properly parse two coins"}, + {types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")), + "parseCoins doesn't properly parse two coins which use spaces"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass, tl.errMsg) + } } diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 9d849d1d05..79694c4052 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -35,7 +35,7 @@ func New() *CounterPlugin { return &CounterPlugin{} } -func (cp *CounterPlugin) SetOption(store types.KVStore, key string, value string) (log string) { +func (cp *CounterPlugin) SetOption(store types.KVStore, key, value string) (log string) { return "" } diff --git a/state/state.go b/state/state.go index 026c6a9ad9..7fb6c48bac 100644 --- a/state/state.go +++ b/state/state.go @@ -38,7 +38,7 @@ func (s *State) GetChainID() string { } func (s *State) Get(key []byte) (value []byte) { - if s.readCache != nil { + if s.readCache != nil { //if not a cachewrap value, ok := s.readCache[string(key)] if ok { return value @@ -48,7 +48,7 @@ func (s *State) Get(key []byte) (value []byte) { } func (s *State) Set(key []byte, value []byte) { - if s.readCache != nil { + if s.readCache != nil { //if not a cachewrap s.readCache[string(key)] = value } s.store.Set(key, value) @@ -78,8 +78,14 @@ func (s *State) CacheSync() { } func (s *State) Commit() abci.Result { - s.readCache = make(map[string][]byte) - return s.store.(*eyes.Client).CommitSync() + switch s.store.(type) { + case *eyes.Client: + s.readCache = make(map[string][]byte) + return s.store.(*eyes.Client).CommitSync() + default: + return abci.NewError(abci.CodeType_InternalError, "can only use Commit if store is merkleeyes") + } + } //---------------------------------------- diff --git a/state/state_test.go b/state/state_test.go new file mode 100644 index 0000000000..162b8e9f76 --- /dev/null +++ b/state/state_test.go @@ -0,0 +1,109 @@ +package state + +import ( + "bytes" + "testing" + + "github.com/tendermint/basecoin/types" + eyes "github.com/tendermint/merkleeyes/client" + + "github.com/stretchr/testify/assert" +) + +func TestState(t *testing.T) { + + //States and Stores for tests + store := types.NewMemKVStore() + state := NewState(store) + cache := state.CacheWrap() + eyesCli := eyes.NewLocalClient("", 0) + + //Account and address for tests + dumAddr := []byte("dummyAddress") + + acc := &types.Account{ + PubKey: nil, + Sequence: 1, + Balance: nil, + } + + //reset the store/state/cache + reset := func() { + store = types.NewMemKVStore() + state = NewState(store) + cache = state.CacheWrap() + } + + //set the state to using the eyesCli instead of MemKVStore + useEyesCli := func() { + state = NewState(eyesCli) + cache = state.CacheWrap() + } + + //key value pairs to be tested within the system + keyvalue := []struct { + key string + value string + }{ + {"foo", "snake"}, + {"bar", "mouse"}, + } + + //set the kvc to have all the key value pairs + setRecords := func(kv types.KVStore) { + for _, n := range keyvalue { + kv.Set([]byte(n.key), []byte(n.value)) + } + } + + //store has all the key value pairs + storeHasAll := func(kv types.KVStore) bool { + for _, n := range keyvalue { + if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) { + return false + } + } + return true + } + + //define the test list + testList := []struct { + testPass func() bool + errMsg string + }{ + //test chainID + {func() bool { state.SetChainID("testchain"); return state.GetChainID() == "testchain" }, + "ChainID is improperly stored"}, + + //test basic retrieve + {func() bool { setRecords(state); return storeHasAll(state) }, + "state doesn't retrieve after Set"}, + + // Test account retrieve + {func() bool { state.SetAccount(dumAddr, acc); return state.GetAccount(dumAddr).Sequence == 1 }, + "GetAccount not retrieving"}, + + //Test CacheWrap with local mem store + {func() bool { reset(); setRecords(cache); return !storeHasAll(store) }, + "store retrieving before CacheSync"}, + {func() bool { cache.CacheSync(); return storeHasAll(store) }, + "store doesn't retrieve after CacheSync"}, + + //Test Commit on state with non-merkle store + {func() bool { return !state.Commit().IsOK() }, + "Commit shouldn't work with non-merkle store"}, + + //Test CacheWrap with merkleeyes client store + {func() bool { useEyesCli(); setRecords(cache); return !storeHasAll(eyesCli) }, + "eyesCli retrieving before Commit"}, + {func() bool { cache.CacheSync(); return state.Commit().IsOK() }, + "Bad Commit"}, + {func() bool { return storeHasAll(eyesCli) }, + "eyesCli doesn't retrieve after Commit"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass(), tl.errMsg) + } +} diff --git a/types/account.go b/types/account.go index d78f560a6e..d1e62d8321 100644 --- a/types/account.go +++ b/types/account.go @@ -13,6 +13,9 @@ type Account struct { } func (acc *Account) Copy() *Account { + if acc == nil { + return nil + } accCopy := *acc return &accCopy } diff --git a/types/account_test.go b/types/account_test.go new file mode 100644 index 0000000000..c6385f36ef --- /dev/null +++ b/types/account_test.go @@ -0,0 +1,26 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAccount(t *testing.T) { + + acc := Account{ + PubKey: nil, + Sequence: 0, + Balance: nil, + } + + //test Copy + accCopy := acc.Copy() + assert.True(t, &acc != accCopy, "Account Copy Error") + assert.True(t, acc.Sequence == accCopy.Sequence) + + //test sending nils for panic + var nilAcc *Account + nilAcc.String() + nilAcc.Copy() +} diff --git a/types/coin_test.go b/types/coin_test.go index 158f159c50..36767ae6b9 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -2,79 +2,64 @@ package types import ( "testing" + + cmn "github.com/tendermint/go-common" + + "github.com/stretchr/testify/assert" ) func TestCoins(t *testing.T) { - coins := Coins{ + + //Define the coins to be used in tests + good := Coins{ Coin{"GAS", 1}, Coin{"MINERAL", 1}, Coin{"TREE", 1}, } - - if !coins.IsValid() { - t.Fatal("Coins are valid") + neg := good.Negative() + sum := good.Plus(neg) + empty := Coins{ + Coin{"GOLD", 0}, } - - if !coins.IsPositive() { - t.Fatalf("Expected coins to be positive: %v", coins) - } - - negCoins := coins.Negative() - if negCoins.IsPositive() { - t.Fatalf("Expected neg coins to not be positive: %v", negCoins) - } - - sumCoins := coins.Plus(negCoins) - if len(sumCoins) != 0 { - t.Fatal("Expected 0 coins") - } -} - -func TestCoinsBadSort(t *testing.T) { - coins := Coins{ + badSort1 := Coins{ Coin{"TREE", 1}, Coin{"GAS", 1}, Coin{"MINERAL", 1}, } - - if coins.IsValid() { - t.Fatal("Coins are not sorted") - } -} - -func TestCoinsBadSort2(t *testing.T) { - // both are after the first one, but the second and third are in the wrong order - coins := Coins{ + badSort2 := Coins{ // both are after the first one, but the second and third are in the wrong order Coin{"GAS", 1}, Coin{"TREE", 1}, Coin{"MINERAL", 1}, } - - if coins.IsValid() { - t.Fatal("Coins are not sorted") - } -} - -func TestCoinsBadAmount(t *testing.T) { - coins := Coins{ + badAmt := Coins{ Coin{"GAS", 1}, Coin{"TREE", 0}, Coin{"MINERAL", 1}, } - - if coins.IsValid() { - t.Fatal("Coins cannot include 0 amounts") - } -} - -func TestCoinsDuplicate(t *testing.T) { - coins := Coins{ + dup := Coins{ Coin{"GAS", 1}, Coin{"GAS", 1}, Coin{"MINERAL", 1}, } - if coins.IsValid() { - t.Fatal("Duplicate coin") + //define the list of coin tests + var testList = []struct { + testPass bool + errMsg string + }{ + {good.IsValid(), "Coins are valid"}, + {good.IsPositive(), cmn.Fmt("Expected coins to be positive: %v", good)}, + {good.IsGTE(empty), cmn.Fmt("Expected %v to be >= %v", good, empty)}, + {!neg.IsPositive(), cmn.Fmt("Expected neg coins to not be positive: %v", neg)}, + {len(sum) == 0, "Expected 0 coins"}, + {!badSort1.IsValid(), "Coins are not sorted"}, + {!badSort2.IsValid(), "Coins are not sorted"}, + {!badAmt.IsValid(), "Coins cannot include 0 amounts"}, + {!dup.IsValid(), "Duplicate coin"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass, tl.errMsg) } } diff --git a/types/kvstore.go b/types/kvstore.go index 96f8c7d0ff..15088bdfb3 100644 --- a/types/kvstore.go +++ b/types/kvstore.go @@ -113,6 +113,7 @@ func (kvc *KVCache) Get(key []byte) (value []byte) { } } +//Update the store with the values from the cache func (kvc *KVCache) Sync() { for e := kvc.keys.Front(); e != nil; e = e.Next() { key := e.Value.([]byte) diff --git a/types/kvstore_test.go b/types/kvstore_test.go new file mode 100644 index 0000000000..76ba009f30 --- /dev/null +++ b/types/kvstore_test.go @@ -0,0 +1,79 @@ +package types + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKVStore(t *testing.T) { + + //stores to be tested + ms := NewMemKVStore() + store := NewMemKVStore() + kvc := NewKVCache(store) + + //key value pairs to be tested within the system + var keyvalue = []struct { + key string + value string + }{ + {"foo", "snake"}, + {"bar", "mouse"}, + } + + //set the kvc to have all the key value pairs + setRecords := func(kv KVStore) { + for _, n := range keyvalue { + kv.Set([]byte(n.key), []byte(n.value)) + } + } + + //store has all the key value pairs + storeHasAll := func(kv KVStore) bool { + for _, n := range keyvalue { + if !bytes.Equal(kv.Get([]byte(n.key)), []byte(n.value)) { + return false + } + } + return true + } + + //define the test list + var testList = []struct { + testPass func() bool + errMsg string + }{ + //test read/write for MemKVStore + {func() bool { setRecords(ms); return storeHasAll(ms) }, + "MemKVStore doesn't retrieve after Set"}, + + //test read/write for KVCache + {func() bool { setRecords(kvc); return storeHasAll(kvc) }, + "KVCache doesn't retrieve after Set"}, + + //test reset + {func() bool { kvc.Reset(); return !storeHasAll(kvc) }, + "KVCache retrieving after reset"}, + + //test sync + {func() bool { setRecords(kvc); return !storeHasAll(store) }, + "store retrieving before synced"}, + {func() bool { kvc.Sync(); return storeHasAll(store) }, + "store isn't retrieving after synced"}, + + //test logging + {func() bool { return len(kvc.GetLogLines()) == 0 }, + "logging events existed before using SetLogging"}, + {func() bool { kvc.SetLogging(); setRecords(kvc); return len(kvc.GetLogLines()) == 2 }, + "incorrect number of logging events recorded"}, + {func() bool { kvc.ClearLogLines(); return len(kvc.GetLogLines()) == 0 }, + "logging events still exists after ClearLogLines"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass(), tl.errMsg) + } +} diff --git a/types/plugin.go b/types/plugin.go index ac95cac679..9783f2c7c4 100644 --- a/types/plugin.go +++ b/types/plugin.go @@ -15,7 +15,7 @@ type Plugin interface { RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result) // Other ABCI message handlers - SetOption(store KVStore, key string, value string) (log string) + SetOption(store KVStore, key, value string) (log string) InitChain(store KVStore, vals []*abci.Validator) BeginBlock(store KVStore, hash []byte, header *abci.Header) EndBlock(store KVStore, height uint64) abci.ResponseEndBlock diff --git a/types/plugin_test.go b/types/plugin_test.go new file mode 100644 index 0000000000..9f4b332d0c --- /dev/null +++ b/types/plugin_test.go @@ -0,0 +1,56 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + abci "github.com/tendermint/abci/types" +) + +//---------------------------------- + +type Dummy struct{} + +func (d *Dummy) Name() string { + return "dummy" +} +func (d *Dummy) RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result) { + return +} +func (d *Dummy) SetOption(storei KVStore, key, value string) (log string) { + return "" +} +func (d *Dummy) InitChain(store KVStore, vals []*abci.Validator) { +} +func (d *Dummy) BeginBlock(store KVStore, hash []byte, header *abci.Header) { +} +func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBlock) { + return +} + +//---------------------------------- + +func TestPlugin(t *testing.T) { + + plugins := NewPlugins() + + //define the test list + var testList = []struct { + testPass func() bool + errMsg string + }{ + {func() bool { return (len(plugins.GetList()) == 0) }, + "plugins object init with a objects"}, + {func() bool { plugins.RegisterPlugin(&Dummy{}); return (len(plugins.GetList()) == 1) }, + "plugin wasn't added to plist after registered"}, + {func() bool { return (plugins.GetByName("dummy").Name() == "dummy") }, + "plugin wasn't retrieved properly with GetByName"}, + } + + //execute the tests + for _, tl := range testList { + assert.True(t, tl.testPass(), tl.errMsg) + } + +} diff --git a/types/tx_test.go b/types/tx_test.go index 43bcc032c6..072b78d206 100644 --- a/types/tx_test.go +++ b/types/tx_test.go @@ -3,7 +3,9 @@ package types import ( "testing" - . "github.com/tendermint/go-common" + cmn "github.com/tendermint/go-common" + + "github.com/stretchr/testify/assert" ) var chainID string = "test_chain" @@ -36,11 +38,11 @@ func TestSendTxSignable(t *testing.T) { }, } signBytes := sendTx.SignBytes(chainID) - signBytesHex := Fmt("%X", signBytes) + signBytesHex := cmn.Fmt("%X", signBytes) expected := "010A746573745F636861696E0100000000000000DE00000000000000006F01020106696E7075743101010000000000000030390301093200000106696E70757432010100000000000000006F01DE0000010201076F757470757431010100000000000000014D01076F75747075743201010000000000000001BC" - if signBytesHex != expected { - t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex) - } + + assert.True(t, signBytesHex == expected, + cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)) } func TestAppTxSignable(t *testing.T) { @@ -56,9 +58,9 @@ func TestAppTxSignable(t *testing.T) { Data: []byte("data1"), } signBytes := callTx.SignBytes(chainID) - signBytesHex := Fmt("%X", signBytes) + signBytesHex := cmn.Fmt("%X", signBytes) expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131" - if signBytesHex != expected { - t.Errorf("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex) - } + + assert.True(t, signBytesHex == expected, + cmn.Fmt("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)) }