Skip to content

Commit

Permalink
Fix #832, replace public API stubs with generated versions
Browse files Browse the repository at this point in the history
The "built-in" logic from existing stubs is converted to a
handler function and moved to a different source file.  The
generated stub references this function if it exists and
uses it as a default handler, so the default behavior is not
changed.

In this pattern the stubs in this directory are strictly
limited to the same public APIs that are defined in header
files under src/os/inc.  This has the side effect of removing
some internal stubs required for coverage test.  Those stubs
will be reinstated in the coverage specific stubs where other
internal functions are.
  • Loading branch information
jphickey committed Apr 16, 2021
1 parent cb374cd commit 591f3b9
Show file tree
Hide file tree
Showing 58 changed files with 5,092 additions and 4,448 deletions.
100 changes: 79 additions & 21 deletions src/ut-stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,91 @@
# provide "stub" versions of all calls in the OSAL public API.
#

set(OSAL_PUBLIC_API_HEADERS
${OSAL_SOURCE_DIR}/src/os/inc/osapi-binsem.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-bsp.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-clock.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-common.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-constants.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-countsem.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-dir.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-error.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-file.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-filesys.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-heap.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-idmap.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-macros.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-module.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-mutex.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-network.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-printf.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-queue.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-select.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-shell.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-sockets.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-task.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-timebase.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-timer.h
${OSAL_SOURCE_DIR}/src/os/inc/osapi-version.h
)

# The following target rule contains the specific commands required
# to auto-generate the stub implementations from the headers
add_custom_target(generate_osapi_stubs
COMMAND ${UT_ASSERT_SOURCE_DIR}/scripts/generate_stubs.pl
${CMAKE_CURRENT_SOURCE_DIR}
${OSAL_PUBLIC_API_HEADERS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)


# NOTE: There is no separate public include directory for the stubs.
# By definition, the stubs must implement the same public API that the
# normal OSAL library implements. Therefore, only the standard OSAL
# header files are used.
add_library(ut_osapi_stubs STATIC
utstub-helpers.c
osapi-utstub-binsem.c
osapi-utstub-clock.c
osapi-utstub-common.c
osapi-utstub-countsem.c
osapi-utstub-dir.c
osapi-utstub-errors.c
osapi-utstub-file.c
osapi-utstub-filesys.c
osapi-utstub-heap.c
osapi-utstub-idmap.c
osapi-utstub-module.c
osapi-utstub-mutex.c
osapi-utstub-network.c
osapi-utstub-printf.c
osapi-utstub-queue.c
osapi-utstub-select.c
osapi-utstub-sockets.c
osapi-utstub-task.c
osapi-utstub-time.c
osapi-utstub-timebase.c
osapi-utstub-version.c
osapi-binsem-stubs.c
osapi-binsem-hooks.c
osapi-clock-stubs.c
osapi-clock-hooks.c
osapi-common-stubs.c
osapi-countsem-stubs.c
osapi-countsem-hooks.c
osapi-dir-stubs.c
osapi-dir-hooks.c
osapi-error-stubs.c
osapi-error-hooks.c
osapi-file-stubs.c
osapi-file-hooks.c
osapi-filesys-stubs.c
osapi-filesys-hooks.c
osapi-heap-stubs.c
osapi-heap-hooks.c
osapi-idmap-stubs.c
osapi-idmap-hooks.c
osapi-module-stubs.c
osapi-module-hooks.c
osapi-mutex-stubs.c
osapi-mutex-hooks.c
osapi-network-stubs.c
osapi-network-hooks.c
osapi-printf-stubs.c
osapi-printf-hooks.c
osapi-queue-stubs.c
osapi-queue-hooks.c
osapi-select-stubs.c
osapi-sockets-stubs.c
osapi-sockets-hooks.c
osapi-task-stubs.c
osapi-task-hooks.c
osapi-timer-stubs.c
osapi-timer-hooks.c
osapi-timebase-stubs.c
osapi-timebase-hooks.c
osapi-version-stubs.c
osapi-version-hooks.c
)

# Some of the internal API definitions in stubs are based on
Expand Down
114 changes: 114 additions & 0 deletions src/ut-stubs/osapi-binsem-hooks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 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-binsem.h" /* OSAL public API for this subsystem */
#include "utstub-helpers.h"

/*
* -----------------------------------------------------------------
* Default handler implementation for 'OS_BinSemCreate' stub
* -----------------------------------------------------------------
*/
void UT_DefaultHandler_OS_BinSemCreate(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
osal_id_t *sem_id = UT_Hook_GetArgValueByName(Context, "sem_id", osal_id_t *);
int32 status;

UT_Stub_GetInt32StatusCode(Context, &status);

if (status == OS_SUCCESS)
{
*sem_id = UT_AllocStubObjId(OS_OBJECT_TYPE_OS_BINSEM);
}
else
{
*sem_id = UT_STUB_FAKE_OBJECT_ID;
}
}

/*
* -----------------------------------------------------------------
* Default handler implementation for 'OS_BinSemGetInfo' stub
* -----------------------------------------------------------------
*/
void UT_DefaultHandler_OS_BinSemGetInfo(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
OS_bin_sem_prop_t *bin_prop = UT_Hook_GetArgValueByName(Context, "bin_prop", OS_bin_sem_prop_t *);
int32 status;

UT_Stub_GetInt32StatusCode(Context, &status);

if (status == OS_SUCCESS &&
UT_Stub_CopyToLocal(UT_KEY(OS_BinSemGetInfo), bin_prop, sizeof(*bin_prop)) < sizeof(*bin_prop))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_TASK, &bin_prop->creator);
strncpy(bin_prop->name, "Name", sizeof(bin_prop->name) - 1);
bin_prop->name[sizeof(bin_prop->name) - 1] = '\0';
}
}

