Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve coverage on OSAL unit tests #268

Merged
merged 3 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/unit-test-coverage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ set(OSALCOVERAGE_USER_C_FLAGS "$ENV{OSALCOVERAGE_USER_C_FLAGS}" CACHE STRING "Us
# The currently supported setup is to use the "pc-linux" BSP to execute the "vxworks6"
# code coverage analysis. Because the actual underlying OS calls are stubbed out, there
# is no dependency on the actual underlying OS.
set(OSALCOVERAGE_TARGET_OSTYPE "vxworks6" CACHE STRING "OSAL target(s) to build coverage tests for")
set(OSALCOVERAGE_TARGET_OSTYPE "vxworks;shared" CACHE STRING "OSAL target(s) to build coverage tests for")
set(OSALCOVERAGE_HOST_BSPTYPE "pc-linux" CACHE STRING "OSAL unit test BSP to execute coverage tests")

# OSALCOVERAGE_SYSTEM_OSTYPE indicates which of the BSP packages to include
Expand Down Expand Up @@ -135,9 +135,19 @@ list(APPEND OSALCOVERAGE_STUB_LIB_LIST ut_osapi_stubs)
# - Links to the stub libraries of everything else, plus UT assert
function (add_coverage_tests SETNAME)
foreach(MODNAME ${ARGN})
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/coveragetest-${MODNAME}.c)
set (TESTCASE_SRCFILE)
foreach (SRCFILE
"${PROJECT_SOURCE_DIR}/portable/coveragetest-${MODNAME}.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/coveragetest-${MODNAME}.c"
)
if (EXISTS "${SRCFILE}")
set (TESTCASE_SRCFILE "${SRCFILE}")
endif (EXISTS "${SRCFILE}")
endforeach()

if (TESTCASE_SRCFILE)
set(TESTNAME "${SETNAME}-${MODNAME}")
message (STATUS "Found test case for ${TESTNAME}")
message (STATUS "Found test case for ${TESTNAME} in ${TESTCASE_SRCFILE}")

if (DEFINED MODULE_LINK_MAP_${MODNAME})
set(LINKMOD ${MODULE_LINK_MAP_${MODNAME}})
Expand All @@ -146,7 +156,7 @@ function (add_coverage_tests SETNAME)
endif()

add_executable(${TESTNAME}-testrunner
src/coveragetest-${MODNAME}.c
${TESTCASE_SRCFILE}
$<TARGET_OBJECTS:ut_${SETNAME}_${LINKMOD}>)

set_target_properties(${TESTNAME}-testrunner PROPERTIES LINK_FLAGS "${UT_C_FLAGS}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ void Test_OS_FileStat_Impl(void)

}

void Test_OS_FileChmod_Impl(void)
{
/*
* Test Case For:
* int32 OS_FileChmod_Impl(const char *local_path, uint32 access)
*/
struct OCS_stat RefStat;

/* failure mode 1 (stat) */
UT_SetForceFail(UT_KEY(OCS_stat), -1);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl("local",OS_READ_WRITE), OS_ERROR);
UT_ClearForceFail(UT_KEY(OCS_stat));

/* failure mode 2 (chmod) */
UT_SetForceFail(UT_KEY(OCS_chmod), -1);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl("local",OS_READ_WRITE), OS_ERROR);
UT_ClearForceFail(UT_KEY(OCS_chmod));

/* all permission bits with uid/gid match */
RefStat.st_uid = Osapi_Internal_GetSelfEUID();
RefStat.st_gid = Osapi_Internal_GetSelfEGID();
RefStat.st_mode = ~0;
RefStat.st_size = 1234;
RefStat.st_mtime = 5678;
UT_SetDataBuffer(UT_KEY(OCS_stat), &RefStat, sizeof(RefStat), false);

/* nominal 1 - full permissions with file owned by own uid/gid */
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl("local",OS_READ_WRITE), OS_SUCCESS);

/* nominal 2 - partial permissions */
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl("local", OS_READ_ONLY), OS_SUCCESS);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl("local", OS_WRITE_ONLY), OS_SUCCESS);

/* nominal 3 - non-owned file */
++RefStat.st_uid;
++RefStat.st_gid;
UT_SetDataBuffer(UT_KEY(OCS_stat), &RefStat, sizeof(RefStat), false);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl("local",OS_READ_WRITE), OS_SUCCESS);
}

