Skip to content

Commit

Permalink
Updates to allow stub override nasa#832
Browse files Browse the repository at this point in the history
Added a new hook set method for override
Added a getter method for stubs to check for override
Added a global value for tracking current override setting
Set global value to false at start and during reset call
Added implementation of setting hook with stub override
Added getter implementation
  • Loading branch information
asgibson committed Feb 26, 2021
1 parent d305b50 commit 94e4452
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ut_assert/inc/utstubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ typedef int32 (*UT_VaHookFunc_t)(void *UserObj, int32 StubRetcode, uint32 CallCo
*/
void UT_ResetState(UT_EntryKey_t FuncKey);

/**
* A value to indicate if the currently running stub's behavior after
* a hook call should be run or not.
*
* \param Retcode The boolean value indicating the override. When 'true'
* indicates remaining stub code should not be run, it is in override.
*/
bool UT_StubIsOveridden(void);

/**
* Add a deferred return code entry for the given stub function
*
Expand Down Expand Up @@ -239,6 +248,18 @@ void UT_ClearForceFail(UT_EntryKey_t FuncKey);
*/
void UT_SetHookFunction(UT_EntryKey_t FuncKey, UT_HookFunc_t HookFunc, void *UserObj);

/**
* Set a Hook function for a particular call, but override any remaining stub functionality
*
* This triggers a callback to a user-defined function when the stub is invoked.
* Upon return to the original stub the OverrideStub will be true and any remaining
* code lines in stub (that are purposefully bypassed by the stub) will not be executed.
*
* \param FuncKey The stub function to add the hook to.
* \param HookFunc User defined hook function. Set NULL to delete/clear an entry.
* \param UserObj Arbitrary user data object to pass to the hook function
*/void UT_SetHookOverrideStubFunction(UT_EntryKey_t FuncKey, UT_HookFunc_t HookFunc, void *UserObj);

/**
* Set a variable-argument Hook function for a particular call
*
Expand Down
14 changes: 14 additions & 0 deletions ut_assert/src/utstubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ typedef struct

static UT_StubTableEntry_t UT_StubTable[UT_MAX_FUNC_STUBS] = {{0}};
static uint32 UT_MaxStubSearchLen = 0;
static bool StubIsOveridden = false;

/**
* Helper function to clear an entry in the stub table.
Expand Down Expand Up @@ -201,6 +202,8 @@ void UT_ResetState(UT_EntryKey_t FuncKey)
{
UT_MaxStubSearchLen = 0;
}

StubIsOveridden = false; /* Override is ONLY on demand */
}

void UT_Stub_CallOnce(void (*Func)(void))
Expand Down Expand Up @@ -602,6 +605,12 @@ void UT_SetHookFunction(UT_EntryKey_t FuncKey, UT_HookFunc_t HookFunc, void *Use
UT_DoSetHookFunction(FuncKey, Value, UserObj, false);
}

void UT_SetHookOverrideStubFunction(UT_EntryKey_t FuncKey, UT_HookFunc_t HookFunc, void *UserObj)
{
StubIsOveridden = true;
UT_SetHookFunction(FuncKey, HookFunc, UserObj);
}

void UT_SetVaHookFunction(UT_EntryKey_t FuncKey, UT_VaHookFunc_t HookFunc, void *UserObj)
{
UT_HookFuncPtr_t Value;
Expand Down Expand Up @@ -853,3 +862,8 @@ int32 UT_DefaultStubImpl(const char *FunctionName, UT_EntryKey_t FuncKey, int32

return Retcode;
}

bool UT_StubIsOveridden()
{
return StubIsOveridden;
}

0 comments on commit 94e4452

Please sign in to comment.