vfs_s_get_path.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. lib/vfs - test vfs_s_get_path() function
  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 "lib/strutil.h"
  22. #include "lib/vfs/direntry.c" /* for testing static methods */
  23. #include "src/vfs/local/local.c"
  24. #define ARCH_NAME "/path/to/some/file.ext"
  25. #define ETALON_PATH "path/to/test1_file.ext"
  26. #define ETALON_VFS_NAME "#test2:user:pass@host.net"
  27. #define ETALON_VFS_URL_NAME "test2://user:pass@host.net"
  28. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  29. static struct vfs_class *vfs_test_ops1 = VFS_CLASS (&test_subclass1);
  30. static struct vfs_class *vfs_test_ops2 = VFS_CLASS (&test_subclass2);
  31. static struct vfs_class *vfs_test_ops3 = VFS_CLASS (&test_subclass3);
  32. /* --------------------------------------------------------------------------------------------- */
  33. static int
  34. test1_mock_open_archive (struct vfs_s_super *super, const vfs_path_t * vpath,
  35. const vfs_path_element_t * vpath_element)
  36. {
  37. struct vfs_s_inode *root;
  38. mctest_assert_str_eq (vfs_path_as_str (vpath), "/" ETALON_VFS_URL_NAME ARCH_NAME);
  39. super->name = g_strdup (vfs_path_as_str (vpath));
  40. root = vfs_s_new_inode (vpath_element->class, super, NULL);
  41. super->root = root;
  42. return 0;
  43. }
  44. /* --------------------------------------------------------------------------------------------- */
  45. static int
  46. test1_mock_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
  47. const vfs_path_t * vpath, void *cookie)
  48. {
  49. const vfs_path_element_t *path_element;
  50. (void) vpath_element;
  51. (void) super;
  52. (void) cookie;
  53. path_element = vfs_path_get_by_index (vpath, -1);
  54. if (strcmp (ARCH_NAME, path_element->path) != 0)
  55. return 0;
  56. return 1;
  57. }
  58. /* --------------------------------------------------------------------------------------------- */
  59. /* @Before */
  60. static void
  61. setup (void)
  62. {
  63. str_init_strings (NULL);
  64. vfs_init ();
  65. vfs_init_localfs ();
  66. vfs_setup_work_dir ();
  67. vfs_init_subclass (&test_subclass1, "testfs1", VFS_NOLINKS | VFS_REMOTE, "test1");
  68. test_subclass1.open_archive = test1_mock_open_archive;
  69. test_subclass1.archive_same = test1_mock_archive_same;
  70. test_subclass1.archive_check = NULL;
  71. vfs_register_class (vfs_test_ops1);
  72. vfs_init_subclass (&test_subclass2, "testfs2", VFS_UNKNOWN, "test2");
  73. vfs_register_class (vfs_test_ops2);
  74. vfs_init_subclass (&test_subclass3, "testfs3", VFS_UNKNOWN, "test3");
  75. vfs_register_class (vfs_test_ops3);
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. /* @After */
  79. static void
  80. teardown (void)
  81. {
  82. vfs_shut ();
  83. str_uninit_strings ();
  84. }
  85. void
  86. vfs_die (const char *m)
  87. {
  88. printf ("VFS_DIE: '%s'\n", m);
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /* @Test */
  92. /* *INDENT-OFF* */
  93. START_TEST (test_vfs_s_get_path)
  94. /* *INDENT-ON* */
  95. {
  96. /* given */
  97. struct vfs_s_super *archive;
  98. const char *result;
  99. /* when */
  100. vfs_path_t *vpath =
  101. vfs_path_from_str_flags ("/" ETALON_VFS_NAME ARCH_NAME "#test1:/" ETALON_PATH,
  102. VPF_USE_DEPRECATED_PARSER);
  103. result = vfs_s_get_path (vpath, &archive, 0);
  104. /* then */
  105. mctest_assert_str_eq (result, ETALON_PATH);
  106. mctest_assert_str_eq (archive->name, "/" ETALON_VFS_URL_NAME ARCH_NAME);
  107. g_free (vpath);
  108. }
  109. /* *INDENT-OFF* */
  110. END_TEST
  111. /* *INDENT-ON* */
  112. /* --------------------------------------------------------------------------------------------- */
  113. int
  114. main (void)
  115. {
  116. int number_failed;
  117. Suite *s = suite_create (TEST_SUITE_NAME);
  118. TCase *tc_core = tcase_create ("Core");
  119. SRunner *sr;
  120. tcase_add_checked_fixture (tc_core, setup, teardown);
  121. /* Add new tests here: *************** */
  122. tcase_add_test (tc_core, test_vfs_s_get_path);
  123. /* *********************************** */
  124. suite_add_tcase (s, tc_core);
  125. sr = srunner_create (s);
  126. srunner_set_log (sr, "vfs_s_get_path.log");
  127. srunner_run_all (sr, CK_ENV);
  128. number_failed = srunner_ntests_failed (sr);
  129. srunner_free (sr);
  130. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  131. }
  132. /* --------------------------------------------------------------------------------------------- */