|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*************************************************************************** |
| 21 | + * Copyright (C) 2017-2025 ZmartZone Holding BV |
| 22 | + * All rights reserved. |
| 23 | + * |
| 24 | + * DISCLAIMER OF WARRANTIES: |
| 25 | + * |
| 26 | + * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT |
| 27 | + * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING, |
| 28 | + * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT, |
| 29 | + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NOR ARE THERE ANY |
| 30 | + * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE |
| 31 | + * USAGE. FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET |
| 32 | + * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE |
| 33 | + * WILL BE UNINTERRUPTED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
| 34 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 35 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF |
| 36 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 37 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | + * |
| 40 | + * @Author: Hans Zandbelt - [email protected] |
| 41 | + * |
| 42 | + **************************************************************************/ |
| 43 | + |
| 44 | +#include "test/helper.h" |
| 45 | +#include "cfg/cfg_int.h" |
| 46 | +#include "cfg/dir.h" |
| 47 | +#include <openssl/evp.h> |
| 48 | + |
| 49 | +static apr_pool_t *pool = NULL; |
| 50 | +static request_rec *request = NULL; |
| 51 | + |
| 52 | +static request_rec *oidc_test_request_init(apr_pool_t *pool) { |
| 53 | + const unsigned int kIdx = 0; |
| 54 | + const unsigned int kEls = kIdx + 1; |
| 55 | + request_rec *request = (request_rec *)apr_pcalloc(pool, sizeof(request_rec)); |
| 56 | + |
| 57 | + request->pool = pool; |
| 58 | + request->subprocess_env = apr_table_make(request->pool, 0); |
| 59 | + |
| 60 | + request->headers_in = apr_table_make(request->pool, 0); |
| 61 | + request->headers_out = apr_table_make(request->pool, 0); |
| 62 | + request->err_headers_out = apr_table_make(request->pool, 0); |
| 63 | + |
| 64 | + apr_table_set(request->headers_in, "Host", "www.example.com"); |
| 65 | + apr_table_set(request->headers_in, "OIDC_foo", "some-value"); |
| 66 | + apr_table_set(request->headers_in, "Cookie", |
| 67 | + "foo=bar; " |
| 68 | + "mod_auth_openidc_session" |
| 69 | + "=0123456789abcdef; baz=zot"); |
| 70 | + |
| 71 | + request->server = apr_pcalloc(request->pool, sizeof(struct server_rec)); |
| 72 | + request->server->process = apr_pcalloc(request->pool, sizeof(struct process_rec)); |
| 73 | + request->server->process->pool = request->pool; |
| 74 | + request->server->process->pconf = request->pool; |
| 75 | + request->connection = apr_pcalloc(request->pool, sizeof(struct conn_rec)); |
| 76 | + request->connection->bucket_alloc = apr_bucket_alloc_create(request->pool); |
| 77 | + request->connection->local_addr = apr_pcalloc(request->pool, sizeof(apr_sockaddr_t)); |
| 78 | + |
| 79 | + apr_pool_userdata_set("https", "scheme", NULL, request->pool); |
| 80 | + request->server->server_hostname = "www.example.com"; |
| 81 | + request->connection->local_addr->port = 443; |
| 82 | + request->unparsed_uri = "/bla?foo=bar¶m1=value1"; |
| 83 | + request->args = "foo=bar¶m1=value1"; |
| 84 | + apr_uri_parse(request->pool, "https://www.example.com/bla?foo=bar¶m1=value1", &request->parsed_uri); |
| 85 | + |
| 86 | + auth_openidc_module.module_index = kIdx; |
| 87 | + oidc_cfg_t *cfg = oidc_cfg_server_create(request->pool, request->server); |
| 88 | + |
| 89 | + oidc_cfg_provider_issuer_set(pool, oidc_cfg_provider_get(cfg), "https://idp.example.com"); |
| 90 | + oidc_cfg_provider_authorization_endpoint_url_set(pool, oidc_cfg_provider_get(cfg), |
| 91 | + "https://idp.example.com/authorize"); |
| 92 | + oidc_cfg_provider_client_id_set(pool, oidc_cfg_provider_get(cfg), "client_id"); |
| 93 | + |
| 94 | + cfg->redirect_uri = "https://www.example.com/protected/"; |
| 95 | + |
| 96 | + oidc_dir_cfg_t *d_cfg = oidc_cfg_dir_config_create(request->pool, NULL); |
| 97 | + |
| 98 | + // coverity[suspicious_sizeof] |
| 99 | + request->server->module_config = apr_pcalloc(request->pool, sizeof(void *) * kEls); |
| 100 | + // coverity[suspicious_sizeof] |
| 101 | + request->per_dir_config = apr_pcalloc(request->pool, sizeof(void *) * kEls); |
| 102 | + ap_set_module_config(request->server->module_config, &auth_openidc_module, cfg); |
| 103 | + ap_set_module_config(request->per_dir_config, &auth_openidc_module, d_cfg); |
| 104 | + |
| 105 | + cfg->crypto_passphrase.secret1 = "12345678901234567890123456789012"; |
| 106 | + cfg->cache.impl = &oidc_cache_shm; |
| 107 | + cfg->cache.cfg = NULL; |
| 108 | + cfg->cache.shm_size_max = 500; |
| 109 | + cfg->cache.shm_entry_size_max = 16384 + 255 + 17; |
| 110 | + cfg->cache.encrypt = 1; |
| 111 | + if (cfg->cache.impl->post_config(request->server) != OK) { |
| 112 | + printf("cfg->cache.impl->post_config failed!\n"); |
| 113 | + exit(-1); |
| 114 | + } |
| 115 | + |
| 116 | + return request; |
| 117 | +} |
| 118 | + |
| 119 | +void oidc_test_setup(void) { |
| 120 | + apr_initialize(); |
| 121 | + oidc_pre_config_init(); |
| 122 | + apr_pool_create(&pool, NULL); |
| 123 | + request = oidc_test_request_init(pool); |
| 124 | +} |
| 125 | + |
| 126 | +void oidc_test_teardown(void) { |
| 127 | + EVP_cleanup(); |
| 128 | + apr_pool_destroy(pool); |
| 129 | + apr_terminate(); |
| 130 | +} |
| 131 | + |
| 132 | +apr_pool_t *oidc_test_pool_get() { |
| 133 | + return pool; |
| 134 | +} |
| 135 | + |
| 136 | +request_rec *oidc_test_request_get() { |
| 137 | + return request; |
| 138 | +} |
| 139 | + |
| 140 | +int oidc_test_suite_run(Suite *s) { |
| 141 | + int n_failed = 0; |
| 142 | + |
| 143 | + SRunner *sr = srunner_create(s); |
| 144 | + srunner_run_all(sr, CK_VERBOSE); |
| 145 | + n_failed = srunner_ntests_failed(sr); |
| 146 | + srunner_free(sr); |
| 147 | + |
| 148 | + return (n_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 149 | +} |
0 commit comments