lightning-terminal/session/migtest/raw_db_test.go
Elle Mouton c8b78bd10d
session: add new ID-to-key index
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.
2023-08-30 11:46:33 +02:00

80 lines
2.1 KiB
Go

package migtest
import (
"testing"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
)
// TestRestoreAndVerifyDB tests that the RestoreDB and VerifyDB helper methods
// works as expected.
func TestRestoreAndVerifyDB(t *testing.T) {
// Define the top leve key name and the DB structure.
topLevelKey := []byte("top-level")
dbStructure := map[string]interface{}{
"key1": "value1",
"bucket1": map[string]interface{}{
"key2": "value2",
"bucket2": map[string]interface{}{
"key3": "value3",
},
"bucket3": map[string]interface{}{
"key4": "value4",
},
},
}
// Create the DB and attempt to restore the DB structure.
db := MakeDB(t)
err := db.Update(func(tx *bbolt.Tx) error {
return RestoreDB(tx, topLevelKey, dbStructure)
})
require.NoError(t, err)
// Check that the VerifyDB method passes.
err = db.View(func(tx *bbolt.Tx) error {
return VerifyDB(tx, topLevelKey, dbStructure)
})
require.NoError(t, err)
// Now manually do some checks based on what we know about the db
// structure.
err = db.View(func(tx *bbolt.Tx) error {
// Query for a bucket we did not create and ensure that nil is
// returned.
bucket := tx.Bucket([]byte("top-level-2"))
require.Nil(t, bucket)
// Query for the actual top level bucket.
bucket = tx.Bucket(topLevelKey)
require.NotNil(t, bucket)
// Test the first bucket level.
require.Equal(t, []byte("value1"), bucket.Get([]byte("key1")))
require.Nil(t, bucket.Get([]byte("bucket1")))
require.Nil(t, bucket.Get([]byte("key2")))
// Test the second bucket level.
bucket1 := bucket.Bucket([]byte("bucket1"))
require.NotNil(t, bucket1)
require.Equal(t, []byte("value2"), bucket1.Get([]byte("key2")))
require.Nil(t, bucket1.Get([]byte("key1")))
require.Nil(t, bucket1.Get([]byte("bucket1")))
require.Nil(t, bucket1.Get([]byte("bucket2")))
bucket2 := bucket1.Bucket([]byte("bucket2"))
require.NotNil(t, bucket2)
require.Equal(t, []byte("value3"), bucket2.Get([]byte("key3")))
bucket3 := bucket1.Bucket([]byte("bucket3"))
require.NotNil(t, bucket3)
require.Equal(t, []byte("value4"), bucket3.Get([]byte("key4")))
return nil
})
require.NoError(t, err)
}