current_dir.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. lib/vfs - manipulate with current directory
  3. Copyright (C) 2011-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  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 "tests/mctest.h"
  21. #include <string.h> /* memset() */
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/vfs/xdirentry.h"
  25. #include "lib/util.h"
  26. #include "src/vfs/local/local.c"
  27. static struct vfs_s_subclass vfs_test_subclass;
  28. static struct vfs_class *vfs_test_ops = VFS_CLASS (&vfs_test_subclass);
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Mock */
  31. static int
  32. test_chdir (const vfs_path_t *vpath)
  33. {
  34. (void) vpath;
  35. return 0;
  36. }
  37. /* --------------------------------------------------------------------------------------------- */
  38. /* @Before */
  39. static void
  40. setup (void)
  41. {
  42. str_init_strings (NULL);
  43. vfs_init ();
  44. vfs_init_localfs ();
  45. vfs_setup_work_dir ();
  46. memset (&vfs_test_subclass, 0, sizeof (vfs_test_subclass));
  47. vfs_init_class (vfs_test_ops, "testfs", VFSF_UNKNOWN, "test");
  48. vfs_test_ops->chdir = test_chdir;
  49. }
  50. /* --------------------------------------------------------------------------------------------- */
  51. /* @After */
  52. static void
  53. teardown (void)
  54. {
  55. vfs_shut ();
  56. str_uninit_strings ();
  57. }
  58. /* --------------------------------------------------------------------------------------------- */
  59. /* @DataSource("test_cd_ds") */
  60. /* *INDENT-OFF* */
  61. static const struct test_cd_ds
  62. {
  63. const char *input_initial_path;
  64. const char *input_cd_path;
  65. const vfs_flags_t input_class_flags;
  66. const char *expected_cd_path;
  67. } test_cd_ds[] =
  68. {
  69. { /* 0. */
  70. "/",
  71. "/dev/some.file/test://",
  72. VFSF_NOLINKS,
  73. "/dev/some.file/test://"
  74. },
  75. { /* 1. */
  76. "/",
  77. "/dev/some.file/test://bla-bla",
  78. VFSF_NOLINKS,
  79. "/dev/some.file/test://bla-bla"
  80. },
  81. { /* 2. */
  82. "/dev/some.file/test://bla-bla",
  83. "..",
  84. VFSF_NOLINKS,
  85. "/dev/some.file/test://"
  86. },
  87. { /* 3. */
  88. "/dev/some.file/test://",
  89. "..",
  90. VFSF_NOLINKS,
  91. "/dev"
  92. },
  93. { /* 4. */
  94. "/dev",
  95. "..",
  96. VFSF_NOLINKS,
  97. "/"
  98. },
  99. { /* 5. */
  100. "/",
  101. "..",
  102. VFSF_NOLINKS,
  103. "/"
  104. },
  105. { /* 6. */
  106. "/",
  107. "/test://user:pass@host.net/path",
  108. VFSF_NOLINKS | VFSF_REMOTE,
  109. "/test://user:pass@host.net/path"
  110. },
  111. { /* 7. */
  112. "/test://user:pass@host.net/path",
  113. "..",
  114. VFSF_NOLINKS | VFSF_REMOTE,
  115. "/test://user:pass@host.net/"
  116. },
  117. { /* 8. */
  118. "/test://user:pass@host.net/",
  119. "..",
  120. VFSF_NOLINKS | VFSF_REMOTE,
  121. "/"
  122. },
  123. };
  124. /* *INDENT-ON* */
  125. /* @Test(dataSource = "test_cd_ds") */
  126. /* *INDENT-OFF* */
  127. START_PARAMETRIZED_TEST (test_cd, test_cd_ds)
  128. /* *INDENT-ON* */
  129. {
  130. /* given */
  131. vfs_path_t *vpath;
  132. vfs_test_ops->flags = data->input_class_flags;
  133. vfs_register_class (vfs_test_ops);
  134. vfs_set_raw_current_dir (vfs_path_from_str (data->input_initial_path));
  135. vpath = vfs_path_from_str (data->input_cd_path);
  136. /* when */
  137. mc_chdir (vpath);
  138. /* then */
  139. {
  140. char *actual_cd_path;
  141. actual_cd_path = vfs_get_cwd ();
  142. mctest_assert_str_eq (actual_cd_path, data->expected_cd_path);
  143. g_free (actual_cd_path);
  144. }
  145. vfs_path_free (vpath, TRUE);
  146. vfs_unregister_class (vfs_test_ops);
  147. }
  148. /* *INDENT-OFF* */
  149. END_PARAMETRIZED_TEST
  150. /* *INDENT-ON* */
  151. /* --------------------------------------------------------------------------------------------- */
  152. int
  153. main (void)
  154. {
  155. TCase *tc_core;
  156. char *cwd;
  157. tc_core = tcase_create ("Core");
  158. /* writable directory where check creates temporary files */
  159. cwd = my_get_current_dir ();
  160. g_setenv ("TEMP", cwd, TRUE);
  161. g_free (cwd);
  162. tcase_add_checked_fixture (tc_core, setup, teardown);
  163. /* Add new tests here: *************** */
  164. mctest_add_parameterized_test (tc_core, test_cd, test_cd_ds);
  165. /* *********************************** */
  166. return mctest_run_all (tc_core);
  167. }
  168. /* --------------------------------------------------------------------------------------------- */