FreeRDP
TestFreeRDPCodecNCrush.c
1 #include <winpr/crt.h>
2 #include <winpr/print.h>
3 
4 #include "../ncrush.h"
5 
6 static const BYTE TEST_BELLS_DATA[] = "for.whom.the.bell.tolls,.the.bell.tolls.for.thee!";
7 
8 static const BYTE TEST_BELLS_NCRUSH[] =
9  "\xfb\x1d\x7e\xe4\xda\xc7\x1d\x70\xf8\xa1\x6b\x1f\x7d\xc0\xbe\x6b"
10  "\xef\xb5\xef\x21\x87\xd0\xc5\xe1\x85\x71\xd4\x10\x16\xe7\xda\xfb"
11  "\x1d\x7e\xe4\xda\x47\x1f\xb0\xef\xbe\xbd\xff\x2f";
12 
13 static BOOL test_NCrushCompressBells(void)
14 {
15  BOOL rc = FALSE;
16  int status = 0;
17  UINT32 Flags = 0;
18  const BYTE* pDstData = NULL;
19  BYTE OutputBuffer[65536] = { 0 };
20  const UINT32 SrcSize = sizeof(TEST_BELLS_DATA) - 1;
21  const BYTE* pSrcData = TEST_BELLS_DATA;
22  const UINT32 expectedSize = sizeof(TEST_BELLS_NCRUSH) - 1;
23  UINT32 DstSize = sizeof(OutputBuffer);
24  NCRUSH_CONTEXT* ncrush = ncrush_context_new(TRUE);
25 
26  if (!ncrush)
27  return rc;
28 
29  status = ncrush_compress(ncrush, pSrcData, SrcSize, OutputBuffer, &pDstData, &DstSize, &Flags);
30 
31  if (status < 0)
32  goto fail;
33 
34  printf("status: %d Flags: 0x%08" PRIX32 " DstSize: %" PRIu32 "\n", status, Flags, DstSize);
35 
36  if (DstSize != expectedSize)
37  {
38  printf("NCrushCompressBells: output size mismatch: Actual: %" PRIu32 ", Expected: %" PRIu32
39  "\n",
40  DstSize, expectedSize);
41  printf("Actual\n");
42  BitDump(__func__, WLOG_INFO, pDstData, DstSize * 8, 0);
43  printf("Expected\n");
44  BitDump(__func__, WLOG_INFO, TEST_BELLS_NCRUSH, expectedSize * 8, 0);
45  goto fail;
46  }
47 
48  if (memcmp(pDstData, TEST_BELLS_NCRUSH, DstSize) != 0)
49  {
50  printf("NCrushCompressBells: output mismatch\n");
51  printf("Actual\n");
52  BitDump(__func__, WLOG_INFO, pDstData, DstSize * 8, 0);
53  printf("Expected\n");
54  BitDump(__func__, WLOG_INFO, TEST_BELLS_NCRUSH, expectedSize * 8, 0);
55  goto fail;
56  }
57 
58  rc = TRUE;
59 fail:
60  ncrush_context_free(ncrush);
61  return rc;
62 }
63 
64 static BOOL test_NCrushDecompressBells(void)
65 {
66  BOOL rc = FALSE;
67  int status = 0;
68  UINT32 Flags = 0;
69  const BYTE* pSrcData = NULL;
70  UINT32 SrcSize = 0;
71  UINT32 DstSize = 0;
72  UINT32 expectedSize = 0;
73  const BYTE* pDstData = NULL;
74  NCRUSH_CONTEXT* ncrush = ncrush_context_new(FALSE);
75 
76  if (!ncrush)
77  return rc;
78 
79  SrcSize = sizeof(TEST_BELLS_NCRUSH) - 1;
80  pSrcData = (const BYTE*)TEST_BELLS_NCRUSH;
81  Flags = PACKET_COMPRESSED | 2;
82  expectedSize = sizeof(TEST_BELLS_DATA) - 1;
83  status = ncrush_decompress(ncrush, pSrcData, SrcSize, &pDstData, &DstSize, Flags);
84 
85  if (status < 0)
86  goto fail;
87 
88  printf("Flags: 0x%08" PRIX32 " DstSize: %" PRIu32 "\n", Flags, DstSize);
89 
90  if (DstSize != expectedSize)
91  {
92  printf("NCrushDecompressBells: output size mismatch: Actual: %" PRIu32
93  ", Expected: %" PRIu32 "\n",
94  DstSize, expectedSize);
95  goto fail;
96  }
97 
98  if (memcmp(pDstData, TEST_BELLS_DATA, DstSize) != 0)
99  {
100  printf("NCrushDecompressBells: output mismatch\n");
101  goto fail;
102  }
103 
104  rc = TRUE;
105 fail:
106  ncrush_context_free(ncrush);
107  return rc;
108 }
109 
110 int TestFreeRDPCodecNCrush(int argc, char* argv[])
111 {
112  WINPR_UNUSED(argc);
113  WINPR_UNUSED(argv);
114 
115  if (!test_NCrushCompressBells())
116  return -1;
117 
118  if (!test_NCrushDecompressBells())
119  return -1;
120 
121  return 0;
122 }