Coverage Report

Created: 2026-12-01 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mod_auth_openidc/test/stub.c
Line
Count
Source
1
#include "util/request_state.h"
2
#include <apr_global_mutex.h>
3
#include <apr_lib.h>
4
#include <apr_strings.h>
5
6
// clang-format off
7
8
#include <httpd.h>
9
#include <http_config.h>
10
#include <http_core.h>
11
#include <http_log.h>
12
13
#ifdef WIN32
14
/* os/win32/os.h maps every exit() onto this server variable, so any test program that includes
15
 * the httpd headers references it; set, the real exit code passes through */
16
int ap_real_exit_code = 1;
17
#endif
18
19
// clang-format on
20
21
/* NB: deliberately not "util.h": including it here pulls in <http_request.h>, whose AP_DECLARE_HOOK
22
 * declarations collide with the hand-rolled ap_hook_* shims below. These mirror the declarations in
23
 * util.h instead; the two must stay in step. */
24
typedef int (*oidc_test_hook_post_config_fn)(apr_pool_t *pool, apr_pool_t *p1, apr_pool_t *p2, server_rec *s);
25
typedef void (*oidc_test_hook_child_init_fn)(apr_pool_t *p, server_rec *s);
26
typedef void (*oidc_test_hook_insert_filter_fn)(request_rec *r);
27
typedef apr_status_t (*oidc_test_input_filter_fn)(ap_filter_t *f, apr_bucket_brigade *b, ap_input_mode_t mode,
28
              apr_read_type_e block, apr_off_t nbytes);
29
30
static oidc_test_hook_insert_filter_fn _oidc_test_hook_insert_filter = NULL;
31
static oidc_test_input_filter_fn _oidc_test_input_filter = NULL;
32
static const char *_oidc_test_added_input_filter = NULL;
33
static const char *_oidc_test_brigade_body = NULL;
34
static apr_status_t _oidc_test_brigade_rc = APR_SUCCESS;
35
static const char *_oidc_test_exec_line_output = NULL;
36
37
/* the error-level request log messages since oidc_test_log_errors_start(), when started */
38
0
#define OIDC_TEST_LOG_ERRORS_MAX 16384
39
static char _oidc_test_log_errors[OIDC_TEST_LOG_ERRORS_MAX];
40
static size_t _oidc_test_log_errors_len = 0;
41
static int _oidc_test_log_errors_on = 0;
42
43
#define ap_HOOK_check_user_id_t void
44
45
AP_DECLARE(void)
46
ap_hook_check_authn(ap_HOOK_check_user_id_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder,
47
0
        int type) {
48
  // comment explaining why the method is empty
49
0
}
50
51
/* the first authz provider the module registers, so its parse_require_line can be driven */
52
static const void *_oidc_test_authz_provider = NULL;
53
54
0
const void *oidc_test_authz_provider_get(void) {
55
0
  return _oidc_test_authz_provider;
56
0
}
57
58
AP_DECLARE(apr_status_t)
59
ap_register_auth_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name,
60
0
        const char *provider_version, const void *provider, int type) {
61
0
  if (_oidc_test_authz_provider == NULL)
62
0
    _oidc_test_authz_provider = provider;
63
0
  return 0;
64
0
}
65
66
4
AP_DECLARE(apr_status_t) ap_unixd_set_global_mutex_perms(apr_global_mutex_t *gmutex) {
67
4
  return 0;
68
4
}
69
70
/* the AuthType reported to the module; default openid-connect, overridable per
71
 * test via oidc_test_set_auth_type() so the OAuth / mixed dispatch paths can be
72
 * exercised */
