diff --git a/jwa/compression_gen_test.go b/jwa/compression_gen_test.go index b3fbeb29..1d60ecf7 100644 --- a/jwa/compression_gen_test.go +++ b/jwa/compression_gen_test.go @@ -97,4 +97,20 @@ func TestCompressionAlgorithm(t *testing.T) { return } }) + t.Run(`check list of elements`, func(t *testing.T) { + t.Parallel() + var expected = map[jwa.CompressionAlgorithm]struct{}{ + jwa.Deflate: {}, + jwa.NoCompress: {}, + } + for _, v := range jwa.CompressionAlgorithms() { + if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) { + return + } + delete(expected, v) + } + if !assert.Len(t, expected, 0) { + return + } + }) } diff --git a/jwa/content_encryption_gen_test.go b/jwa/content_encryption_gen_test.go index 0bdcaddc..b641d4ab 100644 --- a/jwa/content_encryption_gen_test.go +++ b/jwa/content_encryption_gen_test.go @@ -241,4 +241,24 @@ func TestContentEncryptionAlgorithm(t *testing.T) { return } }) + t.Run(`check list of elements`, func(t *testing.T) { + t.Parallel() + var expected = map[jwa.ContentEncryptionAlgorithm]struct{}{ + jwa.A128CBC_HS256: {}, + jwa.A128GCM: {}, + jwa.A192CBC_HS384: {}, + jwa.A192GCM: {}, + jwa.A256CBC_HS512: {}, + jwa.A256GCM: {}, + } + for _, v := range jwa.ContentEncryptionAlgorithms() { + if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) { + return + } + delete(expected, v) + } + if !assert.Len(t, expected, 0) { + return + } + }) } diff --git a/jwa/elliptic_gen_test.go b/jwa/elliptic_gen_test.go index 923f2910..4099847f 100644 --- a/jwa/elliptic_gen_test.go +++ b/jwa/elliptic_gen_test.go @@ -284,4 +284,30 @@ func TestEllipticCurveAlgorithm(t *testing.T) { return } }) + t.Run(`check list of elements`, func(t *testing.T) { + t.Parallel() + var expected = map[jwa.EllipticCurveAlgorithm]struct{}{ + jwa.Ed25519: {}, + jwa.Ed448: {}, + jwa.P256: {}, + jwa.P384: {}, + jwa.P521: {}, + jwa.X25519: {}, + jwa.X448: {}, + } + for _, v := range jwa.EllipticCurveAlgorithms() { + // There is no good way to detect from a test if es256k (secp256k1) + // is supported, so just allow it + if v.String() == `secp256k1` { + continue + } + if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) { + return + } + delete(expected, v) + } + if !assert.Len(t, expected, 0) { + return + } + }) } diff --git a/jwa/internal/cmd/gentypes/main.go b/jwa/internal/cmd/gentypes/main.go index 05417276..b814f36b 100644 --- a/jwa/internal/cmd/gentypes/main.go +++ b/jwa/internal/cmd/gentypes/main.go @@ -601,6 +601,33 @@ func (t typ) GenerateTest() error { fmt.Fprintf(&buf, "\n})") } + fmt.Fprintf(&buf, "\nt.Run(`check list of elements`, func(t *testing.T) {") + fmt.Fprintf(&buf, "\nt.Parallel()") + fmt.Fprintf(&buf, "\nvar expected = map[jwa.%s]struct{} {", t.name) + for _, e := range t.elements { + if !e.invalid { + fmt.Fprintf(&buf, "\njwa.%s: {},", e.name) + } + } + fmt.Fprintf(&buf, "\n}") + fmt.Fprintf(&buf, "\nfor _, v := range jwa.%ss() {", t.name) + if t.name == "EllipticCurveAlgorithm" { + fmt.Fprintf(&buf, "\n// There is no good way to detect from a test if es256k (secp256k1)") + fmt.Fprintf(&buf, "\n// is supported, so just allow it") + fmt.Fprintf(&buf, "\nif v.String() == `secp256k1` {") + fmt.Fprintf(&buf, "\ncontinue") + fmt.Fprintf(&buf, "\n}") + } + fmt.Fprintf(&buf, "\nif _, ok := expected[v]; !assert.True(t, ok, `%%s should be in the expected list`, v) {") + fmt.Fprintf(&buf, "\nreturn") + fmt.Fprintf(&buf, "\n}") + fmt.Fprintf(&buf, "\ndelete(expected, v)") + fmt.Fprintf(&buf, "\n}") + fmt.Fprintf(&buf, "\nif !assert.Len(t, expected, 0) {") + fmt.Fprintf(&buf, "\nreturn") + fmt.Fprintf(&buf, "\n}") + fmt.Fprintf(&buf, "\n})") + fmt.Fprintf(&buf, "\n}") filename := strings.Replace(t.filename, "_gen.go", "_gen_test.go", 1) diff --git a/jwa/key_encryption_gen_test.go b/jwa/key_encryption_gen_test.go index f01f5988..a40ad23d 100644 --- a/jwa/key_encryption_gen_test.go +++ b/jwa/key_encryption_gen_test.go @@ -691,4 +691,35 @@ func TestKeyEncryptionAlgorithm(t *testing.T) { assert.False(t, jwa.RSA_OAEP_256.IsSymmetric(), `jwa.RSA_OAEP_256 should NOT be symmetric`) }) }) + t.Run(`check list of elements`, func(t *testing.T) { + t.Parallel() + var expected = map[jwa.KeyEncryptionAlgorithm]struct{}{ + jwa.A128GCMKW: {}, + jwa.A128KW: {}, + jwa.A192GCMKW: {}, + jwa.A192KW: {}, + jwa.A256GCMKW: {}, + jwa.A256KW: {}, + jwa.DIRECT: {}, + jwa.ECDH_ES: {}, + jwa.ECDH_ES_A128KW: {}, + jwa.ECDH_ES_A192KW: {}, + jwa.ECDH_ES_A256KW: {}, + jwa.PBES2_HS256_A128KW: {}, + jwa.PBES2_HS384_A192KW: {}, + jwa.PBES2_HS512_A256KW: {}, + jwa.RSA1_5: {}, + jwa.RSA_OAEP: {}, + jwa.RSA_OAEP_256: {}, + } + for _, v := range jwa.KeyEncryptionAlgorithms() { + if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) { + return + } + delete(expected, v) + } + if !assert.Len(t, expected, 0) { + return + } + }) } diff --git a/jwa/key_type_gen_test.go b/jwa/key_type_gen_test.go index e3d399c6..33aa785b 100644 --- a/jwa/key_type_gen_test.go +++ b/jwa/key_type_gen_test.go @@ -176,4 +176,22 @@ func TestKeyType(t *testing.T) { return } }) + t.Run(`check list of elements`, func(t *testing.T) { + t.Parallel() + var expected = map[jwa.KeyType]struct{}{ + jwa.EC: {}, + jwa.OKP: {}, + jwa.OctetSeq: {}, + jwa.RSA: {}, + } + for _, v := range jwa.KeyTypes() { + if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) { + return + } + delete(expected, v) + } + if !assert.Len(t, expected, 0) { + return + } + }) } diff --git a/jwa/signature_gen_test.go b/jwa/signature_gen_test.go index ed4f0cad..8b304eba 100644 --- a/jwa/signature_gen_test.go +++ b/jwa/signature_gen_test.go @@ -565,4 +565,33 @@ func TestSignatureAlgorithm(t *testing.T) { return } }) + t.Run(`check list of elements`, func(t *testing.T) { + t.Parallel() + var expected = map[jwa.SignatureAlgorithm]struct{}{ + jwa.ES256: {}, + jwa.ES256K: {}, + jwa.ES384: {}, + jwa.ES512: {}, + jwa.EdDSA: {}, + jwa.HS256: {}, + jwa.HS384: {}, + jwa.HS512: {}, + jwa.NoSignature: {}, + jwa.PS256: {}, + jwa.PS384: {}, + jwa.PS512: {}, + jwa.RS256: {}, + jwa.RS384: {}, + jwa.RS512: {}, + } + for _, v := range jwa.SignatureAlgorithms() { + if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) { + return + } + delete(expected, v) + } + if !assert.Len(t, expected, 0) { + return + } + }) } diff --git a/jwx_test.go b/jwx_test.go index 2e660bc3..f166a3bb 100644 --- a/jwx_test.go +++ b/jwx_test.go @@ -479,3 +479,48 @@ func TestGuessFormat(t *testing.T) { }) } } + +func TestFormat(t *testing.T) { + testcases := []struct { + Value jwx.FormatKind + Expected string + Error bool + }{ + { + Value: jwx.UnknownFormat, + Expected: "UnknownFormat", + }, + { + Value: jwx.JWE, + Expected: "JWE", + }, + { + Value: jwx.JWS, + Expected: "JWS", + }, + { + Value: jwx.JWK, + Expected: "JWK", + }, + { + Value: jwx.JWKS, + Expected: "JWKS", + }, + { + Value: jwx.JWT, + Expected: "JWT", + }, + { + Value: jwx.FormatKind(9999999), + Expected: "FormatKind(9999999)", + }, + } + for _, tc := range testcases { + tc := tc + t.Run(tc.Expected, func(t *testing.T) { + if !assert.Equal(t, tc.Expected, tc.Value.String(), `stringification should match`) { + return + } + }) + } +}