Use adapted test_get_libctx() for simpler test setup and better error reporting
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Sat, 26 Sep 2020 13:21:48 +0000 (15:21 +0200)
committerDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 10 Dec 2020 10:01:26 +0000 (11:01 +0100)
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/13001)

14 files changed:
test/acvp_test.c
test/build.info
test/cmp_client_test.c
test/cmp_msg_test.c
test/cmp_protect_test.c
test/cmp_server_test.c
test/cmp_vfy_test.c
test/evp_extra_test2.c
test/evp_libctx_test.c
test/evp_test.c
test/ssl_old_test.c
test/ssl_test.c
test/testutil.h
test/testutil/provider.c

index 1686d759dbc2b3f3309099bbd9e45f07b2bc575e..5c4a38749f3c37618bb3d0c661f0d8b93ecdac9f 100644 (file)
@@ -1434,18 +1434,9 @@ int setup_tests(void)
         }
     }
 
-    prov_null = OSSL_PROVIDER_load(NULL, "null");
-    if (prov_null == NULL) {
-        opt_printf_stderr("Failed to load null provider into default libctx\n");
+    if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
         return 0;
-    }
 
-    libctx = OSSL_LIB_CTX_new();
-    if (libctx == NULL
-        || !OSSL_LIB_CTX_load_config(libctx, config_file)) {
-        opt_printf_stderr("Failed to load config\n");
-        return 0;
-    }
     OSSL_SELF_TEST_set_callback(libctx, self_test_events, &self_test_args);
 
     ADD_ALL_TESTS(cipher_enc_dec_test, OSSL_NELEM(cipher_enc_data));
index 0386a1febf4008966fc403c7845eb799426811f4..81f9b9cb66ce5419ef57dfe95dada7bd75f75c82 100644 (file)
@@ -706,7 +706,7 @@ IF[{- !$disabled{tests} -}]
 
     SOURCE[ssl_old_test]=ssl_old_test.c helpers/predefined_dhparams.c
     INCLUDE[ssl_old_test]=.. ../include ../apps/include
-    DEPEND[ssl_old_test]=../libcrypto.a ../libssl.a
+    DEPEND[ssl_old_test]=../libcrypto.a ../libssl.a libtestutil.a
   ENDIF
 
   PROGRAMS{noinst}=asn1_time_test
index 17f932a73a41439b7a9ff9999411e4e4b67f4041..efb185402b692bbef54c8d571f9fe7e6ecdc0353 100644 (file)
@@ -366,7 +366,7 @@ int setup_tests(void)
         return 0;
     }
 
-    if (!test_get_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
+    if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
         return 0;
 
     if (!TEST_ptr(server_key = load_pem_key(server_key_f, libctx))
index 41ba5cf975b0a2100bc962ef48715965fbfabd08..0b56d66d45f2578459e5722f68f4ec245df2ddf3 100644 (file)
@@ -561,7 +561,7 @@ int setup_tests(void)
         return 0;
     }
 
-    if (!test_get_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
+    if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
         return 0;
 
     if (!TEST_ptr(newkey = load_pem_key(newkey_f, libctx))
index 3cca30144d53e7dd99fc6aa908a4e5cfab0b3bcf..d4acb716e763e2bb85f8cff25e04e1b849c471d5 100644 (file)
@@ -538,7 +538,7 @@ int setup_tests(void)
         return 0;
     }
 
-    if (!test_get_libctx(&libctx, &default_null_provider, &provider, 10, USAGE))
+    if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 10, USAGE))
         return 0;
 
     if (!TEST_ptr(loadedkey = load_pem_key(server_key_f, libctx))
index 49108bfa177f39a3a02559a61b9f808b3373e6f8..bff42c8bafbe5e427747d6204d13fe8cba406bc3 100644 (file)
@@ -145,7 +145,7 @@ int setup_tests(void)
         return 0;
     }
 
