Revise and cleanup; use strict,warnings
[openssl.git] / test / shlibloadtest.c
index 0a4d5f7cd662a18b62cf5c5a19ed933b2dbac7f3..0bf24eee074a968830c59b0dc8191ebf4cb8a570 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
 #include <openssl/ossl_typ.h>
 #include "testutil.h"
 
-#if !defined(DSO_DLFCN) && !defined(DSO_WIN32)
-int main(void)
-{
-    TEST_info("Not implemented on this platform\n");
-    return 0;
-}
-
-#else
-
 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);
 
+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;
+
 #ifdef DSO_DLFCN
 
 # include <dlfcn.h>
 
+# define SHLIB_INIT NULL
+
 typedef void *SHLIB;
 typedef void *SHLIB_SYM;
-# define SHLIB_INIT NULL
 
 static int shlib_load(const char *filename, SHLIB *lib)
 {
@@ -60,9 +62,10 @@ static int shlib_close(SHLIB lib)
 
 # include <windows.h>
 
+# define SHLIB_INIT 0
+
 typedef HINSTANCE SHLIB;
 typedef void *SHLIB_SYM;
-# define SHLIB_INIT 0
 
 static int shlib_load(const char *filename, SHLIB *lib)
 {
@@ -82,15 +85,8 @@ static int shlib_close(SHLIB lib)
 }
 #endif
 
-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;
+#if defined(DSO_DLFCN) || defined(DSO_WIN32)
 
 static int test_lib(void)
 {
@@ -117,6 +113,7 @@ static int test_lib(void)
         if (!TEST_true(shlib_load(path_crypto, &cryptolib))
                 || !TEST_true(shlib_load(path_ssl, &ssllib)))
             goto end;
+        break;
     case SSL_FIRST:
         if (!TEST_true(shlib_load(path_ssl, &ssllib))
                 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
@@ -144,8 +141,20 @@ static int test_lib(void)
     myERR_get_error = (ERR_get_error_t)symbols[0].func;
     if (!TEST_int_eq(myERR_get_error(), 0))
         goto end;
+
+    /*
+     * The bits that COMPATIBILITY_MASK lets through MUST be the same in
+     * the library and in the application.
+     * The bits that are masked away MUST be a larger or equal number in
+     * the library compared to the application.
+     */
+# define COMPATIBILITY_MASK 0xfff00000L
     myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
-    if (!TEST_int_eq(myOpenSSL_version_num(), OPENSSL_VERSION_NUMBER))
+    if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK,
+                     OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK)
+        goto end;
+    if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK,
+                     OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK)
         goto end;
 
     switch (test_type) {
@@ -157,6 +166,7 @@ static int test_lib(void)
         if (!TEST_true(shlib_close(cryptolib))
                 || !TEST_true(shlib_close(ssllib)))
             goto end;
+        break;
     case SSL_FIRST:
         if (!TEST_true(shlib_close(ssllib))
                 || !TEST_true(shlib_close(cryptolib)))
@@ -168,28 +178,29 @@ static int test_lib(void)
 end:
     return result;
 }
+#endif
 
-int test_main(int argc, char **argv)
+
+int setup_tests(void)
 {
-    if (argc != 4) {
-        TEST_error("Unexpected number of arguments");
-        return EXIT_FAILURE;
-    }
+    const char *p = test_get_argument(0);
 
-    if (strcmp(argv[1], "-crypto_first") == 0) {
+    if (strcmp(p, "-crypto_first") == 0) {
         test_type = CRYPTO_FIRST;
-    } else if (strcmp(argv[1], "-ssl_first") == 0) {
+    } else if (strcmp(p, "-ssl_first") == 0) {
         test_type = SSL_FIRST;
-    } else if (strcmp(argv[1], "-just_crypto") == 0) {
+    } else if (strcmp(p, "-just_crypto") == 0) {
         test_type = JUST_CRYPTO;
     } else {
         TEST_error("Unrecognised argument");
-        return EXIT_FAILURE;
+        return 0;
     }
-    path_crypto = argv[2];
-    path_ssl = argv[3];
+    if (!TEST_ptr(path_crypto = test_get_argument(1))
+            || !TEST_ptr(path_ssl = test_get_argument(2)))
+        return 0;
 
+#if defined(DSO_DLFCN) || defined(DSO_WIN32)
     ADD_TEST(test_lib);
-    return run_tests(argv[0]);
-}
 #endif
+    return 1;
+}