t-path.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-path.c - test path handling code
  4. *
  5. * Copyright © 2009-2012 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 <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <ctype.h>
  23. #include <stdbool.h>
  24. #include <stdlib.h>
  25. #include <dpkg/test.h>
  26. #include <dpkg/path.h>
  27. /* Use the test_trim_eq_ref macro to avoid leaking the string and to get
  28. * meaningful line numbers from assert. */
  29. #define test_trim_eq_ref(p, ref) \
  30. do { \
  31. char *t = test_alloc(strdup((p))); \
  32. path_trim_slash_slashdot(t); \
  33. test_str(t, ==, (ref)); \
  34. free(t); \
  35. } while (0)
  36. static void
  37. test_path_trim(void)
  38. {
  39. test_trim_eq_ref("/a", "/a");
  40. test_trim_eq_ref("./././.", ".");
  41. test_trim_eq_ref("./././", ".");
  42. test_trim_eq_ref("./.", ".");
  43. test_trim_eq_ref("./", ".");
  44. test_trim_eq_ref("/./././.", "/");
  45. test_trim_eq_ref("/./", "/");
  46. test_trim_eq_ref("/.", "/");
  47. test_trim_eq_ref("/", "/");
  48. test_trim_eq_ref("", "");
  49. test_trim_eq_ref("/./../.", "/./..");
  50. test_trim_eq_ref("/foo/bar/./", "/foo/bar");
  51. test_trim_eq_ref("./foo/bar/./", "./foo/bar");
  52. test_trim_eq_ref("/./foo/bar/./", "/./foo/bar");
  53. }
  54. static void
  55. test_path_skip(void)
  56. {
  57. test_str(path_skip_slash_dotslash("./././."), ==, ".");
  58. test_str(path_skip_slash_dotslash("./././"), ==, "");
  59. test_str(path_skip_slash_dotslash("./."), ==, ".");
  60. test_str(path_skip_slash_dotslash("./"), ==, "");
  61. test_str(path_skip_slash_dotslash("/./././."), ==, ".");
  62. test_str(path_skip_slash_dotslash("/./"), ==, "");
  63. test_str(path_skip_slash_dotslash("/."), ==, ".");
  64. test_str(path_skip_slash_dotslash("/"), ==, "");
  65. test_str(path_skip_slash_dotslash("/./../."), ==, "../.");
  66. test_str(path_skip_slash_dotslash("/foo/bar/./"), ==, "foo/bar/./");
  67. test_str(path_skip_slash_dotslash("./foo/bar/./"), ==, "foo/bar/./");
  68. test_str(path_skip_slash_dotslash("/./foo/bar/./"), ==, "foo/bar/./");
  69. }
  70. static void
  71. test_path_basename(void)
  72. {
  73. test_str(path_basename("./."), ==, ".");
  74. test_str(path_basename("./"), ==, "");
  75. test_str(path_basename("/."), ==, ".");
  76. test_str(path_basename("/"), ==, "");
  77. test_str(path_basename("/foo"), ==, "foo");
  78. test_str(path_basename("/foo/bar"), ==, "bar");
  79. test_str(path_basename("/foo/bar/"), ==, "");
  80. }
  81. static void
  82. test_path_temp(void)
  83. {
  84. char *template;
  85. template = path_make_temp_template("test");
  86. test_pass(strstr(template, "test") != NULL);
  87. test_pass(strstr(template, "XXXXXX") != NULL);
  88. free(template);
  89. }
  90. static bool
  91. string_is_ascii(const char *str)
  92. {
  93. while (*str) {
  94. if (!isascii(*str))
  95. return false;
  96. str++;
  97. }
  98. return true;
  99. }
  100. static void
  101. test_path_quote(void)
  102. {
  103. const char src_7_bit[] = "string with 7-bit chars only";
  104. const char src_7_bit_trim[] = "string with 7-bit chars";
  105. const char src_8_bit[] = "text w/ 8-bit chars: \\ \370 \300 \342 end";
  106. const char src_8_bit_end[] = "text \370";
  107. const char src_bs_end[] = "text \\";
  108. char *dst;
  109. size_t len;
  110. /* Test 0 length. */
  111. dst = NULL;
  112. path_quote_filename(dst, src_7_bit, 0);
  113. /* Test no quoting. */
  114. len = strlen(src_7_bit) + 1;
  115. dst = test_alloc(malloc(len));
  116. path_quote_filename(dst, src_7_bit, len);
  117. test_str(dst, ==, src_7_bit);
  118. free(dst);
  119. /* Test no quoting with limit. */
  120. len = strlen(src_7_bit_trim) + 1;
  121. dst = test_alloc(malloc(len));
  122. path_quote_filename(dst, src_7_bit, len);
  123. test_str(dst, ==, src_7_bit_trim);
  124. free(dst);
  125. /* Test normal quoting. */
  126. len = strlen(src_8_bit) * 2 + 1;
  127. dst = test_alloc(malloc(len));
  128. path_quote_filename(dst, src_8_bit, len);
  129. test_pass(strstr(dst, "end") != NULL);
  130. test_pass(string_is_ascii(dst));
  131. free(dst);
  132. /* Test normal quoting with limit. */
  133. len = strlen(src_8_bit_end) + 1 + 2;
  134. dst = test_alloc(malloc(len));
  135. path_quote_filename(dst, src_8_bit_end, len);
  136. test_str(dst, ==, "text ");
  137. free(dst);
  138. /* Test backslash quoting with limit. */
  139. len = strlen(src_bs_end) + 1;
  140. dst = test_alloc(malloc(len));
  141. path_quote_filename(dst, src_bs_end, len);
  142. test_str(dst, ==, "text ");
  143. free(dst);
  144. }
  145. static void
  146. test(void)
  147. {
  148. test_plan(41);
  149. test_path_trim();
  150. test_path_skip();
  151. test_path_basename();
  152. test_path_temp();
  153. test_path_quote();
  154. }