mc_realpath.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. lib - realpath
  3. Copyright (C) 2017-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2017
  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/util"
  20. #include "tests/mctest.h"
  21. #include "lib/strutil.h"
  22. #include "lib/vfs/vfs.h" /* VFS_ENCODING_PREFIX, vfs_init(), vfs_shut() */
  23. #include "src/vfs/local/local.c"
  24. #include "lib/util.h" /* mc_realpath() */
  25. /* --------------------------------------------------------------------------------------------- */
  26. static char resolved_path[PATH_MAX];
  27. /* --------------------------------------------------------------------------------------------- */
  28. /* @Before */
  29. static void
  30. setup (void)
  31. {
  32. str_init_strings (NULL);
  33. vfs_init ();
  34. vfs_init_localfs ();
  35. vfs_setup_work_dir ();
  36. }
  37. /* @After */
  38. static void
  39. teardown (void)
  40. {
  41. vfs_shut ();
  42. str_uninit_strings ();
  43. }
  44. /* --------------------------------------------------------------------------------------------- */
  45. /* @DataSource("data_source") */
  46. /* *INDENT-OFF* */
  47. static const struct data_source
  48. {
  49. const char *input_string;
  50. const char *expected_string;
  51. } data_source[] =
  52. {
  53. /* absolute paths */
  54. { "/", "/"},
  55. { "/usr/bin", "/usr/bin" },
  56. #ifdef HAVE_CHARSET
  57. { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" },
  58. { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
  59. #else
  60. { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" VFS_ENCODING_PREFIX "UTF-8/" },
  61. { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin" },
  62. #endif
  63. /* relative paths are relative to / */
  64. { "usr/bin", "/usr/bin" },
  65. #ifdef HAVE_CHARSET
  66. { VFS_ENCODING_PREFIX "UTF-8/", "/" },
  67. { VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" }
  68. #else
  69. { VFS_ENCODING_PREFIX "UTF-8/", VFS_ENCODING_PREFIX "UTF-8/" },
  70. { VFS_ENCODING_PREFIX "UTF-8/usr/bin", VFS_ENCODING_PREFIX "UTF-8/usr/bin" }
  71. #endif
  72. };
  73. /* *INDENT-ON* */
  74. /* @Test(dataSource = "data_source") */
  75. /* *INDENT-OFF* */
  76. START_PARAMETRIZED_TEST (realpath_test, data_source)
  77. /* *INDENT-ON* */
  78. {
  79. int ret;
  80. /* realpath(3) produces a canonicalized absolute pathname using current directory.
  81. * Change the current directory to produce correct pathname. */
  82. ret = chdir ("/");
  83. /* when */
  84. if (mc_realpath (data->input_string, resolved_path) == NULL)
  85. resolved_path[0] = '\0';
  86. /* then */
  87. mctest_assert_str_eq (resolved_path, data->expected_string);
  88. (void) ret;
  89. }
  90. /* *INDENT-OFF* */
  91. END_PARAMETRIZED_TEST
  92. /* *INDENT-ON* */
  93. /* --------------------------------------------------------------------------------------------- */
  94. int
  95. main (void)
  96. {
  97. TCase *tc_core;
  98. char *cwd;
  99. tc_core = tcase_create ("Core");
  100. /* writable directory where check creates temporary files */
  101. cwd = g_get_current_dir ();
  102. g_setenv ("TEMP", cwd, TRUE);
  103. g_free (cwd);
  104. tcase_add_checked_fixture (tc_core, setup, teardown);
  105. /* Add new tests here: *************** */
  106. mctest_add_parameterized_test (tc_core, realpath_test, data_source);
  107. /* *********************************** */
  108. return mctest_run_all (tc_core);
  109. }
  110. /* --------------------------------------------------------------------------------------------- */