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

Fix #791, Add unit test branch coverage #1114

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 43 additions & 1 deletion src/unit-test-coverage/portable/src/coveragetest-bsd-select.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,22 @@ void Test_OS_SelectSingle_Impl(void)
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 0), OS_ERR_OPERATION_NOT_SUPPORTED);
UT_PortablePosixIOTest_Set_Selectable(UT_INDEX_0, true);
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 0), OS_SUCCESS);

/* Cover FD_ISSET true branches and pend */
UT_SetDeferredRetcode(UT_KEY(OCS_FD_ISSET), 1, true);
UT_SetDeferredRetcode(UT_KEY(OCS_FD_ISSET), 1, true);
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, -1), OS_SUCCESS);

/* No flags and non-read/write flag branches */
SelectFlags = 0;
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 0), OS_SUCCESS);
SelectFlags = OS_STREAM_STATE_BOUND;
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 0), OS_SUCCESS);

/* try a case where select() needs to be repeated to achieve the desired wait time */
UT_ResetState(UT_KEY(OCS_clock_gettime));
UT_ResetState(UT_KEY(OCS_select));
UT_SetDefaultReturnValue(UT_KEY(OCS_select), -1);
OCS_errno = OCS_EINTR;
UT_SetDeferredRetcode(UT_KEY(OCS_select), 2, 0);
Expand All @@ -66,10 +76,22 @@ void Test_OS_SelectSingle_Impl(void)
latertime2.tv_sec = 2;
latertime2.tv_nsec = 200000000;
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &nowtime, sizeof(nowtime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime, sizeof(latertime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime2, sizeof(latertime2), false);
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 1200), OS_ERROR_TIMEOUT);
UtAssert_STUB_COUNT(OCS_clock_gettime, 3);
UtAssert_STUB_COUNT(OCS_select, 2);

/* Repeaded select with alternate branches */
OCS_errno = OCS_EAGAIN;
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
latertime2.tv_nsec = 300000000;
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &nowtime, sizeof(nowtime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime, sizeof(latertime), false);
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime2, sizeof(latertime2), false);
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 1200), OS_ERROR_TIMEOUT);
UtAssert_STUB_COUNT(OCS_clock_gettime, 6);
UtAssert_STUB_COUNT(OCS_select, 3);

UT_SetDefaultReturnValue(UT_KEY(OCS_select), 0);
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
Expand Down Expand Up @@ -107,19 +129,30 @@ void Test_OS_SelectMultiple_Impl(void)
*/
OS_FdSet ReadSet;
OS_FdSet WriteSet;
int i;

UT_PortablePosixIOTest_Set_FD(UT_INDEX_0, 0);
UT_PortablePosixIOTest_Set_Selectable(UT_INDEX_0, true);

memset(&ReadSet, 0, sizeof(ReadSet));
memset(&WriteSet, 0, sizeof(WriteSet));
WriteSet.object_ids[0] = 1;
OSAPI_TEST_FUNCTION_RC(OS_SelectMultiple_Impl, (NULL, &WriteSet, 0), OS_SUCCESS);
ReadSet.object_ids[0] = 1;
OSAPI_TEST_FUNCTION_RC(OS_SelectMultiple_Impl, (&ReadSet, NULL, 0), OS_SUCCESS);

/* Branches for processing the set */
UT_SetDeferredRetcode(UT_KEY(OCS_FD_ISSET), 1, true);
WriteSet.object_ids[0] = 0x0D;
UT_PortablePosixIOTest_Set_FD(OSAL_INDEX_C(2), -1);
UT_PortablePosixIOTest_Set_FD(OSAL_INDEX_C(3), 0);
UT_PortablePosixIOTest_Set_Selectable(OSAL_INDEX_C(3), true);
OSAPI_TEST_FUNCTION_RC(OS_SelectMultiple_Impl, (&ReadSet, &WriteSet, 0), OS_SUCCESS);

memset(&ReadSet, 0, sizeof(ReadSet));
memset(&WriteSet, 0, sizeof(WriteSet));
ReadSet.object_ids[0] = 1;
UT_SetDefaultReturnValue(UT_KEY(OCS_select), 0);
UT_SetDeferredRetcode(UT_KEY(OCS_select), 1, 0);
OSAPI_TEST_FUNCTION_RC(OS_SelectMultiple_Impl, (&ReadSet, &WriteSet, 1), OS_ERROR_TIMEOUT);

/* Test where the FD set is empty */
Expand All @@ -146,6 +179,15 @@ void Test_OS_SelectMultiple_Impl(void)
memset(&WriteSet, 0xff, sizeof(WriteSet));
OSAPI_TEST_FUNCTION_RC(OS_SelectMultiple_Impl, (&ReadSet, &WriteSet, 0), OS_ERR_OPERATION_NOT_SUPPORTED);

