diff --git a/src/app/tests/TestCommandInteraction.cpp b/src/app/tests/TestCommandInteraction.cpp index 30b9fe5b46da62..02bf686248c1df 100644 --- a/src/app/tests/TestCommandInteraction.cpp +++ b/src/app/tests/TestCommandInteraction.cpp @@ -119,6 +119,12 @@ struct ForcedSizeBuffer } }; +enum class ForcedSizeBufferLengthHint +{ + kSizeBetween0and255, + kSizeGreaterThan255, +}; + InteractionModel::Status ServerClusterCommandExists(const ConcreteCommandPath & aRequestCommandPath) { // Mock cluster catalog, only support commands on one cluster on one endpoint. @@ -380,7 +386,7 @@ class TestCommandInteraction bool aNeedStatusCode, CommandId aResponseCommandId = kTestCommandIdWithData, CommandId aRequestCommandId = kTestCommandIdWithData); static uint32_t GetAddResponseDataOverheadSizeForPath(nlTestSuite * apSuite, const ConcreteCommandPath & aRequestCommandPath, - bool aIsForcedSizeBufferLargerThan255); + ForcedSizeBufferLengthHint aBufferSizeHint); static void FillCurrentInvokeResponseBuffer(nlTestSuite * apSuite, CommandHandler * apCommandHandler, const ConcreteCommandPath & aRequestCommandPath, uint32_t aSizeToLeaveInBuffer); static void ValidateCommandHandlerEncodeInvokeResponseMessage(nlTestSuite * apSuite, void * apContext, bool aNeedStatusCode); @@ -581,26 +587,26 @@ void TestCommandInteraction::AddInvokeResponseData(nlTestSuite * apSuite, void * uint32_t TestCommandInteraction::GetAddResponseDataOverheadSizeForPath(nlTestSuite * apSuite, const ConcreteCommandPath & aRequestCommandPath, - bool aIsForcedSizeBufferLargerThan255) + ForcedSizeBufferLengthHint aBufferSizeHint) { BasicCommandPathRegistry<4> mBasicCommandPathRegistry; CommandHandler commandHandler(kCommandHandlerTestOnlyMarker, &mockCommandHandlerDelegate, &mBasicCommandPathRegistry); commandHandler.mReserveSpaceForMoreChunkMessages = true; ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - Optional commandRef; - commandRef.SetValue(1); - mBasicCommandPathRegistry.Add(requestCommandPath1, commandRef); - commandRef.SetValue(2); - mBasicCommandPathRegistry.Add(requestCommandPath2, commandRef); - CHIP_ERROR err = commandHandler.AllocateBuffer(); + CHIP_ERROR err = mBasicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = mBasicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + + err = commandHandler.AllocateBuffer(); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t remainingSizeBefore = commandHandler.mInvokeResponseBuilder.GetWriter()->GetRemainingFreeLength(); // When ForcedSizeBuffer exceeds 255, an extra byte is needed for length, affecting the overhead size required by - // AddResponseData. - uint32_t sizeOfForcedSizeBuffer = aIsForcedSizeBufferLargerThan255 ? 256 : 0; + // AddResponseData. In order to have this accounted for in overhead calculation we set the length to be 256. + uint32_t sizeOfForcedSizeBuffer = aBufferSizeHint == ForcedSizeBufferLengthHint::kSizeGreaterThan255 ? 256 : 0; err = commandHandler.AddResponseData(aRequestCommandPath, ForcedSizeBuffer(sizeOfForcedSizeBuffer)); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t remainingSizeAfter = commandHandler.mInvokeResponseBuilder.GetWriter()->GetRemainingFreeLength(); @@ -622,9 +628,9 @@ void TestCommandInteraction::FillCurrentInvokeResponseBuffer(nlTestSuite * apSui // AddResponseData's overhead calculation depends on the size of ForcedSizeBuffer. If the buffer exceeds 255 bytes, an extra // length byte is required. Since tests using FillCurrentInvokeResponseBuffer currently end up with sizeToFill > 255, we // inform the calculation of this expectation. Nonetheless, we also validate this assumption for correctness. - bool isForcedSizeBufferLargerThan255 = true; + ForcedSizeBufferLengthHint bufferSizeHint = ForcedSizeBufferLengthHint::kSizeGreaterThan255; uint32_t overheadSizeNeededForAddingResponse = - GetAddResponseDataOverheadSizeForPath(apSuite, aRequestCommandPath, isForcedSizeBufferLargerThan255); + GetAddResponseDataOverheadSizeForPath(apSuite, aRequestCommandPath, bufferSizeHint); NL_TEST_ASSERT(apSuite, remainingSize > (aSizeToLeaveInBuffer + overheadSizeNeededForAddingResponse)); uint32_t sizeToFill = remainingSize - aSizeToLeaveInBuffer - overheadSizeNeededForAddingResponse; @@ -1846,11 +1852,11 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere commandHandler.mReserveSpaceForMoreChunkMessages = true; ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - Optional commandRef; - commandRef.SetValue(1); - mBasicCommandPathRegistry.Add(requestCommandPath1, commandRef); - commandRef.SetValue(2); - mBasicCommandPathRegistry.Add(requestCommandPath2, commandRef); + + CHIP_ERROR err = mBasicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = mBasicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t sizeToLeave = 0; FillCurrentInvokeResponseBuffer(apSuite, &commandHandler, requestCommandPath1, sizeToLeave); @@ -1872,11 +1878,11 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere commandHandler.mReserveSpaceForMoreChunkMessages = true; ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - Optional commandRef; - commandRef.SetValue(1); - mBasicCommandPathRegistry.Add(requestCommandPath1, commandRef); - commandRef.SetValue(2); - mBasicCommandPathRegistry.Add(requestCommandPath2, commandRef); + + CHIP_ERROR err = mBasicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = mBasicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t sizeToLeave = 0; FillCurrentInvokeResponseBuffer(apSuite, &commandHandler, requestCommandPath1, sizeToLeave); @@ -1898,11 +1904,11 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere commandHandler.mReserveSpaceForMoreChunkMessages = true; ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - Optional commandRef; - commandRef.SetValue(1); - mBasicCommandPathRegistry.Add(requestCommandPath1, commandRef); - commandRef.SetValue(2); - mBasicCommandPathRegistry.Add(requestCommandPath2, commandRef); + + CHIP_ERROR err = mBasicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + err = mBasicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t sizeToLeave = 0; FillCurrentInvokeResponseBuffer(apSuite, &commandHandler, requestCommandPath1, sizeToLeave); @@ -1910,7 +1916,7 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere NL_TEST_ASSERT(apSuite, remainingSize == sizeToLeave); uint32_t sizeToFill = 50; - CHIP_ERROR err = commandHandler.AddResponseData(requestCommandPath2, ForcedSizeBuffer(sizeToFill)); + err = commandHandler.AddResponseData(requestCommandPath2, ForcedSizeBuffer(sizeToFill)); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); remainingSize = commandHandler.mInvokeResponseBuilder.GetWriter()->GetRemainingFreeLength();