2018-04-20 15:39:53 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-01-24 15:44:46 +00:00
|
|
|
"encoding/hex"
|
2018-04-20 15:39:53 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-24 15:44:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/status-im/status-go/account"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"github.com/status-im/status-go/params"
|
2018-08-02 07:07:55 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-09-13 16:31:29 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2018-04-20 15:39:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackendStartNodeConcurrently(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 2
|
|
|
|
resultCh := make(chan error)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
go func() {
|
2018-09-13 16:31:29 +00:00
|
|
|
resultCh <- backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// close channel as otherwise for loop never finishes
|
|
|
|
go func() { wg.Wait(); close(resultCh) }()
|
|
|
|
|
|
|
|
var results []error
|
|
|
|
for err := range resultCh {
|
|
|
|
results = append(results, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Contains(t, results, nil)
|
|
|
|
require.Contains(t, results, node.ErrNodeRunning)
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StopNode()
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendRestartNodeConcurrently(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 3
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
go func(idx int) {
|
|
|
|
assert.NoError(t, backend.RestartNode())
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(adam): add concurrent tests for ResetChainData()
|
|
|
|
|
|
|
|
func TestBackendGettersConcurrently(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NotNil(t, backend.StatusNode())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NotNil(t, backend.AccountManager())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2018-06-27 08:11:45 +00:00
|
|
|
assert.NotNil(t, backend.personalAPI)
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NotNil(t, backend.Transactor())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.True(t, backend.IsNodeRunning())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.True(t, backend.IsNodeRunning())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendAccountsConcurrently(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wgCreateAccounts sync.WaitGroup
|
|
|
|
count := 3
|
2019-01-18 09:01:14 +00:00
|
|
|
addressCh := make(chan [3]string, count) // use buffered channel to avoid blocking
|
2018-04-20 15:39:53 +00:00
|
|
|
|
|
|
|
// create new accounts concurrently
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wgCreateAccounts.Add(1)
|
|
|
|
go func(pass string) {
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo, _, err := backend.AccountManager().CreateAccount(pass)
|
2018-04-20 15:39:53 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-18 09:01:14 +00:00
|
|
|
addressCh <- [...]string{accountInfo.WalletAddress, accountInfo.ChatAddress, pass}
|
2018-04-20 15:39:53 +00:00
|
|
|
wgCreateAccounts.Done()
|
|
|
|
}("password-00" + string(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
// close addressCh as otherwise for loop never finishes
|
|
|
|
go func() { wgCreateAccounts.Wait(); close(addressCh) }()
|
|
|
|
|
|
|
|
// select, reselect or logout concurrently
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for tuple := range addressCh {
|
|
|
|
wg.Add(1)
|
2019-01-18 09:01:14 +00:00
|
|
|
go func(tuple [3]string) {
|
|
|
|
assert.NoError(t, backend.SelectAccount(tuple[0], tuple[1], tuple[2]))
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}(tuple)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2018-06-27 08:11:45 +00:00
|
|
|
assert.NoError(t, backend.reSelectAccount())
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
assert.NoError(t, backend.Logout())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:44:46 +00:00
|
|
|
func TestBackendInjectChatAccount(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = backend.StartNode(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
chatPrivKey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
encryptionPrivKey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
chatPrivKeyHex := hex.EncodeToString(crypto.FromECDSA(chatPrivKey))
|
|
|
|
chatPubKeyHex := hexutil.Encode(crypto.FromECDSAPub(&chatPrivKey.PublicKey))
|
|
|
|
encryptionPrivKeyHex := hex.EncodeToString(crypto.FromECDSA(encryptionPrivKey))
|
|
|
|
|
|
|
|
whisperService, err := backend.StatusNode().WhisperService()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// public key should not be already in whisper
|
|
|
|
require.False(t, whisperService.HasKeyPair(chatPubKeyHex), "identity already present in whisper")
|
|
|
|
|
|
|
|
// call InjectChatAccount
|
|
|
|
require.NoError(t, backend.InjectChatAccount(chatPrivKeyHex, encryptionPrivKeyHex))
|
|
|
|
|
|
|
|
// public key should now be in whisper
|
|
|
|
require.True(t, whisperService.HasKeyPair(chatPubKeyHex), "identity not injected into whisper")
|
|
|
|
|
|
|
|
// wallet account should not be selected
|
|
|
|
walletAcc, err := backend.AccountManager().SelectedWalletAccount()
|
|
|
|
require.Nil(t, walletAcc)
|
|
|
|
require.Equal(t, account.ErrNoAccountSelected, err)
|
|
|
|
|
|
|
|
// selected chat account should have the key injected previously
|
|
|
|
chatAcc, err := backend.AccountManager().SelectedChatAccount()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, chatPrivKey, chatAcc.AccountKey.PrivateKey)
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
func TestBackendConnectionChangesConcurrently(t *testing.T) {
|
2018-06-27 08:11:45 +00:00
|
|
|
connections := [...]string{wifi, cellular, unknown}
|
2018-04-20 15:39:53 +00:00
|
|
|
backend := NewStatusBackend()
|
|
|
|
count := 3
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
connIdx := rand.Intn(len(connections))
|
2018-04-23 13:34:49 +00:00
|
|
|
backend.ConnectionChange(connections[connIdx], false)
|
2018-04-20 15:39:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
func TestBackendConnectionChangesToOffline(t *testing.T) {
|
|
|
|
b := NewStatusBackend()
|
|
|
|
b.ConnectionChange(none, false)
|
|
|
|
assert.True(t, b.connectionState.Offline)
|
|
|
|
|
|
|
|
b.ConnectionChange(wifi, false)
|
|
|
|
assert.False(t, b.connectionState.Offline)
|
|
|
|
|
|
|
|
b.ConnectionChange("unknown-state", false)
|
|
|
|
assert.False(t, b.connectionState.Offline)
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
func TestBackendCallRPCConcurrently(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
count := 3
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err = backend.StartNode(config)
|
2018-04-20 15:39:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int) {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := backend.CallRPC(fmt.Sprintf(
|
2018-04-20 15:39:53 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":%d}`,
|
|
|
|
idx+1,
|
|
|
|
))
|
2018-12-20 08:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
assert.NotContains(t, result, "error")
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int) {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := backend.CallPrivateRPC(fmt.Sprintf(
|
2018-04-20 15:39:53 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":%d}`,
|
|
|
|
idx+1,
|
|
|
|
))
|
2018-12-20 08:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2018-04-20 15:39:53 +00:00
|
|
|
assert.NotContains(t, result, "error")
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
func TestAppStateChange(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
fromState appState
|
|
|
|
toState appState
|
|
|
|
expectedState appState
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fromState: appStateInactive,
|
|
|
|
toState: appStateBackground,
|
|
|
|
expectedState: appStateBackground,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid state",
|
|
|
|
fromState: appStateInactive,
|
|
|
|
toState: "unexisting",
|
|
|
|
expectedState: appStateInactive,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
backend.appState = tc.fromState
|
|
|
|
backend.AppStateChange(tc.toState.String())
|
|
|
|
assert.Equal(t, tc.expectedState.String(), backend.appState.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:07:55 +00:00
|
|
|
func TestBlockedRPCMethods(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = backend.StartNode(config)
|
2018-08-02 07:07:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { require.NoError(t, backend.StopNode()) }()
|
|
|
|
|
|
|
|
for idx, m := range rpc.BlockedMethods() {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err := backend.CallRPC(fmt.Sprintf(
|
2018-08-02 07:07:55 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"%s","params":[],"id":%d}`,
|
|
|
|
m,
|
|
|
|
idx+1,
|
|
|
|
))
|
2018-12-20 08:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2018-08-02 07:07:55 +00:00
|
|
|
assert.Contains(t, result, fmt.Sprintf(`{"code":-32700,"message":"%s"}`, rpc.ErrMethodNotFound))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 08:31:17 +00:00
|
|
|
func TestCallRPCWithStoppedNode(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
|
|
|
|
|
|
|
resp, err := backend.CallRPC(
|
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}`,
|
|
|
|
)
|
|
|
|
assert.Equal(t, ErrRPCClientUnavailable, err)
|
|
|
|
assert.Equal(t, "", resp)
|
|
|
|
|
|
|
|
resp, err = backend.CallPrivateRPC(
|
|
|
|
`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}`,
|
|
|
|
)
|
|
|
|
assert.Equal(t, ErrRPCClientUnavailable, err)
|
|
|
|
assert.Equal(t, "", resp)
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// TODO(adam): add concurrent tests for: SendTransaction
|
2019-01-17 11:02:45 +00:00
|
|
|
|
|
|
|
func TestStartStopMultipleTimes(t *testing.T) {
|
|
|
|
backend := NewStatusBackend()
|
|
|
|
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
config.NoDiscovery = false
|
|
|
|
// doesn't have to be running. just any valid enode to bypass validation.
|
|
|
|
config.ClusterConfig.BootNodes = []string{
|
|
|
|
"enode://e8a7c03b58911e98bbd66accb2a55d57683f35b23bf9dfca89e5e244eb5cc3f25018b4112db507faca34fb69ffb44b362f79eda97a669a8df29c72e654416784@0.0.0.0:30404",
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
require.NoError(t, backend.StartNode(config))
|
|
|
|
require.NoError(t, backend.StopNode())
|
|
|
|
}
|