2 #include <winpr/print.h>
3 #include <winpr/crypto.h>
4 #include <winpr/stream.h>
6 static BOOL TestStream_Verify(
wStream* s,
size_t mincap,
size_t len,
size_t pos)
8 if (Stream_Buffer(s) == NULL)
10 printf(
"stream buffer is null\n");
14 if (Stream_ConstPointer(s) == NULL)
16 printf(
"stream pointer is null\n");
20 if (Stream_PointerAs(s, BYTE) < Stream_Buffer(s))
22 printf(
"stream pointer (%p) or buffer (%p) is invalid\n", Stream_ConstPointer(s),
23 (
void*)Stream_Buffer(s));
27 if (Stream_Capacity(s) < mincap)
29 printf(
"stream capacity is %" PRIuz
" but minimum expected value is %" PRIuz
"\n",
30 Stream_Capacity(s), mincap);
34 if (Stream_Length(s) != len)
36 printf(
"stream has unexpected length (%" PRIuz
" instead of %" PRIuz
")\n",
37 Stream_Length(s), len);
41 if (Stream_GetPosition(s) != pos)
43 printf(
"stream has unexpected position (%" PRIuz
" instead of %" PRIuz
")\n",
44 Stream_GetPosition(s), pos);
48 if (Stream_GetPosition(s) > Stream_Length(s))
50 printf(
"stream position (%" PRIuz
") exceeds length (%" PRIuz
")\n", Stream_GetPosition(s),
55 if (Stream_GetPosition(s) > Stream_Capacity(s))
57 printf(
"stream position (%" PRIuz
") exceeds capacity (%" PRIuz
")\n",
58 Stream_GetPosition(s), Stream_Capacity(s));
62 if (Stream_Length(s) > Stream_Capacity(s))
64 printf(
"stream length (%" PRIuz
") exceeds capacity (%" PRIuz
")\n", Stream_Length(s),
69 if (Stream_GetRemainingLength(s) != len - pos)
71 printf(
"stream remaining length (%" PRIuz
" instead of %" PRIuz
")\n",
72 Stream_GetRemainingLength(s), len - pos);
79 static BOOL TestStream_New(
void)
82 wStream* s = Stream_New(NULL, 0);
91 static BOOL TestStream_Static(
void)
93 BYTE buffer[20] = { 0 };
98 Stream_StaticInit(s, buffer,
sizeof(buffer));
99 Stream_Write_UINT16(s, 0xcab1);
100 Stream_SetPosition(s, 0);
101 Stream_Read_UINT16(s, v);
106 Stream_SetPosition(s, 0);
107 Stream_Write_UINT16(s, 1);
109 if (!Stream_EnsureRemainingCapacity(s, 10))
113 if (!Stream_EnsureRemainingCapacity(s, 30) || !s->isOwner)
116 Stream_Write_UINT16(s, 2);
117 Stream_SetPosition(s, 0);
118 Stream_Read_UINT16(s, v);
123 Stream_Read_UINT16(s, v);
131 WINPR_PRAGMA_DIAG_PUSH
132 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
133 Stream_Free(s, TRUE);
134 WINPR_PRAGMA_DIAG_POP
138 static BOOL TestStream_Create(
size_t count, BOOL selfAlloc)
145 for (
size_t i = 0; i < count; i++)
151 if (!(buffer = malloc(cap)))
153 printf(
"%s: failed to allocate buffer of size %" PRIuz
"\n", __func__, cap);
158 if (!(s = Stream_New(selfAlloc ? buffer : NULL, len)))
160 printf(
"%s: Stream_New failed for stream #%" PRIuz
"\n", __func__, i);
164 if (!TestStream_Verify(s, cap, len, 0))
169 for (
size_t pos = 0; pos < len; pos++)
171 Stream_SetPosition(s, pos);
172 Stream_SealLength(s);
174 if (!TestStream_Verify(s, cap, pos, pos))
182 memset(buffer, (BYTE)(i % 256), cap);
184 if (memcmp(buffer, Stream_Buffer(s), cap) != 0)
186 printf(
"%s: buffer memory corruption\n", __func__);
191 Stream_Free(s, buffer ? FALSE : TRUE);
201 Stream_Free(s, buffer ? FALSE : TRUE);
207 static BOOL TestStream_Extent(UINT32 maxSize)
212 if (!(s = Stream_New(NULL, 1)))
214 printf(
"%s: Stream_New failed\n", __func__);
218 for (UINT32 i = 1; i < maxSize; i++)
222 if (!Stream_EnsureRemainingCapacity(s, i))
227 if (!Stream_EnsureCapacity(s, i))
231 Stream_SetPosition(s, i);
232 Stream_SealLength(s);
234 if (!TestStream_Verify(s, i, i, i))
236 printf(
"%s: failed to verify stream in iteration %" PRIu32
"\n", __func__, i);
246 Stream_Free(s, TRUE);
252 #define Stream_Peek_UINT8_BE Stream_Peek_UINT8
253 #define Stream_Read_UINT8_BE Stream_Read_UINT8
254 #define Stream_Peek_Get_UINT8_BE Stream_Peek_Get_UINT8
255 #define Stream_Get_UINT8_BE Stream_Get_UINT8
256 #define Stream_Peek_INT8_BE Stream_Peek_INT8
257 #define Stream_Peek_Get_INT8_BE Stream_Peek_Get_INT8
258 #define Stream_Read_INT8_BE Stream_Read_INT8
259 #define Stream_Get_INT8_BE Stream_Get_INT8
261 #define TestStream_PeekAndRead(_s, _r, _t) \
266 BYTE* _p = Stream_Buffer(_s); \
267 Stream_SetPosition(_s, 0); \
268 Stream_Peek_##_t(_s, _a); \
269 Stream_Read_##_t(_s, _b); \
272 printf("%s: test1 " #_t "_LE failed\n", __func__); \
275 Stream_Rewind(_s, sizeof(_t)); \
276 const _t _d = Stream_Peek_Get_##_t(_s); \
277 const _t _c = Stream_Get_##_t(_s); \
280 printf("%s: test1 " #_t "_LE failed\n", __func__); \
283 for (size_t _i = 0; _i < sizeof(_t); _i++) \
285 if (((_a >> (_i * 8)) & 0xFF) != _p[_i]) \
287 printf("%s: test2 " #_t "_LE failed\n", __func__); \
293 Stream_SetPosition(_s, 0); \
294 Stream_Peek_##_t##_BE(_s, _a); \
295 Stream_Read_##_t##_BE(_s, _b); \
298 printf("%s: test1 " #_t "_BE failed\n", __func__); \
301 Stream_Rewind(_s, sizeof(_t)); \
302 const _t _e = Stream_Peek_Get_##_t##_BE(_s); \
303 const _t _f = Stream_Get_##_t##_BE(_s); \
306 printf("%s: test1 " #_t "_BE failed\n", __func__); \
309 for (size_t _i = 0; _i < sizeof(_t); _i++) \
311 if (((_a >> (_i * 8)) & 0xFF) != _p[sizeof(_t) - _i - 1]) \
313 printf("%s: test2 " #_t "_BE failed\n", __func__); \
321 static BOOL TestStream_WriteAndRead(UINT64 value)
336 wStream* s = Stream_New(NULL, 1024);
342 Stream_Write_UINT8(s, val.u8);
343 Stream_Rewind_UINT8(s);
344 const UINT8 ru8 = Stream_Get_UINT8(s);
345 Stream_Rewind_UINT8(s);
350 Stream_Write_UINT16(s, val.u16);
351 Stream_Rewind_UINT16(s);
352 const UINT16 ru = Stream_Get_UINT16(s);
353 Stream_Rewind_UINT16(s);
358 Stream_Write_UINT16_BE(s, val.u16);
359 Stream_Rewind_UINT16(s);
360 const UINT16 ru = Stream_Get_UINT16_BE(s);
361 Stream_Rewind_UINT16(s);
366 Stream_Write_UINT32(s, val.u32);
367 Stream_Rewind_UINT32(s);
368 const UINT32 ru = Stream_Get_UINT32(s);
369 Stream_Rewind_UINT32(s);
374 Stream_Write_UINT32_BE(s, val.u32);
375 Stream_Rewind_UINT32(s);
376 const UINT32 ru = Stream_Get_UINT32_BE(s);
377 Stream_Rewind_UINT32(s);
382 Stream_Write_UINT64(s, val.u64);
383 Stream_Rewind_UINT64(s);
384 const UINT64 ru = Stream_Get_UINT64(s);
385 Stream_Rewind_UINT64(s);
390 Stream_Write_UINT64_BE(s, val.u64);
391 Stream_Rewind_UINT64(s);
392 const UINT64 ru = Stream_Get_UINT64_BE(s);
393 Stream_Rewind_UINT64(s);
398 Stream_Write_INT8(s, val.i8);
400 const INT8 ru8 = Stream_Get_INT8(s);
406 Stream_Write_INT16(s, val.i16);
408 const INT16 ru = Stream_Get_INT16(s);
414 Stream_Write_INT16_BE(s, val.i16);
416 const INT16 ru = Stream_Get_INT16_BE(s);
422 Stream_Write_INT32(s, val.i32);
424 const INT32 ru = Stream_Get_INT32(s);
430 Stream_Write_INT32_BE(s, val.i32);
432 const INT32 ru = Stream_Get_INT32_BE(s);
438 Stream_Write_INT64(s, val.i64);
440 const INT64 ru = Stream_Get_INT64(s);
446 Stream_Write_INT64_BE(s, val.i64);
448 const INT64 ru = Stream_Get_INT64_BE(s);
456 Stream_Free(s, TRUE);
460 static BOOL TestStream_Reading(
void)
462 BYTE src[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
466 if (!(s = Stream_New(src,
sizeof(src))))
468 printf(
"%s: Stream_New failed\n", __func__);
472 TestStream_PeekAndRead(s, result, UINT8);
473 TestStream_PeekAndRead(s, result, INT8);
474 TestStream_PeekAndRead(s, result, UINT16);
475 TestStream_PeekAndRead(s, result, INT16);
476 TestStream_PeekAndRead(s, result, UINT32);
477 TestStream_PeekAndRead(s, result, INT32);
478 TestStream_PeekAndRead(s, result, UINT64);
479 TestStream_PeekAndRead(s, result, INT64);
480 Stream_Free(s, FALSE);
484 static BOOL TestStream_Write(
void)
491 const BYTE data[] =
"someteststreamdata";
492 wStream* s = Stream_New(NULL, 100);
497 if (s->pointer != s->buffer)
500 Stream_Write(s, data,
sizeof(data));
502 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) == 0)
505 if (s->pointer != s->buffer +
sizeof(data))
508 Stream_SetPosition(s, 0);
510 if (s->pointer != s->buffer)
513 Stream_Write_UINT8(s, 42);
515 if (s->pointer != s->buffer + 1)
518 Stream_SetPosition(s, 0);
520 if (s->pointer != s->buffer)
523 Stream_Peek_UINT8(s, u8);
528 Stream_Write_UINT16(s, 0x1234);
530 if (s->pointer != s->buffer + 2)
533 Stream_SetPosition(s, 0);
535 if (s->pointer != s->buffer)
538 Stream_Peek_UINT16(s, u16);
543 Stream_Write_UINT32(s, 0x12345678UL);
545 if (s->pointer != s->buffer + 4)
548 Stream_SetPosition(s, 0);
550 if (s->pointer != s->buffer)
553 Stream_Peek_UINT32(s, u32);
555 if (u32 != 0x12345678UL)
558 Stream_Write_UINT64(s, 0x1234567890ABCDEFULL);
560 if (s->pointer != s->buffer + 8)
563 Stream_SetPosition(s, 0);
565 if (s->pointer != s->buffer)
568 Stream_Peek_UINT64(s, u64);
570 if (u64 != 0x1234567890ABCDEFULL)
574 Stream_Free(s, TRUE);
578 static BOOL TestStream_Seek(
void)
581 wStream* s = Stream_New(NULL, 100);
586 if (s->pointer != s->buffer)
591 if (s->pointer != s->buffer + 5)
594 Stream_Seek_UINT8(s);
596 if (s->pointer != s->buffer + 6)
599 Stream_Seek_UINT16(s);
601 if (s->pointer != s->buffer + 8)
604 Stream_Seek_UINT32(s);
606 if (s->pointer != s->buffer + 12)
609 Stream_Seek_UINT64(s);
611 if (s->pointer != s->buffer + 20)
616 Stream_Free(s, TRUE);
620 static BOOL TestStream_Rewind(
void)
623 wStream* s = Stream_New(NULL, 100);
628 if (s->pointer != s->buffer)
633 if (s->pointer != s->buffer + 100)
636 Stream_Rewind(s, 10);
638 if (s->pointer != s->buffer + 90)
641 Stream_Rewind_UINT8(s);
643 if (s->pointer != s->buffer + 89)
646 Stream_Rewind_UINT16(s);
648 if (s->pointer != s->buffer + 87)
651 Stream_Rewind_UINT32(s);
653 if (s->pointer != s->buffer + 83)
656 Stream_Rewind_UINT64(s);
658 if (s->pointer != s->buffer + 75)
663 Stream_Free(s, TRUE);
667 static BOOL TestStream_Zero(
void)
670 const BYTE data[] =
"someteststreamdata";
671 wStream* s = Stream_New(NULL,
sizeof(data));
676 Stream_Write(s, data,
sizeof(data));
678 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
681 Stream_SetPosition(s, 0);
683 if (s->pointer != s->buffer)
688 if (s->pointer != s->buffer + 5)
691 if (memcmp(Stream_ConstPointer(s), data + 5,
sizeof(data) - 5) != 0)
694 Stream_SetPosition(s, 0);
696 if (s->pointer != s->buffer)
699 for (UINT32 x = 0; x < 5; x++)
702 Stream_Read_UINT8(s, val);
710 Stream_Free(s, TRUE);
714 static BOOL TestStream_Fill(
void)
717 const BYTE fill[7] =
"XXXXXXX";
718 const BYTE data[] =
"someteststreamdata";
719 wStream* s = Stream_New(NULL,
sizeof(data));
724 Stream_Write(s, data,
sizeof(data));
726 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
729 Stream_SetPosition(s, 0);
731 if (s->pointer != s->buffer)
734 Stream_Fill(s, fill[0],
sizeof(fill));
736 if (s->pointer != s->buffer +
sizeof(fill))
739 if (memcmp(Stream_ConstPointer(s), data +
sizeof(fill),
sizeof(data) -
sizeof(fill)) != 0)
742 Stream_SetPosition(s, 0);
744 if (s->pointer != s->buffer)
747 if (memcmp(Stream_ConstPointer(s), fill,
sizeof(fill)) != 0)
752 Stream_Free(s, TRUE);
756 static BOOL TestStream_Copy(
void)
759 const BYTE data[] =
"someteststreamdata";
760 wStream* s = Stream_New(NULL,
sizeof(data));
761 wStream* d = Stream_New(NULL,
sizeof(data));
766 if (s->pointer != s->buffer)
769 Stream_Write(s, data,
sizeof(data));
771 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
774 if (s->pointer != s->buffer +
sizeof(data))
777 Stream_SetPosition(s, 0);
779 if (s->pointer != s->buffer)
782 Stream_Copy(s, d,
sizeof(data));
784 if (s->pointer != s->buffer +
sizeof(data))
787 if (d->pointer != d->buffer +
sizeof(data))
790 if (Stream_GetPosition(s) != Stream_GetPosition(d))
793 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
796 if (memcmp(Stream_Buffer(d), data,
sizeof(data)) != 0)
801 Stream_Free(s, TRUE);
802 Stream_Free(d, TRUE);
806 int TestStream(
int argc,
char* argv[])
811 if (!TestStream_Create(200, FALSE))
814 if (!TestStream_Create(200, TRUE))
817 if (!TestStream_Extent(4096))
820 if (!TestStream_Reading())
823 if (!TestStream_New())
826 if (!TestStream_Write())
829 if (!TestStream_Seek())
832 if (!TestStream_Rewind())
835 if (!TestStream_Zero())
838 if (!TestStream_Fill())
841 if (!TestStream_Copy())
844 if (!TestStream_Static())
847 if (!TestStream_WriteAndRead(0x1234567890abcdef))
850 for (
size_t x = 0; x < 10; x++)
853 winpr_RAND(&val,
sizeof(val));
854 if (!TestStream_WriteAndRead(val))