macaroons: use require for tests

This commit is contained in:
Oliver Gugger 2020-10-06 17:23:29 +02:00
parent 10673b9dfa
commit cd85e17b19
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

@ -1,7 +1,6 @@
package macaroons_test
import (
"bytes"
"context"
"io/ioutil"
"os"
@ -12,99 +11,76 @@ import (
"github.com/lightningnetwork/lnd/macaroons"
"github.com/btcsuite/btcwallet/snacl"
"github.com/stretchr/testify/require"
)
func TestStore(t *testing.T) {
tempDir, err := ioutil.TempDir("", "macaroonstore-")
if err != nil {
t.Fatalf("Error creating temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
db, err := kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
)
if err != nil {
t.Fatalf("Error opening store DB: %v", err)
}
require.NoError(t, err)
store, err := macaroons.NewRootKeyStorage(db)
if err != nil {
db.Close()
_ = db.Close()
t.Fatalf("Error creating root key store: %v", err)
}
defer store.Close()
defer func() {
_ = store.Close()
}()
_, _, err = store.RootKey(context.TODO())
if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err)
}
require.Equal(t, macaroons.ErrStoreLocked, err)
_, err = store.Get(context.TODO(), nil)
if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err)
}
require.Equal(t, macaroons.ErrStoreLocked, err)
pw := []byte("weks")
err = store.CreateUnlock(&pw)
if err != nil {
t.Fatalf("Error creating store encryption key: %v", err)
}
require.NoError(t, err)
// Check ErrContextRootKeyID is returned when no root key ID found in
// context.
_, _, err = store.RootKey(context.TODO())
if err != macaroons.ErrContextRootKeyID {
t.Fatalf("Received %v instead of ErrContextRootKeyID", err)
}
require.Equal(t, macaroons.ErrContextRootKeyID, err)
// Check ErrMissingRootKeyID is returned when empty root key ID is used.
emptyKeyID := []byte{}
emptyKeyID := make([]byte, 0)
badCtx := macaroons.ContextWithRootKeyID(context.TODO(), emptyKeyID)
_, _, err = store.RootKey(badCtx)
if err != macaroons.ErrMissingRootKeyID {
t.Fatalf("Received %v instead of ErrMissingRootKeyID", err)
}
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)
if err != macaroons.ErrKeyValueForbidden {
t.Fatalf("Received %v instead of ErrKeyValueForbidden", err)
}
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)
if err != nil {
t.Fatalf("Error getting root key from store: %v", err)
}
require.NoError(t, err)
rootID := id
if !bytes.Equal(rootID, macaroons.DefaultRootKeyID) {
t.Fatalf("Root key ID doesn't match: expected %v, got %v",
macaroons.DefaultRootKeyID, rootID)
}
require.Equal(t, macaroons.DefaultRootKeyID, rootID)
key2, err := store.Get(ctx, id)
if err != nil {
t.Fatalf("Error getting key with ID %s: %v", string(id), err)
}
if !bytes.Equal(key, key2) {
t.Fatalf("Root key doesn't match: expected %v, got %v",
key, key2)
}
require.NoError(t, err)
require.Equal(t, key, key2)
badpw := []byte("badweks")
err = store.CreateUnlock(&badpw)
if err != macaroons.ErrAlreadyUnlocked {
t.Fatalf("Received %v instead of ErrAlreadyUnlocked", err)
}
require.Equal(t, macaroons.ErrAlreadyUnlocked, err)
store.Close()
_ = store.Close()
// 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
@ -112,61 +88,35 @@ func TestStore(t *testing.T) {
db, err = kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true,
)
if err != nil {
t.Fatalf("Error opening store DB: %v", err)
}
require.NoError(t, err)
store, err = macaroons.NewRootKeyStorage(db)
if err != nil {
db.Close()
_ = db.Close()
t.Fatalf("Error creating root key store: %v", err)
}
err = store.CreateUnlock(&badpw)
if err != snacl.ErrInvalidPassword {
t.Fatalf("Received %v instead of ErrInvalidPassword", err)
}
require.Equal(t, snacl.ErrInvalidPassword, err)
err = store.CreateUnlock(nil)
if err != macaroons.ErrPasswordRequired {
t.Fatalf("Received %v instead of ErrPasswordRequired", err)
}
require.Equal(t, macaroons.ErrPasswordRequired, err)
_, _, err = store.RootKey(ctx)
if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err)
}
require.Equal(t, macaroons.ErrStoreLocked, err)
_, err = store.Get(ctx, nil)
if err != macaroons.ErrStoreLocked {
t.Fatalf("Received %v instead of ErrStoreLocked", err)
}
require.Equal(t, macaroons.ErrStoreLocked, err)
err = store.CreateUnlock(&pw)
if err != nil {
t.Fatalf("Error unlocking root key store: %v", err)
}
require.NoError(t, err)
key, err = store.Get(ctx, rootID)
if err != nil {
t.Fatalf("Error getting key with ID %s: %v",
string(rootID), err)
}
if !bytes.Equal(key, key2) {
t.Fatalf("Root key doesn't match: expected %v, got %v",
key2, key)
}
require.NoError(t, err)
require.Equal(t, key, key2)
key, id, err = store.RootKey(ctx)
if err != nil {
t.Fatalf("Error getting root key from store: %v", err)
}
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)
}
require.NoError(t, err)
require.Equal(t, key, key2)
require.Equal(t, rootID, id)
}