Skip to content

Commit

Permalink
Fix #435, convert internal PSP UT stubs into handler pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jphickey committed Aug 14, 2024
1 parent 6c99a9d commit 257eafa
Show file tree
Hide file tree
Showing 27 changed files with 650 additions and 1,008 deletions.
29 changes: 12 additions & 17 deletions unit-test-coverage/ut-stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,10 @@
# the OCS functions that are actually used.
#
add_library(ut_psp_libc_stubs STATIC EXCLUDE_FROM_ALL
src/libc-stdio-stubs.c
src/libc-stdlib-stubs.c
src/libc-string-stubs.c
src/vxworks-ataDrv-stubs.c
src/vxworks-cacheLib-stubs.c
src/vxworks-moduleLib-stubs.c
src/vxworks-taskLib-stubs.c
src/vxworks-excLib-stubs.c
src/vxworks-fppLib-stubs.c
src/vxworks-mcpx750-stubs.c
src/vxworks-rebootLib-stubs.c
src/vxworks-sysLib-stubs.c
src/vxworks-vxLib-stubs.c
src/vxworks-spyLib-stubs.c
src/vxworks-spyLibP-stubs.c
src/rtems-bsdnet-stubs.c
src/cfe-configdata-stubs.c
src/PCS_arch_ppc_vxPpcLib_handlers.c
src/PCS_arch_ppc_vxPpcLib_stubs.c
src/PCS_bsdnet_handlers.c
src/PCS_bsdnet_stubs.c
src/PCS_cacheLib_stubs.c
src/PCS_cfe_configdata_stubs.c
Expand All @@ -61,20 +48,28 @@ add_library(ut_psp_libc_stubs STATIC EXCLUDE_FROM_ALL
src/PCS_errnoLib_stubs.c
src/PCS_excLib_stubs.c
src/PCS_fppLib_stubs.c
src/PCS_moduleLib_handlers.c
src/PCS_moduleLib_stubs.c
src/PCS_ramDrv_stubs.c
src/PCS_rebootLib_stubs.c
src/PCS_spyLibP_stubs.c
src/PCS_spyLib_stubs.c
src/PCS_stdio_handlers.c
src/PCS_stdio_stubs.c
src/PCS_stdlib_handlers.c
src/PCS_stdlib_stubs.c
src/PCS_string_handlers.c
src/PCS_string_stubs.c
src/PCS_sysLib_handlers.c
src/PCS_sysLib_stubs.c
src/PCS_sys_mman_stubs.c
src/PCS_sys_stat_stubs.c
src/PCS_taskLib_handlers.c
src/PCS_taskLib_stubs.c
src/PCS_unistd_stubs.c
src/PCS_xbdBlkDev_stubs.c)
src/PCS_xbdBlkDev_stubs.c
src/vxworks-mcpx750-stubs.c
)

