Skip to content

Commit

Permalink
Fix #1465, add default handler for OS_strnlen
Browse files Browse the repository at this point in the history
Calls to "OS_strnlen" are likely needed to return an actual length,
so make a default handler that does return a length.  The value
may still be overridden in tests, though.
  • Loading branch information
jphickey committed Jun 24, 2024
1 parent 3b40ede commit 2a0a92d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 60 deletions.
1 change: 0 additions & 1 deletion src/unit-test-coverage/shared/src/coveragetest-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ void Test_OS_strnlen(void)

UtAssert_INT32_EQ(result, sizeof(str));


/* Test case where null character is found */
str[OS_MAX_FILE_NAME - 1] = '\0';

Expand Down
63 changes: 6 additions & 57 deletions src/unit-test-coverage/shared/src/coveragetest-filesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ void Test_OS_TranslatePath(void)
* int32 OS_TranslatePath(const char *VirtualPath, char *LocalPath)
*/
char LocalBuffer[OS_MAX_PATH_LEN];
char DoubleBuffer[2 * OS_MAX_PATH_LEN];
int32 expected = OS_SUCCESS;
int32 actual = ~OS_SUCCESS;

Expand All @@ -422,98 +421,55 @@ void Test_OS_TranslatePath(void)
strcpy(OS_filesys_table[1].virtual_mountpt, "/cf");
strcpy(OS_filesys_table[1].system_mountpt, "/mnt/cf");

UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath(/cf/test) (%ld) == OS_SUCCESS", (long)actual);
UtAssert_True(strcmp(LocalBuffer, "/mnt/cf/test") == 0, "OS_TranslatePath(/cf/test) (%s) == /mnt/cf/test",
LocalBuffer);

/* VirtPathLen >= OS_MAX_PATH_LEN */
memset(DoubleBuffer, 0xFF, sizeof(DoubleBuffer) - 1);
DoubleBuffer[sizeof(DoubleBuffer) - 1] = '\0';
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(DoubleBuffer));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
OSAPI_TEST_FUNCTION_RC(OS_TranslatePath(DoubleBuffer, LocalBuffer), OS_FS_ERR_PATH_TOO_LONG);

/* Check various error paths */
UtAssert_INT32_EQ(OS_TranslatePath("/cf/test", NULL), OS_INVALID_POINTER);
UtAssert_INT32_EQ(OS_TranslatePath(NULL, LocalBuffer), OS_INVALID_POINTER);

UT_SetDefaultReturnValue(UT_KEY(OCS_memchr), OS_ERROR);
expected = OS_FS_ERR_PATH_TOO_LONG;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 1, OS_MAX_PATH_LEN + 1);
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_TOO_LONG", (long)actual);
UT_ResetState(UT_KEY(OS_strnlen));

/* Invalid no '/' */
expected = OS_FS_ERR_PATH_INVALID;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("invalid/"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("invalid", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);

UT_SetDeferredRetcode(UT_KEY(OCS_memchr), 2, OS_ERROR);
UT_SetDeferredRetcode(UT_KEY(OCS_memchr), 1, OS_ERROR);
expected = OS_FS_ERR_NAME_TOO_LONG;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath(/cf/test) (%ld) == OS_FS_ERR_NAME_TOO_LONG", (long)actual);

/* Invalid no leading '/' */
expected = OS_FS_ERR_PATH_INVALID;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("invalid/"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("invalid/", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);

UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetBySearch), OS_ERR_NAME_NOT_FOUND);
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath() (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);
UT_ClearDefaultReturnValue(UT_KEY(OS_ObjectIdGetBySearch));

/* VirtPathLen < VirtPathBegin */
UT_SetDeferredRetcode(UT_KEY(OCS_memchr), 4, OS_ERROR);
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 1, 1);
expected = OS_FS_ERR_PATH_INVALID;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath(/cf/test) (%ld) == OS_FS_ERR_PATH_INVALID", (long)actual);

/* (SysMountPointLen + VirtPathLen) > OS_MAX_LOCAL_PATH_LEN */
UT_SetDeferredRetcode(UT_KEY(OCS_memchr), 3, OS_ERROR);
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, OS_MAX_LOCAL_PATH_LEN + 1);
expected = OS_FS_ERR_PATH_TOO_LONG;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath(/cf/test) (%ld) == OS_FS_ERR_PATH_TOO_LONG", (long)actual);

OS_filesys_table[1].flags = 0;
expected = OS_ERR_INCORRECT_OBJ_STATE;
UT_ResetState(UT_KEY(OS_strnlen));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen("/cf/test"));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(OS_filesys_table[1].system_mountpt));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 3, strlen(OS_filesys_table[1].virtual_mountpt));
actual = OS_TranslatePath("/cf/test", LocalBuffer);
UtAssert_True(actual == expected, "OS_TranslatePath(/cf/test) (%ld) == OS_ERR_INCORRECT_OBJ_STATE", (long)actual);
}
Expand Down Expand Up @@ -541,46 +497,39 @@ void Test_OS_FileSys_FindVirtMountPoint(void)
OS_filesys_table[1].flags = 0;
OS_filesys_table[1].virtual_mountpt[0] = 0;

UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr, &token, &refobj);
UtAssert_True(!result, "OS_FileSys_FindVirtMountPoint(%s) (unmounted) == false", refstr);

OS_filesys_table[1].flags = OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL;

/* Branch coverage for mismatches */
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr, &token, &refobj);
UtAssert_True(!result, "OS_FileSys_FindVirtMountPoint(%s) (mountpt=%s) == false", refstr,
OS_filesys_table[1].virtual_mountpt);

