Skip to content

Commit

Permalink
Makes CF_strnlen publicly accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfiguer authored and jdfiguer committed Jun 17, 2024
1 parent 12eff1c commit 0d2a0ea
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 18 deletions.
16 changes: 0 additions & 16 deletions fsw/src/cf_cfdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,6 @@ CF_Logical_PduBuffer_t *CF_CFDP_ConstructPduHeader(const CF_Transaction_t *txn,
return ph;
}

/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static inline size_t CF_strnlen(const char *str, size_t maxlen)
{
const char *end = memchr(str, 0, maxlen);
if (end != NULL)
{
/* actual length of string is difference */
maxlen = end - str;
}
return maxlen;
}

/*----------------------------------------------------------------
*
* Application-scope internal function
Expand Down
21 changes: 20 additions & 1 deletion fsw/src/cf_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ CFE_Status_t CF_WriteHistoryEntryToFile(osal_id_t fd, const CF_History_t *histor
break;
}

len = strlen(linebuf);
len = CF_strnlen(linebuf, (CF_FILENAME_MAX_LEN * 2) + 128);
ret = CF_WrappedWrite(fd, linebuf, len);
if (ret != len)
{
Expand Down Expand Up @@ -588,3 +588,22 @@ CF_TxnStatus_t CF_TxnStatus_From_ConditionCode(CF_CFDP_ConditionCode_t cc)
/* All CFDP CC values directly correspond to a Transaction Status of the same numeric value */
return (CF_TxnStatus_t)cc;
}

/*----------------------------------------------------------------
*
* Function: CF_strnlen
*
* Application-scope internal function
* See description in cf_utils.h for argument/return detail
*
*-----------------------------------------------------------------*/
size_t CF_strnlen(const char *str, size_t maxlen)
{
const char *end = memchr(str, 0, maxlen);
if (end != NULL)
{
/* actual length of string is difference */
maxlen = end - str;
}
return maxlen;
}
17 changes: 17 additions & 0 deletions fsw/src/cf_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,21 @@ CF_TxnStatus_t CF_TxnStatus_From_ConditionCode(CF_CFDP_ConditionCode_t cc);
*/
bool CF_TxnStatus_IsError(CF_TxnStatus_t txn_stat);

/************************************************************************/
/** @brief Calculates the length of a string up to a maximum length
*
* Purpose: Provides a local OSAL routine to get the functionality
* of the (non-C99) "strnlen()" function, via the
* C89/C99 standard "memchr()" function instead.
*
* @par Assumptions, External Events, and Notes:
* None
*
* @param str Pointer to the input string
* @param maxlen Maximum number of characters to check
*
* @returns Length of the string up to `maxlen` characters
*/
size_t CF_strnlen(const char *str, size_t maxlen);

#endif /* !CF_UTILS_H */
4 changes: 4 additions & 0 deletions unit-test/cf_cfdp_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ void Test_CF_CFDP_SendMd(void)
strncpy(history->fnames.src_filename, "src1", sizeof(history->fnames.src_filename));
txn->state = CF_TxnState_S1;
txn->fsize = 1234;
UT_SetDefaultReturnValue(UT_KEY(CF_strnlen), strlen(history->fnames.src_filename));
UT_SetDeferredRetcode(UT_KEY(CF_strnlen), 1, strlen(history->fnames.dst_filename));
UtAssert_INT32_EQ(CF_CFDP_SendMd(txn), CFE_SUCCESS);
UtAssert_UINT32_EQ(md->size, txn->fsize);
UtAssert_STRINGBUF_EQ(md->dest_filename.data_ptr, md->dest_filename.length, history->fnames.dst_filename,
Expand All @@ -625,6 +627,8 @@ void Test_CF_CFDP_SendMd(void)
strncpy(history->fnames.src_filename, "src2", sizeof(history->fnames.src_filename));
txn->state = CF_TxnState_S2;
txn->fsize = 5678;
UT_SetDefaultReturnValue(UT_KEY(CF_strnlen), strlen(history->fnames.src_filename));
UT_SetDeferredRetcode(UT_KEY(CF_strnlen), 2, strlen(history->fnames.dst_filename));
UtAssert_INT32_EQ(CF_CFDP_SendMd(txn), CFE_SUCCESS);
UtAssert_UINT32_EQ(md->size, txn->fsize);
UtAssert_UINT32_EQ(md->dest_filename.length, sizeof(history->fnames.dst_filename));
Expand Down
45 changes: 45 additions & 0 deletions unit-test/cf_utils_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,42 @@ void Test_CF_TxnStatus_From_ConditionCode(void)
}
}

