From b75662dde8a6d3e808c9c8d440a67df4e0899495 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 6 Dec 2008 15:12:34 +0000 Subject: Change how the tests are logged and failed. --- src/module.c | 277 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 149 insertions(+), 128 deletions(-) (limited to 'src/module.c') diff --git a/src/module.c b/src/module.c index 453acd4..7911bf2 100644 --- a/src/module.c +++ b/src/module.c @@ -26,7 +26,7 @@ CK_FUNCTION_LIST_PTR p11t_module_funcs = NULL; static const char *init_string = NULL; static CK_C_INITIALIZE_ARGS init_args; -static void (*init_func)(void) = NULL; +static int (*init_func)(void) = NULL; static int is_initialized = 0; void @@ -39,20 +39,23 @@ p11t_module_config(const char *name, const char *value) static CK_RV create_mutex(void **mutex) { + const char *old = P11T_SECTION("CreateMutex"); + CK_RV ret = CKR_OK; + if(!mutex) { - p11t_msg_print("CreateMutex: null mutex passed"); - return CKR_ARGUMENTS_BAD; + P11T_CHECK_FAIL("Arguments should not be null"); + ret = CKR_ARGUMENTS_BAD; } - + else + { #ifdef _WIN32 - *mutex = CreateMutex(NULL, FALSE, NULL); - assert(*mutex != NULL); + *mutex = CreateMutex(NULL, FALSE, NULL); + assert(*mutex != NULL); #else /* !_WIN32 */ - { pthread_mutex_t *mut; mut = malloc(sizeof(pthread_mutex_t)); assert(mut); @@ -60,190 +63,200 @@ create_mutex(void **mutex) if(pthread_mutex_init(mut, NULL) != 0) assert(0); *mutex = mut; - } #endif /* !_WIN32 */ + } - return CKR_OK; + p11t_msg_prefix(old); + return ret; } static CK_RV destroy_mutex (void *mutex) { + const char *old = P11T_SECTION("DestroyMutex"); + CK_RV ret = CKR_OK; + if(!mutex) { - p11t_msg_print("DestroyMutex: null mutex"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("Mutex should not be null"); + ret = CKR_MUTEX_BAD; } - + else + { #ifdef _WIN32 - if(!CloseHandle((HANDLE)mutex)) - { - DWORD error = GetLastError(); - if(error == ERROR_INVALID_HANDLE) + if(!CloseHandle((HANDLE)mutex)) { - p11t_msg_print("DestroyMutex: mutex is invalid"); - return CKR_MUTEX_BAD; - } - else - { - p11t_msg_print("DestroyMutex: failed: %d", GetLastError()); - return CKR_GENERAL_ERROR; + DWORD error = GetLastError(); + if(error == ERROR_INVALID_HANDLE) + { + P11T_CHECK_FAIL("Mutex should not be invalid"); + ret = CKR_MUTEX_BAD; + } + else + { + p11t_check_warn("failed: %d", GetLastError()); + ret = CKR_GENERAL_ERROR; + } } - } - #else /* !_WIN32 */ - { int res = pthread_mutex_destroy(mutex); if(res != 0) { if(res == EBUSY) { - p11t_msg_print("DestroyMutex: mutex is locked"); - return CKR_GENERAL_ERROR; + P11T_CHECK_FAIL("Mutex should not be locked"); + ret = CKR_GENERAL_ERROR; } else if(res == EINVAL) { - p11t_msg_print("DestroyMutex: mutex is invalid"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("Mutex should not be invalid"); + ret = CKR_MUTEX_BAD; } else { - p11t_msg_print("DestroyMutex: failed: %d", res); - return CKR_GENERAL_ERROR; + p11t_check_warn("failed: %d", res); + ret = CKR_GENERAL_ERROR; } } - } #endif /* !_WIN32 */ + } - return CKR_OK; + p11t_msg_prefix(old); + return ret; } static CK_RV lock_mutex (void *mutex) { + const char *old = P11T_SECTION("LockMutex"); + CK_RV ret = CKR_OK; + if(!mutex) { - p11t_msg_print("LockMutex: null mutex"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("null mutex"); + ret = CKR_MUTEX_BAD; } - + else + { #ifdef _WIN32 - { DWORD result = WaitForSingleObject((HANDLE)mutex, INFINITE); if(result == WAIT_ABANDONED) { - p11t_msg_print("LockMutex: thread exited without releasing mutex"); - return CKR_CANT_LOCK; + P11T_CHECK_FAIL("Thread should not exit without releasing mutex"); + ret = CKR_CANT_LOCK; } else if(result == WAIT_FAILED) { DWORD error = GetLastError(); if(error == ERROR_INVALID_HANDLE) { - p11t_msg_print("LockMutex: invalid handle"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("Mutex should not be invalid"); + ret = CKR_MUTEX_BAD; } else { - p11t_msg_print("LockMutex: failed: %d", error); - return CKR_GENERAL_ERROR; + p11t_check_warn("failed: %d", error); + ret = CKR_GENERAL_ERROR; } } else if(result != WAIT_OBJECT_0) { - p11t_msg_print("LockMutex: couldn't lock"); - return CKR_CANT_LOCK; + p11t_check_warn("couldn't lock"); + ret = CKR_CANT_LOCK; } - } #else /* !_WIN32 */ - { int res = pthread_mutex_lock(mutex); if(res != 0) { if(res == EDEADLK) { - p11t_msg_print("LockMutex: would deadlock"); - return CKR_CANT_LOCK; + P11T_CHECK_FAIL("Mutex should not deadlock"); + ret = CKR_CANT_LOCK; } else if(res == EINVAL) { - p11t_msg_print("LockMutex: invalid mutex used"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("Mutex should not be invalid"); + ret = CKR_MUTEX_BAD; } else { - p11t_msg_print("LockMutex: failed: %d", res); - return CKR_GENERAL_ERROR; + p11t_check_warn("failed: %d", res); + ret = CKR_GENERAL_ERROR; } } - } #endif /* !_WIN32 */ + } - return CKR_OK; + p11t_msg_prefix(old); + return ret; } static CK_RV unlock_mutex (void *mutex) { + const char *old = P11T_SECTION("UnlockMutex"); + CK_RV ret = CKR_OK; + if(!mutex) { - p11t_msg_print("UnlockMutex: null mutex"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("Mutex should not be null"); + ret = CKR_MUTEX_BAD; } + else + { #ifdef _WIN32 - if(!ReleaseMutex((HANDLE)mutex)) - { - DWORD error = GetLastError(); - if(error == ERROR_NOT_OWNER) + if(!ReleaseMutex((HANDLE)mutex)) { - p11t_msg_print("UnlockMutex: mutex not locked"); - return CKR_MUTEX_NOT_LOCKED; - } - else if(error == ERROR_INVALID_HANDLE) - { - p11t_msg_print("UnlockMutex: invalid mutex used"); - return CKR_MUTEX_BAD; - } - else - { - p11t_msg_print("UnlockMutex: failed: %d", error); - return CKR_GENERAL_ERROR; + DWORD error = GetLastError(); + if(error == ERROR_NOT_OWNER) + { + P11T_CHECK_FAIL("Mutex should be locked"); + ret = CKR_MUTEX_NOT_LOCKED; + } + else if(error == ERROR_INVALID_HANDLE) + { + P11T_CHECK_FAIL("Mutex should not be invalid"); + ret = CKR_MUTEX_BAD; + } + else + { + p11t_check_warn("failed: %d", error); + ret = CKR_GENERAL_ERROR; + } } - } #else /* !_WIN32 */ - { int res = pthread_mutex_lock(mutex); if(res != 0) { if(res == EPERM) { - p11t_msg_print("UnlockMutex: mutex not locked"); - return CKR_MUTEX_NOT_LOCKED; + P11T_CHECK_FAIL("Mutex should not be locked"); + ret = CKR_MUTEX_NOT_LOCKED; } else if(res == EINVAL) { - p11t_msg_print("UnlockMutex: invalid mutex used"); - return CKR_MUTEX_BAD; + P11T_CHECK_FAIL("Mutex should be valid"); + ret = CKR_MUTEX_BAD; } else { - p11t_msg_print("UnlockMutex: failed: %d", res); - return CKR_GENERAL_ERROR; + p11t_check_warn("failed: %d", res); + ret = CKR_GENERAL_ERROR; } } @@ -251,14 +264,15 @@ unlock_mutex (void *mutex) { free(mutex); } - } #endif /* !_WIN32 */ + } - return CKR_OK; + p11t_msg_prefix(old); + return ret; } -void +int p11t_module_load(const char *filename) { CK_FUNCTION_LIST_PTR list; @@ -298,15 +312,16 @@ p11t_module_load(const char *filename) if(p11t_test_unexpected) { - /** C_GetFunctionList */ + P11T_SECTION("C_GetFunctionList"); + rv = (p11t_module_funcs->C_GetFunctionList)(&list); - if(rv != CKR_OK) - p11t_msg_print("C_GetFunctionList: call through function list failed: %s", p11t_msg_rv(rv)); + P11T_CHECK_RV("Call through function list", rv, CKR_OK); - /** - See if returns same data as library entry point */ if(!memcmp(list, p11t_module_funcs, sizeof(*list)) != 0) - p11t_msg_print("C_GetFunctionList: function lists returned directly and recursively differ"); + P11T_CHECK_FAIL("See if returns same data as library entry point"); } + + return CONTINUE; } static int @@ -314,50 +329,54 @@ initialize_common(const char *mode) { CK_RV rv; - /** C_Initialize **/ + P11T_SECTION("C_Initialize"); assert(p11t_module_funcs); - /** - Normal call */ rv = (p11t_module_funcs->C_Initialize) (&init_args); - if (rv != CKR_OK) + if(rv == CKR_CANT_LOCK) { - if(rv != CKR_CANT_LOCK) - p11t_msg_print("C_Initialize: failed (%s): %s", mode, p11t_msg_rv(rv)); + p11t_check_info("Module didn't accept %s", mode); return 0; } - else + + if(rv != CKR_OK) { - is_initialized = 1; - return 1; + p11t_check_fail("Couldn't initialize with %s: %s", mode, p11t_msg_rv(rv)); + return 0; } + + is_initialized = 1; + return 1; } -static void +static int initialize_locking_1(void) { - /** - Locking: no threads */ + P11T_CHECK_NOTE("Locking: no threads"); memset(&init_args, 0, sizeof (init_args)); init_args.pReserved = (void*)init_string; if(initialize_common("locking mode 1: no threads")) init_func = initialize_locking_1; + return CONTINUE; } -static void +static int initialize_locking_2(void) { - /** - Locking: os locking */ + P11T_CHECK_NOTE("Locking: os locking"); memset(&init_args, 0, sizeof (init_args)); init_args.flags = CKF_OS_LOCKING_OK; init_args.pReserved = (void*)init_string; if(initialize_common("locking mode 2: os locking")) init_func = initialize_locking_2; + return CONTINUE; } -static void +static int initialize_locking_3(void) { - /** - Locking: app locking */ + P11T_CHECK_NOTE("Locking: app locking"); memset(&init_args, 0, sizeof (init_args)); init_args.flags = 0; init_args.CreateMutex = create_mutex; @@ -367,12 +386,13 @@ initialize_locking_3(void) init_args.pReserved = (void*)init_string; if(initialize_common("locking mode 3: app locking")) init_func = initialize_locking_3; + return CONTINUE; } -static void +static int initialize_locking_4(void) { - /** - Locking: either locking */ + P11T_CHECK_NOTE("Locking: either locking"); memset(&init_args, 0, sizeof (init_args)); init_args.flags = CKF_OS_LOCKING_OK; init_args.CreateMutex = create_mutex; @@ -382,9 +402,10 @@ initialize_locking_4(void) init_args.pReserved = (void*)init_string; if(initialize_common("locking mode 4: either locking")) init_func = initialize_locking_4; + return CONTINUE; } -void +int p11t_module_initialize(void) { CK_INFO info; @@ -392,20 +413,22 @@ p11t_module_initialize(void) assert(p11t_module_funcs); + P11T_SECTION("C_Initialize"); + if(p11t_test_unexpected) { - /** - Calls without initializing */ rv = (p11t_module_funcs->C_GetInfo)(&info); - p11t_check_returns("C_GetInfo: shouldn't run without initialize", rv, CKR_CRYPTOKI_NOT_INITIALIZED); + P11T_CHECK_RV("Calls without initializing", rv, CKR_CRYPTOKI_NOT_INITIALIZED); - /** - NULL argument */ rv = (p11t_module_funcs->C_Initialize) (NULL); - p11t_check_returns("C_Initialize: should succeed with null argument", rv, CKR_OK); + P11T_CHECK_RV("Null argument", rv, CKR_OK); + is_initialized = 1; p11t_module_finalize(); } - /** - Multiple initialize with C_Finalize between */ + P11T_CHECK_NOTE("Multiple initialize with C_Finalize between"); + initialize_locking_1(); p11t_module_finalize(); initialize_locking_2(); @@ -417,55 +440,52 @@ p11t_module_initialize(void) /* Actually initialize with whatever succeeded last */ if(!init_func) - p11t_msg_fatal("C_Initialize: no initialize succeded cannot continue"); + p11t_msg_fatal("Couldn't initialize module via any method"); (init_func)(); if(!is_initialized) - p11t_msg_fatal("C_Initialize: couldn't initialize pkcs11 module"); + p11t_msg_fatal("Failed to initialize module"); if(p11t_test_unexpected) { - /** - Double initialize in a row */ rv = (p11t_module_funcs->C_Initialize) (&init_args); - if(rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) - { - p11t_msg_print("C_Initialize: double initialize should return CKR_CRYPTOKI_ALREADY_INITIALIZED: %s", p11t_msg_rv(rv)); - } + P11T_CHECK_RV("Double initialize in a row", rv, CKR_CRYPTOKI_ALREADY_INITIALIZED); } is_initialized = 1; + return CONTINUE; } -void +int p11t_module_finalize(void) { CK_RV rv; - /** C_Finalize */ + P11T_SECTION("C_Finalize"); + if(is_initialized) { if(p11t_test_unexpected) { - /** - With invalid argument */ rv = p11t_module_funcs->C_Finalize(&rv); - p11t_check_returns("C_Finalize: bad argument", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("With invalid argument", rv, CKR_ARGUMENTS_BAD); } - /** - Normal call */ assert(p11t_module_funcs); rv = p11t_module_funcs->C_Finalize(NULL); - p11t_check_returns("C_Finalize", rv, CKR_OK); + P11T_CHECK_RV("Normal call", rv, CKR_OK); is_initialized = 0; } if(p11t_test_unexpected) { - /** - Double finalize in a row */ rv = p11t_module_funcs->C_Finalize(NULL); - p11t_check_returns("C_Finalize: double finalize", rv, CKR_CRYPTOKI_NOT_INITIALIZED); + P11T_CHECK_RV("Double finalize in a row", rv, CKR_CRYPTOKI_NOT_INITIALIZED); } + + return CONTINUE; } -void +int p11t_module_unload(void) { if(module) @@ -477,4 +497,5 @@ p11t_module_unload(void) #endif } module = NULL; + return CONTINUE; } -- cgit v1.2.3