memset(OS_filesys_table[1].virtual_mountpt, 'a', sizeof(OS_filesys_table[1].virtual_mountpt));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr, &token, &refobj);
UtAssert_True(!result, "OS_FileSys_FindVirtMountPoint(%s) (mountpt=%s) == false", refstr,
OS_filesys_table[1].virtual_mountpt);

/* Verify cases where one is a substring of the other -
* these should also return false */
strncpy(OS_filesys_table[1].virtual_mountpt, "/ut11", sizeof(OS_filesys_table[1].virtual_mountpt));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr, &token, &refobj);
UtAssert_True(!result, "OS_FileSys_FindVirtMountPoint(%s) (mountpt=%s) == false", refstr,
OS_filesys_table[1].virtual_mountpt);

strncpy(OS_filesys_table[1].virtual_mountpt, "/u", sizeof(OS_filesys_table[1].virtual_mountpt));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr, &token, &refobj);
UtAssert_True(!result, "OS_FileSys_FindVirtMountPoint(%s) (mountpt=%s) == false", refstr,
OS_filesys_table[1].virtual_mountpt);

strncpy(OS_filesys_table[1].virtual_mountpt, "/ut", sizeof(OS_filesys_table[1].virtual_mountpt));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr, &token, &refobj);
UtAssert_True(result, "OS_FileSys_FindVirtMountPoint(%s) (nominal) == true", refstr);

/* Passing case with reference ending in "/" */
strncpy(OS_filesys_table[1].virtual_mountpt, "/ut", sizeof(OS_filesys_table[1].virtual_mountpt));
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(OS_filesys_table[1].virtual_mountpt));
result = OS_FileSys_FindVirtMountPoint((void *)refstr1, &token, &refobj);
UtAssert_True(result, "OS_FileSys_FindVirtMountPoint(%s) (nominal) == true", refstr);
}
Expand Down
1 change: 0 additions & 1 deletion src/unit-test-coverage/shared/src/coveragetest-idmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,6 @@ void Test_OS_GetResourceName(void)
OSAPI_TEST_FUNCTION_RC(OS_GetResourceName(token.obj_id, NameBuffer, sizeof(NameBuffer)), OS_SUCCESS);
UtAssert_True(strcmp(NameBuffer, "UTTask") == 0, "NameBuffer (%s) == UTTask", NameBuffer);

UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(rptr->name_entry));
OSAPI_TEST_FUNCTION_RC(OS_GetResourceName(token.obj_id, NameBuffer, OSAL_SIZE_C(2)), OS_ERR_NAME_TOO_LONG);

/* Null entry */
Expand Down
1 change: 1 addition & 0 deletions src/ut-stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ add_library(ut_osapi_stubs STATIC
osapi-clock-stubs.c
osapi-clock-handlers.c
osapi-common-stubs.c
osapi-common-handlers.c
osapi-condvar-stubs.c
osapi-countsem-stubs.c
osapi-countsem-handlers.c
Expand Down
72 changes: 72 additions & 0 deletions src/ut-stubs/osapi-common-handlers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/************************************************************************
* NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes”
*
* Copyright (c) 2020 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
************************************************************************/

/**
* \file
*
*
* Stub implementations for the functions defined in the OSAL API
*
* The stub implementation can be used for unit testing applications built
* on top of OSAL. The stubs do not do any real function, but allow
* the return code to be crafted such that error paths in the application
* can be executed.
*/

#include "osapi-common.h" /* OSAL public API for this subsystem */
#include "utstub-helpers.h"

/*
* -----------------------------------------------------------------
* Default handler implementation for 'OS_strnlen' stub
* -----------------------------------------------------------------
*/
void UT_DefaultHandler_OS_strnlen(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)

Check notice

Code scanning / CodeQL

Long function without assertion Note

All functions of more than 10 lines should have at least one assertion.
{
const char *s;
const char *end;
size_t maxlen;
size_t retval;
int32 status;

if (UT_Stub_GetInt32StatusCode(Context, &status))

Check warning

Code scanning / CodeQL

Side effect in a Boolean expression Warning

This Boolean expression is not side-effect free.
{
/* If a retval was supplied in the test case, then use it */
retval = status;
}
else
{
s = UT_Hook_GetArgValueByName(Context, "s", const char *);
maxlen = UT_Hook_GetArgValueByName(Context, "maxlen", size_t);

/* This is basically the real impl of strnlen, as it
* usually needs to give back the appropriate value in
* order to follow the expected path */
end = memchr(s, 0, maxlen);
if (end == NULL)
{
retval = maxlen;
}
else
{
retval = end - s;
}
}

UT_Stub_SetReturnValue(FuncKey, retval);
}
4 changes: 3 additions & 1 deletion src/ut-stubs/osapi-common-stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "osapi-common.h"
#include "utgenstub.h"

void UT_DefaultHandler_OS_strnlen(void *, UT_EntryKey_t, const UT_StubContext_t *);

/*
* ----------------------------------------------------
* Generated stub function for OS_API_Init()
Expand Down Expand Up @@ -124,7 +126,7 @@ size_t OS_strnlen(const char *s, size_t maxlen)
UT_GenStub_AddParam(OS_strnlen, const char *, s);
UT_GenStub_AddParam(OS_strnlen, size_t, maxlen);

UT_GenStub_Execute(OS_strnlen, Basic, NULL);
UT_GenStub_Execute(OS_strnlen, Basic, UT_DefaultHandler_OS_strnlen);

return UT_GenStub_GetReturnValue(OS_strnlen, size_t);
}

0 comments on commit 2a0a92d

Please sign in to comment.