From 315beca0035801b86fca7d84b48cbb6f1dd867e6 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sat, 31 Dec 2022 12:24:48 +0000 Subject: [PATCH] Assert expected errors in tests --- claims_test.go | 8 ++++++++ keys_test.go | 4 ++++ token_test.go | 8 ++++++++ vectors_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/claims_test.go b/claims_test.go index ef27a05..9d8cd77 100644 --- a/claims_test.go +++ b/claims_test.go @@ -127,6 +127,8 @@ func TestFutureIat(t *testing.T) { _, err := parser.ParseV4Local(key, encrypted, nil) require.Error(t, err) + require.ErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) } func TestFutureNbf(t *testing.T) { @@ -146,6 +148,8 @@ func TestFutureNbf(t *testing.T) { _, err := parser.ParseV4Local(key, encrypted, nil) require.Error(t, err) + require.ErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) } func TestFutureNbfNotBeforeNbfRule(t *testing.T) { @@ -184,6 +188,8 @@ func TestFutureNbfNotBeforeNbfRuleError(t *testing.T) { _, err := parser.ParseV4Local(key, encrypted, nil) require.Error(t, err) + require.ErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) } func TestPastExp(t *testing.T) { @@ -203,6 +209,8 @@ func TestPastExp(t *testing.T) { _, err := parser.ParseV4Local(key, encrypted, nil) require.Error(t, err) + require.ErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) } func TestReadMeExample(t *testing.T) { diff --git a/keys_test.go b/keys_test.go index 3fb3b71..d65a199 100644 --- a/keys_test.go +++ b/keys_test.go @@ -12,6 +12,8 @@ func TestV2AsymmetricSecretKeyImport(t *testing.T) { _, err := paseto.NewV2AsymmetricSecretKeyFromHex(badKey) require.Error(t, err) + require.NotErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) goodKey := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2" @@ -24,6 +26,8 @@ func TestV4AsymmetricSecretKeyImport(t *testing.T) { _, err := paseto.NewV4AsymmetricSecretKeyFromHex(badKey) require.Error(t, err) + require.NotErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) goodKey := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2" diff --git a/token_test.go b/token_test.go index b31658c..1daf14e 100644 --- a/token_test.go +++ b/token_test.go @@ -29,6 +29,8 @@ func TestSomeInt(t *testing.T) { var output string err = token.Get("foo", &output) require.Error(t, err) + require.NotErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) var intOutput int err = token.Get("foo", &intOutput) @@ -46,6 +48,8 @@ func TestSomeBool(t *testing.T) { var intOutput int err = token.Get("foo", &intOutput) require.Error(t, err) + require.NotErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) var output bool err = token.Get("foo", &output) @@ -92,6 +96,8 @@ func TestSomeWrongType(t *testing.T) { var output bool err = token.Get("baz", &output) require.Error(t, err) + require.NotErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) } func TestSomeWrongKey(t *testing.T) { @@ -103,6 +109,8 @@ func TestSomeWrongKey(t *testing.T) { var output string err = token.Get("bar", &output) require.Error(t, err) + require.NotErrorIs(t, err, &paseto.RuleError{}) + require.NotErrorIs(t, err, &paseto.TokenError{}) } func TestFromMap(t *testing.T) { diff --git a/vectors_test.go b/vectors_test.go index 70ee28e..65c2d3c 100644 --- a/vectors_test.go +++ b/vectors_test.go @@ -50,6 +50,8 @@ func TestV2(t *testing.T) { message, err := paseto.NewMessage(paseto.V2Local, test.Token) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -57,6 +59,8 @@ func TestV2(t *testing.T) { decoded, err = paseto.V2LocalDecrypt(message, sk) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -69,6 +73,8 @@ func TestV2(t *testing.T) { message, err := paseto.NewMessage(paseto.V2Public, test.Token) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -76,6 +82,8 @@ func TestV2(t *testing.T) { decoded, err = paseto.V2PublicVerify(message, pk) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -135,6 +143,8 @@ func TestV3(t *testing.T) { message, err := paseto.NewMessage(paseto.V3Local, test.Token) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -142,6 +152,8 @@ func TestV3(t *testing.T) { decoded, err = paseto.V3LocalDecrypt(message, sk, []byte(test.ImplicitAssertation)) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -154,6 +166,8 @@ func TestV3(t *testing.T) { message, err := paseto.NewMessage(paseto.V3Public, test.Token) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -161,6 +175,8 @@ func TestV3(t *testing.T) { decoded, err = paseto.V3PublicVerify(message, pk, []byte(test.ImplicitAssertation)) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -235,6 +251,8 @@ func TestV4(t *testing.T) { message, err := paseto.NewMessage(paseto.V4Local, test.Token) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -242,6 +260,8 @@ func TestV4(t *testing.T) { decoded, err = paseto.V4LocalDecrypt(message, sk, []byte(test.ImplicitAssertation)) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -254,6 +274,8 @@ func TestV4(t *testing.T) { message, err := paseto.NewMessage(paseto.V4Public, test.Token) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err) @@ -261,6 +283,8 @@ func TestV4(t *testing.T) { decoded, err = paseto.V4PublicVerify(message, pk, []byte(test.ImplicitAssertation)) if test.ExpectFail { require.Error(t, err) + require.ErrorIs(t, err, &paseto.TokenError{}) + require.NotErrorIs(t, err, &paseto.RuleError{}) return } require.NoError(t, err)