vfs_s_get_path.c 5.1 KB

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