Skip to content

Commit

Permalink
Fix MultiBufferEncode test with pool allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jul 1, 2024
1 parent 58572b3 commit 3772abb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/system/SystemPacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese

if (lAllocSize > PacketBuffer::kMaxAllocSize)
{
ChipLogError(chipSystemLayer, "PacketBuffer: allocation exceeding buffer capacity limits.");
ChipLogError(chipSystemLayer, "PacketBuffer: allocation exceeding buffer capacity limits: %lu > %lu",
static_cast<unsigned long>(lAllocSize), static_cast<unsigned long>(PacketBuffer::kMaxAllocSize));
return PacketBufferHandle();
}

Expand Down
33 changes: 22 additions & 11 deletions src/system/tests/TestTLVPacketBufferBackingStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,28 @@ TEST_F(TestTLVPacketBufferBackingStore, MultiBufferEncode)
// Third entry is 1 control byte, 2 length bytes, 2000 bytes of data,
// for a total of 2009 bytes.
constexpr size_t totalSize = 2009;
EXPECT_EQ(buffer->TotalLength(), totalSize);
EXPECT_EQ(buffer->DataLength(), static_cast<size_t>(2));
ASSERT_TRUE(buffer->HasChainedBuffer());
auto nextBuffer = buffer->Next();
EXPECT_EQ(nextBuffer->TotalLength(), totalSize - 2);
EXPECT_EQ(nextBuffer->DataLength(), PacketBuffer::kMaxSizeWithoutReserve);
ASSERT_TRUE(nextBuffer->HasChainedBuffer());
nextBuffer = nextBuffer->Next();
EXPECT_EQ(nextBuffer->TotalLength(), nextBuffer->DataLength());
EXPECT_EQ(nextBuffer->DataLength(), totalSize - 2 - PacketBuffer::kMaxSizeWithoutReserve);
ASSERT_FALSE(nextBuffer->HasChainedBuffer());
#if CHIP_SYSTEM_PACKETBUFFER_FROM_LWIP_STANDARD_POOL || CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL
// In case of pool allocation, the buffer size is always the maximum size.
constexpr size_t bufferSizes[] = { PacketBuffer::kMaxSizeWithoutReserve, totalSize - PacketBuffer::kMaxSizeWithoutReserve };
#elif CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP
constexpr size_t bufferSizes[] = { 2, PacketBuffer::kMaxSizeWithoutReserve,
totalSize - 2 - PacketBuffer::kMaxSizeWithoutReserve };
#else
// Skipping the test for other configurations because the allocation method is unknown.
GTEST_SKIP();
#endif
size_t checkedSize = 0;
auto bufferTmp = buffer.Retain();
for (const auto size : bufferSizes)
{
ASSERT_FALSE(bufferTmp.IsNull());
EXPECT_EQ(bufferTmp->TotalLength(), totalSize - checkedSize);
EXPECT_EQ(bufferTmp->DataLength(), size);
bufferTmp = bufferTmp->Next();
checkedSize += size;
}
// There should be no more buffers.
ASSERT_TRUE(bufferTmp.IsNull());

// PacketBufferTLVReader cannot handle non-contiguous buffers, and our
// buffers are too big to stick into a single packet buffer.
Expand Down

0 comments on commit 3772abb

Please sign in to comment.