testutil: Add provider.c with test_get_libctx(), to use at least for SSL and CMP
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Wed, 12 Aug 2020 05:46:57 +0000 (07:46 +0200)
committerDr. David von Oheimb <David.von.Oheimb@siemens.com>
Fri, 21 Aug 2020 07:04:09 +0000 (09:04 +0200)
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11808)

test/build.info
test/ssl_test.c
test/testutil.h
test/testutil/provider.c [new file with mode: 0644]

index 11fce8e2798caeb46ef449e83594d1302cef85ba..3ad16b51b10c547e700258e08e3d9ca424325777 100644 (file)
@@ -22,7 +22,7 @@ IF[{- !$disabled{tests} -}]
           testutil/driver.c testutil/tests.c testutil/cb.c testutil/stanza.c \
           testutil/format_output.c \
           testutil/test_cleanup.c testutil/main.c testutil/testutil_init.c \
-          testutil/options.c testutil/test_options.c \
+          testutil/options.c testutil/test_options.c testutil/provider.c \
           testutil/apps_mem.c testutil/random.c $LIBAPPSSRC
   INCLUDE[libtestutil.a]=../include ../apps/include ..
   DEPEND[libtestutil.a]=../libcrypto
index 5880468f93a9cbc62cb5ed9e0a9a169690fea586..18e92c7f77b88f1747cafb5d0a84694bc30f9c82 100644 (file)
@@ -513,12 +513,12 @@ err:
     return ret;
 }
 
-OPT_TEST_DECLARE_USAGE("conf_file modulename [fips_conf_file]\n")
+#define USAGE "conf_file module_name [module_conf_file]\n"
+OPT_TEST_DECLARE_USAGE(USAGE)
 
 int setup_tests(void)
 {
     long num_tests;
-    const char *modulename;
 
     if (!test_skip_common_options()) {
         TEST_error("Error parsing test options\n");
@@ -529,29 +529,14 @@ int setup_tests(void)
             /* argv[1] should point to the test conf file */
             || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
             || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
-                                               &num_tests), 0))
+                                               &num_tests), 0)) {
+        TEST_error("usage: ssl_test %s", USAGE);
         return 0;
+    }
 
