tst-support_quote_string.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Test the support_quote_string 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. char *p = support_quote_string ("");
  23. TEST_COMPARE (strlen (p), 0);
  24. free (p);
  25. p = support_quote_string ("X");
  26. TEST_COMPARE (strlen (p), 1);
  27. TEST_COMPARE (p[0], 'X');
  28. free (p);
  29. /* Check escaping of backslash-escaped characters, and lack of
  30. escaping for other shell meta-characters. */
  31. p = support_quote_string ("$()*?`@[]{}~\'\"X");
  32. TEST_COMPARE (strcmp (p, "$()*?`@[]{}~\\'\\\"X"), 0);
  33. free (p);
  34. /* Check lack of escaping for letters and digits. */
  35. #define LETTERS_AND_DIGTS \
  36. "abcdefghijklmnopqrstuvwxyz" \
  37. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  38. "0123456789"
  39. p = support_quote_string (LETTERS_AND_DIGTS "@");
  40. TEST_COMPARE (strcmp (p, LETTERS_AND_DIGTS "@"), 0);
  41. free (p);
  42. /* Check escaping of control characters and other non-printable
  43. characters. */
  44. p = support_quote_string ("\r\n\t\a\b\f\v\1\177\200\377@");
  45. TEST_COMPARE (strcmp (p, "\\r\\n\\t\\a\\b\\f\\v\\001"
  46. "\\177\\200\\377@"), 0);
  47. free (p);
  48. return 0;
  49. }
  50. #include <support/test-driver.c>