Fix bio callback backward compatibility
[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
145     /*
146      * The bits that COMPATIBILITY_MASK lets through MUST be the same in
147      * the library and in the application.
148      * The bits that are masked away MUST be a larger or equal number in
149      * the library compared to the application.
150      */
151 # define COMPATIBILITY_MASK 0xfff00000L
152     myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
153     if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK,
154                      OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK)
155         goto end;
156     if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK,
157                      OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK)
158         goto end;
159
160     switch (test_type) {
161     case JUST_CRYPTO:
162         if (!TEST_true(shlib_close(cryptolib)))
163             goto end;
164         break;
165     case CRYPTO_FIRST:
166         if (!TEST_true(shlib_close(cryptolib))
167                 || !TEST_true(shlib_close(ssllib)))
168             goto end;
169         break;
170     case SSL_FIRST:
171         if (!TEST_true(shlib_close(ssllib))
172                 || !TEST_true(shlib_close(cryptolib)))
173             goto end;
174         break;
175     }
176
177     result = 1;
178 end:
179     return result;
180 }
181 #endif
182
183
184 int setup_tests(void)
185 {
186     const char *p = test_get_argument(0);
187
188     if (strcmp(p, "-crypto_first") == 0) {
189         test_type = CRYPTO_FIRST;
190     } else if (strcmp(p, "-ssl_first") == 0) {
191         test_type = SSL_FIRST;
192     } else if (strcmp(p, "-just_crypto") == 0) {
193         test_type = JUST_CRYPTO;
194     } else {
195         TEST_error("Unrecognised argument");
196         return 0;
197     }
198     if (!TEST_ptr(path_crypto = test_get_argument(1))
199             || !TEST_ptr(path_ssl = test_get_argument(2)))
200         return 0;
201
202 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
203     ADD_TEST(test_lib);
204 #endif
205     return 1;
206 }