testdso.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include "testutil.h"
  17. #include "fspr_general.h"
  18. #include "fspr_pools.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_dso.h"
  21. #include "fspr_strings.h"
  22. #include "fspr_file_info.h"
  23. #include "fspr.h"
  24. #if APR_HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #if APR_HAS_DSO
  28. #ifdef NETWARE
  29. # define MOD_NAME "mod_test.nlm"
  30. #elif defined(BEOS)
  31. # define MOD_NAME "mod_test.so"
  32. #elif defined(WIN32)
  33. # define MOD_NAME "mod_test.dll"
  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. static char *modname;
  48. static void test_load_module(abts_case *tc, void *data)
  49. {
  50. fspr_dso_handle_t *h = NULL;
  51. fspr_status_t status;
  52. char errstr[256];
  53. status = fspr_dso_load(&h, modname, p);
  54. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  55. ABTS_PTR_NOTNULL(tc, h);
  56. fspr_dso_unload(h);
  57. }
  58. static void test_dso_sym(abts_case *tc, void *data)
  59. {
  60. fspr_dso_handle_t *h = NULL;
  61. fspr_dso_handle_sym_t func1 = NULL;
  62. fspr_status_t status;
  63. void (*function)(char str[256]);
  64. char teststr[256];
  65. char errstr[256];
  66. status = fspr_dso_load(&h, modname, p);
  67. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  68. ABTS_PTR_NOTNULL(tc, h);
  69. status = fspr_dso_sym(&func1, h, "print_hello");
  70. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  71. ABTS_PTR_NOTNULL(tc, func1);
  72. if (!tc->failed) {
  73. function = (void (*)(char *))func1;
  74. (*function)(teststr);
  75. ABTS_STR_EQUAL(tc, "Hello - I'm a DSO!\n", teststr);
  76. }
  77. fspr_dso_unload(h);
  78. }
  79. static void test_dso_sym_return_value(abts_case *tc, void *data)
  80. {
  81. fspr_dso_handle_t *h = NULL;
  82. fspr_dso_handle_sym_t func1 = NULL;
  83. fspr_status_t status;
  84. int (*function)(int);
  85. char errstr[256];
  86. status = fspr_dso_load(&h, modname, p);
  87. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  88. ABTS_PTR_NOTNULL(tc, h);
  89. status = fspr_dso_sym(&func1, h, "count_reps");
  90. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  91. ABTS_PTR_NOTNULL(tc, func1);
  92. if (!tc->failed) {
  93. function = (int (*)(int))func1;
  94. status = (*function)(5);
  95. ABTS_INT_EQUAL(tc, 5, status);
  96. }
  97. fspr_dso_unload(h);
  98. }
  99. static void test_unload_module(abts_case *tc, void *data)
  100. {
  101. fspr_dso_handle_t *h = NULL;
  102. fspr_status_t status;
  103. char errstr[256];
  104. fspr_dso_handle_sym_t func1 = NULL;
  105. status = fspr_dso_load(&h, modname, p);
  106. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  107. ABTS_PTR_NOTNULL(tc, h);
  108. status = fspr_dso_unload(h);
  109. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  110. status = fspr_dso_sym(&func1, h, "print_hello");
  111. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ESYMNOTFOUND(status));
  112. }
  113. #ifdef LIB_NAME
  114. static char *libname;
  115. static void test_load_library(abts_case *tc, void *data)
  116. {
  117. fspr_dso_handle_t *h = NULL;
  118. fspr_status_t status;
  119. char errstr[256];
  120. status = fspr_dso_load(&h, libname, p);
  121. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  122. ABTS_PTR_NOTNULL(tc, h);
  123. fspr_dso_unload(h);
  124. }
  125. static void test_dso_sym_library(abts_case *tc, void *data)
  126. {
  127. fspr_dso_handle_t *h = NULL;
  128. fspr_dso_handle_sym_t func1 = NULL;
  129. fspr_status_t status;
  130. void (*function)(char str[256]);
  131. char teststr[256];
  132. char errstr[256];
  133. status = fspr_dso_load(&h, libname, p);
  134. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  135. ABTS_PTR_NOTNULL(tc, h);
  136. status = fspr_dso_sym(&func1, h, "print_hello");
  137. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  138. ABTS_PTR_NOTNULL(tc, func1);
  139. if (!tc->failed) {
  140. function = (void (*)(char *))func1;
  141. (*function)(teststr);
  142. ABTS_STR_EQUAL(tc, "Hello - I'm a DSO!\n", teststr);
  143. }
  144. fspr_dso_unload(h);
  145. }
  146. static void test_dso_sym_return_value_library(abts_case *tc, void *data)
  147. {
  148. fspr_dso_handle_t *h = NULL;
  149. fspr_dso_handle_sym_t func1 = NULL;
  150. fspr_status_t status;
  151. int (*function)(int);
  152. char errstr[256];
  153. status = fspr_dso_load(&h, libname, p);
  154. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  155. ABTS_PTR_NOTNULL(tc, h);
  156. status = fspr_dso_sym(&func1, h, "count_reps");
  157. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  158. ABTS_PTR_NOTNULL(tc, func1);
  159. if (!tc->failed) {
  160. function = (int (*)(int))func1;
  161. status = (*function)(5);
  162. ABTS_INT_EQUAL(tc, 5, status);
  163. }
  164. fspr_dso_unload(h);
  165. }
  166. static void test_unload_library(abts_case *tc, void *data)
  167. {
  168. fspr_dso_handle_t *h = NULL;
  169. fspr_status_t status;
  170. char errstr[256];
  171. fspr_dso_handle_sym_t func1 = NULL;
  172. status = fspr_dso_load(&h, libname, p);
  173. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  174. ABTS_PTR_NOTNULL(tc, h);
  175. status = fspr_dso_unload(h);
  176. ABTS_ASSERT(tc, fspr_dso_error(h, errstr, 256), APR_SUCCESS == status);
  177. status = fspr_dso_sym(&func1, h, "print_hello");
  178. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ESYMNOTFOUND(status));
  179. }
  180. #endif /* def(LIB_NAME) */
  181. static void test_load_notthere(abts_case *tc, void *data)
  182. {
  183. fspr_dso_handle_t *h = NULL;
  184. fspr_status_t status;
  185. status = fspr_dso_load(&h, "No_File.so", p);
  186. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EDSOOPEN(status));
  187. ABTS_PTR_NOTNULL(tc, h);
  188. }
  189. #endif /* APR_HAS_DSO */
  190. abts_suite *testdso(abts_suite *suite)
  191. {
  192. suite = ADD_SUITE(suite)
  193. #if APR_HAS_DSO
  194. fspr_filepath_merge(&modname, NULL, MOD_NAME, 0, p);
  195. abts_run_test(suite, test_load_module, NULL);
  196. abts_run_test(suite, test_dso_sym, NULL);
  197. abts_run_test(suite, test_dso_sym_return_value, NULL);
  198. abts_run_test(suite, test_unload_module, NULL);
  199. #ifdef LIB_NAME
  200. fspr_filepath_merge(&libname, NULL, LIB_NAME, 0, p);
  201. abts_run_test(suite, test_load_library, NULL);
  202. abts_run_test(suite, test_dso_sym_library, NULL);
  203. abts_run_test(suite, test_dso_sym_return_value_library, NULL);
  204. abts_run_test(suite, test_unload_library, NULL);
  205. #endif
  206. abts_run_test(suite, test_load_notthere, NULL);
  207. #endif /* APR_HAS_DSO */
  208. return suite;
  209. }