Skip to content

Commit

Permalink
coverage: Rework some issues and add checks
Browse files Browse the repository at this point in the history
- 100% function coverage
- Allow key_ops we don't recognize to be ignored

Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Jan 9, 2025
1 parent 322546c commit 12c9530
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions libjwt/jwks.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ static jwk_key_op_t jwk_key_op_j(json_t *j_op)
const char *op;

if (!j_op || !json_is_string(j_op))
return JWK_KEY_OP_INVALID;
return JWK_KEY_OP_NONE;

op = json_string_value(j_op);

if (op == NULL)
return JWK_KEY_OP_INVALID;
return JWK_KEY_OP_NONE;

if (!jwt_strcmp(op, "sign"))
return JWK_KEY_OP_SIGN;
Expand Down
1 change: 1 addition & 0 deletions tests/jwt_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ START_TEST(test_jwt_ops)

type = jwt_get_crypto_ops_t();
ck_assert_int_eq(type, op->type);
ck_assert_int_ne(jwt_crypto_ops_supports_jwk(), 0);
}

/* Assert that this fails */
Expand Down
12 changes: 8 additions & 4 deletions tests/jwt_jwks.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ static void __jwks_check(const char *json, const char *pem)
ret = strcmp(jwks_item_pem(item), test_data.key);
free_key();
ck_assert_int_eq(ret, 0);

if (jwks_item_alg(item) == JWT_ALG_ES256) {
ck_assert_str_eq(jwks_item_curve(item), "P-256");
ck_assert_int_eq(jwks_item_kty(item), JWK_KEY_TYPE_EC);
ck_assert_int_eq(jwks_item_key_bits(item), 256);
}
}

/* Should only be one key in the set */
Expand Down Expand Up @@ -205,7 +211,6 @@ END_TEST
START_TEST(test_jwks_key_op_bad_type)
{
const jwk_item_t *item;
const char *msg = "JWK has an invalid value in key_op";
const char *kid = "264265c2-4ef0-4751-adbd-9739550afe5b";

SET_OPS();
Expand All @@ -215,9 +220,8 @@ START_TEST(test_jwks_key_op_bad_type)
item = jwks_item_get(g_jwk_set, 0);
ck_assert_ptr_nonnull(item);

/* One item had a bad type (numeric). */
ck_assert(jwks_item_error(item));
ck_assert_str_eq(jwks_item_error_msg(item), msg);
/* The bad key_op is ignored. */
ck_assert(!jwks_item_error(item));

/* Only these ops set. */
ck_assert_int_eq(jwks_item_key_ops(item),
Expand Down

0 comments on commit 12c9530

Please sign in to comment.