Skip to content

Commit

Permalink
Fix nasa#602, bring OSAL code coverage back up to 100%
Browse files Browse the repository at this point in the history
Adds test cases where necessary to get 100% line coverage
on VxWorks impl + shared/portable layers.
  • Loading branch information
jphickey committed Jan 22, 2021
1 parent bfca5b2 commit bebeb70
Show file tree
Hide file tree
Showing 28 changed files with 348 additions and 181 deletions.
11 changes: 10 additions & 1 deletion src/os/shared/inc/os-shared-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,13 @@ int32 OS_FileRename_Impl(const char *old_path, const char *new_path);
------------------------------------------------------------------*/
int32 OS_FileChmod_Impl(const char *local_path, uint32 access);

#endif /* OS_SHARED_FILE_H */

/*
* Internal helper function
*
* Not called outside this unit, but need to be prototyped
* here for coverage test.
*/
int32 OS_FileIteratorClose(osal_id_t filedes, void *arg);

#endif /* OS_SHARED_FILE_H */
3 changes: 2 additions & 1 deletion src/os/shared/inc/os-shared-filesys.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ int32 OS_FileSysUnmountVolume_Impl(const OS_object_token_t *token);
bool OS_FileSys_FindVirtMountPoint(void *ref, const OS_object_token_t *token, const OS_common_record_t *obj);
int32 OS_FileSys_Initialize(char *address, const char *fsdevname, const char *fsvolname, size_t blocksize,
osal_blockcount_t numblocks, bool should_format);
bool OS_FileSysFilterFree(void *ref, const OS_object_token_t *token, const OS_common_record_t *obj);

#endif /* OS_SHARED_FILESYS_H */
#endif /* OS_SHARED_FILESYS_H */
14 changes: 14 additions & 0 deletions src/unit-test-coverage/portable/src/coveragetest-posix-dirs.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,32 @@
#include <OCS_stdlib.h>
#include <OCS_dirent.h>
#include <OCS_unistd.h>
#include <OCS_fcntl.h>
#include <OCS_stat.h>
#include <OCS_errno.h>

void Test_OS_DirCreate_Impl(void)
{
/*
* Test Case For:
* int32 OS_DirCreate_Impl(const char *local_path, uint32 access)
*/
struct OCS_stat statbuf;

OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_SUCCESS);

/* With errno other than EEXIST it should return OS_ERROR */
OCS_errno = OCS_EROFS;
UT_SetDefaultReturnValue(UT_KEY(OCS_mkdir), -1);
OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_ERROR);

/* If the errno is EEXIST it should return success */
OCS_errno = OCS_EEXIST;
memset(&statbuf, 0, sizeof(statbuf));
statbuf.st_mode = OCS_S_IFDIR;
UT_SetDataBuffer(UT_KEY(OCS_stat), &statbuf, sizeof(statbuf), false);
UT_SetDefaultReturnValue(UT_KEY(OCS_mkdir), -1);
OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_SUCCESS);
}

