[heap-data] add Matches() to compare Heap::Data with a given buffer (#9740)

Also updates the unit test to validate the new method.
This commit is contained in:
Abtin Keshavarzian
2023-12-26 07:51:38 -08:00
committed by GitHub
parent 8167fb3626
commit 6751122f0a
3 changed files with 43 additions and 0 deletions
+19
View File
@@ -78,6 +78,25 @@ void Data::SetFrom(Data &&aData)
TakeFrom(aData);
}
bool Data::Matches(const uint8_t *aBuffer, uint16_t aLength) const
{
bool matches = false;
VerifyOrExit(aLength == mData.GetLength());
if (IsNull())
{
matches = (aLength == 0);
}
else
{
matches = mData.MatchesBytesIn(aBuffer);
}
exit:
return matches;
}
void Data::Free(void)
{
Heap::Free(mData.GetBytes());
+12
View File
@@ -169,6 +169,18 @@ public:
*/
void CopyBytesTo(uint8_t *aBuffer) const { return mData.CopyBytesTo(aBuffer); }
/**
* Compares the `Data` content with the bytes from a given buffer.
*
* @param[in] aBuffer A pointer to a buffer to compare with the data.
* @param[in] aLength The length of @p aBuffer.
*
* @retval TRUE The `Data` content matches the bytes in @p aBuffer.
* @retval FALSE The `Data` content does not match the byes in @p aBuffer.
*
*/
bool Matches(const uint8_t *aBuffer, uint16_t aLength) const;
/**
* Frees any buffer allocated by the `Heap::Data`.
*
+12
View File
@@ -171,11 +171,15 @@ void VerifyData(const Heap::Data &aData, const uint8_t *aBytes, uint16_t aLength
PrintData(aData);
VerifyOrQuit(aData.Matches(aBytes, aLength));
VerifyOrQuit(!aData.Matches(aBytes, aLength + 1));
if (aLength == 0)
{
VerifyOrQuit(aData.IsNull());
VerifyOrQuit(aData.GetBytes() == nullptr);
VerifyOrQuit(aData.GetLength() == 0);
VerifyOrQuit(aData.Matches(nullptr, 0));
}
else
{
@@ -186,6 +190,10 @@ void VerifyData(const Heap::Data &aData, const uint8_t *aBytes, uint16_t aLength
aData.CopyBytesTo(buffer);
VerifyOrQuit(memcmp(buffer, aBytes, aLength) == 0, "CopyBytesTo() failed");
VerifyOrQuit(aData.Matches(buffer, aLength));
buffer[aLength - 1]++;
VerifyOrQuit(!aData.Matches(buffer, aLength));
}
}
@@ -223,6 +231,10 @@ void TestHeapData(void)
printf("After constructor\n");
VerifyData(data, nullptr, 0);
VerifyOrQuit(data.Matches(nullptr, 0));
VerifyOrQuit(data.Matches(kData1, 0));
VerifyOrQuit(!data.Matches(kData1, 1));
printf("------------------------------------------------------------------------------------\n");
printf("SetFrom(aBuffer, aLength)\n");