OSSL_PROVIDER_load_ex tests
authorDmitry Belyavskiy <beldmit@gmail.com>
Thu, 3 Aug 2023 11:20:33 +0000 (13:20 +0200)
committerDmitry Belyavskiy <beldmit@gmail.com>
Wed, 30 Aug 2023 19:55:47 +0000 (21:55 +0200)
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21604)

test/provider_internal_test.c
test/provider_test.c

index bccce7159e1fdf97deee0c89978ffe12ef684415..6c333f85dbb23671e8eb3e2331600252b0276224 100644 (file)
@@ -64,7 +64,7 @@ static int test_builtin_provider(void)
 
     ret =
         TEST_ptr(prov =
-                 ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, 0))
+                 ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, NULL, 0))
         && test_provider(prov, expected_greeting1(name));
 
     EVP_set_default_properties(NULL, "");
@@ -79,7 +79,7 @@ static int test_loaded_provider(void)
     OSSL_PROVIDER *prov = NULL;
 
     return
-        TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, 0))
+        TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, NULL, 0))
         && test_provider(prov, expected_greeting1(name));
 }
 
index b2e0a5da716f91a2e35b839831d99d026098f5f2..d1fe71f46d957cfc4c7b63b4a7c94ff6093df333 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <stddef.h>
 #include <openssl/provider.h>
+#include <openssl/param_build.h>
 #include "testutil.h"
 
 extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
@@ -157,6 +158,60 @@ static int test_provider(OSSL_LIB_CTX **libctx, const char *name,
     return ok;
 }
 
+#ifndef NO_PROVIDER_MODULE
+static int test_provider_ex(OSSL_LIB_CTX **libctx, const char *name)
+{
+    OSSL_PROVIDER *prov = NULL;
+    const char *greeting = NULL;
+    int ok = 0;
+    long err;
+    const char custom_buf[] = "Custom greeting";
+    OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
+    OSSL_PARAM *params = NULL;
+
+    OSSL_PARAM_BLD_push_utf8_string(bld, "greeting", custom_buf, strlen(custom_buf));
+    params = OSSL_PARAM_BLD_to_param(bld);
+
+    OSSL_PARAM_BLD_free(bld);
+
+    if (!TEST_ptr(prov = OSSL_PROVIDER_load_ex(*libctx, name, params)))
+        goto err;
+
+    if (!TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request))
+            || !TEST_ptr(greeting = greeting_request[0].data)
+            || !TEST_size_t_gt(greeting_request[0].data_size, 0)
+            || !TEST_str_eq(greeting, custom_buf))
+        goto err;
+
+    /* Make sure we got the error we were expecting */
+    err = ERR_peek_last_error();
+    if (!TEST_int_gt(err, 0)
+            || !TEST_int_eq(ERR_GET_REASON(err), 1))
+        goto err;
+
+    if (!TEST_true(OSSL_PROVIDER_unload(prov)))
+        goto err;
+    prov = NULL;
+
+    /*
+     * We must free the libctx to force the provider to really be unloaded from
+     * memory
+     */
+    OSSL_LIB_CTX_free(*libctx);
+    *libctx = NULL;
+
+    /* We print out all the data to make sure it can still be accessed */
+    ERR_print_errors_fp(stderr);
+    ok = 1;
+ err:
+    OSSL_PARAM_free(params);
+    OSSL_PROVIDER_unload(prov);
+    OSSL_LIB_CTX_free(*libctx);
+    *libctx = NULL;
+    return ok;
+}
+#endif
+
 static int test_builtin_provider(void)
 {
     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
@@ -211,12 +266,22 @@ static int test_loaded_provider(void)
 {
     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
     const char *name = "p_test";
+    int res = 0;
 
     if (!TEST_ptr(libctx))
         return 0;
 
     /* test_provider will free libctx as part of the test */
-    return test_provider(&libctx, name, NULL);
+    res = test_provider(&libctx, name, NULL);
+
+    libctx = OSSL_LIB_CTX_new();
+    if (!TEST_ptr(libctx))
+        return 0;
+
+    /* test_provider_ex will free libctx as part of the test */
+    res = res && test_provider_ex(&libctx, name);
+
+    return res;
 }
 #endif