2 * Copyright 2016-2017 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>
18 typedef const SSL_METHOD
* (*TLS_method_t
)(void);
19 typedef SSL_CTX
* (*SSL_CTX_new_t
)(const SSL_METHOD
*meth
);
20 typedef void (*SSL_CTX_free_t
)(SSL_CTX
*);
21 typedef unsigned long (*ERR_get_error_t
)(void);
22 typedef unsigned long (*OpenSSL_version_num_t
)(void);
24 typedef enum test_types_en
{
30 static TEST_TYPE test_type
;
31 static const char *path_crypto
;
32 static const char *path_ssl
;
38 # define SHLIB_INIT NULL
41 typedef void *SHLIB_SYM
;
43 static int shlib_load(const char *filename
, SHLIB
*lib
)
45 *lib
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
46 return *lib
== NULL
? 0 : 1;
49 static int shlib_sym(SHLIB lib
, const char *symname
, SHLIB_SYM
*sym
)
51 *sym
= dlsym(lib
, symname
);
55 # ifdef OPENSSL_USE_NODELETE
56 static int shlib_close(SHLIB lib
)
58 return dlclose(lib
) != 0 ? 0 : 1;
69 typedef HINSTANCE SHLIB
;
70 typedef void *SHLIB_SYM
;
72 static int shlib_load(const char *filename
, SHLIB
*lib
)
74 *lib
= LoadLibraryA(filename
);
75 return *lib
== NULL
? 0 : 1;
78 static int shlib_sym(SHLIB lib
, const char *symname
, SHLIB_SYM
*sym
)
80 *sym
= (SHLIB_SYM
)GetProcAddress(lib
, symname
);
84 # ifdef OPENSSL_USE_NODELETE
85 static int shlib_close(SHLIB lib
)
87 return FreeLibrary(lib
) == 0 ? 0 : 1;
93 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
95 static int test_lib(void)
97 SHLIB ssllib
= SHLIB_INIT
;
98 SHLIB cryptolib
= SHLIB_INIT
;
104 TLS_method_t myTLS_method
;
105 SSL_CTX_new_t mySSL_CTX_new
;
106 SSL_CTX_free_t mySSL_CTX_free
;
107 ERR_get_error_t myERR_get_error
;
108 OpenSSL_version_num_t myOpenSSL_version_num
;
113 if (!TEST_true(shlib_load(path_crypto
, &cryptolib
)))
117 if (!TEST_true(shlib_load(path_crypto
, &cryptolib
))
118 || !TEST_true(shlib_load(path_ssl
, &ssllib
)))
122 if (!TEST_true(shlib_load(path_ssl
, &ssllib
))
123 || !TEST_true(shlib_load(path_crypto
, &cryptolib
)))
128 if (test_type
!= JUST_CRYPTO
) {
129 if (!TEST_true(shlib_sym(ssllib
, "TLS_method", &symbols
[0].sym
))
130 || !TEST_true(shlib_sym(ssllib
, "SSL_CTX_new", &symbols
[1].sym
))
131 || !TEST_true(shlib_sym(ssllib
, "SSL_CTX_free", &symbols
[2].sym
)))
133 myTLS_method
= (TLS_method_t
)symbols
[0].func
;
134 mySSL_CTX_new
= (SSL_CTX_new_t
)symbols
[1].func
;
135 mySSL_CTX_free
= (SSL_CTX_free_t
)symbols
[2].func
;
136 if (!TEST_ptr(ctx
= mySSL_CTX_new(myTLS_method())))
141 if (!TEST_true(shlib_sym(cryptolib
, "ERR_get_error", &symbols
[0].sym
))
142 || !TEST_true(shlib_sym(cryptolib
, "OpenSSL_version_num",
145 myERR_get_error
= (ERR_get_error_t
)symbols
[0].func
;
146 if (!TEST_int_eq(myERR_get_error(), 0))
148 myOpenSSL_version_num
= (OpenSSL_version_num_t
)symbols
[1].func
;
149 if (!TEST_int_eq(myOpenSSL_version_num(), OPENSSL_VERSION_NUMBER
))
152 #ifdef OPENSSL_USE_NODELETE
155 if (!TEST_true(shlib_close(cryptolib
)))
159 if (!TEST_true(shlib_close(cryptolib
))
160 || !TEST_true(shlib_close(ssllib
)))
164 if (!TEST_true(shlib_close(ssllib
))
165 || !TEST_true(shlib_close(cryptolib
)))
178 int setup_tests(void)
180 const char *p
= test_get_argument(0);
182 if (strcmp(p
, "-crypto_first") == 0) {
183 test_type
= CRYPTO_FIRST
;
184 } else if (strcmp(p
, "-ssl_first") == 0) {
185 test_type
= SSL_FIRST
;
186 } else if (strcmp(p
, "-just_crypto") == 0) {
187 test_type
= JUST_CRYPTO
;
189 TEST_error("Unrecognised argument");
192 if (!TEST_ptr(path_crypto
= test_get_argument(1))
193 || !TEST_ptr(path_ssl
= test_get_argument(2)))
196 #if defined(DSO_DLFCN) || defined(DSO_WIN32)