2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/opensslv.h>
14 #include <openssl/ssl.h>
15 #include <openssl/ossl_typ.h>
16 #include "internal/dso_conf.h"
21 typedef const SSL_METHOD * (*TLS_method_t)(void);
22 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
23 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
24 typedef unsigned long (*ERR_get_error_t)(void);
25 typedef unsigned long (*OpenSSL_version_num_t)(void);
26 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
27 typedef int (*DSO_free_t)(DSO *dso);
29 typedef enum test_types_en {
36 static TEST_TYPE test_type;
37 static const char *path_crypto;
38 static const char *path_ssl;
44 # define SHLIB_INIT NULL
47 typedef void *SHLIB_SYM;
49 static int shlib_load(const char *filename, SHLIB *lib)
51 int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
53 if (filename[strlen(filename) - 1] == ')')
54 dl_flags |= RTLD_MEMBER;
56 *lib = dlopen(filename, dl_flags);
57 return *lib == NULL ? 0 : 1;
60 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
62 *sym = dlsym(lib, symname);
66 static int shlib_close(SHLIB lib)
68 return dlclose(lib) != 0 ? 0 : 1;
78 typedef HINSTANCE SHLIB;
79 typedef void *SHLIB_SYM;
81 static int shlib_load(const char *filename, SHLIB *lib)
83 *lib = LoadLibraryA(filename);
84 return *lib == NULL ? 0 : 1;
87 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
89 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
93 static int shlib_close(SHLIB lib)
95 return FreeLibrary(lib) == 0 ? 0 : 1;
100 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
102 static int test_lib(void)
104 SHLIB ssllib = SHLIB_INIT;
105 SHLIB cryptolib = SHLIB_INIT;
111 TLS_method_t myTLS_method;
112 SSL_CTX_new_t mySSL_CTX_new;
113 SSL_CTX_free_t mySSL_CTX_free;
114 ERR_get_error_t myERR_get_error;
115 OpenSSL_version_num_t myOpenSSL_version_num;
120 if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
124 if (!TEST_true(shlib_load(path_crypto, &cryptolib))
125 || !TEST_true(shlib_load(path_ssl, &ssllib)))
129 if (!TEST_true(shlib_load(path_ssl, &ssllib))
130 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
134 if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
139 if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
140 if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
141 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
142 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
144 myTLS_method = (TLS_method_t)symbols[0].func;
145 mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
146 mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
147 if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
152 if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
153 || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
156 myERR_get_error = (ERR_get_error_t)symbols[0].func;
157 if (!TEST_int_eq(myERR_get_error(), 0))
161 * The bits that COMPATIBILITY_MASK lets through MUST be the same in
162 * the library and in the application.
163 * The bits that are masked away MUST be a larger or equal number in
164 * the library compared to the application.
166 # define COMPATIBILITY_MASK 0xfff00000L
167 myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
168 if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK,
169 OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK))
171 if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK,
172 OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK))
175 if (test_type == DSO_REFTEST) {
177 DSO_dsobyaddr_t myDSO_dsobyaddr;
178 DSO_free_t myDSO_free;
181 * This is resembling the code used in ossl_init_base() and
182 * OPENSSL_atexit() to block unloading the library after dlclose().
183 * We are not testing this on Windows, because it is done there in a
184 * completely different way. Especially as a call to DSO_dsobyaddr()
185 * will always return an error, because DSO_pathbyaddr() is not
188 if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
189 || !TEST_true(shlib_sym(cryptolib, "DSO_free",
193 myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
194 myDSO_free = (DSO_free_t)symbols[1].func;
198 /* use known symbol from crypto module */
199 if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
203 # endif /* DSO_DLFCN */
208 if (!TEST_true(shlib_close(cryptolib)))
212 if (!TEST_true(shlib_close(cryptolib))
213 || !TEST_true(shlib_close(ssllib)))
217 if (!TEST_true(shlib_close(ssllib))
218 || !TEST_true(shlib_close(cryptolib)))
222 if (!TEST_true(shlib_close(cryptolib)))
234 int setup_tests(void)
236 const char *p = test_get_argument(0);
238 if (strcmp(p, "-crypto_first") == 0) {
239 test_type = CRYPTO_FIRST;
240 } else if (strcmp(p, "-ssl_first") == 0) {
241 test_type = SSL_FIRST;
242 } else if (strcmp(p, "-just_crypto") == 0) {
243 test_type = JUST_CRYPTO;
244 } else if (strcmp(p, "-dso_ref") == 0) {
245 test_type = JUST_CRYPTO;
247 TEST_error("Unrecognised argument");
250 if (!TEST_ptr(path_crypto = test_get_argument(1))
251 || !TEST_ptr(path_ssl = test_get_argument(2)))
254 #if defined(DSO_DLFCN) || defined(DSO_WIN32)