3 * Copyright (c) 2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include <protocols/Protocols.h>
20 #include <protocols/secure_channel/Constants.h>
21 #include <protocols/secure_channel/StatusReport.h>
22 #include <support/BufferWriter.h>
23 #include <support/CHIPMem.h>
24 #include <support/UnitTestRegistration.h>
25 #include <system/SystemPacketBuffer.h>
27 #include <nlunit-test.h>
30 using namespace chip::Protocols;
31 using namespace chip::Protocols::SecureChannel;
33 void TestStatusReport_NoData(nlTestSuite * inSuite, void * inContext)
35 GeneralStatusCode generalCode = GeneralStatusCode::kSuccess;
36 uint32_t protocolId = SecureChannel::Id.ToFullyQualifiedSpecForm();
37 uint16_t protocolCode = kProtocolCodeSuccess;
39 StatusReport testReport(generalCode, protocolId, protocolCode);
41 size_t msgSize = testReport.Size();
42 Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
43 testReport.WriteToBuffer(bbuf);
45 System::PacketBufferHandle msgBuf = bbuf.Finalize();
46 NL_TEST_ASSERT(inSuite, !msgBuf.IsNull());
48 StatusReport reportToParse;
49 CHIP_ERROR err = reportToParse.Parse(std::move(msgBuf));
50 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
51 NL_TEST_ASSERT(inSuite, reportToParse.GetGeneralCode() == generalCode);
52 NL_TEST_ASSERT(inSuite, reportToParse.GetProtocolId() == protocolId);
53 NL_TEST_ASSERT(inSuite, reportToParse.GetProtocolCode() == protocolCode);
55 const System::PacketBufferHandle & data = reportToParse.GetProtocolData();
56 NL_TEST_ASSERT(inSuite, data.IsNull());
59 void TestStatusReport_WithData(nlTestSuite * inSuite, void * inContext)
61 GeneralStatusCode generalCode = GeneralStatusCode::kFailure;
62 uint32_t protocolId = SecureChannel::Id.ToFullyQualifiedSpecForm();
63 uint16_t protocolCode = static_cast<uint16_t>(StatusCode::InvalidFabricConfig);
64 uint8_t data[6] = { 42, 19, 3, 1, 3, 0 };
65 const uint16_t dataLen = 6;
66 System::PacketBufferHandle dataBuf = System::PacketBufferHandle::NewWithData(data, dataLen);
68 StatusReport testReport(generalCode, protocolId, protocolCode, std::move(dataBuf));
70 size_t msgSize = testReport.Size();
71 Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
72 testReport.WriteToBuffer(bbuf);
74 System::PacketBufferHandle msgBuf = bbuf.Finalize();
75 NL_TEST_ASSERT(inSuite, !msgBuf.IsNull());
77 StatusReport reportToParse;
78 CHIP_ERROR err = reportToParse.Parse(std::move(msgBuf));
79 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
80 NL_TEST_ASSERT(inSuite, reportToParse.GetGeneralCode() == generalCode);
81 NL_TEST_ASSERT(inSuite, reportToParse.GetProtocolId() == protocolId);
82 NL_TEST_ASSERT(inSuite, reportToParse.GetProtocolCode() == protocolCode);
84 const System::PacketBufferHandle & rcvData = reportToParse.GetProtocolData();
87 NL_TEST_ASSERT(inSuite, false);
90 NL_TEST_ASSERT(inSuite, rcvData->DataLength() == dataLen);
91 NL_TEST_ASSERT(inSuite, !memcmp(rcvData->Start(), data, dataLen));
94 void TestBadStatusReport(nlTestSuite * inSuite, void * inContext)
97 System::PacketBufferHandle badMsg = System::PacketBufferHandle::New(10);
98 CHIP_ERROR err = report.Parse(std::move(badMsg));
99 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
101 StatusReport report2;
103 err = report2.Parse(std::move(badMsg));
104 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
110 * Test Suite that lists all the test functions.
113 static const nlTest sTests[] =
115 NL_TEST_DEF("TestStatusReport_NoData", TestStatusReport_NoData),
116 NL_TEST_DEF("TestStatusReport_WithData", TestStatusReport_WithData),
117 NL_TEST_DEF("TestBadStatusReport", TestBadStatusReport),
124 * Set up the test suite.
126 static int TestSetup(void * inContext)
128 CHIP_ERROR error = chip::Platform::MemoryInit();
129 if (error != CHIP_NO_ERROR)
135 * Tear down the test suite.
137 static int TestTeardown(void * inContext)
139 chip::Platform::MemoryShutdown();
144 static nlTestSuite sSuite =
146 "Test-CHIP-StatusReport",
156 int TestStatusReport()
158 // Run test suit against one context
159 nlTestRunner(&sSuite, nullptr);
161 return (nlTestRunnerStats(&sSuite));
164 CHIP_REGISTER_TEST_SUITE(TestStatusReport)