void Test_OS_FileRemove_Impl (void)
{
/*
Expand Down Expand Up @@ -156,6 +196,7 @@ void OS_Application_Startup(void)
{
ADD_TEST(OS_FileOpen_Impl);
ADD_TEST(OS_FileStat_Impl);
ADD_TEST(OS_FileChmod_Impl);
ADD_TEST(OS_FileRemove_Impl);
ADD_TEST(OS_FileRename_Impl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

#include <overrides/time.h>

/* JPHFIX: FIXME: This should be in a header somehow, not here */
extern int32 OS_GetLocalTime_Impl(OS_time_t *time_struct);
extern int32 OS_SetLocalTime_Impl(const OS_time_t *time_struct);

#define OSAPI_TEST_FUNCTION_RC(func,exp) \
{ \
int32 rcexp = exp; \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ void Test_OS_GenericClose_Impl(void)
* int32 OS_GenericClose_Impl(uint32 local_id)
*/
OSAPI_TEST_FUNCTION_RC(OS_GenericClose_Impl,(0), OS_SUCCESS);

/*
* Test path where underlying close() fails.
* Should still return success.
*/
UT_SetForceFail(UT_KEY(OCS_close), -1);
OSAPI_TEST_FUNCTION_RC(OS_GenericClose_Impl,(0), OS_SUCCESS);
}

void Test_OS_GenericSeek_Impl (void)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# CMake snippet for building the shared OSAL layer coverage tests

set(MODULE_LIST osapi osfileapi osfilesys osloader osnetwork osselect ostimer)
set(MODULE_LIST osapi)

# This unit test is allowed to directly include any internal file in
# the respective set under test.
include_directories(${OSAL_SOURCE_DIR}/src/os/${SETNAME})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/modules/inc)

# The "ut-stubs" contains additional stubs specific to this sub-module
add_subdirectory(ut-stubs)
Expand All @@ -13,6 +14,11 @@ add_subdirectory(ut-stubs)
# (this is not a stub, this is the real code)
add_subdirectory(modules)

set(MODULE_LINK_MAP_posixio osfileapi)
set(MODULE_LINK_MAP_posixfile osfileapi)
set(MODULE_LINK_MAP_posixdirs osfileapi)
set(MODULE_LINK_MAP_posixgettime ostimer)

# Add all coverage tests in the src dir
add_coverage_tests(${SETNAME} ${MODULE_LIST})

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#define SEEK_CUR OCS_SEEK_CUR
#define SEEK_END OCS_SEEK_END

#define STDOUT_FILENO OCS_STDOUT_FILENO
#define STDIN_FILENO OCS_STDIN_FILENO
#define STDERR_FILENO OCS_STDERR_FILENO

/* pthread-related identifiers */
#define PTHREAD_PRIO_INHERIT OCS_PTHREAD_PRIO_INHERIT
#define PTHREAD_MUTEX_RECURSIVE OCS_PTHREAD_MUTEX_RECURSIVE
Expand Down Expand Up @@ -161,6 +165,7 @@
#define ntohs OCS_ntohs
#define opendir OCS_opendir
#define open OCS_open
#define printf(...) OCS_printf(__VA_ARGS__)
#define pthread_attr_destroy OCS_pthread_attr_destroy
#define pthread_attr_getschedparam OCS_pthread_attr_getschedparam
#define pthread_attr_init OCS_pthread_attr_init
Expand Down Expand Up @@ -229,6 +234,7 @@
#define stdout OCS_stdout
#define strcmp OCS_strcmp
#define strcpy OCS_strcpy
#define strerror OCS_strerror
#define strlen OCS_strlen
#define strncmp OCS_strncmp
#define strncpy OCS_strncpy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ OS_common_record_t * const OS_global_queue_table = OS_stub_queue_table;

OS_queue_internal_record_t OS_queue_table[OS_MAX_QUEUES];
OS_task_internal_record_t OS_task_table[OS_MAX_TASKS];
OS_console_internal_record_t OS_console_table[OS_MAX_CONSOLES];

OS_SharedGlobalVars_t OS_SharedGlobalVars =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ UT_DEFAULT_STUB(OS_Posix_TimeBaseAPI_Impl_Init, (void))
UT_DEFAULT_STUB(OS_Posix_ModuleAPI_Impl_Init, (void))
UT_DEFAULT_STUB(OS_Posix_StreamAPI_Impl_Init, (void))
UT_DEFAULT_STUB(OS_Posix_DirAPI_Impl_Init, (void))
UT_DEFAULT_STUB(OS_Posix_FileSysAPI_Impl_Init, (void))