/*
* Cover OS_FdSet_ConvertOut_Impl for id < OS_MAX_NUM_OPEN_FILES, requires no errors from in conversion
* NOTE - coverage only possible if OS_MAX_NUM_OPEN_FILES is not a multiple of 8 (exact fit)
*/
for (i = 1; i < OS_MAX_NUM_OPEN_FILES; i++)
{
UT_PortablePosixIOTest_Set_FD(OSAL_INDEX_C(i), -1);
}
OSAPI_TEST_FUNCTION_RC(OS_SelectMultiple_Impl, (&ReadSet, &WriteSet, 0), OS_SUCCESS);
} /* end OS_SelectMultiple_Impl */

/* ------------------- End of test cases --------------------------------------*/
Expand Down
9 changes: 9 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 @@ -58,6 +58,15 @@ void Test_OS_DirCreate_Impl(void)
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);

/* EEXIST but not a dir */
statbuf.st_mode = 0;
UT_SetDataBuffer(UT_KEY(OCS_stat), &statbuf, sizeof(statbuf), false);
OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_ERROR);

/* stat failure */
UT_SetDefaultReturnValue(UT_KEY(OCS_stat), -1);
OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_ERROR);
}

void Test_OS_DirOpen_Impl(void)
Expand Down
23 changes: 18 additions & 5 deletions src/unit-test-coverage/portable/src/coveragetest-posix-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ void Test_OS_FileStat_Impl(void)
UtAssert_True(OS_FILESTAT_ISDIR(FileStats), "Directory Bit set");
UtAssert_True(OS_FILESTAT_SIZE(FileStats) == 1234, "Size match");
UtAssert_True(OS_FILESTAT_TIME(FileStats) == 5678, "Time match (seconds)");

/* Repeat without matching uid/gid */
RefStat.st_uid = ~RefStat.st_uid;
RefStat.st_gid = ~RefStat.st_gid;
UT_SetDataBuffer(UT_KEY(OCS_stat), &RefStat, sizeof(RefStat), false);
OSAPI_TEST_FUNCTION_RC(OS_FileStat_Impl, ("local", &FileStats), OS_SUCCESS);
}

void Test_OS_FileChmod_Impl(void)
Expand All @@ -105,15 +111,18 @@ void Test_OS_FileChmod_Impl(void)
*/
struct OCS_stat RefStat;

/* failure mode 0 (open) */
UT_SetDefaultReturnValue(UT_KEY(OCS_open), -1);
/* Read only fail, write succeeds */
UT_SetDeferredRetcode(UT_KEY(OCS_open), 1, -1);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl, ("local", OS_READ_WRITE), OS_SUCCESS);

/* Both opens fail */
UT_SetDeferredRetcode(UT_KEY(OCS_open), 1, -1);
UT_SetDeferredRetcode(UT_KEY(OCS_open), 1, -1);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl, ("local", OS_READ_WRITE), OS_ERROR);
UT_ClearDefaultReturnValue(UT_KEY(OCS_open));

/* failure mode 1 (fstat) */
UT_SetDefaultReturnValue(UT_KEY(OCS_fstat), -1);
UT_SetDeferredRetcode(UT_KEY(OCS_fstat), 1, -1);
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl, ("local", OS_READ_WRITE), OS_ERROR);
UT_ClearDefaultReturnValue(UT_KEY(OCS_fstat));

/* failure mode 2 (fchmod) */
UT_SetDefaultReturnValue(UT_KEY(OCS_fchmod), -1);
Expand All @@ -122,6 +131,10 @@ void Test_OS_FileChmod_Impl(void)
/* non implemented error, e.g. such as DOS Filesystem with no perms */
OCS_errno = OCS_ENOTSUP;
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl, ("local", OS_READ_WRITE), OS_ERR_NOT_IMPLEMENTED);
OCS_errno = OCS_ENOSYS;
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl, ("local", OS_READ_WRITE), OS_ERR_NOT_IMPLEMENTED);
OCS_errno = OCS_EROFS;
OSAPI_TEST_FUNCTION_RC(OS_FileChmod_Impl, ("local", OS_READ_WRITE), OS_ERR_NOT_IMPLEMENTED);
UT_ClearDefaultReturnValue(UT_KEY(OCS_fchmod));

/* all permission bits with uid/gid match */
Expand Down
40 changes: 40 additions & 0 deletions src/unit-test-coverage/portable/src/coveragetest-posix-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@
#include "OCS_fcntl.h"
#include "OCS_errno.h"

/* OS_SelectSingle_Impl hook to clear SelectFlags */
static int32 UT_Hook_OS_SelectSingle_Impl(void *UserObj, int32 StubRetcode, uint32 CallCount,
const UT_StubContext_t *Context)
{
uint32 *SelectFlags;

SelectFlags = UT_Hook_GetArgValueByName(Context, "SelectFlags", uint32 *);

if (SelectFlags != NULL)
{
*SelectFlags = 0;
}

return 0;
}