73
static const char *stub_auth_type = "openid-connect";
74
75
2
void oidc_test_set_auth_type(const char *auth_type) {
76
2
  stub_auth_type = (auth_type != NULL) ? auth_type : "openid-connect";
77
2
}
78
79
0
AP_DECLARE(const char *) ap_auth_type(request_rec *r) {
80
0
  return stub_auth_type;
81
0
}
82
83
0
AP_DECLARE(const char *) ap_auth_name(request_rec *r) {
84
0
  return NULL;
85
0
}
86
87
0
AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz) {
88
  /* tests stash the POST body in r->args (and r->remaining tracks bytes left); copy
89
   * from there into the caller's buffer so oidc_util_read can drive the form parser */
90
0
  if (r->args == NULL || r->remaining == 0)
91
0
    return 0;
92
  /* r->remaining is signed (apr_off_t) and is only ever positive here, but compare and
93
   * assign in one type so the narrowing is explicit rather than a signedness surprise */
94
0
  apr_size_t remaining = (apr_size_t)r->remaining;
95
0
  apr_size_t off = strlen(r->args) - remaining;
96
0
  apr_size_t n = (bufsiz < remaining) ? bufsiz : remaining;
97
0
  memcpy(buffer, r->args + off, n);
98
0
  r->remaining -= n;
99
0
  return (long)n;
100
0
}
101
102
0
AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop) {
103
0
  const char *pos = *line;
104
0
  int len;
105
0
  char *res;
106
107
0
  while ((*pos != stop) && *pos) {
108
0
    ++pos;
109
0
  }
110
111
0
  len = (int)(pos - *line);
112
0
  res = apr_pstrmemdup(atrans, *line, len);
113
114
0
  if (stop) {
115
0
    while (*pos == stop) {
116
0
      ++pos;
117
0
    }
118
0
  }
119
0
  *line = pos;
120
121
0
  return res;
122
0
}
123
124
0
static char *substring_conf(apr_pool_t *p, const char *start, int len, char quote) {
125
0
  char *result = apr_palloc(p, len + 1);
126
0
  char *resp = result;
127
0
  int i;
128
129
0
  for (i = 0; i < len; ++i) {
130
0
    if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote)))
131
0
      *resp++ = start[++i];
132
0
    else
133
0
      *resp++ = start[i];
134
0
  }
135
136
0
  *resp++ = '\0';
137
#if RESOLVE_ENV_PER_TOKEN
138
  return (char *)ap_resolve_env(p, result);
139
#else
140
0
  return result;
141
0
#endif
142
0
}
143
144
0
AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line) {
145
0
  const char *str = *line, *strend;
146
0
  char *res;
147
0
  char quote;
148
149
0
  while (apr_isspace(*str))
150
0
    ++str;
151
152
0
  if (!*str) {
153
0
    *line = str;
154
0
    return "";
155
0
  }
156
157
0
  if ((quote = *str) == '"' || quote == '\'') {
158
0
    strend = str + 1;
159
0
    while (*strend && *strend != quote) {
160
0
      if (*strend == '\\' && strend[1] && (strend[1] == quote || strend[1] == '\\')) {
161
0
        strend += 2;
162
0
      } else {
163
0
        ++strend;
164
0
      }
165
0
    }
166
0
    res = substring_conf(p, str + 1, (int)(strend - str - 1), quote);
167
168
0
    if (*strend == quote)
169
0
      ++strend;
170
0
  } else {
171
0
    strend = str;
172
0
    while (*strend && !apr_isspace(*strend))
173
0
      ++strend;
174
175
0
    res = substring_conf(p, str, (int)(strend - str), 0);
176
0
  }
177
178
0
  while (apr_isspace(*strend))
179
0
    ++strend;
180
0
  *line = strend;
181
0
  return res;
182
0
}
183
184
0
AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *p, const char **line, char stop) {
185
  /* match Apache semantics: return the word before `stop`, advance *line past
186
   * the separator (or to '\0' if `stop` is not present). The previous stub
187
   * silently returned "" and left *line untouched, which masked any callers
188
   * that relied on the advance — e.g. oidc_oauth_token_from_basic. */
189
0
  const char *pos = *line;
190
0
  while (*pos && (*pos != stop))
191
0
    ++pos;
192
0
  char *res = apr_pstrmemdup(p, *line, pos - *line);
193
0
  *line = (*pos == stop) ? pos + 1 : pos;
194
0
  return res;
195
0
}
196
197
0
AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line) {
198
0
  const char *pos = *line;
199
0
  int len;
200
0
  char *res;
201
202
0
  while (!apr_isspace(*pos) && *pos) {
203
0
    ++pos;
204
0
  }
205
206
0
  len = (int)(pos - *line);
207
0
  res = apr_pstrmemdup(atrans, *line, len);
208
209
0
  while (apr_isspace(*pos)) {
210
0
    ++pos;
211
0
  }
212
213
0
  *line = pos;
214
215
0
  return res;
216
0
}
217
218
0
AP_DECLARE(int) ap_hook_check_user_id(request_rec *r) {
219
0
  return 0;
220
0
}
221
222
0
AP_DECLARE(int) ap_hook_auth_checker(request_rec *r) {
223
0
  return 0;
224
0
}
225
226
AP_DECLARE(void)
227
0
ap_hook_fixups(int (*handler)(request_rec *r), const char *const *aszPre, const char *const *aszSucc, int nOrder) {
228
  // comment explaining why the method is empty
229
0
}
230
231
AP_DECLARE(void)
232
ap_hook_insert_filter(void (*insert_filter)(request_rec *r), const char *const *aszPre, const char *const *aszSucc,
233
0
          int nOrder) {
234
0
  _oidc_test_hook_insert_filter = insert_filter;
235
0
}
236
237
/*
238
 * the hooks the module registers are recorded rather than discarded, so a test can drive the
239
 * server-lifetime entry points (post_config, child_init) the way httpd would; see test_config.c
240
 */
