2016-09-11 11:44:14 +00:00
|
|
|
package geth_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-05-06 21:53:18 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-09-11 11:44:14 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-05-06 21:53:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/status-im/status-go/geth"
|
|
|
|
)
|
|
|
|
|
2017-05-06 21:53:18 +00:00
|
|
|
func TestVerifyAccountPassword(t *testing.T) {
|
2017-05-15 21:49:22 +00:00
|
|
|
keyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts")
|
2017-05-06 21:53:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-15 21:49:22 +00:00
|
|
|
defer os.RemoveAll(keyStoreDir) // nolint: errcheck
|
2017-05-06 21:53:18 +00:00
|
|
|
|
2017-05-15 21:49:22 +00:00
|
|
|
emptyKeyStoreDir, err := ioutil.TempDir(os.TempDir(), "empty")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(emptyKeyStoreDir) // nolint: errcheck
|
|
|
|
|
|
|
|
// import account keys
|
|
|
|
if err = geth.ImportTestAccount(keyStoreDir, "test-account1.pk"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = geth.ImportTestAccount(keyStoreDir, "test-account2.pk"); err != nil {
|
2017-05-06 21:53:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
account1Address := common.BytesToAddress(common.FromHex(testConfig.Account1.Address))
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
keyPath string
|
|
|
|
address string
|
|
|
|
password string
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"correct address, correct password (decrypt should succeed)",
|
2017-05-15 21:49:22 +00:00
|
|
|
keyStoreDir,
|
2017-05-06 21:53:18 +00:00
|
|
|
testConfig.Account1.Address,
|
|
|
|
testConfig.Account1.Password,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2017-05-15 21:49:22 +00:00
|
|
|
"correct address, correct password, non-existent key store",
|
|
|
|
filepath.Join(keyStoreDir, "non-existent-folder"),
|
|
|
|
testConfig.Account1.Address,
|
|
|
|
testConfig.Account1.Password,
|
|
|
|
fmt.Errorf("cannot traverse key store folder: lstat %s/non-existent-folder: no such file or directory", keyStoreDir),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"correct address, correct password, empty key store (pk is not there)",
|
|
|
|
emptyKeyStoreDir,
|
2017-05-06 21:53:18 +00:00
|
|
|
testConfig.Account1.Address,
|
|
|
|
testConfig.Account1.Password,
|
2017-05-15 21:49:22 +00:00
|
|
|
fmt.Errorf("cannot locate account for address: %x", account1Address),
|
2017-05-06 21:53:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong address, correct password",
|
2017-05-15 21:49:22 +00:00
|
|
|
keyStoreDir,
|
|
|
|
"0x79791d3e8f2daa1f7fec29649d152c0ada3cc535",
|
2017-05-06 21:53:18 +00:00
|
|
|
testConfig.Account1.Password,
|
2017-05-15 21:49:22 +00:00
|
|
|
fmt.Errorf("cannot locate account for address: %s", "79791d3e8f2daa1f7fec29649d152c0ada3cc535"),
|
2017-05-06 21:53:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"correct address, wrong password",
|
2017-05-15 21:49:22 +00:00
|
|
|
keyStoreDir,
|
2017-05-06 21:53:18 +00:00
|
|
|
testConfig.Account1.Address,
|
|
|
|
"wrong password", // wrong password
|
|
|
|
errors.New("could not decrypt key with given passphrase"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Log(testCase.name)
|
|
|
|
accountKey, err := geth.VerifyAccountPassword(testCase.keyPath, testCase.address, testCase.password)
|
|
|
|
if !reflect.DeepEqual(err, testCase.expectedError) {
|
2017-05-15 21:49:22 +00:00
|
|
|
t.Fatalf("unexpected error: expected \n'%v', got \n'%v'", testCase.expectedError, err)
|
2017-05-06 21:53:18 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if accountKey == nil {
|
|
|
|
t.Error("no error reported, but account key is missing")
|
|
|
|
}
|
|
|
|
accountAddress := common.BytesToAddress(common.FromHex(testCase.address))
|
|
|
|
if accountKey.Address != accountAddress {
|
2017-05-15 21:49:22 +00:00
|
|
|
t.Fatalf("account mismatch: have %x, want %x", accountKey.Address, accountAddress)
|
2017-05-06 21:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:51:08 +00:00
|
|
|
func TestAccountsList(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
les, err := geth.NodeManagerInstance().LightEthereumService()
|
2016-09-27 13:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected LES service: %v", err)
|
|
|
|
}
|
|
|
|
accounts := les.StatusBackend.AccountManager().Accounts()
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = geth.Logout(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-27 13:51:08 +00:00
|
|
|
|
|
|
|
// make sure that we start with empty accounts list (nobody has logged in yet)
|
|
|
|
if len(accounts) != 0 {
|
|
|
|
t.Error("accounts returned, while there should be none (we haven't logged in yet)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-03-14 22:57:07 +00:00
|
|
|
address, _, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2016-09-27 13:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that there is still no accounts returned
|
|
|
|
accounts = les.StatusBackend.AccountManager().Accounts()
|
|
|
|
if len(accounts) != 0 {
|
|
|
|
t.Error("accounts returned, while there should be none (we haven't logged in yet)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// select account (sub-accounts will be created for this key)
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address, testConfig.Account1.Password)
|
2016-09-27 13:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// at this point main account should show up
|
|
|
|
accounts = les.StatusBackend.AccountManager().Accounts()
|
|
|
|
if len(accounts) != 1 {
|
|
|
|
t.Error("exactly single account is expected (main account)")
|
|
|
|
return
|
|
|
|
}
|
2017-02-27 12:52:10 +00:00
|
|
|
if string(accounts[0].Hex()) != "0x"+address {
|
2016-09-27 13:51:08 +00:00
|
|
|
t.Errorf("main account is not retured as the first key: got %s, expected %s",
|
2017-02-27 12:52:10 +00:00
|
|
|
accounts[0].Hex(), "0x"+address)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sub-account 1
|
2017-03-14 22:57:07 +00:00
|
|
|
subAccount1, subPubKey1, err := geth.CreateChildAccount("", testConfig.Account1.Password)
|
2016-09-27 13:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create sub-account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we expect to see both main account and sub-account 1
|
|
|
|
accounts = les.StatusBackend.AccountManager().Accounts()
|
|
|
|
if len(accounts) != 2 {
|
|
|
|
t.Error("exactly 2 accounts are expected (main + sub-account 1)")
|
|
|
|
return
|
|
|
|
}
|
2017-02-27 12:52:10 +00:00
|
|
|
if string(accounts[0].Hex()) != "0x"+address {
|
2016-09-27 13:51:08 +00:00
|
|
|
t.Errorf("main account is not retured as the first key: got %s, expected %s",
|
2017-02-27 12:52:10 +00:00
|
|
|
accounts[0].Hex(), "0x"+address)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-27 12:52:10 +00:00
|
|
|
if string(accounts[1].Hex()) != "0x"+subAccount1 {
|
|
|
|
t.Errorf("subAcount1 not returned: got %s, expected %s", accounts[1].Hex(), "0x"+subAccount1)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sub-account 2, index automatically progresses
|
2017-03-14 22:57:07 +00:00
|
|
|
subAccount2, subPubKey2, err := geth.CreateChildAccount("", testConfig.Account1.Password)
|
2016-09-27 13:51:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create sub-account: %v", err)
|
|
|
|
}
|
|
|
|
if subAccount1 == subAccount2 || subPubKey1 == subPubKey2 {
|
|
|
|
t.Error("sub-account index auto-increament failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, all 3 accounts should show up (main account, sub-accounts 1 and 2)
|
|
|
|
accounts = les.StatusBackend.AccountManager().Accounts()
|
|
|
|
if len(accounts) != 3 {
|
|
|
|
t.Errorf("unexpected number of accounts: expected %d, got %d", 3, len(accounts))
|
|
|
|
return
|
|
|
|
}
|
2017-02-27 12:52:10 +00:00
|
|
|
if string(accounts[0].Hex()) != "0x"+address {
|
2016-09-27 13:51:08 +00:00
|
|
|
t.Errorf("main account is not retured as the first key: got %s, expected %s",
|
2017-02-27 12:52:10 +00:00
|
|
|
accounts[0].Hex(), "0x"+address)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-27 12:52:10 +00:00
|
|
|
subAccount1MatchesKey1 := string(accounts[1].Hex()) != "0x"+subAccount1
|
|
|
|
subAccount1MatchesKey2 := string(accounts[2].Hex()) != "0x"+subAccount1
|
2016-09-27 13:51:08 +00:00
|
|
|
if !subAccount1MatchesKey1 && !subAccount1MatchesKey2 {
|
2017-02-27 12:52:10 +00:00
|
|
|
t.Errorf("subAcount1 not returned: got %s, expected %s", accounts[1].Hex(), "0x"+subAccount1)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-27 12:52:10 +00:00
|
|
|
subAccount2MatchesKey1 := string(accounts[1].Hex()) != "0x"+subAccount2
|
|
|
|
subAccount2MatchesKey2 := string(accounts[2].Hex()) != "0x"+subAccount2
|
2016-09-27 13:51:08 +00:00
|
|
|
if !subAccount2MatchesKey1 && !subAccount2MatchesKey2 {
|
2017-02-27 12:52:10 +00:00
|
|
|
t.Errorf("subAcount2 not returned: got %s, expected %s", accounts[2].Hex(), "0x"+subAccount1)
|
2016-09-27 13:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
func TestAccountsCreateChildAccount(t *testing.T) {
|
2016-09-11 11:44:14 +00:00
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// to make sure that we start with empty account (which might get populated during previous tests)
|
|
|
|
if err = geth.Logout(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-27 13:51:08 +00:00
|
|
|
|
2017-02-27 12:52:10 +00:00
|
|
|
keyStore, err := geth.NodeManagerInstance().AccountKeyStore()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-03-14 22:57:07 +00:00
|
|
|
address, pubKey, mnemonic, err := geth.CreateAccount(testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-02-27 12:52:10 +00:00
|
|
|
account, err := geth.ParseAccountString(address)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not get account from address: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// obtain decrypted key, and make sure that extended key (which will be used as root for sub-accounts) is present
|
2017-05-03 14:24:48 +00:00
|
|
|
_, key, err := keyStore.AccountDecryptedKey(account, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.ExtendedKey == nil {
|
|
|
|
t.Error("CKD#2 has not been generated for new account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try creating sub-account, w/o selecting main account i.e. w/o login to main account
|
2017-03-14 22:57:07 +00:00
|
|
|
_, _, err = geth.CreateChildAccount("", testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if !reflect.DeepEqual(err, geth.ErrNoAccountSelected) {
|
|
|
|
t.Errorf("expected error is not returned (tried to create sub-account w/o login): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to create sub-account with wrong password
|
|
|
|
_, _, err = geth.CreateChildAccount("", "wrong password")
|
2017-05-03 14:24:48 +00:00
|
|
|
if !reflect.DeepEqual(err, errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase")) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Errorf("expected error is not returned (tried to create sub-account with wrong password): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sub-account (from implicit parent)
|
2017-03-14 22:57:07 +00:00
|
|
|
subAccount1, subPubKey1, err := geth.CreateChildAccount("", testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create sub-account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that sub-account index automatically progresses
|
2017-03-14 22:57:07 +00:00
|
|
|
subAccount2, subPubKey2, err := geth.CreateChildAccount("", testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create sub-account: %v", err)
|
|
|
|
}
|
|
|
|
if subAccount1 == subAccount2 || subPubKey1 == subPubKey2 {
|
|
|
|
t.Error("sub-account index auto-increament failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sub-account (from explicit parent)
|
2017-03-14 22:57:07 +00:00
|
|
|
subAccount3, subPubKey3, err := geth.CreateChildAccount(subAccount2, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create sub-account: %v", err)
|
|
|
|
}
|
|
|
|
if subAccount1 == subAccount3 || subPubKey1 == subPubKey3 || subAccount2 == subAccount3 || subPubKey2 == subPubKey3 {
|
|
|
|
t.Error("sub-account index auto-increament failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
func TestAccountsRecoverAccount(t *testing.T) {
|
2016-09-11 11:44:14 +00:00
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:52:10 +00:00
|
|
|
keyStore, _ := geth.NodeManagerInstance().AccountKeyStore()
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
// create an account
|
2017-03-14 22:57:07 +00:00
|
|
|
address, pubKey, mnemonic, err := geth.CreateAccount(testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
// try recovering using password + mnemonic
|
2017-03-14 22:57:07 +00:00
|
|
|
addressCheck, pubKeyCheck, err := geth.RecoverAccount(testConfig.Account1.Password, mnemonic)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("recover account failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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-02-27 12:52:10 +00:00
|
|
|
account, err := geth.ParseAccountString(address)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not get account from address: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
account, key, err := keyStore.AccountDecryptedKey(account, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
extChild2String := key.ExtendedKey.String()
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
if err := keyStore.Delete(account, testConfig.Account1.Password); err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Errorf("cannot remove account: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
addressCheck, pubKeyCheck, err = geth.RecoverAccount(testConfig.Account1.Password, mnemonic)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("recover account failed (for non-cached account): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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-03 14:24:48 +00:00
|
|
|
_, key, err = keyStore.AccountDecryptedKey(account, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("can not obtain decrypted account key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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-03-14 22:57:07 +00:00
|
|
|
addressCheck, pubKeyCheck, err = geth.RecoverAccount(testConfig.Account1.Password, mnemonic)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("recover account failed (for non-cached account): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
2016-12-07 21:07:08 +00:00
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
2016-09-11 11:44:14 +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-09-11 11:44:14 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(addressCheck, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKeyCheck) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountSelect(t *testing.T) {
|
|
|
|
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test to see if the account was injected in whisper
|
2016-12-07 21:07:08 +00:00
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-03-14 22:57:07 +00:00
|
|
|
address1, pubKey1, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Logf("Account created: {address: %s, key: %s}", address1, pubKey1)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
address2, pubKey2, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Test failed: could not create account")
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 01:35:58 +00:00
|
|
|
t.Logf("Account created: {address: %s, key: %s}", address2, pubKey2)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// try selecting with wrong password
|
|
|
|
err = geth.SelectAccount(address1, "wrongPassword")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("select account is expected to throw error: wrong password used")
|
|
|
|
return
|
|
|
|
}
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address1, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey1) {
|
2016-09-11 11:44:14 +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-09-11 11:44:14 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address2, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey2) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Error("identity should be removed, but it is still present in whisper")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
func TestAccountsLogout(t *testing.T) {
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an account
|
2017-03-14 22:57:07 +00:00
|
|
|
address, pubKey, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that identity doesn't exist (yet) in Whisper
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// select/login
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Error("identity not injected into whisper")
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = geth.Logout(); err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Errorf("cannot logout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, logout and check if identity is removed indeed
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey) {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Error("identity not cleared from whisper")
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 18:42:55 +00:00
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
func TestAccountsSelectedAccountOnNodeRestart(t *testing.T) {
|
2017-01-24 18:42:55 +00:00
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2017-01-24 18:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we need to make sure that selected account is injected as identity into Whisper
|
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create test accounts
|
2017-03-14 22:57:07 +00:00
|
|
|
address1, pubKey1, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("account1 created: {address: %s, key: %s}", address1, pubKey1)
|
2017-03-14 22:57:07 +00:00
|
|
|
address2, pubKey2, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("account2 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) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that no account is selected by default
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount != nil {
|
|
|
|
t.Error("account selected, but should not be")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// select account
|
|
|
|
err = geth.SelectAccount(address1, "wrongPassword")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("select account is expected to throw error: wrong password used")
|
|
|
|
return
|
|
|
|
}
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address1, testConfig.Account1.Password)
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Error("identity already present in whisper")
|
|
|
|
}
|
2017-03-14 22:57:07 +00:00
|
|
|
err = geth.SelectAccount(address2, testConfig.Account1.Password)
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test failed: could not select account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey2) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Error("identity should be removed, but it is still present in whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop node (and all of its sub-protocols)
|
|
|
|
if err := geth.NodeManagerInstance().StopNode(); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2017-01-24 18:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that account is still selected
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount == nil {
|
|
|
|
t.Error("no selected account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount.Address.Hex() != "0x"+address2 {
|
|
|
|
t.Error("incorrect address selected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume node
|
|
|
|
if err := geth.NodeManagerInstance().ResumeNode(); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2017-01-24 18:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-check selected account (account2 MUST be selected)
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount == nil {
|
|
|
|
t.Error("no selected account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount.Address.Hex() != "0x"+address2 {
|
|
|
|
t.Error("incorrect address selected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that Whisper gets identity re-injected
|
|
|
|
whisperService, err = geth.NodeManagerInstance().WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if !whisperService.HasKeyPair(pubKey2) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Errorf("identity not injected into whisper: %v", err)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if whisperService.HasKeyPair(pubKey1) {
|
2017-01-24 18:42:55 +00:00
|
|
|
t.Error("identity should not be present, but it is still present in whisper")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
func TestAccountsNodeRestartWithNoSelectedAccount(t *testing.T) {
|
2017-01-24 18:42:55 +00:00
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2017-01-24 18:42:55 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = geth.Logout(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-24 18:42:55 +00:00
|
|
|
|
|
|
|
// we need to make sure that selected account is injected as identity into Whisper
|
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create test accounts
|
2017-03-14 22:57:07 +00:00
|
|
|
address1, pubKey1, _, err := geth.CreateAccount(testConfig.Account1.Password)
|
2017-01-24 18:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not create account: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("account1 created: {address: %s, key: %s}", address1, pubKey1)
|
|
|
|
|
|
|
|
// make sure that identity is not present
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that no account is selected
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount != nil {
|
|
|
|
t.Error("account selected, but should not be")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop node (and all of its sub-protocols)
|
|
|
|
if err := geth.NodeManagerInstance().StopNode(); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2017-01-24 18:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that no account is selected
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount != nil {
|
|
|
|
t.Error("account selected, but should not be")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume node
|
|
|
|
if err := geth.NodeManagerInstance().ResumeNode(); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Fatal(err)
|
2017-01-24 18:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that no account is selected
|
|
|
|
if geth.NodeManagerInstance().SelectedAccount != nil {
|
|
|
|
t.Error("account selected, but should not be")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that Whisper gets identity re-injected
|
|
|
|
whisperService, err = geth.NodeManagerInstance().WhisperService()
|
|
|
|
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.Error("identity should not be present, but it is present in whisper")
|
|
|
|
}
|
|
|
|
}
|