void Test_OS_DirOpen_Impl(void)
Expand Down
41 changes: 39 additions & 2 deletions src/unit-test-coverage/shared/src/coveragetest-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ static int32 TimeBaseInitGlobal(void *UserObj, int32 StubRetcode, uint32 CallCou

static int32 ObjectDeleteCountHook(void *UserObj, int32 StubRetcode, uint32 CallCount, const UT_StubContext_t *Context)
{
uint32 *counter = (uint32 *)Context->ArgPtr[1];
uint32 *counter = UT_Hook_GetArgValueByName(Context, "callback_arg", uint32 *);

if (CallCount == 0)
if (CallCount < 2)
{
*counter = 1;
}
Expand All @@ -77,6 +77,11 @@ static int32 SetShutdownFlagHook(void *UserObj, int32 StubRetcode, uint32 CallCo
return StubRetcode;
}

static int32 TestEventHandlerHook(OS_Event_t event, osal_id_t object_id, void *data)
{
return UT_DEFAULT_IMPL(TestEventHandlerHook);
}

/*
**********************************************************************************
** PUBLIC API FUNCTIONS
Expand Down Expand Up @@ -258,6 +263,37 @@ void Test_OS_IdleLoopAndShutdown(void)
UtAssert_True(CallCount == 1, "OS_ApplicationShutdown_Impl() call count (%lu) == 1", (unsigned long)CallCount);
}

void Test_OS_NotifyEvent(void)
{
/*
* Test cases for:
* int32 OS_NotifyEvent(OS_Event_t event, osal_id_t object_id, void *data)
* int32 OS_RegisterEventHandler(OS_EventHandler_t handler)
*/

OS_SharedGlobalVars.EventHandler = NULL;

/* With no hook function registered OS_NotifyEvent() should return success */
OSAPI_TEST_FUNCTION_RC(OS_NotifyEvent(OS_EVENT_RESERVED, OS_OBJECT_ID_UNDEFINED, NULL), OS_SUCCESS);

/* Registering a NULL hook function should fail */
OSAPI_TEST_FUNCTION_RC(OS_RegisterEventHandler(NULL), OS_INVALID_POINTER);

/* Now Register the locally-defined hook function */
OSAPI_TEST_FUNCTION_RC(OS_RegisterEventHandler(TestEventHandlerHook), OS_SUCCESS);

/* Now this should invoke the test hook */
OSAPI_TEST_FUNCTION_RC(OS_NotifyEvent(OS_EVENT_RESERVED, OS_OBJECT_ID_UNDEFINED, NULL), OS_SUCCESS);
UtAssert_STUB_COUNT(TestEventHandlerHook, 1);

/* Should also return whatever the hook returned */
UT_SetDefaultReturnValue(UT_KEY(TestEventHandlerHook), -12345);
OSAPI_TEST_FUNCTION_RC(OS_NotifyEvent(OS_EVENT_RESERVED, OS_OBJECT_ID_UNDEFINED, NULL), -12345);
UtAssert_STUB_COUNT(TestEventHandlerHook, 2);

OS_SharedGlobalVars.EventHandler = NULL;
}

/* ------------------- End of test cases --------------------------------------*/

/* Osapi_Test_Setup
Expand Down Expand Up @@ -288,4 +324,5 @@ void UtTest_Setup(void)
ADD_TEST(OS_CleanUpObject);
ADD_TEST(OS_IdleLoopAndShutdown);
ADD_TEST(OS_ApplicationExit);
ADD_TEST(OS_NotifyEvent);
}
7 changes: 7 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ void Test_OS_CloseAllFiles(void)
actual = OS_CloseAllFiles();

UtAssert_True(actual == expected, "OS_CloseAllFiles() (%ld) == -222", (long)actual);

/* This uses a helper function OS_FileIteratorClose() with the iterator,
* which needs to be called for coverage - it just invokes OS_close() */
expected = OS_SUCCESS;
actual = OS_FileIteratorClose(UT_OBJID_1, NULL);

UtAssert_True(actual == expected, "OS_FileIteratorClose() (%ld) == OS_SUCCESS", (long)actual);
}

/* Osapi_Test_Setup
Expand Down
19 changes: 13 additions & 6 deletions src/unit-test-coverage/shared/src/coveragetest-filesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,10 @@ void Test_OS_GetFsInfo(void)
* Test Case For:
* int32 OS_GetFsInfo(OS_FsInfo_t *filesys_info)
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
os_fsinfo_t filesys_info;
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
os_fsinfo_t filesys_info;
OS_common_record_t rec;

UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdIteratorGetNext), 1);
UT_SetDeferredRetcode(UT_KEY(OS_ObjectIdIteratorGetNext), 3, 0);
Expand All @@ -469,15 +470,21 @@ void Test_OS_GetFsInfo(void)
"filesys_info.MaxVolumes (%lu) == OS_MAX_FILE_SYSTEMS", (unsigned long)filesys_info.MaxVolumes);

/* since there are no open files, the free fd count should match the max */
UtAssert_True(filesys_info.FreeFds == 2, "filesys_info.FreeFds (%lu) == 2",
(unsigned long)filesys_info.FreeFds);
UtAssert_True(filesys_info.FreeFds == 2, "filesys_info.FreeFds (%lu) == 2", (unsigned long)filesys_info.FreeFds);

UtAssert_True(filesys_info.FreeVolumes == 3, "filesys_info.FreeVolumes (%lu) == 3",
(unsigned long)filesys_info.FreeVolumes);
(unsigned long)filesys_info.FreeVolumes);

expected = OS_INVALID_POINTER;
actual = OS_GetFsInfo(NULL);
UtAssert_True(actual == expected, "OS_GetFsInfo() (%ld) == OS_INVALID_POINTER", (long)actual);

