Add a test to dynamically load and unload the libraries
[openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 2016 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
15 #define SSL_CTX_NEW "SSL_CTX_new"
16 #define SSL_CTX_FREE "SSL_CTX_free"
17 #define TLS_METHOD "TLS_method"
18
19 #define ERR_GET_ERROR "ERR_get_error"
20 #define OPENSSL_VERSION_NUM_FUNC "OpenSSL_version_num"
21
22 typedef struct ssl_ctx_st SSL_CTX;
23 typedef struct ssl_method_st SSL_METHOD;
24 typedef const SSL_METHOD * (*TLS_method_t)(void);
25 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
26 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
27
28 typedef unsigned long (*ERR_get_error_t)(void);
29 typedef unsigned long (*OpenSSL_version_num_t)(void);
30
31 TLS_method_t TLS_method;
32 SSL_CTX_new_t SSL_CTX_new;
33 SSL_CTX_free_t SSL_CTX_free;
34
35 ERR_get_error_t ERR_get_error;
36 OpenSSL_version_num_t OpenSSL_version_num;
37
38
39 #ifdef DSO_DLFCN
40
41 # include <dlfcn.h>
42
43 typedef void * SHLIB;
44 typedef void * SHLIB_SYM;
45
46 # define SHARED_LIBRARY_SUFFIX ".so"
47
48 static int shlib_load(char *filename, SHLIB *lib)
49 {
50     char *tmpfile;
51     size_t filenamelen = strlen(filename);
52
53     /* Total length = base filename len + suffix len + 1 for NULL terminator */
54     tmpfile = malloc(filenamelen + sizeof(SHARED_LIBRARY_SUFFIX) + 1);
55     if (tmpfile == NULL)
56         return 0;
57     strcpy(tmpfile, filename);
58     strcpy(tmpfile + filenamelen, SHARED_LIBRARY_SUFFIX);
59
60     *lib = dlopen(tmpfile, RTLD_GLOBAL | RTLD_LAZY);
61     free(tmpfile);
62
63     if (*lib == NULL)
64         return 0;
65
66     return 1;
67 }
68
69 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
70 {
71     *sym = dlsym(lib, symname);
72
73     return *sym != NULL;
74 }
75
76 static int shlib_close(SHLIB lib)
77 {
78     if (dlclose(lib) != 0)
79         return 0;
80
81     return 1;
82 }
83
84 #elif defined(DSO_WIN32)
85
86 # include <windows.h>
87
88 typedef HINSTANCE SHLIB;
89 typedef void * SHLIB_SYM;
90
91 static int shlib_load(char *filename, SHLIB *lib)
92 {
93     *lib = LoadLibraryA(filename);
94     if (*lib == NULL)
95         return 0;
96
97     return 1;
98 }
99
100 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
101 {
102     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
103
104     return *sym != NULL;
105 }
106
107 static int shlib_close(SHLIB lib)
108 {
109     if (FreeLibrary(lib) == 0)
110         return 0;
111
112     return 1;
113 }
114
115 #endif
116
117 /* The test is only currently implemented for DSO_DLFCN and DSO_WIN32 */
118 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
119
120 # define CRYPTO_FIRST_OPT    "-crypto_first"
121 # define SSL_FIRST_OPT       "-ssl_first"
122 # define JUST_CRYPTO_OPT     "-just_crypto"
123
124 enum test_types_en {
125     CRYPTO_FIRST,
126     SSL_FIRST,
127     JUST_CRYPTO
128 };
129
130 int main(int argc, char **argv)
131 {
132     SHLIB ssllib, cryptolib;
133     SSL_CTX *ctx;
134     union {
135         void (*func) (void);
136         SHLIB_SYM sym;
137     } tls_method_sym, ssl_ctx_new_sym, ssl_ctx_free_sym, err_get_error_sym,
138     openssl_version_num_sym;
139     enum test_types_en test_type;
140     int i;
141
142     if (argc != 4) {
143         printf("Unexpected number of arguments\n");
144         return 1;
145     }
146
147     if (strcmp(argv[1], CRYPTO_FIRST_OPT) == 0) {
148         test_type = CRYPTO_FIRST;
149     } else if (strcmp(argv[1], SSL_FIRST_OPT) == 0) {
150             test_type = SSL_FIRST;
151     } else if (strcmp(argv[1], JUST_CRYPTO_OPT) == 0) {
152             test_type = JUST_CRYPTO;
153     } else {
154         printf("Unrecognised argument\n");
155         return 1;
156     }
157
158     for (i = 0; i < 2; i++) {
159         if ((i == 0 && (test_type == CRYPTO_FIRST
160                        || test_type == JUST_CRYPTO))
161                || (i == 1 && test_type == SSL_FIRST)) {
162             if (!shlib_load(argv[2], &cryptolib)) {
163                 printf("Unable to load libcrypto\n");
164                 return 1;
165             }
166         }
167         if ((i == 0 && test_type == SSL_FIRST)
168                 || (i == 1 && test_type == CRYPTO_FIRST)) {
169             if (!shlib_load(argv[3], &ssllib)) {
170                 printf("Unable to load libssl\n");
171                 return 1;
172             }
173         }
174     }
175
176     if (test_type != JUST_CRYPTO) {
177         if (!shlib_sym(ssllib, TLS_METHOD, &tls_method_sym.sym)
178                 || !shlib_sym(ssllib, SSL_CTX_NEW, &ssl_ctx_new_sym.sym)
179                 || !shlib_sym(ssllib, SSL_CTX_FREE, &ssl_ctx_free_sym.sym)) {
180             printf("Unable to load ssl symbols\n");
181             return 1;
182         }
183
184         TLS_method = (TLS_method_t)tls_method_sym.func;
185         SSL_CTX_new = (SSL_CTX_new_t)ssl_ctx_new_sym.func;
186         SSL_CTX_free = (SSL_CTX_free_t)ssl_ctx_free_sym.func;
187
188         ctx = SSL_CTX_new(TLS_method());
189         if (ctx == NULL) {
190             printf("Unable to create SSL_CTX\n");
191             return 1;
192         }
193         SSL_CTX_free(ctx);
194     }
195
196     if (!shlib_sym(cryptolib, ERR_GET_ERROR, &err_get_error_sym.sym)
197             || !shlib_sym(cryptolib, OPENSSL_VERSION_NUM_FUNC,
198                           &openssl_version_num_sym.sym)) {
199         printf("Unable to load crypto symbols\n");
200         return 1;
201     }
202
203     ERR_get_error = (ERR_get_error_t)err_get_error_sym.func;
204     OpenSSL_version_num = (OpenSSL_version_num_t)openssl_version_num_sym.func;
205
206     if (ERR_get_error() != 0) {
207         printf("Unexpected error in error queue\n");
208         return 1;
209     }
210
211     if (OpenSSL_version_num() != OPENSSL_VERSION_NUMBER) {
212         printf("Unexpected library version loaded\n");
213         return 1;
214     }
215
216     for (i = 0; i < 2; i++) {
217         if ((i == 0 && test_type == CRYPTO_FIRST)
218                 || (i == 1 && test_type == SSL_FIRST)) {
219             if (!shlib_close(ssllib)) {
220                 printf("Unable to close libssl\n");
221                 return 1;
222             }
223         }
224         if ((i == 0 && (test_type == SSL_FIRST
225                        || test_type == JUST_CRYPTO))
226                 || (i == 1 && test_type == CRYPTO_FIRST)) {
227             if (!shlib_close(cryptolib)) {
228                 printf("Unable to close libcrypto\n");
229                 return 1;
230             }
231         }
232     }
233
234     printf("Success\n");
235     return 0;
236 }
237 #else
238 int main(void)
239 {
240     printf("Test not implemented on this platform\n");
241     return 0;
242 }
243 #endif