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