tst-support_quote_blob.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Test the support_quote_blob function.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <support/check.h>
  16. #include <support/support.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. static int
  20. do_test (void)
  21. {
  22. /* Check handling of the empty blob, both with and without trailing
  23. NUL byte. */
  24. char *p = support_quote_blob ("", 0);
  25. TEST_COMPARE (strlen (p), 0);
  26. free (p);
  27. p = support_quote_blob ("X", 0);
  28. TEST_COMPARE (strlen (p), 0);
  29. free (p);
  30. /* Check escaping of backslash-escaped characters, and lack of
  31. escaping for other shell meta-characters. */
  32. p = support_quote_blob ("$()*?`@[]{}~\'\"X", 14);
  33. TEST_COMPARE (strcmp (p, "$()*?`@[]{}~\\'\\\""), 0);
  34. free (p);
  35. /* Check lack of escaping for letters and digits. */
  36. #define LETTERS_AND_DIGTS \
  37. "abcdefghijklmnopqrstuvwxyz" \
  38. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  39. "0123456789"
  40. p = support_quote_blob (LETTERS_AND_DIGTS "@", 2 * 26 + 10);
  41. TEST_COMPARE (strcmp (p, LETTERS_AND_DIGTS), 0);
  42. free (p);
  43. /* Check escaping of control characters and other non-printable
  44. characters. */
  45. p = support_quote_blob ("\r\n\t\a\b\f\v\1\177\200\377\0@", 14);
  46. TEST_COMPARE (strcmp (p, "\\r\\n\\t\\a\\b\\f\\v\\001"
  47. "\\177\\200\\377\\000@\\000"), 0);
  48. free (p);
  49. return 0;
  50. }
  51. #include <support/test-driver.c>