Skip to content

Commit

Permalink
jq: extract oauth2_jq_filter_compile for config testing purposes
Browse files Browse the repository at this point in the history
Signed-off-by: Hans Zandbelt <[email protected]>
  • Loading branch information
zandbelt committed Sep 11, 2024
1 parent b8a7e09 commit aa8546a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions include/oauth2/jq.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "oauth2/cache.h"
#include "oauth2/log.h"

typedef struct jq_state jq_state;

bool oauth2_jq_filter_compile(oauth2_log_t *log, const char *filter,
jq_state **r_jq);
bool oauth2_jq_filter(oauth2_log_t *log, oauth2_cache_t *cache,
const char *input, const char *filter, char **result);

Expand Down
42 changes: 33 additions & 9 deletions src/jq.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ static int oauth2_jq_filter_cache_ttl(oauth2_log_t *log)
return OAUTH2_JQ_FILTER_EXPIRE_DEFAULT;
}

bool oauth2_jq_filter_compile(oauth2_log_t *log, const char *filter,
jq_state **r_jq)
{

bool rc = false;

jq_state *jq = jq_init();
if (jq == NULL) {
oauth2_error(log, "jq_init failed");
goto end;
}

if (jq_compile(jq, filter) == 0) {
oauth2_error(log, "jq_compile failed");
goto end;
}

if (r_jq != NULL)
*r_jq = jq;

rc = true;

end:

if ((rc == false) || (r_jq == NULL)) {
if (jq != NULL)
jq_teardown(&jq);
}

return rc;
}

bool oauth2_jq_filter(oauth2_log_t *log, oauth2_cache_t *cache,
const char *input, const char *filter, char **result)
{
Expand Down Expand Up @@ -105,16 +137,8 @@ bool oauth2_jq_filter(oauth2_log_t *log, oauth2_cache_t *cache,
}
}

jq = jq_init();
if (jq == NULL) {
oauth2_error(log, "jq_init returned NULL");
if (oauth2_jq_filter_compile(log, filter, &jq) == false)
goto end;
}

if (jq_compile(jq, filter) == 0) {
oauth2_error(log, "jq_compile returned an error");
goto end;
}

parser = jv_parser_new(0);
if (parser == NULL) {
Expand Down
16 changes: 14 additions & 2 deletions test/check_jq.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ static void teardown(void)
oauth2_shutdown(_log);
}

START_TEST(test_jq)
START_TEST(test_jq_compile)
{
bool rc = false;

rc = oauth2_jq_filter_compile(_log, ".add + 1", NULL);
ck_assert_int_eq(rc, true);

rc = oauth2_jq_filter_compile(_log, "bla", NULL);
ck_assert_int_eq(rc, false);
}

START_TEST(test_jq_filter)
{
bool rc = false;
oauth2_cache_t *c = NULL;
Expand Down Expand Up @@ -73,7 +84,8 @@ Suite *oauth2_check_jq_suite()

tcase_add_checked_fixture(c, setup, teardown);

tcase_add_test(c, test_jq);
tcase_add_test(c, test_jq_compile);
tcase_add_test(c, test_jq_filter);

suite_add_tcase(s, c);

Expand Down

0 comments on commit aa8546a

Please sign in to comment.