2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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_major_t)(void);
26 typedef unsigned long (*OPENSSL_version_minor_t)(void);
27 typedef unsigned long (*OPENSSL_version_patch_t)(void);
28 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
29 typedef int (*DSO_free_t)(DSO *dso);
31 typedef enum test_types_en {
38 static TEST_TYPE test_type;
39 static const char *path_crypto;
40 static const char *path_ssl;
46 # define SHLIB_INIT NULL
49 typedef void *SHLIB_SYM;
51 static int shlib_load(const char *filename, SHLIB *lib)
53 int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
55 if (filename[strlen(filename) - 1] == ')')
56 dl_flags |= RTLD_MEMBER;
58 *lib = dlopen(filename, dl_flags);
59 return *lib == NULL ? 0 : 1;
62 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
64 *sym = dlsym(lib, symname);
68 static int shlib_close(SHLIB lib)
70 return dlclose(lib) != 0 ? 0 : 1;
80 typedef HINSTANCE SHLIB;
81 typedef void *SHLIB_SYM;
83 static int shlib_load(const char *filename, SHLIB *lib)
85 *lib = LoadLibraryA(filename);
86 return *lib == NULL ? 0 : 1;
89 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
91 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
95 static int shlib_close(SHLIB lib)
97 return FreeLibrary(lib) == 0 ? 0 : 1;
102 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
104 static int test_lib(void)
106 SHLIB ssllib = SHLIB_INIT;
107 SHLIB cryptolib = SHLIB_INIT;
113 TLS_method_t myTLS_method;
114 SSL_CTX_new_t mySSL_CTX_new;
115 SSL_CTX_free_t mySSL_CTX_free;
116 ERR_get_error_t myERR_get_error;
117 OPENSSL_version_major_t myOPENSSL_version_major;
118 OPENSSL_version_minor_t myOPENSSL_version_minor;
119 OPENSSL_version_patch_t myOPENSSL_version_patch;
125 if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
129 if (!TEST_true(shlib_load(path_crypto, &cryptolib))
130 || !TEST_true(shlib_load(path_ssl, &ssllib)))
134 if (!TEST_true(shlib_load(path_ssl, &ssllib))
135 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
140 if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
141 if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
142 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
143 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
145 myTLS_method = (TLS_method_t)symbols[0].func;
146 mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
147 mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
148 if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
153 if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
154 || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_major",
156 || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_minor",
158 || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_patch",
161 myERR_get_error = (ERR_get_error_t)symbols[0].func;
162 if (!TEST_int_eq(myERR_get_error(), 0))
165 /* Make sure the libraries are a compatible version */
166 myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
167 myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
168 myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
169 if (!TEST_int_eq(myOPENSSL_version_major(), OPENSSL_VERSION_MAJOR))
171 if (!TEST_int_ge(myOPENSSL_version_minor(), OPENSSL_VERSION_MINOR))
173 if (myOPENSSL_version_minor() == OPENSSL_VERSION_MINOR
174 && !TEST_int_ge(myOPENSSL_version_patch(), OPENSSL_VERSION_PATCH))
177 if (test_type == DSO_REFTEST) {
179 DSO_dsobyaddr_t myDSO_dsobyaddr;
180 DSO_free_t myDSO_free;
183 * This is resembling the code used in ossl_init_base() and
184 * OPENSSL_atexit() to block unloading the library after dlclose().
185 * We are not testing this on Windows, because it is done there in a
186 * completely different way. Especially as a call to DSO_dsobyaddr()
187 * will always return an error, because DSO_pathbyaddr() is not
190 if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
191 || !TEST_true(shlib_sym(cryptolib, "DSO_free",
195 myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
196 myDSO_free = (DSO_free_t)symbols[1].func;
200 /* use known symbol from crypto module */
201 if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
205 # endif /* DSO_DLFCN */
211 if (!TEST_true(shlib_close(cryptolib)))
215 if (!TEST_true(shlib_close(cryptolib))
216 || !TEST_true(shlib_close(ssllib)))
220 if (!TEST_true(shlib_close(ssllib))
221 || !TEST_true(shlib_close(cryptolib)))
233 int setup_tests(void)
235 const char *p = test_get_argument(0);
237 if (strcmp(p, "-crypto_first") == 0) {
238 test_type = CRYPTO_FIRST;
239 } else if (strcmp(p, "-ssl_first") == 0) {
240 test_type = SSL_FIRST;
241 } else if (strcmp(p, "-just_crypto") == 0) {
242 test_type = JUST_CRYPTO;
243 } else if (strcmp(p, "-dso_ref") == 0) {
244 test_type = DSO_REFTEST;
246 TEST_error("Unrecognised argument");
249 if (!TEST_ptr(path_crypto = test_get_argument(1))
250 || !TEST_ptr(path_ssl = test_get_argument(2)))
253 #if defined(DSO_DLFCN) || defined(DSO_WIN32)