t-string.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <stdlib.h>
  25. #include <string.h>
  26. static void
  27. test_str_escape_fmt(void)
  28. {
  29. char buf[1024], *q;
  30. memset(buf, 'a', sizeof(buf));
  31. q = str_escape_fmt(buf, "", sizeof(buf));
  32. strcpy(q, " end");
  33. test_str(buf, ==, " end");
  34. memset(buf, 'a', sizeof(buf));
  35. q = str_escape_fmt(buf, "%", sizeof(buf));
  36. strcpy(q, " end");
  37. test_str(buf, ==, "%% end");
  38. memset(buf, 'a', sizeof(buf));
  39. q = str_escape_fmt(buf, "%%%", sizeof(buf));
  40. strcpy(q, " end");
  41. test_str(buf, ==, "%%%%%% end");
  42. memset(buf, 'a', sizeof(buf));
  43. q = str_escape_fmt(buf, "%b%b%c%c%%", sizeof(buf));
  44. strcpy(q, " end");
  45. test_str(buf, ==, "%%b%%b%%c%%c%%%% end");
  46. /* Test delimited buffer. */
  47. memset(buf, 'a', sizeof(buf));
  48. q = str_escape_fmt(buf, "%%%", 5);
  49. strcpy(q, " end");
  50. test_str(buf, ==, "%%%% end");
  51. memset(buf, 'a', sizeof(buf));
  52. q = str_escape_fmt(buf, "%%%", 4);
  53. strcpy(q, " end");
  54. test_str(buf, ==, "%% end");
  55. }
  56. static void
  57. test_str_quote_meta(void)
  58. {
  59. char *str;
  60. str = str_quote_meta("foo bar");
  61. test_str(str, ==, "foo\\ bar");
  62. free(str);
  63. str = str_quote_meta("foo?bar");
  64. test_str(str, ==, "foo\\?bar");
  65. free(str);
  66. str = str_quote_meta("foo*bar");
  67. test_str(str, ==, "foo\\*bar");
  68. free(str);
  69. }
  70. static void
  71. test_str_strip_quotes(void)
  72. {
  73. char buf[1024], *str;
  74. strcpy(buf, "unquoted text");
  75. str = str_strip_quotes(buf);
  76. test_str(str, ==, "unquoted text");
  77. strcpy(buf, "contained 'quoted text'");
  78. str = str_strip_quotes(buf);
  79. test_str(str, ==, "contained 'quoted text'");
  80. strcpy(buf, "contained \"quoted text\"");
  81. str = str_strip_quotes(buf);
  82. test_str(str, ==, "contained \"quoted text\"");
  83. strcpy(buf, "'unbalanced quotes");
  84. str = str_strip_quotes(buf);
  85. test_pass(str == NULL);
  86. strcpy(buf, "\"unbalanced quotes");
  87. str = str_strip_quotes(buf);
  88. test_pass(str == NULL);
  89. strcpy(buf, "'mismatched quotes\"");
  90. str = str_strip_quotes(buf);
  91. test_pass(str == NULL);
  92. strcpy(buf, "\"mismatched quotes'");
  93. str = str_strip_quotes(buf);
  94. test_pass(str == NULL);
  95. strcpy(buf, "'completely quoted text'");
  96. str = str_strip_quotes(buf);
  97. test_str(str, ==, "completely quoted text");
  98. strcpy(buf, "\"completely quoted text\"");
  99. str = str_strip_quotes(buf);
  100. test_str(str, ==, "completely quoted text");
  101. }
  102. static void
  103. test(void)
  104. {
  105. test_str_escape_fmt();
  106. test_str_quote_meta();
  107. test_str_strip_quotes();
  108. }