lnd/macaroons/store_test.go

123 lines
3.1 KiB
Go
Raw Normal View History

2018-02-01 01:04:56 +01:00
package macaroons_test
import (
"context"
2018-02-01 01:04:56 +01:00
"io/ioutil"
"os"
"path"
"testing"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
2018-02-01 01:04:56 +01:00
"github.com/lightningnetwork/lnd/macaroons"
"github.com/btcsuite/btcwallet/snacl"
2020-10-06 17:23:29 +02:00
"github.com/stretchr/testify/require"
2018-02-01 01:04:56 +01:00
)
func TestStore(t *testing.T) {
tempDir, err := ioutil.TempDir("", "macaroonstore-")
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
2018-02-01 01:04:56 +01:00
db, err := kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
2018-02-01 01:04:56 +01:00
store, err := macaroons.NewRootKeyStorage(db)
if err != nil {
2020-10-06 17:23:29 +02:00
_ = db.Close()
2018-02-01 01:04:56 +01:00
t.Fatalf("Error creating root key store: %v", err)
}
2020-10-06 17:23:29 +02:00
defer func() {
_ = store.Close()
}()
2018-02-01 01:04:56 +01:00
_, _, err = store.RootKey(context.TODO())
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrStoreLocked, err)
2018-02-01 01:04:56 +01:00
_, err = store.Get(context.TODO(), nil)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrStoreLocked, err)
2018-02-01 01:04:56 +01:00
pw := []byte("weks")
err = store.CreateUnlock(&pw)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
2018-02-01 01:04:56 +01:00
// Check ErrContextRootKeyID is returned when no root key ID found in
// context.
_, _, err = store.RootKey(context.TODO())
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrContextRootKeyID, err)
// Check ErrMissingRootKeyID is returned when empty root key ID is used.
2020-10-06 17:23:29 +02:00
emptyKeyID := make([]byte, 0)
badCtx := macaroons.ContextWithRootKeyID(context.TODO(), emptyKeyID)
_, _, err = store.RootKey(badCtx)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrMissingRootKeyID, err)
// Create a context with illegal root key ID value.
encryptedKeyID := []byte("enckey")
badCtx = macaroons.ContextWithRootKeyID(context.TODO(), encryptedKeyID)
_, _, err = store.RootKey(badCtx)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrKeyValueForbidden, err)
// Create a context with root key ID value.
ctx := macaroons.ContextWithRootKeyID(
context.TODO(), macaroons.DefaultRootKeyID,
)
key, id, err := store.RootKey(ctx)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
2018-02-01 01:04:56 +01:00
rootID := id
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.DefaultRootKeyID, rootID)
2018-02-01 01:04:56 +01:00
key2, err := store.Get(ctx, id)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
require.Equal(t, key, key2)
2018-02-01 01:04:56 +01:00
badpw := []byte("badweks")
err = store.CreateUnlock(&badpw)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrAlreadyUnlocked, err)
2018-02-01 01:04:56 +01:00
2020-10-06 17:23:29 +02:00
_ = store.Close()
2018-02-01 01:04:56 +01:00
// Between here and the re-opening of the store, it's possible to get
// a double-close, but that's not such a big deal since the tests will
// fail anyway in that case.
db, err = kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
2018-02-01 01:04:56 +01:00
store, err = macaroons.NewRootKeyStorage(db)
if err != nil {
2020-10-06 17:23:29 +02:00
_ = db.Close()
2018-02-01 01:04:56 +01:00
t.Fatalf("Error creating root key store: %v", err)
}
err = store.CreateUnlock(&badpw)
2020-10-06 17:23:29 +02:00
require.Equal(t, snacl.ErrInvalidPassword, err)
2018-02-01 01:04:56 +01:00
err = store.CreateUnlock(nil)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrPasswordRequired, err)
2018-02-01 01:04:56 +01:00
_, _, err = store.RootKey(ctx)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrStoreLocked, err)
2018-02-01 01:04:56 +01:00
_, err = store.Get(ctx, nil)
2020-10-06 17:23:29 +02:00
require.Equal(t, macaroons.ErrStoreLocked, err)
2018-02-01 01:04:56 +01:00
err = store.CreateUnlock(&pw)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
2018-02-01 01:04:56 +01:00
key, err = store.Get(ctx, rootID)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
require.Equal(t, key, key2)
2018-02-01 01:04:56 +01:00
key, id, err = store.RootKey(ctx)
2020-10-06 17:23:29 +02:00
require.NoError(t, err)
require.Equal(t, key, key2)
require.Equal(t, rootID, id)
2018-02-01 01:04:56 +01:00
}