Skip to content

Commit

Permalink
jwt_tests: Create macro for common main function in most tests
Browse files Browse the repository at this point in the history
Tired of editing this same code 10 times. Bring it all under
macro to save my sanity.

Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Dec 9, 2024
1 parent a032896 commit 7e35485
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 272 deletions.
28 changes: 1 addition & 27 deletions tests/jwt_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,31 +373,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT Dump - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT Dump");
}
28 changes: 1 addition & 27 deletions tests/jwt_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,31 +319,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT EC Sign/Verify - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT EC Sign/Verify");
}
28 changes: 1 addition & 27 deletions tests/jwt_eddsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,31 +182,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT EdDSA Sign/Verify - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT EdDSA Sign/Verify");
}
28 changes: 1 addition & 27 deletions tests/jwt_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,31 +392,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT Encode - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT Encode");
}
28 changes: 1 addition & 27 deletions tests/jwt_grant.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT Grant - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT Grant");
}
28 changes: 1 addition & 27 deletions tests/jwt_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT Header - %s", jwt_test_ops[i]) < 1)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT Header");
}
28 changes: 1 addition & 27 deletions tests/jwt_new.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,31 +484,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT New - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT New");
}
28 changes: 1 addition & 27 deletions tests/jwt_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,31 +402,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT RSA Sign/Verify - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT RSA Sign/Verify");
}
28 changes: 1 addition & 27 deletions tests/jwt_rsa_pss.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT RSA-PSS Sign/Verify - %s", jwt_test_ops[i]) < 0)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT RSA-PSS Sign/Verify");
}
30 changes: 30 additions & 0 deletions tests/jwt_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,34 @@ static const char *jwt_test_ops[] = {
NULL
};

#define JWT_TEST_MAIN(__title) ({ \
int number_failed = 0; \
int i; \
\
for (i = 0; jwt_test_ops[i] != NULL; i++) { \
SRunner *sr; \
Suite *s; \
char *title; \
const char *name = jwt_test_ops[i]; \
\
if (jwt_set_crypto_ops(name)) \
continue; \
\
if (asprintf(&title, __title " - %s", jwt_test_ops[i]) < 0) \
exit(1); \
\
/* Set this because we fork */ \
setenv("JWT_CRYPTO", name, 1); \
\
s = libjwt_suite(title); \
sr = srunner_create(s); \
\
srunner_run_all(sr, CK_VERBOSE); \
number_failed += srunner_ntests_failed(sr); \
srunner_free(sr); \
} \
\
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; \
})

#endif /* JWT_TESTS_H */
32 changes: 3 additions & 29 deletions tests/jwt_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,9 @@ static Suite *libjwt_suite(const char *title)
Suite *s;
TCase *tc_core;

s = suite_create("LibJWT Grant");
s = suite_create(title);

tc_core = tcase_create("jwt_grant");
tc_core = tcase_create("jwt_validate");

tcase_add_test(tc_core, test_jwt_validate_errno);
tcase_add_test(tc_core, test_jwt_valid_algorithm);
Expand Down Expand Up @@ -676,31 +676,5 @@ static Suite *libjwt_suite(const char *title)

int main(int argc, char *argv[])
{
int number_failed = 0;
Suite *s;
SRunner *sr;
int i;

for (i = 0; jwt_test_ops[i] != NULL; i++) {
char *title;
const char *name = jwt_test_ops[i];

if (jwt_set_crypto_ops(name))
continue;

if (asprintf(&title, "LibJWT EC Sign/Verify - %s", jwt_test_ops[i]) < 1)
exit(1);

/* Set this because we fork */
setenv("JWT_CRYPTO", name, 1);

s = libjwt_suite(title);
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
number_failed += srunner_ntests_failed(sr);
srunner_free(sr);
}

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
JWT_TEST_MAIN("LibJWT Validate");
}

0 comments on commit 7e35485

Please sign in to comment.