FreeRDP
Loading...
Searching...
No Matches
TestStream.c
1#include <winpr/crt.h>
2#include <winpr/print.h>
3#include <winpr/crypto.h>
4#include <winpr/stream.h>
5
6WINPR_ATTR_NODISCARD
7static BOOL TestStream_Verify(wStream* s, size_t mincap, size_t len, size_t pos)
8{
9 if (Stream_Buffer(s) == nullptr)
10 {
11 printf("stream buffer is null\n");
12 return FALSE;
13 }
14
15 if (Stream_ConstPointer(s) == nullptr)
16 {
17 printf("stream pointer is null\n");
18 return FALSE;
19 }
20
21 if (Stream_PointerAs(s, BYTE) < Stream_Buffer(s))
22 {
23 printf("stream pointer (%p) or buffer (%p) is invalid\n", Stream_ConstPointer(s),
24 (void*)Stream_Buffer(s));
25 return FALSE;
26 }
27
28 if (Stream_Capacity(s) < mincap)
29 {
30 printf("stream capacity is %" PRIuz " but minimum expected value is %" PRIuz "\n",
31 Stream_Capacity(s), mincap);
32 return FALSE;
33 }
34
35 if (Stream_Length(s) != len)
36 {
37 printf("stream has unexpected length (%" PRIuz " instead of %" PRIuz ")\n",
38 Stream_Length(s), len);
39 return FALSE;
40 }
41
42 if (Stream_GetPosition(s) != pos)
43 {
44 printf("stream has unexpected position (%" PRIuz " instead of %" PRIuz ")\n",
45 Stream_GetPosition(s), pos);
46 return FALSE;
47 }
48
49 if (Stream_GetPosition(s) > Stream_Length(s))
50 {
51 printf("stream position (%" PRIuz ") exceeds length (%" PRIuz ")\n", Stream_GetPosition(s),
52 Stream_Length(s));
53 return FALSE;
54 }
55
56 if (Stream_GetPosition(s) > Stream_Capacity(s))
57 {
58 printf("stream position (%" PRIuz ") exceeds capacity (%" PRIuz ")\n",
59 Stream_GetPosition(s), Stream_Capacity(s));
60 return FALSE;
61 }
62
63 if (Stream_Length(s) > Stream_Capacity(s))
64 {
65 printf("stream length (%" PRIuz ") exceeds capacity (%" PRIuz ")\n", Stream_Length(s),
66 Stream_Capacity(s));
67 return FALSE;
68 }
69
70 if (Stream_GetRemainingLength(s) != len - pos)
71 {
72 printf("stream remaining length (%" PRIuz " instead of %" PRIuz ")\n",
73 Stream_GetRemainingLength(s), len - pos);
74 return FALSE;
75 }
76
77 return TRUE;
78}
79
80WINPR_ATTR_NODISCARD
81static BOOL TestStream_New(void)
82{
83 /* Test creation of a 0-size stream with no buffer */
84 wStream* s = Stream_New(nullptr, 0);
85
86 if (s)
87 return FALSE;
88 Stream_Free(s, TRUE);
89
90 return TRUE;
91}
92
93WINPR_ATTR_NODISCARD
94static BOOL TestStream_Static(void)
95{
96 BYTE buffer[20] = WINPR_C_ARRAY_INIT;
97 wStream staticStream = WINPR_C_ARRAY_INIT;
98 wStream* s = &staticStream;
99 UINT16 v = 0;
100 /* Test creation of a static stream */
101 Stream_StaticInit(s, buffer, sizeof(buffer));
102 Stream_Write_UINT16(s, 0xcab1);
103 Stream_ResetPosition(s);
104 Stream_Read_UINT16(s, v);
105
106 if (v != 0xcab1)
107 return FALSE;
108
109 Stream_ResetPosition(s);
110 Stream_Write_UINT16(s, 1);
111
112 if (!Stream_EnsureRemainingCapacity(s, 10)) /* we can ask for 10 bytes */
113 return FALSE;
114
115 /* 30 is bigger than the buffer, it will be reallocated on the heap */
116 if (!Stream_EnsureRemainingCapacity(s, 30) || !s->isOwner)
117 return FALSE;
118
119 Stream_Write_UINT16(s, 2);
120 Stream_ResetPosition(s);
121 Stream_Read_UINT16(s, v);
122
123 if (v != 1)
124 return FALSE;
125
126 Stream_Read_UINT16(s, v);
127
128 if (v != 2)
129 return FALSE;
130
131 // Intentional warning as the stream is not allocated.
132 // Still, Stream_Free should not release such memory, therefore this statement
133 // is required to test that.
134 WINPR_PRAGMA_DIAG_PUSH
135 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
136 Stream_Free(s, TRUE);
137 WINPR_PRAGMA_DIAG_POP
138 return TRUE;
139}
140
141WINPR_ATTR_NODISCARD
142static BOOL TestStream_Create(size_t count, BOOL selfAlloc)
143{
144 size_t len = 0;
145 size_t cap = 0;
146 wStream* s = nullptr;
147 void* buffer = nullptr;
148
149 for (size_t i = 0; i < count; i++)
150 {
151 len = cap = i + 1;
152
153 if (selfAlloc)
154 {
155 if (!(buffer = malloc(cap)))
156 {
157 printf("%s: failed to allocate buffer of size %" PRIuz "\n", __func__, cap);
158 goto fail;
159 }
160 }
161
162 if (!(s = Stream_New(selfAlloc ? buffer : nullptr, len)))
163 {
164 printf("%s: Stream_New failed for stream #%" PRIuz "\n", __func__, i);
165 goto fail;
166 }
167
168 if (!TestStream_Verify(s, cap, len, 0))
169 {
170 goto fail;
171 }
172
173 for (size_t pos = 0; pos < len; pos++)
174 {
175 if (!Stream_SetPosition(s, pos))
176 goto fail;
177 Stream_SealLength(s);
178
179 if (!TestStream_Verify(s, cap, pos, pos))
180 {
181 goto fail;
182 }
183 }
184
185 if (selfAlloc)
186 {
187 memset(buffer, (BYTE)(i % 256), cap);
188
189 if (memcmp(buffer, Stream_Buffer(s), cap) != 0)
190 {
191 printf("%s: buffer memory corruption\n", __func__);
192 goto fail;
193 }
194 }
195
196 Stream_Free(s, buffer == nullptr);
197 free(buffer);
198 }
199
200 return TRUE;
201fail:
202 free(buffer);
203
204 if (s)
205 {
206 Stream_Free(s, buffer == nullptr);
207 }
208
209 return FALSE;
210}
211
212WINPR_ATTR_NODISCARD
213static BOOL TestStream_Extent(UINT32 maxSize)
214{
215 wStream* s = nullptr;
216 BOOL result = FALSE;
217
218 if (!(s = Stream_New(nullptr, 1)))
219 {
220 printf("%s: Stream_New failed\n", __func__);
221 return FALSE;
222 }
223
224 for (UINT32 i = 1; i < maxSize; i++)
225 {
226 if (i % 2)
227 {
228 if (!Stream_EnsureRemainingCapacity(s, i))
229 goto fail;
230 }
231 else
232 {
233 if (!Stream_EnsureCapacity(s, i))
234 goto fail;
235 }
236
237 if (!Stream_SetPosition(s, i))
238 goto fail;
239 Stream_SealLength(s);
240
241 if (!TestStream_Verify(s, i, i, i))
242 {
243 printf("%s: failed to verify stream in iteration %" PRIu32 "\n", __func__, i);
244 goto fail;
245 }
246 }
247
248 result = TRUE;
249fail:
250
251 if (s)
252 {
253 Stream_Free(s, TRUE);
254 }
255
256 return result;
257}
258
259WINPR_ATTR_NODISCARD
260static BOOL TestStream_ExtentOverflow(void)
261{
262 const size_t capacity = 256;
263 const size_t position = 20;
264 /* position + size or capacity + size exceed SIZE_MAX */
265 const size_t sizes[] = { SIZE_MAX - capacity + 1, SIZE_MAX - position, SIZE_MAX };
266 BOOL result = FALSE;
267
268 wStream* s = Stream_New(nullptr, capacity);
269 if (!s)
270 {
271 printf("%s: Stream_New failed\n", __func__);
272 return FALSE;
273 }
274
275 if (!Stream_SetPosition(s, position))
276 goto fail;
277
278 for (size_t i = 0; i < ARRAYSIZE(sizes); i++)
279 {
280 if (Stream_EnsureRemainingCapacity(s, sizes[i]))
281 {
282 printf("%s: size %" PRIuz " must be rejected\n", __func__, sizes[i]);
283 goto fail;
284 }
285
286 if ((Stream_Capacity(s) != capacity) || (Stream_GetPosition(s) != position))
287 goto fail;
288 }
289
290 result = TRUE;
291fail:
292 Stream_Free(s, TRUE);
293 return result;
294}
295
296#define Stream_Peek_UINT8_BE Stream_Peek_UINT8
297#define Stream_Read_UINT8_BE Stream_Read_UINT8
298#define Stream_Peek_Get_UINT8_BE Stream_Peek_Get_UINT8
299#define Stream_Get_UINT8_BE Stream_Get_UINT8
300#define Stream_Peek_INT8_BE Stream_Peek_INT8
301#define Stream_Peek_Get_INT8_BE Stream_Peek_Get_INT8
302#define Stream_Read_INT8_BE Stream_Read_INT8
303#define Stream_Get_INT8_BE Stream_Get_INT8
304
305#define TestStream_PeekAndRead(_s, _r, _t) \
306 do \
307 { \
308 _t _a = 0; \
309 _t _b = 0; \
310 BYTE* _p = Stream_Buffer(_s); \
311 Stream_ResetPosition(_s); \
312 Stream_Peek_##_t(_s, _a); \
313 Stream_Read_##_t(_s, _b); \
314 if (_a != _b) \
315 { \
316 printf("%s: test1 " #_t "_LE failed\n", __func__); \
317 (_r) = FALSE; \
318 } \
319 Stream_Rewind(_s, sizeof(_t)); \
320 const _t _d = Stream_Peek_Get_##_t(_s); \
321 const _t _c = Stream_Get_##_t(_s); \
322 if (_c != _d) \
323 { \
324 printf("%s: test1 " #_t "_LE failed\n", __func__); \
325 (_r) = FALSE; \
326 } \
327 for (size_t _i = 0; _i < sizeof(_t); _i++) \
328 { \
329 if (((_a >> (_i * 8)) & 0xFF) != _p[_i]) \
330 { \
331 printf("%s: test2 " #_t "_LE failed\n", __func__); \
332 (_r) = FALSE; \
333 break; \
334 } \
335 } \
336 /* printf("a: 0x%016llX\n", a); */ \
337 Stream_ResetPosition(_s); \
338 Stream_Peek_##_t##_BE(_s, _a); \
339 Stream_Read_##_t##_BE(_s, _b); \
340 if (_a != _b) \
341 { \
342 printf("%s: test1 " #_t "_BE failed\n", __func__); \
343 (_r) = FALSE; \
344 } \
345 Stream_Rewind(_s, sizeof(_t)); \
346 const _t _e = Stream_Peek_Get_##_t##_BE(_s); \
347 const _t _f = Stream_Get_##_t##_BE(_s); \
348 if (_e != _f) \
349 { \
350 printf("%s: test1 " #_t "_BE failed\n", __func__); \
351 (_r) = FALSE; \
352 } \
353 for (size_t _i = 0; _i < sizeof(_t); _i++) \
354 { \
355 if (((_a >> (_i * 8)) & 0xFF) != _p[sizeof(_t) - _i - 1]) \
356 { \
357 printf("%s: test2 " #_t "_BE failed\n", __func__); \
358 (_r) = FALSE; \
359 break; \
360 } \
361 } \
362 /* printf("a: 0x%016llX\n", a); */ \
363 } while (0)
364
365WINPR_ATTR_NODISCARD
366static BOOL TestStream_WriteAndRead(UINT64 value)
367{
368 union
369 {
370 UINT8 u8;
371 UINT16 u16;
372 UINT32 u32;
373 UINT64 u64;
374 INT8 i8;
375 INT16 i16;
376 INT32 i32;
377 INT64 i64;
378 } val;
379 val.u64 = value;
380
381 wStream* s = Stream_New(nullptr, 1024);
382 if (!s)
383 return FALSE;
384 BOOL rc = FALSE;
385
386 {
387 Stream_Write_UINT8(s, val.u8);
388 Stream_Rewind_UINT8(s);
389 const UINT8 ru8 = Stream_Get_UINT8(s);
390 Stream_Rewind_UINT8(s);
391 if (val.u8 != ru8)
392 goto fail;
393 }
394 {
395 Stream_Write_UINT16(s, val.u16);
396 Stream_Rewind_UINT16(s);
397 const UINT16 ru = Stream_Get_UINT16(s);
398 Stream_Rewind_UINT16(s);
399 if (val.u16 != ru)
400 goto fail;
401 }
402 {
403 Stream_Write_UINT16_BE(s, val.u16);
404 Stream_Rewind_UINT16(s);
405 const UINT16 ru = Stream_Get_UINT16_BE(s);
406 Stream_Rewind_UINT16(s);
407 if (val.u16 != ru)
408 goto fail;
409 }
410 {
411 Stream_Write_UINT32(s, val.u32);
412 Stream_Rewind_UINT32(s);
413 const UINT32 ru = Stream_Get_UINT32(s);
414 Stream_Rewind_UINT32(s);
415 if (val.u32 != ru)
416 goto fail;
417 }
418 {
419 Stream_Write_UINT32_BE(s, val.u32);
420 Stream_Rewind_UINT32(s);
421 const UINT32 ru = Stream_Get_UINT32_BE(s);
422 Stream_Rewind_UINT32(s);
423 if (val.u32 != ru)
424 goto fail;
425 }
426 {
427 Stream_Write_UINT64(s, val.u64);
428 Stream_Rewind_UINT64(s);
429 const UINT64 ru = Stream_Get_UINT64(s);
430 Stream_Rewind_UINT64(s);
431 if (val.u64 != ru)
432 goto fail;
433 }
434 {
435 Stream_Write_UINT64_BE(s, val.u64);
436 Stream_Rewind_UINT64(s);
437 const UINT64 ru = Stream_Get_UINT64_BE(s);
438 Stream_Rewind_UINT64(s);
439 if (val.u64 != ru)
440 goto fail;
441 }
442 {
443 Stream_Write_INT8(s, val.i8);
444 Stream_Rewind(s, 1);
445 const INT8 ru8 = Stream_Get_INT8(s);
446 Stream_Rewind(s, 1);
447 if (val.i8 != ru8)
448 goto fail;
449 }
450 {
451 Stream_Write_INT16(s, val.i16);
452 Stream_Rewind(s, 2);
453 const INT16 ru = Stream_Get_INT16(s);
454 Stream_Rewind(s, 2);
455 if (val.i16 != ru)
456 goto fail;
457 }
458 {
459 Stream_Write_INT16_BE(s, val.i16);
460 Stream_Rewind(s, 2);
461 const INT16 ru = Stream_Get_INT16_BE(s);
462 Stream_Rewind(s, 2);
463 if (val.i16 != ru)
464 goto fail;
465 }
466 {
467 Stream_Write_INT32(s, val.i32);
468 Stream_Rewind(s, 4);
469 const INT32 ru = Stream_Get_INT32(s);
470 Stream_Rewind(s, 4);
471 if (val.i32 != ru)
472 goto fail;
473 }
474 {
475 Stream_Write_INT32_BE(s, val.i32);
476 Stream_Rewind(s, 4);
477 const INT32 ru = Stream_Get_INT32_BE(s);
478 Stream_Rewind(s, 4);
479 if (val.i32 != ru)
480 goto fail;
481 }
482 {
483 Stream_Write_INT64(s, val.i64);
484 Stream_Rewind(s, 8);
485 const INT64 ru = Stream_Get_INT64(s);
486 Stream_Rewind(s, 8);
487 if (val.i64 != ru)
488 goto fail;
489 }
490 {
491 Stream_Write_INT64_BE(s, val.i64);
492 Stream_Rewind(s, 8);
493 const INT64 ru = Stream_Get_INT64_BE(s);
494 Stream_Rewind(s, 8);
495 if (val.i64 != ru)
496 goto fail;
497 }
498
499 rc = TRUE;
500fail:
501 Stream_Free(s, TRUE);
502 return rc;
503}
504
505WINPR_ATTR_NODISCARD
506static BOOL TestStream_Reading(void)
507{
508 BYTE src[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
509 wStream* s = nullptr;
510 BOOL result = TRUE;
511
512 if (!(s = Stream_New(src, sizeof(src))))
513 {
514 printf("%s: Stream_New failed\n", __func__);
515 return FALSE;
516 }
517
518 TestStream_PeekAndRead(s, result, UINT8);
519 TestStream_PeekAndRead(s, result, INT8);
520 TestStream_PeekAndRead(s, result, UINT16);
521 TestStream_PeekAndRead(s, result, INT16);
522 TestStream_PeekAndRead(s, result, UINT32);
523 TestStream_PeekAndRead(s, result, INT32);
524 TestStream_PeekAndRead(s, result, UINT64);
525 TestStream_PeekAndRead(s, result, INT64);
526 Stream_Free(s, FALSE);
527 return result;
528}
529
530WINPR_ATTR_NODISCARD
531static BOOL TestStream_Write(void)
532{
533 BOOL rc = FALSE;
534 UINT8 u8 = 0;
535 UINT16 u16 = 0;
536 UINT32 u32 = 0;
537 UINT64 u64 = 0;
538 const BYTE data[] = "someteststreamdata";
539 wStream* s = Stream_New(nullptr, 100);
540
541 if (!s)
542 goto out;
543
544 if (s->pointer != s->buffer)
545 goto out;
546
547 Stream_Write(s, data, sizeof(data));
548
549 if (memcmp(Stream_Buffer(s), data, sizeof(data)) == 0)
550 rc = TRUE;
551
552 if (s->pointer != s->buffer + sizeof(data))
553 goto out;
554
555 Stream_ResetPosition(s);
556
557 if (s->pointer != s->buffer)
558 goto out;
559
560 Stream_Write_UINT8(s, 42);
561
562 if (s->pointer != s->buffer + 1)
563 goto out;
564
565 Stream_ResetPosition(s);
566
567 if (s->pointer != s->buffer)
568 goto out;
569
570 Stream_Peek_UINT8(s, u8);
571
572 if (u8 != 42)
573 goto out;
574
575 Stream_Write_UINT16(s, 0x1234);
576
577 if (s->pointer != s->buffer + 2)
578 goto out;
579
580 Stream_ResetPosition(s);
581
582 if (s->pointer != s->buffer)
583 goto out;
584
585 Stream_Peek_UINT16(s, u16);
586
587 if (u16 != 0x1234)
588 goto out;
589
590 Stream_Write_UINT32(s, 0x12345678UL);
591
592 if (s->pointer != s->buffer + 4)
593 goto out;
594
595 Stream_ResetPosition(s);
596
597 if (s->pointer != s->buffer)
598 goto out;
599
600 Stream_Peek_UINT32(s, u32);
601
602 if (u32 != 0x12345678UL)
603 goto out;
604
605 Stream_Write_UINT64(s, 0x1234567890ABCDEFULL);
606
607 if (s->pointer != s->buffer + 8)
608 goto out;
609
610 Stream_ResetPosition(s);
611
612 if (s->pointer != s->buffer)
613 goto out;
614
615 Stream_Peek_UINT64(s, u64);
616
617 if (u64 != 0x1234567890ABCDEFULL)
618 goto out;
619
620out:
621 Stream_Free(s, TRUE);
622 return rc;
623}
624
625WINPR_ATTR_NODISCARD
626static BOOL TestStream_Seek(void)
627{
628 BOOL rc = FALSE;
629 wStream* s = Stream_New(nullptr, 100);
630
631 if (!s)
632 goto out;
633
634 if (s->pointer != s->buffer)
635 goto out;
636
637 Stream_Seek(s, 5);
638
639 if (s->pointer != s->buffer + 5)
640 goto out;
641
642 Stream_Seek_UINT8(s);
643
644 if (s->pointer != s->buffer + 6)
645 goto out;
646
647 Stream_Seek_UINT16(s);
648
649 if (s->pointer != s->buffer + 8)
650 goto out;
651
652 Stream_Seek_UINT32(s);
653
654 if (s->pointer != s->buffer + 12)
655 goto out;
656
657 Stream_Seek_UINT64(s);
658
659 if (s->pointer != s->buffer + 20)
660 goto out;
661
662 rc = TRUE;
663out:
664 Stream_Free(s, TRUE);
665 return rc;
666}
667
668WINPR_ATTR_NODISCARD
669static BOOL TestStream_Rewind(void)
670{
671 BOOL rc = FALSE;
672 wStream* s = Stream_New(nullptr, 100);
673
674 if (!s)
675 goto out;
676
677 if (s->pointer != s->buffer)
678 goto out;
679
680 Stream_Seek(s, 100);
681
682 if (s->pointer != s->buffer + 100)
683 goto out;
684
685 Stream_Rewind(s, 10);
686
687 if (s->pointer != s->buffer + 90)
688 goto out;
689
690 Stream_Rewind_UINT8(s);
691
692 if (s->pointer != s->buffer + 89)
693 goto out;
694
695 Stream_Rewind_UINT16(s);
696
697 if (s->pointer != s->buffer + 87)
698 goto out;
699
700 Stream_Rewind_UINT32(s);
701
702 if (s->pointer != s->buffer + 83)
703 goto out;
704
705 Stream_Rewind_UINT64(s);
706
707 if (s->pointer != s->buffer + 75)
708 goto out;
709
710 rc = TRUE;
711out:
712 Stream_Free(s, TRUE);
713 return rc;
714}
715
716WINPR_ATTR_NODISCARD
717static BOOL TestStream_Zero(void)
718{
719 BOOL rc = FALSE;
720 const BYTE data[] = "someteststreamdata";
721 wStream* s = Stream_New(nullptr, sizeof(data));
722
723 if (!s)
724 goto out;
725
726 Stream_Write(s, data, sizeof(data));
727
728 if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
729 goto out;
730
731 Stream_ResetPosition(s);
732
733 if (s->pointer != s->buffer)
734 goto out;
735
736 Stream_Zero(s, 5);
737
738 if (s->pointer != s->buffer + 5)
739 goto out;
740
741 if (memcmp(Stream_ConstPointer(s), data + 5, sizeof(data) - 5) != 0)
742 goto out;
743
744 Stream_ResetPosition(s);
745
746 if (s->pointer != s->buffer)
747 goto out;
748
749 for (UINT32 x = 0; x < 5; x++)
750 {
751 UINT8 val = 0;
752 Stream_Read_UINT8(s, val);
753
754 if (val != 0)
755 goto out;
756 }
757
758 rc = TRUE;
759out:
760 Stream_Free(s, TRUE);
761 return rc;
762}
763
764WINPR_ATTR_NODISCARD
765static BOOL TestStream_Fill(void)
766{
767 BOOL rc = FALSE;
768 const BYTE fill[7] = "XXXXXXX";
769 const BYTE data[] = "someteststreamdata";
770 wStream* s = Stream_New(nullptr, sizeof(data));
771
772 if (!s)
773 goto out;
774
775 Stream_Write(s, data, sizeof(data));
776
777 if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
778 goto out;
779
780 Stream_ResetPosition(s);
781
782 if (s->pointer != s->buffer)
783 goto out;
784
785 Stream_Fill(s, fill[0], sizeof(fill));
786
787 if (s->pointer != s->buffer + sizeof(fill))
788 goto out;
789
790 if (memcmp(Stream_ConstPointer(s), data + sizeof(fill), sizeof(data) - sizeof(fill)) != 0)
791 goto out;
792
793 Stream_ResetPosition(s);
794
795 if (s->pointer != s->buffer)
796 goto out;
797
798 if (memcmp(Stream_ConstPointer(s), fill, sizeof(fill)) != 0)
799 goto out;
800
801 rc = TRUE;
802out:
803 Stream_Free(s, TRUE);
804 return rc;
805}
806
807WINPR_ATTR_NODISCARD
808static BOOL TestStream_Copy(void)
809{
810 BOOL rc = FALSE;
811 const BYTE data[] = "someteststreamdata";
812 wStream* s = Stream_New(nullptr, sizeof(data));
813 wStream* d = Stream_New(nullptr, sizeof(data));
814
815 if (!s || !d)
816 goto out;
817
818 if (s->pointer != s->buffer)
819 goto out;
820
821 Stream_Write(s, data, sizeof(data));
822
823 if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
824 goto out;
825
826 if (s->pointer != s->buffer + sizeof(data))
827 goto out;
828
829 Stream_ResetPosition(s);
830
831 if (s->pointer != s->buffer)
832 goto out;
833
834 Stream_Copy(s, d, sizeof(data));
835
836 if (s->pointer != s->buffer + sizeof(data))
837 goto out;
838
839 if (d->pointer != d->buffer + sizeof(data))
840 goto out;
841
842 if (Stream_GetPosition(s) != Stream_GetPosition(d))
843 goto out;
844
845 if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
846 goto out;
847
848 if (memcmp(Stream_Buffer(d), data, sizeof(data)) != 0)
849 goto out;
850
851 rc = TRUE;
852out:
853 Stream_Free(s, TRUE);
854 Stream_Free(d, TRUE);
855 return rc;
856}
857
858WINPR_ATTR_NODISCARD
859static BOOL TestStream_WriteUTF16(void)
860{
861 const char name[] = "UVC Camera (046d:0825)";
862 const char channel[] = "usb-0000:00:07.0-3";
863 const size_t length = strnlen(name, sizeof(name));
864 BOOL success = FALSE;
865 wStream* s = Stream_New(nullptr, 256);
866 if (!s)
867 return FALSE;
868
869 /* Both an aligned RDPECAM header and an unaligned destination. */
870 for (size_t offset = 1; offset <= 2; offset++)
871 {
872 for (size_t fill = 0; fill <= 1; fill++)
873 {
874 memset(Stream_Buffer(s), 0xAA, Stream_Capacity(s));
875 if (!Stream_SetPosition(s, offset))
876 goto out;
877 const SSIZE_T rc =
878 Stream_Write_UTF16_String_From_UTF8(s, length + 1, name, length, (BOOL)fill);
879 const size_t end = offset + (length + fill) * sizeof(WCHAR);
880 if ((rc != (SSIZE_T)length) || (Stream_GetPosition(s) != end))
881 goto out;
882 Stream_Write(s, channel, strnlen(channel, sizeof(channel)) + 1);
883 for (size_t i = 0; i < length; i++)
884 {
885 const BYTE* ptr = Stream_Buffer(s) + offset + i * sizeof(WCHAR);
886 if ((ptr[0] != (BYTE)name[i]) || (ptr[1] != 0))
887 goto out;
888 }
889 if (fill && ((Stream_Buffer(s)[end - 2] != 0) || (Stream_Buffer(s)[end - 1] != 0)))
890 goto out;
891 if (strncmp((const char*)Stream_Buffer(s) + end, channel, sizeof(channel)) != 0)
892 goto out;
893 }
894 }
895
896 /* Allow truncation. */
897 Stream_ResetPosition(s);
898 if (Stream_Write_UTF16_String_From_UTF8(s, 2, name, length, TRUE) != 2)
899 goto out;
900
901 const size_t pos = Stream_GetPosition(s);
902 if (pos != 2 * sizeof(WCHAR))
903 goto out;
904
905 success = TRUE;
906out:
907 if (!success)
908 printf("UTF-16 stream serialization failed\n");
909 Stream_Free(s, TRUE);
910 return success;
911}
912
913int TestStream(int argc, char* argv[])
914{
915 WINPR_UNUSED(argc);
916 WINPR_UNUSED(argv);
917 if (!TestStream_WriteUTF16())
918 return 15;
919
920 if (!TestStream_Create(200, FALSE))
921 return 1;
922
923 if (!TestStream_Create(200, TRUE))
924 return 2;
925
926 if (!TestStream_Extent(4096))
927 return 3;
928
929 if (!TestStream_Reading())
930 return 4;
931
932 if (!TestStream_New())
933 return 5;
934
935 if (!TestStream_Write())
936 return 6;
937
938 if (!TestStream_Seek())
939 return 7;
940
941 if (!TestStream_Rewind())
942 return 8;
943
944 if (!TestStream_Zero())
945 return 9;
946
947 if (!TestStream_Fill())
948 return 10;
949
950 if (!TestStream_Copy())
951 return 11;
952
953 if (!TestStream_Static())
954 return 12;
955
956 if (!TestStream_WriteAndRead(0x1234567890abcdef))
957 return 13;
958
959 for (size_t x = 0; x < 10; x++)
960 {
961 UINT64 val = 0;
962 if (winpr_RAND(&val, sizeof(val)) < 0)
963 return -1;
964 if (!TestStream_WriteAndRead(val))
965 return 14;
966 }
967
968 if (!TestStream_ExtentOverflow())
969 return 16;
970 return 0;
971}