fix: use proper migrations for protocol's test database

- use `appdatabse.DbInitializer{}` in tests to ensure consistent migrations

- remove protocol's open database functions due to improper
  initialization caused by missing node config migration

- introduce `PushNotificationServerConfig` to resolve cyclic dependency
  issues
This commit is contained in:
Patryk Osmaczko 2023-10-02 11:28:42 +02:00 committed by osmaczko
parent ecc8b4cb55
commit 53423e58ba
36 changed files with 169 additions and 313 deletions

View File

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/status-im/status-go/services/ens" "github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/sqlite"
"github.com/imdario/mergo" "github.com/imdario/mergo"
@ -50,7 +51,6 @@ import (
"github.com/status-im/status-go/services/typeddata" "github.com/status-im/status-go/services/typeddata"
wcommon "github.com/status-im/status-go/services/wallet/common" wcommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/signal" "github.com/status-im/status-go/signal"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/transactions" "github.com/status-im/status-go/transactions"
"github.com/status-im/status-go/walletdatabase" "github.com/status-im/status-go/walletdatabase"
) )
@ -609,7 +609,7 @@ func (b *GethStatusBackend) loginAccount(request *requests.Login) error {
} }
if acc.KDFIterations == 0 { if acc.KDFIterations == 0 {
acc.KDFIterations = sqlite.ReducedKDFIterationsNumber acc.KDFIterations = dbsetup.ReducedKDFIterationsNumber
} }
err := b.ensureDBsOpened(acc, password) err := b.ensureDBsOpened(acc, password)
@ -867,7 +867,7 @@ func (b *GethStatusBackend) ExportUnencryptedDatabase(acc multiaccounts.Account,
return err return err
} }
err = dbsetup.DecryptDatabase(dbPath, directory, password, acc.KDFIterations) err = sqlite.DecryptDB(dbPath, directory, password, acc.KDFIterations)
if err != nil { if err != nil {
b.log.Error("failed to initialize db", "err", err) b.log.Error("failed to initialize db", "err", err)
return err return err
@ -887,7 +887,7 @@ func (b *GethStatusBackend) ImportUnencryptedDatabase(acc multiaccounts.Account,
return err return err
} }
err = dbsetup.EncryptDatabase(databasePath, path, password, acc.KDFIterations, signal.SendReEncryptionStarted, signal.SendReEncryptionFinished) err = sqlite.EncryptDB(databasePath, path, password, acc.KDFIterations, signal.SendReEncryptionStarted, signal.SendReEncryptionFinished)
if err != nil { if err != nil {
b.log.Error("failed to initialize db", "err", err) b.log.Error("failed to initialize db", "err", err)
return err return err
@ -990,7 +990,7 @@ func (b *GethStatusBackend) changeAppDBPassword(account *multiaccounts.Account,
} }
// Exporting database to a temporary file with a new password // Exporting database to a temporary file with a new password
err = dbsetup.ExportDB(dbPath, password, account.KDFIterations, tmpDbPath, newPassword, signal.SendReEncryptionStarted, signal.SendReEncryptionFinished) err = sqlite.ExportDB(dbPath, password, account.KDFIterations, tmpDbPath, newPassword, signal.SendReEncryptionStarted, signal.SendReEncryptionFinished)
if err != nil { if err != nil {
return err return err
} }
@ -1032,7 +1032,7 @@ func (b *GethStatusBackend) changeWalletDBPassword(account *multiaccounts.Accoun
} }
// Exporting database to a temporary file with a new password // Exporting database to a temporary file with a new password
err = dbsetup.ExportDB(dbPath, password, account.KDFIterations, tmpDbPath, newPassword, signal.SendReEncryptionStarted, signal.SendReEncryptionFinished) err = sqlite.ExportDB(dbPath, password, account.KDFIterations, tmpDbPath, newPassword, signal.SendReEncryptionStarted, signal.SendReEncryptionFinished)
if err != nil { if err != nil {
return err return err
} }
@ -1300,7 +1300,7 @@ func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizati
Name: request.DisplayName, Name: request.DisplayName,
CustomizationColor: multiacccommon.CustomizationColor(request.CustomizationColor), CustomizationColor: multiacccommon.CustomizationColor(request.CustomizationColor),
CustomizationColorClock: customizationColorClock, CustomizationColorClock: customizationColorClock,
KDFIterations: sqlite.ReducedKDFIterationsNumber, KDFIterations: dbsetup.ReducedKDFIterationsNumber,
} }
if request.ImagePath != "" { if request.ImagePath != "" {
iis, err := images.GenerateIdentityImages(request.ImagePath, 0, 0, 1000, 1000) iis, err := images.GenerateIdentityImages(request.ImagePath, 0, 0, 1000, 1000)

View File

@ -100,7 +100,7 @@ const (
func TestMigrateWalletJsonBlobs(t *testing.T) { func TestMigrateWalletJsonBlobs(t *testing.T) {
openDB := func() (*sql.DB, error) { openDB := func() (*sql.DB, error) {
return sqlite.OpenDB(sqlite.InMemoryPath, "1234567890", sqlite.ReducedKDFIterationsNumber) return sqlite.OpenDB(sqlite.InMemoryPath, "1234567890", dbsetup.ReducedKDFIterationsNumber)
} }
db, err := openDB() db, err := openDB()
require.NoError(t, err) require.NoError(t, err)

View File

@ -17,7 +17,6 @@ import (
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/nodecfg" "github.com/status-im/status-go/nodecfg"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/t/helpers" "github.com/status-im/status-go/t/helpers"
) )
@ -205,7 +204,7 @@ func randomNodeConfig() *params.NodeConfig {
}, },
RegisterTopics: randomTopicSlice(), RegisterTopics: randomTopicSlice(),
RequireTopics: randomTopicLimits(), RequireTopics: randomTopicLimits(),
PushNotificationServerConfig: pushnotificationserver.Config{ PushNotificationServerConfig: params.PushNotificationServerConfig{
Enabled: randomBool(), Enabled: randomBool(),
GorushURL: randomString(), GorushURL: randomString(),
Identity: privK, Identity: privK,

View File

@ -19,11 +19,11 @@ import (
"github.com/status-im/status-go/account/generator" "github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/api" "github.com/status-im/status-go/api"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/accounts" "github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings" "github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
@ -396,7 +396,7 @@ func ImportAccount(seedPhrase string, backend *api.GethStatusBackend) error {
account := multiaccounts.Account{ account := multiaccounts.Account{
KeyUID: generatedAccountInfo.KeyUID, KeyUID: generatedAccountInfo.KeyUID,
KDFIterations: sqlite.ReducedKDFIterationsNumber, KDFIterations: dbsetup.ReducedKDFIterationsNumber,
} }
settings, err := defaultSettings(generatedAccountInfo, derivedAddresses, &seedPhrase) settings, err := defaultSettings(generatedAccountInfo, derivedAddresses, &seedPhrase)
if err != nil { if err != nil {

View File

@ -20,6 +20,7 @@ import (
"github.com/status-im/status-go/account/generator" "github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/api" "github.com/status-im/status-go/api"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
@ -33,7 +34,6 @@ import (
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests" "github.com/status-im/status-go/protocol/requests"
wakuextn "github.com/status-im/status-go/services/wakuext" wakuextn "github.com/status-im/status-go/services/wakuext"
"github.com/status-im/status-go/sqlite"
) )
type testTimeSource struct{} type testTimeSource struct{}
@ -461,7 +461,7 @@ func ImportAccount(seedPhrase string, backend *api.GethStatusBackend) error {
account := multiaccounts.Account{ account := multiaccounts.Account{
KeyUID: generatedAccountInfo.KeyUID, KeyUID: generatedAccountInfo.KeyUID,
KDFIterations: sqlite.ReducedKDFIterationsNumber, KDFIterations: dbsetup.ReducedKDFIterationsNumber,
} }
settings, err := defaultSettings(generatedAccountInfo, derivedAddresses, &seedPhrase) settings, err := defaultSettings(generatedAccountInfo, derivedAddresses, &seedPhrase)
if err != nil { if err != nil {

View File

@ -18,6 +18,7 @@ import (
"github.com/status-im/status-go/account/generator" "github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/api" "github.com/status-im/status-go/api"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
@ -28,7 +29,6 @@ import (
"github.com/status-im/status-go/protocol" "github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/protocol/identity/alias" "github.com/status-im/status-go/protocol/identity/alias"
waku2extn "github.com/status-im/status-go/services/wakuv2ext" waku2extn "github.com/status-im/status-go/services/wakuv2ext"
"github.com/status-im/status-go/sqlite"
) )
const ( const (
@ -357,7 +357,7 @@ func ImportAccount(seedPhrase string, backend *api.GethStatusBackend) error {
account := multiaccounts.Account{ account := multiaccounts.Account{
KeyUID: generatedAccountInfo.KeyUID, KeyUID: generatedAccountInfo.KeyUID,
KDFIterations: sqlite.ReducedKDFIterationsNumber, KDFIterations: dbsetup.ReducedKDFIterationsNumber,
} }
settings, err := defaultSettings(generatedAccountInfo, derivedAddresses, &seedPhrase) settings, err := defaultSettings(generatedAccountInfo, derivedAddresses, &seedPhrase)
if err != nil { if err != nil {

View File

@ -22,6 +22,7 @@ import (
"github.com/status-im/status-go/api" "github.com/status-im/status-go/api"
"github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/common/dbsetup"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
@ -31,7 +32,7 @@ import (
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/profiling" "github.com/status-im/status-go/profiling"
"github.com/status-im/status-go/protocol" "github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/sqlite" "github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/walletdatabase" "github.com/status-im/status-go/walletdatabase"
) )
@ -208,21 +209,24 @@ func main() {
return return
} }
walletDB, err := walletdatabase.InitializeDB(config.DataDir+"/"+installationID.String()+"-wallet.db", "", sqlite.ReducedKDFIterationsNumber) walletDB, err := walletdatabase.InitializeDB(config.DataDir+"/"+installationID.String()+"-wallet.db", "", dbsetup.ReducedKDFIterationsNumber)
if err != nil { if err != nil {
logger.Error("failed to initialize app db", "error", err) logger.Error("failed to initialize app db", "error", err)
return return
} }
appDB, err := appdatabase.InitializeDB(config.DataDir+"/"+installationID.String()+".db", "", sqlite.ReducedKDFIterationsNumber) appDB, err := appdatabase.InitializeDB(config.DataDir+"/"+installationID.String()+".db", "", dbsetup.ReducedKDFIterationsNumber)
if err != nil { if err != nil {
logger.Error("failed to initialize app db", "error", err) logger.Error("failed to initialize app db", "error", err)
return return
} }
options := []protocol.Option{ options := []protocol.Option{
protocol.WithPushNotifications(), protocol.WithPushNotifications(),
protocol.WithPushNotificationServerConfig(&config.PushNotificationServerConfig), protocol.WithPushNotificationServerConfig(&pushnotificationserver.Config{
Enabled: config.PushNotificationServerConfig.Enabled,
Identity: config.PushNotificationServerConfig.Identity,
GorushURL: config.PushNotificationServerConfig.GorushURL,
}),
protocol.WithDatabase(appDB), protocol.WithDatabase(appDB),
protocol.WithWalletDatabase(walletDB), protocol.WithWalletDatabase(walletDB),
protocol.WithTorrentConfig(&config.TorrentConfig), protocol.WithTorrentConfig(&config.TorrentConfig),

View File

@ -5,33 +5,20 @@ import (
"errors" "errors"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/sqlite"
) )
const InMemoryPath = ":memory:"
// The reduced number of kdf iterations (for performance reasons) which is
// currently used for derivation of the database key
// https://github.com/status-im/status-go/pull/1343
// https://notes.status.im/i8Y_l7ccTiOYq09HVgoFwA
const ReducedKDFIterationsNumber = 3200
type DatabaseInitializer interface { type DatabaseInitializer interface {
Initialize(path, password string, kdfIterationsNumber int) (*sql.DB, error) Initialize(path, password string, kdfIterationsNumber int) (*sql.DB, error)
} }
// DecryptDatabase creates an unencrypted copy of the database and copies it
// over to the given directory
func DecryptDatabase(oldPath, newPath, password string, kdfIterationsNumber int) error {
return sqlite.DecryptDB(oldPath, newPath, password, kdfIterationsNumber)
}
// EncryptDatabase creates an encrypted copy of the database and copies it to the
// user path
func EncryptDatabase(oldPath, newPath, password string, kdfIterationsNumber int, onStart func(), onEnd func()) error {
return sqlite.EncryptDB(oldPath, newPath, password, kdfIterationsNumber, onStart, onEnd)
}
func ExportDB(path string, password string, kdfIterationsNumber int, newDbPath string, newPassword string, onStart func(), onEnd func()) error {
return sqlite.ExportDB(path, password, kdfIterationsNumber, newDbPath, newPassword, onStart, onEnd)
}
func ChangeDatabasePassword(path string, password string, kdfIterationsNumber int, newPassword string, onStart func(), onEnd func()) error {
return sqlite.ChangeEncryptionKey(path, password, kdfIterationsNumber, newPassword, onStart, onEnd)
}
// GetDBFilename takes an instance of sql.DB and returns the filename of the "main" database // GetDBFilename takes an instance of sql.DB and returns the filename of the "main" database
func GetDBFilename(db *sql.DB) (string, error) { func GetDBFilename(db *sql.DB) (string, error) {
if db == nil { if db == nil {

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/common" "github.com/status-im/status-go/multiaccounts/common"
"github.com/status-im/status-go/multiaccounts/migrations" "github.com/status-im/status-go/multiaccounts/migrations"
@ -300,7 +301,7 @@ func (db *Database) SaveAccount(account Account) error {
} }
if account.KDFIterations <= 0 { if account.KDFIterations <= 0 {
account.KDFIterations = sqlite.ReducedKDFIterationsNumber account.KDFIterations = dbsetup.ReducedKDFIterationsNumber
} }
_, err = db.db.Exec("INSERT OR REPLACE INTO accounts (name, identicon, colorHash, colorId, customizationColor, customizationColorClock, keycardPairing, keyUid, kdfIterations) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KeyUID, account.KDFIterations) _, err = db.db.Exec("INSERT OR REPLACE INTO accounts (name, identicon, colorHash, colorId, customizationColor, customizationColorClock, keycardPairing, keyUid, kdfIterations) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KeyUID, account.KDFIterations)
@ -322,7 +323,7 @@ func (db *Database) UpdateAccount(account Account) error {
} }
if account.KDFIterations <= 0 { if account.KDFIterations <= 0 {
account.KDFIterations = sqlite.ReducedKDFIterationsNumber account.KDFIterations = dbsetup.ReducedKDFIterationsNumber
} }
_, err = db.db.Exec("UPDATE accounts SET name = ?, identicon = ?, colorHash = ?, colorId = ?, customizationColor = ?, customizationColorClock = ?, keycardPairing = ?, kdfIterations = ? WHERE keyUid = ?", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KDFIterations, account.KeyUID) _, err = db.db.Exec("UPDATE accounts SET name = ?, identicon = ?, colorHash = ?, colorId = ?, customizationColor = ?, customizationColorClock = ?, keycardPairing = ?, kdfIterations = ? WHERE keyUid = ?", account.Name, account.Identicon, colorHash, account.ColorID, account.CustomizationColor, account.CustomizationColorClock, account.KeycardPairing, account.KDFIterations, account.KeyUID)

View File

@ -6,8 +6,8 @@ import (
"os" "os"
"testing" "testing"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -28,7 +28,7 @@ func setupTestDB(t *testing.T) (*Database, func()) {
func TestAccounts(t *testing.T) { func TestAccounts(t *testing.T) {
db, stop := setupTestDB(t) db, stop := setupTestDB(t)
defer stop() defer stop()
expected := Account{Name: "string", KeyUID: "string", CustomizationColor: common.CustomizationColorBlue, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: sqlite.ReducedKDFIterationsNumber} expected := Account{Name: "string", KeyUID: "string", CustomizationColor: common.CustomizationColorBlue, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: dbsetup.ReducedKDFIterationsNumber}
require.NoError(t, db.SaveAccount(expected)) require.NoError(t, db.SaveAccount(expected))
accounts, err := db.GetAccounts() accounts, err := db.GetAccounts()
require.NoError(t, err) require.NoError(t, err)
@ -39,7 +39,7 @@ func TestAccounts(t *testing.T) {
func TestAccountsUpdate(t *testing.T) { func TestAccountsUpdate(t *testing.T) {
db, stop := setupTestDB(t) db, stop := setupTestDB(t)
defer stop() defer stop()
expected := Account{KeyUID: "string", CustomizationColor: common.CustomizationColorBlue, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: sqlite.ReducedKDFIterationsNumber} expected := Account{KeyUID: "string", CustomizationColor: common.CustomizationColorBlue, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: dbsetup.ReducedKDFIterationsNumber}
require.NoError(t, db.SaveAccount(expected)) require.NoError(t, db.SaveAccount(expected))
expected.Name = "chars" expected.Name = "chars"
expected.CustomizationColor = common.CustomizationColorMagenta expected.CustomizationColor = common.CustomizationColorMagenta
@ -54,7 +54,7 @@ func TestLoginUpdate(t *testing.T) {
db, stop := setupTestDB(t) db, stop := setupTestDB(t)
defer stop() defer stop()
accounts := []Account{{Name: "first", KeyUID: "0x1", KDFIterations: sqlite.ReducedKDFIterationsNumber}, {Name: "second", KeyUID: "0x2", KDFIterations: sqlite.ReducedKDFIterationsNumber}} accounts := []Account{{Name: "first", KeyUID: "0x1", KDFIterations: dbsetup.ReducedKDFIterationsNumber}, {Name: "second", KeyUID: "0x2", KDFIterations: dbsetup.ReducedKDFIterationsNumber}}
for _, acc := range accounts { for _, acc := range accounts {
require.NoError(t, db.SaveAccount(acc)) require.NoError(t, db.SaveAccount(acc))
} }
@ -185,7 +185,7 @@ func TestDatabase_GetAccount(t *testing.T) {
db, stop := setupTestDB(t) db, stop := setupTestDB(t)
defer stop() defer stop()
expected := Account{Name: "string", KeyUID: keyUID, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: sqlite.ReducedKDFIterationsNumber} expected := Account{Name: "string", KeyUID: keyUID, ColorHash: ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorID: 10, KDFIterations: dbsetup.ReducedKDFIterationsNumber}
require.NoError(t, db.SaveAccount(expected)) require.NoError(t, db.SaveAccount(expected))
account, err := db.GetAccount(expected.KeyUID) account, err := db.GetAccount(expected.KeyUID)

View File

@ -8,10 +8,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts/errors" "github.com/status-im/status-go/multiaccounts/errors"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/t/helpers" "github.com/status-im/status-go/t/helpers"
) )
@ -71,7 +71,7 @@ func TestClosingsqlDB(t *testing.T) {
password := "settings-tests" password := "settings-tests"
// Make connection with sql.DB // Make connection with sql.DB
db, err := appdatabase.InitializeDB(testFileName, password, sqlite.ReducedKDFIterationsNumber) db, err := appdatabase.InitializeDB(testFileName, password, dbsetup.ReducedKDFIterationsNumber)
// handle removing the test file on any exit // handle removing the test file on any exit
defer func() { defer func() {
@ -96,7 +96,7 @@ func TestClosingsqlDB(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Make another connection with sql.DB // Make another connection with sql.DB
db2, err := appdatabase.InitializeDB(testFileName, password, sqlite.ReducedKDFIterationsNumber) db2, err := appdatabase.InitializeDB(testFileName, password, dbsetup.ReducedKDFIterationsNumber)
require.NoError(t, err) require.NoError(t, err)
// Init settings.Database struct using second connection // Init settings.Database struct using second connection

View File

@ -22,7 +22,6 @@ import (
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/static" "github.com/status-im/status-go/static"
wakucommon "github.com/status-im/status-go/waku/common" wakucommon "github.com/status-im/status-go/waku/common"
wakuv2common "github.com/status-im/status-go/wakuv2/common" wakuv2common "github.com/status-im/status-go/wakuv2/common"
@ -489,7 +488,7 @@ type NodeConfig struct {
MailServerRegistryAddress string MailServerRegistryAddress string
// PushNotificationServerConfig is the config for the push notification server // PushNotificationServerConfig is the config for the push notification server
PushNotificationServerConfig pushnotificationserver.Config `json:"PushNotificationServerConfig"` PushNotificationServerConfig PushNotificationServerConfig `json:"PushNotificationServerConfig"`
OutputMessageCSVEnabled bool OutputMessageCSVEnabled bool
@ -587,6 +586,12 @@ func (p *PushNotificationServer) UnmarshalText(data []byte) error {
return nil return nil
} }
type PushNotificationServerConfig struct {
Enabled bool
Identity *ecdsa.PrivateKey
GorushURL string
}
// ShhextConfig defines options used by shhext service. // ShhextConfig defines options used by shhext service.
type ShhextConfig struct { type ShhextConfig struct {
PFSEnabled bool PFSEnabled bool

View File

@ -1,10 +1,10 @@
package common package common
import ( import (
"path/filepath"
"testing" "testing"
transport2 "github.com/status-im/status-go/protocol/transport" transport2 "github.com/status-im/status-go/protocol/transport"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/waku" "github.com/status-im/status-go/waku"
@ -22,6 +22,8 @@ import (
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
v1protocol "github.com/status-im/status-go/protocol/v1" v1protocol "github.com/status-im/status-go/protocol/v1"
"github.com/status-im/status-go/appdatabase"
) )
func TestMessageSenderSuite(t *testing.T) { func TestMessageSenderSuite(t *testing.T) {
@ -32,7 +34,6 @@ type MessageSenderSuite struct {
suite.Suite suite.Suite
sender *MessageSender sender *MessageSender
tmpDir string
testMessage protobuf.ChatMessage testMessage protobuf.ChatMessage
logger *zap.Logger logger *zap.Logger
} }
@ -52,12 +53,12 @@ func (s *MessageSenderSuite) SetupTest() {
s.logger, err = zap.NewDevelopment() s.logger, err = zap.NewDevelopment()
s.Require().NoError(err) s.Require().NoError(err)
s.tmpDir = s.T().TempDir()
identity, err := crypto.GenerateKey() identity, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
database, err := sqlite.Open(filepath.Join(s.tmpDir, "sender-test.sql"), "some-key", sqlite.ReducedKDFIterationsNumber) database, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(database)
s.Require().NoError(err) s.Require().NoError(err)
encryptionProtocol := encryption.New( encryptionProtocol := encryption.New(
@ -188,8 +189,11 @@ func (s *MessageSenderSuite) TestHandleDecodedMessagesDatasyncEncrypted() {
s.Require().NoError(err) s.Require().NoError(err)
// Create sender encryption protocol. // Create sender encryption protocol.
senderDatabase, err := sqlite.Open(filepath.Join(s.tmpDir, "sender.db.sql"), "", sqlite.ReducedKDFIterationsNumber) senderDatabase, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err) s.Require().NoError(err)
err = sqlite.Migrate(senderDatabase)
s.Require().NoError(err)
senderEncryptionProtocol := encryption.New( senderEncryptionProtocol := encryption.New(
senderDatabase, senderDatabase,
"installation-2", "installation-2",

View File

@ -3,12 +3,12 @@ package encryption
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"io/ioutil"
"testing" "testing"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/t/helpers"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
@ -51,12 +51,12 @@ func setupUser(user string, s *EncryptionServiceMultiDeviceSuite, n int) error {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
installationID := fmt.Sprintf("%s%d", user, i+1) installationID := fmt.Sprintf("%s%d", user, i+1)
dbFileName := fmt.Sprintf("sqlite-persistence-test-%s.sql", installationID)
dbPath, err := ioutil.TempFile("", dbFileName) db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
if err != nil { if err != nil {
return err return err
} }
db, err := sqlite.Open(dbPath.Name(), "some-key", sqlite.ReducedKDFIterationsNumber) err = sqlite.Migrate(db)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,12 +3,12 @@ package encryption
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os"
"testing" "testing"
"time" "time"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
@ -30,20 +30,14 @@ type EncryptionServiceTestSuite struct {
logger *zap.Logger logger *zap.Logger
alice *Protocol alice *Protocol
bob *Protocol bob *Protocol
aliceDBPath *os.File
bobDBPath *os.File
} }
func (s *EncryptionServiceTestSuite) initDatabases(config encryptorConfig) { func (s *EncryptionServiceTestSuite) initDatabases(config encryptorConfig) {
var err error var err error
s.aliceDBPath, err = ioutil.TempFile("", "alice.db.sql") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err) s.Require().NoError(err)
err = sqlite.Migrate(db)
s.bobDBPath, err = ioutil.TempFile("", "bob.db.sql")
s.Require().NoError(err)
db, err := sqlite.Open(s.aliceDBPath.Name(), "alice-key", sqlite.ReducedKDFIterationsNumber)
s.Require().NoError(err) s.Require().NoError(err)
config.InstallationID = aliceInstallationID config.InstallationID = aliceInstallationID
s.alice = NewWithEncryptorConfig( s.alice = NewWithEncryptorConfig(
@ -53,7 +47,9 @@ func (s *EncryptionServiceTestSuite) initDatabases(config encryptorConfig) {
s.logger.With(zap.String("user", "alice")), s.logger.With(zap.String("user", "alice")),
) )
db, err = sqlite.Open(s.bobDBPath.Name(), "bob-key", sqlite.ReducedKDFIterationsNumber) db, err = helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(db)
s.Require().NoError(err) s.Require().NoError(err)
config.InstallationID = bobInstallationID config.InstallationID = bobInstallationID
s.bob = NewWithEncryptorConfig( s.bob = NewWithEncryptorConfig(
@ -70,8 +66,6 @@ func (s *EncryptionServiceTestSuite) SetupTest() {
} }
func (s *EncryptionServiceTestSuite) TearDownTest() { func (s *EncryptionServiceTestSuite) TearDownTest() {
os.Remove(s.aliceDBPath.Name())
os.Remove(s.bobDBPath.Name())
_ = s.logger.Sync() _ = s.logger.Sync()
} }

View File

@ -1,17 +1,13 @@
package multidevice package multidevice
import ( import (
"database/sql"
"os"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
) "github.com/status-im/status-go/t/helpers"
const (
dbPath = "/tmp/status-key-store.db"
) )
func TestSQLLitePersistenceTestSuite(t *testing.T) { func TestSQLLitePersistenceTestSuite(t *testing.T) {
@ -20,15 +16,13 @@ func TestSQLLitePersistenceTestSuite(t *testing.T) {
type SQLLitePersistenceTestSuite struct { type SQLLitePersistenceTestSuite struct {
suite.Suite suite.Suite
// nolint: structcheck, megacheck
db *sql.DB
service *sqlitePersistence service *sqlitePersistence
} }
func (s *SQLLitePersistenceTestSuite) SetupTest() { func (s *SQLLitePersistenceTestSuite) SetupTest() {
os.Remove(dbPath) db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
db, err := sqlite.Open(dbPath, "test-key", sqlite.ReducedKDFIterationsNumber) err = sqlite.Migrate(db)
s.Require().NoError(err) s.Require().NoError(err)
s.service = newSQLitePersistence(db) s.service = newSQLitePersistence(db)

View File

@ -1,13 +1,14 @@
package encryption package encryption
import ( import (
"path/filepath"
"testing" "testing"
dr "github.com/status-im/doubleratchet" dr "github.com/status-im/doubleratchet"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
var ( var (
@ -30,10 +31,9 @@ type SQLLitePersistenceKeysStorageTestSuite struct {
} }
func (s *SQLLitePersistenceKeysStorageTestSuite) SetupTest() { func (s *SQLLitePersistenceKeysStorageTestSuite) SetupTest() {
dir := s.T().TempDir() db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
key := "blahblahblah" s.Require().NoError(err)
err = sqlite.Migrate(db)
db, err := sqlite.Open(filepath.Join(dir, "db.sql"), key, sqlite.ReducedKDFIterationsNumber)
s.Require().NoError(err) s.Require().NoError(err)
p := newSQLitePersistence(db) p := newSQLitePersistence(db)

View File

@ -1,13 +1,13 @@
package encryption package encryption
import ( import (
"database/sql"
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/protocol/encryption/multidevice" "github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
@ -19,15 +19,13 @@ func TestSQLLitePersistenceTestSuite(t *testing.T) {
type SQLLitePersistenceTestSuite struct { type SQLLitePersistenceTestSuite struct {
suite.Suite suite.Suite
// nolint: structcheck, megacheck
db *sql.DB
service *sqlitePersistence service *sqlitePersistence
} }
func (s *SQLLitePersistenceTestSuite) SetupTest() { func (s *SQLLitePersistenceTestSuite) SetupTest() {
dir := s.T().TempDir() db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
db, err := sqlite.Open(filepath.Join(dir, "db.sql"), "test-key", sqlite.ReducedKDFIterationsNumber) err = sqlite.Migrate(db)
s.Require().NoError(err) s.Require().NoError(err)
s.service = newSQLitePersistence(db) s.service = newSQLitePersistence(db)

View File

@ -1,13 +1,12 @@
package encryption package encryption
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/t/helpers"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
@ -21,8 +20,6 @@ func TestProtocolServiceTestSuite(t *testing.T) {
type ProtocolServiceTestSuite struct { type ProtocolServiceTestSuite struct {
suite.Suite suite.Suite
aliceDBPath *os.File
bobDBPath *os.File
alice *Protocol alice *Protocol
bob *Protocol bob *Protocol
logger *zap.Logger logger *zap.Logger
@ -33,15 +30,9 @@ func (s *ProtocolServiceTestSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger() s.logger = tt.MustCreateTestLogger()
s.aliceDBPath, err = ioutil.TempFile("", "alice.db.sql") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err) s.Require().NoError(err)
aliceDBKey := "alice" err = sqlite.Migrate(db)
s.bobDBPath, err = ioutil.TempFile("", "bob.db.sql")
s.Require().NoError(err)
bobDBKey := "bob"
db, err := sqlite.Open(s.aliceDBPath.Name(), aliceDBKey, sqlite.ReducedKDFIterationsNumber)
s.Require().NoError(err) s.Require().NoError(err)
s.alice = New( s.alice = New(
db, db,
@ -49,7 +40,9 @@ func (s *ProtocolServiceTestSuite) SetupTest() {
s.logger.With(zap.String("user", "alice")), s.logger.With(zap.String("user", "alice")),
) )
db, err = sqlite.Open(s.bobDBPath.Name(), bobDBKey, sqlite.ReducedKDFIterationsNumber) db, err = helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(db)
s.Require().NoError(err) s.Require().NoError(err)
s.bob = New( s.bob = New(
db, db,
@ -59,8 +52,6 @@ func (s *ProtocolServiceTestSuite) SetupTest() {
} }
func (s *ProtocolServiceTestSuite) TearDownTest() { func (s *ProtocolServiceTestSuite) TearDownTest() {
os.Remove(s.aliceDBPath.Name())
os.Remove(s.bobDBPath.Name())
_ = s.logger.Sync() _ = s.logger.Sync()
} }

View File

@ -1,17 +1,17 @@
package sharedsecret package sharedsecret
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/t/helpers"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/sqlite"
) )
func TestServiceTestSuite(t *testing.T) { func TestServiceTestSuite(t *testing.T) {
@ -21,25 +21,21 @@ func TestServiceTestSuite(t *testing.T) {
type SharedSecretTestSuite struct { type SharedSecretTestSuite struct {
suite.Suite suite.Suite
service *SharedSecret service *SharedSecret
path string
logger *zap.Logger logger *zap.Logger
} }
func (s *SharedSecretTestSuite) SetupTest() { func (s *SharedSecretTestSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger() s.logger = tt.MustCreateTestLogger()
dbFile, err := ioutil.TempFile(os.TempDir(), "sharedsecret") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err) s.Require().NoError(err)
s.path = dbFile.Name() err = sqlite.Migrate(db)
db, err := sqlite.Open(s.path, "", sqlite.ReducedKDFIterationsNumber)
s.Require().NoError(err) s.Require().NoError(err)
s.service = New(db, s.logger) s.service = New(db, s.logger)
} }
func (s *SharedSecretTestSuite) TearDownTest() { func (s *SharedSecretTestSuite) TearDownTest() {
os.Remove(s.path)
_ = s.logger.Sync() _ = s.logger.Sync()
} }

View File

@ -5,7 +5,9 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
func TestGetENSToBeVerified(t *testing.T) { func TestGetENSToBeVerified(t *testing.T) {
@ -13,9 +15,8 @@ func TestGetENSToBeVerified(t *testing.T) {
name := "test.eth" name := "test.eth"
updatedName := "test2.eth" updatedName := "test2.eth"
db, err := sqlite.Open(sqlite.InMemoryPath, "", sqlite.ReducedKDFIterationsNumber) db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
require.NoError(t, err) require.NoError(t, err)
err = sqlite.Migrate(db) err = sqlite.Migrate(db)
require.NoError(t, err) require.NoError(t, err)

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"database/sql" "database/sql"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"sort" "sort"
"strconv" "strconv"
@ -14,11 +13,13 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
func TestTableUserMessagesAllFieldsCount(t *testing.T) { func TestTableUserMessagesAllFieldsCount(t *testing.T) {
@ -891,11 +892,12 @@ func TestPersistenceEmojiReactions(t *testing.T) {
} }
func openTestDB() (*sql.DB, error) { func openTestDB() (*sql.DB, error) {
dbPath, err := ioutil.TempFile("", "status-go-test-db-") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return sqlite.Open(dbPath.Name(), "", sqlite.ReducedKDFIterationsNumber)
return db, sqlite.Migrate(db)
} }
func insertMinimalMessage(p *sqlitePersistence, id string) error { func insertMinimalMessage(p *sqlitePersistence, id string) error {

View File

@ -3,14 +3,13 @@ package pushnotificationclient
import ( import (
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
"io/ioutil"
"math/rand" "math/rand"
"os"
"testing" "testing"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/crypto/ecies" "github.com/status-im/status-go/eth-node/crypto/ecies"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
@ -18,13 +17,13 @@ import (
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/t/helpers"
) )
const testDeviceToken = "test-token" const testDeviceToken = "test-token"
type ClientSuite struct { type ClientSuite struct {
suite.Suite suite.Suite
tmpFile *os.File
persistence *Persistence persistence *Persistence
identity *ecdsa.PrivateKey identity *ecdsa.PrivateKey
installationID string installationID string
@ -39,13 +38,12 @@ func TestClientSuite(t *testing.T) {
} }
func (s *ClientSuite) SetupTest() { func (s *ClientSuite) SetupTest() {
tmpFile, err := ioutil.TempFile("", "") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(db)
s.Require().NoError(err) s.Require().NoError(err)
s.tmpFile = tmpFile
database, err := sqlite.Open(s.tmpFile.Name(), "", sqlite.ReducedKDFIterationsNumber) s.persistence = NewPersistence(db)
s.Require().NoError(err)
s.persistence = NewPersistence(database)
identity, err := crypto.GenerateKey() identity, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -2,18 +2,18 @@ package pushnotificationclient
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"io/ioutil"
"os"
"testing" "testing"
"time" "time"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
const ( const (
@ -29,22 +29,16 @@ func TestSQLitePersistenceSuite(t *testing.T) {
type SQLitePersistenceSuite struct { type SQLitePersistenceSuite struct {
suite.Suite suite.Suite
tmpFile *os.File
persistence *Persistence persistence *Persistence
} }
func (s *SQLitePersistenceSuite) SetupTest() { func (s *SQLitePersistenceSuite) SetupTest() {
tmpFile, err := ioutil.TempFile("", "") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err) s.Require().NoError(err)
s.tmpFile = tmpFile err = sqlite.Migrate(db)
database, err := sqlite.Open(s.tmpFile.Name(), "", sqlite.ReducedKDFIterationsNumber)
s.Require().NoError(err) s.Require().NoError(err)
s.persistence = NewPersistence(database)
}
func (s *SQLitePersistenceSuite) TearDownTest() { s.persistence = NewPersistence(db)
_ = os.Remove(s.tmpFile.Name())
} }
func (s *SQLitePersistenceSuite) TestSaveAndRetrieveServer() { func (s *SQLitePersistenceSuite) TestSaveAndRetrieveServer() {

View File

@ -1,17 +1,17 @@
package pushnotificationserver package pushnotificationserver
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
func TestSQLitePersistenceSuite(t *testing.T) { func TestSQLitePersistenceSuite(t *testing.T) {
@ -20,22 +20,16 @@ func TestSQLitePersistenceSuite(t *testing.T) {
type SQLitePersistenceSuite struct { type SQLitePersistenceSuite struct {
suite.Suite suite.Suite
tmpFile *os.File
persistence Persistence persistence Persistence
} }
func (s *SQLitePersistenceSuite) SetupTest() { func (s *SQLitePersistenceSuite) SetupTest() {
tmpFile, err := ioutil.TempFile("", "") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err) s.Require().NoError(err)
s.tmpFile = tmpFile err = sqlite.Migrate(db)
database, err := sqlite.Open(s.tmpFile.Name(), "", sqlite.ReducedKDFIterationsNumber)
s.Require().NoError(err) s.Require().NoError(err)
s.persistence = NewSQLitePersistence(database)
}
func (s *SQLitePersistenceSuite) TearDownTest() { s.persistence = NewSQLitePersistence(db)
_ = os.Remove(s.tmpFile.Name())
} }
func (s *SQLitePersistenceSuite) TestSaveAndRetrieve() { func (s *SQLitePersistenceSuite) TestSaveAndRetrieve() {

View File

@ -3,18 +3,18 @@ package pushnotificationserver
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/rand" "crypto/rand"
"io/ioutil"
"os"
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/t/helpers"
) )
func TestServerSuite(t *testing.T) { func TestServerSuite(t *testing.T) {
@ -27,7 +27,6 @@ func TestServerSuite(t *testing.T) {
type ServerSuite struct { type ServerSuite struct {
suite.Suite suite.Suite
tmpFile *os.File
persistence Persistence persistence Persistence
accessToken string accessToken string
installationID string installationID string
@ -39,13 +38,12 @@ type ServerSuite struct {
} }
func (s *ServerSuite) SetupTest() { func (s *ServerSuite) SetupTest() {
tmpFile, err := ioutil.TempFile("", "") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(db)
s.Require().NoError(err) s.Require().NoError(err)
s.tmpFile = tmpFile
database, err := sqlite.Open(s.tmpFile.Name(), "", sqlite.ReducedKDFIterationsNumber) s.persistence = NewSQLitePersistence(db)
s.Require().NoError(err)
s.persistence = NewSQLitePersistence(database)
identity, err := crypto.GenerateKey() identity, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -2,8 +2,6 @@ package sqlite
import ( import (
"database/sql" "database/sql"
"fmt"
"os"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -14,87 +12,8 @@ import (
mvdsmigrations "github.com/vacp2p/mvds/persistenceutil" mvdsmigrations "github.com/vacp2p/mvds/persistenceutil"
) )
// The reduced number of kdf iterations (for performance reasons) which is
// currently used for derivation of the database key
// https://github.com/status-im/status-go/pull/1343
// https://notes.status.im/i8Y_l7ccTiOYq09HVgoFwA
const ReducedKDFIterationsNumber = 3200
const InMemoryPath = ":memory:"
var migrationsTable = "status_protocol_go_" + sqlcipher.DefaultMigrationsTable var migrationsTable = "status_protocol_go_" + sqlcipher.DefaultMigrationsTable
// MigrationConfig is a struct that allows to define bindata migrations.
type MigrationConfig struct {
AssetNames []string
AssetGetter func(name string) ([]byte, error)
}
// Open opens or initializes a new database for a given file path.
// MigrationConfig is optional but if provided migrations are applied automatically.
func Open(path, key string, kdfIterationNumber int) (*sql.DB, error) {
return openAndMigrate(path, key, kdfIterationNumber)
}
// OpenInMemory opens an in memory SQLite database.
// Number of KDF iterations is reduced to 0.
func OpenInMemory() (*sql.DB, error) {
return openAndMigrate(InMemoryPath, "", 0)
}
// OpenWithIter allows to open a new database with a custom number of kdf iterations.
// Higher kdf iterations number makes it slower to open the database.
func OpenWithIter(path, key string, kdfIter int) (*sql.DB, error) {
return openAndMigrate(path, key, kdfIter)
}
func open(path string, key string, kdfIter int) (*sql.DB, error) {
if path != InMemoryPath {
_, err := os.OpenFile(path, os.O_CREATE, 0600)
if err != nil {
return nil, err
}
}
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
keyString := fmt.Sprintf("PRAGMA key = '%s'", key)
// Disable concurrent access as not supported by the driver
db.SetMaxOpenConns(1)
if _, err = db.Exec("PRAGMA foreign_keys=ON"); err != nil {
return nil, err
}
if _, err = db.Exec(keyString); err != nil {
return nil, err
}
kdfString := fmt.Sprintf("PRAGMA kdf_iter = '%d'", kdfIter)
if _, err = db.Exec(kdfString); err != nil {
return nil, err
}
return db, nil
}
func openAndMigrate(path string, key string, kdfIter int) (*sql.DB, error) {
db, err := open(path, key, kdfIter)
if err != nil {
return nil, err
}
if err := Migrate(db); err != nil {
return nil, err
}
return db, nil
}
// applyMigrations allows to apply bindata migrations on the current *sql.DB. // applyMigrations allows to apply bindata migrations on the current *sql.DB.
// `assetNames` is a list of assets with migrations and `assetGetter` is responsible // `assetNames` is a list of assets with migrations and `assetGetter` is responsible
// for returning the content of the asset with a given name. // for returning the content of the asset with a given name.

View File

@ -1,42 +1,21 @@
package sqlite package sqlite
import ( import (
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/t/helpers"
) )
func TestOpen(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "db.sql")
// Open the db for the first time.
db, err := openAndMigrate(dbPath, "some-key", ReducedKDFIterationsNumber)
require.NoError(t, err)
// Insert some data.
_, err = db.Exec("CREATE TABLE test(name TEXT)")
require.NoError(t, err)
_, err = db.Exec(`INSERT INTO test (name) VALUES ("abc")`)
require.NoError(t, err)
db.Close()
// Open again with different key should fail
// because the file already exists and it should not
// be recreated.
_, err = openAndMigrate(dbPath, "different-key", ReducedKDFIterationsNumber)
require.Error(t, err)
}
// TestCommunitiesMigrationDirty tests the communities migration when // TestCommunitiesMigrationDirty tests the communities migration when
// dirty flag has been set to true. // dirty flag has been set to true.
// We first make it fail, then clean up so that it can be replayed, and // We first make it fail, then clean up so that it can be replayed, and
// then execute again, and we should be all migrated. // then execute again, and we should be all migrated.
func TestCommunitiesMigrationDirty(t *testing.T) { func TestCommunitiesMigrationDirty(t *testing.T) {
// Open the db for the first time. // Open the db for the first time.
db, err := open(InMemoryPath, "some-key", ReducedKDFIterationsNumber) db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
require.NoError(t, err) require.NoError(t, err)
// Create a communities table, so that migration will fail // Create a communities table, so that migration will fail
@ -84,7 +63,7 @@ func TestCommunitiesMigrationDirty(t *testing.T) {
// dirty to false and then execute again, and we should be all migrated. // dirty to false and then execute again, and we should be all migrated.
func TestCommunitiesMigrationNotDirty(t *testing.T) { func TestCommunitiesMigrationNotDirty(t *testing.T) {
// Open the db for the first time. // Open the db for the first time.
db, err := open(InMemoryPath, "some-key", ReducedKDFIterationsNumber) db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
require.NoError(t, err) require.NoError(t, err)
// Create a communities table, so that migration will fail // Create a communities table, so that migration will fail

View File

@ -1,11 +1,11 @@
package transport package transport
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -13,10 +13,11 @@ import (
) )
func TestNewTransport(t *testing.T) { func TestNewTransport(t *testing.T) {
dbPath, err := ioutil.TempFile("", "transport.sql") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
require.NoError(t, err) require.NoError(t, err)
defer os.Remove(dbPath.Name()) err = sqlite.Migrate(db)
db, err := sqlite.Open(dbPath.Name(), "some-key", sqlite.ReducedKDFIterationsNumber) require.NoError(t, err)
require.NoError(t, err) require.NoError(t, err)
logger := tt.MustCreateTestLogger() logger := tt.MustCreateTestLogger()

View File

@ -1,13 +1,14 @@
package verification package verification
import ( import (
"io/ioutil"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
func TestPersistenceSuite(t *testing.T) { func TestPersistenceSuite(t *testing.T) {
@ -21,13 +22,10 @@ type PersistenceSuite struct {
} }
func (s *PersistenceSuite) SetupTest() { func (s *PersistenceSuite) SetupTest() {
s.db = nil db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
dbPath, err := ioutil.TempFile("", "") err = sqlite.Migrate(db)
s.NoError(err, "creating temp file for db") s.Require().NoError(err)
db, err := sqlite.Open(dbPath.Name(), "", sqlite.ReducedKDFIterationsNumber)
s.NoError(err, "creating sqlite db instance")
s.db = &Persistence{db: db} s.db = &Persistence{db: db}
} }

View File

@ -3,7 +3,6 @@ package server
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -12,17 +11,18 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
) )
func setupTest(t *testing.T) (*sql.DB, *zap.Logger) { func setupTest(t *testing.T) (*sql.DB, *zap.Logger) {
dbPath, err := ioutil.TempFile("", "status-go-test-db-") db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
require.NoError(t, err) require.NoError(t, err)
err = sqlite.Migrate(db)
db, err := sqlite.Open(dbPath.Name(), "", sqlite.ReducedKDFIterationsNumber)
require.NoError(t, err) require.NoError(t, err)
logger := logutils.ZapLogger() logger := logutils.ZapLogger()

View File

@ -13,9 +13,9 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/server/servertest" "github.com/status-im/status-go/server/servertest"
"github.com/status-im/status-go/t/utils" "github.com/status-im/status-go/t/utils"
) )
@ -29,7 +29,7 @@ var (
ColorHash: multiaccounts.ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}}, ColorHash: multiaccounts.ColorHash{{4, 3}, {4, 0}, {4, 3}, {4, 0}},
ColorID: 10, ColorID: 10,
Images: images.SampleIdentityImages(), Images: images.SampleIdentityImages(),
KDFIterations: sqlite.ReducedKDFIterationsNumber, KDFIterations: dbsetup.ReducedKDFIterationsNumber,
} }
account1Hash = []byte{0x8f, 0xba, 0x35, 0x1, 0x2b, 0x9d, 0xad, 0xf0, 0x2d, 0x3c, 0x4d, 0x6, 0xb5, 0x22, 0x2, 0x47, 0xd4, 0x1c, 0xf4, 0x31, 0x2f, 0xb, 0x5b, 0x27, 0x5d, 0x43, 0x97, 0x58, 0x2d, 0xf0, 0xe1, 0xbe} account1Hash = []byte{0x8f, 0xba, 0x35, 0x1, 0x2b, 0x9d, 0xad, 0xf0, 0x2d, 0x3c, 0x4d, 0x6, 0xb5, 0x22, 0x2, 0x47, 0xd4, 0x1c, 0xf4, 0x31, 0x2f, 0xb, 0x5b, 0x27, 0x5d, 0x43, 0x97, 0x58, 0x2d, 0xf0, 0xe1, 0xbe}
account2Hash = []byte{0x9, 0xf8, 0x5c, 0xe9, 0x92, 0x96, 0x2d, 0x88, 0x2b, 0x8e, 0x42, 0x3f, 0xa4, 0x93, 0x6c, 0xad, 0xe9, 0xc0, 0x1b, 0x8a, 0x8, 0x8c, 0x5e, 0x7a, 0x84, 0xa2, 0xf, 0x9f, 0x77, 0x58, 0x2c, 0x2c} account2Hash = []byte{0x9, 0xf8, 0x5c, 0xe9, 0x92, 0x96, 0x2d, 0x88, 0x2b, 0x8e, 0x42, 0x3f, 0xa4, 0x93, 0x6c, 0xad, 0xe9, 0xc0, 0x1b, 0x8a, 0x8, 0x8c, 0x5e, 0x7a, 0x84, 0xa2, 0xf, 0x9f, 0x77, 0x58, 0x2c, 0x2c}

View File

@ -13,6 +13,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/encryption/multidevice" "github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/protocol/tt"
@ -36,7 +37,6 @@ import (
"github.com/status-im/status-go/protocol/requests" "github.com/status-im/status-go/protocol/requests"
accservice "github.com/status-im/status-go/services/accounts" accservice "github.com/status-im/status-go/services/accounts"
"github.com/status-im/status-go/services/browsers" "github.com/status-im/status-go/services/browsers"
"github.com/status-im/status-go/sqlite"
) )
const ( const (
@ -102,7 +102,7 @@ func (s *SyncDeviceSuite) prepareBackendWithAccount(mnemonic, tmpdir string) *ap
} }
account := multiaccounts.Account{ account := multiaccounts.Account{
KeyUID: generatedAccountInfo.KeyUID, KeyUID: generatedAccountInfo.KeyUID,
KDFIterations: sqlite.ReducedKDFIterationsNumber, KDFIterations: dbsetup.ReducedKDFIterationsNumber,
} }
err = accountManager.InitKeystore(filepath.Join(tmpdir, keystoreDir, account.KeyUID)) err = accountManager.InitKeystore(filepath.Join(tmpdir, keystoreDir, account.KeyUID))
require.NoError(s.T(), err) require.NoError(s.T(), err)

View File

@ -9,13 +9,13 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/services/wallet/bigint" "github.com/status-im/status-go/services/wallet/bigint"
"github.com/status-im/status-go/walletdatabase" "github.com/status-im/status-go/walletdatabase"
) )
func setupBalanceDBTest(t *testing.T) (*BalanceDB, func()) { func setupBalanceDBTest(t *testing.T) (*BalanceDB, func()) {
db, err := walletdatabase.InitializeDB(sqlite.InMemoryPath, "wallet-history-balance_db-tests", 1) db, err := walletdatabase.InitializeDB(dbsetup.InMemoryPath, "wallet-history-balance_db-tests", 1)
require.NoError(t, err) require.NoError(t, err)
return NewBalanceDB(db), func() { return NewBalanceDB(db), func() {
require.NoError(t, db.Close()) require.NoError(t, db.Close())

View File

@ -12,7 +12,7 @@ import (
sqlcipher "github.com/mutecomm/go-sqlcipher/v4" // We require go sqlcipher that overrides default implementation sqlcipher "github.com/mutecomm/go-sqlcipher/v4" // We require go sqlcipher that overrides default implementation
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/common/dbsetup"
) )
const ( const (
@ -80,7 +80,7 @@ func attachDatabaseWithDefaultSettings(db *sql.DB, attachedDbPath string, attach
} }
if kdfIterationsNumber <= 0 { if kdfIterationsNumber <= 0 {
kdfIterationsNumber = sqlite.ReducedKDFIterationsNumber kdfIterationsNumber = dbsetup.ReducedKDFIterationsNumber
} }
if _, err := db.Exec(fmt.Sprintf(`PRAGMA %s.busy_timeout = 60000`, attachedDbName)); err != nil { if _, err := db.Exec(fmt.Sprintf(`PRAGMA %s.busy_timeout = 60000`, attachedDbName)); err != nil {
@ -177,7 +177,7 @@ func openDB(path string, key string, kdfIterationsNumber int, cipherPageSize int
} }
if kdfIterationsNumber <= 0 { if kdfIterationsNumber <= 0 {
kdfIterationsNumber = sqlite.ReducedKDFIterationsNumber kdfIterationsNumber = dbsetup.ReducedKDFIterationsNumber
} }
if _, err := conn.Exec(fmt.Sprintf("PRAGMA cipher_page_size = %d", cipherPageSize), nil); err != nil { if _, err := conn.Exec(fmt.Sprintf("PRAGMA cipher_page_size = %d", cipherPageSize), nil); err != nil {
@ -289,7 +289,7 @@ func ChangeEncryptionKey(path string, key string, kdfIterationsNumber int, newKe
} }
if kdfIterationsNumber <= 0 { if kdfIterationsNumber <= 0 {
kdfIterationsNumber = sqlite.ReducedKDFIterationsNumber kdfIterationsNumber = dbsetup.ReducedKDFIterationsNumber
} }
db, err := openDB(path, key, kdfIterationsNumber, V4CipherPageSize) db, err := openDB(path, key, kdfIterationsNumber, V4CipherPageSize)

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"github.com/status-im/status-go/common/dbsetup" "github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/protocol/sqlite"
) )
const kdfIterationsNumberForTests = 1 const kdfIterationsNumberForTests = 1
@ -33,7 +32,7 @@ func SetupTestSQLDB(dbInit dbsetup.DatabaseInitializer, prefix string) (*sql.DB,
} }
func SetupTestMemorySQLDB(dbInit dbsetup.DatabaseInitializer) (*sql.DB, error) { func SetupTestMemorySQLDB(dbInit dbsetup.DatabaseInitializer) (*sql.DB, error) {
db, err := dbInit.Initialize(sqlite.InMemoryPath, "password", kdfIterationsNumberForTests) db, err := dbInit.Initialize(dbsetup.InMemoryPath, "password", kdfIterationsNumberForTests)
if err != nil { if err != nil {
return nil, err return nil, err
} }