/*******************************************************************************
**
** CF_strnlen tests
**
*******************************************************************************/
void Test_CF_strnlen_null_character_found(void)
{
/* Arrange */
size_t result;
char str[CF_FILENAME_MAX_LEN];

memset(str, 0xFF, sizeof(str) - 1);
str[CF_FILENAME_MAX_LEN - 1] = '\0';

/* Act */
result = CF_strnlen(str, sizeof(str));

/* Assert */
UtAssert_INT32_EQ(result, sizeof(str) - 1);
}

void Test_CF_strnlen_null_character_not_found(void)
{
/* Arrange */
size_t result;
char str[CF_FILENAME_MAX_LEN];

memset(str, 0xFF, sizeof(str));

/* Act */
result = CF_strnlen(str, sizeof(str));

/* Assert */
UtAssert_INT32_EQ(result, sizeof(str));
}

/*******************************************************************************
**
** cf_utils_tests UtTest_Add groups
Expand Down Expand Up @@ -1291,6 +1327,13 @@ void add_CF_WrappedLseek_tests(void)
cf_utils_tests_Teardown, "Test_CF_WrappedLseek_Call_OS_lseek_WithGivenArgumentsAndReturnItsReturnValue");
}

void add_CF_strnlen_tests(void)
{
UtTest_Add(Test_CF_strnlen_null_character_found, cf_utils_tests_Setup, cf_utils_tests_Teardown,
"Test_CF_strnlen_null_character_found");
UtTest_Add(Test_CF_strnlen_null_character_not_found, cf_utils_tests_Setup, cf_utils_tests_Teardown,
"Test_CF_strnlen_null_character_not_found");
}
/*******************************************************************************
**
** cf_utils_tests UtTest_Setup
Expand Down Expand Up @@ -1330,4 +1373,6 @@ void UtTest_Setup(void)
add_CF_WrappedWrite_tests();

add_CF_WrappedLseek_tests();

add_CF_strnlen_tests();
}
19 changes: 18 additions & 1 deletion unit-test/stubs/cf_utils_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void UT_DefaultHandler_CF_WriteTxnQueueDataToFile(void *, UT_EntryKey_t, const U
* Generated stub function for CF_FindTransactionBySequenceNumber()
* ----------------------------------------------------
*/
CF_Transaction_t *CF_FindTransactionBySequenceNumber(CF_Channel_t *chan,
CF_Transaction_t *CF_FindTransactionBySequenceNumber(CF_Channel_t * chan,
CF_TransactionSeq_t transaction_sequence_number,
CF_EntityId_t src_eid)
{
Expand Down Expand Up @@ -416,3 +416,20 @@ CFE_Status_t CF_WriteTxnQueueDataToFile(osal_id_t fd, CF_Channel_t *chan, CF_Que

return UT_GenStub_GetReturnValue(CF_WriteTxnQueueDataToFile, CFE_Status_t);
}

/*
* ----------------------------------------------------
* Generated stub function for CF_strnlen()
* ----------------------------------------------------
*/
size_t CF_strnlen(const char *str, size_t maxlen)
{
UT_GenStub_SetupReturnBuffer(CF_strnlen, size_t);

UT_GenStub_AddParam(CF_strnlen, const char *, str);
UT_GenStub_AddParam(CF_strnlen, size_t, maxlen);

UT_GenStub_Execute(CF_strnlen, Basic, NULL);

return UT_GenStub_GetReturnValue(CF_strnlen, size_t);
}

0 comments on commit 0d2a0ea

Please sign in to comment.