Blame libfreerdp/codec/test/TestFreeRDPCodecZGfx.c

Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/print.h>
Packit 1fb8d4
#include <winpr/bitstream.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/freerdp.h>
Packit 1fb8d4
#include <freerdp/codec/zgfx.h>
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
Packit 1fb8d4
/* Sample from [MS-RDPEGFX] */
Packit Service 5a9772
static const BYTE TEST_FOX_DATA[] = "The quick brown "
Packit Service 5a9772
                                    "fox jumps over t"
Packit Service 5a9772
                                    "he lazy dog";
Packit 1fb8d4
Packit 1fb8d4
static const BYTE TEST_FOX_DATA_SINGLE[] =
Packit 1fb8d4
    "\xE0\x04\x54\x68\x65\x20\x71\x75\x69\x63\x6B\x20\x62\x72\x6F\x77"
Packit 1fb8d4
    "\x6E\x20\x66\x6F\x78\x20\x6A\x75\x6D\x70\x73\x20\x6F\x76\x65\x72"
Packit 1fb8d4
    "\x20\x74\x68\x65\x20\x6C\x61\x7A\x79\x20\x64\x6F\x67";
Packit 1fb8d4
Packit 1fb8d4
static const BYTE TEST_FOX_DATA_MULTIPART[] =
Packit 1fb8d4
    "\xE1\x03\x00\x2B\x00\x00\x00\x11\x00\x00\x00\x04\x54\x68\x65\x20"
Packit 1fb8d4
    "\x71\x75\x69\x63\x6B\x20\x62\x72\x6F\x77\x6E\x20\x0E\x00\x00\x00"
Packit 1fb8d4
    "\x04\x66\x6F\x78\x20\x6A\x75\x6D\x70\x73\x20\x6F\x76\x65\x10\x00"
Packit 1fb8d4
    "\x00\x00\x24\x39\x08\x0E\x91\xF8\xD8\x61\x3D\x1E\x44\x06\x43\x79"
Packit 1fb8d4
    "\x9C\x02";
