Skip to content

Commit 1dac235

Browse files
committed
test: add helper functions and test_util skeleton
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent 3015c02 commit 1dac235

File tree

6 files changed

+332
-42
lines changed

6 files changed

+332
-42
lines changed

Makefile.am

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,17 @@ test_test_CFLAGS = ${AM_CFLAGS} -fPIC
166166

167167
if HAVE_CHECK
168168

169-
TESTS += test/test_jose
169+
noinst_HEADERS += test/helper.h
170170

171+
TESTS += test/test_jose
171172
test_test_jose_CFLAGS = @CHECK_CFLAGS@ ${AM_CFLAGS}
172173
test_test_jose_LDADD = @CHECK_LIBS@ ${LDADD}
173-
test_test_jose_SOURCES = test/test_jose.c
174+
test_test_jose_SOURCES = test/test_jose.c test/helper.c test/stub.c
175+
176+
TESTS += test/test_util
177+
test_test_util_CFLAGS = @CHECK_CFLAGS@ ${AM_CFLAGS}
178+
test_test_util_LDADD = @CHECK_LIBS@ ${LDADD}
179+
test_test_util_SOURCES = test/test_util.c test/helper.c test/stub.c
174180

175181
endif
176182

test/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/test
22
/test-cmd
3-
/test_jose
43
/*.lo
54
/*.o
65
/*.slo
@@ -11,3 +10,5 @@
1110
/.dirstamp
1211
/*.gcno
1312
/*.gcda
13+
/test_jose
14+
/test_util

test/helper.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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&param1=value1";
83+
request->args = "foo=bar&param1=value1";
84+
apr_uri_parse(request->pool, "https://www.example.com/bla?foo=bar&param1=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+
}

test/helper.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#ifndef _MOD_AUTH_OPENIDC_TEST_COMMON_H_
44+
#define _MOD_AUTH_OPENIDC_TEST_COMMON_H_
45+
46+
#include "const.h" // for the PACKAGE_* defines
47+
#include <apr_pools.h>
48+
#include <check.h>
49+
#include <httpd.h>
50+
#include <stdbool.h>
51+
#include <stdlib.h>
52+
53+
void oidc_test_setup(void);
54+
void oidc_test_teardown(void);
55+
int oidc_test_suite_run(Suite *s);
56+
apr_pool_t *oidc_test_pool_get();
57+
request_rec *oidc_test_request_get();
58+
59+
#endif // _MOD_AUTH_OPENIDC_TEST_COMMON_H_

test/test_jose.c

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,88 +41,66 @@
4141
*
4242
**************************************************************************/
4343

44-
#include <check.h>
45-
#include <stdbool.h>
46-
#include <stdlib.h>
47-
48-
#include <openssl/evp.h>
49-
50-
#include "cfg/cfg.h"
5144
#include "jose.h"
52-
53-
static apr_pool_t *pool = NULL;
45+
#include "test/helper.h"
5446

5547
// supported
5648

5749
START_TEST(test_jose_jws_supported_algorithms) {
5850
apr_array_header_t *arr = NULL;
59-
arr = oidc_jose_jws_supported_algorithms(pool);
51+
arr = oidc_jose_jws_supported_algorithms(oidc_test_pool_get());
6052
ck_assert_msg(arr != NULL, "list of supported signing algorithms is empty");
6153
}
6254
END_TEST
6355

6456
START_TEST(test_jose_jws_algorithm_is_supported) {
6557
apr_byte_t rv = FALSE;
66-
rv = oidc_jose_jws_algorithm_is_supported(pool, "RS256");
58+
rv = oidc_jose_jws_algorithm_is_supported(oidc_test_pool_get(), "RS256");
6759
ck_assert_msg(rv == TRUE, "algorithm RS256 is not supported");
68-
rv = oidc_jose_jws_algorithm_is_supported(pool, "NO256");
60+
rv = oidc_jose_jws_algorithm_is_supported(oidc_test_pool_get(), "NO256");
6961
ck_assert_msg(rv == FALSE, "algorithm NO256 should not be supported");
7062
#ifdef OIDC_JOSE_EC_SUPPORT
71-
rv = oidc_jose_jws_algorithm_is_supported(pool, "ES256");
63+
rv = oidc_jose_jws_algorithm_is_supported(oidc_test_pool_get(), "ES256");
7264
ck_assert_msg(rv == TRUE, "algorithm ES256 is not supported");
7365
#endif
7466
}
7567
END_TEST
7668

