current_dir.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. lib/vfs - manipulate with current directory
  3. Copyright (C) 2011, 2013
  4. The 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 "lib/global.h"
  22. #include "lib/strutil.h"
  23. #include "lib/vfs/xdirentry.h"
  24. #include "src/vfs/local/local.c"
  25. static struct vfs_s_subclass test_subclass;
  26. static struct vfs_class vfs_test_ops;
  27. /* --------------------------------------------------------------------------------------------- */
  28. /* @Mock */
  29. static int
  30. test_chdir (const vfs_path_t * vpath)
  31. {
  32. (void) vpath;
  33. return 0;
  34. }
  35. /* --------------------------------------------------------------------------------------------- */
  36. /* @Before */
  37. static void
  38. setup (void)
  39. {
  40. str_init_strings (NULL);
  41. vfs_init ();
  42. init_localfs ();
  43. vfs_setup_work_dir ();
  44. vfs_s_init_class (&vfs_test_ops, &test_subclass);
  45. vfs_test_ops.name = "testfs";
  46. vfs_test_ops.prefix = "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_class_flags_t input_class_flags;
  65. const vfs_subclass_flags_t input_subclass_flags;
  66. const char *expected_cd_path;
  67. } test_cd_ds[] =
  68. {
  69. { /* 0. */
  70. "/",
  71. "/dev/some.file/test://",
  72. VFSF_NOLINKS,
  73. 0,
  74. "/dev/some.file/test://"
  75. },
  76. { /* 1. */
  77. "/",
  78. "/dev/some.file/test://bla-bla",
  79. VFSF_NOLINKS,
  80. 0,
  81. "/dev/some.file/test://bla-bla"
  82. },
  83. { /* 2. */
  84. "/dev/some.file/test://bla-bla",
  85. "..",
  86. VFSF_NOLINKS,
  87. 0,
  88. "/dev/some.file/test://"
  89. },
  90. { /* 3. */
  91. "/dev/some.file/test://",
  92. "..",
  93. VFSF_NOLINKS,
  94. 0,
  95. "/dev"
  96. },
  97. { /* 4. */
  98. "/dev",
  99. "..",
  100. VFSF_NOLINKS,
  101. 0,
  102. "/"
  103. },
  104. { /* 5. */
  105. "/",
  106. "..",
  107. VFSF_NOLINKS,
  108. 0,
  109. "/"
  110. },
  111. { /* 6. */
  112. "/",
  113. "/test://user:pass@host.net/path",
  114. VFSF_NOLINKS,
  115. VFS_S_REMOTE,
  116. "/test://user:pass@host.net/path"
  117. },
  118. { /* 7. */
  119. "/test://user:pass@host.net/path",
  120. "..",
  121. VFSF_NOLINKS,
  122. VFS_S_REMOTE,
  123. "/test://user:pass@host.net/"
  124. },
  125. { /* 8. */
  126. "/test://user:pass@host.net/",
  127. "..",
  128. VFSF_NOLINKS,
  129. VFS_S_REMOTE,
  130. "/"
  131. },
  132. };
  133. /* *INDENT-ON* */
  134. /* @Test(dataSource = "test_cd_ds") */
  135. /* *INDENT-OFF* */
  136. START_PARAMETRIZED_TEST (test_cd, test_cd_ds)
  137. /* *INDENT-ON* */
  138. {
  139. /* given */
  140. vfs_path_t *vpath;
  141. vfs_test_ops.flags = data->input_class_flags;
  142. test_subclass.flags = data->input_subclass_flags;
  143. vfs_register_class (&vfs_test_ops);
  144. vfs_set_raw_current_dir (vfs_path_from_str (data->input_initial_path));
  145. vpath = vfs_path_from_str (data->input_cd_path);
  146. /* when */
  147. mc_chdir (vpath);
  148. /* then */
  149. {
  150. char *actual_cd_path;
  151. actual_cd_path = _vfs_get_cwd ();
  152. mctest_assert_str_eq (actual_cd_path, data->expected_cd_path);
  153. g_free (actual_cd_path);
  154. }
  155. vfs_path_free (vpath);
  156. }
  157. /* *INDENT-OFF* */
  158. END_PARAMETRIZED_TEST
  159. /* *INDENT-ON* */
  160. /* --------------------------------------------------------------------------------------------- */
  161. int
  162. main (void)
  163. {
  164. int number_failed;
  165. Suite *s = suite_create (TEST_SUITE_NAME);
  166. TCase *tc_core = tcase_create ("Core");
  167. SRunner *sr;
  168. tcase_add_checked_fixture (tc_core, setup, teardown);
  169. /* Add new tests here: *************** */
  170. mctest_add_parameterized_test (tc_core, test_cd, test_cd_ds);
  171. /* *********************************** */
  172. suite_add_tcase (s, tc_core);
  173. sr = srunner_create (s);
  174. srunner_set_log (sr, "current_dir.log");
  175. srunner_run_all (sr, CK_NORMAL);
  176. number_failed = srunner_ntests_failed (sr);
  177. srunner_free (sr);
  178. return (number_failed == 0) ? 0 : 1;
  179. }
  180. /* --------------------------------------------------------------------------------------------- */