t-path.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-path.c - test path handling code
  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 <stdlib.h>
  21. #include <dpkg/test.h>
  22. #include <dpkg/path.h>
  23. /* Use the test_trim_eq_ref macro to avoid leaking the string and to get
  24. * meaningful line numbers from assert. */
  25. #define test_trim_eq_ref(p, ref) \
  26. do { \
  27. char *t = strdup((p)); \
  28. path_rtrim_slash_slashdot(t); \
  29. test_str(t, ==, (ref)); \
  30. free(t); \
  31. } while (0)
  32. static void
  33. test_path_rtrim(void)
  34. {
  35. test_trim_eq_ref("./././.", ".");
  36. test_trim_eq_ref("./././", ".");
  37. test_trim_eq_ref("./.", ".");
  38. test_trim_eq_ref("./", ".");
  39. test_trim_eq_ref("/./././.", "/");
  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("/foo/bar/./", "/foo/bar");
  46. test_trim_eq_ref("./foo/bar/./", "./foo/bar");
  47. test_trim_eq_ref("/./foo/bar/./", "/./foo/bar");
  48. }
  49. static void
  50. test_path_skip(void)
  51. {
  52. test_str(path_skip_slash_dotslash("./././."), ==, ".");
  53. test_str(path_skip_slash_dotslash("./././"), ==, "");
  54. test_str(path_skip_slash_dotslash("./."), ==, ".");
  55. test_str(path_skip_slash_dotslash("./"), ==, "");
  56. test_str(path_skip_slash_dotslash("/./././."), ==, ".");
  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("/foo/bar/./"), ==, "foo/bar/./");
  62. test_str(path_skip_slash_dotslash("./foo/bar/./"), ==, "foo/bar/./");
  63. test_str(path_skip_slash_dotslash("/./foo/bar/./"), ==, "foo/bar/./");
  64. }
  65. static void
  66. test(void)
  67. {
  68. test_path_rtrim();
  69. test_path_skip();
  70. }