FreeRDP
TestPrint.c
1 
2 #include <math.h>
3 #include <winpr/crt.h>
4 #include <winpr/tchar.h>
5 #include <winpr/print.h>
6 
18 #define _printf printf // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
19 
20 static BOOL test_bin_tohex_string(void)
21 {
22  BOOL rc = FALSE;
23  {
24  const BYTE binbuffer[33] = { 0 };
25  const char empty[33] = { 0 };
26  char strbuffer[33] = { 0 };
27 
28  size_t len =
29  winpr_BinToHexStringBuffer(NULL, sizeof(binbuffer), strbuffer, sizeof(strbuffer), TRUE);
30  if (len != 0)
31  goto fail;
32  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
33  goto fail;
34  len = winpr_BinToHexStringBuffer(binbuffer, 0, strbuffer, sizeof(strbuffer), TRUE);
35  if (len != 0)
36  goto fail;
37  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
38  goto fail;
39  len =
40  winpr_BinToHexStringBuffer(binbuffer, sizeof(binbuffer), NULL, sizeof(strbuffer), TRUE);
41  if (len != 0)
42  goto fail;
43  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
44  goto fail;
45  len = winpr_BinToHexStringBuffer(binbuffer, sizeof(binbuffer), strbuffer, 0, TRUE);
46  if (len != 0)
47  goto fail;
48  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
49  goto fail;
50  len = winpr_BinToHexStringBuffer(binbuffer, 0, strbuffer, 0, TRUE);
51  if (len != 0)
52  goto fail;
53  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
54  goto fail;
55  len = winpr_BinToHexStringBuffer(binbuffer, sizeof(binbuffer), NULL, 0, TRUE);
56  if (len != 0)
57  goto fail;
58  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
59  goto fail;
60  len = winpr_BinToHexStringBuffer(NULL, sizeof(binbuffer), strbuffer, 0, TRUE);
61  if (len != 0)
62  goto fail;
63  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
64  goto fail;
65 
66  len = winpr_BinToHexStringBuffer(binbuffer, 0, NULL, 0, TRUE);
67  if (len != 0)
68  goto fail;
69  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
70  goto fail;
71  len = winpr_BinToHexStringBuffer(NULL, 0, NULL, 0, TRUE);
72  if (len != 0)
73  goto fail;
74  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
75  goto fail;
76  len = winpr_BinToHexStringBuffer(binbuffer, 0, NULL, 0, FALSE);
77  if (len != 0)
78  goto fail;
79  if (memcmp(strbuffer, empty, sizeof(strbuffer)) != 0)
80  goto fail;
81  }
82  {
83  const BYTE binbuffer1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
84  const char strbuffer1[] = "0102030405060708090A0B0C0D0E0F1011";
85  const char strbuffer1_space[] = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11";
86 
87  char buffer[1024] = { 0 };
88  size_t len = winpr_BinToHexStringBuffer(binbuffer1, sizeof(binbuffer1), buffer,
89  sizeof(buffer), FALSE);
90  if (len != strnlen(strbuffer1, sizeof(strbuffer1)))
91  goto fail;
92  if (memcmp(strbuffer1, buffer, sizeof(strbuffer1)) != 0)
93  goto fail;
94  len = winpr_BinToHexStringBuffer(binbuffer1, sizeof(binbuffer1), buffer, sizeof(buffer),
95  TRUE);
96  if (len != strnlen(strbuffer1_space, sizeof(strbuffer1_space)))
97  goto fail;
98  if (memcmp(strbuffer1_space, buffer, sizeof(strbuffer1_space)) != 0)
99  goto fail;
100  }
101  {
102  const BYTE binbuffer1[] = { 0xF1, 0xe2, 0xD3, 0xc4, 0xB5, 0xA6, 0x97, 0x88, 0x79,
103  0x6A, 0x5b, 0x4C, 0x3d, 0x2E, 0x1f, 0x00, 0xfF };
104  const char strbuffer1[] = "F1E2D3C4B5A69788796A5B4C3D2E1F00FF";
105  const char strbuffer1_space[] = "F1 E2 D3 C4 B5 A6 97 88 79 6A 5B 4C 3D 2E 1F 00 FF";
106  char buffer[1024] = { 0 };
107  size_t len = winpr_BinToHexStringBuffer(binbuffer1, sizeof(binbuffer1), buffer,
108  sizeof(buffer), FALSE);
109  if (len != strnlen(strbuffer1, sizeof(strbuffer1)))
110  goto fail;
111  if (memcmp(strbuffer1, buffer, sizeof(strbuffer1)) != 0)
112  goto fail;
113  len = winpr_BinToHexStringBuffer(binbuffer1, sizeof(binbuffer1), buffer, sizeof(buffer),
114  TRUE);
115  if (len != strnlen(strbuffer1_space, sizeof(strbuffer1_space)))
116  goto fail;
117  if (memcmp(strbuffer1_space, buffer, sizeof(strbuffer1_space)) != 0)
118  goto fail;
119  }
120  {
121  }
122  rc = TRUE;
123 fail:
124  return rc;
125 }
126 
127 static BOOL test_bin_tohex_string_alloc(void)
128 {
129  BOOL rc = FALSE;
130  char* str = NULL;
131  {
132  const BYTE binbuffer[33] = { 0 };
133 
134  str = winpr_BinToHexString(NULL, sizeof(binbuffer), TRUE);
135  if (str)
136  goto fail;
137  str = winpr_BinToHexString(binbuffer, 0, TRUE);
138  if (str)
139  goto fail;
140  str = winpr_BinToHexString(binbuffer, 0, FALSE);
141  if (str)
142  goto fail;
143  str = winpr_BinToHexString(NULL, sizeof(binbuffer), FALSE);
144  if (str)
145  goto fail;
146  }
147  {
148  const BYTE binbuffer1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
149  const char strbuffer1[] = "0102030405060708090A0B0C0D0E0F1011";
150  const char strbuffer1_space[] = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11";
151 
152  str = winpr_BinToHexString(binbuffer1, sizeof(binbuffer1), FALSE);
153  if (!str)
154  goto fail;
155  if (memcmp(strbuffer1, str, sizeof(strbuffer1)) != 0)
156  goto fail;
157  free(str);
158  str = winpr_BinToHexString(binbuffer1, sizeof(binbuffer1), TRUE);
159  if (!str)
160  goto fail;
161  if (memcmp(strbuffer1_space, str, sizeof(strbuffer1_space)) != 0)
162  goto fail;
163  free(str);
164  str = NULL;
165  }
166  {
167  const BYTE binbuffer1[] = { 0xF1, 0xe2, 0xD3, 0xc4, 0xB5, 0xA6, 0x97, 0x88, 0x79,
168  0x6A, 0x5b, 0x4C, 0x3d, 0x2E, 0x1f, 0x00, 0xfF };
169  const char strbuffer1[] = "F1E2D3C4B5A69788796A5B4C3D2E1F00FF";
170  const char strbuffer1_space[] = "F1 E2 D3 C4 B5 A6 97 88 79 6A 5B 4C 3D 2E 1F 00 FF";
171  str = winpr_BinToHexString(binbuffer1, sizeof(binbuffer1), FALSE);
172  if (!str)
173  goto fail;
174  if (memcmp(strbuffer1, str, sizeof(strbuffer1)) != 0)
175  goto fail;
176 
177  free(str);
178  str = winpr_BinToHexString(binbuffer1, sizeof(binbuffer1), TRUE);
179  if (!str)
180  goto fail;
181  if (memcmp(strbuffer1_space, str, sizeof(strbuffer1_space)) != 0)
182  goto fail;
183  free(str);
184  str = NULL;
185  }
186  rc = TRUE;
187 fail:
188  free(str);
189  return rc;
190 }
191 
192 static BOOL test_hex_string_to_bin(void)
193 {
194  BOOL rc = FALSE;
195  {
196  const char stringbuffer[] = "123456789ABCDEFabcdef";
197  const BYTE empty[1024] = { 0 };
198  BYTE buffer[1024] = { 0 };
199  size_t len = winpr_HexStringToBinBuffer(NULL, 0, NULL, 0);
200  if (len != 0)
201  goto fail;
202  if (memcmp(buffer, empty, sizeof(buffer)) != 0)
203  goto fail;
204  len = winpr_HexStringToBinBuffer(NULL, sizeof(stringbuffer), buffer, sizeof(buffer));
205  if (len != 0)
206  goto fail;
207  if (memcmp(buffer, empty, sizeof(buffer)) != 0)
208  goto fail;
209  len = winpr_HexStringToBinBuffer(stringbuffer, 0, buffer, sizeof(buffer));
210  if (len != 0)
211  goto fail;
212  if (memcmp(buffer, empty, sizeof(buffer)) != 0)
213  goto fail;
214  len = winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), NULL, sizeof(buffer));
215  if (len != 0)
216  goto fail;
217  if (memcmp(buffer, empty, sizeof(buffer)) != 0)
218  goto fail;
219  len = winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer, 0);
220  if (len != 0)
221  goto fail;
222  if (memcmp(buffer, empty, sizeof(buffer)) != 0)
223  goto fail;
224  }
225  {
226  const char stringbuffer[] = "123456789ABCDEF1abcdef";
227  const BYTE expected[] = {
228  0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0xab, 0xcd, 0xef
229  };
230  BYTE buffer[32] = { 0 };
231  size_t len =
232  winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer, sizeof(buffer));
233  if (len != sizeof(expected))
234  goto fail;
235  if (memcmp(buffer, expected, sizeof(expected)) != 0)
236  goto fail;
237  len = winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer,
238  sizeof(expected) / 2);
239  if (len != sizeof(expected) / 2)
240  goto fail;
241  if (memcmp(buffer, expected, sizeof(expected) / 2) != 0)
242  goto fail;
243  }
244  {
245  const char stringbuffer[] = "12 34 56 78 9A BC DE F1 ab cd ef";
246  const BYTE expected[] = {
247  0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0xab, 0xcd, 0xef
248  };
249  BYTE buffer[1024] = { 0 };
250  size_t len =
251  winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer, sizeof(buffer));
252  if (len != sizeof(expected))
253  goto fail;
254  if (memcmp(buffer, expected, sizeof(expected)) != 0)
255  goto fail;
256  len = winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer,
257  sizeof(expected) / 2);
258  if (len != sizeof(expected) / 2)
259  goto fail;
260  if (memcmp(buffer, expected, sizeof(expected) / 2) != 0)
261  goto fail;
262  }
263  {
264  const char stringbuffer[] = "123456789ABCDEF1abcdef9";
265  const BYTE expected[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
266  0xde, 0xf1, 0xab, 0xcd, 0xef, 0x09 };
267  BYTE buffer[1024] = { 0 };
268  size_t len =
269  winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer, sizeof(buffer));
270  if (len != sizeof(expected))
271  goto fail;
272  if (memcmp(buffer, expected, sizeof(expected)) != 0)
273  goto fail;
274  len = winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer,
275  sizeof(expected) / 2);
276  if (len != sizeof(expected) / 2)
277  goto fail;
278  if (memcmp(buffer, expected, sizeof(expected) / 2) != 0)
279  goto fail;
280  }
281  {
282  const char stringbuffer[] = "12 34 56 78 9A BC DE F1 ab cd ef 9";
283  const BYTE expected[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
284  0xde, 0xf1, 0xab, 0xcd, 0xef, 0x09 };
285  BYTE buffer[1024] = { 0 };
286  size_t len =
287  winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer, sizeof(buffer));
288  if (len != sizeof(expected))
289  goto fail;
290  if (memcmp(buffer, expected, sizeof(expected)) != 0)
291  goto fail;
292  len = winpr_HexStringToBinBuffer(stringbuffer, sizeof(stringbuffer), buffer,
293  sizeof(expected) / 2);
294  if (len != sizeof(expected) / 2)
295  goto fail;
296  if (memcmp(buffer, expected, sizeof(expected) / 2) != 0)
297  goto fail;
298  }
299  rc = TRUE;
300 fail:
301  return rc;
302 }
303 
304 int TestPrint(int argc, char* argv[])
305 {
306  int a = 0;
307  int b = 0;
308  float c = NAN;
309  float d = NAN;
310 
311  WINPR_UNUSED(argc);
312  WINPR_UNUSED(argv);
313 
321  a = 15;
322  b = a / 2;
323  _printf("%d\n", b);
324  _printf("%3d\n", b);
325  _printf("%03d\n", b);
326  c = 15.3f;
327  d = c / 3.0f;
328  _printf("%3.2f\n", d);
329 
349  for (int a = 0; a <= 300; a = a + 20)
350  _printf("%3d %06.3f\n", a, (5.0 / 9.0) * (a - 32));
351 
364  _printf("The color: %s\n", "blue");
365  _printf("First number: %d\n", 12345);
366  _printf("Second number: %04d\n", 25);
367  _printf("Third number: %i\n", 1234);
368  _printf("Float number: %3.2f\n", 3.14159);
369  _printf("Hexadecimal: %x/%X\n", 255, 255);
370  _printf("Octal: %o\n", 255);
371  _printf("Unsigned value: %u\n", 150);
372  _printf("Just print the percentage sign %%\n");
373 
385  _printf(":%s:\n", "Hello, world!");
386  _printf(":%15s:\n", "Hello, world!");
387  _printf(":%.10s:\n", "Hello, world!");
388  _printf(":%-10s:\n", "Hello, world!");
389  _printf(":%-15s:\n", "Hello, world!");
390  _printf(":%.15s:\n", "Hello, world!");
391  _printf(":%15.10s:\n", "Hello, world!");
392  _printf(":%-15.10s:\n", "Hello, world!");
393 
394  if (!test_bin_tohex_string())
395  return -1;
396  if (!test_bin_tohex_string_alloc())
397  return -1;
398  if (!test_hex_string_to_bin())
399  return -1;
400  return 0;
401 }