vfs_s_get_path.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. lib/vfs - test vfs_s_get_path() function
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  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 <config.h>
  21. #include <check.h>
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/vfs/direntry.c" /* for testing static methods */
  25. #include "src/vfs/local/local.c"
  26. #define ARCH_NAME "/path/to/some/file.ext"
  27. #define ETALON_PATH "path/to/test1_file.ext"
  28. #define ETALON_VFS_NAME "#test2:user:pass@host.net"
  29. #define ETALON_VFS_URL_NAME "test2://user:pass@host.net"
  30. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  31. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  32. static int
  33. test1_mock_open_archive(struct vfs_s_super *super, const vfs_path_t *vpath, const vfs_path_element_t *vpath_element)
  34. {
  35. struct vfs_s_inode *root;
  36. char *spath = vfs_path_to_str (vpath);
  37. fail_unless(strcmp("/" ETALON_VFS_URL_NAME ARCH_NAME, spath) == 0,
  38. "etalon(%s) doesn't equal to actual(%s)", "/" ETALON_VFS_URL_NAME ARCH_NAME, spath);
  39. super->name = g_strdup (spath);
  40. super->data = g_new (char *, 1);
  41. root = vfs_s_new_inode (vpath_element->class, super, NULL);
  42. super->root = root;
  43. g_free(spath);
  44. return 0;
  45. }
  46. static int
  47. test1_mock_archive_same (const vfs_path_element_t *vpath_element, struct vfs_s_super *super,
  48. const vfs_path_t *vpath, void *cookie)
  49. {
  50. const vfs_path_element_t *path_element;
  51. (void) vpath_element;
  52. (void) super;
  53. (void) cookie;
  54. path_element = vfs_path_get_by_index(vpath, -1);
  55. if (strcmp(ARCH_NAME, path_element->path) != 0)
  56. return 0;
  57. return 1;
  58. }
  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. static void
  85. teardown (void)
  86. {
  87. vfs_shut ();
  88. str_uninit_strings ();
  89. }
  90. void
  91. vfs_die (const char *m)
  92. {
  93. printf("VFS_DIE: '%s'\n", m);
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. START_TEST (test_vfs_s_get_path)
  97. {
  98. struct vfs_s_super *archive;
  99. const char *result;
  100. vfs_path_t *vpath = vfs_path_from_str_flags ("/" ETALON_VFS_NAME ARCH_NAME "#test1:/" ETALON_PATH,
  101. VPF_USE_DEPRECATED_PARSER);
  102. result = vfs_s_get_path (vpath, &archive, 0);
  103. fail_unless(strcmp(ETALON_PATH, result) == 0,
  104. "expected(%s) doesn't equal to actual(%s)", ETALON_PATH, result);
  105. fail_unless(strcmp("/" ETALON_VFS_URL_NAME ARCH_NAME,archive->name) == 0,
  106. "expected(%s) doesn't equal to actual(%s)", "/" ETALON_VFS_URL_NAME ARCH_NAME, archive->name);
  107. g_free(vpath);
  108. }
  109. END_TEST
  110. /* --------------------------------------------------------------------------------------------- */
  111. int
  112. main (void)
  113. {
  114. int number_failed;
  115. Suite *s = suite_create (TEST_SUITE_NAME);
  116. TCase *tc_core = tcase_create ("Core");
  117. SRunner *sr;
  118. tcase_add_checked_fixture (tc_core, setup, teardown);
  119. /* Add new tests here: *************** */
  120. tcase_add_test (tc_core, test_vfs_s_get_path);
  121. /* *********************************** */
  122. suite_add_tcase (s, tc_core);
  123. sr = srunner_create (s);
  124. srunner_set_log (sr, "vfs_s_get_path.log");
  125. srunner_run_all (sr, CK_NORMAL);
  126. number_failed = srunner_ntests_failed (sr);
  127. srunner_free (sr);
  128. return (number_failed == 0) ? 0 : 1;
  129. }
  130. /* --------------------------------------------------------------------------------------------- */