Skip to content

Commit 0738d50

Browse files
committed
test: complete util/expr.c coverage
use apr_pstrndup in oidc_pcre_subst and don't allocate a zero length result Signed-off-by: Hans Zandbelt <[email protected]>
1 parent 0b0347b commit 0738d50

File tree

5 files changed

+144
-4
lines changed

5 files changed

+144
-4
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
09/03/2025
2+
- test: complete util/expr.c coverage
3+
14
08/31/2025
25
- pass JSON real claims without trailing zeros, use 8 digits precision instead of 6
36
- test: complete util/appinfo.c coverage

src/util/pcre_subst.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,10 @@ char *oidc_pcre_subst(apr_pool_t *pool, const struct oidc_pcre *pcre, const char
184184
PCRE2_SIZE length = (PCRE2_SIZE)len;
185185
PCRE2_SPTR replacement = (PCRE2_SPTR)rep;
186186
if (pcre2_substitute(pcre->preg, subject, length, 0, PCRE2_SUBSTITUTE_GLOBAL, 0, 0, replacement,
187-
PCRE2_ZERO_TERMINATED, output, &outlen) > 0)
188-
rv = apr_pstrdup(pool, (const char *)output);
187+
PCRE2_ZERO_TERMINATED, output, &outlen) > 0) {
188+
if (outlen > 0)
189+
rv = apr_pstrndup(pool, (const char *)output, outlen);
190+
}
189191
free(output);
190192
#else
191193
char *substituted = NULL;

