2017-11-22 13:06:14 +00:00
|
|
|
// +build e2e_test
|
|
|
|
|
|
|
|
// This is a file with e2e tests for C bindings written in library.go.
|
|
|
|
// As a CGO file, it can't have `_test.go` suffix as it's not allowed by Go.
|
|
|
|
// At the same time, we don't want this file to be included in the binaries.
|
|
|
|
// This is why `e2e_test` tag was introduced. Without it, this file is excluded
|
|
|
|
// from the build. Providing this tag will include this file into the build
|
|
|
|
// and that's what is done while running e2e tests for `lib/` package.
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "C"
|
|
|
|
import (
|
2017-10-24 16:54:13 +00:00
|
|
|
"context"
|
2016-11-05 17:12:24 +00:00
|
|
|
"encoding/json"
|
2017-10-23 11:05:52 +00:00
|
|
|
"fmt"
|
2017-05-06 21:53:18 +00:00
|
|
|
"io/ioutil"
|
2016-11-05 17:12:24 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2017-03-15 21:03:01 +00:00
|
|
|
"strconv"
|
2017-11-07 17:36:42 +00:00
|
|
|
"strings"
|
2016-11-05 17:12:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2017-02-20 09:44:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2017-03-15 21:03:01 +00:00
|
|
|
gethparams "github.com/ethereum/go-ethereum/params"
|
2017-10-24 16:54:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
"github.com/status-im/status-go/geth/account"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-03-15 21:03:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-09-25 18:22:57 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/status-im/status-go/geth/transactions"
|
|
|
|
"github.com/status-im/status-go/geth/transactions/queue"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2017-10-20 09:06:22 +00:00
|
|
|
. "github.com/status-im/status-go/testing" //nolint: golint
|
2016-11-05 17:12:24 +00:00
|
|
|
)
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
const zeroHash = "0x0000000000000000000000000000000000000000000000000000000000000000"
|
2018-01-09 16:53:33 +00:00
|
|
|
const initJS = `
|
|
|
|
var _status_catalog = {
|
|
|
|
foo: 'bar'
|
|
|
|
};`
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
var testChainDir string
|
|
|
|
var nodeConfigJSON string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
testChainDir = filepath.Join(TestDataDir, TestNetworkNames[GetNetworkID()])
|
|
|
|
|
|
|
|
nodeConfigJSON = `{
|
|
|
|
"NetworkId": ` + strconv.Itoa(GetNetworkID()) + `,
|
|
|
|
"DataDir": "` + testChainDir + `",
|
2017-05-16 12:09:52 +00:00
|
|
|
"HTTPPort": ` + strconv.Itoa(TestConfig.Node.HTTPPort) + `,
|
|
|
|
"WSPort": ` + strconv.Itoa(TestConfig.Node.WSPort) + `,
|
|
|
|
"LogLevel": "INFO"
|
|
|
|
}`
|
2017-11-07 17:46:11 +00:00
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// nolint: deadcode
|
2016-11-05 17:12:24 +00:00
|
|
|
func testExportedAPI(t *testing.T, done chan struct{}) {
|
|
|
|
<-startTestNode(t)
|
2017-11-07 17:46:11 +00:00
|
|
|
defer func() {
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// prepare accounts
|
|
|
|
testKeyDir := filepath.Join(testChainDir, "keystore")
|
2017-11-20 18:21:30 +00:00
|
|
|
if err := common.ImportTestAccount(testKeyDir, GetAccount1PKFile()); err != nil {
|
2017-11-07 17:46:11 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-20 18:21:30 +00:00
|
|
|
if err := common.ImportTestAccount(testKeyDir, GetAccount2PKFile()); err != nil {
|
2017-11-07 17:46:11 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-09-08 12:32:02 +00:00
|
|
|
// FIXME(tiabc): All of that is done because usage of cgo is not supported in tests.
|
|
|
|
// Probably, there should be a cleaner way, for example, test cgo bindings in e2e tests
|
|
|
|
// separately from other internal tests.
|
2017-11-07 17:46:11 +00:00
|
|
|
// FIXME(@jekamas): ATTENTION! this tests depends on each other!
|
2016-11-05 17:12:24 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fn func(t *testing.T) bool
|
|
|
|
}{
|
2017-03-15 21:03:01 +00:00
|
|
|
{
|
|
|
|
"check default configuration",
|
|
|
|
testGetDefaultConfig,
|
|
|
|
},
|
2017-02-11 01:33:02 +00:00
|
|
|
{
|
2017-05-16 12:09:52 +00:00
|
|
|
"stop/resume node",
|
2017-02-11 01:33:02 +00:00
|
|
|
testStopResumeNode,
|
|
|
|
},
|
2017-05-28 13:57:30 +00:00
|
|
|
{
|
|
|
|
"call RPC on in-proc handler",
|
|
|
|
testCallRPC,
|
|
|
|
},
|
2016-11-14 01:35:58 +00:00
|
|
|
{
|
|
|
|
"create main and child accounts",
|
|
|
|
testCreateChildAccount,
|
|
|
|
},
|
2017-05-06 21:53:18 +00:00
|
|
|
{
|
|
|
|
"verify account password",
|
|
|
|
testVerifyAccountPassword,
|
|
|
|
},
|
2016-11-14 01:35:58 +00:00
|
|
|
{
|
|
|
|
"recover account",
|
|
|
|
testRecoverAccount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"account select/login",
|
|
|
|
testAccountSelect,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"account logout",
|
|
|
|
testAccountLogout,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"complete single queued transaction",
|
|
|
|
testCompleteTransaction,
|
|
|
|
},
|
2016-11-05 17:12:24 +00:00
|
|
|
{
|
|
|
|
"test complete multiple queued transactions",
|
|
|
|
testCompleteMultipleQueuedTransactions,
|
|
|
|
},
|
2016-11-14 01:35:58 +00:00
|
|
|
{
|
|
|
|
"discard single queued transaction",
|
|
|
|
testDiscardTransaction,
|
|
|
|
},
|
2016-11-05 17:12:24 +00:00
|
|
|
{
|
|
|
|
"test discard multiple queued transactions",
|
|
|
|
testDiscardMultipleQueuedTransactions,
|
|
|
|
},
|
2017-08-04 16:14:17 +00:00
|
|
|
{
|
|
|
|
"test jail invalid initialization",
|
|
|
|
testJailInitInvalid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test jail invalid parse",
|
|
|
|
testJailParseInvalid,
|
|
|
|
},
|
2016-11-14 01:35:58 +00:00
|
|
|
{
|
|
|
|
"test jail initialization",
|
|
|
|
testJailInit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test jailed calls",
|
|
|
|
testJailFunctionCall,
|
|
|
|
},
|
2017-11-07 17:36:42 +00:00
|
|
|
{
|
|
|
|
"test ExecuteJS",
|
|
|
|
testExecuteJS,
|
|
|
|
},
|
2017-11-23 12:37:59 +00:00
|
|
|
{
|
|
|
|
"test deprecated Parse",
|
|
|
|
testJailParseDeprecated,
|
|
|
|
},
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-05-16 12:09:52 +00:00
|
|
|
t.Logf("=== RUN %s", test.name)
|
2016-11-05 17:12:24 +00:00
|
|
|
if ok := test.fn(t); !ok {
|
2017-11-07 17:46:11 +00:00
|
|
|
t.Logf("=== FAILED %s", test.name)
|
2016-11-05 17:12:24 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 21:53:18 +00:00
|
|
|
func testVerifyAccountPassword(t *testing.T) bool {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "accounts")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir) // nolint: errcheck
|
|
|
|
|
2017-11-20 18:21:30 +00:00
|
|
|
if err = common.ImportTestAccount(tmpDir, GetAccount1PKFile()); err != nil {
|
2017-05-06 21:53:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-20 18:21:30 +00:00
|
|
|
if err = common.ImportTestAccount(tmpDir, GetAccount2PKFile()); err != nil {
|
2017-05-15 21:49:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename account file (to see that file's internals reviewed, when locating account key)
|
2017-11-20 18:21:30 +00:00
|
|
|
accountFilePathOriginal := filepath.Join(tmpDir, GetAccount1PKFile())
|
2017-05-16 12:09:52 +00:00
|
|
|
accountFilePath := filepath.Join(tmpDir, "foo"+TestConfig.Account1.Address+"bar.pk")
|
2017-05-15 21:49:22 +00:00
|
|
|
if err := os.Rename(accountFilePathOriginal, accountFilePath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-06 21:53:18 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
response := common.APIResponse{}
|
2017-05-06 21:53:18 +00:00
|
|
|
rawResponse := VerifyAccountPassword(
|
2017-05-15 21:49:22 +00:00
|
|
|
C.CString(tmpDir),
|
2017-05-16 12:09:52 +00:00
|
|
|
C.CString(TestConfig.Account1.Address),
|
|
|
|
C.CString(TestConfig.Account1.Password))
|
2017-05-06 21:53:18 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &response); err != nil {
|
|
|
|
t.Errorf("cannot decode response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if response.Error != "" {
|
|
|
|
t.Errorf("unexpected error: %s", response.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:03:01 +00:00
|
|
|
func testGetDefaultConfig(t *testing.T) bool {
|
2017-09-08 12:32:02 +00:00
|
|
|
networks := []struct {
|
|
|
|
chainID int
|
|
|
|
refChainConfig *gethparams.ChainConfig
|
|
|
|
}{
|
|
|
|
{params.MainNetworkID, gethparams.MainnetChainConfig},
|
|
|
|
{params.RopstenNetworkID, gethparams.TestnetChainConfig},
|
|
|
|
{params.RinkebyNetworkID, gethparams.RinkebyChainConfig},
|
2017-10-23 11:05:52 +00:00
|
|
|
// TODO(tiabc): The same for params.StatusChainNetworkID
|
2017-09-08 12:32:02 +00:00
|
|
|
}
|
|
|
|
for i := range networks {
|
|
|
|
network := networks[i]
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("networkID=%d", network.chainID), func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
nodeConfig = params.NodeConfig{}
|
|
|
|
rawResponse = GenerateConfig(C.CString("/tmp/data-folder"), C.int(network.chainID), 1)
|
|
|
|
)
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &nodeConfig); err != nil {
|
|
|
|
t.Errorf("cannot decode response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genesis := new(core.Genesis)
|
|
|
|
if err := json.Unmarshal([]byte(nodeConfig.LightEthConfig.Genesis), genesis); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, network.refChainConfig, genesis.Config)
|
|
|
|
})
|
2017-03-15 21:03:01 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//@TODO(adam): quarantined this test until it uses a different directory.
|
|
|
|
//nolint: deadcode
|
2017-01-24 18:42:55 +00:00
|
|
|
func testResetChainData(t *testing.T) bool {
|
2017-10-11 14:20:51 +00:00
|
|
|
t.Skip()
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
resetChainDataResponse := common.APIResponse{}
|
2017-01-24 18:42:55 +00:00
|
|
|
rawResponse := ResetChainData()
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &resetChainDataResponse); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode ResetChainData response (%s): %v", C.GoString(rawResponse), err)
|
2017-01-24 18:42:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if resetChainDataResponse.Error != "" {
|
|
|
|
t.Errorf("unexpected error: %s", resetChainDataResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-23 21:39:13 +00:00
|
|
|
EnsureNodeSync(statusAPI.NodeManager())
|
2017-01-24 18:42:55 +00:00
|
|
|
testCompleteTransaction(t)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testStopResumeNode(t *testing.T) bool { //nolint: gocyclo
|
2017-10-23 11:05:52 +00:00
|
|
|
// to make sure that we start with empty account (which might have gotten populated during previous tests)
|
2017-05-16 12:09:52 +00:00
|
|
|
if err := statusAPI.Logout(); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-24 18:42:55 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
whisperService, err := statusAPI.NodeManager().WhisperService()
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-05-16 12:09:52 +00:00
|
|
|
address1, pubKey1, _, err := statusAPI.CreateAccount(TestConfig.Account1.Password)
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t.Logf("account created: {address: %s, key: %s}", address1, pubKey1)
|
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// select account
|
2017-05-16 12:09:52 +00:00
|
|
|
loginResponse := common.APIResponse{}
|
|
|
|
rawResponse := Login(C.CString(address1), C.CString(TestConfig.Account1.Password))
|
2017-01-24 18:42:55 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2017-01-24 18:42:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Error != "" {
|
|
|
|
t.Errorf("could not select account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey1) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop and resume node, then make sure that selected account is still selected
|
2017-05-03 14:24:48 +00:00
|
|
|
// nolint: dupl
|
2017-01-24 18:42:55 +00:00
|
|
|
stopNodeFn := func() bool {
|
2017-05-16 12:09:52 +00:00
|
|
|
response := common.APIResponse{}
|
2017-10-23 11:05:52 +00:00
|
|
|
// FIXME(tiabc): Implement https://github.com/status-im/status-go/issues/254 to avoid
|
|
|
|
// 9-sec timeout below after stopping the node.
|
2017-01-24 18:42:55 +00:00
|
|
|
rawResponse = StopNode()
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &response); err != nil {
|
|
|
|
t.Errorf("cannot decode StopNode response (%s): %v", C.GoString(rawResponse), err)
|
2017-01-24 18:42:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if response.Error != "" {
|
|
|
|
t.Errorf("unexpected error: %s", response.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// nolint: dupl
|
2017-01-24 18:42:55 +00:00
|
|
|
resumeNodeFn := func() bool {
|
2017-05-16 12:09:52 +00:00
|
|
|
response := common.APIResponse{}
|
2017-10-23 11:05:52 +00:00
|
|
|
// FIXME(tiabc): Implement https://github.com/status-im/status-go/issues/254 to avoid
|
|
|
|
// 10-sec timeout below after resuming the node.
|
2017-05-16 12:09:52 +00:00
|
|
|
rawResponse = StartNode(C.CString(nodeConfigJSON))
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &response); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
t.Errorf("cannot decode StartNode response (%s): %v", C.GoString(rawResponse), err)
|
2017-01-24 18:42:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if response.Error != "" {
|
|
|
|
t.Errorf("unexpected error: %s", response.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stopNodeFn() {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-23 11:05:52 +00:00
|
|
|
|
|
|
|
time.Sleep(9 * time.Second) // allow to stop
|
|
|
|
|
2017-01-24 18:42:55 +00:00
|
|
|
if !resumeNodeFn() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-23 11:05:52 +00:00
|
|
|
time.Sleep(10 * time.Second) // allow to start (instead of using blocking version of start, of filter event)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-01-24 18:42:55 +00:00
|
|
|
// now, verify that we still have account logged in
|
2017-05-16 12:09:52 +00:00
|
|
|
whisperService, err = statusAPI.NodeManager().WhisperService()
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey1) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Errorf("identity evicted from whisper on node restart: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// additionally, let's complete transaction (just to make sure that node lives through pause/resume w/o issues)
|
|
|
|
testCompleteTransaction(t)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-28 13:57:30 +00:00
|
|
|
func testCallRPC(t *testing.T) bool {
|
2017-10-11 14:20:51 +00:00
|
|
|
expected := `{"jsonrpc":"2.0","id":64,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}`
|
2017-05-28 13:57:30 +00:00
|
|
|
rawResponse := CallRPC(C.CString(`{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`))
|
|
|
|
received := C.GoString(rawResponse)
|
|
|
|
if expected != received {
|
2017-10-20 09:06:22 +00:00
|
|
|
t.Errorf("unexpected response: expected: %v, got: %v", expected, received)
|
2017-05-28 13:57:30 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testCreateChildAccount(t *testing.T) bool { //nolint: gocyclo
|
2017-05-03 14:24:48 +00:00
|
|
|
// to make sure that we start with empty account (which might get populated during previous tests)
|
2017-05-16 12:09:52 +00:00
|
|
|
if err := statusAPI.Logout(); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
keyStore, err := statusAPI.NodeManager().AccountKeyStore()
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-05-16 12:09:52 +00:00
|
|
|
createAccountResponse := common.AccountInfo{}
|
|
|
|
rawResponse := CreateAccount(C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &createAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode CreateAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if createAccountResponse.Error != "" {
|
|
|
|
t.Errorf("could not create account: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
address, pubKey, mnemonic := createAccountResponse.Address, createAccountResponse.PubKey, createAccountResponse.Mnemonic
|
|
|
|
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
|
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
acct, err := common.ParseAccountString(address)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not get account from address: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// obtain decrypted key, and make sure that extended key (which will be used as root for sub-accounts) is present
|
2017-09-26 13:44:26 +00:00
|
|
|
_, key, err := keyStore.AccountDecryptedKey(acct, TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.ExtendedKey == nil {
|
|
|
|
t.Error("CKD#2 has not been generated for new account")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// try creating sub-account, w/o selecting main account i.e. w/o login to main account
|
2017-05-16 12:09:52 +00:00
|
|
|
createSubAccountResponse := common.AccountInfo{}
|
|
|
|
rawResponse = CreateChildAccount(C.CString(""), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &createSubAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode CreateChildAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
if createSubAccountResponse.Error != account.ErrNoAccountSelected.Error() {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("expected error is not returned (tried to create sub-account w/o login): %v", createSubAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
err = statusAPI.SelectAccount(address, TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to create sub-account with wrong password
|
2017-05-16 12:09:52 +00:00
|
|
|
createSubAccountResponse = common.AccountInfo{}
|
2016-11-14 01:35:58 +00:00
|
|
|
rawResponse = CreateChildAccount(C.CString(""), C.CString("wrong password"))
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &createSubAccountResponse); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode CreateChildAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if createSubAccountResponse.Error != "cannot retrieve a valid key for a given account: could not decrypt key with given passphrase" {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("expected error is not returned (tried to create sub-account with wrong password): %v", createSubAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sub-account (from implicit parent)
|
2017-05-16 12:09:52 +00:00
|
|
|
createSubAccountResponse1 := common.AccountInfo{}
|
|
|
|
rawResponse = CreateChildAccount(C.CString(""), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &createSubAccountResponse1); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode CreateChildAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if createSubAccountResponse1.Error != "" {
|
|
|
|
t.Errorf("cannot create sub-account: %v", createSubAccountResponse1.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that sub-account index automatically progresses
|
2017-05-16 12:09:52 +00:00
|
|
|
createSubAccountResponse2 := common.AccountInfo{}
|
|
|
|
rawResponse = CreateChildAccount(C.CString(""), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &createSubAccountResponse2); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode CreateChildAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if createSubAccountResponse2.Error != "" {
|
|
|
|
t.Errorf("cannot create sub-account: %v", createSubAccountResponse2.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if createSubAccountResponse1.Address == createSubAccountResponse2.Address || createSubAccountResponse1.PubKey == createSubAccountResponse2.PubKey {
|
|
|
|
t.Error("sub-account index auto-increament failed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sub-account (from explicit parent)
|
2017-05-16 12:09:52 +00:00
|
|
|
createSubAccountResponse3 := common.AccountInfo{}
|
|
|
|
rawResponse = CreateChildAccount(C.CString(createSubAccountResponse2.Address), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &createSubAccountResponse3); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode CreateChildAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if createSubAccountResponse3.Error != "" {
|
|
|
|
t.Errorf("cannot create sub-account: %v", createSubAccountResponse3.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
subAccount1, subAccount2, subAccount3 := createSubAccountResponse1.Address, createSubAccountResponse2.Address, createSubAccountResponse3.Address
|
|
|
|
subPubKey1, subPubKey2, subPubKey3 := createSubAccountResponse1.PubKey, createSubAccountResponse2.PubKey, createSubAccountResponse3.PubKey
|
|
|
|
|
|
|
|
if subAccount1 == subAccount3 || subPubKey1 == subPubKey3 || subAccount2 == subAccount3 || subPubKey2 == subPubKey3 {
|
|
|
|
t.Error("sub-account index auto-increament failed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testRecoverAccount(t *testing.T) bool { //nolint: gocyclo
|
2017-05-16 12:09:52 +00:00
|
|
|
keyStore, _ := statusAPI.NodeManager().AccountKeyStore()
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
// create an account
|
2017-05-16 12:09:52 +00:00
|
|
|
address, pubKey, mnemonic, err := statusAPI.CreateAccount(TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
|
|
|
|
|
|
|
|
// try recovering using password + mnemonic
|
2017-05-16 12:09:52 +00:00
|
|
|
recoverAccountResponse := common.AccountInfo{}
|
|
|
|
rawResponse := RecoverAccount(C.CString(TestConfig.Account1.Password), C.CString(mnemonic))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &recoverAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recoverAccountResponse.Error != "" {
|
|
|
|
t.Errorf("recover account failed: %v", recoverAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
addressCheck, pubKeyCheck := recoverAccountResponse.Address, recoverAccountResponse.PubKey
|
|
|
|
if address != addressCheck || pubKey != pubKeyCheck {
|
|
|
|
t.Error("recover account details failed to pull the correct details")
|
|
|
|
}
|
|
|
|
|
|
|
|
// now test recovering, but make sure that account/key file is removed i.e. simulate recovering on a new device
|
2017-05-16 12:09:52 +00:00
|
|
|
account, err := common.ParseAccountString(address)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not get account from address: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
account, key, err := keyStore.AccountDecryptedKey(account, TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
extChild2String := key.ExtendedKey.String()
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
if err = keyStore.Delete(account, TestConfig.Account1.Password); err != nil {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("cannot remove account: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
recoverAccountResponse = common.AccountInfo{}
|
|
|
|
rawResponse = RecoverAccount(C.CString(TestConfig.Account1.Password), C.CString(mnemonic))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &recoverAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recoverAccountResponse.Error != "" {
|
|
|
|
t.Errorf("recover account failed (for non-cached account): %v", recoverAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
addressCheck, pubKeyCheck = recoverAccountResponse.Address, recoverAccountResponse.PubKey
|
|
|
|
if address != addressCheck || pubKey != pubKeyCheck {
|
|
|
|
t.Error("recover account details failed to pull the correct details (for non-cached account)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that extended key exists and is imported ok too
|
2017-05-16 12:09:52 +00:00
|
|
|
_, key, err = keyStore.AccountDecryptedKey(account, TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if extChild2String != key.ExtendedKey.String() {
|
|
|
|
t.Errorf("CKD#2 key mismatch, expected: %s, got: %s", extChild2String, key.ExtendedKey.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that calling import several times, just returns from cache (no error is expected)
|
2017-05-16 12:09:52 +00:00
|
|
|
recoverAccountResponse = common.AccountInfo{}
|
|
|
|
rawResponse = RecoverAccount(C.CString(TestConfig.Account1.Password), C.CString(mnemonic))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &recoverAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recoverAccountResponse.Error != "" {
|
|
|
|
t.Errorf("recover account failed (for non-cached account): %v", recoverAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
addressCheck, pubKeyCheck = recoverAccountResponse.Address, recoverAccountResponse.PubKey
|
|
|
|
if address != addressCheck || pubKey != pubKeyCheck {
|
|
|
|
t.Error("recover account details failed to pull the correct details (for non-cached account)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// time to login with recovered data
|
2017-05-16 12:09:52 +00:00
|
|
|
whisperService, err := statusAPI.NodeManager().WhisperService()
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKeyCheck) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
err = statusAPI.SelectAccount(addressCheck, TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKeyCheck) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testAccountSelect(t *testing.T) bool { //nolint: gocyclo
|
2016-11-14 01:35:58 +00:00
|
|
|
// test to see if the account was injected in whisper
|
2017-05-16 12:09:52 +00:00
|
|
|
whisperService, err := statusAPI.NodeManager().WhisperService()
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-05-16 12:09:52 +00:00
|
|
|
address1, pubKey1, _, err := statusAPI.CreateAccount(TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t.Logf("Account created: {address: %s, key: %s}", address1, pubKey1)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
address2, pubKey2, _, err := statusAPI.CreateAccount(TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Test failed: could not create account")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t.Logf("Account created: {address: %s, key: %s}", address2, pubKey2)
|
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// try selecting with wrong password
|
2017-05-16 12:09:52 +00:00
|
|
|
loginResponse := common.APIResponse{}
|
2016-11-14 01:35:58 +00:00
|
|
|
rawResponse := Login(C.CString(address1), C.CString("wrongPassword"))
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Error == "" {
|
|
|
|
t.Error("select account is expected to throw error: wrong password used")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
loginResponse = common.APIResponse{}
|
|
|
|
rawResponse = Login(C.CString(address1), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Error != "" {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey1) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// select another account, make sure that previous account is wiped out from Whisper cache
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey2) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
loginResponse = common.APIResponse{}
|
|
|
|
rawResponse = Login(C.CString(address2), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Error != "" {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", loginResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey2) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity should be removed, but it is still present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccountLogout(t *testing.T) bool {
|
2017-05-16 12:09:52 +00:00
|
|
|
whisperService, err := statusAPI.NodeManager().WhisperService()
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-05-16 12:09:52 +00:00
|
|
|
address, pubKey, _, err := statusAPI.CreateAccount(TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that identity doesn't exist (yet) in Whisper
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// select/login
|
2017-05-16 12:09:52 +00:00
|
|
|
err = statusAPI.SelectAccount(address, TestConfig.Account1.Password)
|
2016-11-14 01:35:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity not injected into whisper")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
logoutResponse := common.APIResponse{}
|
2016-11-14 01:35:58 +00:00
|
|
|
rawResponse := Logout()
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &logoutResponse); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if logoutResponse.Error != "" {
|
|
|
|
t.Errorf("cannot logout: %v", logoutResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, logout and check if identity is removed indeed
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("identity not cleared from whisper")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCompleteTransaction(t *testing.T) bool {
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueueManager := statusAPI.TxQueueManager()
|
|
|
|
txQueue := txQueueManager.TransactionQueue()
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueue.Reset()
|
2017-10-23 21:39:13 +00:00
|
|
|
EnsureNodeSync(statusAPI.NodeManager())
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2016-12-15 09:49:05 +00:00
|
|
|
// log into account from which transactions will be sent
|
2017-09-04 12:56:58 +00:00
|
|
|
if err := statusAPI.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password); err != nil {
|
2017-11-07 17:46:11 +00:00
|
|
|
t.Errorf("cannot select account: %v. Error %q", TestConfig.Account1.Address, err)
|
2016-12-15 09:49:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:35:58 +00:00
|
|
|
// make sure you panic if transaction complete doesn't return
|
|
|
|
queuedTxCompleted := make(chan struct{}, 1)
|
|
|
|
abortPanic := make(chan struct{}, 1)
|
2017-05-16 12:09:52 +00:00
|
|
|
common.PanicAfter(10*time.Second, abortPanic, "testCompleteTransaction")
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
var txHash = ""
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-09-04 12:56:58 +00:00
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
2017-11-07 17:46:11 +00:00
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s. Error %q", jsonEvent, err)
|
2016-11-14 01:35:58 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionQueued {
|
2016-11-14 01:35:58 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2017-03-14 22:57:07 +00:00
|
|
|
t.Logf("transaction queued (will be completed shortly): {id: %s}\n", event["id"].(string))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
completeTxResponse := common.CompleteTransactionResult{}
|
|
|
|
rawResponse := CompleteTransaction(C.CString(event["id"].(string)), C.CString(TestConfig.Account1.Password))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &completeTxResponse); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if completeTxResponse.Error != "" {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot complete queued transaction[%v]: %v", event["id"], completeTxResponse.Error)
|
2016-11-14 01:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txHash = completeTxResponse.Hash
|
|
|
|
|
|
|
|
t.Logf("transaction complete: https://testnet.etherscan.io/tx/%s", txHash)
|
|
|
|
abortPanic <- struct{}{} // so that timeout is aborted
|
|
|
|
queuedTxCompleted <- struct{}{}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// this call blocks, up until Complete Transaction is called
|
2017-10-20 09:06:22 +00:00
|
|
|
txCheckHash, err := statusAPI.SendTransaction(context.TODO(), common.SendTxArgs{
|
2017-05-16 12:09:52 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
2017-02-20 09:44:38 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
2016-11-14 01:35:58 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2017-09-04 12:56:58 +00:00
|
|
|
t.Errorf("Failed to SendTransaction: %s", err)
|
|
|
|
return false
|
2016-11-14 01:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<-queuedTxCompleted // make sure that complete transaction handler completes its magic, before we proceed
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if txHash != txCheckHash.Hex() {
|
|
|
|
t.Errorf("Transaction hash returned from SendTransaction is invalid: expected %s, got %s",
|
|
|
|
txCheckHash.Hex(), txHash)
|
2016-11-14 01:35:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if reflect.DeepEqual(txCheckHash, gethcommon.Hash{}) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("Test failed: transaction was never queued or completed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Count() != 0 {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("tx queue must be empty at this point")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testCompleteMultipleQueuedTransactions(t *testing.T) bool { //nolint: gocyclo
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueue := statusAPI.TxQueueManager().TransactionQueue()
|
|
|
|
txQueue.Reset()
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2016-12-15 09:49:05 +00:00
|
|
|
// log into account from which transactions will be sent
|
2017-05-16 12:09:52 +00:00
|
|
|
if err := statusAPI.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password); err != nil {
|
|
|
|
t.Errorf("cannot select account: %v", TestConfig.Account1.Address)
|
2016-12-15 09:49:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
// make sure you panic if transaction complete doesn't return
|
|
|
|
testTxCount := 3
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDs := make(chan string, testTxCount)
|
2016-11-05 17:12:24 +00:00
|
|
|
allTestTxCompleted := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
2017-05-03 14:24:48 +00:00
|
|
|
var txID string
|
2017-09-25 18:22:57 +00:00
|
|
|
var envelope signal.Envelope
|
2016-11-05 17:12:24 +00:00
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionQueued {
|
2016-11-05 17:12:24 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2017-05-03 14:24:48 +00:00
|
|
|
txID = event["id"].(string)
|
|
|
|
t.Logf("transaction queued (will be completed in a single call, once aggregated): {id: %s}\n", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDs <- txID
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// this call blocks, and should return when DiscardQueuedTransaction() for a given tx id is called
|
|
|
|
sendTx := func() {
|
2017-10-20 09:06:22 +00:00
|
|
|
txHashCheck, err := statusAPI.SendTransaction(context.TODO(), common.SendTxArgs{
|
2017-05-16 12:09:52 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
2017-02-20 09:44:38 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
2016-11-05 17:12:24 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error thrown: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
if reflect.DeepEqual(txHashCheck, gethcommon.Hash{}) {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Error("transaction returned empty hash")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for transactions, and complete them in a single call
|
2017-05-03 14:24:48 +00:00
|
|
|
completeTxs := func(txIDStrings string) {
|
|
|
|
var parsedIDs []string
|
|
|
|
if err := json.Unmarshal([]byte(txIDStrings), &parsedIDs); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
parsedIDs = append(parsedIDs, "invalid-tx-id")
|
|
|
|
updatedTxIDStrings, _ := json.Marshal(parsedIDs)
|
2016-11-05 17:12:24 +00:00
|
|
|
|
|
|
|
// complete
|
2017-05-16 12:09:52 +00:00
|
|
|
resultsString := CompleteTransactions(C.CString(string(updatedTxIDStrings)), C.CString(TestConfig.Account1.Password))
|
|
|
|
resultsStruct := common.CompleteTransactionsResult{}
|
2017-05-03 14:24:48 +00:00
|
|
|
if err := json.Unmarshal([]byte(C.GoString(resultsString)), &resultsStruct); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
results := resultsStruct.Results
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
if len(results) != (testTxCount+1) || results["invalid-tx-id"].Error != queue.ErrQueuedTxIDNotFound.Error() {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Errorf("cannot complete txs: %v", results)
|
|
|
|
return
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
for txID, txResult := range results {
|
|
|
|
if txID != txResult.ID {
|
|
|
|
t.Errorf("tx id not set in result: expected id is %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
if txResult.Error != "" && txID != "invalid-tx-id" {
|
|
|
|
t.Errorf("invalid error for %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
if txResult.Hash == zeroHash && txID != "invalid-tx-id" {
|
|
|
|
t.Errorf("invalid hash (expected non empty hash): %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if txResult.Hash != zeroHash {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Logf("transaction complete: https://testnet.etherscan.io/tx/%s", txResult.Hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure that tx complete signal propagates
|
2017-05-03 14:24:48 +00:00
|
|
|
for _, txID := range parsedIDs {
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Has(common.QueuedTxID(txID)) {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("txqueue should not have test tx at this point (it should be completed): %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
2017-05-03 14:24:48 +00:00
|
|
|
var txIDStrings []string
|
2016-11-05 17:12:24 +00:00
|
|
|
for i := 0; i < testTxCount; i++ {
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDStrings = append(txIDStrings, <-txIDs)
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDJSON, _ := json.Marshal(txIDStrings)
|
|
|
|
completeTxs(string(txIDJSON))
|
2016-11-05 17:12:24 +00:00
|
|
|
allTestTxCompleted <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send multiple transactions
|
|
|
|
for i := 0; i < testTxCount; i++ {
|
|
|
|
go sendTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-allTestTxCompleted:
|
2017-08-04 16:14:17 +00:00
|
|
|
// pass
|
2016-11-05 17:12:24 +00:00
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Error("test timed out")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Count() != 0 {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Error("tx queue must be empty at this point")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testDiscardTransaction(t *testing.T) bool { //nolint: gocyclo
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueue := statusAPI.TxQueueManager().TransactionQueue()
|
|
|
|
txQueue.Reset()
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2016-12-15 09:49:05 +00:00
|
|
|
// log into account from which transactions will be sent
|
2017-09-04 12:56:58 +00:00
|
|
|
if err := statusAPI.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
t.Errorf("cannot select account: %v", TestConfig.Account1.Address)
|
2016-12-15 09:49:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:35:58 +00:00
|
|
|
// make sure you panic if transaction complete doesn't return
|
|
|
|
completeQueuedTransaction := make(chan struct{}, 1)
|
2017-05-16 12:09:52 +00:00
|
|
|
common.PanicAfter(20*time.Second, completeQueuedTransaction, "testDiscardTransaction")
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
2017-05-03 14:24:48 +00:00
|
|
|
var txID string
|
2016-11-14 01:35:58 +00:00
|
|
|
txFailedEventCalled := false
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-09-04 12:56:58 +00:00
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionQueued {
|
2016-11-14 01:35:58 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2017-05-03 14:24:48 +00:00
|
|
|
txID = event["id"].(string)
|
|
|
|
t.Logf("transaction queued (will be discarded soon): {id: %s}\n", txID)
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if !txQueue.Has(common.QueuedTxID(txID)) {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("txqueue should still have test tx: %s", txID)
|
2016-11-14 01:35:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// discard
|
2017-05-16 12:09:52 +00:00
|
|
|
discardResponse := common.DiscardTransactionResult{}
|
2017-05-03 14:24:48 +00:00
|
|
|
rawResponse := DiscardTransaction(C.CString(txID))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &discardResponse); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
2016-11-14 01:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if discardResponse.Error != "" {
|
|
|
|
t.Errorf("cannot discard tx: %v", discardResponse.Error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try completing discarded transaction
|
2017-09-04 12:56:58 +00:00
|
|
|
_, err := statusAPI.CompleteTransaction(common.QueuedTxID(txID), TestConfig.Account1.Password)
|
2018-01-26 05:59:21 +00:00
|
|
|
if err != queue.ErrQueuedTxIDNotFound {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("expects tx not found, but call to CompleteTransaction succeeded")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure that tx complete signal propagates
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Has(common.QueuedTxID(txID)) {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("txqueue should not have test tx at this point (it should be discarded): %s", txID)
|
2016-11-14 01:35:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
completeQueuedTransaction <- struct{}{} // so that timeout is aborted
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionFailed {
|
2016-11-14 01:35:58 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
t.Logf("transaction return event received: {id: %s}\n", event["id"].(string))
|
|
|
|
|
|
|
|
receivedErrMessage := event["error_message"].(string)
|
2018-01-05 20:58:17 +00:00
|
|
|
expectedErrMessage := transactions.ErrQueuedTxDiscarded.Error()
|
2016-11-14 01:35:58 +00:00
|
|
|
if receivedErrMessage != expectedErrMessage {
|
|
|
|
t.Errorf("unexpected error message received: got %v", receivedErrMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
receivedErrCode := event["error_code"].(string)
|
2018-01-26 05:59:21 +00:00
|
|
|
if receivedErrCode != strconv.Itoa(transactions.SendTransactionDiscardedErrorCode) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("unexpected error code received: got %v", receivedErrCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txFailedEventCalled = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// this call blocks, and should return when DiscardQueuedTransaction() is called
|
2017-10-20 09:06:22 +00:00
|
|
|
txHashCheck, err := statusAPI.SendTransaction(context.TODO(), common.SendTxArgs{
|
2017-05-16 12:09:52 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
2017-02-20 09:44:38 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
2016-11-14 01:35:58 +00:00
|
|
|
})
|
2018-01-05 20:58:17 +00:00
|
|
|
if err != transactions.ErrQueuedTxDiscarded {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Errorf("expected error not thrown: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
if !reflect.DeepEqual(txHashCheck, gethcommon.Hash{}) {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("transaction returned hash, while it shouldn't")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Count() != 0 {
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Error("tx queue must be empty at this point")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !txFailedEventCalled {
|
|
|
|
t.Error("expected tx failure signal is not received")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
func testDiscardMultipleQueuedTransactions(t *testing.T) bool { //nolint: gocyclo
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueue := statusAPI.TxQueueManager().TransactionQueue()
|
|
|
|
txQueue.Reset()
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2016-12-15 09:49:05 +00:00
|
|
|
// log into account from which transactions will be sent
|
2017-05-16 12:09:52 +00:00
|
|
|
if err := statusAPI.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password); err != nil {
|
|
|
|
t.Errorf("cannot select account: %v", TestConfig.Account1.Address)
|
2016-12-15 09:49:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
// make sure you panic if transaction complete doesn't return
|
|
|
|
testTxCount := 3
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDs := make(chan string, testTxCount)
|
2016-11-05 17:12:24 +00:00
|
|
|
allTestTxDiscarded := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
txFailedEventCallCount := 0
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
2017-05-03 14:24:48 +00:00
|
|
|
var txID string
|
2017-09-25 18:22:57 +00:00
|
|
|
var envelope signal.Envelope
|
2016-11-05 17:12:24 +00:00
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionQueued {
|
2016-11-05 17:12:24 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2017-05-03 14:24:48 +00:00
|
|
|
txID = event["id"].(string)
|
|
|
|
t.Logf("transaction queued (will be discarded soon): {id: %s}\n", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if !txQueue.Has(common.QueuedTxID(txID)) {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("txqueue should still have test tx: %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDs <- txID
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionFailed {
|
2016-11-05 17:12:24 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
t.Logf("transaction return event received: {id: %s}\n", event["id"].(string))
|
|
|
|
|
|
|
|
receivedErrMessage := event["error_message"].(string)
|
2018-01-05 20:58:17 +00:00
|
|
|
expectedErrMessage := transactions.ErrQueuedTxDiscarded.Error()
|
2016-11-05 17:12:24 +00:00
|
|
|
if receivedErrMessage != expectedErrMessage {
|
|
|
|
t.Errorf("unexpected error message received: got %v", receivedErrMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
receivedErrCode := event["error_code"].(string)
|
2018-01-26 05:59:21 +00:00
|
|
|
if receivedErrCode != strconv.Itoa(transactions.SendTransactionDiscardedErrorCode) {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Errorf("unexpected error code received: got %v", receivedErrCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txFailedEventCallCount++
|
|
|
|
if txFailedEventCallCount == testTxCount {
|
|
|
|
allTestTxDiscarded <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// this call blocks, and should return when DiscardQueuedTransaction() for a given tx id is called
|
2016-11-05 17:12:24 +00:00
|
|
|
sendTx := func() {
|
2017-10-20 09:06:22 +00:00
|
|
|
txHashCheck, err := statusAPI.SendTransaction(context.TODO(), common.SendTxArgs{
|
2017-05-16 12:09:52 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
2017-02-20 09:44:38 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
2016-11-05 17:12:24 +00:00
|
|
|
})
|
2018-01-05 20:58:17 +00:00
|
|
|
if err != transactions.ErrQueuedTxDiscarded {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Errorf("expected error not thrown: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
if !reflect.DeepEqual(txHashCheck, gethcommon.Hash{}) {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Error("transaction returned hash, while it shouldn't")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for transactions, and discard immediately
|
2017-05-03 14:24:48 +00:00
|
|
|
discardTxs := func(txIDStrings string) {
|
|
|
|
var parsedIDs []string
|
|
|
|
if err := json.Unmarshal([]byte(txIDStrings), &parsedIDs); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
parsedIDs = append(parsedIDs, "invalid-tx-id")
|
|
|
|
updatedTxIDStrings, _ := json.Marshal(parsedIDs)
|
2016-11-05 17:12:24 +00:00
|
|
|
|
|
|
|
// discard
|
2017-05-03 14:24:48 +00:00
|
|
|
discardResultsString := DiscardTransactions(C.CString(string(updatedTxIDStrings)))
|
2017-05-16 12:09:52 +00:00
|
|
|
discardResultsStruct := common.DiscardTransactionsResult{}
|
2017-05-03 14:24:48 +00:00
|
|
|
if err := json.Unmarshal([]byte(C.GoString(discardResultsString)), &discardResultsStruct); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
discardResults := discardResultsStruct.Results
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
if len(discardResults) != 1 || discardResults["invalid-tx-id"].Error != queue.ErrQueuedTxIDNotFound.Error() {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Errorf("cannot discard txs: %v", discardResults)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try completing discarded transaction
|
2017-05-16 12:09:52 +00:00
|
|
|
completeResultsString := CompleteTransactions(C.CString(string(updatedTxIDStrings)), C.CString(TestConfig.Account1.Password))
|
|
|
|
completeResultsStruct := common.CompleteTransactionsResult{}
|
2017-05-03 14:24:48 +00:00
|
|
|
if err := json.Unmarshal([]byte(C.GoString(completeResultsString)), &completeResultsStruct); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
completeResults := completeResultsStruct.Results
|
|
|
|
|
|
|
|
if len(completeResults) != (testTxCount + 1) {
|
|
|
|
t.Error("unexpected number of errors (call to CompleteTransaction should not succeed)")
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
for txID, txResult := range completeResults {
|
|
|
|
if txID != txResult.ID {
|
|
|
|
t.Errorf("tx id not set in result: expected id is %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if txResult.Error != queue.ErrQueuedTxIDNotFound.Error() {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Errorf("invalid error for %s", txResult.Hash)
|
|
|
|
return
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
if txResult.Hash != zeroHash {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Errorf("invalid hash (expected zero): %s", txResult.Hash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure that tx complete signal propagates
|
2017-05-03 14:24:48 +00:00
|
|
|
for _, txID := range parsedIDs {
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Has(common.QueuedTxID(txID)) {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("txqueue should not have test tx at this point (it should be discarded): %s", txID)
|
2016-11-05 17:12:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
2017-05-03 14:24:48 +00:00
|
|
|
var txIDStrings []string
|
2016-11-05 17:12:24 +00:00
|
|
|
for i := 0; i < testTxCount; i++ {
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDStrings = append(txIDStrings, <-txIDs)
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
txIDJSON, _ := json.Marshal(txIDStrings)
|
|
|
|
discardTxs(string(txIDJSON))
|
2016-11-05 17:12:24 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// send multiple transactions
|
|
|
|
for i := 0; i < testTxCount; i++ {
|
|
|
|
go sendTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-allTestTxDiscarded:
|
2017-08-04 16:14:17 +00:00
|
|
|
// pass
|
2016-11-05 17:12:24 +00:00
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Error("test timed out")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if txQueue.Count() != 0 {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Error("tx queue must be empty at this point")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
func testJailInitInvalid(t *testing.T) bool {
|
|
|
|
// Arrange.
|
|
|
|
initInvalidCode := `
|
|
|
|
var _status_catalog = {
|
|
|
|
foo: 'bar'
|
|
|
|
`
|
|
|
|
|
|
|
|
// Act.
|
|
|
|
InitJail(C.CString(initInvalidCode))
|
2017-11-07 17:36:42 +00:00
|
|
|
response := C.GoString(CreateAndInitCell(C.CString("CHAT_ID_INIT_INVALID_TEST"), C.CString(``)))
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
// Assert.
|
2017-11-07 17:36:42 +00:00
|
|
|
expectedSubstr := `"error":"(anonymous): Line 4:3 Unexpected identifier`
|
|
|
|
if !strings.Contains(response, expectedSubstr) {
|
|
|
|
t.Errorf("unexpected response, didn't find '%s' in '%s'", expectedSubstr, response)
|
2017-08-04 16:14:17 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func testJailParseInvalid(t *testing.T) bool {
|
|
|
|
// Arrange.
|
2018-01-09 16:53:33 +00:00
|
|
|
InitJail(C.CString(initJS))
|
2017-08-04 16:14:17 +00:00
|
|
|
// Act.
|
|
|
|
extraInvalidCode := `
|
|
|
|
var extraFunc = function (x) {
|
|
|
|
return x * x;
|
|
|
|
`
|
2017-11-07 17:36:42 +00:00
|
|
|
response := C.GoString(CreateAndInitCell(C.CString("CHAT_ID_PARSE_INVALID_TEST"), C.CString(extraInvalidCode)))
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
// Assert.
|
2017-11-07 17:36:42 +00:00
|
|
|
expectedResponse := `{"error":"(anonymous): Line 4:2 Unexpected end of input (and 1 more errors)"}`
|
2017-08-04 16:14:17 +00:00
|
|
|
if expectedResponse != response {
|
|
|
|
t.Errorf("unexpected response, expected: %v, got: %v", expectedResponse, response)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:35:58 +00:00
|
|
|
func testJailInit(t *testing.T) bool {
|
2018-01-09 16:53:33 +00:00
|
|
|
InitJail(C.CString(initJS))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
chatID := C.CString("CHAT_ID_INIT_TEST")
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
// Cell initialization return the result of the last JS operation provided to it.
|
|
|
|
response := CreateAndInitCell(chatID, C.CString(`var extraFunc = function (x) { return x * x; }; extraFunc(2);`))
|
|
|
|
require.Equal(t, `{"result": 4}`, C.GoString(response), "Unexpected response from jail.CreateAndInitCell()")
|
2016-11-14 01:35:58 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
// Commands from the jail initialization are available in any of the created cells.
|
|
|
|
response = ExecuteJS(chatID, C.CString(`JSON.stringify({ result: _status_catalog });`))
|
|
|
|
require.Equal(t, `{"result":{"foo":"bar"}}`, C.GoString(response), "Environment from `InitJail` is not available in the created cell")
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:37:59 +00:00
|
|
|
func testJailParseDeprecated(t *testing.T) bool {
|
2018-01-09 16:53:33 +00:00
|
|
|
InitJail(C.CString(initJS))
|
2017-11-23 12:37:59 +00:00
|
|
|
|
|
|
|
extraCode := `
|
|
|
|
var extraFunc = function (x) {
|
|
|
|
return x * x;
|
|
|
|
};
|
2018-01-09 16:53:33 +00:00
|
|
|
|
|
|
|
extraFunc(2);
|
2017-11-23 12:37:59 +00:00
|
|
|
`
|
2018-01-09 16:53:33 +00:00
|
|
|
chatID := C.CString("CHAT_ID_PARSE_TEST")
|
|
|
|
|
|
|
|
response := Parse(chatID, C.CString(extraCode))
|
|
|
|
require.Equal(t, `{"result": 4}`, C.GoString(response))
|
2017-11-23 12:37:59 +00:00
|
|
|
|
|
|
|
// cell already exists but Parse should not complain
|
2018-01-09 16:53:33 +00:00
|
|
|
response = Parse(chatID, C.CString(extraCode))
|
|
|
|
require.Equal(t, `{"result": 4}`, C.GoString(response))
|
2017-11-23 12:37:59 +00:00
|
|
|
|
|
|
|
// test extraCode
|
2018-01-09 16:53:33 +00:00
|
|
|
response = ExecuteJS(chatID, C.CString(`extraFunc(10)`))
|
|
|
|
require.Equal(t, `100`, C.GoString(response))
|
2017-11-23 12:37:59 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:35:58 +00:00
|
|
|
func testJailFunctionCall(t *testing.T) bool {
|
|
|
|
InitJail(C.CString(""))
|
|
|
|
|
|
|
|
// load Status JS and add test command to it
|
2017-05-16 12:09:52 +00:00
|
|
|
statusJS := string(static.MustAsset("testdata/jail/status.js")) + `;
|
2016-11-14 01:35:58 +00:00
|
|
|
_status_catalog.commands["testCommand"] = function (params) {
|
|
|
|
return params.val * params.val;
|
|
|
|
};`
|
2017-11-07 17:36:42 +00:00
|
|
|
CreateAndInitCell(C.CString("CHAT_ID_CALL_TEST"), C.CString(statusJS))
|
2016-11-14 01:35:58 +00:00
|
|
|
|
|
|
|
// call with wrong chat id
|
|
|
|
rawResponse := Call(C.CString("CHAT_IDNON_EXISTENT"), C.CString(""), C.CString(""))
|
|
|
|
parsedResponse := C.GoString(rawResponse)
|
2017-11-07 17:36:42 +00:00
|
|
|
expectedError := `{"error":"cell 'CHAT_IDNON_EXISTENT' not found"}`
|
2016-11-14 01:35:58 +00:00
|
|
|
if parsedResponse != expectedError {
|
|
|
|
t.Errorf("expected error is not returned: expected %s, got %s", expectedError, parsedResponse)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// call extraFunc()
|
|
|
|
rawResponse = Call(C.CString("CHAT_ID_CALL_TEST"), C.CString(`["commands", "testCommand"]`), C.CString(`{"val": 12}`))
|
|
|
|
parsedResponse = C.GoString(rawResponse)
|
|
|
|
expectedResponse := `{"result": 144}`
|
|
|
|
if parsedResponse != expectedResponse {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, parsedResponse)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("jailed method called: %s", parsedResponse)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
func testExecuteJS(t *testing.T) bool {
|
|
|
|
InitJail(C.CString(""))
|
|
|
|
|
|
|
|
// cell does not exist
|
|
|
|
response := C.GoString(ExecuteJS(C.CString("CHAT_ID_EXECUTE_TEST"), C.CString("('some string')")))
|
|
|
|
expectedResponse := `{"error":"cell 'CHAT_ID_EXECUTE_TEST' not found"}`
|
|
|
|
if response != expectedResponse {
|
|
|
|
t.Errorf("expected '%s' but got '%s'", expectedResponse, response)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateAndInitCell(C.CString("CHAT_ID_EXECUTE_TEST"), C.CString(`var obj = { status: true }`))
|
|
|
|
|
|
|
|
// cell does not exist
|
|
|
|
response = C.GoString(ExecuteJS(C.CString("CHAT_ID_EXECUTE_TEST"), C.CString(`JSON.stringify(obj)`)))
|
|
|
|
expectedResponse = `{"status":true}`
|
|
|
|
if response != expectedResponse {
|
|
|
|
t.Errorf("expected '%s' but got '%s'", expectedResponse, response)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
func startTestNode(t *testing.T) <-chan struct{} {
|
2017-11-07 17:46:11 +00:00
|
|
|
testDir := filepath.Join(TestDataDir, TestNetworkNames[GetNetworkID()])
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
syncRequired := false
|
2017-11-07 17:46:11 +00:00
|
|
|
if _, err := os.Stat(testDir); os.IsNotExist(err) {
|
2016-11-05 17:12:24 +00:00
|
|
|
syncRequired = true
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:49:15 +00:00
|
|
|
// inject test accounts
|
2017-11-07 17:46:11 +00:00
|
|
|
testKeyDir := filepath.Join(testDir, "keystore")
|
2017-11-20 18:21:30 +00:00
|
|
|
if err := common.ImportTestAccount(testKeyDir, GetAccount1PKFile()); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-20 18:21:30 +00:00
|
|
|
if err := common.ImportTestAccount(testKeyDir, GetAccount2PKFile()); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-15 21:03:01 +00:00
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
waitForNodeStart := make(chan struct{}, 1)
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
2016-11-05 17:12:24 +00:00
|
|
|
t.Log(jsonEvent)
|
2017-09-25 18:22:57 +00:00
|
|
|
var envelope signal.Envelope
|
2016-11-05 17:12:24 +00:00
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
2017-09-25 18:22:57 +00:00
|
|
|
if envelope.Type == signal.EventNodeCrashed {
|
|
|
|
signal.TriggerDefaultNodeNotificationHandler(jsonEvent)
|
2017-01-12 17:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
if envelope.Type == transactions.EventTransactionQueued {
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-09-25 18:22:57 +00:00
|
|
|
if envelope.Type == signal.EventNodeStarted {
|
2017-05-16 12:09:52 +00:00
|
|
|
t.Log("Node started, but we wait till it be ready")
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
2017-09-25 18:22:57 +00:00
|
|
|
if envelope.Type == signal.EventNodeReady {
|
2016-11-14 01:35:58 +00:00
|
|
|
// sync
|
2016-11-05 17:12:24 +00:00
|
|
|
if syncRequired {
|
2017-10-21 17:04:07 +00:00
|
|
|
t.Logf("Sync is required")
|
2017-10-23 21:39:13 +00:00
|
|
|
EnsureNodeSync(statusAPI.NodeManager())
|
2016-11-05 17:12:24 +00:00
|
|
|
} else {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we can proceed with tests
|
|
|
|
waitForNodeStart <- struct{}{}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-11-14 01:35:58 +00:00
|
|
|
go func() {
|
2017-05-16 12:09:52 +00:00
|
|
|
response := StartNode(C.CString(nodeConfigJSON))
|
|
|
|
responseErr := common.APIResponse{}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err := json.Unmarshal([]byte(C.GoString(response)), &responseErr); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if responseErr.Error != "" {
|
|
|
|
panic("cannot start node: " + responseErr.Error)
|
2016-11-14 01:35:58 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-11-05 17:12:24 +00:00
|
|
|
|
|
|
|
return waitForNodeStart
|
|
|
|
}
|
2017-08-10 15:31:29 +00:00
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//nolint: deadcode
|
2017-08-10 15:31:29 +00:00
|
|
|
func testValidateNodeConfig(t *testing.T, config string, fn func(common.APIDetailedResponse)) {
|
|
|
|
result := ValidateNodeConfig(C.CString(config))
|
|
|
|
|
|
|
|
var resp common.APIDetailedResponse
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(result)), &resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fn(resp)
|
|
|
|
}
|