2018-03-29 09:20:55 +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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-20 15:38:40 +00:00
|
|
|
"C"
|
2019-01-24 15:44:46 +00:00
|
|
|
"encoding/hex"
|
2018-03-29 09:20:55 +00:00
|
|
|
"encoding/json"
|
2019-07-26 14:45:10 +00:00
|
|
|
"fmt"
|
2018-03-29 09:20:55 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2018-03-29 09:20:55 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-01-24 15:44:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2018-05-03 07:35:58 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2018-03-29 09:20:55 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils" //nolint: golint
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2019-01-18 09:01:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-20 15:38:40 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-03-29 09:20:55 +00:00
|
|
|
)
|
2019-08-29 08:06:22 +00:00
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
|
|
|
const initJS = `
|
|
|
|
var _status_catalog = {
|
|
|
|
foo: 'bar'
|
|
|
|
};`
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
var (
|
2018-08-16 11:37:53 +00:00
|
|
|
zeroHash = gethcommon.Hash{}
|
2018-04-10 10:02:54 +00:00
|
|
|
testChainDir string
|
2019-08-20 15:38:40 +00:00
|
|
|
keystoreDir string
|
2018-04-10 10:02:54 +00:00
|
|
|
nodeConfigJSON string
|
|
|
|
)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func buildAccountData(name, chatAddress string) *C.char {
|
2019-07-26 14:45:10 +00:00
|
|
|
return C.CString(fmt.Sprintf(`{
|
2019-08-20 15:38:40 +00:00
|
|
|
"name": "%s",
|
|
|
|
"address": "%s"
|
|
|
|
}`, name, chatAddress))
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildSubAccountData(chatAddress string) *C.char {
|
|
|
|
accs := []accounts.Account{
|
|
|
|
{
|
|
|
|
Wallet: true,
|
|
|
|
Chat: true,
|
|
|
|
Address: gethcommon.HexToAddress(chatAddress),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data, _ := json.Marshal(accs)
|
|
|
|
return C.CString(string(data))
|
2019-07-26 14:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildLoginParams(mainAccountAddress, chatAddress, password string) account.LoginParams {
|
|
|
|
return account.LoginParams{
|
|
|
|
ChatAddress: gethcommon.HexToAddress(chatAddress),
|
|
|
|
Password: password,
|
|
|
|
MainAccount: gethcommon.HexToAddress(mainAccountAddress),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func waitSignal(feed *event.Feed, event string, timeout time.Duration) error {
|
|
|
|
events := make(chan signal.Envelope)
|
|
|
|
sub := feed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
after := time.After(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case envelope := <-events:
|
|
|
|
if envelope.Type == event {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case <-after:
|
|
|
|
return fmt.Errorf("signal %v wasn't received in %v", event, timeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAccountAndLogin(t *testing.T, feed *event.Feed) account.Info {
|
|
|
|
account1, _, err := statusBackend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("account created: {address: %s, key: %s}", account1.WalletAddress, account1.WalletPubKey)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// select account
|
|
|
|
loginResponse := APIResponse{}
|
|
|
|
rawResponse := SaveAccountAndLogin(buildAccountData("test", account1.WalletAddress), C.CString(TestConfig.Account1.Password), C.CString(nodeConfigJSON), buildSubAccountData(account1.WalletAddress))
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse))
|
|
|
|
require.Empty(t, loginResponse.Error)
|
|
|
|
require.NoError(t, waitSignal(feed, signal.EventLoggedIn, 5*time.Second))
|
|
|
|
return account1
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: deadcode
|
|
|
|
func testExportedAPI(t *testing.T) {
|
|
|
|
testDir := filepath.Join(TestDataDir, TestNetworkNames[GetNetworkID()])
|
|
|
|
_ = OpenAccounts(C.CString(testDir))
|
|
|
|
// inject test accounts
|
|
|
|
testKeyDir := filepath.Join(testDir, "keystore")
|
|
|
|
_ = InitKeystore(C.CString(testKeyDir))
|
|
|
|
require.NoError(t, ImportTestAccount(testKeyDir, GetAccount1PKFile()))
|
|
|
|
require.NoError(t, ImportTestAccount(testKeyDir, GetAccount2PKFile()))
|
2018-03-29 09:20:55 +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.
|
2019-08-20 15:38:40 +00:00
|
|
|
// NOTE(dshulyak) tests are using same backend with same keystore. but after every test we explicitly logging out.
|
2018-03-29 09:20:55 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-08-20 15:38:40 +00:00
|
|
|
fn func(t *testing.T, feed *event.Feed) bool
|
2018-03-29 09:20:55 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"StopResumeNode",
|
2018-03-29 09:20:55 +00:00
|
|
|
testStopResumeNode,
|
|
|
|
},
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"RPCInProc",
|
2018-03-29 09:20:55 +00:00
|
|
|
testCallRPC,
|
|
|
|
},
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"RPCPrivateAPI",
|
2018-04-16 08:01:37 +00:00
|
|
|
testCallRPCWithPrivateAPI,
|
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"RPCPrivateClient",
|
2018-04-16 08:01:37 +00:00
|
|
|
testCallPrivateRPCWithPrivateAPI,
|
|
|
|
},
|
2018-03-29 09:20:55 +00:00
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"VerifyAccountPassword",
|
2018-03-29 09:20:55 +00:00
|
|
|
testVerifyAccountPassword,
|
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"RecoverAccount",
|
2018-03-29 09:20:55 +00:00
|
|
|
testRecoverAccount,
|
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"LoginKeycard",
|
2019-01-24 15:44:46 +00:00
|
|
|
testLoginWithKeycard,
|
|
|
|
},
|
2018-03-29 09:20:55 +00:00
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"AccountLoout",
|
2018-03-29 09:20:55 +00:00
|
|
|
testAccountLogout,
|
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"SendTransaction",
|
|
|
|
testSendTransactionWithLogin,
|
2018-03-29 09:20:55 +00:00
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"SendTransactionInvalidPassword",
|
2018-08-16 11:37:53 +00:00
|
|
|
testSendTransactionInvalidPassword,
|
2018-03-29 09:20:55 +00:00
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"SendTransactionFailed",
|
2018-08-16 11:37:53 +00:00
|
|
|
testFailedTransaction,
|
2018-03-29 09:20:55 +00:00
|
|
|
},
|
2019-07-24 18:59:15 +00:00
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"MultiAccount/Generate/Derive/StoreDerived/Load/Reset",
|
2019-07-26 09:33:38 +00:00
|
|
|
testMultiAccountGenerateDeriveStoreLoadReset,
|
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"MultiAccount/ImportMnemonic/Derive",
|
2019-07-26 09:33:38 +00:00
|
|
|
testMultiAccountImportMnemonicAndDerive,
|
2019-07-24 18:59:15 +00:00
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"MultiAccount/GenerateAndDerive",
|
2019-07-24 18:59:15 +00:00
|
|
|
testMultiAccountGenerateAndDerive,
|
|
|
|
},
|
|
|
|
{
|
2019-08-20 15:38:40 +00:00
|
|
|
"MultiAccount/Import/Store",
|
2019-07-24 18:59:15 +00:00
|
|
|
testMultiAccountImportStore,
|
|
|
|
},
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
feed := &event.Feed{}
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(jsonEvent), &envelope))
|
|
|
|
feed.Send(envelope)
|
|
|
|
})
|
|
|
|
defer func() {
|
|
|
|
if snode := statusBackend.StatusNode(); snode == nil || !snode.IsRunning() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Logout()
|
|
|
|
waitSignal(feed, signal.EventNodeStopped, 5*time.Second)
|
|
|
|
}()
|
|
|
|
require.True(t, tc.fn(t, feed))
|
|
|
|
})
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testVerifyAccountPassword(t *testing.T, feed *event.Feed) bool {
|
2018-03-29 09:20:55 +00:00
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "accounts")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir) // nolint: errcheck
|
|
|
|
|
2018-04-04 17:39:38 +00:00
|
|
|
if err = ImportTestAccount(tmpDir, GetAccount1PKFile()); err != nil {
|
2018-03-29 09:20:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-04 17:39:38 +00:00
|
|
|
if err = ImportTestAccount(tmpDir, GetAccount2PKFile()); err != nil {
|
2018-03-29 09:20:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename account file (to see that file's internals reviewed, when locating account key)
|
|
|
|
accountFilePathOriginal := filepath.Join(tmpDir, GetAccount1PKFile())
|
2019-01-18 09:01:14 +00:00
|
|
|
accountFilePath := filepath.Join(tmpDir, "foo"+TestConfig.Account1.WalletAddress+"bar.pk")
|
2018-03-29 09:20:55 +00:00
|
|
|
if err := os.Rename(accountFilePathOriginal, accountFilePath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
response := APIResponse{}
|
|
|
|
rawResponse := VerifyAccountPassword(
|
|
|
|
C.CString(tmpDir),
|
2019-01-18 09:01:14 +00:00
|
|
|
C.CString(TestConfig.Account1.WalletAddress),
|
2018-03-29 09:20:55 +00:00
|
|
|
C.CString(TestConfig.Account1.Password))
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testStopResumeNode(t *testing.T, feed *event.Feed) bool { //nolint: gocyclo
|
|
|
|
account1 := createAccountAndLogin(t, feed)
|
2018-06-19 07:49:24 +00:00
|
|
|
whisperService, err := statusBackend.StatusNode().WhisperService()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, whisperService.HasKeyPair(account1.ChatPubKey), "whisper should have keypair")
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
response := APIResponse{}
|
|
|
|
rawResponse := StopNode()
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(C.GoString(rawResponse)), &response))
|
|
|
|
require.Empty(t, response.Error)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, waitSignal(feed, signal.EventNodeStopped, 3*time.Second))
|
|
|
|
response = APIResponse{}
|
|
|
|
rawResponse = StartNode(C.CString(nodeConfigJSON))
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(C.GoString(rawResponse)), &response))
|
|
|
|
require.Empty(t, response.Error)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, waitSignal(feed, signal.EventNodeReady, 5*time.Second))
|
2018-03-29 09:20:55 +00:00
|
|
|
|
|
|
|
// now, verify that we still have account logged in
|
2018-06-19 07:49:24 +00:00
|
|
|
whisperService, err = statusBackend.StatusNode().WhisperService()
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, whisperService.HasKeyPair(account1.ChatPubKey))
|
2018-03-29 09:20:55 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testCallRPC(t *testing.T, feed *event.Feed) bool {
|
|
|
|
createAccountAndLogin(t, feed)
|
2018-03-29 09:20:55 +00:00
|
|
|
expected := `{"jsonrpc":"2.0","id":64,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}`
|
|
|
|
rawResponse := CallRPC(C.CString(`{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`))
|
|
|
|
received := C.GoString(rawResponse)
|
|
|
|
if expected != received {
|
|
|
|
t.Errorf("unexpected response: expected: %v, got: %v", expected, received)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testCallRPCWithPrivateAPI(t *testing.T, feed *event.Feed) bool {
|
|
|
|
createAccountAndLogin(t, feed)
|
2019-10-04 15:21:24 +00:00
|
|
|
expected := `{"jsonrpc":"2.0","id":64,"error":{"code":-32601,"message":"the method admin_nodeInfo does not exist/is not available"}}`
|
2018-04-16 08:01:37 +00:00
|
|
|
rawResponse := CallRPC(C.CString(`{"jsonrpc":"2.0","method":"admin_nodeInfo","params":[],"id":64}`))
|
2019-08-20 15:38:40 +00:00
|
|
|
require.Equal(t, expected, C.GoString(rawResponse))
|
2018-04-16 08:01:37 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testCallPrivateRPCWithPrivateAPI(t *testing.T, feed *event.Feed) bool {
|
|
|
|
createAccountAndLogin(t, feed)
|
2018-04-16 08:01:37 +00:00
|
|
|
rawResponse := CallPrivateRPC(C.CString(`{"jsonrpc":"2.0","method":"admin_nodeInfo","params":[],"id":64}`))
|
|
|
|
received := C.GoString(rawResponse)
|
|
|
|
if strings.Contains(received, "error") {
|
|
|
|
t.Errorf("unexpected response containing error: %v", received)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testRecoverAccount(t *testing.T, feed *event.Feed) bool { //nolint: gocyclo
|
|
|
|
keyStore := statusBackend.AccountManager().GetKeystore()
|
|
|
|
require.NotNil(t, keyStore)
|
2018-03-29 09:20:55 +00:00
|
|
|
// create an account
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo, mnemonic, err := statusBackend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2018-03-29 09:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2019-01-18 09:01:14 +00:00
|
|
|
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", accountInfo.WalletAddress, accountInfo.WalletPubKey, mnemonic)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
|
|
|
// try recovering using password + mnemonic
|
|
|
|
recoverAccountResponse := AccountInfo{}
|
|
|
|
rawResponse := RecoverAccount(C.CString(TestConfig.Account1.Password), C.CString(mnemonic))
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &recoverAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recoverAccountResponse.Error != "" {
|
|
|
|
t.Errorf("recover account failed: %v", recoverAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
2019-01-18 09:01:14 +00:00
|
|
|
|
|
|
|
if recoverAccountResponse.Address != recoverAccountResponse.WalletAddress ||
|
|
|
|
recoverAccountResponse.PubKey != recoverAccountResponse.WalletPubKey {
|
|
|
|
t.Error("for backward compatibility pubkey/address should be equal to walletAddress/walletPubKey")
|
|
|
|
}
|
|
|
|
|
|
|
|
walletAddressCheck, walletPubKeyCheck := recoverAccountResponse.Address, recoverAccountResponse.PubKey
|
|
|
|
chatAddressCheck, chatPubKeyCheck := recoverAccountResponse.ChatAddress, recoverAccountResponse.ChatPubKey
|
|
|
|
|
|
|
|
if accountInfo.WalletAddress != walletAddressCheck || accountInfo.WalletPubKey != walletPubKeyCheck {
|
|
|
|
t.Error("recover wallet account details failed to pull the correct details")
|
|
|
|
}
|
|
|
|
|
|
|
|
if accountInfo.ChatAddress != chatAddressCheck || accountInfo.ChatPubKey != chatPubKeyCheck {
|
|
|
|
t.Error("recover chat account details failed to pull the correct details")
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now test recovering, but make sure that account/key file is removed i.e. simulate recovering on a new device
|
2019-01-18 09:01:14 +00:00
|
|
|
account, err := account.ParseAccountString(accountInfo.WalletAddress)
|
2018-03-29 09:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not get account from address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
account, key, err := keyStore.AccountDecryptedKey(account, TestConfig.Account1.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
extChild2String := key.ExtendedKey.String()
|
|
|
|
|
|
|
|
if err = keyStore.Delete(account, TestConfig.Account1.Password); err != nil {
|
|
|
|
t.Errorf("cannot remove account: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recoverAccountResponse = AccountInfo{}
|
|
|
|
rawResponse = RecoverAccount(C.CString(TestConfig.Account1.Password), C.CString(mnemonic))
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &recoverAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recoverAccountResponse.Error != "" {
|
|
|
|
t.Errorf("recover account failed (for non-cached account): %v", recoverAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
2019-01-18 09:01:14 +00:00
|
|
|
walletAddressCheck, walletPubKeyCheck = recoverAccountResponse.Address, recoverAccountResponse.PubKey
|
|
|
|
if accountInfo.WalletAddress != walletAddressCheck || accountInfo.WalletPubKey != walletPubKeyCheck {
|
|
|
|
t.Error("recover wallet account details failed to pull the correct details (for non-cached account)")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatAddressCheck, chatPubKeyCheck = recoverAccountResponse.ChatAddress, recoverAccountResponse.ChatPubKey
|
|
|
|
if accountInfo.ChatAddress != chatAddressCheck || accountInfo.ChatPubKey != chatPubKeyCheck {
|
|
|
|
t.Error("recover chat account details failed to pull the correct details (for non-cached account)")
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that extended key exists and is imported ok too
|
|
|
|
_, key, err = keyStore.AccountDecryptedKey(account, TestConfig.Account1.Password)
|
|
|
|
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)
|
|
|
|
recoverAccountResponse = AccountInfo{}
|
|
|
|
rawResponse = RecoverAccount(C.CString(TestConfig.Account1.Password), C.CString(mnemonic))
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &recoverAccountResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recoverAccountResponse.Error != "" {
|
|
|
|
t.Errorf("recover account failed (for non-cached account): %v", recoverAccountResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
2019-01-18 09:01:14 +00:00
|
|
|
walletAddressCheck, walletPubKeyCheck = recoverAccountResponse.Address, recoverAccountResponse.PubKey
|
|
|
|
if accountInfo.WalletAddress != walletAddressCheck || accountInfo.WalletPubKey != walletPubKeyCheck {
|
|
|
|
t.Error("recover wallet account details failed to pull the correct details (for non-cached account)")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatAddressCheck, chatPubKeyCheck = recoverAccountResponse.ChatAddress, recoverAccountResponse.ChatPubKey
|
|
|
|
if accountInfo.ChatAddress != chatAddressCheck || accountInfo.ChatPubKey != chatPubKeyCheck {
|
|
|
|
t.Error("recover chat account details failed to pull the correct details (for non-cached account)")
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
loginResponse := APIResponse{}
|
|
|
|
rawResponse = SaveAccountAndLogin(buildAccountData("test", walletAddressCheck), C.CString(TestConfig.Account1.Password), C.CString(nodeConfigJSON), buildSubAccountData(walletAddressCheck))
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse))
|
|
|
|
require.Empty(t, loginResponse.Error)
|
|
|
|
require.NoError(t, waitSignal(feed, signal.EventLoggedIn, 5*time.Second))
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
// time to login with recovered data
|
2018-06-19 07:49:24 +00:00
|
|
|
whisperService, err := statusBackend.StatusNode().WhisperService()
|
2018-03-29 09:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
if !whisperService.HasKeyPair(chatPubKeyCheck) {
|
2018-03-29 09:20:55 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testLoginWithKeycard(t *testing.T, feed *event.Feed) bool { //nolint: gocyclo
|
|
|
|
createAccountAndLogin(t, feed)
|
2019-01-24 15:44:46 +00:00
|
|
|
chatPrivKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error generating chat key")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
chatPrivKeyHex := hex.EncodeToString(crypto.FromECDSA(chatPrivKey))
|
|
|
|
|
|
|
|
encryptionPrivKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error generating encryption key")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
encryptionPrivKeyHex := hex.EncodeToString(crypto.FromECDSA(encryptionPrivKey))
|
|
|
|
|
|
|
|
whisperService, err := statusBackend.StatusNode().WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chatPubKeyHex := hexutil.Encode(crypto.FromECDSAPub(&chatPrivKey.PublicKey))
|
|
|
|
if whisperService.HasKeyPair(chatPubKeyHex) {
|
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
loginResponse := APIResponse{}
|
|
|
|
rawResponse := LoginWithKeycard(C.CString(chatPrivKeyHex), C.CString(encryptionPrivKeyHex))
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode LoginWithKeycard response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Error != "" {
|
|
|
|
t.Errorf("Test failed: could not login with keycard: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !whisperService.HasKeyPair(chatPubKeyHex) {
|
|
|
|
t.Error("identity not present in whisper after logging in with keycard")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testAccountLogout(t *testing.T, feed *event.Feed) bool {
|
|
|
|
accountInfo := createAccountAndLogin(t, feed)
|
2018-06-19 07:49:24 +00:00
|
|
|
whisperService, err := statusBackend.StatusNode().WhisperService()
|
2018-03-29 09:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
if !whisperService.HasKeyPair(accountInfo.ChatPubKey) {
|
2018-03-29 09:20:55 +00:00
|
|
|
t.Error("identity not injected into whisper")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
logoutResponse := APIResponse{}
|
|
|
|
rawResponse := Logout()
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResponse)), &logoutResponse); err != nil {
|
|
|
|
t.Errorf("cannot decode RecoverAccount response (%s): %v", C.GoString(rawResponse), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if logoutResponse.Error != "" {
|
|
|
|
t.Errorf("cannot logout: %v", logoutResponse.Error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, logout and check if identity is removed indeed
|
2019-01-18 09:01:14 +00:00
|
|
|
if whisperService.HasKeyPair(accountInfo.ChatPubKey) {
|
2018-03-29 09:20:55 +00:00
|
|
|
t.Error("identity not cleared from whisper")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
type jsonrpcAnyResponse struct {
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
jsonrpcErrorResponse
|
|
|
|
}
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testSendTransactionWithLogin(t *testing.T, feed *event.Feed) bool {
|
|
|
|
loginResponse := APIResponse{}
|
|
|
|
rawResponse := SaveAccountAndLogin(buildAccountData("test", TestConfig.Account1.WalletAddress), C.CString(TestConfig.Account1.Password), C.CString(nodeConfigJSON), buildSubAccountData(TestConfig.Account1.WalletAddress))
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(C.GoString(rawResponse)), &loginResponse))
|
|
|
|
require.Empty(t, loginResponse.Error)
|
|
|
|
require.NoError(t, waitSignal(feed, signal.EventLoggedIn, 5*time.Second))
|
2018-06-19 07:49:24 +00:00
|
|
|
EnsureNodeSync(statusBackend.StatusNode().EnsureSync)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
args, err := json.Marshal(transactions.SendTxArgs{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-03-29 09:20:55 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-08-16 11:37:53 +00:00
|
|
|
t.Errorf("failed to marshal errors: %v", err)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
rawResult := SendTransaction(C.CString(string(args)), C.CString(TestConfig.Account1.Password))
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
var result jsonrpcAnyResponse
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResult)), &result); err != nil {
|
|
|
|
t.Errorf("failed to unmarshal rawResult '%s': %v", C.GoString(rawResult), err)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
if result.Error.Message != "" {
|
|
|
|
t.Errorf("failed to send transaction: %v", result.Error)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
hash := gethcommon.BytesToHash(result.Result)
|
|
|
|
if reflect.DeepEqual(hash, gethcommon.Hash{}) {
|
|
|
|
t.Errorf("response hash empty: %s", hash.Hex())
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testSendTransactionInvalidPassword(t *testing.T, feed *event.Feed) bool {
|
2019-08-29 08:06:22 +00:00
|
|
|
acc := createAccountAndLogin(t, feed)
|
2018-08-16 11:37:53 +00:00
|
|
|
EnsureNodeSync(statusBackend.StatusNode().EnsureSync)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
args, err := json.Marshal(transactions.SendTxArgs{
|
2019-08-29 08:06:22 +00:00
|
|
|
From: common.HexToAddress(acc.WalletAddress),
|
2019-01-18 09:01:14 +00:00
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-08-16 11:37:53 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
2018-03-29 09:20:55 +00:00
|
|
|
})
|
2018-08-16 11:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to marshal errors: %v", err)
|
|
|
|
return false
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
rawResult := SendTransaction(C.CString(string(args)), C.CString("invalid password"))
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
var result jsonrpcAnyResponse
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResult)), &result); err != nil {
|
|
|
|
t.Errorf("failed to unmarshal rawResult '%s': %v", C.GoString(rawResult), err)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
if result.Error.Message != keystore.ErrDecrypt.Error() {
|
|
|
|
t.Errorf("invalid result: %q", result)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func testFailedTransaction(t *testing.T, feed *event.Feed) bool {
|
|
|
|
createAccountAndLogin(t, feed)
|
2018-08-16 11:37:53 +00:00
|
|
|
EnsureNodeSync(statusBackend.StatusNode().EnsureSync)
|
2018-03-29 09:20:55 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
args, err := json.Marshal(transactions.SendTxArgs{
|
2019-08-29 08:06:22 +00:00
|
|
|
From: *account.ToAddress(TestConfig.Account1.WalletAddress),
|
2019-01-18 09:01:14 +00:00
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-03-29 09:20:55 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
2018-08-16 11:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to marshal errors: %v", err)
|
2018-05-29 11:24:23 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
rawResult := SendTransaction(C.CString(string(args)), C.CString(TestConfig.Account1.Password))
|
2018-05-29 11:24:23 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
var result jsonrpcAnyResponse
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(rawResult)), &result); err != nil {
|
|
|
|
t.Errorf("failed to unmarshal rawResult '%s': %v", C.GoString(rawResult), err)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
if result.Error.Message != transactions.ErrAccountDoesntExist.Error() {
|
|
|
|
t.Errorf("expected error to be ErrAccountDoesntExist, got %s", result.Error.Message)
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
if result.Result != nil {
|
|
|
|
t.Errorf("expected result to be nil")
|
2018-03-29 09:20:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-29 11:24:23 +00:00
|
|
|
return true
|
2018-03-29 09:20:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint: deadcode
|
2019-01-17 12:56:22 +00:00
|
|
|
func testValidateNodeConfig(t *testing.T, config string, fn func(*testing.T, APIDetailedResponse)) {
|
2018-03-29 09:20:55 +00:00
|
|
|
result := ValidateNodeConfig(C.CString(config))
|
|
|
|
|
|
|
|
var resp APIDetailedResponse
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(result)), &resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-01-17 12:56:22 +00:00
|
|
|
fn(t, resp)
|
2018-03-29 09:20:55 +00:00
|
|
|
}
|