]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/shlibloadtest.c
test/recipes/80-test_tsa.t: Don't trust 'OPENSSL_CONF'
[thirdparty/openssl.git] / test / shlibloadtest.c
CommitLineData
b987d748 1/*
5511101a 2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
b987d748
MC
3 *
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
8 */
9
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13#include <openssl/opensslv.h>
bdd07c78
RS
14#include <openssl/ssl.h>
15#include <openssl/ossl_typ.h>
16#include "testutil.h"
b987d748 17
b987d748
MC
18typedef const SSL_METHOD * (*TLS_method_t)(void);
19typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
20typedef void (*SSL_CTX_free_t)(SSL_CTX *);
b987d748
MC
21typedef unsigned long (*ERR_get_error_t)(void);
22typedef unsigned long (*OpenSSL_version_num_t)(void);
23
e0011aa8
RS
24typedef enum test_types_en {
25 CRYPTO_FIRST,
26 SSL_FIRST,
27 JUST_CRYPTO
28} TEST_TYPE;
29
30static TEST_TYPE test_type;
31static const char *path_crypto;
32static const char *path_ssl;
33
b987d748
MC
34#ifdef DSO_DLFCN
35
36# include <dlfcn.h>
37
e0011aa8
RS
38# define SHLIB_INIT NULL
39
bdd07c78
RS
40typedef void *SHLIB;
41typedef void *SHLIB_SYM;
b987d748 42
62dd3351 43static int shlib_load(const char *filename, SHLIB *lib)
b987d748 44{
62dd3351 45 *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
bdd07c78 46 return *lib == NULL ? 0 : 1;
b987d748
MC
47}
48
49static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
50{
51 *sym = dlsym(lib, symname);
b987d748
MC
52 return *sym != NULL;
53}
54
e0011aa8 55# ifdef OPENSSL_USE_NODELETE
b987d748
MC
56static int shlib_close(SHLIB lib)
57{
bdd07c78 58 return dlclose(lib) != 0 ? 0 : 1;
b987d748 59}
e0011aa8 60# endif
bdd07c78 61#endif
b987d748 62
bdd07c78 63#ifdef DSO_WIN32
b987d748
MC
64
65# include <windows.h>
66
e0011aa8
RS
67# define SHLIB_INIT 0
68
b987d748 69typedef HINSTANCE SHLIB;
bdd07c78 70typedef void *SHLIB_SYM;
b987d748 71
62dd3351 72static int shlib_load(const char *filename, SHLIB *lib)
b987d748
MC
73{
74 *lib = LoadLibraryA(filename);
bdd07c78 75 return *lib == NULL ? 0 : 1;
b987d748
MC
76}
77
78static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
79{
80 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
b987d748
MC
81 return *sym != NULL;
82}
83
e0011aa8 84# ifdef OPENSSL_USE_NODELETE
b987d748
MC
85static int shlib_close(SHLIB lib)
86{
bdd07c78 87 return FreeLibrary(lib) == 0 ? 0 : 1;
b987d748 88}
e0011aa8 89# endif
b987d748
MC
90#endif
91
b987d748 92
e0011aa8 93#if defined(DSO_DLFCN) || defined(DSO_WIN32)
bdd07c78
RS
94
95static int test_lib(void)
b987d748 96{
bdd07c78
RS
97 SHLIB ssllib = SHLIB_INIT;
98 SHLIB cryptolib = SHLIB_INIT;
b987d748
MC
99 SSL_CTX *ctx;
100 union {
bdd07c78 101 void (*func)(void);
b987d748 102 SHLIB_SYM sym;
bdd07c78
RS
103 } symbols[3];
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;
109 int result = 0;
110
111 switch (test_type) {
112 case JUST_CRYPTO:
113 if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
114 goto end;
115 break;
116 case CRYPTO_FIRST:
117 if (!TEST_true(shlib_load(path_crypto, &cryptolib))
118 || !TEST_true(shlib_load(path_ssl, &ssllib)))
119 goto end;
5511101a 120 break;
bdd07c78
RS
121 case SSL_FIRST:
122 if (!TEST_true(shlib_load(path_ssl, &ssllib))
123 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
124 goto end;
125 break;
b987d748
MC
126 }
127
128 if (test_type != JUST_CRYPTO) {
bdd07c78
RS
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)))
132 goto end;
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())))
137 goto end;
138 mySSL_CTX_free(ctx);
b987d748
MC
139 }
140
bdd07c78
RS
141 if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
142 || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
143 &symbols[1].sym)))
144 goto end;
145 myERR_get_error = (ERR_get_error_t)symbols[0].func;
146 if (!TEST_int_eq(myERR_get_error(), 0))
147 goto end;
148 myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
149 if (!TEST_int_eq(myOpenSSL_version_num(), OPENSSL_VERSION_NUMBER))
150 goto end;
151
e0011aa8 152#ifdef OPENSSL_USE_NODELETE
bdd07c78
RS
153 switch (test_type) {
154 case JUST_CRYPTO:
155 if (!TEST_true(shlib_close(cryptolib)))
156 goto end;
157 break;
158 case CRYPTO_FIRST:
159 if (!TEST_true(shlib_close(cryptolib))
160 || !TEST_true(shlib_close(ssllib)))
161 goto end;
5511101a 162 break;
bdd07c78
RS
163 case SSL_FIRST:
164 if (!TEST_true(shlib_close(ssllib))
165 || !TEST_true(shlib_close(cryptolib)))
166 goto end;
167 break;
b987d748 168 }
e0011aa8 169#endif
b987d748 170
bdd07c78
RS
171 result = 1;
172end:
173 return result;
174}
e0011aa8
RS
175#endif
176
b987d748 177
bdd07c78
RS
178int test_main(int argc, char **argv)
179{
180 if (argc != 4) {
181 TEST_error("Unexpected number of arguments");
182 return EXIT_FAILURE;
b987d748
MC
183 }
184
bdd07c78
RS
185 if (strcmp(argv[1], "-crypto_first") == 0) {
186 test_type = CRYPTO_FIRST;
187 } else if (strcmp(argv[1], "-ssl_first") == 0) {
188 test_type = SSL_FIRST;
189 } else if (strcmp(argv[1], "-just_crypto") == 0) {
190 test_type = JUST_CRYPTO;
191 } else {
192 TEST_error("Unrecognised argument");
193 return EXIT_FAILURE;
b987d748 194 }
bdd07c78
RS
195 path_crypto = argv[2];
196 path_ssl = argv[3];
b987d748 197
e0011aa8 198#if defined(DSO_DLFCN) || defined(DSO_WIN32)
bdd07c78 199 ADD_TEST(test_lib);
e0011aa8 200#endif
bdd07c78 201 return run_tests(argv[0]);
b987d748 202}