Skip to content

Commit

Permalink
Play some coverage games (#425)
Browse files Browse the repository at this point in the history
* Play some coverage games

* remove leading whitespace

* work around tag-based feature
  • Loading branch information
lestrrat committed Aug 4, 2021
1 parent 322ba77 commit 917a1ab
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
16 changes: 16 additions & 0 deletions jwa/compression_gen_test.go
Expand Up @@ -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
}
})
}
20 changes: 20 additions & 0 deletions jwa/content_encryption_gen_test.go
Expand Up @@ -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
}
})
}
26 changes: 26 additions & 0 deletions jwa/elliptic_gen_test.go
Expand Up @@ -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
}
})
}
27 changes: 27 additions & 0 deletions jwa/internal/cmd/gentypes/main.go
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions jwa/key_encryption_gen_test.go
Expand Up @@ -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
}
})
}
18 changes: 18 additions & 0 deletions jwa/key_type_gen_test.go
Expand Up @@ -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
}
})
}
29 changes: 29 additions & 0 deletions jwa/signature_gen_test.go
Expand Up @@ -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
}
})
}
45 changes: 45 additions & 0 deletions jwx_test.go
Expand Up @@ -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
}
})
}
}

0 comments on commit 917a1ab

Please sign in to comment.