vfs_s_get_path.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. lib/vfs - test vfs_s_get_path() function
  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 "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. static 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 char *path;
  50. (void) vpath_element;
  51. (void) super;
  52. (void) cookie;
  53. path = vfs_path_get_last_path_str (vpath);
  54. return (strcmp (ARCH_NAME, path) != 0 ? 0 : 1);
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /* @Before */
  58. static void
  59. setup (void)
  60. {
  61. str_init_strings (NULL);
  62. vfs_init ();
  63. vfs_init_localfs ();
  64. vfs_setup_work_dir ();
  65. vfs_init_subclass (&test_subclass1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  66. test_subclass1.open_archive = test1_mock_open_archive;
  67. test_subclass1.archive_same = test1_mock_archive_same;
  68. test_subclass1.archive_check = NULL;
  69. vfs_register_class (vfs_test_ops1);
  70. vfs_init_subclass (&test_subclass2, "testfs2", VFSF_UNKNOWN, "test2");
  71. vfs_register_class (vfs_test_ops2);
  72. vfs_init_subclass (&test_subclass3, "testfs3", VFSF_UNKNOWN, "test3");
  73. vfs_register_class (vfs_test_ops3);
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. /* @After */
  77. static void
  78. teardown (void)
  79. {
  80. vfs_shut ();
  81. str_uninit_strings ();
  82. }
  83. void
  84. vfs_die (const char *m)
  85. {
  86. printf ("VFS_DIE: '%s'\n", m);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. /* @Test */
  90. /* *INDENT-OFF* */
  91. START_TEST (test_vfs_s_get_path)
  92. /* *INDENT-ON* */
  93. {
  94. /* given */
  95. struct vfs_s_super *archive;
  96. const char *result;
  97. /* when */
  98. vfs_path_t *vpath =
  99. vfs_path_from_str_flags ("/" ETALON_VFS_NAME ARCH_NAME "#test1:/" ETALON_PATH,
  100. VPF_USE_DEPRECATED_PARSER);
  101. result = vfs_s_get_path (vpath, &archive, 0);
  102. /* then */
  103. mctest_assert_str_eq (result, ETALON_PATH);
  104. mctest_assert_str_eq (archive->name, "/" ETALON_VFS_URL_NAME ARCH_NAME);
  105. g_free (vpath);
  106. }
  107. /* *INDENT-OFF* */
  108. END_TEST
  109. /* *INDENT-ON* */
  110. /* --------------------------------------------------------------------------------------------- */
  111. int
  112. main (void)
  113. {
  114. TCase *tc_core;
  115. tc_core = tcase_create ("Core");
  116. tcase_add_checked_fixture (tc_core, setup, teardown);
  117. /* Add new tests here: *************** */
  118. tcase_add_test (tc_core, test_vfs_s_get_path);
  119. /* *********************************** */
  120. return mctest_run_all (tc_core);
  121. }
  122. /* --------------------------------------------------------------------------------------------- */