PageRenderTime 35ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test-printf.c

https://github.com/TeamBAKED/external_wpa_supplicant_8_ti
C | 83 lines | 63 code | 13 blank | 7 comment | 9 complexity | b4837d854a9554c22cfa166ec475a02e MD5 | raw file
  1. /*
  2. * printf format routines - test program
  3. * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/os.h"
  10. #include "utils/common.h"
  11. struct test_data {
  12. u8 *data;
  13. size_t len;
  14. char *encoded;
  15. };
  16. static const struct test_data tests[] = {
  17. { (u8 *) "abcde", 5, "abcde" },
  18. { (u8 *) "a\0b\nc\ed\re\tf", 11, "a\\0b\\nc\\ed\\re\\tf" },
  19. { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
  20. { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
  21. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  22. "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
  23. { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
  24. "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
  25. { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
  26. "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
  27. { NULL, 0, NULL }
  28. };
  29. static void print_hex(const u8 *data, size_t len)
  30. {
  31. size_t i;
  32. for (i = 0; i < len; i++)
  33. printf(" %02x", data[i]);
  34. }
  35. int main(int argc, char *argv[])
  36. {
  37. int i;
  38. size_t binlen;
  39. char buf[100];
  40. u8 bin[100];
  41. int errors = 0;
  42. for (i = 0; tests[i].data; i++) {
  43. const struct test_data *test = &tests[i];
  44. printf("%d:", i);
  45. print_hex(test->data, test->len);
  46. printf_encode(buf, sizeof(buf), test->data, test->len);
  47. printf(" -> \"%s\"\n", buf);
  48. binlen = printf_decode(bin, sizeof(bin), buf);
  49. if (binlen != test->len ||
  50. os_memcmp(bin, test->data, binlen) != 0) {
  51. printf("Error in decoding#1:");
  52. print_hex(bin, binlen);
  53. printf("\n");
  54. errors++;
  55. }
  56. binlen = printf_decode(bin, sizeof(bin), test->encoded);
  57. if (binlen != test->len ||
  58. os_memcmp(bin, test->data, binlen) != 0) {
  59. printf("Error in decoding#2:");
  60. print_hex(bin, binlen);
  61. printf("\n");
  62. errors++;
  63. }
  64. }
  65. if (errors) {
  66. printf("%d test(s) failed\n", errors);
  67. return -1;
  68. }
  69. return 0;
  70. }