tests: updated due to updating old accounts as a part of migration process

This commit is contained in:
Sale Djenic 2023-03-27 16:33:00 +02:00 committed by saledjenic
parent 5d79b3514c
commit e9482e3974
7 changed files with 335 additions and 35 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
@ -19,6 +20,7 @@ import (
gethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/connection"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
@ -29,6 +31,7 @@ import (
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/t/utils"
"github.com/status-im/status-go/transactions"
)
@ -37,6 +40,7 @@ var (
networks = json.RawMessage("{}")
testSettings = settings.Settings{
Address: types.HexToAddress("0xeC540f3745Ff2964AFC1171a5A0DD726d1F6B472"),
DisplayName: "UserDisplayName",
CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress("0xe1300f99fDF7346986CbC766903245087394ecd0"),
EIP1581Address: types.HexToAddress("0xe1DDDE9235a541d1344550d969715CF43982de9f"),
@ -52,10 +56,74 @@ var (
WalletRootAddress: types.HexToAddress("0xeB591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")}
)
func setupTestDB() (*sql.DB, func() error, error) {
tmpfile, err := ioutil.TempFile("", "tests")
if err != nil {
return nil, nil, err
}
db, err := appdatabase.InitializeDB(tmpfile.Name(), "tests", sqlite.ReducedKDFIterationsNumber)
if err != nil {
return nil, nil, err
}
return db, func() error {
err := db.Close()
if err != nil {
return err
}
return os.Remove(tmpfile.Name())
}, nil
}
func setupTestMultiDB() (*multiaccounts.Database, func() error, error) {
tmpfile, err := ioutil.TempFile("", "tests")
if err != nil {
return nil, nil, err
}
db, err := multiaccounts.InitializeDB(tmpfile.Name())
if err != nil {
return nil, nil, err
}
return db, func() error {
err := db.Close()
if err != nil {
return err
}
return os.Remove(tmpfile.Name())
}, nil
}
func setupGethStatusBackend() (*GethStatusBackend, func() error, func() error, error) {
db, stop1, err := setupTestDB()
if err != nil {
return nil, nil, nil, err
}
backend := NewGethStatusBackend()
backend.StatusNode().SetAppDB(db)
ma, stop2, err := setupTestMultiDB()
backend.StatusNode().SetMultiaccountsDB(ma)
return backend, stop1, stop2, err
}
func TestBackendStartNodeConcurrently(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -90,7 +158,21 @@ func TestBackendStartNodeConcurrently(t *testing.T) {
func TestBackendRestartNodeConcurrently(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
count := 3
@ -118,7 +200,21 @@ func TestBackendRestartNodeConcurrently(t *testing.T) {
func TestBackendGettersConcurrently(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -203,7 +299,21 @@ func TestBackendConnectionChangesToOffline(t *testing.T) {
func TestBackendCallRPCConcurrently(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -279,7 +389,21 @@ func TestAppStateChange(t *testing.T) {
func TestBlockedRPCMethods(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -319,7 +443,21 @@ func TestCallRPCWithStoppedNode(t *testing.T) {
func TestStartStopMultipleTimes(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -338,7 +476,21 @@ func TestStartStopMultipleTimes(t *testing.T) {
func TestHashTypedData(t *testing.T) {
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
@ -651,15 +803,29 @@ func TestConvertAccount(t *testing.T) {
keyStoreDir := filepath.Join(rootDataDir, "keystore")
utils.Init()
backend := NewGethStatusBackend()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
backend.rootDataDir = rootDataDir
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
config.DataDir = rootDataDir
config.KeyStoreDir = keyStoreDir
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
err = backend.StartNode(config)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
require.NoError(t, backend.StartNode(config))
defer func() {
require.NoError(t, backend.StopNode())
}()
@ -693,7 +859,7 @@ func TestConvertAccount(t *testing.T) {
PublicKey: types.Hex2Bytes(accountInfo.PublicKey),
Path: pathEIP1581Chat,
Wallet: false,
Chat: false,
Chat: true,
Name: "GeneratedAccount",
})
@ -710,6 +876,7 @@ func TestConvertAccount(t *testing.T) {
KeyUID: genAccInfo.KeyUID,
Wallet: false,
Chat: false,
Type: accounts.AccountTypeGenerated,
Path: p,
Name: "derivacc" + p,
Hidden: false,
@ -730,6 +897,7 @@ func TestConvertAccount(t *testing.T) {
s := settings.Settings{
Address: types.HexToAddress(masterAddress),
DisplayName: "UserDisplayName",
CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress(derivedAccounts[pathDefaultWalletAccount].Address),
EIP1581Address: types.HexToAddress(derivedAccounts[pathEIP1581Root].Address),

View File

@ -74,6 +74,7 @@ func setupWalletTest(t *testing.T, password string) (backend *GethStatusBackend,
networks := json.RawMessage("{}")
s := settings.Settings{
Address: types.HexToAddress(walletRootAddress),
DisplayName: "UserDisplayName",
CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress(walletRootAddress),
EIP1581Address: types.HexToAddress(walletRootAddress),

View File

@ -14,7 +14,23 @@ import (
)
func TestHashMessage(t *testing.T) {
backend := NewGethStatusBackend()
utils.Init()
backend, stop1, stop2, err := setupGethStatusBackend()
defer func() {
err := stop1()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
defer func() {
err := stop2()
if err != nil {
require.NoError(t, backend.StopNode())
}
}()
require.NoError(t, err)
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))

View File

@ -183,6 +183,10 @@ func updateKeypairNameAndLastDerivationIndexIfNeeded(db *sql.DB) error {
return err
}
if displayName == "" {
displayName = "Status"
}
var (
seedKeyPairIndex int
keyKeyPairIndex int

View File

@ -3,28 +3,26 @@ package node
import (
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/params"
)
func TestWakuLightModeEnabledSetsEmptyBloomFilter(t *testing.T) {
config := params.NodeConfig{
statusNode, err := createAndStartStatusNode(&params.NodeConfig{
EnableNTPSync: true,
WakuConfig: params.WakuConfig{
Enabled: true,
LightClient: true,
},
}
node := New(nil)
require.NoError(t, node.Start(&config, &accounts.Manager{}))
})
require.NoError(t, err)
defer func() {
require.NoError(t, node.Stop())
err := statusNode.Stop()
require.NoError(t, err)
}()
waku := node.WakuService()
waku := statusNode.WakuService()
require.NotNil(t, waku)
bloomFilter := waku.BloomFilter()
@ -34,20 +32,20 @@ func TestWakuLightModeEnabledSetsEmptyBloomFilter(t *testing.T) {
}
func TestWakuLightModeEnabledSetsNilBloomFilter(t *testing.T) {
config := params.NodeConfig{
statusNode, err := createAndStartStatusNode(&params.NodeConfig{
EnableNTPSync: true,
WakuConfig: params.WakuConfig{
Enabled: true,
LightClient: false,
},
}
node := New(nil)
require.NoError(t, node.Start(&config, &accounts.Manager{}))
})
require.NoError(t, err)
defer func() {
require.NoError(t, node.Stop())
err := statusNode.Stop()
require.NoError(t, err)
}()
waku := node.WakuService()
waku := statusNode.WakuService()
require.NotNil(t, waku)
require.Nil(t, waku.BloomFilter())
}

View File

@ -29,6 +29,18 @@ func TestStatusNodeStart(t *testing.T) {
require.Nil(t, n.Config())
require.Nil(t, n.RPCClient())
require.Equal(t, 0, n.PeerCount())
db, stop, err := setupTestDB()
defer func() {
err := stop()
if err != nil {
n.log.Error("stopping db", err)
}
}()
require.NoError(t, err)
require.NotNil(t, db)
n.appDB = db
// start node
require.NoError(t, n.Start(config, nil))
@ -73,7 +85,21 @@ func TestStatusNodeWithDataDir(t *testing.T) {
DataDir: dir,
KeyStoreDir: keyStoreDir,
}
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.Start(&config, nil))
require.NoError(t, n.Stop())
@ -95,7 +121,20 @@ func TestStatusNodeAddPeer(t *testing.T) {
defer func() { require.NoError(t, peer.Close()) }()
peerURL := peer.Server().Self().URLv4()
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
// checks before node is started
require.EqualError(t, n.AddPeer(peerURL), ErrNoRunningNode.Error())
@ -127,7 +166,22 @@ func TestStatusNodeRendezvousDiscovery(t *testing.T) {
// use custom address to test the all possibilities
AdvertiseAddr: "127.0.0.1",
}
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.Start(&config, nil))
require.NotNil(t, n.discovery)
require.True(t, n.discovery.Running())
@ -146,7 +200,22 @@ func TestStatusNodeStartDiscoveryManual(t *testing.T) {
// use custom address to test the all possibilities
AdvertiseAddr: "127.0.0.1",
}
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.StartWithOptions(&config, StartOptions{}))
require.Nil(t, n.discovery)
// start discovery manually
@ -161,7 +230,22 @@ func TestStatusNodeDiscoverNode(t *testing.T) {
NoDiscovery: true,
ListenAddr: "127.0.0.1:0",
}
n := New(nil)
n, stop1, stop2, err := createStatusNode()
defer func() {
err := stop1()
if err != nil {
n.log.Error("stopping db", err)
}
}()
defer func() {
err := stop2()
if err != nil {
n.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n.Start(&config, nil))
node, err := n.discoverNode()
require.NoError(t, err)
@ -172,9 +256,24 @@ func TestStatusNodeDiscoverNode(t *testing.T) {
AdvertiseAddr: "127.0.0.2",
ListenAddr: "127.0.0.1:0",
}
n = New(nil)
require.NoError(t, n.Start(&config, nil))
node, err = n.discoverNode()
n1, stop11, stop12, err := createStatusNode()
defer func() {
err := stop11()
if err != nil {
n1.log.Error("stopping db", err)
}
}()
defer func() {
err := stop12()
if err != nil {
n1.log.Error("stopping multiaccount db", err)
}
}()
require.NoError(t, err)
require.NoError(t, n1.Start(&config, nil))
node, err = n1.discoverNode()
require.NoError(t, err)
require.Equal(t, net.ParseIP("127.0.0.2").To4(), node.IP())
}

View File

@ -93,6 +93,20 @@ func createAndStartStatusNode(config *params.NodeConfig) (*StatusNode, error) {
return statusNode, nil
}
func createStatusNode() (*StatusNode, func() error, func() error, error) {
db, stop1, err := setupTestDB()
if err != nil {
return nil, nil, nil, err
}
statusNode := New(nil)
statusNode.SetAppDB(db)
ma, stop2, err := setupTestMultiDB()
statusNode.SetMultiaccountsDB(ma)
return statusNode, stop1, stop2, err
}
func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) {
var err error