blob: d3e4c115f6ca3a8b07c559e49815a91f0ddcd1c3 [file] [log] [blame]
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -07001/*
2 * Copyright (c) 2021, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "test_platform.h"
30
31#include <string.h>
32
33#include <openthread/config.h>
34
35#include "test_util.hpp"
36#include "common/code_utils.hpp"
Abtin Keshavarzian2fb17562021-11-10 09:28:01 -080037#include "common/heap_data.hpp"
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070038#include "common/heap_string.hpp"
39
40namespace ot {
41
42void PrintString(const char *aName, const HeapString &aString)
43{
44 if (aString.IsNull())
45 {
46 printf("%s = (null)\n", aName);
47 }
48 else
49 {
50 printf("%s = [%zu] \"%s\"\n", aName, strlen(aString.AsCString()), aString.AsCString());
51 }
52}
53
54void VerifyString(const char *aName, const HeapString &aString, const char *aExpectedString)
55{
56 PrintString(aName, aString);
57
58 if (aExpectedString == nullptr)
59 {
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -070060 VerifyOrQuit(aString.IsNull());
61 VerifyOrQuit(aString.AsCString() == nullptr);
62 VerifyOrQuit(aString != "something");
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070063 }
64 else
65 {
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -070066 VerifyOrQuit(!aString.IsNull());
67 VerifyOrQuit(aString.AsCString() != nullptr);
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070068 VerifyOrQuit(strcmp(aString.AsCString(), aExpectedString) == 0, "String content is incorrect");
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -070069 VerifyOrQuit(aString != nullptr);
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070070 }
71
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -070072 VerifyOrQuit(aString == aExpectedString);
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070073}
74
75// Function returning a `HeapString` by value.
76HeapString GetName(void)
77{
78 HeapString name;
79
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -070080 SuccessOrQuit(name.Set("name"));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070081
82 return name;
83}
84
85void TestHeapString(void)
86{
87 HeapString str1;
88 HeapString str2;
89 const char *oldBuffer;
90
Abtin Keshavarzian2fb17562021-11-10 09:28:01 -080091 printf("====================================================================================\n");
92 printf("TestHeapString\n\n");
93
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -070094 printf("------------------------------------------------------------------------------------\n");
95 printf("After constructor\n\n");
96 VerifyString("str1", str1, nullptr);
97
98 printf("------------------------------------------------------------------------------------\n");
99 printf("Set(const char *aCstring)\n\n");
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700100 SuccessOrQuit(str1.Set("hello"));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700101 VerifyString("str1", str1, "hello");
102 oldBuffer = str1.AsCString();
103
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700104 SuccessOrQuit(str1.Set("0123456789"));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700105 VerifyString("str1", str1, "0123456789");
106 printf("\tDid reuse its old buffer: %s\n", str1.AsCString() == oldBuffer ? "yes" : "no");
107 oldBuffer = str1.AsCString();
108
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700109 SuccessOrQuit(str1.Set("9876543210"));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700110 VerifyString("str1", str1, "9876543210");
111 printf("\tDid reuse its old buffer (same length): %s\n", str1.AsCString() == oldBuffer ? "yes" : "no");
112
113 printf("------------------------------------------------------------------------------------\n");
114 printf("Set(const HeapString &)\n\n");
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700115 SuccessOrQuit(str2.Set(str1));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700116 VerifyString("str2", str2, str1.AsCString());
117
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700118 SuccessOrQuit(str1.Set(nullptr));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700119 VerifyString("str1", str1, nullptr);
120
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700121 SuccessOrQuit(str2.Set(str1));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700122 VerifyString("str2", str2, nullptr);
123
124 printf("------------------------------------------------------------------------------------\n");
125 printf("Free()\n\n");
126 str1.Free();
127 VerifyString("str1", str1, nullptr);
128
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700129 SuccessOrQuit(str1.Set("hello again"));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700130 VerifyString("str1", str1, "hello again");
131
132 str1.Free();
133 VerifyString("str1", str1, nullptr);
134
135 printf("------------------------------------------------------------------------------------\n");
136 printf("Set() move semantics\n\n");
Abtin Keshavarzian57d072d2021-06-28 11:38:10 -0700137 SuccessOrQuit(str1.Set("old name"));
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700138 PrintString("str1", str1);
139 SuccessOrQuit(str1.Set(GetName()), "Set() with move semantics failed");
140 VerifyString("str1", str1, "name");
141
142 printf("------------------------------------------------------------------------------------\n");
143 printf("operator==() with two null string\n\n");
144 str1.Free();
145 str2.Free();
146 VerifyString("str1", str1, nullptr);
147 VerifyString("str2", str2, nullptr);
148 VerifyOrQuit(str1 == str2, "operator==() failed with two null strings");
149
150 printf("\n -- PASS\n");
151}
152
Abtin Keshavarzian2fb17562021-11-10 09:28:01 -0800153void PrintData(const HeapData &aData)
154{
155 DumpBuffer("data", aData.GetBytes(), aData.GetLength());
156}
157
158template <uint16_t kLength> void VerifyData(const HeapData &aData, const uint8_t (&aArray)[kLength])
159{
160 VerifyData(aData, &aArray[0], kLength);
161}
162
163static const uint8_t kTestValue = 0x77;
164
165// Function returning a `HeapData` by value.
166HeapData GetData(void)
167{
168 HeapData data;
169
170 SuccessOrQuit(data.SetFrom(&kTestValue, sizeof(kTestValue)));
171
172 return data;
173}
174
175void VerifyData(const HeapData &aData, const uint8_t *aBytes, uint16_t aLength)
176{
177 static constexpr uint16_t kMaxLength = 100;
178 uint8_t buffer[kMaxLength];
179
180 PrintData(aData);
181
182 if (aLength == 0)
183 {
184 VerifyOrQuit(aData.IsNull());
185 VerifyOrQuit(aData.GetBytes() == nullptr);
186 VerifyOrQuit(aData.GetLength() == 0);
187 }
188 else
189 {
190 VerifyOrQuit(!aData.IsNull());
191 VerifyOrQuit(aData.GetBytes() != nullptr);
192 VerifyOrQuit(aData.GetLength() == aLength);
193 VerifyOrQuit(memcmp(aData.GetBytes(), aBytes, aLength) == 0, "Data content is incorrect");
194
195 aData.CopyBytesTo(buffer);
196 VerifyOrQuit(memcmp(buffer, aBytes, aLength) == 0, "CopyBytesTo() failed");
197 }
198}
199
200void TestHeapData(void)
201{
202 Instance * instance;
203 MessagePool * messagePool;
204 Message * message;
205 HeapData data;
206 const uint8_t *oldBuffer;
207
208 static const uint8_t kData1[] = {10, 20, 3, 15, 100, 0, 60, 16};
209 static const uint8_t kData2[] = "OpenThread HeapData";
210 static const uint8_t kData3[] = {0xaa, 0xbb, 0xcc};
211 static const uint8_t kData4[] = {0x11, 0x22, 0x33};
212
213 instance = static_cast<Instance *>(testInitInstance());
214 VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
215
216 messagePool = &instance->Get<MessagePool>();
217 VerifyOrQuit((message = messagePool->New(Message::kTypeIp6, 0)) != nullptr, "Message::New failed");
218
219 message->SetOffset(0);
220
221 printf("\n\n====================================================================================\n");
222 printf("TestHeapData\n\n");
223
224 printf("------------------------------------------------------------------------------------\n");
225 printf("After constructor\n");
226 VerifyData(data, nullptr, 0);
227
228 printf("------------------------------------------------------------------------------------\n");
229 printf("SetFrom(aBuffer, aLength)\n");
230
231 SuccessOrQuit(data.SetFrom(kData1, sizeof(kData1)));
232 VerifyData(data, kData1);
233
234 SuccessOrQuit(data.SetFrom(kData2, sizeof(kData2)));
235 VerifyData(data, kData2);
236
237 SuccessOrQuit(data.SetFrom(kData3, sizeof(kData3)));
238 VerifyData(data, kData3);
239 oldBuffer = data.GetBytes();
240
241 SuccessOrQuit(data.SetFrom(kData4, sizeof(kData4)));
242 VerifyData(data, kData4);
243 VerifyOrQuit(oldBuffer == data.GetBytes(), "did not reuse old buffer on same data length");
244
245 SuccessOrQuit(data.SetFrom(kData4, 0));
246 VerifyData(data, nullptr, 0);
247
248 printf("------------------------------------------------------------------------------------\n");
249 printf("SetFrom(aMessage)\n");
250
251 SuccessOrQuit(message->Append(kData2));
252 SuccessOrQuit(data.SetFrom(*message));
253 VerifyData(data, kData2);
254
255 SuccessOrQuit(message->Append(kData3));
256 SuccessOrQuit(data.SetFrom(*message));
257 PrintData(data);
258 VerifyOrQuit(data.GetLength() == message->GetLength());
259
260 message->SetOffset(sizeof(kData2));
261 SuccessOrQuit(data.SetFrom(*message));
262 VerifyData(data, kData3);
263
264 printf("------------------------------------------------------------------------------------\n");
265 printf("Free()\n");
266
267 data.Free();
268 VerifyData(data, nullptr, 0);
269
270 data.Free();
271 VerifyData(data, nullptr, 0);
272
273 printf("------------------------------------------------------------------------------------\n");
274 printf("CopyBytesTo(aMessage)\n");
275
276 SuccessOrQuit(message->SetLength(0));
277
278 SuccessOrQuit(data.CopyBytesTo(*message));
279 VerifyOrQuit(message->GetLength() == 0, "CopyBytesTo() failed");
280
281 SuccessOrQuit(data.SetFrom(kData1, sizeof(kData1)));
282 VerifyData(data, kData1);
283 SuccessOrQuit(data.CopyBytesTo(*message));
284 VerifyOrQuit(message->GetLength() == data.GetLength(), "CopyBytesTo() failed");
285 VerifyOrQuit(message->Compare(0, kData1), "CopyBytesTo() failed");
286
287 printf("------------------------------------------------------------------------------------\n");
288 printf("SetFrom() move semantics\n\n");
289 data.SetFrom(GetData());
290 VerifyData(data, &kTestValue, sizeof(kTestValue));
291
292 printf("\n -- PASS\n");
293}
294
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700295} // namespace ot
296
297int main(void)
298{
299 ot::TestHeapString();
Abtin Keshavarzian2fb17562021-11-10 09:28:01 -0800300 ot::TestHeapData();
Abtin Keshavarzian2b2b3902021-06-14 22:48:43 -0700301 printf("\nAll tests passed.\n");
302 return 0;
303}