Use adapted test_get_libctx() for simpler test setup and better error reporting

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/13001)
diff --git a/test/acvp_test.c b/test/acvp_test.c
index 1686d75..5c4a387 100644
--- a/test/acvp_test.c
+++ b/test/acvp_test.c
@@ -1434,18 +1434,9 @@
         }
     }
 
-    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));
diff --git a/test/build.info b/test/build.info
index 0386a1f..81f9b9c 100644
--- a/test/build.info
+++ b/test/build.info
@@ -706,7 +706,7 @@
 
     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
diff --git a/test/cmp_client_test.c b/test/cmp_client_test.c
index 17f932a..efb1854 100644
--- a/test/cmp_client_test.c
+++ b/test/cmp_client_test.c
@@ -366,7 +366,7 @@
         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))
diff --git a/test/cmp_msg_test.c b/test/cmp_msg_test.c
index 41ba5cf..0b56d66 100644
--- a/test/cmp_msg_test.c
+++ b/test/cmp_msg_test.c
@@ -561,7 +561,7 @@
         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))
diff --git a/test/cmp_protect_test.c b/test/cmp_protect_test.c
index 3cca301..d4acb71 100644
--- a/test/cmp_protect_test.c
+++ b/test/cmp_protect_test.c
@@ -538,7 +538,7 @@
         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))
diff --git a/test/cmp_server_test.c b/test/cmp_server_test.c
index 49108bf..bff42c8 100644
--- a/test/cmp_server_test.c
+++ b/test/cmp_server_test.c
@@ -145,7 +145,7 @@
         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))) {
diff --git a/test/cmp_vfy_test.c b/test/cmp_vfy_test.c
index 67112f6..d45c938 100644
--- a/test/cmp_vfy_test.c
+++ b/test/cmp_vfy_test.c
@@ -600,7 +600,7 @@
         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 */
diff --git a/test/evp_extra_test2.c b/test/evp_extra_test2.c
index f91e66a..9181061 100644
--- a/test/evp_extra_test2.c
+++ b/test/evp_extra_test2.c
@@ -272,13 +272,7 @@
 
 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;
diff --git a/test/evp_libctx_test.c b/test/evp_libctx_test.c
index cf39bf0..c306bd9 100644
--- a/test/evp_libctx_test.c
+++ b/test/evp_libctx_test.c
@@ -648,19 +648,7 @@
         }
     }
 
-    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)
diff --git a/test/evp_test.c b/test/evp_test.c
index d4d6069..cc579ff 100644
--- a/test/evp_test.c
+++ b/test/evp_test.c
@@ -3590,22 +3590,12 @@
     }
 
     /*
+     * 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)
diff --git a/test/ssl_old_test.c b/test/ssl_old_test.c
index 54b1ca4..f63aa58 100644
--- a/test/ssl_old_test.c
+++ b/test/ssl_old_test.c
@@ -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 @@
                                         unsigned int max_psk_len);
 #endif
 
-static BIO *bio_err = NULL;
 static BIO *bio_stdout = NULL;
 
 #ifndef OPENSSL_NO_NEXTPROTONEG
@@ -1344,22 +1344,9 @@
     }
 #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);
diff --git a/test/ssl_test.c b/test/ssl_test.c
index 09141b2..042a05e 100644
--- a/test/ssl_test.c
+++ b/test/ssl_test.c
@@ -530,7 +530,7 @@
         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);
diff --git a/test/testutil.h b/test/testutil.h
index f1e3aae..91e4d4b 100644
--- a/test/testutil.h
+++ b/test/testutil.h
@@ -205,8 +205,10 @@
  */
 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);
 
 /*
diff --git a/test/testutil/provider.c b/test/testutil/provider.c
index c7ff44c..c50ef03 100644
--- a/test/testutil/provider.c
+++ b/test/testutil/provider.c
@@ -11,8 +11,40 @@
 #include <openssl/provider.h>
 #include <string.h>
 
-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)
+{
+    if ((*libctx = OSSL_LIB_CTX_new()) == NULL) {
+        opt_printf_stderr("Failed to create libctx\n");
+        goto err;
+    }
+
+    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 (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;
+
+ err:
+    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;
@@ -21,31 +53,8 @@
         TEST_error("usage: <prog> %s", usage);
         return 0;
     }
-    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;
-        }
-    }
-    return 1;
-
- err:
-    ERR_print_errors_fp(stderr);
-    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);
 }