FreeRDP/winpr/libwinpr/utils/test/TestStream.c

684 lines
14 KiB
C
Raw Normal View History

#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/stream.h>
2018-10-25 16:48:48 +03:00
static BOOL TestStream_Verify(wStream* s, size_t mincap, size_t len, size_t pos)
{
if (Stream_Buffer(s) == NULL)
{
printf("stream buffer is null\n");
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_ConstPointer(s) == NULL)
{
printf("stream pointer is null\n");
return FALSE;
}
2018-10-25 16:48:48 +03:00
2023-07-20 10:09:52 +03:00
if (Stream_PointerAs(s, BYTE) < Stream_Buffer(s))
{
printf("stream pointer (%p) or buffer (%p) is invalid\n", Stream_ConstPointer(s),
2019-11-06 17:24:51 +03:00
(void*)Stream_Buffer(s));
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_Capacity(s) < mincap)
{
2019-11-06 17:24:51 +03:00
printf("stream capacity is %" PRIuz " but minimum expected value is %" PRIuz "\n",
2018-10-25 16:48:48 +03:00
Stream_Capacity(s), mincap);
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_Length(s) != len)
{
2019-11-06 17:24:51 +03:00
printf("stream has unexpected length (%" PRIuz " instead of %" PRIuz ")\n",
2018-10-25 16:48:48 +03:00
Stream_Length(s), len);
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_GetPosition(s) != pos)
{
2019-11-06 17:24:51 +03:00
printf("stream has unexpected position (%" PRIuz " instead of %" PRIuz ")\n",
2018-10-25 16:48:48 +03:00
Stream_GetPosition(s), pos);
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_GetPosition(s) > Stream_Length(s))
{
2019-11-06 17:24:51 +03:00
printf("stream position (%" PRIuz ") exceeds length (%" PRIuz ")\n", Stream_GetPosition(s),
Stream_Length(s));
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_GetPosition(s) > Stream_Capacity(s))
{
2019-11-06 17:24:51 +03:00
printf("stream position (%" PRIuz ") exceeds capacity (%" PRIuz ")\n",
2018-10-25 16:48:48 +03:00
Stream_GetPosition(s), Stream_Capacity(s));
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_Length(s) > Stream_Capacity(s))
{
2019-11-06 17:24:51 +03:00
printf("stream length (%" PRIuz ") exceeds capacity (%" PRIuz ")\n", Stream_Length(s),
Stream_Capacity(s));
return FALSE;
}
2018-10-25 16:48:48 +03:00
if (Stream_GetRemainingLength(s) != len - pos)
{
2019-11-06 17:24:51 +03:00
printf("stream remaining length (%" PRIuz " instead of %" PRIuz ")\n",
2018-10-25 16:48:48 +03:00
Stream_GetRemainingLength(s), len - pos);
return FALSE;
}
return TRUE;
}
2018-10-25 16:48:48 +03:00
static BOOL TestStream_New(void)
{
2018-10-25 16:48:48 +03:00
wStream* s = NULL;
/* Test creation of a 0-size stream with no buffer */
s = Stream_New(NULL, 0);
2018-10-25 16:48:48 +03:00
if (s)
return FALSE;
2018-10-25 16:48:48 +03:00
return TRUE;
}
static BOOL TestStream_Static(void)
{
BYTE buffer[20];
wStream staticStream, *s = &staticStream;
UINT16 v;
/* Test creation of a static stream */
Stream_StaticInit(s, buffer, sizeof(buffer));
Stream_Write_UINT16(s, 0xcab1);
Stream_SetPosition(s, 0);
Stream_Read_UINT16(s, v);
2018-10-25 16:48:48 +03:00
if (v != 0xcab1)
return FALSE;
Stream_SetPosition(s, 0);
Stream_Write_UINT16(s, 1);
2018-10-25 16:48:48 +03:00
if (!Stream_EnsureRemainingCapacity(s, 10)) /* we can ask for 10 bytes */
return FALSE;
/* 30 is bigger than the buffer, it will be reallocated on the heap */
if (!Stream_EnsureRemainingCapacity(s, 30) || !s->isOwner)
return FALSE;
2018-10-25 16:48:48 +03:00
Stream_Write_UINT16(s, 2);
Stream_SetPosition(s, 0);
Stream_Read_UINT16(s, v);
2018-10-25 16:48:48 +03:00
if (v != 1)
return FALSE;
2018-10-25 16:48:48 +03:00
Stream_Read_UINT16(s, v);
2018-10-25 16:48:48 +03:00
if (v != 2)
return FALSE;
Stream_Free(s, TRUE);
return TRUE;
}
2018-10-25 16:48:48 +03:00
static BOOL TestStream_Create(size_t count, BOOL selfAlloc)
{
2018-10-25 16:48:48 +03:00
size_t i, len, cap, pos;
wStream* s = NULL;
void* buffer = NULL;
for (i = 0; i < count; i++)
{
2018-10-25 16:48:48 +03:00
len = cap = i + 1;
pos = 0;
if (selfAlloc)
{
if (!(buffer = malloc(cap)))
{
printf("%s: failed to allocate buffer of size %" PRIuz "\n", __func__, cap);
goto fail;
}
}
if (!(s = Stream_New(selfAlloc ? buffer : NULL, len)))
{
printf("%s: Stream_New failed for stream #%" PRIuz "\n", __func__, i);
goto fail;
}
if (!TestStream_Verify(s, cap, len, pos))
{
goto fail;
}
for (pos = 0; pos < len; pos++)
{
Stream_SetPosition(s, pos);
Stream_SealLength(s);
2018-10-25 16:48:48 +03:00
if (!TestStream_Verify(s, cap, pos, pos))
{
goto fail;
}
}
if (selfAlloc)
{
2018-10-25 16:48:48 +03:00
memset(buffer, i % 256, cap);
if (memcmp(buffer, Stream_Buffer(s), cap))
{
printf("%s: buffer memory corruption\n", __func__);
goto fail;
}
}
Stream_Free(s, buffer ? FALSE : TRUE);
free(buffer);
}
return TRUE;
fail:
free(buffer);
2018-10-25 16:48:48 +03:00
if (s)
{
Stream_Free(s, buffer ? FALSE : TRUE);
}
return FALSE;
}
static BOOL TestStream_Extent(UINT32 maxSize)
{
2016-08-10 13:42:57 +03:00
UINT32 i;
2018-10-25 16:48:48 +03:00
wStream* s = NULL;
BOOL result = FALSE;
if (!(s = Stream_New(NULL, 1)))
{
printf("%s: Stream_New failed\n", __func__);
return FALSE;
}
for (i = 1; i < maxSize; i++)
{
if (i % 2)
{
if (!Stream_EnsureRemainingCapacity(s, i))
goto fail;
}
else
{
if (!Stream_EnsureCapacity(s, i))
goto fail;
}
Stream_SetPosition(s, i);
Stream_SealLength(s);
if (!TestStream_Verify(s, i, i, i))
{
printf("%s: failed to verify stream in iteration %" PRIu32 "\n", __func__, i);
goto fail;
}
}
result = TRUE;
fail:
2018-10-25 16:48:48 +03:00
if (s)
{
Stream_Free(s, TRUE);
}
return result;
}
2019-11-06 17:24:51 +03:00
#define Stream_Peek_UINT8_BE Stream_Peek_UINT8
#define Stream_Read_UINT8_BE Stream_Read_UINT8
#define Stream_Peek_INT8_BE Stream_Peek_INT8
#define Stream_Read_INT8_BE Stream_Read_INT8
#define TestStream_PeekAndRead(_s, _r, _t) \
do \
{ \
_t _a, _b; \
size_t _i; \
BYTE* _p = Stream_Buffer(_s); \
Stream_SetPosition(_s, 0); \
Stream_Peek_##_t(_s, _a); \
Stream_Read_##_t(_s, _b); \
if (_a != _b) \
{ \
printf("%s: test1 " #_t "_LE failed\n", __func__); \
_r = FALSE; \
} \
for (_i = 0; _i < sizeof(_t); _i++) \
{ \
if (((_a >> (_i * 8)) & 0xFF) != _p[_i]) \
{ \
printf("%s: test2 " #_t "_LE failed\n", __func__); \
_r = FALSE; \
break; \
} \
} \
/* printf("a: 0x%016llX\n", a); */ \
Stream_SetPosition(_s, 0); \
Stream_Peek_##_t##_BE(_s, _a); \
Stream_Read_##_t##_BE(_s, _b); \
if (_a != _b) \
{ \
printf("%s: test1 " #_t "_BE failed\n", __func__); \
_r = FALSE; \
} \
for (_i = 0; _i < sizeof(_t); _i++) \
{ \
if (((_a >> (_i * 8)) & 0xFF) != _p[sizeof(_t) - _i - 1]) \
{ \
printf("%s: test2 " #_t "_BE failed\n", __func__); \
_r = FALSE; \
break; \
} \
} \
/* printf("a: 0x%016llX\n", a); */ \
2018-10-25 16:48:48 +03:00
} while (0)
static BOOL TestStream_Reading(void)
{
BYTE src[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
2018-10-25 16:48:48 +03:00
wStream* s = NULL;
BOOL result = TRUE;
if (!(s = Stream_New(src, sizeof(src))))
{
printf("%s: Stream_New failed\n", __func__);
return FALSE;
}
TestStream_PeekAndRead(s, result, UINT8);
TestStream_PeekAndRead(s, result, INT8);
TestStream_PeekAndRead(s, result, UINT16);
TestStream_PeekAndRead(s, result, INT16);
TestStream_PeekAndRead(s, result, UINT32);
TestStream_PeekAndRead(s, result, INT32);
TestStream_PeekAndRead(s, result, UINT64);
TestStream_PeekAndRead(s, result, INT64);
Stream_Free(s, FALSE);
return result;
}
static BOOL TestStream_Write(void)
{
BOOL rc = FALSE;
UINT8 u8;
UINT16 u16;
UINT32 u32;
UINT64 u64;
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, 100);
2018-10-25 16:48:48 +03:00
if (!s)
goto out;
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
Stream_Write(s, data, sizeof(data));
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_Buffer(s), data, sizeof(data)) == 0)
rc = TRUE;
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + sizeof(data))
goto out;
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Write_UINT8(s, 42);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 1)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Peek_UINT8(s, u8);
2018-10-25 16:48:48 +03:00
if (u8 != 42)
goto out;
Stream_Write_UINT16(s, 0x1234);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 2)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Peek_UINT16(s, u16);
2018-10-25 16:48:48 +03:00
if (u16 != 0x1234)
goto out;
Stream_Write_UINT32(s, 0x12345678UL);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 4)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Peek_UINT32(s, u32);
2018-10-25 16:48:48 +03:00
if (u32 != 0x12345678UL)
goto out;
Stream_Write_UINT64(s, 0x1234567890ABCDEFULL);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 8)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Peek_UINT64(s, u64);
2018-10-25 16:48:48 +03:00
if (u64 != 0x1234567890ABCDEFULL)
goto out;
2018-10-25 16:48:48 +03:00
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Seek(void)
{
BOOL rc = FALSE;
wStream* s = Stream_New(NULL, 100);
2018-10-25 16:48:48 +03:00
if (!s)
goto out;
if (s->pointer != s->buffer)
goto out;
Stream_Seek(s, 5);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 5)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Seek_UINT8(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 6)
goto out;
Stream_Seek_UINT16(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 8)
goto out;
Stream_Seek_UINT32(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 12)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Seek_UINT64(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 20)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Rewind(void)
{
BOOL rc = FALSE;
wStream* s = Stream_New(NULL, 100);
2018-10-25 16:48:48 +03:00
if (!s)
goto out;
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
Stream_Seek(s, 100);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 100)
goto out;
Stream_Rewind(s, 10);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 90)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Rewind_UINT8(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 89)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Rewind_UINT16(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 87)
goto out;
Stream_Rewind_UINT32(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 83)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Rewind_UINT64(s);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 75)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Zero(void)
{
UINT32 x;
BOOL rc = FALSE;
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, sizeof(data));
2018-10-25 16:48:48 +03:00
if (!s)
goto out;
Stream_Write(s, data, sizeof(data));
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Zero(s, 5);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + 5)
goto out;
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_ConstPointer(s), data + 5, sizeof(data) - 5) != 0)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
for (x = 0; x < 5; x++)
{
UINT8 val;
Stream_Read_UINT8(s, val);
2018-10-25 16:48:48 +03:00
if (val != 0)
goto out;
}
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Fill(void)
{
BOOL rc = FALSE;
const BYTE fill[7] = "XXXXXXX";
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, sizeof(data));
2018-10-25 16:48:48 +03:00
if (!s)
goto out;
Stream_Write(s, data, sizeof(data));
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
Stream_Fill(s, fill[0], sizeof(fill));
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + sizeof(fill))
goto out;
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_ConstPointer(s), data + sizeof(fill), sizeof(data) - sizeof(fill)) != 0)
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_ConstPointer(s), fill, sizeof(fill)) != 0)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Copy(void)
{
BOOL rc = FALSE;
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, sizeof(data));
wStream* d = Stream_New(NULL, sizeof(data));
2018-10-25 16:48:48 +03:00
if (!s || !d)
goto out;
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
Stream_Write(s, data, sizeof(data));
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + sizeof(data))
goto out;
2018-10-25 16:48:48 +03:00
Stream_SetPosition(s, 0);
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer)
goto out;
Stream_Copy(s, d, sizeof(data));
2018-10-25 16:48:48 +03:00
if (s->pointer != s->buffer + sizeof(data))
goto out;
2018-10-25 16:48:48 +03:00
if (d->pointer != d->buffer + sizeof(data))
goto out;
2018-10-25 16:48:48 +03:00
if (Stream_GetPosition(s) != Stream_GetPosition(d))
goto out;
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
2018-10-25 16:48:48 +03:00
if (memcmp(Stream_Buffer(d), data, sizeof(data)) != 0)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
Stream_Free(d, TRUE);
return rc;
}
int TestStream(int argc, char* argv[])
{
2018-10-25 16:48:48 +03:00
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!TestStream_Create(200, FALSE))
return 1;
if (!TestStream_Create(200, TRUE))
return 2;
if (!TestStream_Extent(4096))
return 3;
if (!TestStream_Reading())
return 4;
if (!TestStream_New())
return 5;
if (!TestStream_Write())
return 6;
if (!TestStream_Seek())
return 7;
if (!TestStream_Rewind())
return 8;
if (!TestStream_Zero())
return 9;
if (!TestStream_Fill())
return 10;
if (!TestStream_Copy())
return 11;
if (!TestStream_Static())
return 12;
return 0;
}