2023-05-09 18:48:56 +00:00
|
|
|
package keycards
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
)
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func setupTestDB(t *testing.T) (*Keycards, func()) {
|
2023-02-20 09:52:14 +00:00
|
|
|
db, stop, err := appdatabase.SetupTestSQLDB("settings-tests-")
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, stop())
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
d := NewKeycards(db)
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
return d, func() {
|
|
|
|
require.NoError(t, stop())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func TestKeycards(t *testing.T) {
|
2023-02-20 09:52:14 +00:00
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
keycardUID := "00000000000000000000000000000000"
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard1 := Keycard{
|
2023-02-20 09:52:14 +00:00
|
|
|
KeycardUID: "00000000000000000000000000000001",
|
|
|
|
KeycardName: "Card01",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}, {0x03}, {0x04}},
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000001",
|
2023-02-21 09:50:48 +00:00
|
|
|
LastUpdateClock: 100,
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard2 := Keycard{
|
2023-02-20 09:52:14 +00:00
|
|
|
KeycardUID: "00000000000000000000000000000002",
|
|
|
|
KeycardName: "Card02",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}},
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000002",
|
2023-02-21 09:50:48 +00:00
|
|
|
LastUpdateClock: 200,
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard3 := Keycard{
|
2023-02-20 09:52:14 +00:00
|
|
|
KeycardUID: "00000000000000000000000000000003",
|
|
|
|
KeycardName: "Card02 Copy",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}},
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000002",
|
2023-02-21 09:50:48 +00:00
|
|
|
LastUpdateClock: 300,
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard4 := Keycard{
|
2023-02-20 09:52:14 +00:00
|
|
|
KeycardUID: "00000000000000000000000000000004",
|
|
|
|
KeycardName: "Card04",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: []types.Address{{0x01}, {0x02}, {0x03}},
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000004",
|
2023-02-21 09:50:48 +00:00
|
|
|
LastUpdateClock: 400,
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test adding key pairs
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err := db.AddKeycardOrAddAccountsIfKeycardIsAdded(keycard1)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-21 12:35:26 +00:00
|
|
|
require.Equal(t, true, addedKc)
|
|
|
|
require.Equal(t, false, addedAccs)
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err = db.AddKeycardOrAddAccountsIfKeycardIsAdded(keycard2)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-21 12:35:26 +00:00
|
|
|
require.Equal(t, true, addedKc)
|
|
|
|
require.Equal(t, false, addedAccs)
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err = db.AddKeycardOrAddAccountsIfKeycardIsAdded(keycard3)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-21 12:35:26 +00:00
|
|
|
require.Equal(t, true, addedKc)
|
|
|
|
require.Equal(t, false, addedAccs)
|
2023-02-21 09:50:48 +00:00
|
|
|
// this should be added
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err = db.AddKeycardOrAddAccountsIfKeycardIsAdded(Keycard{
|
|
|
|
KeycardUID: keycard3.KeycardUID,
|
2023-02-20 22:30:45 +00:00
|
|
|
AccountsAddresses: []types.Address{{0x03}},
|
2023-05-09 18:48:56 +00:00
|
|
|
LastUpdateClock: keycard3.LastUpdateClock + 1,
|
2023-02-20 22:30:45 +00:00
|
|
|
})
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-21 12:35:26 +00:00
|
|
|
require.Equal(t, false, addedKc)
|
|
|
|
require.Equal(t, true, addedAccs)
|
2023-02-21 09:50:48 +00:00
|
|
|
// this should not be added as it has clock value less than last updated clock value
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err = db.AddKeycardOrAddAccountsIfKeycardIsAdded(Keycard{
|
|
|
|
KeycardUID: keycard3.KeycardUID,
|
2023-02-21 09:50:48 +00:00
|
|
|
AccountsAddresses: []types.Address{{0x04}},
|
2023-05-09 18:48:56 +00:00
|
|
|
LastUpdateClock: keycard3.LastUpdateClock,
|
2023-02-21 09:50:48 +00:00
|
|
|
})
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-21 12:35:26 +00:00
|
|
|
require.Equal(t, false, addedKc)
|
|
|
|
require.Equal(t, false, addedAccs)
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err = db.AddKeycardOrAddAccountsIfKeycardIsAdded(keycard4)
|
2023-02-21 09:50:48 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-21 12:35:26 +00:00
|
|
|
require.Equal(t, true, addedKc)
|
|
|
|
require.Equal(t, false, addedAccs)
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
// Test reading migrated key pairs
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err := db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(rows))
|
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeyUID == keycard1.KeyUID {
|
|
|
|
require.Equal(t, keycard1.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard1.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard1.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard1.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
} else if kp.KeyUID == keycard2.KeyUID { // keycard 2 and 3, cause 3 is a copy of 2
|
|
|
|
require.Equal(t, keycard2.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard2.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard2.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard2.AccountsAddresses)+1, len(kp.AccountsAddresses)) // Add 1, cause one account is additionally added for the same keycard.
|
2023-02-20 09:52:14 +00:00
|
|
|
} else {
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Equal(t, keycard4.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard4.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard4.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard4.AccountsAddresses), len(kp.AccountsAddresses))
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetKeycardByKeyUID(keycard1.KeyUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rows))
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Equal(t, keycard1.KeyUID, rows[0].KeyUID)
|
|
|
|
require.Equal(t, keycard1.KeycardUID, rows[0].KeycardUID)
|
|
|
|
require.Equal(t, keycard1.KeycardName, rows[0].KeycardName)
|
|
|
|
require.Equal(t, keycard1.KeycardLocked, rows[0].KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard1.AccountsAddresses), len(rows[0].AccountsAddresses))
|
|
|
|
require.Equal(t, keycard1.LastUpdateClock, rows[0].LastUpdateClock)
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
rows, err = db.GetAllKnownKeycards()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 4, len(rows))
|
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeycardUID == keycard1.KeycardUID {
|
|
|
|
require.Equal(t, keycard1.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard1.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard1.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard1.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
require.Equal(t, keycard1.LastUpdateClock, kp.LastUpdateClock)
|
|
|
|
} else if kp.KeycardUID == keycard2.KeycardUID {
|
|
|
|
require.Equal(t, keycard2.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard2.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard2.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard2.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
require.Equal(t, keycard2.LastUpdateClock, kp.LastUpdateClock)
|
|
|
|
} else if kp.KeycardUID == keycard3.KeycardUID {
|
|
|
|
require.Equal(t, keycard3.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard3.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard3.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard3.AccountsAddresses)+1, len(kp.AccountsAddresses)) // Add 1, cause one account is additionally added.
|
|
|
|
require.Equal(t, keycard3.LastUpdateClock+1, kp.LastUpdateClock)
|
2023-02-20 09:52:14 +00:00
|
|
|
} else {
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Equal(t, keycard4.KeycardUID, kp.KeycardUID)
|
|
|
|
require.Equal(t, keycard4.KeycardName, kp.KeycardName)
|
|
|
|
require.Equal(t, keycard4.KeycardLocked, kp.KeycardLocked)
|
|
|
|
require.Equal(t, len(keycard4.AccountsAddresses), len(kp.AccountsAddresses))
|
|
|
|
require.Equal(t, keycard4.LastUpdateClock, kp.LastUpdateClock)
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test seting a new keycard name
|
2023-05-09 18:48:56 +00:00
|
|
|
err = db.SetKeycardName(keycard1.KeycardUID, "Card101", 1000)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
newKeycardName := ""
|
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeyUID == keycard1.KeyUID {
|
2023-02-20 09:52:14 +00:00
|
|
|
newKeycardName = kp.KeycardName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, "Card101", newKeycardName)
|
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
// Test seting a new keycard name with an old clock value
|
2023-05-09 18:48:56 +00:00
|
|
|
err = db.SetKeycardName(keycard1.KeycardUID, "Card102", 999) // clock is less than the last one
|
2023-02-21 09:50:48 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-21 09:50:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
newKeycardName = ""
|
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeyUID == keycard1.KeyUID {
|
2023-02-21 09:50:48 +00:00
|
|
|
newKeycardName = kp.KeycardName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, "Card101", newKeycardName)
|
|
|
|
|
2023-02-20 09:52:14 +00:00
|
|
|
// Test locking a keycard
|
2023-05-09 18:48:56 +00:00
|
|
|
err = db.KeycardLocked(keycard1.KeycardUID, 1001)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
locked := false
|
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeyUID == keycard1.KeyUID {
|
2023-02-20 09:52:14 +00:00
|
|
|
locked = kp.KeycardLocked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, true, locked)
|
|
|
|
|
|
|
|
// Test detleting accounts (addresses) for a certain keycard
|
|
|
|
const numOfAccountsToRemove = 2
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Greater(t, len(keycard1.AccountsAddresses), numOfAccountsToRemove)
|
|
|
|
accountsToRemove := keycard1.AccountsAddresses[:numOfAccountsToRemove]
|
|
|
|
err = db.RemoveMigratedAccountsForKeycard(keycard1.KeycardUID, accountsToRemove, 1002)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetKeycardByKeyUID(keycard1.KeyUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rows))
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Equal(t, len(keycard1.AccountsAddresses)-numOfAccountsToRemove, len(rows[0].AccountsAddresses))
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
// Test deleting accounts one by one, with the last deleted account keycard should be delete as well
|
2023-05-09 18:48:56 +00:00
|
|
|
for i, addr := range keycard4.AccountsAddresses {
|
|
|
|
err = db.RemoveMigratedAccountsForKeycard(keycard4.KeycardUID, []types.Address{addr}, 1003+uint64(i))
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
// Test if correct keycard is deleted
|
2023-05-09 18:48:56 +00:00
|
|
|
deletedKeycard4 := true
|
2023-02-20 09:52:14 +00:00
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeycardUID == keycard4.KeycardUID {
|
|
|
|
deletedKeycard4 = false
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Equal(t, true, deletedKeycard4)
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
// Test update keycard uid
|
2023-05-09 18:48:56 +00:00
|
|
|
err = db.UpdateKeycardUID(keycard1.KeycardUID, keycardUID, 1100)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test unlocking a locked keycard
|
2023-02-21 12:35:26 +00:00
|
|
|
err = db.KeycardUnlocked(keycardUID, 1101)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
locked = true
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeycardUID == keycardUID {
|
|
|
|
locked = kp.KeycardLocked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, false, locked)
|
|
|
|
|
|
|
|
// Test detleting a keycard
|
2023-02-21 12:35:26 +00:00
|
|
|
err = db.DeleteKeycard(keycardUID, 1102)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rows))
|
|
|
|
// Test if correct keycard is deleted
|
|
|
|
deletedKeyCard := true
|
|
|
|
for _, kp := range rows {
|
|
|
|
if kp.KeycardUID == keycardUID {
|
|
|
|
deletedKeyCard = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, true, deletedKeyCard)
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
// Test detleting a keycard
|
|
|
|
err = db.DeleteAllKeycardsWithKeyUID(keycard2.KeyUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-09 18:48:56 +00:00
|
|
|
rows, err = db.GetAllKnownKeycardsGroupedByKeyUID()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
// Test if correct keycard is deleted
|
2023-05-09 18:48:56 +00:00
|
|
|
deletedKeycard2And3 := true
|
2023-02-20 09:52:14 +00:00
|
|
|
for _, kp := range rows {
|
2023-05-09 18:48:56 +00:00
|
|
|
if kp.KeyUID == keycard2.KeyUID {
|
|
|
|
deletedKeycard2And3 = false
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Equal(t, true, deletedKeycard2And3)
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|