Skip to content

Commit 58b3d90

Browse files
committed
test: add more util/util.c tests
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent 396ab71 commit 58b3d90

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

test/stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy) {
288288
}
289289

290290
AP_DECLARE(int) ap_should_client_block(request_rec *r) {
291-
return 0;
291+
return 1;
292292
}
293293

294294
AP_DECLARE(int) ap_unescape_url(char *url) {

test/test_util.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,91 @@ START_TEST(test_util_table_and_hash_clear_and_openssl) {
840840
}
841841
END_TEST
842842

843+
START_TEST(test_util_read_form_encoded_params) {
844+
request_rec *r = oidc_test_request_get();
845+
apr_table_t *t = apr_table_make(r->pool, 4);
846+
847+
char *form = "a=1&b=two%20words&c=3";
848+
ck_assert_msg(oidc_util_read_form_encoded_params(r, t, form) == TRUE,
849+
"oidc_util_read_form_encoded_params returned FALSE");
850+
851+
ck_assert_str_eq(apr_table_get(t, "a"), "1");
852+
ck_assert_str_eq(apr_table_get(t, "b"), "two words");
853+
ck_assert_str_eq(apr_table_get(t, "c"), "3");
854+
}
855+
END_TEST
856+
857+
START_TEST(test_util_read_post_params_wrong_content_type) {
858+
request_rec *r = oidc_test_request_get();
859+
apr_table_t *t = apr_table_make(r->pool, 2);
860+
861+
r->method_number = M_GET;
862+
apr_table_set(r->headers_in, "Content-Type", "application/x-www-form-urlencoded");
863+
ck_assert_msg(oidc_util_read_post_params(r, t, FALSE, NULL) == FALSE,
864+
"oidc_util_read_post_params should return FALSE for non-POST method");
865+
866+
r->method_number = M_POST;
867+
apr_table_set(r->headers_in, "Content-Type", "application/json");
868+
ck_assert_msg(oidc_util_read_post_params(r, t, FALSE, NULL) == FALSE,
869+
"oidc_util_read_post_params should return FALSE for wrong content-type");
870+
}
871+
END_TEST
872+
873+
START_TEST(test_util_read_post_params_oversized) {
874+
request_rec *r = oidc_test_request_get();
875+
apr_table_t *t = apr_table_make(r->pool, 2);
876+
877+
r->method_number = M_POST;
878+
apr_table_set(r->headers_in, "Content-Type", "application/x-www-form-urlencoded");
879+
880+
r->remaining = (apr_size_t)(1024 * 1024 + 1);
881+
882+
ck_assert_msg(oidc_util_read_post_params(r, t, FALSE, NULL) == FALSE,
883+
"oidc_util_read_post_params should return FALSE for oversized POST body");
884+
}
885+
END_TEST
886+
887+
START_TEST(test_util_read_post_params) {
888+
request_rec *r = oidc_test_request_get();
889+
apr_table_t *t = apr_table_make(r->pool, 4);
890+
891+
r->method_number = M_POST;
892+
apr_table_set(r->headers_in, "Content-Type", "application/x-www-form-urlencoded");
893+
const char *form = "a=1&b=2";
894+
r->remaining = (apr_size_t)_oidc_strlen(form);
895+
r->args = apr_pstrdup(r->pool, form);
896+
897+
apr_byte_t rc = oidc_util_read_post_params(r, t, FALSE, NULL);
898+
ck_assert_msg(rc == TRUE, "oidc_util_read_post_params returned FALSE when propagate==FALSE");
899+
900+
apr_table_t *userdata_post_params = NULL;
901+
apr_pool_userdata_get((void **)&userdata_post_params, OIDC_USERDATA_POST_PARAMS_KEY, r->pool);
902+
ck_assert_ptr_null(userdata_post_params);
903+
904+
rc = oidc_util_read_post_params(r, t, TRUE, NULL);
905+
ck_assert_msg(rc == TRUE, "oidc_util_read_post_params returned FALSE when propagate==TRUE");
906+
}
907+
END_TEST
908+
909+
START_TEST(test_util_set_trace_parent_flags) {
910+
request_rec *r = oidc_test_request_get();
911+
oidc_cfg_t *c = oidc_test_cfg_get();
912+
913+
oidc_cmd_trace_parent_set(oidc_test_cmd_get(OIDCTraceParent), NULL, "generate");
914+
oidc_cmd_metrics_hook_data_set(oidc_test_cmd_get(OIDCMetricsData), NULL, "authn");
915+
916+
oidc_request_state_set(r, OIDC_REQUEST_STATE_TRACE_ID, NULL);
917+
oidc_util_set_trace_parent(r, c, NULL);
918+
919+
const char *tp = apr_table_get(r->headers_in, OIDC_HTTP_HDR_TRACE_PARENT);
920+
ck_assert_ptr_nonnull(tp);
921+
int len = _oidc_strlen(tp);
922+
ck_assert_msg(len >= 2, "traceparent header too short");
923+
ck_assert_msg(_oidc_strncmp(&tp[len - 2], "01", 2) == 0,
924+
"traceparent flags byte is not 01 when metrics hook is set");
925+
}
926+
END_TEST
927+
843928
int main(void) {
844929
TCase *c = NULL;
845930
Suite *s = suite_create("util");
@@ -912,9 +997,14 @@ int main(void) {
912997
c = tcase_create("util");
913998
tcase_add_checked_fixture(c, oidc_test_setup, oidc_test_teardown);
914999
tcase_add_test(c, test_util_strenv_and_casestr);
1000+
tcase_add_test(c, test_util_read_form_encoded_params);
1001+
tcase_add_test(c, test_util_read_post_params_wrong_content_type);
1002+
tcase_add_test(c, test_util_read_post_params_oversized);
1003+
tcase_add_test(c, test_util_read_post_params);
9151004
tcase_add_test(c, test_util_spaced_string_helpers);
9161005
tcase_add_test(c, test_util_hex_and_hash);
9171006
tcase_add_test(c, test_util_cookie_domain_and_issuer);
1007+
tcase_add_test(c, test_util_set_trace_parent_flags);
9181008
tcase_add_test(c, test_util_table_and_hash_clear_and_openssl);
9191009
suite_add_tcase(s, c);
9201010

0 commit comments

Comments
 (0)