2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/errors"
|
2022-09-06 07:10:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/keypairs"
|
2019-12-27 09:58:25 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
2022-03-23 18:47:00 +00:00
|
|
|
db, stop, err := appdatabase.SetupTestSQLDB("settings-tests-")
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, stop())
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-27 09:58:25 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
d, err := NewDB(db)
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, stop())
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
2019-12-27 09:58:25 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return d, func() {
|
|
|
|
require.NoError(t, stop())
|
|
|
|
}
|
2019-08-08 07:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func TestSaveAccounts(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
description string
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts []*Account
|
2019-08-20 15:38:40 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
for _, tc := range []testCase{
|
|
|
|
{
|
|
|
|
description: "NoError",
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts: []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}},
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "UniqueChat",
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts: []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true},
|
|
|
|
{Address: types.Address{0x02}, Chat: true},
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
2022-03-23 18:47:00 +00:00
|
|
|
err: errors.ErrChatNotUnique,
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "UniqueWallet",
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts: []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}, Wallet: true},
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
2022-03-23 18:47:00 +00:00
|
|
|
err: errors.ErrWalletNotUnique,
|
2019-08-20 15:38:40 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
require.Equal(t, tc.err, db.SaveAccounts(tc.accounts))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateAccounts(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts := []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}},
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
accounts[0].Chat = false
|
|
|
|
accounts[1].Chat = true
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
rst, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, accounts, rst)
|
|
|
|
}
|
|
|
|
|
2019-12-16 15:23:36 +00:00
|
|
|
func TestDeleteAccount(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts := []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
2019-12-16 15:23:36 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
rst, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
2019-12-11 13:59:37 +00:00
|
|
|
require.NoError(t, db.DeleteAccount(types.Address{0x01}))
|
2019-12-16 15:23:36 +00:00
|
|
|
rst2, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst2))
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func TestGetAddresses(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts := []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}},
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
addresses, err := db.GetAddresses()
|
|
|
|
require.NoError(t, err)
|
2019-12-11 13:59:37 +00:00
|
|
|
require.Equal(t, []types.Address{{0x01}, {0x02}}, addresses)
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWalletAddress(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2019-12-11 13:59:37 +00:00
|
|
|
address := types.Address{0x01}
|
2019-08-20 15:38:40 +00:00
|
|
|
_, err := db.GetWalletAddress()
|
|
|
|
require.Equal(t, err, sql.ErrNoRows)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.NoError(t, db.SaveAccounts([]*Account{{Address: address, Wallet: true}}))
|
2019-08-20 15:38:40 +00:00
|
|
|
wallet, err := db.GetWalletAddress()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, address, wallet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetChatAddress(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2019-12-11 13:59:37 +00:00
|
|
|
address := types.Address{0x01}
|
2019-08-20 15:38:40 +00:00
|
|
|
_, err := db.GetChatAddress()
|
|
|
|
require.Equal(t, err, sql.ErrNoRows)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.NoError(t, db.SaveAccounts([]*Account{{Address: address, Chat: true}}))
|
2019-08-20 15:38:40 +00:00
|
|
|
chat, err := db.GetChatAddress()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, address, chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAccounts(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts := []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
|
|
|
{Address: types.Address{0x02}, PublicKey: types.HexBytes{0x01, 0x02}},
|
|
|
|
{Address: types.Address{0x03}, PublicKey: types.HexBytes{0x02, 0x03}},
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
rst, err := db.GetAccounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, accounts, rst)
|
|
|
|
}
|
2019-08-29 08:06:22 +00:00
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
func TestGetAccountByAddress(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
address := types.Address{0x01}
|
2022-05-18 10:42:51 +00:00
|
|
|
account := &Account{Address: address, Chat: true, Wallet: true}
|
|
|
|
dilute := []*Account{
|
2020-10-28 07:56:14 +00:00
|
|
|
{Address: types.Address{0x02}, PublicKey: types.HexBytes{0x01, 0x02}},
|
|
|
|
{Address: types.Address{0x03}, PublicKey: types.HexBytes{0x02, 0x03}},
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts := append(dilute, account)
|
|
|
|
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
rst, err := db.GetAccountByAddress(address)
|
|
|
|
require.NoError(t, err)
|
2022-05-18 10:42:51 +00:00
|
|
|
require.Equal(t, account, rst)
|
2020-10-28 07:56:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
func TestAddressExists(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts := []*Account{
|
2019-12-11 13:59:37 +00:00
|
|
|
{Address: types.Address{0x01}, Chat: true, Wallet: true},
|
2019-08-29 08:06:22 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, db.SaveAccounts(accounts))
|
|
|
|
|
|
|
|
exists, err := db.AddressExists(accounts[0].Address)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, exists)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddressDoesntExist(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2019-12-11 13:59:37 +00:00
|
|
|
exists, err := db.AddressExists(types.Address{1, 1, 1})
|
2019-08-29 08:06:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, exists)
|
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
|
|
|
|
func TestKeypairs(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2022-09-20 10:45:50 +00:00
|
|
|
keycardUID := "00000000000000000000000000000000"
|
2022-09-06 07:10:40 +00:00
|
|
|
keyPair1 := keypairs.KeyPair{
|
|
|
|
KeycardUID: "00000000000000000000000000000001",
|
|
|
|
KeycardName: "Card01",
|
|
|
|
KeycardLocked: false,
|
2023-01-06 09:57:22 +00:00
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}, {0x03}, {0x04}},
|
2022-09-06 07:10:40 +00:00
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
}
|
|
|
|
keyPair2 := keypairs.KeyPair{
|
|
|
|
KeycardUID: "00000000000000000000000000000002",
|
|
|
|
KeycardName: "Card02",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}},
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
}
|
2022-12-07 17:14:19 +00:00
|
|
|
keyPair3 := keypairs.KeyPair{
|
|
|
|
KeycardUID: "00000000000000000000000000000003",
|
|
|
|
KeycardName: "Card02 Copy",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}},
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
|
|
|
|
// Test adding key pairs
|
|
|
|
err := db.AddMigratedKeyPair(keyPair1.KeycardUID, keyPair1.KeycardName, keyPair1.KeyUID, keyPair1.AccountsAddresses)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = db.AddMigratedKeyPair(keyPair2.KeycardUID, keyPair2.KeycardName, keyPair2.KeyUID, keyPair2.AccountsAddresses)
|
|
|
|
require.NoError(t, err)
|
2022-12-07 17:14:19 +00:00
|
|
|
err = db.AddMigratedKeyPair(keyPair3.KeycardUID, keyPair3.KeycardName, keyPair3.KeyUID, keyPair3.AccountsAddresses)
|
|
|
|
require.NoError(t, err)
|
2022-09-06 07:10:40 +00:00
|
|
|
|
|
|
|
// Test reading migrated key pairs
|
|
|
|
rows, err := db.GetAllMigratedKeyPairs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(rows))
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeyUID == keyPair1.KeyUID {
|
|
|
|
require.Equal(t, keyPair1.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keyPair1.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keyPair1.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keyPair1.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, keyPair2.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keyPair2.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keyPair2.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keyPair2.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err = db.GetMigratedKeyPairByKeyUID(keyPair1.KeyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rows))
|
|
|
|
require.Equal(t, keyPair1.KeyUID, rows[0].KeyUID)
|
|
|
|
require.Equal(t, keyPair1.KeycardUID, rows[0].KeycardUID)
|
|
|
|
require.Equal(t, keyPair1.KeycardName, rows[0].KeycardName)
|
|
|
|
require.Equal(t, keyPair1.KeycardLocked, rows[0].KeycardLocked)
|
|
|
|
require.Equal(t, len(keyPair1.AccountsAddresses), len(rows[0].AccountsAddresses))
|
|
|
|
|
2022-12-07 17:14:19 +00:00
|
|
|
rows, err = db.GetAllKnownKeycards()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(rows))
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeycardUID == keyPair1.KeycardUID {
|
|
|
|
require.Equal(t, keyPair1.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keyPair1.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keyPair1.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keyPair1.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
} else if kp.KeycardUID == keyPair2.KeycardUID {
|
|
|
|
require.Equal(t, keyPair2.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keyPair2.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keyPair2.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keyPair2.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, keyPair3.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keyPair3.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keyPair3.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keyPair3.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
// Test seting a new keycard name
|
|
|
|
err = db.SetKeycardName(keyPair1.KeycardUID, "Card101")
|
|
|
|
require.NoError(t, err)
|
|
|
|
rows, err = db.GetAllMigratedKeyPairs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
newKeycardName := ""
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeyUID == keyPair1.KeyUID {
|
|
|
|
newKeycardName = kp.KeycardName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, "Card101", newKeycardName)
|
|
|
|
|
|
|
|
// Test locking a keycard
|
|
|
|
err = db.KeycardLocked(keyPair1.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rows, err = db.GetAllMigratedKeyPairs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
locked := false
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeyUID == keyPair1.KeyUID {
|
|
|
|
locked = kp.KeycardLocked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, true, locked)
|
|
|
|
|
2023-01-06 09:57:22 +00:00
|
|
|
// Test detleting accounts (addresses) for a certain keycard
|
|
|
|
const numOfAccountsToRemove = 2
|
|
|
|
require.Greater(t, len(keyPair1.AccountsAddresses), numOfAccountsToRemove)
|
|
|
|
accountsToRemove := keyPair1.AccountsAddresses[:numOfAccountsToRemove]
|
|
|
|
err = db.RemoveMigratedAccountsForKeycard(keyPair1.KeycardUID, accountsToRemove)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rows, err = db.GetMigratedKeyPairByKeyUID(keyPair1.KeyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rows))
|
|
|
|
require.Equal(t, len(keyPair1.AccountsAddresses)-numOfAccountsToRemove, len(rows[0].AccountsAddresses))
|
|
|
|
|
2022-09-20 10:45:50 +00:00
|
|
|
// Test update keycard uid
|
|
|
|
err = db.UpdateKeycardUID(keyPair1.KeycardUID, keycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
// Test unlocking a locked keycard
|
2022-09-20 10:45:50 +00:00
|
|
|
err = db.KeycardUnlocked(keycardUID)
|
2022-09-06 07:10:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rows, err = db.GetAllMigratedKeyPairs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
locked = true
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeyUID == keyPair1.KeyUID {
|
|
|
|
locked = kp.KeycardLocked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, false, locked)
|
|
|
|
|
|
|
|
// Test detleting a keycard
|
2022-09-20 10:45:50 +00:00
|
|
|
err = db.DeleteKeycard(keycardUID)
|
2022-09-06 07:10:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rows, err = db.GetAllMigratedKeyPairs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rows))
|
|
|
|
// Test if correct keycard is deleted
|
|
|
|
deletedKeyPair1 := true
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeyUID == keyPair1.KeyUID {
|
|
|
|
deletedKeyPair1 = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, true, deletedKeyPair1)
|
|
|
|
}
|