Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / protocols / bdx / tests / TestBdxMessages.cpp
1 #include <protocols/bdx/BdxMessages.h>
2
3 #include <nlunit-test.h>
4
5 #include <support/BufferWriter.h>
6 #include <support/CHIPMem.h>
7 #include <support/CodeUtils.h>
8 #include <support/UnitTestRegistration.h>
9
10 #include <limits>
11
12 using namespace chip;
13 using namespace chip::bdx;
14
15 /**
16  * Helper method for testing that WriteToBuffer() and Parse() are successful, and that the parsed message
17  * is identical to the origianl.
18  */
19 template <class MsgType>
20 void TestHelperWrittenAndParsedMatch(nlTestSuite * inSuite, void * inContext, MsgType & testMsg)
21 {
22     CHIP_ERROR err = CHIP_NO_ERROR;
23
24     size_t msgSize = testMsg.MessageSize();
25     Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
26     NL_TEST_ASSERT(inSuite, !bbuf.IsNull());
27
28     testMsg.WriteToBuffer(bbuf);
29     NL_TEST_ASSERT(inSuite, bbuf.Fit());
30
31     System::PacketBufferHandle msgBuf = bbuf.Finalize();
32     NL_TEST_ASSERT(inSuite, !msgBuf.IsNull());
33     System::PacketBufferHandle rcvBuf = System::PacketBufferHandle::NewWithData(msgBuf->Start(), msgSize);
34     NL_TEST_ASSERT(inSuite, !rcvBuf.IsNull());
35
36     MsgType testMsgRcvd;
37     err = testMsgRcvd.Parse(std::move(rcvBuf));
38     NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
39     NL_TEST_ASSERT(inSuite, testMsgRcvd == testMsg);
40 }
41
42 void TestTransferInitMessage(nlTestSuite * inSuite, void * inContext)
43 {
44     TransferInit testMsg;
45
46     testMsg.TransferCtlOptions.ClearAll().Set(TransferControlFlags::kReceiverDrive, true);
47     testMsg.Version = 1;
48
49     // Make sure MaxLength is greater than UINT32_MAX to test widerange being set
50     testMsg.MaxLength = static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1;
51
52     testMsg.StartOffset  = 42;
53     testMsg.MaxBlockSize = 256;
54
55     char testFileDes[9]    = { "test.txt" };
56     testMsg.FileDesLength  = 9;
57     testMsg.FileDesignator = reinterpret_cast<uint8_t *>(testFileDes);
58
59     uint8_t fakeData[5]    = { 7, 6, 5, 4, 3 };
60     testMsg.MetadataLength = 5;
61     testMsg.Metadata       = reinterpret_cast<uint8_t *>(fakeData);
62
63     TestHelperWrittenAndParsedMatch<TransferInit>(inSuite, inContext, testMsg);
64 }
65
66 void TestSendAcceptMessage(nlTestSuite * inSuite, void * inContext)
67 {
68     SendAccept testMsg;
69
70     testMsg.Version = 1;
71     testMsg.TransferCtlFlags.ClearAll().Set(TransferControlFlags::kReceiverDrive, true);
72     testMsg.MaxBlockSize = 256;
73
74     uint8_t fakeData[5]    = { 7, 6, 5, 4, 3 };
75     testMsg.MetadataLength = 5;
76     testMsg.Metadata       = reinterpret_cast<uint8_t *>(fakeData);
77
78     TestHelperWrittenAndParsedMatch<SendAccept>(inSuite, inContext, testMsg);
79 }
80
81 void TestReceiveAcceptMessage(nlTestSuite * inSuite, void * inContext)
82 {
83     ReceiveAccept testMsg;
84
85     testMsg.Version = 1;
86     testMsg.TransferCtlFlags.ClearAll().Set(TransferControlFlags::kReceiverDrive, true);
87
88     // Make sure Length is greater than UINT32_MAX to test widerange being set
89     testMsg.Length = static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1;
90
91     testMsg.StartOffset  = 42;
92     testMsg.MaxBlockSize = 256;
93
94     uint8_t fakeData[5]    = { 7, 6, 5, 4, 3 };
95     testMsg.MetadataLength = 5;
96     testMsg.Metadata       = reinterpret_cast<uint8_t *>(fakeData);
97
98     TestHelperWrittenAndParsedMatch<ReceiveAccept>(inSuite, inContext, testMsg);
99 }
100
101 void TestCounterMessage(nlTestSuite * inSuite, void * inContext)
102 {
103     CounterMessage testMsg;
104
105     testMsg.BlockCounter = 4;
106
107     TestHelperWrittenAndParsedMatch<CounterMessage>(inSuite, inContext, testMsg);
108 }
109
110 void TestDataBlockMessage(nlTestSuite * inSuite, void * inContext)
111 {
112     DataBlock testMsg;
113
114     testMsg.BlockCounter = 4;
115     uint8_t fakeData[5]  = { 7, 6, 5, 4, 3 };
116     testMsg.DataLength   = 5;
117     testMsg.Data         = reinterpret_cast<uint8_t *>(fakeData);
118
119     TestHelperWrittenAndParsedMatch<DataBlock>(inSuite, inContext, testMsg);
120 }
121
122 // Test Suite
123
124 /**
125  *  Test Suite that lists all the test functions.
126  */
127 // clang-format off
128 static const nlTest sTests[] =
129 {
130     NL_TEST_DEF("TestTransferInitMessage", TestTransferInitMessage),
131     NL_TEST_DEF("TestSendAcceptMessage", TestSendAcceptMessage),
132     NL_TEST_DEF("TestReceiveAcceptMessage", TestReceiveAcceptMessage),
133     NL_TEST_DEF("TestCounterMessage", TestCounterMessage),
134     NL_TEST_DEF("TestDataBlockMessage", TestDataBlockMessage),
135
136     NL_TEST_SENTINEL()
137 };
138 // clang-format on
139
140 /**
141  *  Set up the test suite.
142  */
143 static int TestSetup(void * inContext)
144 {
145     CHIP_ERROR error = chip::Platform::MemoryInit();
146     if (error != CHIP_NO_ERROR)
147         return FAILURE;
148     return SUCCESS;
149 }
150
151 /**
152  *  Tear down the test suite.
153  */
154 static int TestTeardown(void * inContext)
155 {
156     chip::Platform::MemoryShutdown();
157     return SUCCESS;
158 }
159
160 // clang-format off
161 static nlTestSuite sSuite =
162 {
163     "Test-CHIP-BdxMessages",
164     &sTests[0],
165     TestSetup,
166     TestTeardown,
167 };
168 // clang-format on
169
170 /**
171  *  Main
172  */
173 int TestBdxMessages()
174 {
175     // Run test suit against one context
176     nlTestRunner(&sSuite, nullptr);
177
178     return (nlTestRunnerStats(&sSuite));
179 }
180
181 CHIP_REGISTER_TEST_SUITE(TestBdxMessages)