macaroons: use require for tests
This commit is contained in:
parent
10673b9dfa
commit
cd85e17b19
@ -1,7 +1,6 @@
|
|||||||
package macaroons_test
|
package macaroons_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@ -12,99 +11,76 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/macaroons"
|
"github.com/lightningnetwork/lnd/macaroons"
|
||||||
|
|
||||||
"github.com/btcsuite/btcwallet/snacl"
|
"github.com/btcsuite/btcwallet/snacl"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStore(t *testing.T) {
|
func TestStore(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "macaroonstore-")
|
tempDir, err := ioutil.TempDir("", "macaroonstore-")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error creating temp dir: %v", err)
|
defer func() {
|
||||||
}
|
_ = os.RemoveAll(tempDir)
|
||||||
defer os.RemoveAll(tempDir)
|
}()
|
||||||
|
|
||||||
db, err := kvdb.Create(
|
db, err := kvdb.Create(
|
||||||
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
|
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
|
||||||
)
|
)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error opening store DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
store, err := macaroons.NewRootKeyStorage(db)
|
store, err := macaroons.NewRootKeyStorage(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.Close()
|
_ = db.Close()
|
||||||
t.Fatalf("Error creating root key store: %v", err)
|
t.Fatalf("Error creating root key store: %v", err)
|
||||||
}
|
}
|
||||||
defer store.Close()
|
defer func() {
|
||||||
|
_ = store.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
_, _, err = store.RootKey(context.TODO())
|
_, _, err = store.RootKey(context.TODO())
|
||||||
if err != macaroons.ErrStoreLocked {
|
require.Equal(t, macaroons.ErrStoreLocked, err)
|
||||||
t.Fatalf("Received %v instead of ErrStoreLocked", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = store.Get(context.TODO(), nil)
|
_, err = store.Get(context.TODO(), nil)
|
||||||
if err != macaroons.ErrStoreLocked {
|
require.Equal(t, macaroons.ErrStoreLocked, err)
|
||||||
t.Fatalf("Received %v instead of ErrStoreLocked", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pw := []byte("weks")
|
pw := []byte("weks")
|
||||||
err = store.CreateUnlock(&pw)
|
err = store.CreateUnlock(&pw)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error creating store encryption key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check ErrContextRootKeyID is returned when no root key ID found in
|
// Check ErrContextRootKeyID is returned when no root key ID found in
|
||||||
// context.
|
// context.
|
||||||
_, _, err = store.RootKey(context.TODO())
|
_, _, err = store.RootKey(context.TODO())
|
||||||
if err != macaroons.ErrContextRootKeyID {
|
require.Equal(t, macaroons.ErrContextRootKeyID, err)
|
||||||
t.Fatalf("Received %v instead of ErrContextRootKeyID", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check ErrMissingRootKeyID is returned when empty root key ID is used.
|
// Check ErrMissingRootKeyID is returned when empty root key ID is used.
|
||||||
emptyKeyID := []byte{}
|
emptyKeyID := make([]byte, 0)
|
||||||
badCtx := macaroons.ContextWithRootKeyID(context.TODO(), emptyKeyID)
|
badCtx := macaroons.ContextWithRootKeyID(context.TODO(), emptyKeyID)
|
||||||
_, _, err = store.RootKey(badCtx)
|
_, _, err = store.RootKey(badCtx)
|
||||||
if err != macaroons.ErrMissingRootKeyID {
|
require.Equal(t, macaroons.ErrMissingRootKeyID, err)
|
||||||
t.Fatalf("Received %v instead of ErrMissingRootKeyID", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a context with illegal root key ID value.
|
// Create a context with illegal root key ID value.
|
||||||
encryptedKeyID := []byte("enckey")
|
encryptedKeyID := []byte("enckey")
|
||||||
badCtx = macaroons.ContextWithRootKeyID(context.TODO(), encryptedKeyID)
|
badCtx = macaroons.ContextWithRootKeyID(context.TODO(), encryptedKeyID)
|
||||||
_, _, err = store.RootKey(badCtx)
|
_, _, err = store.RootKey(badCtx)
|
||||||
if err != macaroons.ErrKeyValueForbidden {
|
require.Equal(t, macaroons.ErrKeyValueForbidden, err)
|
||||||
t.Fatalf("Received %v instead of ErrKeyValueForbidden", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a context with root key ID value.
|
// Create a context with root key ID value.
|
||||||
ctx := macaroons.ContextWithRootKeyID(
|
ctx := macaroons.ContextWithRootKeyID(
|
||||||
context.TODO(), macaroons.DefaultRootKeyID,
|
context.TODO(), macaroons.DefaultRootKeyID,
|
||||||
)
|
)
|
||||||
key, id, err := store.RootKey(ctx)
|
key, id, err := store.RootKey(ctx)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error getting root key from store: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rootID := id
|
rootID := id
|
||||||
if !bytes.Equal(rootID, macaroons.DefaultRootKeyID) {
|
require.Equal(t, macaroons.DefaultRootKeyID, rootID)
|
||||||
t.Fatalf("Root key ID doesn't match: expected %v, got %v",
|
|
||||||
macaroons.DefaultRootKeyID, rootID)
|
|
||||||
}
|
|
||||||
|
|
||||||
key2, err := store.Get(ctx, id)
|
key2, err := store.Get(ctx, id)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error getting key with ID %s: %v", string(id), err)
|
require.Equal(t, key, key2)
|
||||||
}
|
|
||||||
if !bytes.Equal(key, key2) {
|
|
||||||
t.Fatalf("Root key doesn't match: expected %v, got %v",
|
|
||||||
key, key2)
|
|
||||||
}
|
|
||||||
|
|
||||||
badpw := []byte("badweks")
|
badpw := []byte("badweks")
|
||||||
err = store.CreateUnlock(&badpw)
|
err = store.CreateUnlock(&badpw)
|
||||||
if err != macaroons.ErrAlreadyUnlocked {
|
require.Equal(t, macaroons.ErrAlreadyUnlocked, err)
|
||||||
t.Fatalf("Received %v instead of ErrAlreadyUnlocked", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
store.Close()
|
_ = store.Close()
|
||||||
|
|
||||||
// Between here and the re-opening of the store, it's possible to get
|
// 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
|
// a double-close, but that's not such a big deal since the tests will
|
||||||
@ -112,61 +88,35 @@ func TestStore(t *testing.T) {
|
|||||||
db, err = kvdb.Create(
|
db, err = kvdb.Create(
|
||||||
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
|
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
|
||||||
)
|
)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error opening store DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
store, err = macaroons.NewRootKeyStorage(db)
|
store, err = macaroons.NewRootKeyStorage(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.Close()
|
_ = db.Close()
|
||||||
t.Fatalf("Error creating root key store: %v", err)
|
t.Fatalf("Error creating root key store: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.CreateUnlock(&badpw)
|
err = store.CreateUnlock(&badpw)
|
||||||
if err != snacl.ErrInvalidPassword {
|
require.Equal(t, snacl.ErrInvalidPassword, err)
|
||||||
t.Fatalf("Received %v instead of ErrInvalidPassword", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = store.CreateUnlock(nil)
|
err = store.CreateUnlock(nil)
|
||||||
if err != macaroons.ErrPasswordRequired {
|
require.Equal(t, macaroons.ErrPasswordRequired, err)
|
||||||
t.Fatalf("Received %v instead of ErrPasswordRequired", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, err = store.RootKey(ctx)
|
_, _, err = store.RootKey(ctx)
|
||||||
if err != macaroons.ErrStoreLocked {
|
require.Equal(t, macaroons.ErrStoreLocked, err)
|
||||||
t.Fatalf("Received %v instead of ErrStoreLocked", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = store.Get(ctx, nil)
|
_, err = store.Get(ctx, nil)
|
||||||
if err != macaroons.ErrStoreLocked {
|
require.Equal(t, macaroons.ErrStoreLocked, err)
|
||||||
t.Fatalf("Received %v instead of ErrStoreLocked", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = store.CreateUnlock(&pw)
|
err = store.CreateUnlock(&pw)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error unlocking root key store: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
key, err = store.Get(ctx, rootID)
|
key, err = store.Get(ctx, rootID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error getting key with ID %s: %v",
|
require.Equal(t, key, key2)
|
||||||
string(rootID), err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(key, key2) {
|
|
||||||
t.Fatalf("Root key doesn't match: expected %v, got %v",
|
|
||||||
key2, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
key, id, err = store.RootKey(ctx)
|
key, id, err = store.RootKey(ctx)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error getting root key from store: %v", err)
|
require.Equal(t, key, key2)
|
||||||
}
|
require.Equal(t, rootID, id)
|
||||||
if !bytes.Equal(key, key2) {
|
|
||||||
t.Fatalf("Root key doesn't match: expected %v, got %v",
|
|
||||||
key2, key)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(rootID, id) {
|
|
||||||
t.Fatalf("Root ID doesn't match: expected %v, got %v",
|
|
||||||
rootID, id)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user