FreeRDP
TestFreeRDPCodecXCrush.c
1 #include <winpr/crt.h>
2 #include <winpr/print.h>
3 
4 #include "../xcrush.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_DATA_XCRUSH[] =
9  "\x12\x00\x66\x6f\x72\x2e\x77\x68\x6f\x6d\x2e\x74\x68\x65\x2e\x62"
10  "\x65\x6c\x6c\x2e\x74\x6f\x6c\x6c\x73\x2c\x2e\x74\x68\x65\x2e\x62"
11  "\x65\x6c\x6c\x2e\x74\x6f\x6c\x6c\x73\x2e\x66\x6f\x72\x2e\x74\x68"
12  "\x65";
13 
14 static const BYTE TEST_ISLAND_DATA[] = "No man is an island entire of itself; every man "
15  "is a piece of the continent, a part of the main; "
16  "if a clod be washed away by the sea, Europe "
17  "is the less, as well as if a promontory were, as"
18  "well as any manner of thy friends or of thine "
19  "own were; any man's death diminishes me, "
20  "because I am involved in mankind. "
21  "And therefore never send to know for whom "
22  "the bell tolls; it tolls for thee.";
23 
24 static const BYTE TEST_ISLAND_DATA_XCRUSH[] =
25  "\x12\x61\x4e\x6f\x20\x6d\x61\x6e\x20\x69\x73\x20\xf8\xd2\xd8\xc2"
26  "\xdc\xc8\x40\xca\xdc\xe8\xd2\xe4\xca\x40\xde\xcc\x40\xd2\xe8\xe6"
27  "\xca\xd8\xcc\x76\x40\xca\xec\xca\xe4\xf3\xfa\x71\x20\x70\x69\x65"
28  "\x63\xfc\x12\xe8\xd0\xca\x40\xc6\xdf\xfb\xcd\xdf\xd0\x58\x40\xc2"
29  "\x40\xe0\xc2\xe4\xe9\xfe\x63\xec\xc3\x6b\x0b\x4b\x71\xd9\x03\x4b"
30  "\x37\xd7\x31\xb6\x37\xb2\x10\x31\x32\x90\x3b\xb0\xb9\xb4\x32\xb2"
31  "\x10\x30\xbb\xb0\xbc\x90\x31\x3c\x90\x7e\x68\x73\x65\x61\x2c\x20"
32  "\x45\x75\x72\x6f\x70\x65\xf2\x34\x7d\x38\x6c\x65\x73\x73\xf0\x69"
33  "\xcc\x81\xdd\x95\xb1\xb0\x81\x85\xcf\xc0\x94\xe0\xe4\xde\xdb\xe2"
34  "\xb3\x7f\x92\x4e\xec\xae\x4c\xbf\x86\x3f\x06\x0c\x2d\xde\x5d\x96"
35  "\xe6\x57\x2f\x1e\x53\xc9\x03\x33\x93\x4b\x2b\x73\x23\x99\x03\x7f"
36  "\xd2\xb6\x96\xef\x38\x1d\xdb\xbc\x24\x72\x65\x3b\xf5\x5b\xf8\x49"
37  "\x3b\x99\x03\x23\x2b\x0b\xa3\x41\x03\x23\x4b\x6b\x4b\x73\x4f\x96"
38  "\xce\x64\x0d\xbe\x19\x31\x32\xb1\xb0\xba\xb9\xb2\x90\x24\x90\x30"
39  "\xb6\x90\x34\xb7\x3b\x37\xb6\x3b\x79\xd4\xd2\xdd\xec\x18\x6b\x69"
40  "\x6e\x64\x2e\x20\x41\xf7\x33\xcd\x47\x26\x56\x66\xff\x74\x9b\xbd"
41  "\xbf\x04\x0e\x7e\x31\x10\x3a\x37\x90\x35\xb7\x37\xbb\x90\x7d\x81"
42  "\x03\xbb\x43\x7b\x6f\xa8\xe5\x8b\xd0\xf0\xe8\xde\xd8\xd8\xe7\xec"
43  "\xf3\xa7\xe4\x7c\xa7\xe2\x9f\x01\x99\x4b\x80";
44 
45 static void test_dump(const char* fkt, const void* generated, size_t generated_size,
46  const void* expected, size_t expected_size)
47 {
48  printf("[%s] output size mismatch: Actual: %" PRIuz ", Expected: %" PRIuz "\n", fkt,
49  generated_size, expected_size);
50  printf("[%s] Actual\n", fkt);
51  BitDump(fkt, WLOG_INFO, generated, generated_size * 8ull, 0);
52  printf("[%s] Expected\n", fkt);
53  BitDump(fkt, WLOG_INFO, expected, expected_size * 8ull, 0);
54 }
55 
56 static BOOL test_compare(const char* fkt, const void* generated, size_t generated_size,
57  const void* expected, size_t expected_size)
58 {
59  if (generated_size != expected_size)
60  {
61  test_dump(fkt, generated, generated_size, expected, expected_size);
62  return FALSE;
63  }
64 
65  if (memcmp(generated, expected, generated_size) != 0)
66  {
67  test_dump(fkt, generated, generated_size, expected, expected_size);
68  return FALSE;
69  }
70 
71  return TRUE;
72 }
73 
74 static BOOL test_run(const char* fkt, const void* src, UINT32 src_size, const void* expected,
75  size_t expected_size)
76 {
77  BOOL rc = FALSE;
78  int status = -1;
79  UINT32 Flags = 0;
80  const BYTE* pDstData = NULL;
81  BYTE OutputBuffer[65536] = { 0 };
82  UINT32 DstSize = sizeof(OutputBuffer);
83  XCRUSH_CONTEXT* xcrush = xcrush_context_new(TRUE);
84  if (!xcrush)
85  return -1;
86  status = xcrush_compress(xcrush, src, src_size, OutputBuffer, &pDstData, &DstSize, &Flags);
87  printf("[%s] status: %d Flags: 0x%08" PRIX32 " DstSize: %" PRIu32 "\n", fkt, status, Flags,
88  DstSize);
89 
90  rc = test_compare(fkt, pDstData, DstSize, expected, expected_size);
91 
92  xcrush_context_free(xcrush);
93  return rc;
94 }
95 
96 struct test_argument
97 {
98  const char* name;
99  const void* src;
100  UINT32 src_size;
101  const void* expected;
102  size_t expected_size;
103 };
104 
105 static const struct test_argument tests[] = {
106  { "XCrushCompressIsland", TEST_ISLAND_DATA, sizeof(TEST_ISLAND_DATA) - 1,
107  TEST_ISLAND_DATA_XCRUSH, sizeof(TEST_ISLAND_DATA_XCRUSH) - 1 }
108 #if 0
109  ,{ "XCrushCompressBells", TEST_BELLS_DATA, sizeof(TEST_BELLS_DATA) - 1, TEST_BELLS_DATA_XCRUSH,
110  sizeof(TEST_BELLS_DATA_XCRUSH) - 1 }
111 #endif
112 };
113 
114 int TestFreeRDPCodecXCrush(int argc, char* argv[])
115 {
116  int rc = 0;
117 
118  WINPR_UNUSED(argc);
119  WINPR_UNUSED(argv);
120 
121  for (size_t x = 0; x < ARRAYSIZE(tests); x++)
122  {
123  const struct test_argument* arg = &tests[x];
124 
125  if (!test_run(arg->name, arg->src, arg->src_size, arg->expected, arg->expected_size))
126  rc = -1;
127  }
128 
129  return rc;
130 }