/*
* -----------------------------------------------------------------
* Default handler implementation for 'OS_BinSemDelete' stub
* -----------------------------------------------------------------
*/
void UT_DefaultHandler_OS_BinSemDelete(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
osal_id_t sem_id = UT_Hook_GetArgValueByName(Context, "sem_id", osal_id_t);
int32 status;

UT_Stub_GetInt32StatusCode(Context, &status);

if (status == OS_SUCCESS)
{
UT_DeleteStubObjId(OS_OBJECT_TYPE_OS_BINSEM, sem_id);
}
}

/*
* -----------------------------------------------------------------
* Default handler implementation for 'OS_BinSemGetIdByName' stub
* -----------------------------------------------------------------
*/
void UT_DefaultHandler_OS_BinSemGetIdByName(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
osal_id_t *sem_id = UT_Hook_GetArgValueByName(Context, "sem_id", osal_id_t *);
int32 status;

UT_Stub_GetInt32StatusCode(Context, &status);

if (status == OS_SUCCESS &&
UT_Stub_CopyToLocal(UT_KEY(OS_BinSemGetIdByName), sem_id, sizeof(*sem_id)) < sizeof(*sem_id))
{
UT_ObjIdCompose(1, OS_OBJECT_TYPE_OS_BINSEM, sem_id);
}
}
167 changes: 167 additions & 0 deletions src/ut-stubs/osapi-binsem-stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 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
*
* Auto-Generated stub implementations for functions defined in osapi-binsem header
*/

#include "osapi-binsem.h"
#include "utgenstub.h"

extern void UT_DefaultHandler_OS_BinSemCreate(void *, UT_EntryKey_t, const UT_StubContext_t *);
extern void UT_DefaultHandler_OS_BinSemDelete(void *, UT_EntryKey_t, const UT_StubContext_t *);
extern void UT_DefaultHandler_OS_BinSemGetIdByName(void *, UT_EntryKey_t, const UT_StubContext_t *);
extern void UT_DefaultHandler_OS_BinSemGetInfo(void *, UT_EntryKey_t, const UT_StubContext_t *);

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemCreate()
* ----------------------------------------------------
*/
int32 OS_BinSemCreate(osal_id_t *sem_id, const char *sem_name, uint32 sem_initial_value, uint32 options)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemCreate, int32);