Packit 1fb8d4
Packit 1fb8d4
static int test_ZGfxCompressFox(void)
Packit 1fb8d4
{
Packit 1fb8d4
	int rc = -1;
Packit 1fb8d4
	int status;
Packit 1fb8d4
	UINT32 Flags;
Packit 1fb8d4
	BYTE* pSrcData = NULL;
Packit 1fb8d4
	UINT32 SrcSize;
Packit 1fb8d4
	UINT32 DstSize;
Packit 1fb8d4
	BYTE* pDstData = NULL;
Packit 1fb8d4
	ZGFX_CONTEXT* zgfx;
Packit 1fb8d4
	UINT32 expectedSize;
Packit 1fb8d4
	zgfx = zgfx_context_new(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!zgfx)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	SrcSize = sizeof(TEST_FOX_DATA) - 1;
Packit Service 5a9772
	pSrcData = (BYTE*)TEST_FOX_DATA;
Packit 1fb8d4
	Flags = 0;
Packit 1fb8d4
	expectedSize = sizeof(TEST_FOX_DATA_SINGLE) - 1;
Packit 1fb8d4
	status = zgfx_compress(zgfx, pSrcData, SrcSize, &pDstData, &DstSize, &Flags);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	printf("flags: 0x%08" PRIX32 " size: %" PRIu32 "\n", Flags, DstSize);
Packit 1fb8d4
Packit 1fb8d4
	if (DstSize != expectedSize)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("test_ZGfxCompressFox: output size mismatch: Actual: %" PRIu32 ", Expected: %" PRIu32
Packit Service 5a9772
		       "\n",
Packit 1fb8d4
		       DstSize, expectedSize);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (memcmp(pDstData, TEST_FOX_DATA_SINGLE, DstSize) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("test_ZGfxCompressFox: output mismatch\n");
Packit 1fb8d4
		printf("Actual\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData, DstSize * 8, 0);
Packit 1fb8d4
		printf("Expected\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, TEST_FOX_DATA_SINGLE, DstSize * 8, 0);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = 0;
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(pDstData);
Packit 1fb8d4
	zgfx_context_free(zgfx);
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int test_ZGfxDecompressFoxSingle(void)
Packit 1fb8d4
{
Packit 1fb8d4
	int rc = -1;
Packit 1fb8d4
	int status;
Packit 1fb8d4
	UINT32 Flags;
Packit 1fb8d4
	BYTE* pSrcData;
Packit 1fb8d4
	UINT32 SrcSize;
Packit 1fb8d4
	UINT32 DstSize;
Packit 1fb8d4
	BYTE* pDstData = NULL;
Packit 1fb8d4
	ZGFX_CONTEXT* zgfx;
Packit 1fb8d4
	UINT32 expectedSize;
Packit 1fb8d4
	zgfx = zgfx_context_new(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!zgfx)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	SrcSize = sizeof(TEST_FOX_DATA_SINGLE) - 1;
Packit Service 5a9772
	pSrcData = (BYTE*)TEST_FOX_DATA_SINGLE;
Packit 1fb8d4
	Flags = 0;
Packit 1fb8d4
	expectedSize = sizeof(TEST_FOX_DATA) - 1;
Packit 1fb8d4
	status = zgfx_decompress(zgfx, pSrcData, SrcSize, &pDstData, &DstSize, Flags);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	printf("flags: 0x%08" PRIX32 " size: %" PRIu32 "\n", Flags, DstSize);
Packit 1fb8d4
Packit 1fb8d4
	if (DstSize != expectedSize)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("test_ZGfxDecompressFoxSingle: output size mismatch: Actual: %" PRIu32
Packit Service 5a9772
		       ", Expected: %" PRIu32 "\n",
Packit 1fb8d4
		       DstSize, expectedSize);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (memcmp(pDstData, TEST_FOX_DATA, DstSize) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("test_ZGfxDecompressFoxSingle: output mismatch\n");
Packit 1fb8d4
		printf("Actual\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData, DstSize * 8, 0);
Packit 1fb8d4
		printf("Expected\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, TEST_FOX_DATA, DstSize * 8, 0);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = 0;
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(pDstData);
Packit 1fb8d4
	zgfx_context_free(zgfx);
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int test_ZGfxDecompressFoxMultipart(void)
Packit 1fb8d4
{
Packit 1fb8d4
	int rc = -1;
Packit 1fb8d4
	int status;
Packit 1fb8d4
	UINT32 Flags;
Packit 1fb8d4
	BYTE* pSrcData;
Packit 1fb8d4
	UINT32 SrcSize;
Packit 1fb8d4
	UINT32 DstSize;
Packit 1fb8d4
	BYTE* pDstData = NULL;
Packit 1fb8d4
	ZGFX_CONTEXT* zgfx;
Packit 1fb8d4
	UINT32 expectedSize;
Packit 1fb8d4
	zgfx = zgfx_context_new(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!zgfx)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	SrcSize = sizeof(TEST_FOX_DATA_MULTIPART) - 1;
Packit Service 5a9772
	pSrcData = (BYTE*)TEST_FOX_DATA_MULTIPART;
Packit 1fb8d4
	Flags = 0;
Packit 1fb8d4
	expectedSize = sizeof(TEST_FOX_DATA) - 1;
Packit 1fb8d4
	status = zgfx_decompress(zgfx, pSrcData, SrcSize, &pDstData, &DstSize, Flags);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	printf("flags: 0x%08" PRIX32 " size: %" PRIu32 "\n", Flags, DstSize);
Packit 1fb8d4
Packit 1fb8d4
	if (DstSize != expectedSize)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("test_ZGfxDecompressFoxSingle: output size mismatch: Actual: %" PRIu32
Packit Service 5a9772
		       ", Expected: %" PRIu32 "\n",
Packit 1fb8d4
		       DstSize, expectedSize);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (memcmp(pDstData, TEST_FOX_DATA, DstSize) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("test_ZGfxDecompressFoxSingle: output mismatch\n");
Packit 1fb8d4
		printf("Actual\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData, DstSize * 8, 0);
Packit 1fb8d4
		printf("Expected\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, TEST_FOX_DATA, DstSize * 8, 0);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = 0;
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(pDstData);
Packit 1fb8d4
	zgfx_context_free(zgfx);
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int test_ZGfxCompressConsistent(void)
Packit 1fb8d4
{
Packit 1fb8d4
	int rc = -1;
Packit 1fb8d4
	int status;
Packit 1fb8d4
	UINT32 Flags;
Packit 1fb8d4
	BYTE* pSrcData;
Packit 1fb8d4
	UINT32 SrcSize;
Packit 1fb8d4
	UINT32 DstSize;
Packit 1fb8d4
	BYTE* pDstData = NULL;
Packit 1fb8d4
	UINT32 DstSize2;
Packit 1fb8d4
	BYTE* pDstData2 = NULL;
Packit 1fb8d4
	ZGFX_CONTEXT* zgfx;
Packit 1fb8d4
	UINT32 expectedSize;
Packit 1fb8d4
	BYTE BigBuffer[65536];
Packit 1fb8d4
	memset(BigBuffer, 0xaa, sizeof(BigBuffer));
Packit 1fb8d4
	memcpy(BigBuffer, TEST_FOX_DATA, sizeof(TEST_FOX_DATA) - 1);
Packit 1fb8d4
	zgfx = zgfx_context_new(TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!zgfx)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* Compress */
Packit 1fb8d4
	expectedSize = SrcSize = sizeof(BigBuffer);
Packit Service 5a9772
	pSrcData = (BYTE*)BigBuffer;
Packit 1fb8d4
	Flags = 0;
Packit 1fb8d4
	status = zgfx_compress(zgfx, pSrcData, SrcSize, &pDstData2, &DstSize2, &Flags);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	printf("Compress: flags: 0x%08" PRIX32 " size: %" PRIu32 "\n", Flags, DstSize2);
Packit 1fb8d4
	/* Decompress */
Packit 1fb8d4
	status = zgfx_decompress(zgfx, pDstData2, DstSize2, &pDstData, &DstSize, Flags);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	printf("Decompress: flags: 0x%08" PRIX32 " size: %" PRIu32 "\n", Flags, DstSize);
Packit 1fb8d4
Packit 1fb8d4
	if (DstSize != expectedSize)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("test_ZGfxDecompressFoxSingle: output size mismatch: Actual: %" PRIu32
Packit Service 5a9772
		       ", Expected: %" PRIu32 "\n",
Packit 1fb8d4
		       DstSize, expectedSize);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (memcmp(pDstData, BigBuffer, DstSize) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("test_ZGfxDecompressFoxSingle: output mismatch\n");
Packit 1fb8d4
		printf("Actual\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData, 64 * 8, 0);
Packit 1fb8d4
		printf("...\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData + DstSize - 64, 64 * 8, 0);
Packit 1fb8d4
		printf("Expected\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, BigBuffer, 64 * 8, 0);
Packit 1fb8d4
		printf("...\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, BigBuffer + DstSize - 64, 64 * 8, 0);
Packit 1fb8d4
		printf("Middle Result\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData2, 64 * 8, 0);
Packit 1fb8d4
		printf("...\n");
Packit 1fb8d4
		BitDump(__FUNCTION__, WLOG_INFO, pDstData2 + DstSize2 - 64, 64 * 8, 0);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = 0;
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(pDstData);
Packit 1fb8d4
	free(pDstData2);
Packit 1fb8d4
	zgfx_context_free(zgfx);
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int TestFreeRDPCodecZGfx(int argc, char* argv[])
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(argc);
Packit Service 5a9772
	WINPR_UNUSED(argv);
Packit Service 5a9772
Packit 1fb8d4
	if (test_ZGfxCompressFox() < 0)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (test_ZGfxDecompressFoxSingle() < 0)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (test_ZGfxDecompressFoxMultipart() < 0)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (test_ZGfxCompressConsistent() < 0)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}