test/helper.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ cmd_parms *oidc_test_cmd_get(const char *primitive) {
150150
cmd_parms *cmd = apr_pcalloc(r->pool, sizeof(cmd_parms));
151151
cmd->server = r->server;
152152
cmd->pool = r->pool;
153+
cmd->temp_pool = r->pool;
153154
cmd->directive = apr_pcalloc(cmd->pool, sizeof(ap_directive_t));
154155
cmd->directive->directive = primitive;
155156
return cmd;

test/stub.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ AP_DECLARE(int) ap_is_initial_req(request_rec *r) {
177177
AP_DECLARE(ap_expr_info_t *)
178178
ap_expr_parse_cmd_mi(const cmd_parms *cmd, const char *expr, unsigned int flags, const char **err,
179179
ap_expr_lookup_fn_t *lookup_fn, int module_index) {
180-
return NULL;
180+
if (strcmp(expr, "#") == 0) {
181+
*err = "error";
182+
return NULL;
183+
}
184+
ap_expr_info_t *rv = apr_pcalloc(cmd->pool, sizeof(ap_expr_info_t));
185+
rv->filename = "stub.c";
186+
rv->root_node = NULL;
187+
return rv;
181188
}
182189

183190
AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r, const ap_expr_info_t *expr, const char **err) {
@@ -357,6 +364,7 @@ ap_timeout_parameter_parse(const char *timeout_parameter, apr_interval_time_t *t
357364

358365
#if HAVE_APACHE_24
359366
AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr, const char **err) {
360-
return 0;
367+
*err = "error";
368+
return 1;
361369
}
362370
#endif

test/test_util.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,127 @@ START_TEST(test_util_appinfo_set) {
170170
}
171171
END_TEST
172172

173+
START_TEST(test_util_expr_substitute) {
174+
apr_byte_t rc = FALSE;
175+
apr_pool_t *pool = oidc_test_pool_get();
176+
const char *input = "match 292 numbers";
177+
const char *regexp = "^.* ([0-9]+).*$";
178+
const char *replace = "$1";
179+
char *output = NULL;
180+
char *error_str = NULL;
181+
182+
rc = oidc_util_regexp_substitute(pool, input, "$$$$$**@@", replace, &output, &error_str);
183+
ck_assert_msg(rc == FALSE, "oidc_util_regexp_substitute returned TRUE");
184+
ck_assert_ptr_nonnull(error_str);
185+
186+
error_str = NULL;
187+
rc = oidc_util_regexp_substitute(
188+
pool,
189+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
190+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
191+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
192+
regexp, replace, &output, &error_str);
193+
ck_assert_msg(rc == FALSE, "oidc_util_regexp_substitute returned TRUE");
194+
ck_assert_ptr_nonnull(error_str);
195+
196+
error_str = NULL;
197+
rc = oidc_util_regexp_substitute(pool, "", "", "", &output, &error_str);
198+
ck_assert_msg(rc == FALSE, "oidc_util_regexp_substitute returned TRUE");
199+
ck_assert_ptr_nonnull(error_str);
200+
201+
error_str = NULL;
202+
rc = oidc_util_regexp_substitute(pool, input, regexp, replace, &output, &error_str);
203+
ck_assert_msg(rc == TRUE, "oidc_util_regexp_substitute returned FALSE");
204+
ck_assert_ptr_null(error_str);
205+
ck_assert_str_eq(output, "292");
206+
}
207+
END_TEST
208+
209+
START_TEST(test_util_expr_first_match) {
210+
apr_byte_t rc = FALSE;
211+
apr_pool_t *pool = oidc_test_pool_get();
212+
const char *input = "12345 hello";
213+
const char *regexp = "^([0-9]+)\\s+([a-z]+)$";
214+
;
215+
char *output = NULL;
216+
char *error_str = NULL;
217+
218+
rc = oidc_util_regexp_first_match(pool, input, "$$$$$**@@", &output, &error_str);
219+
ck_assert_msg(rc == FALSE, "oidc_util_regexp_first_match returned TRUE");
220+
ck_assert_ptr_nonnull(error_str);
221+
222+
error_str = NULL;
223+
rc = oidc_util_regexp_first_match(pool, "abc", regexp, &output, &error_str);
224+
ck_assert_msg(rc == FALSE, "oidc_util_regexp_first_match returned TRUE");
225+
ck_assert_ptr_nonnull(error_str);
226+
227+
error_str = NULL;
228+
rc = oidc_util_regexp_first_match(pool, "abc abc", regexp, &output, &error_str);
229+
ck_assert_msg(rc == FALSE, "oidc_util_regexp_first_match returned TRUE");
230+
ck_assert_ptr_nonnull(error_str);
231+
232+
error_str = NULL;
233+
rc = oidc_util_regexp_first_match(pool, input, regexp, &output, &error_str);
234+
ck_assert_msg(rc == TRUE, "oidc_util_regexp_first_match returned FALSE");
235+
ck_assert_ptr_null(error_str);
236+
ck_assert_str_eq(output, "12345");
237+
}
238+
END_TEST
239+
240+
START_TEST(test_util_expr_parse) {
241+
char *rv = NULL;
242+
cmd_parms *cmd = oidc_test_cmd_get("");
243+
oidc_apr_expr_t *expr = NULL;
244+
245+
// NB: stub only
246+
247+
expr = NULL;
248+
rv = oidc_util_apr_expr_parse(cmd, NULL, &expr, FALSE);
249+
ck_assert_ptr_null(rv);
250+
ck_assert_ptr_null(expr);
251+
252+
// expr = NULL;
253+
// rv = oidc_util_apr_expr_parse(cmd, "% ||| true)", &expr, FALSE);
254+
// ck_assert_ptr_nonnull(rv);
255+
// ck_assert_ptr_null(expr);
256+
257+
expr = NULL;
258+
rv = oidc_util_apr_expr_parse(cmd, "", &expr, TRUE);
259+
ck_assert_ptr_null(rv);
260+
ck_assert_ptr_nonnull(expr);
261+
}
262+
END_TEST
263+
264+
START_TEST(test_util_expr_exec) {
265+
const char *result = NULL;
266+
char *rv = NULL;
267+
cmd_parms *cmd = oidc_test_cmd_get("");
268+
request_rec *r = oidc_test_request_get();
269+
oidc_apr_expr_t *expr = NULL;
270+
271+
// NB: stub only
272+
expr = NULL;
273+
rv = oidc_util_apr_expr_parse(cmd, "true", &expr, FALSE);
274+
ck_assert_ptr_null(rv);
275+
ck_assert_ptr_nonnull(expr);
276+
277+
// NB: stub only
278+
result = oidc_util_apr_expr_exec(r, expr, TRUE);
279+
ck_assert_ptr_nonnull(result);
280+
ck_assert_str_eq(result, "stub.c");
281+
282+
// NB: stub only
283+
result = oidc_util_apr_expr_exec(r, expr, FALSE);
284+
ck_assert_ptr_null(result);
285+
286+
// NB: stub only
287+
expr = NULL;
288+
rv = oidc_util_apr_expr_parse(cmd, "#", &expr, FALSE);
289+
ck_assert_ptr_nonnull(rv);
290+
ck_assert_ptr_null(expr);
291+
}
292+
END_TEST
293+
173294
int main(void) {
174295
TCase *core = tcase_create("base64");
175296
tcase_add_checked_fixture(core, oidc_test_setup, oidc_test_teardown);
@@ -180,6 +301,11 @@ int main(void) {
180301

181302
tcase_add_test(core, test_util_appinfo_set);
182303

304+
tcase_add_test(core, test_util_expr_substitute);
305+
tcase_add_test(core, test_util_expr_first_match);
306+
tcase_add_test(core, test_util_expr_parse);
307+
tcase_add_test(core, test_util_expr_exec);
308+
183309
Suite *s = suite_create("util");
184310
suite_add_tcase(s, core);
185311

0 commit comments

Comments
 (0)