1 /*
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"
37 #include "common/heap_data.hpp"
38 #include "common/heap_string.hpp"
39
40 namespace ot {
41
PrintString(const char * aName,const Heap::String & aString)42 void PrintString(const char *aName, const Heap::String &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
VerifyString(const char * aName,const Heap::String & aString,const char * aExpectedString)54 void VerifyString(const char *aName, const Heap::String &aString, const char *aExpectedString)
55 {
56 PrintString(aName, aString);
57
58 if (aExpectedString == nullptr)
59 {
60 VerifyOrQuit(aString.IsNull());
61 VerifyOrQuit(aString.AsCString() == nullptr);
62 VerifyOrQuit(aString != "something");
63 }
64 else
65 {
66 VerifyOrQuit(!aString.IsNull());
67 VerifyOrQuit(aString.AsCString() != nullptr);
68 VerifyOrQuit(strcmp(aString.AsCString(), aExpectedString) == 0, "String content is incorrect");
69 VerifyOrQuit(aString != nullptr);
70 }
71
72 VerifyOrQuit(aString == aExpectedString);
73 }
74
75 // Function returning a `Heap::String` by value.
GetName(void)76 Heap::String GetName(void)
77 {
78 Heap::String name;
79
80 SuccessOrQuit(name.Set("name"));
81
82 return name;
83 }
84
TestHeapString(void)85 void TestHeapString(void)
86 {
87 Heap::String str1;
88 Heap::String str2;
89 const char *oldBuffer;
90
91 printf("====================================================================================\n");
92 printf("TestHeapString\n\n");
93
94 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");
100 SuccessOrQuit(str1.Set("hello"));
101 VerifyString("str1", str1, "hello");
102 oldBuffer = str1.AsCString();
103
104 SuccessOrQuit(str1.Set("0123456789"));
105 VerifyString("str1", str1, "0123456789");
106 printf("\tDid reuse its old buffer: %s\n", str1.AsCString() == oldBuffer ? "yes" : "no");
107 oldBuffer = str1.AsCString();
108
109 SuccessOrQuit(str1.Set("9876543210"));
110 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 Heap::String &)\n\n");
115 SuccessOrQuit(str2.Set(str1));
116 VerifyString("str2", str2, str1.AsCString());
117
118 SuccessOrQuit(str1.Set(nullptr));
119 VerifyString("str1", str1, nullptr);
120
121 SuccessOrQuit(str2.Set(str1));
122 VerifyString("str2", str2, nullptr);
123
124 printf("------------------------------------------------------------------------------------\n");
125 printf("Free()\n\n");
126 str1.Free();
127 VerifyString("str1", str1, nullptr);
128
129 SuccessOrQuit(str1.Set("hello again"));
130 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");
137 SuccessOrQuit(str1.Set("old name"));
138 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
PrintData(const Heap::Data & aData)153 void PrintData(const Heap::Data &aData) { DumpBuffer("data", aData.GetBytes(), aData.GetLength()); }
154
155 static const uint8_t kTestValue = 0x77;
156
157 // Function returning a `Heap::Data` by value.
GetData(void)158 Heap::Data GetData(void)
159 {
160 Heap::Data data;
161
162 SuccessOrQuit(data.SetFrom(&kTestValue, sizeof(kTestValue)));
163
164 return data;
165 }
166
VerifyData(const Heap::Data & aData,const uint8_t * aBytes,uint16_t aLength)167 void VerifyData(const Heap::Data &aData, const uint8_t *aBytes, uint16_t aLength)
168 {
169 static constexpr uint16_t kMaxLength = 100;
170 uint8_t buffer[kMaxLength];
171
172 PrintData(aData);
173
174 VerifyOrQuit(aData.Matches(aBytes, aLength));
175 VerifyOrQuit(!aData.Matches(aBytes, aLength + 1));
176
177 if (aLength == 0)
178 {
179 VerifyOrQuit(aData.IsNull());
180 VerifyOrQuit(aData.GetBytes() == nullptr);
181 VerifyOrQuit(aData.GetLength() == 0);
182 VerifyOrQuit(aData.Matches(nullptr, 0));
183 }
184 else
185 {
186 VerifyOrQuit(!aData.IsNull());
187 VerifyOrQuit(aData.GetBytes() != nullptr);
188 VerifyOrQuit(aData.GetLength() == aLength);
189 VerifyOrQuit(memcmp(aData.GetBytes(), aBytes, aLength) == 0, "Data content is incorrect");
190
191 aData.CopyBytesTo(buffer);
192 VerifyOrQuit(memcmp(buffer, aBytes, aLength) == 0, "CopyBytesTo() failed");
193
194 VerifyOrQuit(aData.Matches(buffer, aLength));
195 buffer[aLength - 1]++;
196 VerifyOrQuit(!aData.Matches(buffer, aLength));
197 }
198 }
199
VerifyData(const Heap::Data & aData,const uint8_t (& aArray)[kLength])200 template <uint16_t kLength> void VerifyData(const Heap::Data &aData, const uint8_t (&aArray)[kLength])
201 {
202 VerifyData(aData, &aArray[0], kLength);
203 }
204
TestHeapData(void)205 void TestHeapData(void)
206 {
207 Instance *instance;
208 MessagePool *messagePool;
209 Message *message;
210 Heap::Data data;
211 uint16_t offset;
212 const uint8_t *oldBuffer;
213
214 static const uint8_t kData1[] = {10, 20, 3, 15, 100, 0, 60, 16};
215 static const uint8_t kData2[] = "OpenThread HeapData";
216 static const uint8_t kData3[] = {0xaa, 0xbb, 0xcc};
217 static const uint8_t kData4[] = {0x11, 0x22, 0x33};
218
219 instance = static_cast<Instance *>(testInitInstance());
220 VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
221
222 messagePool = &instance->Get<MessagePool>();
223 VerifyOrQuit((message = messagePool->Allocate(Message::kTypeIp6)) != nullptr);
224
225 message->SetOffset(0);
226
227 printf("\n\n====================================================================================\n");
228 printf("TestHeapData\n\n");
229
230 printf("------------------------------------------------------------------------------------\n");
231 printf("After constructor\n");
232 VerifyData(data, nullptr, 0);
233
234 VerifyOrQuit(data.Matches(nullptr, 0));
235 VerifyOrQuit(data.Matches(kData1, 0));
236 VerifyOrQuit(!data.Matches(kData1, 1));
237
238 printf("------------------------------------------------------------------------------------\n");
239 printf("SetFrom(aBuffer, aLength)\n");
240
241 SuccessOrQuit(data.SetFrom(kData1, sizeof(kData1)));
242 VerifyData(data, kData1);
243
244 SuccessOrQuit(data.SetFrom(kData2, sizeof(kData2)));
245 VerifyData(data, kData2);
246
247 SuccessOrQuit(data.SetFrom(kData3, sizeof(kData3)));
248 VerifyData(data, kData3);
249 oldBuffer = data.GetBytes();
250
251 SuccessOrQuit(data.SetFrom(kData4, sizeof(kData4)));
252 VerifyData(data, kData4);
253 VerifyOrQuit(oldBuffer == data.GetBytes(), "did not reuse old buffer on same data length");
254
255 SuccessOrQuit(data.SetFrom(kData4, 0));
256 VerifyData(data, nullptr, 0);
257
258 printf("------------------------------------------------------------------------------------\n");
259 printf("SetFrom(aMessage)\n");
260
261 SuccessOrQuit(message->Append(kData2));
262 SuccessOrQuit(data.SetFrom(*message));
263 VerifyData(data, kData2);
264
265 SuccessOrQuit(message->Append(kData3));
266 SuccessOrQuit(data.SetFrom(*message));
267 PrintData(data);
268 VerifyOrQuit(data.GetLength() == message->GetLength());
269
270 message->SetOffset(sizeof(kData2));
271 SuccessOrQuit(data.SetFrom(*message));
272 VerifyData(data, kData3);
273
274 SuccessOrQuit(message->Append(kData4));
275
276 offset = 0;
277 SuccessOrQuit(data.SetFrom(*message, offset, sizeof(kData2)));
278 VerifyData(data, kData2);
279
280 offset = sizeof(kData2);
281 SuccessOrQuit(data.SetFrom(*message, offset, sizeof(kData3)));
282 VerifyData(data, kData3);
283
284 offset += sizeof(kData3);
285 SuccessOrQuit(data.SetFrom(*message, offset, sizeof(kData4)));
286 VerifyData(data, kData4);
287
288 VerifyOrQuit(data.SetFrom(*message, offset, sizeof(kData4) + 1) == kErrorParse);
289 VerifyOrQuit(data.SetFrom(*message, 0, message->GetLength() + 1) == kErrorParse);
290 VerifyOrQuit(data.SetFrom(*message, 1, message->GetLength()) == kErrorParse);
291
292 printf("------------------------------------------------------------------------------------\n");
293 printf("Free()\n");
294
295 data.Free();
296 VerifyData(data, nullptr, 0);
297
298 data.Free();
299 VerifyData(data, nullptr, 0);
300
301 printf("------------------------------------------------------------------------------------\n");
302 printf("CopyBytesTo(aMessage)\n");
303
304 SuccessOrQuit(message->SetLength(0));
305
306 SuccessOrQuit(data.CopyBytesTo(*message));
307 VerifyOrQuit(message->GetLength() == 0, "CopyBytesTo() failed");
308
309 SuccessOrQuit(data.SetFrom(kData1, sizeof(kData1)));
310 VerifyData(data, kData1);
311 SuccessOrQuit(data.CopyBytesTo(*message));
312 VerifyOrQuit(message->GetLength() == data.GetLength(), "CopyBytesTo() failed");
313 VerifyOrQuit(message->Compare(0, kData1), "CopyBytesTo() failed");
314
315 printf("------------------------------------------------------------------------------------\n");
316 printf("SetFrom() move semantics\n\n");
317 data.SetFrom(GetData());
318 VerifyData(data, &kTestValue, sizeof(kTestValue));
319
320 printf("\n -- PASS\n");
321 }
322
323 } // namespace ot
324
main(void)325 int main(void)
326 {
327 ot::TestHeapString();
328 ot::TestHeapData();
329 printf("\nAll tests passed.\n");
330 return 0;
331 }
332