Skip to content

Commit aa8546a

Browse files
committed
jq: extract oauth2_jq_filter_compile for config testing purposes
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent b8a7e09 commit aa8546a

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

include/oauth2/jq.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "oauth2/cache.h"
2525
#include "oauth2/log.h"
2626

27+
typedef struct jq_state jq_state;
28+
29+
bool oauth2_jq_filter_compile(oauth2_log_t *log, const char *filter,
30+
jq_state **r_jq);
2731
bool oauth2_jq_filter(oauth2_log_t *log, oauth2_cache_t *cache,
2832
const char *input, const char *filter, char **result);
2933

src/jq.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ static int oauth2_jq_filter_cache_ttl(oauth2_log_t *log)
6464
return OAUTH2_JQ_FILTER_EXPIRE_DEFAULT;
6565
}
6666

67+
bool oauth2_jq_filter_compile(oauth2_log_t *log, const char *filter,
68+
jq_state **r_jq)
69+
{
70+
71+
bool rc = false;
72+
73+
jq_state *jq = jq_init();
74+
if (jq == NULL) {
75+
oauth2_error(log, "jq_init failed");
76+
goto end;
77+
}
78+
79+
if (jq_compile(jq, filter) == 0) {
80+
oauth2_error(log, "jq_compile failed");
81+
goto end;
82+
}
83+
84+
if (r_jq != NULL)
85+
*r_jq = jq;
86+
87+
rc = true;
88+
89+
end:
90+
91+
if ((rc == false) || (r_jq == NULL)) {
92+
if (jq != NULL)
93+
jq_teardown(&jq);
94+
}
95+
96+
return rc;
97+
}
98+
6799
bool oauth2_jq_filter(oauth2_log_t *log, oauth2_cache_t *cache,
68100
const char *input, const char *filter, char **result)
69101
{
@@ -105,16 +137,8 @@ bool oauth2_jq_filter(oauth2_log_t *log, oauth2_cache_t *cache,
105137
}
106138
}
107139

108-
jq = jq_init();
109-
if (jq == NULL) {
110-
oauth2_error(log, "jq_init returned NULL");
140+
if (oauth2_jq_filter_compile(log, filter, &jq) == false)
111141
goto end;
112-
}
113-
114-
if (jq_compile(jq, filter) == 0) {
115-
oauth2_error(log, "jq_compile returned an error");
116-
goto end;
117-
}
118142

119143
parser = jv_parser_new(0);
120144
if (parser == NULL) {

test/check_jq.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ static void teardown(void)
3636
oauth2_shutdown(_log);
3737
}
3838

39-
START_TEST(test_jq)
39+
START_TEST(test_jq_compile)
40+
{
41+
bool rc = false;
42+
43+
rc = oauth2_jq_filter_compile(_log, ".add + 1", NULL);
44+
ck_assert_int_eq(rc, true);
45+
46+
rc = oauth2_jq_filter_compile(_log, "bla", NULL);
47+
ck_assert_int_eq(rc, false);
48+
}
49+
50+
START_TEST(test_jq_filter)
4051
{
4152
bool rc = false;
4253
oauth2_cache_t *c = NULL;
@@ -73,7 +84,8 @@ Suite *oauth2_check_jq_suite()
7384

7485
tcase_add_checked_fixture(c, setup, teardown);
7586

76-
tcase_add_test(c, test_jq);
87+
tcase_add_test(c, test_jq_compile);
88+
tcase_add_test(c, test_jq_filter);
7789

7890
suite_add_tcase(s, c);
7991

0 commit comments

Comments
 (0)