241
static oidc_test_hook_post_config_fn _oidc_test_hook_post_config = NULL;
242
static oidc_test_hook_child_init_fn _oidc_test_hook_child_init = NULL;
243
244
0
oidc_test_hook_post_config_fn oidc_test_hook_post_config_get(void) {
245
0
  return _oidc_test_hook_post_config;
246
0
}
247
248
0
oidc_test_hook_child_init_fn oidc_test_hook_child_init_get(void) {
249
0
  return _oidc_test_hook_child_init;
250
0
}
251
252
AP_DECLARE(void)
253
ap_hook_post_config(int (*post_config)(apr_pool_t *pool, apr_pool_t *p1, apr_pool_t *p2, server_rec *s),
254
0
        const char *const *aszPre, const char *const *aszSucc, int nOrder) {
255
0
  _oidc_test_hook_post_config = post_config;
256
0
}
257
258
AP_DECLARE(void)
259
ap_hook_child_init(void (*child_init)(apr_pool_t *p, server_rec *s), const char *const *aszPre,
260
0
       const char *const *aszSucc, int nOrder) {
261
0
  _oidc_test_hook_child_init = child_init;
262
0
}
263
264
AP_DECLARE(void)
265
0
ap_hook_handler(int (*handler)(request_rec *r), const char *const *aszPre, const char *const *aszSucc, int nOrder) {
266
  // comment explaining why the method is empty
267
0
}
268
269
0
AP_DECLARE(int) ap_is_initial_req(request_rec *r) {
270
  /* Match Apache: an initial request has neither main nor prev. */
271
0
  return (r->main == NULL) && (r->prev == NULL);
272
0
}
273
274
AP_DECLARE(ap_expr_info_t *)
275
ap_expr_parse_cmd_mi(const cmd_parms *cmd, const char *expr, unsigned int flags, const char **err,
276
0
         ap_expr_lookup_fn_t *lookup_fn, int module_index) {
277
0
  if (strcmp(expr, "#") == 0) {
278
0
    *err = "error";
279
0
    return NULL;
280
0
  }
281
0
  ap_expr_info_t *rv = apr_pcalloc(cmd->pool, sizeof(ap_expr_info_t));
282
  /* echo the expression through ap_expr_str_exec below so string-valued
283
   * expression directives behave like literal strings in the tests */
284
0
  rv->filename = apr_pstrdup(cmd->pool, expr);
285
0
  rv->root_node = NULL;
286
0
  return rv;
287
0
}
288
289
0
AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r, const ap_expr_info_t *expr, const char **err) {
290
0
  if (err)
291
0
    *err = NULL;
292
0
  return expr->filename;
293
0
}
294
295
/* Return the primed first command-output line; an unprimed command fails with NULL. */
296
0
AP_DECLARE(char *) ap_get_exec_line(apr_pool_t *p, const char *cmd, const char *const *argv) {
297
0
  return (_oidc_test_exec_line_output == NULL) ? NULL : apr_pstrdup(p, _oidc_test_exec_line_output);
298
0
}
299
300
0
void oidc_test_exec_line_prime(const char *output) {
301
0
  _oidc_test_exec_line_output = output;
302
0
}
303
304
AP_DECLARE(void)
305
ap_log_error_(const char *file, int line, int module_index, int level, apr_status_t status, const server_rec *s,
306
2
        const char *fmt, ...) {
307
2
  if (level < APLOG_DEBUG) {
308
0
    fprintf(stderr, "%s:%d [%d] [%d] ", file, line, level, status);
309
0
    va_list ap;
310
0
    va_start(ap, fmt);
311
0
    vfprintf(stderr, fmt, ap);
312
0
    va_end(ap);
313
0
    fprintf(stderr, "\n");
314
0
  }
315
2
}
316
317
AP_DECLARE(void)
318
ap_log_rerror_(const char *file, int line, int module_index, int level, apr_status_t status, const request_rec *r,
319
90.1k
         const char *fmt, ...) {
320
90.1k
  va_list ap;
321
90.1k
  if ((_oidc_test_log_errors_on) && (level <= APLOG_ERR) &&
322
0
      (_oidc_test_log_errors_len < OIDC_TEST_LOG_ERRORS_MAX - 1)) {
323
0
    va_start(ap, fmt);
324
0
    int n = vsnprintf(_oidc_test_log_errors + _oidc_test_log_errors_len,
325
0
          OIDC_TEST_LOG_ERRORS_MAX - 1 - _oidc_test_log_errors_len, fmt, ap);
326
0
    va_end(ap);
327
0
    if (n > 0)
328
0
      _oidc_test_log_errors_len += (size_t)n;
329
0
    if (_oidc_test_log_errors_len > OIDC_TEST_LOG_ERRORS_MAX - 2)
330
0
      _oidc_test_log_errors_len = OIDC_TEST_LOG_ERRORS_MAX - 2;
331
0
    _oidc_test_log_errors[_oidc_test_log_errors_len++] = '\n';
332
0
    _oidc_test_log_errors[_oidc_test_log_errors_len] = '\0';
333
0
  }
334
90.1k
  if (level < APLOG_DEBUG) {
335
55.7k
    fprintf(stderr, "%s:%d [%d] [%d] ", file, line, level, status);
336
55.7k
    va_start(ap, fmt);
337
55.7k
    vfprintf(stderr, fmt, ap);
338
55.7k
    va_end(ap);
339
55.7k
    fprintf(stderr, "\n");
340
55.7k
  }
341
90.1k
}
342
343
0
void oidc_test_log_errors_start(void) {
344
0
  _oidc_test_log_errors_len = 0;
345
0
  _oidc_test_log_errors[0] = '\0';
346
0
  _oidc_test_log_errors_on = 1;
347
0
}
348
349
0
const char *oidc_test_log_errors_stop(void) {
350
0
  _oidc_test_log_errors_on = 0;
351
0
  return _oidc_test_log_errors;
352
0
}
353
354
0
AP_DECLARE(void) ap_note_auth_failure(request_rec *r) {
355
  // comment explaining why the method is empty
356
0
}
357
358
/* capture the response body passed down the output filter chain in the request
359
 * state under "sent_body" so tests can assert on generated content; the test
360
 * fixture wires request->output_filters->r for this (see test/util.c) */
