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 90 : CHIP_ERROR MockCommandHandler::FallibleAddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
32 : const Protocols::InteractionModel::ClusterStatusCode & aStatus,
33 : const char * context)
34 : {
35 90 : mStatuses.emplace_back(StatusRecord{ aRequestCommandPath, aStatus, context });
36 90 : return CHIP_NO_ERROR;
37 : }
38 :
39 39 : void MockCommandHandler::AddStatus(const app::ConcreteCommandPath & aRequestCommandPath,
40 : const Protocols::InteractionModel::ClusterStatusCode & aStatus, const char * context)
41 : {
42 39 : CHIP_ERROR err = FallibleAddStatus(aRequestCommandPath, aStatus, context);
43 78 : VerifyOrDie(err == CHIP_NO_ERROR);
44 39 : }
45 :
46 257 : CHIP_ERROR MockCommandHandler::AddResponseData(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
47 : const app::DataModel::EncodableToTLV & aEncodable)
48 : {
49 257 : chip::System::PacketBufferHandle handle = chip::MessagePacketBuffer::New(chip::kMaxAppMessageLen);
50 257 : VerifyOrReturnError(!handle.IsNull(), CHIP_ERROR_NO_MEMORY);
51 :
52 257 : TLV::TLVWriter baseWriter;
53 257 : baseWriter.Init(handle->Start(), handle->MaxDataLength());
54 257 : app::DataModel::FabricAwareTLVWriter writer(baseWriter, mSubjectDescriptor.fabricIndex);
55 : TLV::TLVType ct;
56 :
57 257 : ReturnErrorOnFailure(writer.mTLVWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, ct));
58 257 : ReturnErrorOnFailure(aEncodable.EncodeTo(writer, TLV::ContextTag(app::CommandDataIB::Tag::kFields)));
59 257 : ReturnErrorOnFailure(writer.mTLVWriter.EndContainer(ct));
60 257 : handle->SetDataLength(writer.mTLVWriter.GetLengthWritten());
61 :
62 257 : mResponses.emplace_back(ResponseRecord{ aResponseCommandId, std::move(handle), aRequestCommandPath });
63 257 : return CHIP_NO_ERROR;
64 257 : }
65 :
66 257 : void MockCommandHandler::AddResponse(const app::ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
67 : const app::DataModel::EncodableToTLV & aEncodable)
68 : {
69 257 : CHIP_ERROR err = AddResponseData(aRequestCommandPath, aResponseCommandId, aEncodable);
70 514 : VerifyOrDie(err == CHIP_NO_ERROR);
71 257 : }
72 :
73 254 : CHIP_ERROR MockCommandHandler::GetResponseReader(TLV::TLVReader & reader) const
74 : {
75 254 : return GetResponseReader(reader, 0);
76 : }
77 :
78 254 : CHIP_ERROR MockCommandHandler::GetResponseReader(TLV::TLVReader & reader, size_t index) const
79 : {
80 254 : VerifyOrReturnError(!mResponses.empty(), CHIP_ERROR_INCORRECT_STATE);
81 254 : VerifyOrReturnError(index < mResponses.size(), CHIP_ERROR_INVALID_ARGUMENT);
82 254 : VerifyOrReturnError(!mResponses[index].encodedData.IsNull(), CHIP_ERROR_INCORRECT_STATE);
83 :
84 254 : reader.Init(mResponses[index].encodedData->Start(), mResponses[index].encodedData->DataLength());
85 254 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag()));
86 :
87 : TLV::TLVType outerContainer;
88 254 : ReturnErrorOnFailure(reader.EnterContainer(outerContainer));
89 254 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::ContextTag(app::CommandDataIB::Tag::kFields)));
90 :
91 254 : return CHIP_NO_ERROR;
92 : }
93 :
94 : } // namespace Testing
95 : } // namespace chip
|