shlibloadtest.c 5.1 KB
Newer Older
1
/*
2
 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/opensslv.h>
14 15 16
#include <openssl/ssl.h>
#include <openssl/ossl_typ.h>
#include "testutil.h"
17 18 19 20 21 22 23

typedef const SSL_METHOD * (*TLS_method_t)(void);
typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
typedef void (*SSL_CTX_free_t)(SSL_CTX *);
typedef unsigned long (*ERR_get_error_t)(void);
typedef unsigned long (*OpenSSL_version_num_t)(void);

24 25 26 27 28 29 30 31 32 33
typedef enum test_types_en {
    CRYPTO_FIRST,
    SSL_FIRST,
    JUST_CRYPTO
} TEST_TYPE;

static TEST_TYPE test_type;
static const char *path_crypto;
static const char *path_ssl;

34 35 36 37
#ifdef DSO_DLFCN

# include <dlfcn.h>

38 39
# define SHLIB_INIT NULL

40 41
typedef void *SHLIB;
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 50 51 52 53 54
}

static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
{
    *sym = dlsym(lib, symname);
    return *sym != NULL;
}

55
# ifdef OPENSSL_USE_NODELETE
56 57
static int shlib_close(SHLIB lib)
{
58
    return dlclose(lib) != 0 ? 0 : 1;
59
}
60
# endif
61
#endif
62

63
#ifdef DSO_WIN32
64 65 66

# include <windows.h>

67 68
# define SHLIB_INIT 0

69
typedef HINSTANCE SHLIB;
70
typedef void *SHLIB_SYM;
71

72
static int shlib_load(const char *filename, SHLIB *lib)
73 74
{
    *lib = LoadLibraryA(filename);
75
    return *lib == NULL ? 0 : 1;
76 77 78 79 80 81 82 83
}

static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
{
    *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
    return *sym != NULL;
}

84
# ifdef OPENSSL_USE_NODELETE
85 86
static int shlib_close(SHLIB lib)
{
87
    return FreeLibrary(lib) == 0 ? 0 : 1;
88
}
89
# endif
90 91 92
#endif


93
#if defined(DSO_DLFCN) || defined(DSO_WIN32)
94 95

static int test_lib(void)
96
{
97 98
    SHLIB ssllib = SHLIB_INIT;
    SHLIB cryptolib = SHLIB_INIT;
99 100
    SSL_CTX *ctx;
    union {
101
        void (*func)(void);
102
        SHLIB_SYM sym;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    } symbols[3];
    TLS_method_t myTLS_method;
    SSL_CTX_new_t mySSL_CTX_new;
    SSL_CTX_free_t mySSL_CTX_free;
    ERR_get_error_t myERR_get_error;
    OpenSSL_version_num_t myOpenSSL_version_num;
    int result = 0;

    switch (test_type) {
    case JUST_CRYPTO:
        if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
            goto end;
        break;
    case CRYPTO_FIRST:
        if (!TEST_true(shlib_load(path_crypto, &cryptolib))
                || !TEST_true(shlib_load(path_ssl, &ssllib)))
            goto end;
120
        break;
121 122 123 124 125
    case SSL_FIRST:
        if (!TEST_true(shlib_load(path_ssl, &ssllib))
                || !TEST_true(shlib_load(path_crypto, &cryptolib)))
            goto end;
        break;
126 127 128
    }

    if (test_type != JUST_CRYPTO) {
129 130 131 132 133 134 135 136 137 138
        if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
                || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
                || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
            goto end;
        myTLS_method = (TLS_method_t)symbols[0].func;
        mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
        mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
        if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
            goto end;
        mySSL_CTX_free(ctx);
139 140
    }

141 142 143 144 145 146 147 148 149 150 151
    if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
            || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
                                    &symbols[1].sym)))
        goto end;
    myERR_get_error = (ERR_get_error_t)symbols[0].func;
    if (!TEST_int_eq(myERR_get_error(), 0))
        goto end;
    myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
    if (!TEST_int_eq(myOpenSSL_version_num(), OPENSSL_VERSION_NUMBER))
        goto end;

152
#ifdef OPENSSL_USE_NODELETE
153 154 155 156 157 158 159 160 161
    switch (test_type) {
    case JUST_CRYPTO:
        if (!TEST_true(shlib_close(cryptolib)))
            goto end;
        break;
    case CRYPTO_FIRST:
        if (!TEST_true(shlib_close(cryptolib))
                || !TEST_true(shlib_close(ssllib)))
            goto end;
162
        break;
163 164 165 166 167
    case SSL_FIRST:
        if (!TEST_true(shlib_close(ssllib))
                || !TEST_true(shlib_close(cryptolib)))
            goto end;
        break;
168
    }
169
#endif
170

171 172 173 174
    result = 1;
end:
    return result;
}
175 176
#endif

177

178 179 180 181 182
int test_main(int argc, char **argv)
{
    if (argc != 4) {
        TEST_error("Unexpected number of arguments");
        return EXIT_FAILURE;
183 184
    }

185 186 187 188 189 190 191 192 193
    if (strcmp(argv[1], "-crypto_first") == 0) {
        test_type = CRYPTO_FIRST;
    } else if (strcmp(argv[1], "-ssl_first") == 0) {
        test_type = SSL_FIRST;
    } else if (strcmp(argv[1], "-just_crypto") == 0) {
        test_type = JUST_CRYPTO;
    } else {
        TEST_error("Unrecognised argument");
        return EXIT_FAILURE;
194
    }
195 196
    path_crypto = argv[2];
    path_ssl = argv[3];
197

198
#if defined(DSO_DLFCN) || defined(DSO_WIN32)
199
    ADD_TEST(test_lib);
200
#endif
201
    return run_tests(argv[0]);
202
}