target_include_directories(ut_libc_stubs PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
* limitations under the License.
************************************************************************/

/* PSP coverage stub replacement for ataDrv.h */
/* PSP coverage stub replacement for vxLib.h */
#include <string.h>
#include <stdlib.h>
#include "utstubs.h"

#include "PCS_drv_hdisk_ataDrv.h"
#include "PCS_vxLib.h"
#include "PCS_arch_ppc_vxPpcLib.h"

PCS_device_t PCS_ataXbdDevCreate(int ctrl, int drive, unsigned int nBlks, unsigned int offset, const char *name)
void UT_DefaultHandler_PCS_vxTimeBaseGet(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
return UT_DEFAULT_IMPL(PCS_ataXbdDevCreate);
/* void PCS_vxTimeBaseGet(uint32_t *u, uint32_t *l) */
uint32_t *u = UT_Hook_GetArgValueByName(Context, "u", uint32 *);
uint32_t *l = UT_Hook_GetArgValueByName(Context, "l", uint32 *);

if (!UT_Stub_GetInt32StatusCode(Context, NULL))
{
*u = 0;
*l = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "PCS_arch_ppc_vxPpcLib.h"
#include "utgenstub.h"

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

/*
* ----------------------------------------------------
* Generated stub function for PCS_vxDecGet()
Expand Down Expand Up @@ -101,5 +103,5 @@ void PCS_vxTimeBaseGet(uint32_t *u, uint32_t *l)
UT_GenStub_AddParam(PCS_vxTimeBaseGet, uint32_t *, u);
UT_GenStub_AddParam(PCS_vxTimeBaseGet, uint32_t *, l);

UT_GenStub_Execute(PCS_vxTimeBaseGet, Basic, NULL);
UT_GenStub_Execute(PCS_vxTimeBaseGet, Basic, UT_DefaultHandler_PCS_vxTimeBaseGet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,17 @@
#include "PCS_bsdnet.h"
#include "PCS_rtems.h"

void PCS_rtems_bsdnet_do_dhcp_failsafe(void)
{
UT_DEFAULT_IMPL(PCS_rtems_bsdnet_do_dhcp_failsafe);
}

const char *PCS_rtems_status_text(PCS_rtems_status_code code)
void UT_DefaultHandler_PCS_rtems_status_text(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
/* const char *PCS_rtems_status_text(PCS_rtems_status_code code) */
const char *ReturnCode = NULL;
int32 Status;

Status = UT_DEFAULT_IMPL(PCS_rtems_status_text);
if (Status == 0)
UT_Stub_GetInt32StatusCode(Context, &Status);

if (Status >= 0 &&
UT_Stub_CopyToLocal(UT_KEY(PCS_rtems_status_text), &ReturnCode, sizeof(ReturnCode)) == sizeof(ReturnCode))
{
UT_Stub_CopyToLocal(UT_KEY(PCS_rtems_status_text), &ReturnCode, sizeof(ReturnCode));
UT_Stub_SetReturnValue(FuncKey, ReturnCode);
}

return ReturnCode;
}

int PCS_rtems_bsdnet_initialize_network(void)
{
return UT_DEFAULT_IMPL(PCS_rtems_bsdnet_initialize_network);
}

int PCS_rtems_fxp_attach(struct PCS_rtems_bsdnet_ifconfig *config, int attaching)
{
return UT_DEFAULT_IMPL(PCS_rtems_fxp_attach);
}
4 changes: 3 additions & 1 deletion unit-test-coverage/ut-stubs/src/PCS_bsdnet_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "PCS_bsdnet.h"
#include "utgenstub.h"

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

/*
* ----------------------------------------------------
* Generated stub function for PCS_rtems_bsdnet_do_dhcp_failsafe()
Expand Down Expand Up @@ -78,7 +80,7 @@ const char *PCS_rtems_status_text(PCS_rtems_status_code sc)

UT_GenStub_AddParam(PCS_rtems_status_text, PCS_rtems_status_code, sc);

UT_GenStub_Execute(PCS_rtems_status_text, Basic, NULL);
UT_GenStub_Execute(PCS_rtems_status_text, Basic, UT_DefaultHandler_PCS_rtems_status_text);

return UT_GenStub_GetReturnValue(PCS_rtems_status_text, const char *);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,34 @@

#include "PCS_moduleLib.h"

PCS_MODULE_ID PCS_moduleFindByName(const char *moduleName)
void UT_DefaultHandler_PCS_moduleFindByName(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
/* PCS_MODULE_ID PCS_moduleFindByName(const char *moduleName) */
PCS_MODULE_ID retval;
int32 Status;

retval = NULL;
Status = UT_DEFAULT_IMPL(PCS_moduleFindByName);
if (Status == 0)

UT_Stub_GetInt32StatusCode(Context, &Status);

if (Status >= 0 && UT_Stub_CopyToLocal(FuncKey, &retval, sizeof(retval)) == sizeof(retval))
{
UT_Stub_CopyToLocal(UT_KEY(PCS_moduleFindByName), &retval, sizeof(retval));
UT_Stub_SetReturnValue(FuncKey, retval);
}

return retval;
}

PCS_STATUS PCS_moduleInfoGet(PCS_MODULE_ID moduleId, PCS_MODULE_INFO *pModuleInfo)
void UT_DefaultHandler_PCS_moduleInfoGet(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
/* PCS_STATUS PCS_moduleInfoGet(PCS_MODULE_ID moduleId, PCS_MODULE_INFO *pModuleInfo) */
PCS_MODULE_INFO *pModuleInfo = UT_Hook_GetArgValueByName(Context, "pModuleInfo", PCS_MODULE_INFO *);

int32 Status;

Status = UT_DEFAULT_IMPL(PCS_moduleInfoGet);
UT_Stub_GetInt32StatusCode(Context, &Status);

if (Status == 0 &&
if (Status >= 0 &&
UT_Stub_CopyToLocal(UT_KEY(PCS_moduleInfoGet), pModuleInfo, sizeof(*pModuleInfo)) < sizeof(*pModuleInfo))
{
memset(pModuleInfo, 0, sizeof(*pModuleInfo));
}

return Status;
}
7 changes: 5 additions & 2 deletions unit-test-coverage/ut-stubs/src/PCS_moduleLib_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include "PCS_moduleLib.h"
#include "utgenstub.h"

void UT_DefaultHandler_PCS_moduleFindByName(void *, UT_EntryKey_t, const UT_StubContext_t *);
void UT_DefaultHandler_PCS_moduleInfoGet(void *, UT_EntryKey_t, const UT_StubContext_t *);

/*
* ----------------------------------------------------
* Generated stub function for PCS_moduleFindByName()
Expand All @@ -36,7 +39,7 @@ PCS_MODULE_ID PCS_moduleFindByName(const char *moduleName)

UT_GenStub_AddParam(PCS_moduleFindByName, const char *, moduleName);

UT_GenStub_Execute(PCS_moduleFindByName, Basic, NULL);
UT_GenStub_Execute(PCS_moduleFindByName, Basic, UT_DefaultHandler_PCS_moduleFindByName);

return UT_GenStub_GetReturnValue(PCS_moduleFindByName, PCS_MODULE_ID);
}
Expand All @@ -53,7 +56,7 @@ PCS_STATUS PCS_moduleInfoGet(PCS_MODULE_ID moduleId, PCS_MODULE_INFO *pModuleInf
UT_GenStub_AddParam(PCS_moduleInfoGet, PCS_MODULE_ID, moduleId);
UT_GenStub_AddParam(PCS_moduleInfoGet, PCS_MODULE_INFO *, pModuleInfo);

UT_GenStub_Execute(PCS_moduleInfoGet, Basic, NULL);
UT_GenStub_Execute(PCS_moduleInfoGet, Basic, UT_DefaultHandler_PCS_moduleInfoGet);

return UT_GenStub_GetReturnValue(PCS_moduleInfoGet, PCS_STATUS);
}
142 changes: 142 additions & 0 deletions unit-test-coverage/ut-stubs/src/PCS_stdio_handlers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/************************************************************************
* 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.
************************************************************************/

/* PSP coverage stub replacement for stdio.h */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "utstubs.h"

#include "PCS_stdio.h"

struct PCS_FILE
{
int f;
};

#define PCS_STDIO_MAX_SIZE 0x01000000

void UT_DefaultHandler_PCS_fgets(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
/* char *PCS_fgets(char *s, int n, PCS_FILE *stream) */
char *s = UT_Hook_GetArgValueByName(Context, "s", char *);
int n = UT_Hook_GetArgValueByName(Context, "n", int);

int32 Status;
uint32 CopySize;
void * DataBufferPtr;

Status = 0;
UT_GetDataBuffer(FuncKey, &DataBufferPtr, NULL, NULL);
UT_Stub_GetInt32StatusCode(Context, &Status);

if (DataBufferPtr != NULL)
{
if (Status > n)
{
CopySize = n;
}
else
{
CopySize = Status;
}

CopySize = UT_Stub_CopyToLocal(UT_KEY(PCS_fgets), s, CopySize);
}
else
{
CopySize = 0;
}

if (n < Status)
{
Status = n;
}

if (UT_GetStubCount(UT_KEY(PCS_fgets) < 4) && Status > 0 && CopySize < Status)
{
memset(&s[CopySize], 'x', Status - CopySize);
CopySize = Status;
}

if (CopySize <= 0)
{
s = NULL;
}
else
{
s[CopySize - 1] = 0;
}

UT_Stub_SetReturnValue(FuncKey, s);
}

void UT_DefaultHandler_PCS_fopen(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
/* PCS_FILE *PCS_fopen(const char *filename, const char *modes) */
PCS_FILE * retval;
static PCS_FILE FOPEN_FP = {0};

if (!UT_Stub_GetInt32StatusCode(Context, NULL))
{
retval = &FOPEN_FP;
UT_Stub_SetReturnValue(FuncKey, retval);
}
}

void UT_DefaultHandler_PCS_snprintf(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context,
va_list ArgList)
{
/* int PCS_snprintf(char *s, size_t maxlen, const char *format, ...) */
char * s = UT_Hook_GetArgValueByName(Context, "s", char *);
size_t maxlen = UT_Hook_GetArgValueByName(Context, "maxlen", size_t);
const char *format = UT_Hook_GetArgValueByName(Context, "format", const char *);

int retval;

/* need to actually _do_ the snprintf */
if (!UT_Stub_GetInt32StatusCode(Context, NULL))
{
retval = vsnprintf(s, maxlen, format, ArgList);
UT_Stub_SetReturnValue(FuncKey, retval);
}
}

void UT_DefaultHandler_PCS_vsnprintf(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context)
{
/* int PCS_vsnprintf(char *s, size_t maxlen, const char *format, PCS_va_list arg) */
char * s = UT_Hook_GetArgValueByName(Context, "s", char *);
size_t maxlen = UT_Hook_GetArgValueByName(Context, "maxlen", size_t);
const char *format = UT_Hook_GetArgValueByName(Context, "format", const char *);

int retval = 0;

/* need to actually _do_ something here -
* cannot do the real vsnprintf because we lost the args. */
if (!UT_Stub_GetInt32StatusCode(Context, NULL))
{
retval = snprintf(s, maxlen, "%s", format);
UT_Stub_SetReturnValue(FuncKey, retval);
}
}

static PCS_FILE LOCAL_FP[3] = {{10}, {11}, {12}};

PCS_FILE *PCS_stdin = &LOCAL_FP[0];
PCS_FILE *PCS_stdout = &LOCAL_FP[1];
PCS_FILE *PCS_stderr = &LOCAL_FP[2];
Loading

0 comments on commit 257eafa

Please sign in to comment.