mc_realpath.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. lib - realpath
  3. Copyright (C) 2017-2019
  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. { "/" VFS_ENCODING_PREFIX "UTF-8/", "/" },
  56. { "/usr/bin", "/usr/bin" },
  57. { "/" VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" },
  58. /* relative paths are relative to / */
  59. { VFS_ENCODING_PREFIX "UTF-8/", "/" },
  60. { "usr/bin", "/usr/bin" },
  61. { VFS_ENCODING_PREFIX "UTF-8/usr/bin", "/usr/bin" }
  62. };
  63. /* *INDENT-ON* */
  64. /* @Test(dataSource = "data_source") */
  65. /* *INDENT-OFF* */
  66. START_PARAMETRIZED_TEST (realpath_test, data_source)
  67. /* *INDENT-ON* */
  68. {
  69. int ret;
  70. /* realpath(3) produces a canonicalized absolute pathname using curent directory.
  71. * Change the current directory to produce correct pathname. */
  72. ret = chdir ("/");
  73. /* when */
  74. if (mc_realpath (data->input_string, resolved_path) == NULL)
  75. resolved_path[0] = '\0';
  76. /* then */
  77. mctest_assert_str_eq (resolved_path, data->expected_string);
  78. (void) ret;
  79. }
  80. /* *INDENT-OFF* */
  81. END_PARAMETRIZED_TEST
  82. /* *INDENT-ON* */
  83. /* --------------------------------------------------------------------------------------------- */
  84. int
  85. main (void)
  86. {
  87. int number_failed;
  88. char *cwd;
  89. Suite *s = suite_create (TEST_SUITE_NAME);
  90. TCase *tc_core = tcase_create ("Core");
  91. SRunner *sr;
  92. /* writable directory where check creates temporary files */
  93. cwd = g_get_current_dir ();
  94. g_setenv ("TEMP", cwd, TRUE);
  95. g_free (cwd);
  96. tcase_add_checked_fixture (tc_core, setup, teardown);
  97. /* Add new tests here: *************** */
  98. mctest_add_parameterized_test (tc_core, realpath_test, data_source);
  99. /* *********************************** */
  100. suite_add_tcase (s, tc_core);
  101. sr = srunner_create (s);
  102. srunner_set_log (sr, "mc_realpath.log");
  103. srunner_run_all (sr, CK_ENV);
  104. number_failed = srunner_ntests_failed (sr);
  105. srunner_free (sr);
  106. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  107. }
  108. /* --------------------------------------------------------------------------------------------- */