status-go/account/accounts_test.go

447 lines
14 KiB
Go
Raw Normal View History

2017-12-19 15:45:30 +00:00
package account
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
2017-12-19 17:20:53 +00:00
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/golang/mock/gomock"
. "github.com/status-im/status-go/t/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2017-12-19 13:07:31 +00:00
"github.com/stretchr/testify/suite"
)
func TestVerifyAccountPassword(t *testing.T) {
2017-12-19 15:45:30 +00:00
accManager := NewManager(nil)
keyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts")
require.NoError(t, err)
defer os.RemoveAll(keyStoreDir) //nolint: errcheck
emptyKeyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts_empty")
require.NoError(t, err)
defer os.RemoveAll(emptyKeyStoreDir) //nolint: errcheck
// import account keys
require.NoError(t, ImportTestAccount(keyStoreDir, GetAccount1PKFile()))
require.NoError(t, ImportTestAccount(keyStoreDir, GetAccount2PKFile()))
account1Address := gethcommon.BytesToAddress(gethcommon.FromHex(TestConfig.Account1.WalletAddress))
testCases := []struct {
name string
keyPath string
address string
password string
expectedError error
}{
{
"correct address, correct password (decrypt should succeed)",
keyStoreDir,
TestConfig.Account1.WalletAddress,
TestConfig.Account1.Password,
nil,
},
{
"correct address, correct password, non-existent key store",
filepath.Join(keyStoreDir, "non-existent-folder"),
TestConfig.Account1.WalletAddress,
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,
TestConfig.Account1.WalletAddress,
TestConfig.Account1.Password,
2017-10-10 09:38:49 +00:00
fmt.Errorf("cannot locate account for address: %s", account1Address.Hex()),
},
{
"wrong address, correct password",
keyStoreDir,
"0x79791d3e8f2daa1f7fec29649d152c0ada3cc535",
TestConfig.Account1.Password,
2017-10-10 09:38:49 +00:00
fmt.Errorf("cannot locate account for address: %s", "0x79791d3E8F2dAa1F7FeC29649d152c0aDA3cc535"),
},
{
"correct address, wrong password",
keyStoreDir,
TestConfig.Account1.WalletAddress,
"wrong password", // wrong password
errors.New("could not decrypt key with given passphrase"),
},
}
for _, testCase := range testCases {
2017-12-19 15:45:30 +00:00
accountKey, err := accManager.VerifyAccountPassword(testCase.keyPath, testCase.address, testCase.password)
if !reflect.DeepEqual(err, testCase.expectedError) {
require.FailNow(t, fmt.Sprintf("unexpected error: expected \n'%v', got \n'%v'", testCase.expectedError, err))
}
if err == nil {
if accountKey == nil {
require.Fail(t, "no error reported, but account key is missing")
}
accountAddress := gethcommon.BytesToAddress(gethcommon.FromHex(testCase.address))
if accountKey.Address != accountAddress {
require.Fail(t, "account mismatch: have %s, want %s", accountKey.Address.Hex(), accountAddress.Hex())
}
}
}
}
2017-10-10 09:38:49 +00:00
// TestVerifyAccountPasswordWithAccountBeforeEIP55 verifies if VerifyAccountPassword
// can handle accounts before introduction of EIP55.
func TestVerifyAccountPasswordWithAccountBeforeEIP55(t *testing.T) {
2017-10-10 09:38:49 +00:00
keyStoreDir, err := ioutil.TempDir("", "status-accounts-test")
require.NoError(t, err)
defer os.RemoveAll(keyStoreDir) //nolint: errcheck
2017-10-10 09:38:49 +00:00
// Import keys and make sure one was created before EIP55 introduction.
err = ImportTestAccount(keyStoreDir, "test-account3-before-eip55.pk")
require.NoError(t, err)
2017-10-10 09:38:49 +00:00
2017-12-19 15:45:30 +00:00
accManager := NewManager(nil)
2017-10-10 09:38:49 +00:00
address := gethcommon.HexToAddress(TestConfig.Account3.WalletAddress)
2017-12-19 15:45:30 +00:00
_, err = accManager.VerifyAccountPassword(keyStoreDir, address.Hex(), TestConfig.Account3.Password)
require.NoError(t, err)
2017-10-10 09:38:49 +00:00
}
var errKeyStore = errors.New("Can't return a key store")
2017-12-19 16:59:43 +00:00
2017-12-19 13:07:31 +00:00
func TestManagerTestSuite(t *testing.T) {
2018-03-27 16:30:37 +00:00
gethServiceProvider := newMockGethServiceProvider(t)
accManager := NewManager(gethServiceProvider)
2017-12-19 13:07:31 +00:00
keyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts")
require.NoError(t, err)
2017-12-19 13:07:31 +00:00
keyStore := keystore.NewKeyStore(keyStoreDir, keystore.LightScryptN, keystore.LightScryptP)
defer os.RemoveAll(keyStoreDir) //nolint: errcheck
2017-12-14 13:48:01 +00:00
testPassword := "test-password"
// Initial test - create test account
2018-03-27 16:30:37 +00:00
gethServiceProvider.EXPECT().AccountKeyStore().Return(keyStore, nil)
accountInfo, mnemonic, err := accManager.CreateAccount(testPassword)
require.NoError(t, err)
require.NotEmpty(t, accountInfo.WalletAddress)
require.NotEmpty(t, accountInfo.WalletPubKey)
require.NotEmpty(t, accountInfo.ChatAddress)
require.NotEmpty(t, accountInfo.ChatPubKey)
2017-12-26 13:32:41 +00:00
require.NotEmpty(t, mnemonic)
// Before the complete decoupling of the keys, wallet and chat keys are the same
assert.Equal(t, accountInfo.WalletAddress, accountInfo.ChatAddress)
assert.Equal(t, accountInfo.WalletPubKey, accountInfo.ChatPubKey)
s := &ManagerTestSuite{
testAccount: testAccount{
"test-password",
accountInfo.WalletAddress,
accountInfo.WalletPubKey,
accountInfo.ChatAddress,
accountInfo.ChatPubKey,
mnemonic,
},
2018-03-27 16:30:37 +00:00
gethServiceProvider: gethServiceProvider,
accManager: accManager,
keyStore: keyStore,
gethAccManager: accounts.NewManager(),
}
suite.Run(t, s)
2017-12-19 13:07:31 +00:00
}
2018-03-27 16:30:37 +00:00
func newMockGethServiceProvider(t *testing.T) *MockGethServiceProvider {
2017-12-19 15:45:30 +00:00
ctrl := gomock.NewController(t)
2018-03-27 16:30:37 +00:00
return NewMockGethServiceProvider(ctrl)
2017-12-19 15:45:30 +00:00
}
2017-12-19 13:07:31 +00:00
type ManagerTestSuite struct {
suite.Suite
testAccount
2018-03-27 16:30:37 +00:00
gethServiceProvider *MockGethServiceProvider
accManager *Manager
keyStore *keystore.KeyStore
gethAccManager *accounts.Manager
2017-12-19 13:07:31 +00:00
}
type testAccount struct {
password string
walletAddress string
walletPubKey string
chatAddress string
chatPubKey string
mnemonic string
}
2017-12-19 15:45:30 +00:00
// reinitMock is for reassigning a new mock node manager to account manager.
// Stating the amount of times for mock calls kills the flexibility for
// development so this is a good workaround to use with EXPECT().Func().AnyTimes()
2017-12-19 15:45:30 +00:00
func (s *ManagerTestSuite) reinitMock() {
2018-03-27 16:30:37 +00:00
s.gethServiceProvider = newMockGethServiceProvider(s.T())
s.accManager.geth = s.gethServiceProvider
2017-12-19 15:45:30 +00:00
}
// SetupTest is used here for reinitializing the mock before every
// test function to avoid faulty execution.
func (s *ManagerTestSuite) SetupTest() {
2017-12-19 15:45:30 +00:00
s.reinitMock()
}
func (s *ManagerTestSuite) TestCreateAccount() {
2017-12-14 13:50:12 +00:00
// Don't fail on empty password
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(s.keyStore, nil)
_, _, err := s.accManager.CreateAccount(s.password)
2017-12-19 13:07:31 +00:00
s.NoError(err)
2017-12-14 13:50:12 +00:00
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(nil, errKeyStore)
_, _, err = s.accManager.CreateAccount(s.password)
s.Equal(errKeyStore, err)
2017-12-28 13:40:56 +00:00
}
func (s *ManagerTestSuite) TestRecoverAccount() {
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(s.keyStore, nil)
accountInfo, err := s.accManager.RecoverAccount(s.password, s.mnemonic)
2017-12-19 13:07:31 +00:00
s.NoError(err)
s.Equal(s.walletAddress, accountInfo.WalletAddress)
s.Equal(s.walletPubKey, accountInfo.WalletPubKey)
s.Equal(s.chatAddress, accountInfo.ChatAddress)
s.Equal(s.chatPubKey, accountInfo.ChatPubKey)
2017-12-14 13:48:01 +00:00
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(nil, errKeyStore)
_, err = s.accManager.RecoverAccount(s.password, s.mnemonic)
s.Equal(errKeyStore, err)
}
2017-12-18 14:08:31 +00:00
func (s *ManagerTestSuite) TestOnboarding() {
// try to choose an account before starting onboarding
_, _, err := s.accManager.ImportOnboardingAccount("test-id", "test-password")
s.Equal(ErrOnboardingNotStarted, err)
// generates 5 random accounts
count := 5
accounts, err := s.accManager.StartOnboarding(count, 24)
s.Require().NoError(err)
s.Equal(count, len(accounts))
// try to choose an account with an undefined id
_, _, err = s.accManager.ImportOnboardingAccount("test-id", "test-password")
s.Equal(ErrOnboardingAccountNotFound, err)
// choose one account and encrypt it with password
password := "test-onboarding-account"
account := accounts[0]
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(s.keyStore, nil)
info, mnemonic, err := s.accManager.ImportOnboardingAccount(account.ID, password)
s.Require().NoError(err)
s.Equal(account.Info, info)
s.Equal(account.mnemonic, mnemonic)
s.Nil(s.accManager.onboarding)
// try to decrypt it with password to check if it's been imported correctly
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(s.keyStore, nil)
decAccount, _, err := s.accManager.AddressToDecryptedAccount(info.WalletAddress, password)
s.Require().NoError(err)
s.Equal(info.WalletAddress, decAccount.Address.Hex())
// try resetting onboarding
_, err = s.accManager.StartOnboarding(count, 24)
s.Require().NoError(err)
s.NotNil(s.accManager.onboarding)
s.accManager.RemoveOnboarding()
s.Nil(s.accManager.onboarding)
}
2017-12-19 13:07:31 +00:00
func (s *ManagerTestSuite) TestSelectAccount() {
2017-12-18 14:08:31 +00:00
testCases := []struct {
name string
accountKeyStoreReturn []interface{}
walletAddress string
chatAddress string
2017-12-18 14:08:31 +00:00
password string
expectedError error
2017-12-18 14:08:31 +00:00
}{
{
"success",
2017-12-19 15:45:30 +00:00
[]interface{}{s.keyStore, nil},
s.walletAddress,
s.chatAddress,
2017-12-19 15:45:30 +00:00
s.password,
nil,
2017-12-18 14:08:31 +00:00
},
{
"fail_keyStore",
[]interface{}{nil, errKeyStore},
s.walletAddress,
s.chatAddress,
2017-12-19 15:45:30 +00:00
s.password,
errKeyStore,
2017-12-18 14:08:31 +00:00
},
{
"fail_wrongChatAddress",
[]interface{}{s.keyStore, nil},
s.walletAddress,
"0x0000000000000000000000000000000000000001",
2017-12-19 15:45:30 +00:00
s.password,
errors.New("cannot retrieve a valid key for a given account: no key for given address or file"),
2017-12-18 14:08:31 +00:00
},
{
"fail_wrongPassword",
2017-12-19 15:45:30 +00:00
[]interface{}{s.keyStore, nil},
s.walletAddress,
s.chatAddress,
2017-12-18 14:08:31 +00:00
"wrong-password",
errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase"),
2017-12-18 14:08:31 +00:00
},
}
for _, testCase := range testCases {
2017-12-19 13:07:31 +00:00
s.T().Run(testCase.name, func(t *testing.T) {
2017-12-19 15:45:30 +00:00
s.reinitMock()
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(testCase.accountKeyStoreReturn...).AnyTimes()
loginParams := LoginParams{
ChatAddress: common.HexToAddress(testCase.chatAddress),
MainAccount: common.HexToAddress(testCase.walletAddress),
Password: testCase.password,
}
err := s.accManager.SelectAccount(loginParams)
s.Equal(testCase.expectedError, err)
selectedMainAccountAddress, walletErr := s.accManager.MainAccountAddress()
selectedChatAccount, chatErr := s.accManager.SelectedChatAccount()
if testCase.expectedError == nil {
s.Equal(testCase.walletAddress, selectedMainAccountAddress.String())
s.Equal(testCase.chatAddress, crypto.PubkeyToAddress(selectedChatAccount.AccountKey.PrivateKey.PublicKey).Hex())
s.NoError(walletErr)
s.NoError(chatErr)
} else {
s.Equal(common.Address{}, selectedMainAccountAddress)
s.Nil(selectedChatAccount)
s.Equal(walletErr, ErrNoAccountSelected)
s.Equal(chatErr, ErrNoAccountSelected)
}
s.accManager.Logout()
2017-12-19 15:45:30 +00:00
})
}
}
func (s *ManagerTestSuite) TestSetChatAccount() {
s.accManager.Logout()
privKey, err := crypto.GenerateKey()
s.Require().NoError(err)
address := crypto.PubkeyToAddress(privKey.PublicKey)
s.accManager.SetChatAccount(privKey)
selectedChatAccount, err := s.accManager.SelectedChatAccount()
s.Require().NoError(err)
s.Require().NotNil(selectedChatAccount)
s.Equal(privKey, selectedChatAccount.AccountKey.PrivateKey)
s.Equal(address, selectedChatAccount.Address)
selectedMainAccountAddress, err := s.accManager.MainAccountAddress()
s.Error(err)
s.Equal(common.Address{}, selectedMainAccountAddress)
2017-12-18 14:08:31 +00:00
}
2017-12-19 16:11:46 +00:00
2017-12-19 16:59:43 +00:00
func (s *ManagerTestSuite) TestLogout() {
s.accManager.Logout()
s.Equal(common.Address{}, s.accManager.mainAccountAddress)
s.Nil(s.accManager.selectedChatAccount)
s.Len(s.accManager.watchAddresses, 0)
2017-12-19 16:59:43 +00:00
}
2017-12-19 17:20:53 +00:00
// TestAccounts tests cases for (*Manager).Accounts.
2017-12-19 17:20:53 +00:00
func (s *ManagerTestSuite) TestAccounts() {
// Select the test account
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(s.keyStore, nil).AnyTimes()
loginParams := LoginParams{
MainAccount: common.HexToAddress(s.walletAddress),
ChatAddress: common.HexToAddress(s.chatAddress),
Password: s.password,
}
err := s.accManager.SelectAccount(loginParams)
2017-12-19 17:20:53 +00:00
s.NoError(err)
// Success
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountManager().Return(s.gethAccManager, nil)
2017-12-19 17:20:53 +00:00
accs, err := s.accManager.Accounts()
s.NoError(err)
s.NotNil(accs)
// Selected main account address is zero address but doesn't fail
s.accManager.mainAccountAddress = common.Address{}
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountManager().Return(s.gethAccManager, nil)
2017-12-19 17:20:53 +00:00
accs, err = s.accManager.Accounts()
s.NoError(err)
s.NotNil(accs)
}
2017-12-19 17:32:10 +00:00
func (s *ManagerTestSuite) TestAddressToDecryptedAccount() {
testCases := []struct {
name string
accountKeyStoreReturn []interface{}
walletAddress string
2017-12-19 17:32:10 +00:00
password string
expectedError error
2017-12-19 17:32:10 +00:00
}{
{
"success",
[]interface{}{s.keyStore, nil},
s.walletAddress,
2017-12-19 17:32:10 +00:00
s.password,
nil,
2017-12-19 17:32:10 +00:00
},
{
"fail_keyStore",
[]interface{}{nil, errKeyStore},
s.walletAddress,
2017-12-19 17:32:10 +00:00
s.password,
errKeyStore,
2017-12-19 17:32:10 +00:00
},
{
"fail_wrongWalletAddress",
2017-12-19 17:32:10 +00:00
[]interface{}{s.keyStore, nil},
"wrong-wallet-address",
2017-12-19 17:32:10 +00:00
s.password,
ErrAddressToAccountMappingFailure,
2017-12-19 17:32:10 +00:00
},
{
"fail_wrongPassword",
[]interface{}{s.keyStore, nil},
s.walletAddress,
2017-12-19 17:32:10 +00:00
"wrong-password",
errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase"),
2017-12-19 17:32:10 +00:00
},
}
for _, testCase := range testCases {
s.T().Run(testCase.name, func(t *testing.T) {
s.reinitMock()
2018-03-27 16:30:37 +00:00
s.gethServiceProvider.EXPECT().AccountKeyStore().Return(testCase.accountKeyStoreReturn...).AnyTimes()
acc, key, err := s.accManager.AddressToDecryptedAccount(testCase.walletAddress, testCase.password)
if testCase.expectedError != nil {
s.Equal(testCase.expectedError, err)
2017-12-19 17:32:10 +00:00
} else {
s.NoError(err)
s.NotNil(acc)
s.NotNil(key)
s.Equal(acc.Address, key.Address)
2017-12-19 17:32:10 +00:00
}
})
}
}