2017-09-26 13:44:26 +00:00
|
|
|
package account_test
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
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-10-11 14:20:51 +00:00
|
|
|
. "github.com/status-im/status-go/testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func TestVerifyAccountPassword(t *testing.T) {
|
|
|
|
acctManager := account.NewManager(nil)
|
2017-05-16 12:09:52 +00:00
|
|
|
keyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts")
|
2017-10-11 14:20:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(keyStoreDir)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
emptyKeyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts_empty")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(emptyKeyStoreDir)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// import account keys
|
2017-10-11 14:20:51 +00:00
|
|
|
require.NoError(t, common.ImportTestAccount(keyStoreDir, "test-account1.pk"))
|
|
|
|
require.NoError(t, common.ImportTestAccount(keyStoreDir, "test-account2.pk"))
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
account1Address := gethcommon.BytesToAddress(gethcommon.FromHex(TestConfig.Account1.Address))
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
keyPath string
|
|
|
|
address string
|
|
|
|
password string
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"correct address, correct password (decrypt should succeed)",
|
|
|
|
keyStoreDir,
|
|
|
|
TestConfig.Account1.Address,
|
|
|
|
TestConfig.Account1.Password,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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,
|
|
|
|
TestConfig.Account1.Address,
|
|
|
|
TestConfig.Account1.Password,
|
2017-10-10 09:38:49 +00:00
|
|
|
fmt.Errorf("cannot locate account for address: %s", account1Address.Hex()),
|
2017-05-16 12:09:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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"),
|
2017-05-16 12:09:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"correct address, wrong password",
|
|
|
|
keyStoreDir,
|
|
|
|
TestConfig.Account1.Address,
|
|
|
|
"wrong password", // wrong password
|
|
|
|
errors.New("could not decrypt key with given passphrase"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2017-10-11 14:20:51 +00:00
|
|
|
accountKey, err := acctManager.VerifyAccountPassword(testCase.keyPath, testCase.address, testCase.password)
|
2017-05-16 12:09:52 +00:00
|
|
|
if !reflect.DeepEqual(err, testCase.expectedError) {
|
2017-10-11 14:20:51 +00:00
|
|
|
require.FailNow(t, fmt.Sprintf("unexpected error: expected \n'%v', got \n'%v'", testCase.expectedError, err))
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if accountKey == nil {
|
2017-10-11 14:20:51 +00:00
|
|
|
require.Fail(t, "no error reported, but account key is missing")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
accountAddress := gethcommon.BytesToAddress(gethcommon.FromHex(testCase.address))
|
|
|
|
if accountKey.Address != accountAddress {
|
2017-10-11 14:20:51 +00:00
|
|
|
require.Fail(t, "account mismatch: have %s, want %s", accountKey.Address.Hex(), accountAddress.Hex())
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 09:38:49 +00:00
|
|
|
|
|
|
|
// TestVerifyAccountPasswordWithAccountBeforeEIP55 verifies if VerifyAccountPassword
|
|
|
|
// can handle accounts before introduction of EIP55.
|
2017-10-11 14:20:51 +00:00
|
|
|
func TestVerifyAccountPasswordWithAccountBeforeEIP55(t *testing.T) {
|
2017-10-10 09:38:49 +00:00
|
|
|
keyStoreDir, err := ioutil.TempDir("", "status-accounts-test")
|
2017-10-11 14:20:51 +00:00
|
|
|
require.NoError(t, err)
|
2017-10-10 09:38:49 +00:00
|
|
|
defer os.RemoveAll(keyStoreDir)
|
|
|
|
|
|
|
|
// Import keys and make sure one was created before EIP55 introduction.
|
|
|
|
err = common.ImportTestAccount(keyStoreDir, "test-account1-before-eip55.pk")
|
2017-10-11 14:20:51 +00:00
|
|
|
require.NoError(t, err)
|
2017-10-10 09:38:49 +00:00
|
|
|
|
|
|
|
acctManager := account.NewManager(nil)
|
|
|
|
|
|
|
|
address := gethcommon.HexToAddress(TestConfig.Account1.Address)
|
|
|
|
_, err = acctManager.VerifyAccountPassword(keyStoreDir, address.Hex(), TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
require.NoError(t, err)
|
2017-10-10 09:38:49 +00:00
|
|
|
}
|