Use a static array of testdata, to initialize stub pointer

Simplified function pointer parameter handling by introducing STUB_PARAMETER_POINTER_HANDLING_CONTENT for pointer tests
This commit is contained in:
2021-12-24 00:19:07 +01:00
parent d7f6efd0a6
commit 5782449388
2 changed files with 101 additions and 109 deletions

View File

@@ -181,7 +181,7 @@ STATIC void createVariableSpecificCheck(FILE *aFile, cfile_variable_t *aVariable
}
}
STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cfunction_parameter_t *aParameter, char aParameterIndex)
STATIC void createParameterSpecificCheck(char *aNoSuffix, FILE *aFile, cfunction_t *aFunction, cfunction_parameter_t *aParameter, char aParameterIndex)
{
// skip parameter without dataType
if (NULL == aParameter->dataType)
@@ -195,69 +195,49 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf
fprintf(aFile, STUBINIT_PARAM_PARAMETER_S1", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1, aParameterIndex, aFunction->name,
aFunction->name, aParameterIndex);
fprintf(aFile, ");"NEWLINES);
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, aFunction->name,
aFunction->name, aParameterIndex, aParameter->dataType);
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S1";" NEWLINES, aFunction->name,
aFunction->name, aParameterIndex, aParameter->dataType, aNoSuffix);
}
else if (NULL != strstr(aParameter->dataType, "*") //
&& NULL != strstr(aParameter->dataType, "char") // handle char* as strings
&& NULL == strstr(aParameter->dataType, "uchar")) // don't handle uchar (-> signed warning for strlen)
else if (NULL != strstr(aParameter->dataType, "*") || NULL != aParameter->array) //
{
// char array with string comparison
fprintf(aFile, "\tif (" //
STUBPARAMETER_POINTERHANDLING_ADDRESS_S " == " STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_POINTERHANDLING_S//
")" NEWLINES "\t{" NEWLINES, aFunction->name, aFunction->name);
// only assert if result should not be ignored (_size parameter)
fprintf(aFile, "\t\tif ("STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size)" NEWLINES, //
aFunction->name, aFunction->name, aParameterIndex);
fprintf(aFile, "\t\t\tCU_ASSERT_PTR_EQUAL(");
fprintf(aFile, STUBINIT_PARAM_PARAMETER_S1", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1");" NEWLINES, //
aParameterIndex, aFunction->name, aFunction->name, aParameterIndex);
fprintf(aFile, "\t}" NEWLINES);
// STUB_PARAMETER_POINTER_HANDLING_CONTENT
fprintf(aFile, "\telse if (" //
STUBPARAMETER_POINTERHANDLING_CONTENT_S " == " STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_POINTERHANDLING_S//
")" NEWLINES "\t{" NEWLINES, aFunction->name, aFunction->name);
fprintf(aFile, "\t\tif ("STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size)" NEWLINES, aFunction->name, aFunction->name,
aParameterIndex);
fprintf(aFile, "\t\t\tCU_ASSERT_MEMORY_CHECK(\"%s(%s)\", ", aFunction->name, aParameter->name);
fprintf(aFile, STUBINIT_PARAM_PARAMETER_S1", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1, aParameterIndex, aFunction->name,
aFunction->name, aParameterIndex);
fprintf(aFile, ", strlen("STUBINIT_PARAM_PARAMETER_S1"));"NEWLINES, aParameterIndex);
fprintf(aFile, "\t}" NEWLINES);
// injection only for non constant and non NULL parameter
if (NULL == strstr(aParameter->dataType, CPARS_ELEMENT_CONST))
{
fprintf(aFile, "\telse if (NULL != " STUBINIT_PARAM_PARAMETER_S1 ")" NEWLINES "\t{" NEWLINES, aParameterIndex);
fprintf(aFile, "\t\tmemcpy(" STUBINIT_PARAM_PARAMETER_S1 ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1", " //
STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size",//
aParameterIndex, aFunction->name, aFunction->name, aParameterIndex, aFunction->name, aFunction->name, aParameterIndex);
fprintf(aFile, ");" NEWLINES);
fprintf(aFile, "\t}" NEWLINES);
}
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, aFunction->name,
aFunction->name, aParameterIndex, aParameter->dataType);
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size = "STUBDEFAULT_VALUE_POINTERSIZE_S";" NEWLINES,
fprintf(aFile, ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size);" NEWLINES, //
aFunction->name, aFunction->name, aParameterIndex);
}
else if (NULL != strstr(aParameter->dataType, "*") || NULL != aParameter->array)
{
// pointer
fprintf(aFile, "\tif (" //
STUBPARAMETER_POINTERHANDLING_ADDRESS_S " == " STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_POINTERHANDLING_S//
")" NEWLINES "\t{" NEWLINES, aFunction->name, aFunction->name);
// only assert if result should not be ignored (_size parameter)
fprintf(aFile, "\t\tif ("STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size)" NEWLINES, aFunction->name, aFunction->name,
aParameterIndex);
fprintf(aFile, "\t\t\tCU_ASSERT_PTR_EQUAL(");
fprintf(aFile, STUBINIT_PARAM_PARAMETER_S1", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1, aParameterIndex, aFunction->name,
aFunction->name, aParameterIndex);
fprintf(aFile, ");" NEWLINES);
fprintf(aFile, "\t}" NEWLINES);
// injection only for non constant and non NULL parameter
if (NULL == strstr(aParameter->dataType, CPARS_ELEMENT_CONST))
{
fprintf(aFile, "\telse if (NULL != " STUBINIT_PARAM_PARAMETER_S1 ")" NEWLINES "\t{" NEWLINES, aParameterIndex);
fprintf(aFile, "\t\tmemcpy(" STUBINIT_PARAM_PARAMETER_S1 ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1", " //
STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size",//
STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size);" NEWLINES,//
aParameterIndex, aFunction->name, aFunction->name, aParameterIndex, aFunction->name, aFunction->name, aParameterIndex);
fprintf(aFile, ");" NEWLINES);
fprintf(aFile, "\t}" NEWLINES);
}
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, //
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S1";" NEWLINES, //
aFunction->name, // STUBPARAMETER_STRUCT_S1
aFunction->name, // STUBINSTANCE_S1
aParameterIndex, // STUBPARAMETER_PARAM_S1,
aParameter->dataType, (NULL == strstr(aParameter->dataType, "*") ? "*" : "")); // cast array only parameter to be pointer
aParameter->dataType, (NULL == strstr(aParameter->dataType, "*") ? "*" : ""), // cast array only parameter to be pointer
aNoSuffix);
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size = "STUBDEFAULT_VALUE_POINTERSIZE_S";" NEWLINES,
aFunction->name, aFunction->name, aParameterIndex);
}
@@ -414,14 +394,14 @@ STATIC int8_t createStubCheck(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfile
return 0;
}
STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *aFunction)
STATIC int8_t createStubFunctionBlock(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfunction_t *aFunction)
{
cfunction_parameter_t *parameter = NULL;
char parameterIndex = STUBDEFAULT_PARAMETER_INDEX_CHAR;
char *dataTypeTemp = NULL;
bool pointerAvailable = false;
if (NULL == aFile || NULL == aHeader || NULL == aFunction)
if (NULL == aNoSuffix || NULL == aFile || NULL == aHeader || NULL == aFunction)
{
return -1;
}
@@ -556,10 +536,11 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a
// initialize _size parameter to 1 (default to check addresses)
if (CPARAMETER_TYPE_FUNCPTR != parameter->type && (parameter->array || NULL != strstr(parameter->dataType, "*")))
{
fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S"," NEWLINES, //
fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S1"," NEWLINES, //
STUB_INSTANCES_AMOUNT - 1, parameterIndex, //STUBDEFAULT_VALUE_POINTER_S
parameter->dataType, //
(NULL == strstr(parameter->dataType, "*") ? "*" : "")); // cast array only parameter to be pointer
(NULL == strstr(parameter->dataType, "*") ? "*" : ""), // cast array only parameter to be pointer
aNoSuffix);
fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1"_size = 1," NEWLINES, STUB_INSTANCES_AMOUNT - 1, parameterIndex);
}
else
@@ -711,6 +692,8 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a
fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_CALLTEST_S" = "STUBINIT_PARAM_CALLTEST_S";" NEWLINES,
aFunction->name);
if (aFunction->parameter.amount)
{
// if STUB_CALL_PASS is requested init with parameter
fprintf(aFile, "\tif ( STUB_CALL_PASS == " STUBINIT_PARAM_CALLTEST_S ")" NEWLINES "\t{" NEWLINES);
if (pointerAvailable)
@@ -767,17 +750,19 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a
// initialize _size parameter to 1 (default to check addresses)
if (CPARAMETER_TYPE_FUNCPTR != parameter->type && (parameter->array || NULL != strstr(parameter->dataType, "*")))
{
fprintf(aFile, "\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES,
fprintf(aFile,
"\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S1";" NEWLINES,
aFunction->name, parameterIndex, //
parameter->dataType,
(NULL == strstr(parameter->dataType, "*") ? "*" : "")); // cast array only parameter to be pointer
parameter->dataType, (NULL == strstr(parameter->dataType, "*") ? "*" : ""), // cast array only parameter to be pointer
aNoSuffix);
fprintf(aFile,
"\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1"_size = "STUBDEFAULT_VALUE_POINTERSIZE_S";" NEWLINES,
aFunction->name, parameterIndex);
}
else
{
fprintf(aFile, "\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_VARIABLE_S";" NEWLINES,
fprintf(aFile,
"\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_VARIABLE_S";" NEWLINES,
aFunction->name, parameterIndex, parameter->dataType);
}
@@ -785,7 +770,7 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a
parameter = parameter->next;
}
fprintf(aFile, "\t}" NEWLINES);
}
fprintf(aFile, "}" NEWLINES);
NEWLINE(aFile);
@@ -844,7 +829,7 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a
parameter = parameter->next;
continue;
}
createParameterSpecificCheck(aFile, aFunction, parameter, parameterIndex);
createParameterSpecificCheck(aNoSuffix, aFile, aFunction, parameter, parameterIndex);
++parameterIndex;
parameter = parameter->next;
}
@@ -1006,6 +991,12 @@ STATIC int8_t createStub(char *aOutput, char *aNoSuffix, cfile_t *aCfile)
fprintf(cfile, "#include \"stub_%s_user.h\""NEWLINES, aNoSuffix);
fprintf(cfile, "#include \"%s\""NEWLINES NEWLINES, gnu_basename(cHeaderName));
if (0 == cfile_hasPointerParameter(aCfile))
{
// Test data for stub pointer parameter to be initialized to.
fprintf(cfile, "static uint8_t "STUBDEFAULT_VALUE_POINTER_S1"[9] = { [0 ... 7] = 'A', [8] = '\\0' };" NEWLINES NEWLINES, aNoSuffix);
}
fprintf(cheader, "/*! @file %s"NEWLINES, gnu_basename(cHeaderName));
fprintf(cheader, " * @details"NEWLINES" * This is a stub header.\\n"NEWLINES);
fprintf(cheader, " * - generated by stubser -"NEWLINES" */" NEWLINES NEWLINES);
@@ -1020,7 +1011,7 @@ STATIC int8_t createStub(char *aOutput, char *aNoSuffix, cfile_t *aCfile)
function = aCfile->functions.head;
while (function)
{
(void) createStubFunctionBlock(cfile, cheader, function);
(void) createStubFunctionBlock(aNoSuffix, cfile, cheader, function);
function = function->next;
}

