1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "apr_general.h"
20 #include "apr_pools.h"
21 #include "apr_errno.h"
23 #include "apr_strings.h"
24 #include "apr_file_info.h"
31 # define MOD_NAME "mod_test.nlm"
32 #elif defined(BEOS) || defined(WIN32)
33 # define MOD_NAME "mod_test.so"
35 # define MOD_NAME ".libs/mod_test.so"
36 # define LIB_NAME ".libs/libmod_test.dylib"
37 #elif defined(__hpux__) || defined(__hpux)
38 # define MOD_NAME ".libs/mod_test.sl"
39 # define LIB_NAME ".libs/libmod_test.sl"
40 #elif defined(_AIX) || defined(__bsdi__)
41 # define MOD_NAME ".libs/libmod_test.so"
42 # define LIB_NAME ".libs/libmod_test.so"
43 #else /* Every other Unix */
44 # define MOD_NAME ".libs/mod_test.so"
45 # define LIB_NAME ".libs/libmod_test.so"
50 static void test_load_module(CuTest *tc)
52 apr_dso_handle_t *h = NULL;
56 status = apr_dso_load(&h, modname, p);
57 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
58 CuAssertPtrNotNull(tc, h);
63 static void test_dso_sym(CuTest *tc)
65 apr_dso_handle_t *h = NULL;
66 apr_dso_handle_sym_t func1 = NULL;
68 void (*function)(char str[256]);
72 status = apr_dso_load(&h, modname, p);
73 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
74 CuAssertPtrNotNull(tc, h);
76 status = apr_dso_sym(&func1, h, "print_hello");
77 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
78 CuAssertPtrNotNull(tc, func1);
80 function = (void (*)(char *))func1;
82 CuAssertStrEquals(tc, "Hello - I'm a DSO!\n", teststr);
87 static void test_dso_sym_return_value(CuTest *tc)
89 apr_dso_handle_t *h = NULL;
90 apr_dso_handle_sym_t func1 = NULL;
95 status = apr_dso_load(&h, modname, p);
96 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
97 CuAssertPtrNotNull(tc, h);
99 status = apr_dso_sym(&func1, h, "count_reps");
100 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
101 CuAssertPtrNotNull(tc, func1);
103 function = (int (*)(int))func1;
104 status = (*function)(5);
105 CuAssertIntEquals(tc, 5, status);
110 static void test_unload_module(CuTest *tc)
112 apr_dso_handle_t *h = NULL;
115 apr_dso_handle_sym_t func1 = NULL;
117 status = apr_dso_load(&h, modname, p);
118 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
119 CuAssertPtrNotNull(tc, h);
121 status = apr_dso_unload(h);
122 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
124 status = apr_dso_sym(&func1, h, "print_hello");
125 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ESYMNOTFOUND(status));
130 static char *libname;
132 static void test_load_library(CuTest *tc)
134 apr_dso_handle_t *h = NULL;
138 status = apr_dso_load(&h, libname, p);
139 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
140 CuAssertPtrNotNull(tc, h);
145 static void test_dso_sym_library(CuTest *tc)
147 apr_dso_handle_t *h = NULL;
148 apr_dso_handle_sym_t func1 = NULL;
150 void (*function)(char str[256]);
154 status = apr_dso_load(&h, libname, p);
155 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
156 CuAssertPtrNotNull(tc, h);
158 status = apr_dso_sym(&func1, h, "print_hello");
159 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
160 CuAssertPtrNotNull(tc, func1);
162 function = (void (*)(char *))func1;
163 (*function)(teststr);
164 CuAssertStrEquals(tc, "Hello - I'm a DSO!\n", teststr);
169 static void test_dso_sym_return_value_library(CuTest *tc)
171 apr_dso_handle_t *h = NULL;
172 apr_dso_handle_sym_t func1 = NULL;
174 int (*function)(int);
177 status = apr_dso_load(&h, libname, p);
178 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
179 CuAssertPtrNotNull(tc, h);
181 status = apr_dso_sym(&func1, h, "count_reps");
182 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
183 CuAssertPtrNotNull(tc, func1);
185 function = (int (*)(int))func1;
186 status = (*function)(5);
187 CuAssertIntEquals(tc, 5, status);
192 static void test_unload_library(CuTest *tc)
194 apr_dso_handle_t *h = NULL;
197 apr_dso_handle_sym_t func1 = NULL;
199 status = apr_dso_load(&h, libname, p);
200 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
201 CuAssertPtrNotNull(tc, h);
203 status = apr_dso_unload(h);
204 CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
206 status = apr_dso_sym(&func1, h, "print_hello");
207 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ESYMNOTFOUND(status));
210 #endif /* def(LIB_NAME) */
212 static void test_load_notthere(CuTest *tc)
214 apr_dso_handle_t *h = NULL;
217 status = apr_dso_load(&h, "No_File.so", p);
219 CuAssertIntEquals(tc, 1, APR_STATUS_IS_EDSOOPEN(status));
220 CuAssertPtrNotNull(tc, h);
223 CuSuite *testdso(void)
225 CuSuite *suite = CuSuiteNew("DSO");
227 apr_filepath_merge(&modname, NULL, MOD_NAME, 0, p);
229 SUITE_ADD_TEST(suite, test_load_module);
230 SUITE_ADD_TEST(suite, test_dso_sym);
231 SUITE_ADD_TEST(suite, test_dso_sym_return_value);
232 SUITE_ADD_TEST(suite, test_unload_module);
235 apr_filepath_merge(&libname, NULL, LIB_NAME, 0, p);
237 SUITE_ADD_TEST(suite, test_load_library);
238 SUITE_ADD_TEST(suite, test_dso_sym_library);
239 SUITE_ADD_TEST(suite, test_dso_sym_return_value_library);
240 SUITE_ADD_TEST(suite, test_unload_library);
243 SUITE_ADD_TEST(suite, test_load_notthere);