bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testdso.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include "test_apr.h"
19 #include "apr_general.h"
20 #include "apr_pools.h"
21 #include "apr_errno.h"
22 #include "apr_dso.h"
23 #include "apr_strings.h"
24 #include "apr_file_info.h"
25 #include "apr.h"
26 #if APR_HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #ifdef NETWARE
31 # define MOD_NAME "mod_test.nlm"
32 #elif defined(BEOS) || defined(WIN32)
33 # define MOD_NAME "mod_test.so"
34 #elif defined(DARWIN)
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"
46 #endif
47
48 static char *modname;
49
50 static void test_load_module(CuTest *tc)
51 {
52     apr_dso_handle_t *h = NULL;
53     apr_status_t status;
54     char errstr[256];
55
56     status = apr_dso_load(&h, modname, p);
57     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
58     CuAssertPtrNotNull(tc, h);
59
60     apr_dso_unload(h);
61 }
62
63 static void test_dso_sym(CuTest *tc)
64 {
65     apr_dso_handle_t *h = NULL;
66     apr_dso_handle_sym_t func1 = NULL;
67     apr_status_t status;
68     void (*function)(char str[256]);
69     char teststr[256];
70     char errstr[256];
71
72     status = apr_dso_load(&h, modname, p);
73     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
74     CuAssertPtrNotNull(tc, h);
75
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);
79
80     function = (void (*)(char *))func1;
81     (*function)(teststr);
82     CuAssertStrEquals(tc, "Hello - I'm a DSO!\n", teststr);
83
84     apr_dso_unload(h);
85 }
86
87 static void test_dso_sym_return_value(CuTest *tc)
88 {
89     apr_dso_handle_t *h = NULL;
90     apr_dso_handle_sym_t func1 = NULL;
91     apr_status_t status;
92     int (*function)(int);
93     char errstr[256];
94
95     status = apr_dso_load(&h, modname, p);
96     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
97     CuAssertPtrNotNull(tc, h);
98
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);
102
103     function = (int (*)(int))func1;
104     status = (*function)(5);
105     CuAssertIntEquals(tc, 5, status);
106
107     apr_dso_unload(h);
108 }
109
110 static void test_unload_module(CuTest *tc)
111 {
112     apr_dso_handle_t *h = NULL;
113     apr_status_t status;
114     char errstr[256];
115     apr_dso_handle_sym_t func1 = NULL;
116
117     status = apr_dso_load(&h, modname, p);
118     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
119     CuAssertPtrNotNull(tc, h);
120
121     status = apr_dso_unload(h);
122     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
123
124     status = apr_dso_sym(&func1, h, "print_hello");
125     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ESYMNOTFOUND(status));
126 }
127
128
129 #ifdef LIB_NAME
130 static char *libname;
131
132 static void test_load_library(CuTest *tc)
133 {
134     apr_dso_handle_t *h = NULL;
135     apr_status_t status;
136     char errstr[256];
137
138     status = apr_dso_load(&h, libname, p);
139     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
140     CuAssertPtrNotNull(tc, h);
141
142     apr_dso_unload(h);
143 }
144
145 static void test_dso_sym_library(CuTest *tc)
146 {
147     apr_dso_handle_t *h = NULL;
148     apr_dso_handle_sym_t func1 = NULL;
149     apr_status_t status;
150     void (*function)(char str[256]);
151     char teststr[256];
152     char errstr[256];
153
154     status = apr_dso_load(&h, libname, p);
155     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
156     CuAssertPtrNotNull(tc, h);
157
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);
161
162     function = (void (*)(char *))func1;
163     (*function)(teststr);
164     CuAssertStrEquals(tc, "Hello - I'm a DSO!\n", teststr);
165
166     apr_dso_unload(h);
167 }
168
169 static void test_dso_sym_return_value_library(CuTest *tc)
170 {
171     apr_dso_handle_t *h = NULL;
172     apr_dso_handle_sym_t func1 = NULL;
173     apr_status_t status;
174     int (*function)(int);
175     char errstr[256];
176
177     status = apr_dso_load(&h, libname, p);
178     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
179     CuAssertPtrNotNull(tc, h);
180
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);
184
185     function = (int (*)(int))func1;
186     status = (*function)(5);
187     CuAssertIntEquals(tc, 5, status);
188
189     apr_dso_unload(h);
190 }
191
192 static void test_unload_library(CuTest *tc)
193 {
194     apr_dso_handle_t *h = NULL;
195     apr_status_t status;
196     char errstr[256];
197     apr_dso_handle_sym_t func1 = NULL;
198
199     status = apr_dso_load(&h, libname, p);
200     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
201     CuAssertPtrNotNull(tc, h);
202
203     status = apr_dso_unload(h);
204     CuAssert(tc, apr_dso_error(h, errstr, 256), APR_SUCCESS == status);
205
206     status = apr_dso_sym(&func1, h, "print_hello");
207     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ESYMNOTFOUND(status));
208 }
209
210 #endif /* def(LIB_NAME) */
211
212 static void test_load_notthere(CuTest *tc)
213 {
214     apr_dso_handle_t *h = NULL;
215     apr_status_t status;
216
217     status = apr_dso_load(&h, "No_File.so", p);
218
219     CuAssertIntEquals(tc, 1, APR_STATUS_IS_EDSOOPEN(status));
220     CuAssertPtrNotNull(tc, h);
221 }    
222
223 CuSuite *testdso(void)
224 {
225     CuSuite *suite = CuSuiteNew("DSO");
226
227     apr_filepath_merge(&modname, NULL, MOD_NAME, 0, p);
228
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);
233
234 #ifdef LIB_NAME
235     apr_filepath_merge(&libname, NULL, LIB_NAME, 0, p);
236
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);
241 #endif
242
243     SUITE_ADD_TEST(suite, test_load_notthere);
244
245     return suite;
246 }
247