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/slot.c | 193 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 89 insertions(+), 104 deletions(-) (limited to 'src/slot.c') diff --git a/src/slot.c b/src/slot.c index e3ff99f..13851c3 100644 --- a/src/slot.c +++ b/src/slot.c @@ -20,42 +20,38 @@ static CK_MECHANISM_INFO_PTR *slot_mech_info; * TESTS */ -void +static int test_slot_global(void) { CK_RV rv; - /** C_GetInfo */ + P11T_SECTION("C_GetInfo"); assert(p11t_module_funcs); if(p11t_test_unexpected) { - /** - NULL argument */ rv = (p11t_module_funcs->C_GetInfo)(NULL); - p11t_check_returns("C_GetInfo: null argument", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("Null argument", rv, CKR_ARGUMENTS_BAD); } /* Obvious crap fill */ memset(&slot_global, 0xFF, sizeof(CK_INFO)); - /** - Normal call */ rv = (p11t_module_funcs->C_GetInfo)(&slot_global); - if(!p11t_check_returns("C_GetInfo", rv, CKR_OK)) - { - memset(&slot_global, 0, sizeof(CK_INFO)); - return; - } + P11T_CHECK_RV("Normal call", rv, CKR_OK); - /** - Space padded strings in CK_INFO */ - p11t_check_padded("CK_INFO.manufacturerID", slot_global.manufacturerID); - p11t_check_padded("CK_INFO.libraryDescription", slot_global.libraryDescription); + P11T_SECTION("CK_INFO"); - /** - No flags set */ - p11t_check_ulong("CK_INFO.flags", slot_global.flags, 0); + P11T_CHECK_PADDED("manufacturerID", slot_global.manufacturerID); + P11T_CHECK_PADDED("libraryDescription", slot_global.libraryDescription); + + P11T_CHECK_ULONG("flags", slot_global.flags, 0); + + return CONTINUE; } -int +static int compar_slot_id(const void *one, const void *two) { CK_SLOT_ID a = *((CK_SLOT_ID*)one); @@ -67,7 +63,7 @@ compar_slot_id(const void *one, const void *two) return -1; } -void +static int test_slot_info(void) { CK_SLOT_ID_PTR present, only; @@ -75,25 +71,23 @@ test_slot_info(void) CK_ULONG i; CK_RV rv; - /** C_GetSlotList */ + P11T_SECTION("C_GetSlotList"); assert(p11t_module_funcs); if(p11t_test_unexpected) { - /** - NULL arguments */ rv = (p11t_module_funcs->C_GetSlotList)(FALSE, NULL, NULL); - p11t_check_returns("C_GetSlotList: null argument", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("Null arguments", rv, CKR_ARGUMENTS_BAD); } - /** - Retrieving the count */ rv = (p11t_module_funcs->C_GetSlotList)(FALSE, NULL, &p11t_slot_count); - p11t_check_returns("C_GetSlotList", rv, CKR_OK); + P11T_CHECK_RV("Retrieving the count", rv, CKR_OK); if(p11t_slot_count == 0) { - p11t_msg_print("C_GetSlotList: no slots to test"); - return; + p11t_check_info("no slots to test"); + return CONTINUE; } /* Allocate a bit extra. We're going to try and trip up module */ @@ -108,35 +102,29 @@ test_slot_info(void) if(p11t_test_unexpected) { - /** - Passing buffer space along with zero count. */ count = 0; rv = (p11t_module_funcs->C_GetSlotList)(FALSE, slot_ids, &count); - p11t_check_returns("C_GetSlotList: zero buffer", rv, CKR_BUFFER_TOO_SMALL); - p11t_check_ulong("C_GetSlotList: count invalid zero space passed", count, p11t_slot_count); + P11T_CHECK_RV("Passing buffer with zero count", rv, CKR_BUFFER_TOO_SMALL); + P11T_CHECK_ULONG("Resulting count when buffer with zero count passed", count, p11t_slot_count); if(p11t_slot_count > 1) { - /** - Passing buffer space along with low count. */ count = 1; rv = (p11t_module_funcs->C_GetSlotList)(FALSE, slot_ids, &count); - p11t_check_returns("C_GetSlotList: low buffer", rv, CKR_BUFFER_TOO_SMALL); - p11t_check_ulong("C_GetSlotList: count invalid when too little space passed", count, p11t_slot_count); + P11T_CHECK_RV("Passing buffer along with low count", rv, CKR_BUFFER_TOO_SMALL); + P11T_CHECK_ULONG("Resulting count when buffer with low count passed", count, p11t_slot_count); } } - /** - Passing too much buffer space. */ if(p11t_test_unexpected) count = p11t_slot_count + 5; else count = p11t_slot_count; rv = (p11t_module_funcs->C_GetSlotList)(FALSE, slot_ids, &count); - if(!p11t_check_returns("C_GetSlotList", rv, CKR_OK)) - { - p11t_slot_count = 0; - return; - } + P11T_CHECK_RV("Normal call", rv, CKR_OK); - p11t_check_ulong("C_GetSlotList: count invalid when too much space passed", count, p11t_slot_count); + if(p11t_test_unexpected) + P11T_CHECK_ULONG("Count invalid when too much buffer passed", count, p11t_slot_count); /* When we ask for just token valid ones, they should only be the ones we noted with flags */ present = calloc(p11t_slot_count, sizeof(CK_SLOT_ID)); @@ -147,54 +135,50 @@ test_slot_info(void) /* Now go through and load info about each slot */ for(i = 0; i < p11t_slot_count; ++i) { - /** C_GetSlotInfo */ + P11T_SECTION("C_GetSlotInfo"); if(p11t_test_unexpected) { - /** - NULL argument */ rv = (p11t_module_funcs->C_GetSlotInfo)(slot_ids[i], NULL); - p11t_check_returns("C_GetSlotInfo: null argument", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("Null argument", rv, CKR_ARGUMENTS_BAD); } - /** - Normal call */ rv = (p11t_module_funcs->C_GetSlotInfo)(slot_ids[i], &slot_info[i]); - p11t_check_returns("C_GetSlotInfo", rv, CKR_OK); + P11T_CHECK_RV("Normal call", rv, CKR_OK); + + P11T_SECTION("CK_SLOT_INFO"); - /** - Space padded CK_SLOT_INFO fields */ - p11t_check_padded("CK_SLOT_INFO.slotDescription", slot_info[i].slotDescription); - p11t_check_padded("CK_SLOT_INFO.manufacturerID", slot_info[i].manufacturerID); + P11T_CHECK_PADDED("slotDescription", slot_info[i].slotDescription); + P11T_CHECK_PADDED("manufacturerID", slot_info[i].manufacturerID); - /** - CK_SLOT_INFO flags are from valid set */ - p11t_check_mask("CK_SLOT_INFO.flags", slot_info[i].flags, + P11T_CHECK_MASK("flags", slot_info[i].flags, CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE | CKF_HW_SLOT); - /** - Track CKF_TOKEN_PRESENT flag and compare to C_GetSlotList(TRUE) */ + P11T_CHECK_NOTE("CKF_TOKEN_PRESENT flag is equivalent to C_GetSlotList(TRUE, ...)"); if((slot_info[i].flags & CKF_TOKEN_PRESENT) == CKF_TOKEN_PRESENT) { /* Note if token is present for later */ present[n_present++] = slot_ids[i]; - /** C_GetTokenInfo */ + P11T_SECTION("C_GetTokenInfo"); if(p11t_test_unexpected) { - /** - Null arguments */ rv = (p11t_module_funcs->C_GetTokenInfo)(slot_ids[i], NULL); - p11t_check_returns("C_GetTokenInfo: null arguments", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("Null arguments", rv, CKR_ARGUMENTS_BAD); } - /** - Normal call */ rv = (p11t_module_funcs->C_GetTokenInfo)(slot_ids[i], &slot_token_info[i]); - p11t_check_returns("C_GetTokenInfo", rv, CKR_OK); + P11T_CHECK_RV("Normal call", rv, CKR_OK); - /** - Space padded CK_TOKEN_INFO fields */ - p11t_check_padded("CK_TOKEN_INFO.label", slot_token_info[i].label); - p11t_check_padded("CK_TOKEN_INFO.manufacturerID", slot_token_info[i].manufacturerID); - p11t_check_padded("CK_TOKEN_INFO.model", slot_token_info[i].model); - p11t_check_padded("CK_TOKEN_INFO.serialNumber", slot_token_info[i].serialNumber); + P11T_SECTION("CK_TOKEN_INFO"); - /** - CK_TOKEN_INFO flags are from valid set */ - p11t_check_mask("CK_TOKEN_INFO.flags", slot_token_info[i].flags, + P11T_CHECK_PADDED("label", slot_token_info[i].label); + P11T_CHECK_PADDED("manufacturerID", slot_token_info[i].manufacturerID); + P11T_CHECK_PADDED("model", slot_token_info[i].model); + P11T_CHECK_PADDED("serialNumber", slot_token_info[i].serialNumber); + + P11T_CHECK_MASK("flags", slot_token_info[i].flags, CKF_RNG | CKF_WRITE_PROTECTED | CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_RESTORE_KEY_NOT_NEEDED | CKF_CLOCK_ON_TOKEN | CKF_PROTECTED_AUTHENTICATION_PATH | CKF_DUAL_CRYPTO_OPERATIONS | CKF_TOKEN_INITIALIZED | CKF_SECONDARY_AUTHENTICATION | @@ -204,7 +188,7 @@ test_slot_info(void) /* Can't validate all the statistics, any number is valid */ - /** - Validate token time when CKF_CLOCK_ON_TOKEN */ + P11T_CHECK_NOTE("Validate token time when CKF_CLOCK_ON_TOKEN"); if((slot_token_info[i].flags & CKF_CLOCK_ON_TOKEN) == CKF_CLOCK_ON_TOKEN) { int year, month, day, hour, minute, second, extra, n; @@ -226,37 +210,43 @@ test_slot_info(void) second < 0 || second > 50 || extra != 0) { - p11t_msg_print("C_GetTokenInfo: invalid time on token: %s", buffer); + p11t_check_fail("C_GetTokenInfo: invalid time on token: %s", buffer); } } } else if(p11t_test_unexpected) { - /** - Calling on slot without token */ + P11T_SECTION("C_GetTokenInfo"); + rv = (p11t_module_funcs->C_GetTokenInfo)(slot_ids[i], &slot_token_info[i]); - p11t_check_returns("C_GetSlotInfo: without token", rv, CKR_TOKEN_NOT_PRESENT); + P11T_CHECK_RV("Calling on slot without token", rv, CKR_TOKEN_NOT_PRESENT); } } + P11T_SECTION("C_GetSlotList"); + rv = (p11t_module_funcs->C_GetSlotList)(TRUE, only, &n_only); - p11t_check_returns("C_GetSlotList: only tokens", rv, CKR_OK); + P11T_CHECK_RV("Listing only tokens", rv, CKR_OK); - p11t_check_ulong("C_GetSlotInfo: number of present tokens doesn't match token info flags. ie: CKF_TOKEN_PRESENT", n_only, n_present); + P11T_CHECK_ULONG("Number of present tokens doesn't match token info flags. ie: CKF_TOKEN_PRESENT", n_only, n_present); qsort(only, n_only, sizeof(CK_SLOT_ID), compar_slot_id); qsort(present, n_present, sizeof(CK_SLOT_ID), compar_slot_id); if(memcmp(only, present, sizeof(CK_SLOT_ID) * n_only) != 0) - p11t_msg_print("C_GetSlotInfo: present tokens don't match those in token info flags. ie: CKF_TOKEN_PRESENT"); + P11T_CHECK_FAIL("Present tokens don't match those in token info flags. ie: CKF_TOKEN_PRESENT"); + + return CONTINUE; } -void +static int test_slot_events(void) { - /** C_WaitForSlotEvent */ - /** - Not Implemented */ + P11T_SECTION("C_WaitForSlotEvent"); + P11T_CHECK_NOTE("Not Tested"); + return CONTINUE; } -void +static int test_slot_mechanisms(void) { CK_MECHANISM_TYPE_PTR mech_list; @@ -268,17 +258,16 @@ test_slot_mechanisms(void) assert(p11t_module_funcs); - /** C_GetMechanismList */ + P11T_SECTION("C_GetMechanismList"); if(!p11t_slot_count) - return; + return CONTINUE; if(p11t_test_unexpected) { - /* - Invalid Slot */ mech_count = 0; rv = (p11t_module_funcs->C_GetMechanismList)((CK_ULONG)-10, NULL, &mech_count); - p11t_check_returns("C_GetMechanismList: invalid slot id", rv, CKR_SLOT_ID_INVALID); + P11T_CHECK_RV("Invalid Slot", rv, CKR_SLOT_ID_INVALID); } slot_mech_count = calloc(p11t_slot_count, sizeof(CK_ULONG)); @@ -296,14 +285,12 @@ test_slot_mechanisms(void) if(p11t_test_unexpected) { - /** - Null arguments */ rv = (p11t_module_funcs->C_GetMechanismList)(slot_id, NULL, NULL); - p11t_check_returns("C_GetMechanismList: null arguments", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("Null arguments", rv, CKR_ARGUMENTS_BAD); } - /** - Without buffer */ rv = (p11t_module_funcs->C_GetMechanismList)(slot_id, NULL, &mech_count); - p11t_check_returns("C_GetMechanismList: without buffer", rv, CKR_OK); + P11T_CHECK_RV("Without buffer", rv, CKR_OK); mech_list = NULL; mech_info = NULL; @@ -315,67 +302,63 @@ test_slot_mechanisms(void) if(p11t_test_unexpected) { - /** - Zero count but buffer present */ value = 0; rv = (p11t_module_funcs->C_GetMechanismList)(slot_id, mech_list, &value); - p11t_check_returns("C_GetMechanismList: zero buffer", rv, CKR_BUFFER_TOO_SMALL); - p11t_check_ulong("C_GetMechanismList: should have number of mechs", value, mech_count); + P11T_CHECK_RV("Zero count but buffer present", rv, CKR_BUFFER_TOO_SMALL); + P11T_CHECK_ULONG("Should return number of mechs", value, mech_count); if(mech_count > 1) { - /** - Low count but buffer present */ value = 1; rv = (p11t_module_funcs->C_GetMechanismList)(slot_id, mech_list, &value); - p11t_check_returns("C_GetMechanismList: low buffer", rv, CKR_BUFFER_TOO_SMALL); - p11t_check_ulong("C_GetMechanismList: should have number of mechs", value, mech_count); + P11T_CHECK_RV("Low count but buffer present", rv, CKR_BUFFER_TOO_SMALL); + P11T_CHECK_ULONG("Should return number of mechs", value, mech_count); } } - /* - Call with too much buffer */ + /* - */ if(p11t_test_unexpected) value = mech_count + 5; else value = mech_count; rv = (p11t_module_funcs->C_GetMechanismList)(slot_id, mech_list, &value); - p11t_check_returns("C_GetMechanismList: high buffer", rv, CKR_OK); - p11t_check_ulong("C_GetMechanismList: should have number of mechs", value, mech_count); + P11T_CHECK_RV("Call with too much buffer", rv, CKR_OK); + P11T_CHECK_ULONG("Should return number of mechs", value, mech_count); mech_info = calloc(mech_count, sizeof(CK_MECHANISM_INFO)); assert(mech_info); - /** C_GetMechanismInfo */ + P11T_SECTION("C_GetMechanismInfo"); if(p11t_test_unexpected) { - /** - Invalid mechanism */ rv = (p11t_module_funcs->C_GetMechanismInfo)(slot_id, (CK_ULONG)-10, mech_info); - p11t_check_returns("C_GetMechanismInfo: with invalid mech", rv, CKR_MECHANISM_INVALID); + P11T_CHECK_RV("Invalid mechanism", rv, CKR_MECHANISM_INVALID); - /** - Null arguments */ rv = (p11t_module_funcs->C_GetMechanismInfo)(slot_id, mech_list[0], NULL); - p11t_check_returns("C_GetMechanismInfo: with null arg", rv, CKR_ARGUMENTS_BAD); + P11T_CHECK_RV("Null arguments", rv, CKR_ARGUMENTS_BAD); - /** - Invalid slot id */ rv = (p11t_module_funcs->C_GetMechanismInfo)((CK_ULONG)-11, mech_list[0], mech_info); - p11t_check_returns("C_GetMechanismInfo: with invalid slot", rv, CKR_SLOT_ID_INVALID); + P11T_CHECK_RV("Invalid slot id", rv, CKR_SLOT_ID_INVALID); } for(j = 0; j < mech_count; ++j) { - /** - Normal call */ rv = (p11t_module_funcs->C_GetMechanismInfo)(slot_id, mech_list[i], &mech_info[i]); - p11t_check_returns("C_GetMechanismInfo", rv, CKR_OK); + P11T_CHECK_RV("Normal call", rv, CKR_OK); + + P11T_SECTION("CK_MECHANISM_INFO"); if(mech_info[i].ulMinKeySize > mech_info[i].ulMaxKeySize) - p11t_msg_print("C_GetMechanismInfo: Mechanism min key size is greater than max"); + P11T_CHECK_FAIL("Mechanism min key size should not be greater than max"); - p11t_check_mask("CK_MECHANISM_INFO.flags", mech_info[i].flags, + P11T_CHECK_MASK("flags", mech_info[i].flags, CKF_HW | CKF_ENCRYPT | CKF_DECRYPT | CKF_DIGEST | CKF_SIGN | CKF_SIGN_RECOVER | CKF_VERIFY | CKF_VERIFY_RECOVER | CKF_GENERATE | CKF_GENERATE_KEY_PAIR | CKF_WRAP | CKF_UNWRAP | CKF_DERIVE); - p11t_check_nflag("CK_MECHANISM_INFO.flags", mech_info[i].flags, CKF_EXTENSION); + P11T_CHECK_NFLAG("flags", mech_info[i].flags, CKF_EXTENSION); } } @@ -384,14 +367,16 @@ test_slot_mechanisms(void) slot_mech_type[i] = mech_list; slot_mech_count[i] = mech_count; } + + return CONTINUE; } -void +static int test_init_token(void) { - /** C_InitToken */ - - /** - Not Implemented */ + P11T_SECTION("C_InitToken"); + P11T_CHECK_NOTE("Not Tested"); + return CONTINUE; } void -- cgit v1.2.3