t-string.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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 <config.h>
  21. #include <compat.h>
  22. #include <dpkg/test.h>
  23. #include <dpkg/string.h>
  24. #include <string.h>
  25. static void
  26. test_str_escape_fmt(void)
  27. {
  28. char buf[1024], *q;
  29. memset(buf, 'a', sizeof(buf));
  30. q = str_escape_fmt(buf, "");
  31. strcpy(q, " end");
  32. test_str(buf, ==, " end");
  33. memset(buf, 'a', sizeof(buf));
  34. q = str_escape_fmt(buf, "%");
  35. strcpy(q, " end");
  36. test_str(buf, ==, "%% end");
  37. memset(buf, 'a', sizeof(buf));
  38. q = str_escape_fmt(buf, "%%%");
  39. strcpy(q, " end");
  40. test_str(buf, ==, "%%%%%% end");
  41. memset(buf, 'a', sizeof(buf));
  42. q = str_escape_fmt(buf, "%b%b%c%c%%");
  43. strcpy(q, " end");
  44. test_str(buf, ==, "%%b%%b%%c%%c%%%% end");
  45. }
  46. static void
  47. test_str_strip_quotes(void)
  48. {
  49. char buf[1024], *str;
  50. strcpy(buf, "unquoted text");
  51. str = str_strip_quotes(buf);
  52. test_str(str, ==, "unquoted text");
  53. strcpy(buf, "contained 'quoted text'");
  54. str = str_strip_quotes(buf);
  55. test_str(str, ==, "contained 'quoted text'");
  56. strcpy(buf, "contained \"quoted text\"");
  57. str = str_strip_quotes(buf);
  58. test_str(str, ==, "contained \"quoted text\"");
  59. strcpy(buf, "'unbalanced quotes");
  60. str = str_strip_quotes(buf);
  61. test_pass(str == NULL);
  62. strcpy(buf, "\"unbalanced quotes");
  63. str = str_strip_quotes(buf);
  64. test_pass(str == NULL);
  65. strcpy(buf, "'mismatched quotes\"");
  66. str = str_strip_quotes(buf);
  67. test_pass(str == NULL);
  68. strcpy(buf, "\"mismatched quotes'");
  69. str = str_strip_quotes(buf);
  70. test_pass(str == NULL);
  71. strcpy(buf, "'completely quoted text'");
  72. str = str_strip_quotes(buf);
  73. test_str(str, ==, "completely quoted text");
  74. strcpy(buf, "\"completely quoted text\"");
  75. str = str_strip_quotes(buf);
  76. test_str(str, ==, "completely quoted text");
  77. }
  78. static void
  79. test(void)
  80. {
  81. test_str_escape_fmt();
  82. test_str_strip_quotes();
  83. }