canonicalize_pathname.c 3.8 KB

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