]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/shlibloadtest.c
Add code to run test, get malloc counts
[thirdparty/openssl.git] / test / shlibloadtest.c
1 /*
2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
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>
14 #include <openssl/ssl.h>
15 #include <openssl/ossl_typ.h>
16 #include "testutil.h"
17
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);
23
24 typedef enum test_types_en {
25 CRYPTO_FIRST,
26 SSL_FIRST,
27 JUST_CRYPTO
28 } TEST_TYPE;
29
30 static TEST_TYPE test_type;
31 static const char *path_crypto;
32 static const char *path_ssl;
33
34 #ifdef DSO_DLFCN
35
36 # include <dlfcn.h>
37
38 # define SHLIB_INIT NULL
39
40 typedef void *SHLIB;
41 typedef void *SHLIB_SYM;
42
43 static int shlib_load(const char *filename, SHLIB *lib)
44 {
45 *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
46 return *lib == NULL ? 0 : 1;
47 }
48
49 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
50 {
51 *sym = dlsym(lib, symname);
52 return *sym != NULL;
53 }
54
55 static int shlib_close(SHLIB lib)
56 {
57 return dlclose(lib) != 0 ? 0 : 1;
58 }
59 #endif
60
61 #ifdef DSO_WIN32
62
63 # include <windows.h>
64
65 # define SHLIB_INIT 0
66
67 typedef HINSTANCE SHLIB;
68 typedef void *SHLIB_SYM;
69
70 static int shlib_load(const char *filename, SHLIB *lib)
71 {
72 *lib = LoadLibraryA(filename);
73 return *lib == NULL ? 0 : 1;
74 }
75
76 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
77 {
78 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
79 return *sym != NULL;
80 }
81
82 static int shlib_close(SHLIB lib)
83 {
84 return FreeLibrary(lib) == 0 ? 0 : 1;
85 }
86 #endif
87
88
89 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
90
91 static int test_lib(void)
92 {
93 SHLIB ssllib = SHLIB_INIT;
94 SHLIB cryptolib = SHLIB_INIT;
95 SSL_CTX *ctx;
96 union {
97 void (*func)(void);
98 SHLIB_SYM sym;
99 } symbols[3];
100 TLS_method_t myTLS_method;
101 SSL_CTX_new_t mySSL_CTX_new;
102 SSL_CTX_free_t mySSL_CTX_free;
103 ERR_get_error_t myERR_get_error;
104 OpenSSL_version_num_t myOpenSSL_version_num;
105 int result = 0;
106
107 switch (test_type) {
108 case JUST_CRYPTO:
109 if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
110 goto end;
111 break;
112 case CRYPTO_FIRST:
113 if (!TEST_true(shlib_load(path_crypto, &cryptolib))
114 || !TEST_true(shlib_load(path_ssl, &ssllib)))
115 goto end;
116 break;
117 case SSL_FIRST:
118 if (!TEST_true(shlib_load(path_ssl, &ssllib))
119 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
120 goto end;
121 break;
122 }
123
124 if (test_type != JUST_CRYPTO) {
125 if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
126 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
127 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
128 goto end;
129 myTLS_method = (TLS_method_t)symbols[0].func;
130 mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
131 mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
132 if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
133 goto end;
134 mySSL_CTX_free(ctx);
135 }
136
137 if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
138 || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
139 &symbols[1].sym)))
140 goto end;
141 myERR_get_error = (ERR_get_error_t)symbols[0].func;
142 if (!TEST_int_eq(myERR_get_error(), 0))
143 goto end;
144 myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
145 if (!TEST_int_eq(myOpenSSL_version_num(), OPENSSL_VERSION_NUMBER))
146 goto end;
147
148 switch (test_type) {
149 case JUST_CRYPTO:
150 if (!TEST_true(shlib_close(cryptolib)))
151 goto end;
152 break;
153 case CRYPTO_FIRST:
154 if (!TEST_true(shlib_close(cryptolib))
155 || !TEST_true(shlib_close(ssllib)))
156 goto end;
157 break;
158 case SSL_FIRST:
159 if (!TEST_true(shlib_close(ssllib))
160 || !TEST_true(shlib_close(cryptolib)))
161 goto end;
162 break;
163 }
164
165 result = 1;
166 end:
167 return result;
168 }
169 #endif
170
171
172 int setup_tests(void)
173 {
174 const char *p = test_get_argument(0);
175
176 if (strcmp(p, "-crypto_first") == 0) {
177 test_type = CRYPTO_FIRST;
178 } else if (strcmp(p, "-ssl_first") == 0) {
179 test_type = SSL_FIRST;
180 } else if (strcmp(p, "-just_crypto") == 0) {
181 test_type = JUST_CRYPTO;
182 } else {
183 TEST_error("Unrecognised argument");
184 return 0;
185 }
186 if (!TEST_ptr(path_crypto = test_get_argument(1))
187 || !TEST_ptr(path_ssl = test_get_argument(2)))
188 return 0;
189
190 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
191 ADD_TEST(test_lib);
192 #endif
193 return 1;
194 }