/* This function uses a helper OS_FileSysFilterFree() that needs to be called for coverage. */
/* It is just a wrapper around OS_ObjectIdDefined() for the record ID */
memset(&rec, 0, sizeof(rec));
UtAssert_True(OS_FileSysFilterFree(NULL, NULL, &rec), "OS_FileSysFilterFree() (unused record)");
rec.active_id = UT_OBJID_1;
UtAssert_True(!OS_FileSysFilterFree(NULL, NULL, &rec), "!OS_FileSysFilterFree() (used record)");
}

void Test_OS_TranslatePath(void)
Expand Down
47 changes: 46 additions & 1 deletion src/unit-test-coverage/shared/src/coveragetest-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,44 @@ void Test_OS_SymbolLookup(void)
UtAssert_True(actual == expected, "OS_SymbolLookup(UT_staticsym) (%ld) == OS_SUCCESS", (long)actual);
}

void Test_OS_ModuleSymbolLookup(void)
{
/*
* Test Case For:
* int32 OS_ModuleSymbolLookup(osal_id_t module_id, cpuaddr *symbol_address, const char *symbol_name)
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
cpuaddr testaddr = 0;
cpuaddr symaddr = 0;

actual = OS_ModuleSymbolLookup(OS_OBJECT_ID_UNDEFINED, &symaddr, "uttestsym0");
UtAssert_True(actual == expected, "OS_ModuleSymbolLookup(name=%s) (%ld) == OS_SUCCESS", "uttestsym0", (long)actual);

UT_ResetState(UT_KEY(OS_ModuleSymbolLookup_Impl));
UT_SetDefaultReturnValue(UT_KEY(OS_ModuleSymbolLookup_Impl), OS_ERROR);

/* this lookup should always fail */
symaddr = 0;
testaddr = 0;
actual = OS_ModuleSymbolLookup(OS_OBJECT_ID_UNDEFINED, &symaddr, "uttestsym1");
expected = OS_ERROR;
UtAssert_True(actual == expected, "OS_ModuleSymbolLookup(name=%s) (%ld) == OS_ERROR", "uttestsym1", (long)actual);
UtAssert_True(symaddr == testaddr, "OS_ModuleSymbolLookup(address=%lx) == %lx", (unsigned long)symaddr,
(unsigned long)testaddr);

actual = OS_ModuleSymbolLookup(OS_OBJECT_ID_UNDEFINED, NULL, NULL);
expected = OS_INVALID_POINTER;
UtAssert_True(actual == expected, "OS_ModuleSymbolLookup(NULL) (%ld) == OS_INVALID_POINTER", (long)actual);

/*
* Look up a symbol that is present in the static symbol table
*/
actual = OS_ModuleSymbolLookup(OS_OBJECT_ID_UNDEFINED, &symaddr, "UT_staticsym");
expected = OS_SUCCESS;
UtAssert_True(actual == expected, "OS_ModuleSymbolLookup(UT_staticsym) (%ld) == OS_SUCCESS", (long)actual);
}

