Following the license change, modify the boilerplates in test/
[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         if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
125             goto end;
126         break;
127     case CRYPTO_FIRST:
128         if (!TEST_true(shlib_load(path_crypto, &cryptolib))
129                 || !TEST_true(shlib_load(path_ssl, &ssllib)))
130             goto end;
131         break;
132     case SSL_FIRST:
133         if (!TEST_true(shlib_load(path_ssl, &ssllib))
134                 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
135             goto end;
136         break;
137     case DSO_REFTEST:
138         if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
139             goto end;
140         break;
141     }
142
143     if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
144         if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
145                 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
146                 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
147             goto end;
148         myTLS_method = (TLS_method_t)symbols[0].func;
149         mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
150         mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
151         if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
152             goto end;
153         mySSL_CTX_free(ctx);
154     }
155
156     if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
157            || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_major",
158                                    &symbols[1].sym))
159            || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_minor",
160                                    &symbols[2].sym))
161            || !TEST_true(shlib_sym(cryptolib, "OPENSSL_version_patch",
162                                    &symbols[3].sym)))
163         goto end;
164     myERR_get_error = (ERR_get_error_t)symbols[0].func;
165     if (!TEST_int_eq(myERR_get_error(), 0))
166         goto end;
167
168     /* Make sure the libraries are a compatible version */
169     myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
170     myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
171     myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
172     if (!TEST_int_eq(myOPENSSL_version_major(), OPENSSL_VERSION_MAJOR))
173         goto end;
174     if (!TEST_int_ge(myOPENSSL_version_minor(), OPENSSL_VERSION_MINOR))
175         goto end;
176     if (myOPENSSL_version_minor() == OPENSSL_VERSION_MINOR
177         && !TEST_int_ge(myOPENSSL_version_patch(), OPENSSL_VERSION_PATCH))
178         goto end;
179
180     if (test_type == DSO_REFTEST) {
181 # ifdef DSO_DLFCN
182         DSO_dsobyaddr_t myDSO_dsobyaddr;
183         DSO_free_t myDSO_free;
184
185         /*
186          * This is resembling the code used in ossl_init_base() and
187          * OPENSSL_atexit() to block unloading the library after dlclose().
188          * We are not testing this on Windows, because it is done there in a
189          * completely different way. Especially as a call to DSO_dsobyaddr()
190          * will always return an error, because DSO_pathbyaddr() is not
191          * implemented there.
192          */
193         if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
194                 || !TEST_true(shlib_sym(cryptolib, "DSO_free",
195                                         &symbols[1].sym)))
196             goto end;
197
198         myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
199         myDSO_free = (DSO_free_t)symbols[1].func;
200
201         {
202             DSO *hndl;
203             /* use known symbol from crypto module */
204             if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
205                 goto end;
206             myDSO_free(hndl);
207         }
208 # endif /* DSO_DLFCN */
209     }
210
211     switch (test_type) {
212     case JUST_CRYPTO:
213         if (!TEST_true(shlib_close(cryptolib)))
214             goto end;
215         break;
216     case CRYPTO_FIRST:
217         if (!TEST_true(shlib_close(cryptolib))
218                 || !TEST_true(shlib_close(ssllib)))
219             goto end;
220         break;
221     case SSL_FIRST:
222         if (!TEST_true(shlib_close(ssllib))
223                 || !TEST_true(shlib_close(cryptolib)))
224             goto end;
225         break;
226     case DSO_REFTEST:
227         if (!TEST_true(shlib_close(cryptolib)))
228             goto end;
229         break;
230     }
231
232     result = 1;
233 end:
234     return result;
235 }
236 #endif
237
238
239 int setup_tests(void)
240 {
241     const char *p = test_get_argument(0);
242
243     if (strcmp(p, "-crypto_first") == 0) {
244         test_type = CRYPTO_FIRST;
245     } else if (strcmp(p, "-ssl_first") == 0) {
246         test_type = SSL_FIRST;
247     } else if (strcmp(p, "-just_crypto") == 0) {
248         test_type = JUST_CRYPTO;
249     } else if (strcmp(p, "-dso_ref") == 0) {
250         test_type = JUST_CRYPTO;
251     } else {
252         TEST_error("Unrecognised argument");
253         return 0;
254     }
255     if (!TEST_ptr(path_crypto = test_get_argument(1))
256             || !TEST_ptr(path_ssl = test_get_argument(2)))
257         return 0;
258
259 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
260     ADD_TEST(test_lib);
261 #endif
262     return 1;
263 }