current_dir.c 5.2 KB

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