void Test_OS_StaticSymbolLookup(void)
{
/*
Expand Down Expand Up @@ -190,7 +228,7 @@ void Test_OS_StaticSymbolLookup(void)
UtAssert_True(actual == expected, "OS_ModuleLoad_Static(name=%s) (%ld) == OS_SUCCESS", "UT", (long)actual);

expected = OS_ERROR;
actual = OS_SymbolLookup_Static(&addr, "UT_staticsym", "NoModuleMatch");
actual = OS_SymbolLookup_Static(&addr, "UT_staticsym", "NoModuleMatch");
UtAssert_True(actual == expected, "OS_SymbolLookup_Static(name=%s, NoModuleMatch) (%ld) == OS_ERROR", "Test_Func1",
(long)actual);
UtAssert_True(addr == (cpuaddr)&Test_DummyFunc, "OS_SymbolLookup_Static(address=%lx) == %lx", (unsigned long)addr,
Expand Down Expand Up @@ -221,6 +259,12 @@ void Test_OS_SymbolTableDump(void)
expected = OS_ERROR;
actual = OS_SymbolTableDump("test", OSAL_SIZE_C(555));
UtAssert_True(actual == expected, "OS_SymbolTableDump() (%ld) == OS_ERROR", (long)actual);

UT_ResetState(UT_KEY(OS_TranslatePath));
UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdTransactionInit), OS_ERROR);
expected = OS_ERROR;
actual = OS_SymbolTableDump("test", OSAL_SIZE_C(555));
UtAssert_True(actual == expected, "OS_SymbolTableDump() (%ld) == OS_ERROR", (long)actual);
}

void Test_OS_ModuleGetInfo(void)
Expand Down Expand Up @@ -274,6 +318,7 @@ void UtTest_Setup(void)
ADD_TEST(OS_ModuleLoad);
ADD_TEST(OS_ModuleUnload);
ADD_TEST(OS_SymbolLookup);
ADD_TEST(OS_ModuleSymbolLookup);
ADD_TEST(OS_ModuleGetInfo);
ADD_TEST(OS_SymbolTableDump);
ADD_TEST(OS_StaticSymbolLookup);
Expand Down
39 changes: 33 additions & 6 deletions src/unit-test-coverage/shared/src/coveragetest-mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,26 @@ void Test_OS_MutSemGive(void)
* Test Case For:
* int32 OS_MutSemGive ( uint32 sem_id )
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
OS_mutex_internal_record_t *mutex;
int32 expected;
int32 actual;

actual = OS_MutSemGive(UT_OBJID_1);
expected = OS_SUCCESS;

/* Set up for "last owner" matching the calling task (nominal) */
mutex = &OS_mutex_table[1];
mutex->last_owner = OS_TaskGetId();
actual = OS_MutSemGive(UT_OBJID_1);

UtAssert_True(actual == expected, "OS_MutSemGive() (%ld) == OS_SUCCESS", (long)actual);

/* owner should be unset */
UtAssert_True(!OS_ObjectIdDefined(mutex->last_owner), "Mutex owner unset");

/* Call again when not "owned". This still works (or at least it calls the OS impl)
* but should generate a debug message */
actual = OS_MutSemGive(UT_OBJID_1);
UtAssert_True(actual == expected, "OS_MutSemGive(), not owned (%ld) == OS_SUCCESS", (long)actual);
}

void Test_OS_MutSemTake(void)
Expand All @@ -99,11 +113,24 @@ void Test_OS_MutSemTake(void)
* Test Case For:
* int32 OS_MutSemTake ( uint32 sem_id )
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
OS_mutex_internal_record_t *mutex;
int32 expected;
int32 actual;

actual = OS_MutSemTake(UT_OBJID_1);
expected = OS_SUCCESS;

/* Set up for "last owner" being undefined (nominal) */
mutex = &OS_mutex_table[1];
mutex->last_owner = OS_OBJECT_ID_UNDEFINED;
actual = OS_MutSemTake(UT_OBJID_1);
UtAssert_True(actual == expected, "OS_MutSemTake() (%ld) == OS_SUCCESS", (long)actual);

/* owner should be set */
UtAssert_True(OS_ObjectIdDefined(mutex->last_owner), "Mutex owner set");

/* Call again when not already "owned". This still works (or at least it calls the OS impl)
* but should generate a debug message */
actual = OS_MutSemTake(UT_OBJID_1);
UtAssert_True(actual == expected, "OS_MutSemTake() (%ld) == OS_SUCCESS", (long)actual);
}

Expand Down
8 changes: 7 additions & 1 deletion src/unit-test-coverage/shared/src/coveragetest-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,20 @@ void Test_OS_QueuePut(void)
int32 actual = ~OS_SUCCESS;
const char Data[4] = "xyz";

actual = OS_QueuePut(UT_OBJID_1, Data, sizeof(Data), 0);
OS_queue_table[1].max_depth = 10;
OS_queue_table[1].max_size = sizeof(Data);

actual = OS_QueuePut(UT_OBJID_1, Data, sizeof(Data), 0);
UtAssert_True(actual == expected, "OS_QueuePut() (%ld) == OS_SUCCESS", (long)actual);

/* test error cases */
expected = OS_INVALID_POINTER;
actual = OS_QueuePut(UT_OBJID_1, NULL, sizeof(Data), 0);
UtAssert_True(actual == expected, "OS_QueuePut() (%ld) == OS_INVALID_POINTER", (long)actual);

expected = OS_QUEUE_INVALID_SIZE;
actual = OS_QueuePut(UT_OBJID_1, Data, 1 + sizeof(Data), 0);
UtAssert_True(actual == expected, "OS_QueuePut() (%ld) == OS_QUEUE_INVALID_SIZE", (long)actual);
}

void Test_OS_QueueGetIdByName(void)
Expand Down
Loading

0 comments on commit bebeb70

Please sign in to comment.