7 changes: 7 additions & 0 deletions src/unit-test-coverage/shared/modules/inc/ut-osapi-idmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ void Osapi_Internal_ResetState(void);
*/
int32 Osapi_Call_ObjectIdFindNext(uint32 idtype, uint32 *array_index, OS_common_record_t **record);

/**
* Wrapper around the OS_ObjectIdConvertLock call so the test code can invoke it
* (it is defined as static)
*/
int32 Osapi_Call_ObjectIdConvertLock(OS_lock_mode_t lock_mode, uint32 idtype, uint32 reference_id, OS_common_record_t *obj);


#endif /* _OSAL_UT_OSAPI_IDMAP_H_ */

6 changes: 6 additions & 0 deletions src/unit-test-coverage/shared/modules/inc/ut-osapi-sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define _OSAL_UT_OSAPI_SOCKETS_H_

#include <common_types.h>
#include <os-impl.h>

/*****************************************************
*
Expand All @@ -20,6 +21,11 @@
*/
void Osapi_Internal_ResetState(void);

/**
* Invoke the OS_CreateSocketName() static helper function
*/
void Osapi_Call_CreateSocketName_Static(OS_stream_internal_record_t *sock,
const OS_SockAddr_t *Addr, const char *parent_name);

#endif /* _OSAL_UT_OSAPI_SOCKETS_H_ */

5 changes: 5 additions & 0 deletions src/unit-test-coverage/shared/modules/src/ut-osapi-idmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ int32 Osapi_Call_ObjectIdFindNext(uint32 idtype, uint32 *array_index, OS_common_
{
return OS_ObjectIdFindNext(idtype, array_index, record);
}

int32 Osapi_Call_ObjectIdConvertLock(OS_lock_mode_t lock_mode, uint32 idtype, uint32 reference_id, OS_common_record_t *obj)
{
return OS_ObjectIdConvertLock(lock_mode, idtype, reference_id, obj);
}
5 changes: 5 additions & 0 deletions src/unit-test-coverage/shared/modules/src/ut-osapi-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ OS_stream_internal_record_t OS_stream_table[OS_MAX_NUM_OPEN_FILES];
OS_common_record_t OS_stub_socket_table[OS_MAX_NUM_OPEN_FILES];
OS_common_record_t * const OS_global_stream_table = OS_stub_socket_table;

void Osapi_Call_CreateSocketName_Static(OS_stream_internal_record_t *sock, const OS_SockAddr_t *Addr, const char *parent_name)
{
OS_CreateSocketName(sock, Addr, parent_name);
}

11 changes: 11 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "os-shared-coveragetest.h"
#include "ut-osapi-binsem.h"

#include <overrides/string.h>

/*
**********************************************************************************
** PUBLIC API FUNCTIONS
Expand Down Expand Up @@ -47,6 +49,10 @@ void Test_OS_BinSemCreate(void)

UtAssert_True(actual == expected, "OS_BinSemCreate() (%ld) == OS_SUCCESS", (long)actual);
UtAssert_True(objid != 0, "objid (%lu) != 0", (unsigned long)objid);

OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(NULL, NULL, 0, 0), OS_INVALID_POINTER);
UT_SetForceFail(UT_KEY(OCS_strlen), 10 + OS_MAX_API_NAME);
OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(&objid, "UT", 0, 0), OS_ERR_NAME_TOO_LONG);
}

void Test_OS_BinSemDelete(void)
Expand Down Expand Up @@ -141,6 +147,8 @@ void Test_OS_BinSemGetIdByName(void)
actual = OS_BinSemGetIdByName(&objid, "NF");
UtAssert_True(actual == expected, "OS_BinSemGetIdByName() (%ld) == %ld",
(long)actual, (long)expected);

OSAPI_TEST_FUNCTION_RC(OS_BinSemGetIdByName(NULL, NULL), OS_INVALID_POINTER);
}

void Test_OS_BinSemGetInfo(void)
Expand Down Expand Up @@ -168,6 +176,9 @@ void Test_OS_BinSemGetInfo(void)
(unsigned long)prop.creator);
UtAssert_True(strcmp(prop.name, "ABC") == 0, "prop.name (%s) == ABC",
prop.name);


OSAPI_TEST_FUNCTION_RC(OS_BinSemGetInfo(0, NULL), OS_INVALID_POINTER);
}


Expand Down
54 changes: 37 additions & 17 deletions src/unit-test-coverage/shared/src/coveragetest-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
extern void OS_CleanUpObject(uint32 object_id, void *arg);


int32 Test_MicroSecPerTick = 0;
int32 Test_TicksPerSecond = 0;

/*
**********************************************************************************
Expand All @@ -35,8 +37,8 @@ extern void OS_CleanUpObject(uint32 object_id, void *arg);
/* as a side effect, the OS_TimeBaseAPI_Init must initialize the globals */
static int32 TimeBaseInitGlobal(void *UserObj, int32 StubRetcode, uint32 CallCount, const UT_StubContext_t *Context)
{
OS_SharedGlobalVars.MicroSecPerTick = 1000;
OS_SharedGlobalVars.TicksPerSecond = 1000;
OS_SharedGlobalVars.MicroSecPerTick = Test_MicroSecPerTick;
OS_SharedGlobalVars.TicksPerSecond = Test_TicksPerSecond;
return StubRetcode;
}

Expand Down Expand Up @@ -71,25 +73,39 @@ static int32 SetShutdownFlagHook(void *UserObj, int32 StubRetcode, uint32 CallCo
*/
void Test_OS_API_Init(void)
{
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;

/* Setup Inputs */

UT_SetHookFunction(UT_KEY(OS_TimeBaseAPI_Init), TimeBaseInitGlobal, NULL);

/* Execute Test */
actual = OS_API_Init();
Test_MicroSecPerTick = 0;
Test_TicksPerSecond = 0;
OS_SharedGlobalVars.Initialized = false;
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_ERROR);

/* Verify Outputs */
UtAssert_True(actual == expected, "OS_API_Init() (%ld) != OS_SUCCESS", (long)actual);
Test_MicroSecPerTick = 1000;
Test_TicksPerSecond = 1000;
OS_SharedGlobalVars.Initialized = false;
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_SUCCESS);

/* Second call should return ERROR */
expected = OS_ERROR;
actual = OS_API_Init();
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_ERROR);