7769
START_TEST(test_jose_jwe_supported_algorithms) {
7870
apr_array_header_t *arr = NULL;
79-
arr = oidc_jose_jwe_supported_algorithms(pool);
71+
arr = oidc_jose_jwe_supported_algorithms(oidc_test_pool_get());
8072
ck_assert_msg(arr != NULL, "list of supported encryption algorithms is empty");
8173
}
8274
END_TEST
8375

8476
START_TEST(test_jose_jwe_algorithm_is_supported) {
8577
apr_byte_t rv = FALSE;
86-
rv = oidc_jose_jwe_algorithm_is_supported(pool, "A128KW");
78+
rv = oidc_jose_jwe_algorithm_is_supported(oidc_test_pool_get(), "A128KW");
8779
ck_assert_msg(rv == TRUE, "algorithm A128KW is not supported");
8880
}
8981
END_TEST
9082

9183
START_TEST(test_jose_jwe_supported_encryptions) {
9284
apr_array_header_t *arr = NULL;
93-
arr = oidc_jose_jwe_supported_encryptions(pool);
85+
arr = oidc_jose_jwe_supported_encryptions(oidc_test_pool_get());
9486
ck_assert_msg(arr != NULL, "list of supported encryption ciphers is empty");
9587
}
9688
END_TEST
9789

9890
START_TEST(test_jose_jwe_encryption_is_supported) {
9991
apr_byte_t rv = FALSE;
100-
rv = oidc_jose_jwe_encryption_is_supported(pool, "A128CBC-HS256");
92+
rv = oidc_jose_jwe_encryption_is_supported(oidc_test_pool_get(), "A128CBC-HS256");
10193
ck_assert_msg(rv == TRUE, "cipher A128CBC-HS256 is not supported");
10294
#if (OIDC_JOSE_GCM_SUPPORT)
103-
rv = oidc_jose_jwe_encryption_is_supported(pool, "A256GCM");
95+
rv = oidc_jose_jwe_encryption_is_supported(oidc_test_pool_get(), "A256GCM");
10496
ck_assert_msg(rv == TRUE, "cipher A256GCM is not supported");
10597
#endif
10698
}
10799
END_TEST
108100

109-
static void setup(void) {
110-
apr_initialize();
111-
oidc_pre_config_init();
112-
apr_pool_create(&pool, NULL);
113-
}
114-
115-
static void teardown(void) {
116-
EVP_cleanup();
117-
apr_pool_destroy(pool);
118-
apr_terminate();
119-
}
120-
121101
int main(void) {
122-
int n_failed = 0;
123-
124102
TCase *sup = tcase_create("supported");
125-
tcase_add_checked_fixture(sup, setup, teardown);
103+
tcase_add_checked_fixture(sup, oidc_test_setup, oidc_test_teardown);
126104

127105
tcase_add_test(sup, test_jose_jws_supported_algorithms);
128106
tcase_add_test(sup, test_jose_jws_algorithm_is_supported);
@@ -134,10 +112,5 @@ int main(void) {
134112
Suite *s = suite_create("jose");
135113
suite_add_tcase(s, sup);
136114

137-
SRunner *sr = srunner_create(s);
138-
srunner_run_all(sr, CK_VERBOSE);
139-
n_failed = srunner_ntests_failed(sr);
140-
srunner_free(sr);
141-
142-
return (n_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
115+
return oidc_test_suite_run(s);
143116
}

0 commit comments

Comments
 (0)