mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-17 13:07:41 +02:00
This commit does a few things: 1. Instead of deriving IDs using the first 4 bytes of the session's serialised local pub key, we instead use bytes [1:5] in order to skip the first byte which is either 0x02 or 0x03. This results in a greater entropy set. 2. We also add a new index from ID to key and we write to this index each time a new session is added. 3. We add a `ReserveNewSessionID` method to the session store which will grind through private keys until it finds one that does not clash with the current ID set. 4. A migration is added to back-fill the ID-to-key index. If any old sessions are found that _do_ have a colliding ID, they are sorted by created time and all but the newest session is revoked. Only an entry for the newest session will be added to the ID-to-key index.
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package migtest
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
const (
|
|
// dbFilePermission is the default permission the rules' database file
|
|
// is created with.
|
|
dbFilePermission = 0600
|
|
)
|
|
|
|
// MakeDB creates a new instance of the firewall DB for testing purposes.
|
|
func MakeDB(t *testing.T) *bbolt.DB {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "test.db")
|
|
|
|
db, err := bbolt.Open(path, dbFilePermission, &bbolt.Options{
|
|
Timeout: time.Second * 5,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
return db
|
|
}
|
|
|
|
// ApplyMigration is a helper test function that encapsulates the general steps
|
|
// which are needed to properly check the result of applying migration function.
|
|
func ApplyMigration(t *testing.T, beforeMigration, afterMigration,
|
|
migrationFunc func(tx *bbolt.Tx) error, shouldFail bool) {
|
|
|
|
t.Helper()
|
|
|
|
db := MakeDB(t)
|
|
|
|
// beforeMigration is usually used for populating the database with
|
|
// test data.
|
|
require.NoError(t, db.Update(beforeMigration))
|
|
|
|
defer func() {
|
|
t.Helper()
|
|
|
|
var err error
|
|
if r := recover(); r != nil {
|
|
err = newError(r)
|
|
}
|
|
|
|
if shouldFail {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// afterMigration usually used for checking the database state
|
|
// and throwing the error if something went wrong.
|
|
err = db.Update(afterMigration)
|
|
require.NoError(t, err)
|
|
}()
|
|
|
|
// Apply migration.
|
|
require.NoError(t, db.Update(migrationFunc))
|
|
}
|
|
|
|
func newError(e interface{}) error {
|
|
var err error
|
|
switch e := e.(type) {
|
|
case error:
|
|
err = e
|
|
default:
|
|
err = fmt.Errorf("%v", e)
|
|
}
|
|
|
|
return err
|
|
}
|