/* other error paths */
OS_SharedGlobalVars.Initialized = false;
UT_SetForceFail(UT_KEY(OS_ObjectIdInit), -222);
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), -222);
UT_ResetState(UT_KEY(OS_ObjectIdInit));

OS_SharedGlobalVars.Initialized = false;
UT_SetForceFail(UT_KEY(OS_API_Impl_Init), -333);
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), -333);
UT_ResetState(UT_KEY(OS_API_Impl_Init));

OS_SharedGlobalVars.Initialized = false;
UT_SetForceFail(UT_KEY(OS_TaskAPI_Init), -444);
OSAPI_TEST_FUNCTION_RC(OS_API_Init(), -444);
UT_ResetState(UT_KEY(OS_TaskAPI_Init));

/* Verify Outputs */
UtAssert_True(actual == expected, "OS_API_Init() (%ld) != OS_ERROR", (long)actual);
}

void Test_OS_ApplicationExit(void)
Expand Down Expand Up @@ -119,6 +135,9 @@ void Test_OS_CleanUpObject(void)
objtype = OS_OBJECT_TYPE_UNDEFINED;
while (objtype < OS_OBJECT_TYPE_USER)
{
UT_ResetState(0);
UT_SetForceFail(UT_KEY(OS_IdentifyObject), objtype);

switch(objtype)
{
case OS_OBJECT_TYPE_OS_TASK:
Expand Down Expand Up @@ -158,21 +177,22 @@ void Test_OS_CleanUpObject(void)

if (delhandler != 0)
{
UT_ResetState(0);
/* note the return code here is ignored -
* the goal is simply to defeat the default
* check that the objid was valid (it isn't) */
UT_SetForceFail(delhandler, OS_ERROR);
UT_SetForceFail(UT_KEY(OS_IdentifyObject), objtype);
OS_CleanUpObject(0, &ActualObjs);

CallCount = UT_GetStubCount(delhandler);
UtAssert_True(CallCount == 1, "Objtype %lu call count (%lu) == 1",
(unsigned long)objtype, (unsigned long)CallCount);
++ExpObjs;
}

else
{
OS_CleanUpObject(0, &ActualObjs);
}
++objtype;
++ExpObjs;
}


Expand Down
Loading