t-string.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-string.c - test string handling
  4. *
  5. * Copyright © 2009 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <string.h>
  21. #include <dpkg/test.h>
  22. #include <dpkg/string.h>
  23. static void
  24. test_str_escape_fmt(void)
  25. {
  26. char buf[1024], *q;
  27. memset(buf, sizeof(buf), 'a');
  28. q = str_escape_fmt(buf, "");
  29. strcpy(q, " end");
  30. test_str(buf, ==, " end");
  31. memset(buf, sizeof(buf), 'a');
  32. q = str_escape_fmt(buf, "%");
  33. strcpy(q, " end");
  34. test_str(buf, ==, "%% end");
  35. memset(buf, sizeof(buf), 'a');
  36. q = str_escape_fmt(buf, "%%%");
  37. strcpy(q, " end");
  38. test_str(buf, ==, "%%%%%% end");
  39. memset(buf, sizeof(buf), 'a');
  40. q = str_escape_fmt(buf, "%b%b%c%c%%");
  41. strcpy(q, " end");
  42. test_str(buf, ==, "%%b%%b%%c%%c%%%% end");
  43. }
  44. static void
  45. test_str_strip_quotes(void)
  46. {
  47. char buf[1024], *str;
  48. strcpy(buf, "unquoted text");
  49. str = str_strip_quotes(buf);
  50. test_str(str, ==, "unquoted text");
  51. strcpy(buf, "contained 'quoted text'");
  52. str = str_strip_quotes(buf);
  53. test_str(str, ==, "contained 'quoted text'");
  54. strcpy(buf, "contained \"quoted text\"");
  55. str = str_strip_quotes(buf);
  56. test_str(str, ==, "contained \"quoted text\"");
  57. strcpy(buf, "'unbalanced quotes");
  58. str = str_strip_quotes(buf);
  59. test_pass(str == NULL);
  60. strcpy(buf, "\"unbalanced quotes");
  61. str = str_strip_quotes(buf);
  62. test_pass(str == NULL);
  63. strcpy(buf, "'mismatched quotes\"");
  64. str = str_strip_quotes(buf);
  65. test_pass(str == NULL);
  66. strcpy(buf, "\"mismatched quotes'");
  67. str = str_strip_quotes(buf);
  68. test_pass(str == NULL);
  69. strcpy(buf, "'completely quoted text'");
  70. str = str_strip_quotes(buf);
  71. test_str(str, ==, "completely quoted text");
  72. strcpy(buf, "\"completely quoted text\"");
  73. str = str_strip_quotes(buf);
  74. test_str(str, ==, "completely quoted text");
  75. }
  76. static void
  77. test(void)
  78. {
  79. test_str_escape_fmt();
  80. test_str_strip_quotes();
  81. }