From 16d58719b93f854e665c35a9b695d9061a7e5a16 Mon Sep 17 00:00:00 2001 From: Chris Knight Date: Tue, 31 Mar 2020 06:47:06 -0700 Subject: [PATCH] fix for #397 - strawman for discussion --- src/unit-tests/inc/ut_os_support.h | 28 +++ .../oscore-test/ut_oscore_binsem_test.c | 170 ++---------------- 2 files changed, 42 insertions(+), 156 deletions(-) diff --git a/src/unit-tests/inc/ut_os_support.h b/src/unit-tests/inc/ut_os_support.h index c8955de67..29346ea44 100644 --- a/src/unit-tests/inc/ut_os_support.h +++ b/src/unit-tests/inc/ut_os_support.h @@ -41,6 +41,34 @@ */ #define UT_OS_IO_BUFF_SIZE 128 +static inline bool UtOsalRetVal(int32 Fn, int32 Exp, const char *File, uint32 Line, const char *FnTxt, const char *ExpTxt, const char *Msg) +{ + return UtAssertEx(Fn == Exp, UtAssert_GetContext(), File, Line, "%s (%d) == %s (%d): %s", FnTxt, (int)Fn, ExpTxt, (int)Exp, Msg); +} + +/* Only report errors */ +static inline bool UtOsalCheck(int32 Fn, int32 Exp, const char *File, uint32 Line, const char *FnTxt, const char *ExpTxt, const char *Msg) +{ + return Fn == Exp ? true : + UtAssertEx(Fn == Exp, UtAssert_GetContext(), File, Line, "%s (%d) == %s (%d): %s", FnTxt, (int)Fn, ExpTxt, (int)Exp, Msg); +} + +static inline bool UtOsalImplemented(int32 Fn, const char *File, uint32 Line) +{ + if (Fn == OS_ERR_NOT_IMPLEMENTED) + { + UtAssertEx(false, UTASSERT_CASETYPE_NA, File, Line, "API not implemented"); + return false; + } + + return true; +} + + +#define UT_OS_SUCCESS(Fn, Msg) UtOsalRetVal(Fn, OS_SUCCESS, __FILE__, __LINE__, #Fn, "OS_SUCCESS", Msg) +#define UT_OS_RETVAL(Fn, Exp, Msg) UtOsalRetVal(Fn, Exp, __FILE__, __LINE__, #Fn, #Exp, Msg) +#define UT_OS_CHECK(Fn, Msg) UtOsalCheck(Fn, OS_SUCCESS, __FILE__, __LINE__, #Fn, "OS_SUCCESS", Msg) +#define UT_OS_IMPL(Fn) UtOsalImplemented(Fn, __FILE__, __LINE__) /*--------------------------------------------------------------------------------*/ diff --git a/src/unit-tests/oscore-test/ut_oscore_binsem_test.c b/src/unit-tests/oscore-test/ut_oscore_binsem_test.c index 892b155ca..93debaf86 100644 --- a/src/unit-tests/oscore-test/ut_oscore_binsem_test.c +++ b/src/unit-tests/oscore-test/ut_oscore_binsem_test.c @@ -34,84 +34,6 @@ ** Local function definitions **--------------------------------------------------------------------------------*/ -/* Test code template for testing a single OSAL API with multiple test cases */ - -#if 0 -void UT_os_sample_test() -{ - /* Must declare these variables for each function. They can be renamed. - * They're referenced in the macros used to track test cases and their results. */ - int32 idx = 0; - const char* testDesc; - - /*-----------------------------------------------------* - * For each test case, - * 1. Assign testDesc a brief description of the test - * 2. Setup the test environment, if necessary - * 3. Run the test - * 4. Log result by calling UT_OS_SET_TEST_RESULT_MACRO - * 4. Reset the test environment, if neccessary - * - * NOTE: "Not implemented" is always checked first but not - * being included as a test case. - * "Nominal" test case is always the last test case. - *-----------------------------------------------------*/ - - /*-----------------------------------------------------*/ - testDesc = "API not implemented"; - - /* TODO: Setup the test environment, if necessary */ - - if (OS_xxx() == OS_ERR_NOT_IMPLEMENTED) - { - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_NA); - goto UT_os_sample_test_exit_tag; - } - - /* TODO: Reset the test environment here, if necessary */ - - /*-----------------------------------------------------*/ - testDesc = "#1 Null-pointer-arg"; - - /* TODO: Setup the test environment here, if necessary */ - - if (OS_xxx(NULL,...) == OS_INVALID_POINTER) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); - - /* TODO: Reset the test environment here, if necessary */ - - /*-----------------------------------------------------*/ - testDesc = "#2 Name-too-long-arg"; - - /* TODO: Setup the test environment here, if necessary */ - - if (OS_xxx(aVeryLoooooongName) == OS_ERR_NAME_TOO_LONG) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); - - /* TODO: Reset the test environment here, if necessary */ - - /*-----------------------------------------------------*/ - testDesc = "#3 Nominal"; - - /* TODO: Setup the test environment here, if necessary */ - - if (OS_xxx(...) != OS_SUCCESS) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - - /* TODO: Reset the test environment here, if necessary */ - -UT_os_sample_test_exit_tag: - return; - -} -#endif - /*--------------------------------------------------------------------------------* ** Syntax: OS_BinSemCreate ** Purpose: Creates a binary semaphore @@ -126,128 +48,64 @@ void UT_os_sample_test() void UT_os_bin_sem_create_test() { int i; - int32 res = 0; - const char* testDesc; - uint32 test_setup_invalid = 0; char sem_name[UT_OS_NAME_BUFF_SIZE]; char long_sem_name[UT_OS_NAME_BUFF_SIZE]; uint32 sem_ids[OS_MAX_BIN_SEMAPHORES+1]; /*-----------------------------------------------------*/ - testDesc = "API not implemented"; - - res = OS_BinSemCreate(&sem_ids[0], "Good", 1, 0 ); - if (res == OS_ERR_NOT_IMPLEMENTED) - { - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_NA); - goto UT_os_bin_sem_create_test_exit_tag; - } - - /* Clean up */ + if (!UT_OS_IMPL(OS_BinSemCreate(&sem_ids[0], "Good", 1, 0))) return; OS_BinSemDelete(sem_ids[0]); /*-----------------------------------------------------*/ - testDesc = "#1 Null-pointer-arg-1"; - - res = OS_BinSemCreate(NULL, "BinSem1", 1, 0); - if (res == OS_INVALID_POINTER) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); + UT_OS_RETVAL(OS_BinSemCreate(NULL, "BinSem1", 1, 0), OS_INVALID_POINTER, "#1 Null-pointer-arg-1"); /*-----------------------------------------------------*/ - testDesc = "#2 Null-pointer-arg-2"; - - res = OS_BinSemCreate(&sem_ids[0], NULL, 1, 0); - if (res == OS_INVALID_POINTER) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); + UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[0], NULL, 1, 0), OS_INVALID_POINTER, "#2 Null-pointer-arg-2"); /*-----------------------------------------------------*/ - testDesc = "#3 Name-too-long"; - memset(long_sem_name, 'X', sizeof(long_sem_name)); long_sem_name[sizeof(long_sem_name)-1] = '\0'; - res = OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0); - if (res == OS_ERR_NAME_TOO_LONG) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); + UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0), OS_ERR_NAME_TOO_LONG, "#3 Name-too-long"); /*-----------------------------------------------------*/ - testDesc = "#4 No-free-IDs"; - /* Setup */ for ( i = 0; i< OS_MAX_BIN_SEMAPHORES; i++ ) { memset(sem_name, '\0', sizeof(sem_name)); UT_os_sprintf(sem_name, "BINSEM%d", i); - res = OS_BinSemCreate(&sem_ids[i], sem_name, 1, 0); - if ( res != OS_SUCCESS ) + if(!UT_OS_CHECK(OS_BinSemCreate(&sem_ids[i], sem_name, 1, 0), "BinSemCreate")) { - testDesc = "#4 No-free-IDs - Bin Sem Create failed"; - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_TSF); - test_setup_invalid = 1; - break; + break; } } - if ( test_setup_invalid == 0 ) + if ( i == OS_MAX_BIN_SEMAPHORES ) { - res = OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0); - if (res == OS_ERR_NO_FREE_IDS) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); - + UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0), OS_ERR_NO_FREE_IDS, "#4 No-free-IDs"); } /* Reset test environment */ OS_DeleteAllObjects(); /*-----------------------------------------------------*/ - testDesc = "#5 Duplicate-name"; - /* Setup */ - res = OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0); - if ( res != OS_SUCCESS ) + if(UT_OS_CHECK(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), "BinSemCreate")) { - testDesc = "#5 Duplicate-name - Bin Sem Create failed"; - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_TSF); - } - else - { - res = OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0); - if (res == OS_ERR_NAME_TAKEN) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); + UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), OS_ERR_NAME_TAKEN, "#5 Duplicate-name"); /* Reset test environment */ - res = OS_BinSemDelete(sem_ids[0]); + UT_OS_CHECK(OS_BinSemDelete(sem_ids[0]), "BinSemDelete"); } /*-----------------------------------------------------*/ - testDesc = "#6 OS-call-failure"; - - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_INFO); + UT_OS_TEST_RESULT( "#6 OS-call-failure", UTASSERT_CASETYPE_INFO); /*-----------------------------------------------------*/ - testDesc = "#7 Nominal"; - - res = OS_BinSemCreate(&sem_ids[0], "Good", 1, 0); - if ( res == OS_SUCCESS ) - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS); - else - UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE); + UT_OS_SUCCESS(OS_BinSemCreate(&sem_ids[0], "Good", 1, 0), "#7 Nominal"); /* Reset test environment */ - res = OS_BinSemDelete(sem_ids[0]); - -UT_os_bin_sem_create_test_exit_tag: - return; + UT_OS_CHECK(OS_BinSemDelete(sem_ids[0]), "BinSemDelete"); } /*--------------------------------------------------------------------------------*