-    if (!TEST_ptr(modulename = test_get_argument(1)))
+    if (!test_get_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
         return 0;
 
-    if (strcmp(modulename, "none") != 0) {
-        const char *configfile = test_get_argument(2);
-
-        defctxnull = OSSL_PROVIDER_load(NULL, "null");
-        libctx = OPENSSL_CTX_new();
-        if (!TEST_ptr(libctx))
-            return 0;
-
-        if (configfile != NULL
-                && !TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
-            return 0;
-
-        thisprov = OSSL_PROVIDER_load(libctx, modulename);
-        if (!TEST_ptr(thisprov))
-            return 0;
-    }
-
     ADD_ALL_TESTS(test_handshake, (int)num_tests);
     return 1;
 }
index c5c284a0cc2ba54fca3a78682b34e5a6f8621f18..14483fd40512d26aef2c3b21232a22e41145bbfd 100644 (file)
 #ifndef OSSL_TESTUTIL_H
 # define OSSL_TESTUTIL_H
 
-#include <stdarg.h>
+# include <stdarg.h>
 
-#include <openssl/err.h>
-#include <openssl/e_os2.h>
-#include <openssl/bn.h>
-#include "opt.h"
+# include <openssl/provider.h>
+# include <openssl/err.h>
+# include <openssl/e_os2.h>
+# include <openssl/bn.h>
+# include "opt.h"
 
 /*-
  * Simple unit tests should implement setup_tests().
 
 
 /* The default test enum which should be common to all tests */
-#define OPT_TEST_ENUM \
+# define OPT_TEST_ENUM \
     OPT_TEST_HELP = 500, \
     OPT_TEST_LIST, \
     OPT_TEST_SINGLE, \
     OPT_TEST_SEED
 
 /* The Default test OPTIONS common to all tests (without a usage string) */
-#define OPT_TEST_OPTIONS \
+# define OPT_TEST_OPTIONS \
     { OPT_HELP_STR, 1,  '-', "Valid options are:\n" }, \
     { "help", OPT_TEST_HELP, '-', "Display this summary" }, \
     { "list", OPT_TEST_LIST, '-', "Display the list of tests available" }, \
     { "seed", OPT_TEST_SEED, 'n', "Seed value to randomize tests with" }
 
 /* The Default test OPTIONS common to all tests starting with an additional usage string */
-#define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
+# define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
     { OPT_HELP_STR, 1, '-', "Usage: %s [options] " usage }, \
     OPT_TEST_OPTIONS
 
 /* The Default test OPTIONS common to all tests with an default usage string */
-#define OPT_TEST_OPTIONS_DEFAULT_USAGE \
+# define OPT_TEST_OPTIONS_DEFAULT_USAGE \
     { OPT_HELP_STR, 1, '-', "Usage: %s [options]\n" }, \
     OPT_TEST_OPTIONS
 
  * Optional Cases that need to be ignored by the test app when using opt_next(),
  * (that are handled internally).
  */
-#define OPT_TEST_CASES \
+# define OPT_TEST_CASES \
          OPT_TEST_HELP: \
     case OPT_TEST_LIST: \
     case OPT_TEST_SINGLE: \
  *      well as the additional options that need to be handled.
  *  (3) case OPT_TEST_CASES: break; inside the opt_next() handling code.
  */
-#define OPT_TEST_DECLARE_USAGE(usage_str) \
+# define OPT_TEST_DECLARE_USAGE(usage_str) \
 const OPTIONS *test_get_options(void) \
 { \
     enum { OPT_TEST_ENUM }; \
@@ -204,6 +205,10 @@ size_t test_get_argument_count(void);
  */
 int test_skip_common_options(void);
 
+int test_get_libctx(OPENSSL_CTX **libctx,
+                    OSSL_PROVIDER **default_null_provider,
+                    OSSL_PROVIDER **provider, int argn, const char *usage);
+
 /*
  * Internal helpers. Test programs shouldn't use these directly, but should
  * rather link to one of the helper main() methods.
@@ -235,17 +240,17 @@ const OPTIONS *test_get_options(void);
  *  Test assumption verification helpers.
  */
 
-#define PRINTF_FORMAT(a, b)
-#if defined(__GNUC__) && defined(__STDC_VERSION__)
+# define PRINTF_FORMAT(a, b)
+# if defined(__GNUC__) && defined(__STDC_VERSION__)
   /*
    * Because we support the 'z' modifier, which made its appearance in C99,
    * we can't use __attribute__ with pre C99 dialects.
    */
-# if __STDC_VERSION__ >= 199901L
-#  undef PRINTF_FORMAT
-#  define PRINTF_FORMAT(a, b)   __attribute__ ((format(printf, a, b)))
+#  if __STDC_VERSION__ >= 199901L
+#   undef PRINTF_FORMAT
+#   define PRINTF_FORMAT(a, b)   __attribute__ ((format(printf, a, b)))
+#  endif
 # endif
-#endif
 
 # define DECLARE_COMPARISON(type, name, opname)                         \
     int test_ ## name ## _ ## opname(const char *, int,                 \
@@ -503,7 +508,7 @@ void test_output_memory(const char *name, const unsigned char *m, size_t l);
 /*
  * Utilities to parse a test file.
  */
-#define TESTMAXPAIRS        150
+# define TESTMAXPAIRS        150
 
 typedef struct pair_st {
     char *key;
diff --git a/test/testutil/provider.c b/test/testutil/provider.c
new file mode 100644 (file)
index 0000000..f7c7b1c
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (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 "../testutil.h"
+#include <openssl/provider.h>
+#include <string.h>
+
+int test_get_libctx(OPENSSL_CTX **libctx,
+                    OSSL_PROVIDER **default_null_provider,
+                    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) {
+        const char *config_fname = test_get_argument(argn + 1);
+
+        *default_null_provider = OSSL_PROVIDER_load(NULL, "null");
+        *libctx = OPENSSL_CTX_new();
+        if (!TEST_ptr(*libctx)) {
+            TEST_error("Failed to create libctx\n");
+            goto err;
+        }
+
+        if (config_fname != NULL
+                && !TEST_true(OPENSSL_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;
+}