void Test_OS_GenericClose_Impl(void)
{
/*
Expand Down Expand Up @@ -111,9 +127,21 @@ void Test_OS_GenericRead_Impl(void)
OSAPI_TEST_FUNCTION_RC(OS_GenericRead_Impl, (&token, DestData, sizeof(DestData), 0), sizeof(DestData));
UtAssert_True(UT_GetStubCount(UT_KEY(OS_SelectSingle_Impl)) == 1, "OS_SelectSingle() called");

/* Read 0 bytes */
OSAPI_TEST_FUNCTION_RC(OS_GenericRead_Impl, (&token, DestData, 0, 0), OS_SUCCESS);

/* read() failure */
UT_SetDefaultReturnValue(UT_KEY(OCS_read), -1);
OSAPI_TEST_FUNCTION_RC(OS_GenericRead_Impl, (&token, DestData, sizeof(DestData), 0), OS_ERROR);

/* Fail select */
UT_SetDeferredRetcode(UT_KEY(OS_SelectSingle_Impl), 1, OS_ERROR_TIMEOUT);
OSAPI_TEST_FUNCTION_RC(OS_GenericRead_Impl, (&token, DestData, sizeof(DestData), 0), OS_ERROR_TIMEOUT);

/* Not readable */
UT_SetHookFunction(UT_KEY(OS_SelectSingle_Impl), UT_Hook_OS_SelectSingle_Impl, NULL);
OSAPI_TEST_FUNCTION_RC(OS_GenericRead_Impl, (&token, DestData, sizeof(DestData), 0), OS_SUCCESS);
UT_SetHookFunction(UT_KEY(OS_SelectSingle_Impl), NULL, NULL);
}

void Test_OS_GenericWrite_Impl(void)
Expand All @@ -140,9 +168,21 @@ void Test_OS_GenericWrite_Impl(void)
OSAPI_TEST_FUNCTION_RC(OS_GenericWrite_Impl, (&token, SrcData, sizeof(SrcData), 0), sizeof(SrcData));
UtAssert_True(UT_GetStubCount(UT_KEY(OS_SelectSingle_Impl)) == 1, "OS_SelectSingle() called");

/* Fail select */
UT_SetDeferredRetcode(UT_KEY(OS_SelectSingle_Impl), 1, OS_ERROR_TIMEOUT);
OSAPI_TEST_FUNCTION_RC(OS_GenericWrite_Impl, (&token, SrcData, sizeof(SrcData), 0), OS_ERROR_TIMEOUT);

/* Write 0 bytes */
OSAPI_TEST_FUNCTION_RC(OS_GenericWrite_Impl, (&token, SrcData, 0, 0), OS_SUCCESS);

/* write() failure */
UT_SetDefaultReturnValue(UT_KEY(OCS_write), -1);
OSAPI_TEST_FUNCTION_RC(OS_GenericWrite_Impl, (&token, DestData, sizeof(DestData), 0), OS_ERROR);

/* Not writeable */
UT_SetHookFunction(UT_KEY(OS_SelectSingle_Impl), UT_Hook_OS_SelectSingle_Impl, NULL);
OSAPI_TEST_FUNCTION_RC(OS_GenericWrite_Impl, (&token, SrcData, sizeof(SrcData), 0), OS_SUCCESS);
UT_SetHookFunction(UT_KEY(OS_SelectSingle_Impl), NULL, NULL);
}

/* ------------------- End of test cases --------------------------------------*/
Expand Down
60 changes: 26 additions & 34 deletions src/unit-test-coverage/shared/src/coveragetest-binsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ void Test_OS_BinSemAPI_Init(void)
* Test Case For:
* int32 OS_BinSemAPI_Init(void)
*/
int32 expected = OS_SUCCESS;
int32 actual = OS_BinSemAPI_Init();

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

void Test_OS_BinSemCreate(void)
Expand All @@ -54,17 +51,18 @@ void Test_OS_BinSemCreate(void)
* int32 OS_BinSemCreate (uint32 *sem_id, const char *sem_name,
* uint32 sem_initial_value, uint32 options)
*/
int32 expected = OS_SUCCESS;
osal_id_t objid;
int32 actual = OS_BinSemCreate(&objid, "UT", 0, 0);

UtAssert_True(actual == expected, "OS_BinSemCreate() (%ld) == OS_SUCCESS", (long)actual);
OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(&objid, "UT", 0, 0), OS_SUCCESS);
OSAPI_TEST_OBJID(objid, !=, OS_OBJECT_ID_UNDEFINED);

OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(NULL, "UT", 0, 0), OS_INVALID_POINTER);
OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(&objid, NULL, 0, 0), OS_INVALID_POINTER);
UT_SetDefaultReturnValue(UT_KEY(OCS_memchr), OS_ERROR);
UT_SetDeferredRetcode(UT_KEY(OCS_memchr), 1, OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(&objid, "UT", 0, 0), OS_ERR_NAME_TOO_LONG);

UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdAllocateNew), OS_ERR_NO_FREE_IDS);
OSAPI_TEST_FUNCTION_RC(OS_BinSemCreate(&objid, "UT", 0, 0), OS_ERR_NO_FREE_IDS);
}

void Test_OS_BinSemDelete(void)
Expand All @@ -73,12 +71,10 @@ void Test_OS_BinSemDelete(void)
* Test Case For:
* int32 OS_BinSemDelete (uint32 sem_id)
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
OSAPI_TEST_FUNCTION_RC(OS_BinSemDelete(UT_OBJID_1), OS_SUCCESS);

actual = OS_BinSemDelete(UT_OBJID_1);

UtAssert_True(actual == expected, "OS_BinSemDelete() (%ld) == OS_SUCCESS", (long)actual);
UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_BinSemDelete(UT_OBJID_1), OS_ERR_INVALID_ID);
}

void Test_OS_BinSemGive(void)
Expand All @@ -87,12 +83,10 @@ void Test_OS_BinSemGive(void)
* Test Case For:
* int32 OS_BinSemGive ( uint32 sem_id )
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;

actual = OS_BinSemGive(UT_OBJID_1);
OSAPI_TEST_FUNCTION_RC(OS_BinSemGive(UT_OBJID_1), OS_SUCCESS);

UtAssert_True(actual == expected, "OS_BinSemGive() (%ld) == OS_SUCCESS", (long)actual);
UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_BinSemGive(UT_OBJID_1), OS_ERR_INVALID_ID);
}

void Test_OS_BinSemTake(void)
Expand All @@ -101,12 +95,10 @@ void Test_OS_BinSemTake(void)
* Test Case For:
* int32 OS_BinSemTake ( uint32 sem_id )
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
OSAPI_TEST_FUNCTION_RC(OS_BinSemTake(UT_OBJID_1), OS_SUCCESS);

actual = OS_BinSemTake(UT_OBJID_1);

UtAssert_True(actual == expected, "OS_BinSemTake() (%ld) == OS_SUCCESS", (long)actual);
UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_BinSemTake(UT_OBJID_1), OS_ERR_INVALID_ID);
}

void Test_OS_BinSemFlush(void)
Expand All @@ -115,12 +107,10 @@ void Test_OS_BinSemFlush(void)
* Test Case For:
* int32 OS_BinSemFlush (uint32 sem_id)
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;

actual = OS_BinSemFlush(UT_OBJID_1);
OSAPI_TEST_FUNCTION_RC(OS_BinSemFlush(UT_OBJID_1), OS_SUCCESS);

UtAssert_True(actual == expected, "OS_BinSemFlush() (%ld) == OS_SUCCESS", (long)actual);
UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_BinSemFlush(UT_OBJID_1), OS_ERR_INVALID_ID);
}

void Test_OS_BinSemTimedWait(void)
Expand All @@ -129,12 +119,10 @@ void Test_OS_BinSemTimedWait(void)
* Test Case For:
* int32 OS_BinSemTimedWait ( uint32 sem_id, uint32 msecs )
*/
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;
OSAPI_TEST_FUNCTION_RC(OS_BinSemTimedWait(UT_OBJID_1, 1), OS_SUCCESS);

actual = OS_BinSemTimedWait(UT_OBJID_1, 1);

UtAssert_True(actual == expected, "OS_BinSemTimedWait() (%ld) == OS_SUCCESS", (long)actual);
UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_BinSemTimedWait(UT_OBJID_1, 1), OS_ERR_INVALID_ID);
}

void Test_OS_BinSemGetIdByName(void)
Expand All @@ -157,7 +145,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);
OSAPI_TEST_FUNCTION_RC(OS_BinSemGetIdByName(NULL, "UT"), OS_INVALID_POINTER);
OSAPI_TEST_FUNCTION_RC(OS_BinSemGetIdByName(&objid, NULL), OS_INVALID_POINTER);
}

void Test_OS_BinSemGetInfo(void)
Expand All @@ -179,6 +168,9 @@ void Test_OS_BinSemGetInfo(void)
UtAssert_True(strcmp(prop.name, "ABC") == 0, "prop.name (%s) == ABC", prop.name);

OSAPI_TEST_FUNCTION_RC(OS_BinSemGetInfo(UT_OBJID_1, NULL), OS_INVALID_POINTER);

UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_BinSemGetInfo(UT_OBJID_1, &prop), OS_ERR_INVALID_ID);
}

/* Osapi_Test_Setup
Expand Down
Loading