relative_cd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* lib/vfs - test vfs_path_t manipulation functions
  2. Copyright (C) 2011-2025
  3. Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #define TEST_SUITE_NAME "/lib/vfs"
  19. #include "tests/mctest.h"
  20. #include <string.h> // memset()
  21. #include "lib/strutil.h"
  22. #include "lib/vfs/xdirentry.h"
  23. #include "lib/vfs/path.h"
  24. #include "lib/util.h"
  25. #include "src/vfs/local/local.c"
  26. static struct vfs_s_subclass vfs_test_subclass1;
  27. static struct vfs_class *vfs_test_ops1 = VFS_CLASS (&vfs_test_subclass1);
  28. static int test_chdir (const vfs_path_t *vpath);
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @CapturedValue */
  31. static vfs_path_t *test_chdir__vpath__captured;
  32. /* @ThenReturnValue */
  33. static int test_chdir__return_value;
  34. /* @Mock */
  35. static int
  36. test_chdir (const vfs_path_t *vpath)
  37. {
  38. test_chdir__vpath__captured = vfs_path_clone (vpath);
  39. return test_chdir__return_value;
  40. }
  41. static void
  42. test_chdir__init (void)
  43. {
  44. test_chdir__vpath__captured = NULL;
  45. }
  46. static void
  47. test_chdir__deinit (void)
  48. {
  49. vfs_path_free (test_chdir__vpath__captured, TRUE);
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. /* @Before */
  53. static void
  54. setup (void)
  55. {
  56. str_init_strings (NULL);
  57. vfs_init ();
  58. vfs_init_localfs ();
  59. vfs_setup_work_dir ();
  60. memset (&vfs_test_subclass1, 0, sizeof (vfs_test_subclass1));
  61. vfs_init_class (vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  62. vfs_test_ops1->chdir = test_chdir;
  63. vfs_register_class (vfs_test_ops1);
  64. vfs_local_ops->chdir = test_chdir;
  65. test_chdir__init ();
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. /* @After */
  69. static void
  70. teardown (void)
  71. {
  72. test_chdir__deinit ();
  73. vfs_shut ();
  74. str_uninit_strings ();
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. /* @DataSource("test_relative_cd_ds") */
  78. static const struct test_relative_cd_ds
  79. {
  80. const char *input_string;
  81. const vfs_path_flag_t input_flags;
  82. const char *expected_element_path;
  83. } test_relative_cd_ds[] = {
  84. {
  85. // 0.
  86. "/test1://user:pass@some.host:12345/path/to/dir",
  87. VPF_NONE,
  88. "path/to/dir",
  89. },
  90. {
  91. // 1.
  92. "some-non-exists-dir",
  93. VPF_NO_CANON,
  94. "some-non-exists-dir",
  95. },
  96. };
  97. /* @Test(dataSource = "test_relative_cd_ds") */
  98. START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
  99. {
  100. // given
  101. vfs_path_t *vpath;
  102. int actual_result;
  103. test_chdir__return_value = 0;
  104. vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
  105. // when
  106. actual_result = mc_chdir (vpath);
  107. // then
  108. {
  109. const char *element_path;
  110. ck_assert_int_eq (actual_result, 0);
  111. element_path = vfs_path_get_last_path_str (vpath);
  112. mctest_assert_str_eq (element_path, data->expected_element_path);
  113. vfs_path_free (vpath, TRUE);
  114. }
  115. }
  116. END_PARAMETRIZED_TEST
  117. /* --------------------------------------------------------------------------------------------- */
  118. /* Relative to panel_correct_path_to_show() */
  119. /* @Test */
  120. START_TEST (test_vpath_to_str_filter)
  121. {
  122. // given
  123. vfs_path_t *vpath, *last_vpath;
  124. char *filtered_path;
  125. const vfs_path_element_t *path_element;
  126. // when
  127. vpath = vfs_path_from_str ("/test1://some.host/dir");
  128. path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
  129. vfs_path_free (vpath, TRUE);
  130. last_vpath = vfs_path_new (TRUE);
  131. vfs_path_add_element (last_vpath, path_element);
  132. filtered_path = vfs_path_to_str_flags (last_vpath, 0,
  133. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
  134. // then
  135. mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
  136. vfs_path_free (last_vpath, TRUE);
  137. g_free (filtered_path);
  138. }
  139. END_TEST
  140. /* --------------------------------------------------------------------------------------------- */
  141. int
  142. main (void)
  143. {
  144. TCase *tc_core;
  145. char *cwd;
  146. tc_core = tcase_create ("Core");
  147. // writable directory where check creates temporary files
  148. cwd = my_get_current_dir ();
  149. g_setenv ("TEMP", cwd, TRUE);
  150. g_free (cwd);
  151. tcase_add_checked_fixture (tc_core, setup, teardown);
  152. // Add new tests here: ***************
  153. mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
  154. tcase_add_test (tc_core, test_vpath_to_str_filter);
  155. // ***********************************
  156. return mctest_run_all (tc_core);
  157. }
  158. /* --------------------------------------------------------------------------------------------- */