361
extern void oidc_request_state_set(request_rec *r, const char *key, const char *value);
362
363
0
AP_DECLARE(apr_status_t) ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket) {
364
0
  char *buf = NULL;
365
0
  apr_size_t len = 0;
366
0
  if ((filter != NULL) && (filter->r != NULL) &&
367
0
      (apr_brigade_pflatten(bucket, &buf, &len, filter->r->pool) == APR_SUCCESS) && (len > 0))
368
0
    oidc_request_state_set(filter->r, "sent_body", apr_pstrmemdup(filter->r->pool, buf, len));
369
0
  return APR_SUCCESS;
370
0
}
371
372
0
AP_DECLARE(const apr_array_header_t *) ap_requires(request_rec *r) {
373
0
  return NULL;
374
0
}
375
376
25.2k
const char *ap_run_http_scheme(const request_rec *r) {
377
25.2k
  char *rv;
378
25.2k
  apr_pool_userdata_get((void **)&rv, "scheme", r->pool);
379
25.2k
  return (const char *)rv;
380
25.2k
}
381
382
0
AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct) {
383
  // comment explaining why the method is empty
384
0
}
385
386
0
AP_DECLARE_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd, void *struct_ptr, int arg) {
387
0
  return "";
388
0
}
389
390
0
AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd, void *struct_ptr, const char *arg) {
391
0
  return "";
392
0
}
393
394
0
AP_DECLARE_NONSTD(const char *) ap_set_int_slot(cmd_parms *cmd, void *struct_ptr, const char *arg) {
395
0
  return "";
396
0
}
397
398
0
AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy) {
399
0
  return 0;
400
0
}
401
402
0
AP_DECLARE(int) ap_should_client_block(request_rec *r) {
403
0
  return 1;
404
0
}
405
406
0
AP_DECLARE(int) ap_unescape_url(char *url) {
407
0
  return 0;
408
0
}
409
410
0
AP_DECLARE(apr_status_t) unixd_set_global_mutex_perms(apr_global_mutex_t *gmutex) {
411
0
  return APR_SUCCESS;
412
0
}
413
414
0
AP_DECLARE(const char *) ap_get_server_name(request_rec *r) {
415
0
  return "www.example.com";
416
0
}
417
418
0
AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *file) {
419
  /* Apache's real implementation prepends ServerRoot to relative paths; in tests
420
   * we treat the path as already-absolute (or relative-to-cwd) and return it verbatim */
421
0
  if (file == NULL)
422
0
    return NULL;
423
0
  return apr_pstrdup(p, file);
424
0
}
425
426
0
AP_DECLARE(ap_filter_t *) ap_add_input_filter(const char *name, void *ctx, request_rec *r, conn_rec *c) {
427
0
  _oidc_test_added_input_filter = name;
428
0
  return NULL;
429
0
}
430
431
0
const char *oidc_test_added_input_filter_get(void) {
432
0
  return _oidc_test_added_input_filter;
433
0
}
434
435
0
void oidc_test_added_input_filter_reset(void) {
436
0
  _oidc_test_added_input_filter = NULL;
437
0
}
438
439
AP_DECLARE(apr_status_t)
440
ap_get_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket, ap_input_mode_t mode, apr_read_type_e block,
441
0
         apr_off_t readbytes) {
442
  /* Return the primed request body followed by EOS; unprimed calls return an empty brigade. */
443
0
  if (_oidc_test_brigade_body != NULL) {
444
0
    apr_bucket_alloc_t *ba = bucket->bucket_alloc;
445
0
    APR_BRIGADE_INSERT_TAIL(
446
0
        bucket, apr_bucket_heap_create(_oidc_test_brigade_body, strlen(_oidc_test_brigade_body), NULL, ba));
447
0
    APR_BRIGADE_INSERT_TAIL(bucket, apr_bucket_eos_create(ba));
448
0
    _oidc_test_brigade_body = NULL;
449
0
  }
450
0
  return _oidc_test_brigade_rc;
451
0
}
452
453
0
void oidc_test_brigade_prime(const char *body, apr_status_t rc) {
454
0
  _oidc_test_brigade_body = body;
455
0
  _oidc_test_brigade_rc = rc;
456
0
}
457
458
AP_DECLARE(ap_filter_rec_t *)
459
ap_register_input_filter(const char *name, ap_in_filter_func filter_func, ap_init_filter_func filter_init,
460
0
       ap_filter_type ftype) {
461
0
  _oidc_test_input_filter = filter_func;
462
0
  return NULL;
463
0
}
464
465
0
oidc_test_input_filter_fn oidc_test_input_filter_get(void) {
466
0
  return _oidc_test_input_filter;
467
0
}
468
469
0
oidc_test_hook_insert_filter_fn oidc_test_hook_insert_filter_get(void) {
470
0
  return _oidc_test_hook_insert_filter;
471
0
}
472
473
0
AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s) {
474
0
  return NULL;
475
0
}
476
477
0
AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result) {
478
0
  *result = 1;
479
0
  return APR_SUCCESS;
480
0
}
481
482
#if AP_MODULE_MAGIC_AT_LEAST(20080920, 2)
483
AP_DECLARE(apr_status_t)
484
0
ap_timeout_parameter_parse(const char *timeout_parameter, apr_interval_time_t *timeout, const char *default_time_unit) {
485
0
  char *endp;
486
0
  const char *time_str;
487
0
  apr_int64_t tout;
488
489
0
  tout = apr_strtoi64(timeout_parameter, &endp, 10);
490
0
  if (!endp || !*endp) {
491
0
    time_str = default_time_unit;
492
0
  } else {
493
0
    time_str = endp;
494
0
  }
495
496
0
  switch (*time_str) {
497
    /* Time is in seconds */
498
0
  case 's':
499
0
    *timeout = (apr_interval_time_t)apr_time_from_sec(tout);
500
0
    break;
501
0
  case 'h':
502
    /* Time is in hours */
503
0
    *timeout = (apr_interval_time_t)apr_time_from_sec(tout * 3600);
504
0
    break;
505
0
  case 'm':
506
0
    switch (*(++time_str)) {
507
    /* Time is in milliseconds */
508
0
    case 's':
509
0
      *timeout = (apr_interval_time_t)tout * 1000;
510
0
      break;
511
    /* Time is in minutes */
512
0
    case 'i':
513
0
      *timeout = (apr_interval_time_t)apr_time_from_sec(tout * 60);
514
0
      break;
515
0
    default:
516
0
      return APR_EGENERAL;
517
0
    }
518
0
    break;
519
0
  default:
520
0
    return APR_EGENERAL;
521
0
  }
522
0
  return APR_SUCCESS;
523
0
}
524
#endif
525
526
0
AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr, const char **err) {
527
0
  *err = "error";
528
0
  return 1;
529
0
}