t-path.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <dpkg-test.h>
  22. #include <dpkg-priv.h>
  23. #include <stdlib.h>
  24. /* Use the test_trim_eq_ref macro to avoid leaking the string and to get
  25. * meaningful line numbers from assert. */
  26. #define test_trim_eq_ref(p, ref) \
  27. do { \
  28. char *t = strdup((p)); \
  29. rtrim_slash_slashdot(t); \
  30. test_str(t, ==, (ref)); \
  31. free(t); \
  32. } while (0)
  33. static void
  34. test_path_rtrim(void)
  35. {
  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("/./../.", "/./..");
  46. test_trim_eq_ref("/foo/bar/./", "/foo/bar");
  47. test_trim_eq_ref("./foo/bar/./", "./foo/bar");
  48. test_trim_eq_ref("/./foo/bar/./", "/./foo/bar");
  49. }
  50. static void
  51. test_path_skip(void)
  52. {
  53. test_str(skip_slash_dotslash("./././."), ==, ".");
  54. test_str(skip_slash_dotslash("./././"), ==, "");
  55. test_str(skip_slash_dotslash("./."), ==, ".");
  56. test_str(skip_slash_dotslash("./"), ==, "");
  57. test_str(skip_slash_dotslash("/./././."), ==, ".");
  58. test_str(skip_slash_dotslash("/./"), ==, "");
  59. test_str(skip_slash_dotslash("/."), ==, ".");
  60. test_str(skip_slash_dotslash("/"), ==, "");
  61. test_str(skip_slash_dotslash("/./../."), ==, "../.");
  62. test_str(skip_slash_dotslash("/foo/bar/./"), ==, "foo/bar/./");
  63. test_str(skip_slash_dotslash("./foo/bar/./"), ==, "foo/bar/./");
  64. test_str(skip_slash_dotslash("/./foo/bar/./"), ==, "foo/bar/./");
  65. }
  66. static void
  67. test(void)
  68. {
  69. test_path_rtrim();
  70. test_path_skip();
  71. }