canonicalize_pathname.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. lib - canonicalize path
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander 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. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/vfs"
  20. #include <config.h>
  21. #include <check.h>
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/util.h"
  25. #include "lib/vfs/xdirentry.h"
  26. #include "src/vfs/local/local.c"
  27. static void
  28. setup (void)
  29. {
  30. str_init_strings (NULL);
  31. vfs_init ();
  32. init_localfs ();
  33. vfs_setup_work_dir ();
  34. }
  35. static void
  36. teardown (void)
  37. {
  38. vfs_shut ();
  39. str_uninit_strings ();
  40. }
  41. /* --------------------------------------------------------------------------------------------- */
  42. #define check_canonicalize( input, etalon ) { \
  43. path = g_strdup(input); \
  44. canonicalize_pathname (path); \
  45. fail_unless ( \
  46. strcmp(path, etalon) == 0, \
  47. "\nactual value (%s)\nnot equal to etalon (%s)", path, etalon \
  48. ); \
  49. g_free(path); \
  50. }
  51. START_TEST (test_canonicalize_path)
  52. {
  53. char *path;
  54. static struct vfs_s_subclass test_subclass;
  55. static struct vfs_class vfs_test_ops;
  56. vfs_s_init_class (&vfs_test_ops, &test_subclass);
  57. vfs_test_ops.name = "testfs";
  58. vfs_test_ops.flags = VFSF_NOLINKS;
  59. vfs_test_ops.prefix = "ftp";
  60. test_subclass.flags = VFS_S_REMOTE;
  61. vfs_register_class (&vfs_test_ops);
  62. /* UNC path */
  63. check_canonicalize ("//some_server/ww", "//some_server/ww");
  64. /* join slashes */
  65. check_canonicalize ("///some_server/////////ww", "/some_server/ww");
  66. /* Collapse "/./" -> "/" */
  67. check_canonicalize ("//some_server//.///////ww/./././.", "//some_server/ww");
  68. /* Remove leading "./" */
  69. check_canonicalize ("./some_server/ww", "some_server/ww");
  70. /* some/.. -> . */
  71. check_canonicalize ("some_server/..", ".");
  72. /* Collapse "/.." with the previous part of path */
  73. check_canonicalize ("/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww", "/some_server/ww/ww/some_server/ww");
  74. /* URI style */
  75. check_canonicalize ("/some_server/ww/ftp://user:pass@host.net/path/", "/some_server/ww/ftp://user:pass@host.net/path");
  76. check_canonicalize ("/some_server/ww/ftp://user:pass@host.net/path/../../", "/some_server/ww");
  77. check_canonicalize ("ftp://user:pass@host.net/path/../../", ".");
  78. check_canonicalize ("ftp://user/../../", "..");
  79. }
  80. END_TEST
  81. /* --------------------------------------------------------------------------------------------- */
  82. int
  83. main (void)
  84. {
  85. int number_failed;
  86. Suite *s = suite_create (TEST_SUITE_NAME);
  87. TCase *tc_core = tcase_create ("Core");
  88. SRunner *sr;
  89. tcase_add_checked_fixture (tc_core, setup, teardown);
  90. /* Add new tests here: *************** */
  91. tcase_add_test (tc_core, test_canonicalize_path);
  92. /* *********************************** */
  93. suite_add_tcase (s, tc_core);
  94. sr = srunner_create (s);
  95. srunner_set_log (sr, "canonicalize_pathname.log");
  96. srunner_run_all (sr, CK_NORMAL);
  97. number_failed = srunner_ntests_failed (sr);
  98. srunner_free (sr);
  99. return (number_failed == 0) ? 0 : 1;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */