From 5a5a4025185569a997e67767c90298e7c80860ec Mon Sep 17 00:00:00 2001 From: dauTT Date: Sat, 18 Jul 2020 19:46:56 +0200 Subject: [PATCH] Merge PR #6775: x/auth/simulation/genesis.go: add unit tests --- x/auth/simulation/genesis_test.go | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 x/auth/simulation/genesis_test.go diff --git a/x/auth/simulation/genesis_test.go b/x/auth/simulation/genesis_test.go new file mode 100644 index 000000000..bbec0a479 --- /dev/null +++ b/x/auth/simulation/genesis_test.go @@ -0,0 +1,72 @@ +package simulation_test + +import ( + "encoding/json" + "math/rand" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/auth/simulation" + "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + cdc := codec.New() + // Make sure to register cdc. + // otherwise the test will panic + types.RegisterCodec(cdc) + + s := rand.NewSource(1) + r := rand.New(s) + + simState := module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + NumBonded: 3, + Accounts: simtypes.RandomAccounts(r, 3), + InitialStake: 1000, + GenState: make(map[string]json.RawMessage), + } + + simulation.RandomizedGenState(&simState) + + var authGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &authGenesis) + + require.Equal(t, uint64(0x8c), authGenesis.Params.GetMaxMemoCharacters()) + require.Equal(t, uint64(0x2b6), authGenesis.Params.GetSigVerifyCostED25519()) + require.Equal(t, uint64(0x1ff), authGenesis.Params.GetSigVerifyCostSecp256k1()) + require.Equal(t, uint64(9), authGenesis.Params.GetTxSigLimit()) + require.Equal(t, uint64(5), authGenesis.Params.GetTxSizeCostPerByte()) + require.Len(t, authGenesis.Accounts, 3) + require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", authGenesis.Accounts[2].GetAddress().String()) + require.Equal(t, uint64(0), authGenesis.Accounts[2].GetAccountNumber()) + require.Equal(t, uint64(0), authGenesis.Accounts[2].GetSequence()) +} + +// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. +func TestRandomizedGenState1(t *testing.T) { + cdc := codec.New() + + s := rand.NewSource(1) + r := rand.New(s) + + simState := module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + NumBonded: 3, + Accounts: simtypes.RandomAccounts(r, 3), + InitialStake: 1000, + GenState: make(map[string]json.RawMessage), + } + + require.Panicsf(t, func() { simulation.RandomizedGenState(&simState) }, "Unregistered interface types.GenesisAccount") +}