relative_cd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* lib/vfs - test vfs_path_t manipulation functions
  2. Copyright (C) 2011, 2013
  3. The Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License
  8. as published by the Free Software Foundation; either version 2 of
  9. the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define TEST_SUITE_NAME "/lib/vfs"
  19. #include "tests/mctest.h"
  20. #include "lib/strutil.h"
  21. #include "lib/vfs/xdirentry.h"
  22. #include "lib/vfs/path.h"
  23. #include "src/vfs/local/local.c"
  24. struct vfs_s_subclass test_subclass1;
  25. struct vfs_class vfs_test_ops1;
  26. static int test_chdir (const vfs_path_t * vpath);
  27. /* --------------------------------------------------------------------------------------------- */
  28. /* @CapturedValue */
  29. static vfs_path_t *test_chdir__vpath__captured;
  30. /* @ThenReturnValue */
  31. static int test_chdir__return_value;
  32. /* @Mock */
  33. static int
  34. test_chdir (const vfs_path_t * vpath)
  35. {
  36. test_chdir__vpath__captured = vfs_path_clone (vpath);
  37. return test_chdir__return_value;
  38. }
  39. static void
  40. test_chdir__init (void)
  41. {
  42. test_chdir__vpath__captured = NULL;
  43. }
  44. static void
  45. test_chdir__deinit (void)
  46. {
  47. vfs_path_free (test_chdir__vpath__captured);
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. /* @Before */
  51. static void
  52. setup (void)
  53. {
  54. str_init_strings (NULL);
  55. vfs_init ();
  56. init_localfs ();
  57. vfs_setup_work_dir ();
  58. test_subclass1.flags = VFS_S_REMOTE;
  59. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  60. vfs_test_ops1.name = "testfs1";
  61. vfs_test_ops1.flags = VFSF_NOLINKS;
  62. vfs_test_ops1.prefix = "test1";
  63. vfs_test_ops1.chdir = test_chdir;
  64. vfs_register_class (&vfs_test_ops1);
  65. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  66. vfs_local_ops.chdir = test_chdir;
  67. test_chdir__init ();
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. /* @After */
  71. static void
  72. teardown (void)
  73. {
  74. test_chdir__deinit ();
  75. vfs_shut ();
  76. str_uninit_strings ();
  77. }
  78. /* --------------------------------------------------------------------------------------------- */
  79. /* @DataSource("test_relative_cd_ds") */
  80. /* *INDENT-OFF* */
  81. static const struct test_relative_cd_ds
  82. {
  83. const char *input_string;
  84. const vfs_path_flag_t input_flags;
  85. const char *expected_element_path;
  86. } test_relative_cd_ds[] =
  87. {
  88. { /* 0. */
  89. "/test1://user:pass@some.host:12345/path/to/dir",
  90. VPF_NONE,
  91. "path/to/dir"
  92. },
  93. { /* 1. */
  94. "some-non-exists-dir",
  95. VPF_NO_CANON,
  96. "some-non-exists-dir"
  97. },
  98. };
  99. /* *INDENT-ON* */
  100. /* @Test(dataSource = "test_relative_cd_ds") */
  101. /* *INDENT-OFF* */
  102. START_PARAMETRIZED_TEST (test_relative_cd, test_relative_cd_ds)
  103. /* *INDENT-ON* */
  104. {
  105. /* given */
  106. vfs_path_t *vpath;
  107. int actual_result;
  108. test_chdir__return_value = 0;
  109. vpath = vfs_path_from_str_flags (data->input_string, data->input_flags);
  110. /* when */
  111. actual_result = mc_chdir (vpath);
  112. /* then */
  113. {
  114. const vfs_path_element_t *element;
  115. mctest_assert_int_eq (actual_result, 0);
  116. element = vfs_path_get_by_index (vpath, -1);
  117. mctest_assert_str_eq (element->path, data->expected_element_path);
  118. vfs_path_free (vpath);
  119. }
  120. }
  121. /* *INDENT-OFF* */
  122. END_PARAMETRIZED_TEST
  123. /* *INDENT-ON* */
  124. /* --------------------------------------------------------------------------------------------- */
  125. /* Relative to panel_correct_path_to_show() */
  126. /* @Test */
  127. /* *INDENT-OFF* */
  128. START_TEST (test_vpath_to_str_filter)
  129. /* *INDENT-ON* */
  130. {
  131. /* given */
  132. vfs_path_t *vpath, *last_vpath;
  133. char *filtered_path;
  134. const vfs_path_element_t *path_element;
  135. /* when */
  136. vpath = vfs_path_from_str ("/test1://some.host/dir");
  137. path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, -1));
  138. vfs_path_free (vpath);
  139. last_vpath = vfs_path_new ();
  140. last_vpath->relative = TRUE;
  141. vfs_path_add_element (last_vpath, path_element);
  142. filtered_path = vfs_path_to_str_flags (last_vpath, 0,
  143. VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
  144. /* then */
  145. mctest_assert_str_eq (filtered_path, "test1://some.host/dir");
  146. vfs_path_free (last_vpath);
  147. g_free (filtered_path);
  148. }
  149. /* *INDENT-OFF* */
  150. END_TEST
  151. /* *INDENT-ON* */
  152. /* --------------------------------------------------------------------------------------------- */
  153. int
  154. main (void)
  155. {
  156. int number_failed;
  157. Suite *s = suite_create (TEST_SUITE_NAME);
  158. TCase *tc_core = tcase_create ("Core");
  159. SRunner *sr;
  160. tcase_add_checked_fixture (tc_core, setup, teardown);
  161. /* Add new tests here: *************** */
  162. mctest_add_parameterized_test (tc_core, test_relative_cd, test_relative_cd_ds);
  163. tcase_add_test (tc_core, test_vpath_to_str_filter);
  164. /* *********************************** */
  165. suite_add_tcase (s, tc_core);
  166. sr = srunner_create (s);
  167. srunner_set_log (sr, "relative_cd.log");
  168. srunner_run_all (sr, CK_NORMAL);
  169. number_failed = srunner_ntests_failed (sr);
  170. srunner_free (sr);
  171. return (number_failed == 0) ? 0 : 1;
  172. }
  173. /* --------------------------------------------------------------------------------------------- */