View File

@@ -34,6 +34,7 @@
#define STUBPARAMETER_POINTERHANDLING_S "stubPointerHandling"
#define STUBPARAMETER_POINTERHANDLING_ENUM_S "stub_parameter_pointer_handling_t"
#define STUBPARAMETER_POINTERHANDLING_ADDRESS_S "STUB_PARAMETER_POINTER_HANDLING_ADDRESS"
#define STUBPARAMETER_POINTERHANDLING_CONTENT_S "STUB_PARAMETER_POINTER_HANDLING_CONTENT"
#define STUBPARAMETER_POINTERHANDLING_INJECT_S "STUB_PARAMETER_POINTER_HANDLING_INJECT"
#define STUBPARAMETER_RETURN_S "returnValue" // "stubparameter_modulfunction_1".returnValue
#define STUBINSTANCE_S1 "stubInstance_%s" // stubInstance_modulfunction_1
@@ -46,7 +47,7 @@
#define STUBDEFAULT_PARAMETER_INDEX_CHAR 'a'
#define STUBDEFAULT_VALUE_FUNCTIONPOINTER_S "0xAA"
#define STUBDEFAULT_VALUE_POINTER_S "0xAA"
#define STUBDEFAULT_VALUE_POINTER_S1 "%s_testData" // stubfilename_testData
#define STUBDEFAULT_VALUE_POINTERSIZE_S "1"
#define STUBDEFAULT_VALUE_VARIABLE_S "0xAA"