UT_GenStub_AddParam(OS_BinSemCreate, osal_id_t *, sem_id);
UT_GenStub_AddParam(OS_BinSemCreate, const char *, sem_name);
UT_GenStub_AddParam(OS_BinSemCreate, uint32, sem_initial_value);
UT_GenStub_AddParam(OS_BinSemCreate, uint32, options);

UT_GenStub_Execute(OS_BinSemCreate, Basic, UT_DefaultHandler_OS_BinSemCreate);

return UT_GenStub_GetReturnValue(OS_BinSemCreate, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemDelete()
* ----------------------------------------------------
*/
int32 OS_BinSemDelete(osal_id_t sem_id)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemDelete, int32);

UT_GenStub_AddParam(OS_BinSemDelete, osal_id_t, sem_id);

UT_GenStub_Execute(OS_BinSemDelete, Basic, UT_DefaultHandler_OS_BinSemDelete);

return UT_GenStub_GetReturnValue(OS_BinSemDelete, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemFlush()
* ----------------------------------------------------
*/
int32 OS_BinSemFlush(osal_id_t sem_id)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemFlush, int32);

UT_GenStub_AddParam(OS_BinSemFlush, osal_id_t, sem_id);

UT_GenStub_Execute(OS_BinSemFlush, Basic, NULL);

return UT_GenStub_GetReturnValue(OS_BinSemFlush, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemGetIdByName()
* ----------------------------------------------------
*/
int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemGetIdByName, int32);

UT_GenStub_AddParam(OS_BinSemGetIdByName, osal_id_t *, sem_id);
UT_GenStub_AddParam(OS_BinSemGetIdByName, const char *, sem_name);

UT_GenStub_Execute(OS_BinSemGetIdByName, Basic, UT_DefaultHandler_OS_BinSemGetIdByName);

return UT_GenStub_GetReturnValue(OS_BinSemGetIdByName, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemGetInfo()
* ----------------------------------------------------
*/
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemGetInfo, int32);

UT_GenStub_AddParam(OS_BinSemGetInfo, osal_id_t, sem_id);
UT_GenStub_AddParam(OS_BinSemGetInfo, OS_bin_sem_prop_t *, bin_prop);

UT_GenStub_Execute(OS_BinSemGetInfo, Basic, UT_DefaultHandler_OS_BinSemGetInfo);

return UT_GenStub_GetReturnValue(OS_BinSemGetInfo, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemGive()
* ----------------------------------------------------
*/
int32 OS_BinSemGive(osal_id_t sem_id)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemGive, int32);

UT_GenStub_AddParam(OS_BinSemGive, osal_id_t, sem_id);

UT_GenStub_Execute(OS_BinSemGive, Basic, NULL);

return UT_GenStub_GetReturnValue(OS_BinSemGive, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemTake()
* ----------------------------------------------------
*/
int32 OS_BinSemTake(osal_id_t sem_id)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemTake, int32);

UT_GenStub_AddParam(OS_BinSemTake, osal_id_t, sem_id);

UT_GenStub_Execute(OS_BinSemTake, Basic, NULL);

return UT_GenStub_GetReturnValue(OS_BinSemTake, int32);
}

/*
* ----------------------------------------------------
* Generated stub function for OS_BinSemTimedWait()
* ----------------------------------------------------
*/
int32 OS_BinSemTimedWait(osal_id_t sem_id, uint32 msecs)
{
UT_GenStub_SetupReturnBuffer(OS_BinSemTimedWait, int32);

UT_GenStub_AddParam(OS_BinSemTimedWait, osal_id_t, sem_id);
UT_GenStub_AddParam(OS_BinSemTimedWait, uint32, msecs);

UT_GenStub_Execute(OS_BinSemTimedWait, Basic, NULL);

return UT_GenStub_GetReturnValue(OS_BinSemTimedWait, int32);
}
Loading

0 comments on commit 591f3b9

Please sign in to comment.