From 363a869b000ee1c9337bc0086108f6a5960da326 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sun, 15 Nov 2009 20:26:16 +0000 Subject: Allow use of p11-tests as a library. --- src/p11-tests.c | 123 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 91 insertions(+), 32 deletions(-) (limited to 'src/p11-tests.c') diff --git a/src/p11-tests.c b/src/p11-tests.c index 900338e..4d8b9ff 100644 --- a/src/p11-tests.c +++ b/src/p11-tests.c @@ -1,13 +1,27 @@ - #include "p11-tests.h" +#include "p11-tests-lib.h" #include "compat.h" #include #include -#include -#include +#ifdef _WIN32 + +#define WIN32_LEAN_AND_MEAN +#define _WIN32_WINNT 0x400 +#include + +static HMODULE module = NULL; + +#else /* !_WIN32 */ + +#include +#include + +static void *module = NULL; + +#endif /* !_WIN32 */ int p11t_test_unexpected = 1; int p11t_test_write_session = 0; @@ -19,10 +33,74 @@ usage() exit(2); } +void +fatal (const char *message, ...) +{ + va_list va; + va_start (va, message); + fprintf (stderr, message, va); + va_end (va); + exit (1); +} + +CK_FUNCTION_LIST_PTR +module_load(const char *filename) +{ + CK_FUNCTION_LIST_PTR list = NULL; + CK_C_GetFunctionList get_function_list; + CK_RV rv; + +#ifdef _WIN32 + + module = LoadLibrary (filename); + if (!module) + fatal ("couldn't load library: %s: %s", filename, p11t_msg_os ()); + + /* Lookup the appropriate function in the library */ + get_function_list = (CK_C_GetFunctionList)GetProcAddress (module, "C_GetFunctionList"); + if (!get_function_list) + fatal ("couldn't find function 'C_GetFunctionList' in library: %s: %s", + filename, p11t_msg_os ()); + +#else /* !_WIN32 */ + + module = dlopen (filename, RTLD_NOW); + if (!module) + fatal ("couldn't open library: %s: %s", filename, dlerror ()); + + /* Lookup the appropriate function in library */ + get_function_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList"); + if (!get_function_list) + fatal ("couldn't find function 'C_GetFunctionList' in library: %s: %s", + filename, dlerror ()); + +#endif /* !_WIN32 */ + + /* Get the function list */ + rv = (get_function_list) (&list); + if(rv != CKR_OK || !list) + fatal ("couldn't get function list: %d", (int)rv); + + return list; +} + +void +module_unload (void) +{ + if (module) { +#ifdef _WIN32 + FreeLibrary (module); +#else + dlclose (module); +#endif + } + module = NULL; +} + int main(int argc, char* argv[]) { - const char *config = NULL; + CK_FUNCTION_LIST_PTR module; int ch; while((ch = getopt(argc, argv, "f:svz")) != -1) @@ -30,16 +108,17 @@ main(int argc, char* argv[]) switch(ch) { case 'f': - config = optarg; + if (p11_tests_load_config (optarg) < 0) + exit (2); break; case 's': - p11t_test_write_session = 1; + p11_tests_set_write_session (1); break; case 'v': - p11t_check_verbose = 1; + p11_tests_set_verbose (1); break; case 'z': - p11t_test_unexpected = 0; + p11_tests_set_unexpected (0); break; case '?': default: @@ -54,30 +133,10 @@ main(int argc, char* argv[]) if(argc != 1) usage(); - if(config) - p11t_config_parse(config); - - ERR_load_crypto_strings(); - OpenSSL_add_all_digests(); - - /* Basic module tests */ - p11t_module_load(argv[0]); - p11t_module_initialize(); - - p11t_slot_tests(); - p11t_session_tests(); - p11t_object_tests(); - p11t_key_tests(); - p11t_certificate_tests(); - p11t_rsa_tests(); - p11t_dsa_tests(); - - /* Remaining module tests */ - p11t_module_finalize(); - p11t_module_unload(); - - p11t_slot_cleanup(); - p11t_config_cleanup(); + module = module_load (argv[0]); + p11_tests_perform (module); + module_unload (); + /* TODO: Change return value based on tests passed, failed */ return 0; } -- cgit v1.2.3