Matter SDK Coverage Report
Current view: top level - app/server-cluster/testing - MockCommandHandler.cpp (source / functions) Coverage Total Hit
Test: SHA:e98a48c2e59f85a25417956e1d105721433aa5d1 Lines: 100.0 % 35 35
Test Date: 2026-01-09 16:53:50 Functions: 100.0 % 6 6

            Line data    Source code
       1              : /*
       2              :  *    Copyright (c) 2025 Project CHIP Authors
       3              :  *
       4              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       5              :  *    you may not use this file except in compliance with the License.
       6              :  *    You may obtain a copy of the License at
       7              :  *
       8              :  *        http://www.apache.org/licenses/LICENSE-2.0
       9              :  *
      10              :  *    Unless required by applicable law or agreed to in writing, software
      11              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      12              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13              :  *    See the License for the specific language governing permissions and
      14              :  *    limitations under the License.
      15              :  */
      16              : 
      17              : #include <app/server-cluster/testing/MockCommandHandler.h>
      18              : 
      19              : #include <app/data-model/Encode.h>
      20              : #include <lib/core/CHIPError.h>
      21              : #include <lib/core/TLVTags.h>
      22              : #include <lib/support/CodeUtils.h>
      23              : #include <protocols/interaction_model/StatusCode.h>
      24              : #include <system/SystemPacketBuffer.h>
      25              : #include <transport/SessionManager.h>
      26              : #include <transport/raw/MessageHeader.h>
      27              : 
      28              : namespace chip {
      29              : namespace Testing {
      30              : 
      31            5 : CHIP_ERROR MockCommandHandler::FallibleAddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
      32              :                                                  const Protocols::InteractionModel::ClusterStatusCode & aStatus,
      33              :                                                  const char * context)
      34              : {
      35            5 :     mStatuses.emplace_back(StatusRecord{ aRequestCommandPath, aStatus, context });
      36            5 :     return CHIP_NO_ERROR;
      37              : }
      38              : 
      39            5 : void MockCommandHandler::AddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
      40              :                                    const Protocols::InteractionModel::ClusterStatusCode & aStatus, const char * context)
      41              : {
      42            5 :     CHIP_ERROR err = FallibleAddStatus(aRequestCommandPath, aStatus, context);
      43           10 :     VerifyOrDie(err == CHIP_NO_ERROR);
      44            5 : }
      45              : 
      46          143 : CHIP_ERROR MockCommandHandler::AddResponseData(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
      47              :                                                const app::DataModel::EncodableToTLV & aEncodable)
      48              : {
      49          143 :     chip::System::PacketBufferHandle handle = chip::MessagePacketBuffer::New(chip::kMaxAppMessageLen);
      50          143 :     VerifyOrReturnError(!handle.IsNull(), CHIP_ERROR_NO_MEMORY);
      51              : 
      52          143 :     TLV::TLVWriter baseWriter;
      53          143 :     baseWriter.Init(handle->Start(), handle->MaxDataLength());
      54          143 :     app::DataModel::FabricAwareTLVWriter writer(baseWriter, mFabricIndex);
      55              :     TLV::TLVType ct;
      56              : 
      57          143 :     ReturnErrorOnFailure(writer.mTLVWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, ct));
      58          143 :     ReturnErrorOnFailure(aEncodable.EncodeTo(writer, TLV::ContextTag(app::CommandDataIB::Tag::kFields)));
      59          143 :     ReturnErrorOnFailure(writer.mTLVWriter.EndContainer(ct));
      60          143 :     handle->SetDataLength(writer.mTLVWriter.GetLengthWritten());
      61              : 
      62          143 :     mResponses.emplace_back(ResponseRecord{ aResponseCommandId, std::move(handle), aRequestCommandPath });
      63          143 :     return CHIP_NO_ERROR;
      64          143 : }
      65              : 
      66          143 : void MockCommandHandler::AddResponse(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
      67              :                                      const app::DataModel::EncodableToTLV & aEncodable)
      68              : {
      69          143 :     CHIP_ERROR err = AddResponseData(aRequestCommandPath, aResponseCommandId, aEncodable);
      70          286 :     VerifyOrDie(err == CHIP_NO_ERROR);
      71          143 : }
      72              : 
      73          141 : CHIP_ERROR MockCommandHandler::GetResponseReader(TLV::TLVReader & reader) const
      74              : {
      75          141 :     return GetResponseReader(reader, 0);
      76              : }
      77              : 
      78          141 : CHIP_ERROR MockCommandHandler::GetResponseReader(TLV::TLVReader & reader, size_t index) const
      79              : {
      80          141 :     VerifyOrReturnError(!mResponses.empty(), CHIP_ERROR_INCORRECT_STATE);
      81          141 :     VerifyOrReturnError(index < mResponses.size(), CHIP_ERROR_INVALID_ARGUMENT);
      82          141 :     VerifyOrReturnError(!mResponses[index].encodedData.IsNull(), CHIP_ERROR_INCORRECT_STATE);
      83              : 
      84          141 :     reader.Init(mResponses[index].encodedData->Start(), mResponses[index].encodedData->DataLength());
      85          141 :     ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag()));
      86              : 
      87              :     TLV::TLVType outerContainer;
      88          141 :     ReturnErrorOnFailure(reader.EnterContainer(outerContainer));
      89          141 :     ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::ContextTag(app::CommandDataIB::Tag::kFields)));
      90              : 
      91          141 :     return CHIP_NO_ERROR;
      92              : }
      93              : 
      94              : } // namespace Testing
      95              : } // namespace chip
        

Generated by: LCOV version 2.0-1