Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / protocols / secure_channel / tests / TestStatusReport.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
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>
26
27 #include <nlunit-test.h>
28
29 using namespace chip;
30 using namespace chip::Protocols;
31 using namespace chip::Protocols::SecureChannel;
32
33 void TestStatusReport_NoData(nlTestSuite * inSuite, void * inContext)
34 {
35     GeneralStatusCode generalCode = GeneralStatusCode::kSuccess;
36     uint32_t protocolId           = SecureChannel::Id.ToFullyQualifiedSpecForm();
37     uint16_t protocolCode         = kProtocolCodeSuccess;
38
39     StatusReport testReport(generalCode, protocolId, protocolCode);
40
41     size_t msgSize = testReport.Size();
42     Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
43     testReport.WriteToBuffer(bbuf);
44
45     System::PacketBufferHandle msgBuf = bbuf.Finalize();
46     NL_TEST_ASSERT(inSuite, !msgBuf.IsNull());
47
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);
54
55     const System::PacketBufferHandle & data = reportToParse.GetProtocolData();
56     NL_TEST_ASSERT(inSuite, data.IsNull());
57 }
58
59 void TestStatusReport_WithData(nlTestSuite * inSuite, void * inContext)
60 {
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);
67
68     StatusReport testReport(generalCode, protocolId, protocolCode, std::move(dataBuf));
69
70     size_t msgSize = testReport.Size();
71     Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
72     testReport.WriteToBuffer(bbuf);
73
74     System::PacketBufferHandle msgBuf = bbuf.Finalize();
75     NL_TEST_ASSERT(inSuite, !msgBuf.IsNull());
76
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);
83
84     const System::PacketBufferHandle & rcvData = reportToParse.GetProtocolData();
85     if (rcvData.IsNull())
86     {
87         NL_TEST_ASSERT(inSuite, false);
88         return;
89     }
90     NL_TEST_ASSERT(inSuite, rcvData->DataLength() == dataLen);
91     NL_TEST_ASSERT(inSuite, !memcmp(rcvData->Start(), data, dataLen));
92 }
93
94 void TestBadStatusReport(nlTestSuite * inSuite, void * inContext)
95 {
96     StatusReport report;
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);
100
101     StatusReport report2;
102     badMsg = nullptr;
103     err    = report2.Parse(std::move(badMsg));
104     NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
105 }
106
107 // Test Suite
108
109 /**
110  *  Test Suite that lists all the test functions.
111  */
112 // clang-format off
113 static const nlTest sTests[] =
114 {
115     NL_TEST_DEF("TestStatusReport_NoData", TestStatusReport_NoData),
116     NL_TEST_DEF("TestStatusReport_WithData", TestStatusReport_WithData),
117     NL_TEST_DEF("TestBadStatusReport", TestBadStatusReport),
118
119     NL_TEST_SENTINEL()
120 };
121 // clang-format on
122
123 /**
124  *  Set up the test suite.
125  */
126 static int TestSetup(void * inContext)
127 {
128     CHIP_ERROR error = chip::Platform::MemoryInit();
129     if (error != CHIP_NO_ERROR)
130         return FAILURE;
131     return SUCCESS;
132 }
133
134 /**
135  *  Tear down the test suite.
136  */
137 static int TestTeardown(void * inContext)
138 {
139     chip::Platform::MemoryShutdown();
140     return SUCCESS;
141 }
142
143 // clang-format off
144 static nlTestSuite sSuite =
145 {
146     "Test-CHIP-StatusReport",
147     &sTests[0],
148     TestSetup,
149     TestTeardown,
150 };
151 // clang-format on
152
153 /**
154  *  Main
155  */
156 int TestStatusReport()
157 {
158     // Run test suit against one context
159     nlTestRunner(&sSuite, nullptr);
160
161     return (nlTestRunnerStats(&sSuite));
162 }
163
164 CHIP_REGISTER_TEST_SUITE(TestStatusReport)