Fix shlibloadtest to properly execute the dso_ref test
[oweals/openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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 "internal/dso_conf.h"
17 #include "testutil.h"
18
19 typedef void DSO;
20
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);
30
31 typedef enum test_types_en {
32     CRYPTO_FIRST,
33     SSL_FIRST,
34     JUST_CRYPTO,
35     DSO_REFTEST
36 } TEST_TYPE;
37
38 static TEST_TYPE test_type;
39 static const char *path_crypto;
40 static const char *path_ssl;
41
42 #ifdef DSO_DLFCN
43
44 # include <dlfcn.h>
45
46 # define SHLIB_INIT NULL
47
48 typedef void *SHLIB;
49 typedef void *SHLIB_SYM;
50
51 static int shlib_load(const char *filename, SHLIB *lib)
52 {
53     int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
54 #ifdef _AIX
55     if (filename[strlen(filename) - 1] == ')')
56         dl_flags |= RTLD_MEMBER;
57 #endif
58     *lib = dlopen(filename, dl_flags);
59     return *lib == NULL ? 0 : 1;
60 }
61
62 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
63 {
64     *sym = dlsym(lib, symname);
65     return *sym != NULL;
66 }
67
68 static int shlib_close(SHLIB lib)
69 {
70     return dlclose(lib) != 0 ? 0 : 1;
71 }
72 #endif
73
74 #ifdef DSO_WIN32
75
76 # include <windows.h>
77
78 # define SHLIB_INIT 0
79
80 typedef HINSTANCE SHLIB;
81 typedef void *SHLIB_SYM;
82
83 static int shlib_load(const char *filename, SHLIB *lib)
84 {
85     *lib = LoadLibraryA(filename);
86     return *lib == NULL ? 0 : 1;
87 }
88
89 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
90 {
91     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
92     return *sym != NULL;
93 }
94
95 static int shlib_close(SHLIB lib)
96 {
97     return FreeLibrary(lib) == 0 ? 0 : 1;
98 }
99 #endif
100
101
102 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
103
104 static int test_lib(void)
105 {
106     SHLIB ssllib = SHLIB_INIT;
107     SHLIB cryptolib = SHLIB_INIT;
108     SSL_CTX *ctx;
109     union {
110         void (*func)(void);
111         SHLIB_SYM sym;
112     } symbols[4];
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;
120     int result = 0;
121
122     switch (test_type) {
123     case JUST_CRYPTO:
124     case DSO_REFTEST:
125         if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
126             goto end;
127         break;
128     case CRYPTO_FIRST:
129         if (!TEST_true(shlib_load(path_crypto, &cryptolib))
130                 || !TEST_true(shlib_load(path_ssl, &ssllib)))
131             goto end;
132         break;
133     case SSL_FIRST:
134         if (!TEST_true(shlib_load(path_ssl, &ssllib))
135                 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
136             goto end;
137         break;
138     }
139
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)))
144             goto end;
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())))
149             goto end;
150         mySSL_CTX_free(ctx);
151     }
152
153     if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
154            || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_major",
155                                    &symbols[1].sym))
156            || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_minor",
157                                    &symbols[2].sym))
158            || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_patch",
159                                    &symbols[3].sym)))
160         goto end;
161     myERR_get_error = (ERR_get_error_t)symbols[0].func;
162     if (!TEST_int_eq(myERR_get_error(), 0))
163         goto end;
164
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))
170         goto end;
171     if (!TEST_int_ge(myOPENSSL_version_minor(), OPENSSL_VERSION_MINOR))
172         goto end;
173     if (myOPENSSL_version_minor() == OPENSSL_VERSION_MINOR
174         && !TEST_int_ge(myOPENSSL_version_patch(), OPENSSL_VERSION_PATCH))
175         goto end;
176
177     if (test_type == DSO_REFTEST) {
178 # ifdef DSO_DLFCN
179         DSO_dsobyaddr_t myDSO_dsobyaddr;
180         DSO_free_t myDSO_free;
181
182         /*
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
188          * implemented there.
189          */
190         if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
191                 || !TEST_true(shlib_sym(cryptolib, "DSO_free",
192                                         &symbols[1].sym)))
193             goto end;
194
195         myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
196         myDSO_free = (DSO_free_t)symbols[1].func;
197
198         {
199             DSO *hndl;
200             /* use known symbol from crypto module */
201             if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
202                 goto end;
203             myDSO_free(hndl);
204         }
205 # endif /* DSO_DLFCN */
206     }
207
208     switch (test_type) {
209     case JUST_CRYPTO:
210     case DSO_REFTEST:
211         if (!TEST_true(shlib_close(cryptolib)))
212             goto end;
213         break;
214     case CRYPTO_FIRST:
215         if (!TEST_true(shlib_close(cryptolib))
216                 || !TEST_true(shlib_close(ssllib)))
217             goto end;
218         break;
219     case SSL_FIRST:
220         if (!TEST_true(shlib_close(ssllib))
221                 || !TEST_true(shlib_close(cryptolib)))
222             goto end;
223         break;
224     }
225
226     result = 1;
227 end:
228     return result;
229 }
230 #endif
231
232
233 int setup_tests(void)
234 {
235     const char *p = test_get_argument(0);
236
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;
245     } else {
246         TEST_error("Unrecognised argument");
247         return 0;
248     }
249     if (!TEST_ptr(path_crypto = test_get_argument(1))
250             || !TEST_ptr(path_ssl = test_get_argument(2)))
251         return 0;
252
253 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
254     ADD_TEST(test_lib);
255 #endif
256     return 1;
257 }