2 #include <winpr/print.h>
3 #include <winpr/stream.h>
5 static BOOL TestStream_Verify(
wStream* s,
size_t mincap,
size_t len,
size_t pos)
7 if (Stream_Buffer(s) == NULL)
9 printf(
"stream buffer is null\n");
13 if (Stream_ConstPointer(s) == NULL)
15 printf(
"stream pointer is null\n");
19 if (Stream_PointerAs(s, BYTE) < Stream_Buffer(s))
21 printf(
"stream pointer (%p) or buffer (%p) is invalid\n", Stream_ConstPointer(s),
22 (
void*)Stream_Buffer(s));
26 if (Stream_Capacity(s) < mincap)
28 printf(
"stream capacity is %" PRIuz
" but minimum expected value is %" PRIuz
"\n",
29 Stream_Capacity(s), mincap);
33 if (Stream_Length(s) != len)
35 printf(
"stream has unexpected length (%" PRIuz
" instead of %" PRIuz
")\n",
36 Stream_Length(s), len);
40 if (Stream_GetPosition(s) != pos)
42 printf(
"stream has unexpected position (%" PRIuz
" instead of %" PRIuz
")\n",
43 Stream_GetPosition(s), pos);
47 if (Stream_GetPosition(s) > Stream_Length(s))
49 printf(
"stream position (%" PRIuz
") exceeds length (%" PRIuz
")\n", Stream_GetPosition(s),
54 if (Stream_GetPosition(s) > Stream_Capacity(s))
56 printf(
"stream position (%" PRIuz
") exceeds capacity (%" PRIuz
")\n",
57 Stream_GetPosition(s), Stream_Capacity(s));
61 if (Stream_Length(s) > Stream_Capacity(s))
63 printf(
"stream length (%" PRIuz
") exceeds capacity (%" PRIuz
")\n", Stream_Length(s),
68 if (Stream_GetRemainingLength(s) != len - pos)
70 printf(
"stream remaining length (%" PRIuz
" instead of %" PRIuz
")\n",
71 Stream_GetRemainingLength(s), len - pos);
78 static BOOL TestStream_New(
void)
81 wStream* s = Stream_New(NULL, 0);
90 static BOOL TestStream_Static(
void)
92 BYTE buffer[20] = { 0 };
97 Stream_StaticInit(s, buffer,
sizeof(buffer));
98 Stream_Write_UINT16(s, 0xcab1);
99 Stream_SetPosition(s, 0);
100 Stream_Read_UINT16(s, v);
105 Stream_SetPosition(s, 0);
106 Stream_Write_UINT16(s, 1);
108 if (!Stream_EnsureRemainingCapacity(s, 10))
112 if (!Stream_EnsureRemainingCapacity(s, 30) || !s->isOwner)
115 Stream_Write_UINT16(s, 2);
116 Stream_SetPosition(s, 0);
117 Stream_Read_UINT16(s, v);
122 Stream_Read_UINT16(s, v);
130 WINPR_PRAGMA_DIAG_PUSH
131 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
132 Stream_Free(s, TRUE);
133 WINPR_PRAGMA_DIAG_POP
137 static BOOL TestStream_Create(
size_t count, BOOL selfAlloc)
144 for (
size_t i = 0; i < count; i++)
150 if (!(buffer = malloc(cap)))
152 printf(
"%s: failed to allocate buffer of size %" PRIuz
"\n", __func__, cap);
157 if (!(s = Stream_New(selfAlloc ? buffer : NULL, len)))
159 printf(
"%s: Stream_New failed for stream #%" PRIuz
"\n", __func__, i);
163 if (!TestStream_Verify(s, cap, len, 0))
168 for (
size_t pos = 0; pos < len; pos++)
170 Stream_SetPosition(s, pos);
171 Stream_SealLength(s);
173 if (!TestStream_Verify(s, cap, pos, pos))
181 memset(buffer, (BYTE)(i % 256), cap);
183 if (memcmp(buffer, Stream_Buffer(s), cap) != 0)
185 printf(
"%s: buffer memory corruption\n", __func__);
190 Stream_Free(s, buffer ? FALSE : TRUE);
200 Stream_Free(s, buffer ? FALSE : TRUE);
206 static BOOL TestStream_Extent(UINT32 maxSize)
211 if (!(s = Stream_New(NULL, 1)))
213 printf(
"%s: Stream_New failed\n", __func__);
217 for (UINT32 i = 1; i < maxSize; i++)
221 if (!Stream_EnsureRemainingCapacity(s, i))
226 if (!Stream_EnsureCapacity(s, i))
230 Stream_SetPosition(s, i);
231 Stream_SealLength(s);
233 if (!TestStream_Verify(s, i, i, i))
235 printf(
"%s: failed to verify stream in iteration %" PRIu32
"\n", __func__, i);
245 Stream_Free(s, TRUE);
251 #define Stream_Peek_UINT8_BE Stream_Peek_UINT8
252 #define Stream_Read_UINT8_BE Stream_Read_UINT8
253 #define Stream_Peek_Get_UINT8_BE Stream_Peek_Get_UINT8
254 #define Stream_Get_UINT8_BE Stream_Get_UINT8
255 #define Stream_Peek_INT8_BE Stream_Peek_INT8
256 #define Stream_Peek_Get_INT8_BE Stream_Peek_Get_INT8
257 #define Stream_Read_INT8_BE Stream_Read_INT8
258 #define Stream_Get_INT8_BE Stream_Get_INT8
260 #define TestStream_PeekAndRead(_s, _r, _t) \
265 BYTE* _p = Stream_Buffer(_s); \
266 Stream_SetPosition(_s, 0); \
267 Stream_Peek_##_t(_s, _a); \
268 Stream_Read_##_t(_s, _b); \
271 printf("%s: test1 " #_t "_LE failed\n", __func__); \
274 Stream_Rewind(_s, sizeof(_t)); \
275 const _t _d = Stream_Peek_Get_##_t(_s); \
276 const _t _c = Stream_Get_##_t(_s); \
279 printf("%s: test1 " #_t "_LE failed\n", __func__); \
282 for (size_t _i = 0; _i < sizeof(_t); _i++) \
284 if (((_a >> (_i * 8)) & 0xFF) != _p[_i]) \
286 printf("%s: test2 " #_t "_LE failed\n", __func__); \
292 Stream_SetPosition(_s, 0); \
293 Stream_Peek_##_t##_BE(_s, _a); \
294 Stream_Read_##_t##_BE(_s, _b); \
297 printf("%s: test1 " #_t "_BE failed\n", __func__); \
300 Stream_Rewind(_s, sizeof(_t)); \
301 const _t _e = Stream_Peek_Get_##_t##_BE(_s); \
302 const _t _f = Stream_Get_##_t##_BE(_s); \
305 printf("%s: test1 " #_t "_BE failed\n", __func__); \
308 for (size_t _i = 0; _i < sizeof(_t); _i++) \
310 if (((_a >> (_i * 8)) & 0xFF) != _p[sizeof(_t) - _i - 1]) \
312 printf("%s: test2 " #_t "_BE failed\n", __func__); \
320 static BOOL TestStream_Reading(
void)
322 BYTE src[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
326 if (!(s = Stream_New(src,
sizeof(src))))
328 printf(
"%s: Stream_New failed\n", __func__);
332 TestStream_PeekAndRead(s, result, UINT8);
333 TestStream_PeekAndRead(s, result, INT8);
334 TestStream_PeekAndRead(s, result, UINT16);
335 TestStream_PeekAndRead(s, result, INT16);
336 TestStream_PeekAndRead(s, result, UINT32);
337 TestStream_PeekAndRead(s, result, INT32);
338 TestStream_PeekAndRead(s, result, UINT64);
339 TestStream_PeekAndRead(s, result, INT64);
340 Stream_Free(s, FALSE);
344 static BOOL TestStream_Write(
void)
351 const BYTE data[] =
"someteststreamdata";
352 wStream* s = Stream_New(NULL, 100);
357 if (s->pointer != s->buffer)
360 Stream_Write(s, data,
sizeof(data));
362 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) == 0)
365 if (s->pointer != s->buffer +
sizeof(data))
368 Stream_SetPosition(s, 0);
370 if (s->pointer != s->buffer)
373 Stream_Write_UINT8(s, 42);
375 if (s->pointer != s->buffer + 1)
378 Stream_SetPosition(s, 0);
380 if (s->pointer != s->buffer)
383 Stream_Peek_UINT8(s, u8);
388 Stream_Write_UINT16(s, 0x1234);
390 if (s->pointer != s->buffer + 2)
393 Stream_SetPosition(s, 0);
395 if (s->pointer != s->buffer)
398 Stream_Peek_UINT16(s, u16);
403 Stream_Write_UINT32(s, 0x12345678UL);
405 if (s->pointer != s->buffer + 4)
408 Stream_SetPosition(s, 0);
410 if (s->pointer != s->buffer)
413 Stream_Peek_UINT32(s, u32);
415 if (u32 != 0x12345678UL)
418 Stream_Write_UINT64(s, 0x1234567890ABCDEFULL);
420 if (s->pointer != s->buffer + 8)
423 Stream_SetPosition(s, 0);
425 if (s->pointer != s->buffer)
428 Stream_Peek_UINT64(s, u64);
430 if (u64 != 0x1234567890ABCDEFULL)
434 Stream_Free(s, TRUE);
438 static BOOL TestStream_Seek(
void)
441 wStream* s = Stream_New(NULL, 100);
446 if (s->pointer != s->buffer)
451 if (s->pointer != s->buffer + 5)
454 Stream_Seek_UINT8(s);
456 if (s->pointer != s->buffer + 6)
459 Stream_Seek_UINT16(s);
461 if (s->pointer != s->buffer + 8)
464 Stream_Seek_UINT32(s);
466 if (s->pointer != s->buffer + 12)
469 Stream_Seek_UINT64(s);
471 if (s->pointer != s->buffer + 20)
476 Stream_Free(s, TRUE);
480 static BOOL TestStream_Rewind(
void)
483 wStream* s = Stream_New(NULL, 100);
488 if (s->pointer != s->buffer)
493 if (s->pointer != s->buffer + 100)
496 Stream_Rewind(s, 10);
498 if (s->pointer != s->buffer + 90)
501 Stream_Rewind_UINT8(s);
503 if (s->pointer != s->buffer + 89)
506 Stream_Rewind_UINT16(s);
508 if (s->pointer != s->buffer + 87)
511 Stream_Rewind_UINT32(s);
513 if (s->pointer != s->buffer + 83)
516 Stream_Rewind_UINT64(s);
518 if (s->pointer != s->buffer + 75)
523 Stream_Free(s, TRUE);
527 static BOOL TestStream_Zero(
void)
530 const BYTE data[] =
"someteststreamdata";
531 wStream* s = Stream_New(NULL,
sizeof(data));
536 Stream_Write(s, data,
sizeof(data));
538 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
541 Stream_SetPosition(s, 0);
543 if (s->pointer != s->buffer)
548 if (s->pointer != s->buffer + 5)
551 if (memcmp(Stream_ConstPointer(s), data + 5,
sizeof(data) - 5) != 0)
554 Stream_SetPosition(s, 0);
556 if (s->pointer != s->buffer)
559 for (UINT32 x = 0; x < 5; x++)
562 Stream_Read_UINT8(s, val);
570 Stream_Free(s, TRUE);
574 static BOOL TestStream_Fill(
void)
577 const BYTE fill[7] =
"XXXXXXX";
578 const BYTE data[] =
"someteststreamdata";
579 wStream* s = Stream_New(NULL,
sizeof(data));
584 Stream_Write(s, data,
sizeof(data));
586 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
589 Stream_SetPosition(s, 0);
591 if (s->pointer != s->buffer)
594 Stream_Fill(s, fill[0],
sizeof(fill));
596 if (s->pointer != s->buffer +
sizeof(fill))
599 if (memcmp(Stream_ConstPointer(s), data +
sizeof(fill),
sizeof(data) -
sizeof(fill)) != 0)
602 Stream_SetPosition(s, 0);
604 if (s->pointer != s->buffer)
607 if (memcmp(Stream_ConstPointer(s), fill,
sizeof(fill)) != 0)
612 Stream_Free(s, TRUE);
616 static BOOL TestStream_Copy(
void)
619 const BYTE data[] =
"someteststreamdata";
620 wStream* s = Stream_New(NULL,
sizeof(data));
621 wStream* d = Stream_New(NULL,
sizeof(data));
626 if (s->pointer != s->buffer)
629 Stream_Write(s, data,
sizeof(data));
631 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
634 if (s->pointer != s->buffer +
sizeof(data))
637 Stream_SetPosition(s, 0);
639 if (s->pointer != s->buffer)
642 Stream_Copy(s, d,
sizeof(data));
644 if (s->pointer != s->buffer +
sizeof(data))
647 if (d->pointer != d->buffer +
sizeof(data))
650 if (Stream_GetPosition(s) != Stream_GetPosition(d))
653 if (memcmp(Stream_Buffer(s), data,
sizeof(data)) != 0)
656 if (memcmp(Stream_Buffer(d), data,
sizeof(data)) != 0)
661 Stream_Free(s, TRUE);
662 Stream_Free(d, TRUE);
666 int TestStream(
int argc,
char* argv[])
671 if (!TestStream_Create(200, FALSE))
674 if (!TestStream_Create(200, TRUE))
677 if (!TestStream_Extent(4096))
680 if (!TestStream_Reading())
683 if (!TestStream_New())
686 if (!TestStream_Write())
689 if (!TestStream_Seek())
692 if (!TestStream_Rewind())
695 if (!TestStream_Zero())
698 if (!TestStream_Fill())
701 if (!TestStream_Copy())
704 if (!TestStream_Static())