-    if (!test_get_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
+    if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
         return 0;
 
     if (!TEST_ptr(request = load_pkimsg(request_f))) {
index 67112f6489e7715967090b2807a16b0bc69c2889..d45c938335157ef892b997fb491dab13ac3e1aef 100644 (file)
@@ -600,7 +600,7 @@ int setup_tests(void)
         return 0;
     }
 
-    if (!test_get_libctx(&libctx, &default_null_provider, &provider, 14, USAGE))
+    if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 14, USAGE))
         return 0;
 
     /* Load certificates for cert chain */
index f91e66a08b35860ad56225d0955abe0ef48a0f3c..9181061247821cce10d9f3420e3841efbd054117 100644 (file)
@@ -272,13 +272,7 @@ static int test_d2i_PrivateKey_ex(void) {
 
 int setup_tests(void)
 {
-    mainctx = OSSL_LIB_CTX_new();
-
-    if (!TEST_ptr(mainctx))
-        return 0;
-
-    nullprov = OSSL_PROVIDER_load(NULL, "null");
-    if (!TEST_ptr(nullprov)) {
+    if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
         OSSL_LIB_CTX_free(mainctx);
         mainctx = NULL;
         return 0;
index cf39bf0871ef686c2f7ae8d7924f9addb4c4f93f..c306bd9a16874813631f3e8fa70823bb2ed21af8 100644 (file)
@@ -648,19 +648,7 @@ int setup_tests(void)
         }
     }
 
-    nullprov = OSSL_PROVIDER_load(NULL, "null");
-    if (!TEST_ptr(nullprov))
-        return 0;
-
-    libctx = OSSL_LIB_CTX_new();
-    if (!TEST_ptr(libctx))
-        return 0;
-    if (config_file != NULL
-        && !TEST_true(OSSL_LIB_CTX_load_config(libctx, config_file)))
-        return 0;
-
-    libprov = OSSL_PROVIDER_load(libctx, prov_name);
-    if (!TEST_ptr(libprov))
+    if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name))
         return 0;
 
 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH)
index d4d60694d428b38c740c2d7badc5b5d4cede5860..cc579ff3109878763be949f356fb62dddce55ef4 100644 (file)
@@ -3590,22 +3590,12 @@ int setup_tests(void)
     }
 
     /*
+     * Load the provider via configuration into the created library context.
      * Load the 'null' provider into the default library context to ensure that
      * the the tests do not fallback to using the default provider.
      */
-    prov_null = OSSL_PROVIDER_load(NULL, "null");
-    if (prov_null == NULL) {
-        opt_printf_stderr("Failed to load null provider into default libctx\n");
+    if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
         return 0;
-    }
-
-    /* load the provider via configuration into the created library context */
-    libctx = OSSL_LIB_CTX_new();
-    if (libctx == NULL
-        || !OSSL_LIB_CTX_load_config(libctx, config_file)) {
-        TEST_error("Failed to load config %s\n", config_file);
-        return 0;
-    }
 
     n = test_get_argument_count();
     if (n == 0)
index 54b1ca4236f84cc76e416bcebe95138ee666e210..f63aa58b120365fc468850d9e726a9e1732fad52 100644 (file)
@@ -57,6 +57,7 @@
 # include <openssl/ct.h>
 #endif
 #include <openssl/provider.h>
+#include "testutil.h"
 
 /*
  * Or gethostname won't be declared properly
@@ -103,7 +104,6 @@ static unsigned int psk_server_callback(SSL *ssl, const char *identity,
                                         unsigned int max_psk_len);
 #endif
 
-static BIO *bio_err = NULL;
 static BIO *bio_stdout = NULL;
 
 #ifndef OPENSSL_NO_NEXTPROTONEG
@@ -1344,22 +1344,9 @@ int main(int argc, char *argv[])
     }
 #endif
 
-    if (provider != NULL) {
-        defctxnull = OSSL_PROVIDER_load(NULL, "null");
-        if (defctxnull == NULL)
-            goto end;
-        libctx = OSSL_LIB_CTX_new();
-        if (libctx == NULL)
-            goto end;
-
-        if (config != NULL
-                && !OSSL_LIB_CTX_load_config(libctx, config))
-            goto end;
-
-        thisprov = OSSL_PROVIDER_load(libctx, provider);
-        if (thisprov == NULL)
-            goto end;
-    }
+    if (provider != NULL
+            && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
+        goto end;
 
     c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
     s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
index 09141b2fedd6942ce9275d3f9472afe478a96dcd..042a05e453b90cebf1b325107acd2669363a7c88 100644 (file)
@@ -530,7 +530,7 @@ int setup_tests(void)
         return 0;
     }
 
-    if (!test_get_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
+    if (!test_arg_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
         return 0;
 
     ADD_ALL_TESTS(test_handshake, (int)num_tests);
index f1e3aae6fbc981cd5d45ae8a5c7d6d7b12964239..91e4d4bdd99f60a89801625793b699efa22f75c5 100644 (file)
@@ -205,8 +205,10 @@ size_t test_get_argument_count(void);
  */
 int test_skip_common_options(void);
 
