blob: 4d7a277fa8e04f2b64059116ba40ea1e6872ce2f [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053030/*==============================================================================
31 Modifications beyond the version released on CAF are under the MIT license:
32
33 Copyright 2018 Laurence Lundblade
34
35 Permission is hereby granted, free of charge, to any person obtaining
36 a copy of this software and associated documentation files (the
37 "Software"), to deal in the Software without restriction, including
38 without limitation the rights to use, copy, modify, merge, publish,
39 distribute, sublicense, and/or sell copies of the Software, and to
40 permit persons to whom the Software is furnished to do so, subject to
41 the following conditions:
42
43 The above copyright notice and this permission notice shall be included
44 in all copies or substantial portions of the Software.
45
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 SOFTWARE.
54 ==============================================================================*/
55
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056#include "UsefulBuf.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057
58
59/* Basic exercise...
60
61 Call all the main public functions.
62
63 Binary compare the result to the expected.
64
65 There is nothing adversarial in this test
66 */
Laurence Lundblade7566b9f2018-10-12 09:13:32 +080067const char * UOBTest_NonAdversarial()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080068{
69 const char *szReturn = NULL;
70
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053071 UsefulBuf_MAKE_STACK_UB(outbuf,50);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080072
73 UsefulOutBuf UOB;
74
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080076
77 if(!UsefulOutBuf_AtStart(&UOB)) {
78 szReturn = "Not at start";
79 goto Done;
80 }
81
82 // Put 7 bytes at beginning of buf
83 UsefulOutBuf_AppendData(&UOB, "bluster", 7);
84
85 if(UsefulOutBuf_AtStart(&UOB)) {
86 szReturn = "At start";
87 goto Done;
88 }
89
90 // add a space to end
91 UsefulOutBuf_AppendByte(&UOB, ' ');
92
93 // Add 5 bytes to the end
94 UsefulBufC UBC = {"hunny", 5};
95 UsefulOutBuf_AppendUsefulBuf(&UOB, UBC);
96
97 // Insert 9 bytes at the beginning, slide the previous stuff right
98 UsefulOutBuf_InsertData(&UOB, "heffalump", 9, 0);
99 UsefulOutBuf_InsertByte(&UOB, ' ', 9);
100
101 // Put 9 bytes in at position 10 -- just after "heffalump "
102 UsefulBufC UBC2 = {"unbounce ", 9};
103 UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10);
104
105 // Make it a null terminated string (because all the appends and inserts above not strcpy !)
106 UsefulOutBuf_AppendByte(&UOB, '\0');
107
108
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530109 UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110
111 const char *expected = "heffalump unbounce bluster hunny";
112
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530113 if(UsefulBuf_IsNULLC(U) || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 szReturn = "OutUBuf";
115 }
116
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530117 UsefulBuf_MAKE_STACK_UB(buf, 50);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530118 UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530120 if(UsefulBuf_IsNULLC(Out) || Out.len-1 != strlen(expected) || strcmp(expected, Out.ptr)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 szReturn = "CopyOut";
122 }
123
124Done:
125 return szReturn;
126}
127
128
129/*
130 Append test utility.
131 pUOB is the buffer to append too
132 num is the amount to append
133 expected is the expected return code, 0 or 1
134
135 returns 0 if test passed
136
137 */
138static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
139{
140 //reset
141 UsefulOutBuf_Reset(pUOB);
142
143 // check status first
144 if(UsefulOutBuf_GetError(pUOB))
145 return 1;
146
147 // append the bytes
148 UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
149
150 // check error status after
151 if(UsefulOutBuf_GetError(pUOB) != expected)
152 return 1;
153
154 return 0;
155}
156
157
158/*
159 Same as append, but takes a position param too
160 */
161static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
162{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530163 // reset
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 UsefulOutBuf_Reset(pUOB);
165
166 // check
167 if(UsefulOutBuf_GetError(pUOB))
168 return 1;
169
170 UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
171
172 if(UsefulOutBuf_GetError(pUOB) != expected)
173 return 1;
174
175 return 0;
176}
177
178
179/*
180 Boundary conditions to test
181 - around 0
182 - around the buffer size
183 - around MAX size_t
184
185
186 Test these for the buffer size and the cursor, the insert amount, the append amount and the insert position
187
188 */
189
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800190const char *UOBTest_BoundaryConditionsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530192 UsefulBuf_MAKE_STACK_UB(outbuf,2);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193
194 UsefulOutBuf UOB;
195
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530196 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197
198 // append 0 byte to a 2 byte buffer --> success
199 if(AppendTest(&UOB, 0, 0))
200 return "Append 0 bytes failed";
201
202 // append 1 byte to a 2 byte buffer --> success
203 if(AppendTest(&UOB, 1, 0))
204 return "Append of 1 byte failed";
205
206 // append 2 byte to a 2 byte buffer --> success
207 if(AppendTest(&UOB, 2, 0))
208 return "Append to fill buffer failed";
209
210 // append 3 bytes to a 2 byte buffer --> failure
211 if(AppendTest(&UOB, 3, 1))
212 return "Overflow of buffer not caught";
213
214 // append max size_t to a 2 byte buffer --> failure
215 if(AppendTest(&UOB, SIZE_MAX, 1))
216 return "Append of SIZE_MAX error not caught";
217
218 if(InsertTest(&UOB, 1, 0, 0))
219 return "Insert 1 byte at start failed";
220
221 if(InsertTest(&UOB, 2, 0, 0))
222 return "Insert 2 bytes at start failed";
223
224 if(InsertTest(&UOB, 3, 0, 1))
225 return "Insert overflow not caught";
226
227 if(InsertTest(&UOB, 1, 1, 1))
228 return "Bad insertion point not caught";
229
230
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530231 UsefulBuf_MAKE_STACK_UB(outBuf2,10);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530233 UsefulOutBuf_Init(&UOB, outBuf2);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234
235 UsefulOutBuf_Reset(&UOB);
236 // put data in the buffer
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800237 UsefulOutBuf_AppendString(&UOB, "abc123");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800239 UsefulOutBuf_InsertString(&UOB, "xyz*&^", 0);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240
241 if(!UsefulOutBuf_GetError(&UOB)) {
242 return "insert with data should have failed";
243 }
244
245
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530246 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6);
248 if(UsefulOutBuf_GetError(&UOB)) {
249 return "insert in huge should have succeeded";
250 }
251
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530252 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
254 if(UsefulOutBuf_GetError(&UOB)) {
255 return "insert in huge should have succeeded";
256 }
257
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530258 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
260 if(!UsefulOutBuf_GetError(&UOB)) {
261 return "lengths near max size";
262 }
263
264 return NULL;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265}
266
267
268
269
270
271// Test function to get size and magic number check
272
273const char *TestBasicSanity()
274{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530275 UsefulBuf_MAKE_STACK_UB(outbuf,10);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800276
277 UsefulOutBuf UOB;
278
279 // First -- make sure that the room left function returns the right amount
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530280 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530282 if(UsefulOutBuf_RoomLeft(&UOB) != 10)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 return "room left failed";
284
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800285 if(!UsefulOutBuf_WillItFit(&UOB, 9)) {
286 return "it did not fit";
287 }
288
289 if(UsefulOutBuf_WillItFit(&UOB, 11)) {
290 return "it should have not fit";
291 }
292
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293
294 // Next -- make sure that the magic number checking is working right
295 UOB.magic = 8888; // make magic bogus
296
297 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
298
299 if(!UsefulOutBuf_GetError(&UOB))
300 return "magic corruption check failed";
301
302
303
304 // Next make sure that the valid data length check is working right
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530305 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530307 UOB.data_len = UOB.UB.len+1; // make size bogus
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308
309 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
310 if(!UsefulOutBuf_GetError(&UOB))
311 return "valid data check failed";
312
313 return NULL;
314}
315
316
317
318const char *UBMacroConversionsTest()
319{
320 char *szFoo = "foo";
321
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530322 UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800323 if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
324 return "SZToUsefulBufC failed";
325
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530326 UsefulBufC Too = UsefulBuf_FROM_SZ_LITERAL("Toooo");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530328 return "UsefulBuf_FROM_SZ_LITERAL failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329
330 uint8_t pB[] = {0x42, 0x6f, 0x6f};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530331 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800332 if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530333 return "UsefulBuf_FROM_BYTE_ARRAY_LITERAL failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334
335 char *sz = "not const"; // some data for the test
336 UsefulBuf B = (UsefulBuf){sz, sizeof(sz)};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530337 UsefulBufC BC = UsefulBuf_Const(B);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 if(BC.len != sizeof(sz) || BC.ptr != sz)
339 return "UsefulBufConst failed";
340
341 return NULL;
342}
343
344
345const char *UBUtilTests()
346{
347 UsefulBuf UB = NULLUsefulBuf;
348
349 if(!UsefulBuf_IsNULL(UB)){
350 return "IsNull failed";
351 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530352
353 if(!UsefulBuf_IsEmpty(UB)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800354 return "IsEmpty failed";
355 }
356
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530357 if(!UsefulBuf_IsNULLOrEmpty(UB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 return "IsNULLOrEmpty failed";
359 }
360
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530361 UsefulBufC UBC = UsefulBuf_Const(UB);
362
363 if(!UsefulBuf_IsNULLC(UBC)){
364 return "IsNull const failed";
365 }
366
367 if(!UsefulBuf_IsEmptyC(UBC)){
368 return "IsEmptyC failed";
369 }
370
371 if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
372 return "IsNULLOrEmptyC failed";
373 }
374
375 UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
376 if(!UsefulBuf_IsEmpty(UB2)) {
377 return "Back to UB is Empty failed";
378 }
379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 UB.ptr = "x"; // just some valid pointer
381
382 if(UsefulBuf_IsNULL(UB)){
383 return "IsNull failed";
384 }
385
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530386 if(!UsefulBuf_IsEmptyC(UBC)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 return "IsEmpty failed";
388 }
389
390 // test the Unconst.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530391 if(UsefulBuf_Unconst(UBC).ptr != NULL) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 return "Unconst failed";
393 }
394
395 // Set 100 bytes of '+'; validated a few tests later
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530396 UsefulBuf_MAKE_STACK_UB(Temp, 100);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530397 UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800398
399 // Try to copy into a buf that is too small and see failure
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530400 UsefulBuf_MAKE_STACK_UB(Temp2, 99);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530401 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 return "Copy should have failed";
403 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530404
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800405 if(UsefulBuf_IsNULLC(UsefulBuf_CopyPtr(Temp2, "xx", 2))) {
406 return "CopyPtr failed";
407 }
408
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530409 UsefulBufC xxyy = UsefulBuf_CopyOffset(Temp2, 2, UsefulBuf_FROM_SZ_LITERAL("yy"));
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800410 if(UsefulBuf_IsNULLC(xxyy)) {
411 return "CopyOffset Failed";
412 }
413
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530414 if(UsefulBuf_Compare(UsefulBuf_Head(xxyy, 3), UsefulBuf_FROM_SZ_LITERAL("xxy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800415 return "head failed";
416 }
417
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530418 if(UsefulBuf_Compare(UsefulBuf_Tail(xxyy, 1), UsefulBuf_FROM_SZ_LITERAL("xyy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800419 return "tail failed";
420 }
421
422 if(!UsefulBuf_IsNULLC(UsefulBuf_Head(xxyy, 5))) {
423 return "head should have failed";
424 }
425
426 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(xxyy, 5))) {
427 return "tail should have failed";
428 }
429
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530430 if(!UsefulBuf_IsNULLC(UsefulBuf_CopyOffset(Temp2, 100, UsefulBuf_FROM_SZ_LITERAL("yy")))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800431 return "Copy Offset should have failed";
432 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530433
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434 // Try to copy into a NULL/empty buf and see failure
435 UsefulBuf UBNull = NULLUsefulBuf;
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530436 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 return "Copy to NULL should have failed";
438 }
439
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800440
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800441 // Try to set a NULL/empty buf; nothing should happen
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530442 UsefulBuf_Set(UBNull, '+'); // This will crash on failure
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443
444 // Copy successfully to a buffer
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530445 UsefulBuf_MAKE_STACK_UB(Temp3, 101);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530446 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800447 return "Copy should not have failed";
448 }
449
450 static const uint8_t pExpected[] = {
451 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
452 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
453 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
454 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
455 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
456 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
457 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
458 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
459 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
460 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
461 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530462 UsefulBufC Expected = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpected);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 // This validates comparison for equality and the UsefulBuf_Set
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530464 if(UsefulBuf_Compare(Expected, TempC)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800465 return "Set / Copy / Compare failed";
466 }
467
468 // Compare two empties and expect success
469 if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
470 return "Compare Empties failed";
471 }
472
473 // Compare with empty and expect the first to be larger
474 if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
475 return "Compare with empty failed";
476 }
477
478
479 static const uint8_t pExpectedBigger[] = {
480 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
481 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
482 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
483 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
484 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
485 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
486 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
487 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
488 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
489 '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
490 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530491 UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800492
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530493 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800494 if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
495 return "Compare with bigger";
496 }
497
498
499 static const uint8_t pExpectedSmaller[] = {
500 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
501 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
502 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
503 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
504 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
505 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
506 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
507 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
508 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
509 '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
510 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530511 UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530512 // Expect +1 when the first arg is larger
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513 if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
514 return "Compare with smaller";
515 }
516
517
518 static const uint8_t pExpectedLonger[] = {
519 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
520 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
521 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
522 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
523 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
524 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
525 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
526 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
527 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
528 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
529 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530530 UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800531
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530532 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800533 if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
534 return "Compare with longer";
535 }
536
537
538 static const uint8_t pExpectedShorter[] = {
539 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
540 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
541 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
542 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
543 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
544 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
545 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
546 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
547 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
548 '+', '+', '+', '+', '+', '+', '+', '+', '+',
549 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530550 UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551 // Expect +1 with the first arg is larger
552 if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
553 return "Compare with shorter";
554 }
555
556
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530557 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800558 return "Copy null/empty failed";
559 }
560
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800561 // Look for +++++... in +++++... and find it at the beginning
562 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
563 return "Failed to find";
564 }
565
566 // look for ++* in ....++* and find it at the end
567 static const uint8_t pToFind[] = {'+', '+', '*'};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530568 UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569
570 if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
571 return "Failed to find 2";
572 }
573
574 // look for ++* in ....++, and find it near the end
575 if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
576 return "Failed to not find";
577 }
578
579 // Look for the whole buffer in itself and succeed.
580 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
581 return "Failed to find 3";
582 }
583
584 return NULL;
585}
586
587
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800588const char * UIBTest_IntegerFormat()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530590 UsefulOutBuf_MakeOnStack(UOB,100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591
592 const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
593 const uint64_t u64 = 1984738472938472;
594 const uint16_t u16 = 40000;
595 const uint8_t u8 = 9;
596 const float f = (float)314.15;
597 const double d = 2.1e10;
598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530600 UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
601 UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
602 UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800603 UsefulOutBuf_AppendByte(&UOB, u8);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530604 UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
605 UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530607 UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
608 if(UsefulBuf_IsNULLC(O))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 return "Couldn't output integers";
610
611 // from https://en.wikipedia.org/wiki/Endianness
612 const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
613 if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
614 return "not in network order";
615 }
616
617 UsefulInputBuf UIB;
618
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530619 UsefulInputBuf_Init(&UIB, O);
620
621 if(UsefulInputBuf_Tell(&UIB) != 0) {
622 return "UsefulInputBuf_Tell failed";
623 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624
625 if(UsefulInputBuf_GetUint32(&UIB) != u32) {
626 return "u32 out then in failed";
627 }
628 if(UsefulInputBuf_GetUint64(&UIB) != u64) {
629 return "u64 out then in failed";
630 }
631 if(UsefulInputBuf_GetUint16(&UIB) != u16) {
632 return "u16 out then in failed";
633 }
634 if(UsefulInputBuf_GetByte(&UIB) != u8) {
635 return "u8 out then in failed";
636 }
637 if(UsefulInputBuf_GetFloat(&UIB) != f) {
638 return "float out then in failed";
639 }
640 if(UsefulInputBuf_GetDouble(&UIB) != d) {
641 return "double out then in failed";
642 }
643
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800644 // Reset and go again for a few more tests
645 UsefulInputBuf_Init(&UIB, O);
646
647 UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
648 if(UsefulBuf_IsNULLC(Four)) {
649 return "Four is NULL";
650 }
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530651 if(UsefulBuf_Compare(Four, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedNetworkOrder))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800652 return "Four compare failed";
653 }
654
655 if(UsefulInputBuf_BytesUnconsumed(&UIB) != 23){
656 return "Wrong number of unconsumed bytes";
657 }
658
659 if(!UsefulInputBuf_BytesAvailable(&UIB, 23)){
660 return "Wrong number of bytes available I";
661 }
662
663 if(UsefulInputBuf_BytesAvailable(&UIB, 24)){
664 return "Wrong number of bytes available II";
665 }
666
667 UsefulInputBuf_Seek(&UIB, 0);
668
669 if(UsefulInputBuf_GetError(&UIB)) {
670 return "unexpected error after seek";
671 }
672
673 const uint8_t *pGetBytes = (const uint8_t *)UsefulInputBuf_GetBytes(&UIB, 4);
674 if(pGetBytes == NULL) {
675 return "GetBytes returns NULL";
676 }
677
678 if(memcmp(pGetBytes, pExpectedNetworkOrder, 4)) {
679 return "Got wrong bytes";
680 }
681
682 UsefulInputBuf_Seek(&UIB, 28);
683
684 if(!UsefulInputBuf_GetError(&UIB)) {
685 return "expected error after seek";
686 }
687
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530688 return NULL;
689}
690
691
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800692const char *UBUTest_CopyUtil()
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530693{
694 if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
695 return "CopyFloatToUint32 failed";
696 }
697
698 if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
699 return "CopyDoubleToUint64 failed";
700 }
701
702 if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
703 return "CopyUint64ToDouble failed";
704 }
705
706 if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
707 return "CopyUint32ToFloat failed";
708 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800709
710 return NULL;
711}
712
713
714