multi: move many t.Fatalf calls to require.NoError

This commit is contained in:
Tommy Volk
2022-05-05 20:11:50 +00:00
parent 9e6f0ef46b
commit 9a10c80bcb
92 changed files with 1905 additions and 5565 deletions

View File

@@ -23,9 +23,7 @@ func createDummyMacaroon(t *testing.T) *macaroon.Macaroon {
dummyMacaroon, err := macaroon.New(
testRootKey, testID, testLocation, testVersion,
)
if err != nil {
t.Fatalf("Error creating initial macaroon: %v", err)
}
require.NoError(t, err, "Error creating initial macaroon")
return dummyMacaroon
}
@@ -41,9 +39,7 @@ func TestAddConstraints(t *testing.T) {
newMac, err := macaroons.AddConstraints(
initialMac, macaroons.TimeoutConstraint(1),
)
if err != nil {
t.Fatalf("Error adding constraint: %v", err)
}
require.NoError(t, err, "Error adding constraint")
if &newMac == &initialMac {
t.Fatalf("Initial macaroon has been changed, something " +
"went wrong!")
@@ -66,9 +62,7 @@ func TestTimeoutConstraint(t *testing.T) {
// function to.
testMacaroon := createDummyMacaroon(t)
err := constraintFunc(testMacaroon)
if err != nil {
t.Fatalf("Error applying timeout constraint: %v", err)
}
require.NoError(t, err, "Error applying timeout constraint")
// Finally, check that the created caveat has an
// acceptable value.
@@ -92,9 +86,7 @@ func TestIpLockConstraint(t *testing.T) {
// function to.
testMacaroon := createDummyMacaroon(t)
err := constraintFunc(testMacaroon)
if err != nil {
t.Fatalf("Error applying timeout constraint: %v", err)
}
require.NoError(t, err, "Error applying timeout constraint")
// Finally, check that the created caveat has an
// acceptable value.

View File

@@ -35,16 +35,12 @@ var (
// and read the store on its own.
func setupTestRootKeyStorage(t *testing.T) (string, kvdb.Backend) {
tempDir, err := ioutil.TempDir("", "macaroonstore-")
if err != nil {
t.Fatalf("Error creating temp dir: %v", err)
}
require.NoError(t, err, "Error creating temp dir")
db, err := kvdb.Create(
kvdb.BoltBackendName, path.Join(tempDir, "macaroons.db"), true,
kvdb.DefaultDBTimeout,
)
if err != nil {
t.Fatalf("Error opening store DB: %v", err)
}
require.NoError(t, err, "Error opening store DB")
store, err := macaroons.NewRootKeyStorage(db)
if err != nil {
db.Close()
@@ -52,9 +48,7 @@ func setupTestRootKeyStorage(t *testing.T) (string, kvdb.Backend) {
}
defer store.Close()
err = store.CreateUnlock(&defaultPw)
if err != nil {
t.Fatalf("error creating unlock: %v", err)
}
require.NoError(t, err, "error creating unlock")
return tempDir, db
}
@@ -70,14 +64,10 @@ func TestNewService(t *testing.T) {
service, err := macaroons.NewService(
db, "lnd", false, macaroons.IPLockChecker,
)
if err != nil {
t.Fatalf("Error creating new service: %v", err)
}
require.NoError(t, err, "Error creating new service")
defer service.Close()
err = service.CreateUnlock(&defaultPw)
if err != nil {
t.Fatalf("Error unlocking root key storage: %v", err)
}
require.NoError(t, err, "Error unlocking root key storage")
// Third, check if the created service can bake macaroons.
_, err = service.NewMacaroon(context.TODO(), nil, testOperation)
@@ -88,9 +78,7 @@ func TestNewService(t *testing.T) {
macaroon, err := service.NewMacaroon(
context.TODO(), macaroons.DefaultRootKeyID, testOperation,
)
if err != nil {
t.Fatalf("Error creating macaroon from service: %v", err)
}
require.NoError(t, err, "Error creating macaroon from service")
if macaroon.Namespace().String() != "std:" {
t.Fatalf("The created macaroon has an invalid namespace: %s",
macaroon.Namespace().String())
@@ -121,28 +109,20 @@ func TestValidateMacaroon(t *testing.T) {
service, err := macaroons.NewService(
db, "lnd", false, macaroons.IPLockChecker,
)
if err != nil {
t.Fatalf("Error creating new service: %v", err)
}
require.NoError(t, err, "Error creating new service")
defer service.Close()
err = service.CreateUnlock(&defaultPw)
if err != nil {
t.Fatalf("Error unlocking root key storage: %v", err)
}
require.NoError(t, err, "Error unlocking root key storage")
// Then, create a new macaroon that we can serialize.
macaroon, err := service.NewMacaroon(
context.TODO(), macaroons.DefaultRootKeyID, testOperation,
testOperationURI,
)
if err != nil {
t.Fatalf("Error creating macaroon from service: %v", err)
}
require.NoError(t, err, "Error creating macaroon from service")
macaroonBinary, err := macaroon.M().MarshalBinary()
if err != nil {
t.Fatalf("Error serializing macaroon: %v", err)
}
require.NoError(t, err, "Error serializing macaroon")
// Because the macaroons are always passed in a context, we need to
// mock one that has just the serialized macaroon as a value.
@@ -155,18 +135,14 @@ func TestValidateMacaroon(t *testing.T) {
err = service.ValidateMacaroon(
mockContext, []bakery.Op{testOperation}, "FooMethod",
)
if err != nil {
t.Fatalf("Error validating the macaroon: %v", err)
}
require.NoError(t, err, "Error validating the macaroon")
// If the macaroon has the method specific URI permission, the list of
// required entity/action pairs is irrelevant.
err = service.ValidateMacaroon(
mockContext, []bakery.Op{{Entity: "irrelevant"}}, "SomeMethod",
)
if err != nil {
t.Fatalf("Error validating the macaroon: %v", err)
}
require.NoError(t, err, "Error validating the macaroon")
}
// TestListMacaroonIDs checks that ListMacaroonIDs returns the expected result.