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)
154 {
155 DumpBuffer("data", aData.GetBytes(), aData.GetLength());
156 }
157
158 static const uint8_t kTestValue = 0x77;
159
160 // Function returning a `Heap::Data` by value.
GetData(void)161 Heap::Data GetData(void)
162 {
163 Heap::Data data;
164
165 SuccessOrQuit(data.SetFrom(&kTestValue, sizeof(kTestValue)));
166
167 return data;
168 }
169
VerifyData(const Heap::Data & aData,const uint8_t * aBytes,uint16_t aLength)170 void VerifyData(const Heap::Data &aData, const uint8_t *aBytes, uint16_t aLength)
171 {
172 static constexpr uint16_t kMaxLength = 100;
173 uint8_t buffer[kMaxLength];
174
175 PrintData(aData);
176
177 if (aLength == 0)
178 {
179 VerifyOrQuit(aData.IsNull());
180 VerifyOrQuit(aData.GetBytes() == nullptr);
181 VerifyOrQuit(aData.GetLength() == 0);
182 }
183 else
184 {
185 VerifyOrQuit(!aData.IsNull());
186 VerifyOrQuit(aData.GetBytes() != nullptr);
187 VerifyOrQuit(aData.GetLength() == aLength);
188 VerifyOrQuit(memcmp(aData.GetBytes(), aBytes, aLength) == 0, "Data content is incorrect");
189
190 aData.CopyBytesTo(buffer);
191 VerifyOrQuit(memcmp(buffer, aBytes, aLength) == 0, "CopyBytesTo() failed");
192 }
193 }
194
VerifyData(const Heap::Data & aData,const uint8_t (& aArray)[kLength])195 template <uint16_t kLength> void VerifyData(const Heap::Data &aData, const uint8_t (&aArray)[kLength])
196 {
197 VerifyData(aData, &aArray[0], kLength);
198 }
199
TestHeapData(void)200 void TestHeapData(void)
201 {
202 Instance * instance;
203 MessagePool * messagePool;
204 Message * message;
205 Heap::Data data;
206 uint16_t offset;
207 const uint8_t *oldBuffer;
208
209 static const uint8_t kData1[] = {10, 20, 3, 15, 100, 0, 60, 16};
210 static const uint8_t kData2[] = "OpenThread HeapData";
211 static const uint8_t kData3[] = {0xaa, 0xbb, 0xcc};
212 static const uint8_t kData4[] = {0x11, 0x22, 0x33};
213
214 instance = static_cast<Instance *>(testInitInstance());
215 VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
216
217 messagePool = &instance->Get<MessagePool>();
218 VerifyOrQuit((message = messagePool->Allocate(Message::kTypeIp6)) != nullptr);
219
220 message->SetOffset(0);
221
222 printf("\n\n====================================================================================\n");
223 printf("TestHeapData\n\n");
224
225 printf("------------------------------------------------------------------------------------\n");
226 printf("After constructor\n");
227 VerifyData(data, nullptr, 0);
228
229 printf("------------------------------------------------------------------------------------\n");
230 printf("SetFrom(aBuffer, aLength)\n");
231
232 SuccessOrQuit(data.SetFrom(kData1, sizeof(kData1)));
233 VerifyData(data, kData1);
234
235 SuccessOrQuit(data.SetFrom(kData2, sizeof(kData2)));
236 VerifyData(data, kData2);
237
238 SuccessOrQuit(data.SetFrom(kData3, sizeof(kData3)));
239 VerifyData(data, kData3);
240 oldBuffer = data.GetBytes();
241
242 SuccessOrQuit(data.SetFrom(kData4, sizeof(kData4)));
243 VerifyData(data, kData4);
244 VerifyOrQuit(oldBuffer == data.GetBytes(), "did not reuse old buffer on same data length");
245
246 SuccessOrQuit(data.SetFrom(kData4, 0));
247 VerifyData(data, nullptr, 0);
248
249 printf("------------------------------------------------------------------------------------\n");
250 printf("SetFrom(aMessage)\n");
251
252 SuccessOrQuit(message->Append(kData2));
253 SuccessOrQuit(data.SetFrom(*message));
254 VerifyData(data, kData2);
255
256 SuccessOrQuit(message->Append(kData3));
257 SuccessOrQuit(data.SetFrom(*message));
258 PrintData(data);
259 VerifyOrQuit(data.GetLength() == message->GetLength());
260
261 message->SetOffset(sizeof(kData2));
262 SuccessOrQuit(data.SetFrom(*message));
263 VerifyData(data, kData3);
264
265 SuccessOrQuit(message->Append(kData4));
266
267 offset = 0;
268 SuccessOrQuit(data.SetFrom(*message, offset, sizeof(kData2)));
269 VerifyData(data, kData2);
270
271 offset = sizeof(kData2);
272 SuccessOrQuit(data.SetFrom(*message, offset, sizeof(kData3)));
273 VerifyData(data, kData3);
274
275 offset += sizeof(kData3);
276 SuccessOrQuit(data.SetFrom(*message, offset, sizeof(kData4)));
277 VerifyData(data, kData4);
278
279 VerifyOrQuit(data.SetFrom(*message, offset, sizeof(kData4) + 1) == kErrorParse);
280 VerifyOrQuit(data.SetFrom(*message, 0, message->GetLength() + 1) == kErrorParse);
281 VerifyOrQuit(data.SetFrom(*message, 1, message->GetLength()) == kErrorParse);
282
283 printf("------------------------------------------------------------------------------------\n");
284 printf("Free()\n");
285
286 data.Free();
287 VerifyData(data, nullptr, 0);
288
289 data.Free();
290 VerifyData(data, nullptr, 0);
291
292 printf("------------------------------------------------------------------------------------\n");
293 printf("CopyBytesTo(aMessage)\n");
294
295 SuccessOrQuit(message->SetLength(0));
296
297 SuccessOrQuit(data.CopyBytesTo(*message));
298 VerifyOrQuit(message->GetLength() == 0, "CopyBytesTo() failed");
299
300 SuccessOrQuit(data.SetFrom(kData1, sizeof(kData1)));
301 VerifyData(data, kData1);
302 SuccessOrQuit(data.CopyBytesTo(*message));
303 VerifyOrQuit(message->GetLength() == data.GetLength(), "CopyBytesTo() failed");
304 VerifyOrQuit(message->Compare(0, kData1), "CopyBytesTo() failed");
305
306 printf("------------------------------------------------------------------------------------\n");
307 printf("SetFrom() move semantics\n\n");
308 data.SetFrom(GetData());
309 VerifyData(data, &kTestValue, sizeof(kTestValue));
310
311 printf("\n -- PASS\n");
312 }
313
314 } // namespace ot
315
main(void)316 int main(void)
317 {
318 ot::TestHeapString();
319 ot::TestHeapData();
320 printf("\nAll tests passed.\n");
321 return 0;
322 }
323