-int test_get_libctx(OSSL_LIB_CTX **libctx,
-                    OSSL_PROVIDER **default_null_provider,
+int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
+                    const char *config_file,
+                    OSSL_PROVIDER **provider, const char *module_name);
+int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
                     OSSL_PROVIDER **provider, int argn, const char *usage);
 
 /*
index c7ff44c754a4099110bbf2026c605c33390f7975..c50ef035fcfadbe38bc46c4f05368dba2b0379ee 100644 (file)
 #include <openssl/provider.h>
 #include <string.h>
 
-int test_get_libctx(OSSL_LIB_CTX **libctx,
-                    OSSL_PROVIDER **default_null_provider,
-                    OSSL_PROVIDER **provider, int argn, const char *usage)
+int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
+                    const char *config_file,
+                    OSSL_PROVIDER **provider, const char *module_name)
 {
-    const char *module_name;
+    if ((*libctx = OSSL_LIB_CTX_new()) == NULL) {
+        opt_printf_stderr("Failed to create libctx\n");
+        goto err;
+    }
 
-    if (!TEST_ptr(module_name = test_get_argument(argn))) {
-        TEST_error("usage: <prog> %s", usage);
-        return 0;
+    if (default_null_prov != NULL
+        && (*default_null_prov = OSSL_PROVIDER_load(NULL, "null")) == NULL) {
+        opt_printf_stderr("Failed to load null provider into default libctx\n");
+        goto err;
     }
-    if (strcmp(module_name, "none") != 0) {
-        const char *config_fname = test_get_argument(argn + 1);
-
-        *default_null_provider = OSSL_PROVIDER_load(NULL, "null");
-        *libctx = OSSL_LIB_CTX_new();
-        if (!TEST_ptr(*libctx)) {
-            TEST_error("Failed to create libctx\n");
-            goto err;
-        }
-
-        if (config_fname != NULL
-            && !TEST_true(OSSL_LIB_CTX_load_config(*libctx, config_fname))) {
-            TEST_error("Error loading config file %s\n", config_fname);
-            goto err;
-        }
-
-        *provider = OSSL_PROVIDER_load(*libctx, module_name);
-        if (!TEST_ptr(*provider)) {
-            TEST_error("Failed to load provider %s\n", module_name);
-            goto err;
-        }
+
+    if (config_file != NULL
+            && !OSSL_LIB_CTX_load_config(*libctx, config_file)) {
+        opt_printf_stderr("Error loading config from file %s\n", config_file);
+        goto err;
+    }
+
+    if (module_name != NULL
+            && (*provider = OSSL_PROVIDER_load(*libctx, module_name)) == NULL) {
+        opt_printf_stderr("Failed to load provider %s\n", module_name);
+        goto err;
     }
     return 1;
 
@@ -49,3 +43,18 @@ int test_get_libctx(OSSL_LIB_CTX **libctx,
     ERR_print_errors_fp(stderr);
     return 0;
 }
+
+int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
+                    OSSL_PROVIDER **provider, int argn, const char *usage)
+{
+    const char *module_name;
+
+    if (!TEST_ptr(module_name = test_get_argument(argn))) {
+        TEST_error("usage: <prog> %s", usage);
+        return 0;
+    }
+    if (strcmp(module_name, "none") == 0)
+        return 1;
+    return test_get_libctx(libctx, default_null_prov,
+                           test_get_argument(argn + 1), provider, module_name);
+}