vfs_parse_ls_lga.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. lib/vfs - test vfs_parse_ls_lga() functionality
  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 <stdio.h>
  23. #include "lib/global.h"
  24. #include "lib/vfs/utilvfs.h"
  25. #include "lib/vfs/xdirentry.h"
  26. #include "lib/strutil.h"
  27. #include "src/vfs/local/local.c"
  28. struct vfs_s_subclass test_subclass1;
  29. struct vfs_class vfs_test_ops1;
  30. struct vfs_s_entry *vfs_root_entry;
  31. static struct vfs_s_inode *vfs_root_inode;
  32. static struct vfs_s_super *vfs_test_super;
  33. void message (int flags, const char *title, const char *text, ...);
  34. static void
  35. setup (void)
  36. {
  37. static struct stat initstat;
  38. str_init_strings (NULL);
  39. vfs_init ();
  40. init_localfs ();
  41. vfs_setup_work_dir ();
  42. test_subclass1.flags = VFS_S_REMOTE;
  43. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  44. vfs_test_ops1.name = "testfs1";
  45. vfs_test_ops1.flags = VFSF_NOLINKS;
  46. vfs_test_ops1.prefix = "test1:";
  47. vfs_register_class (&vfs_test_ops1);
  48. vfs_test_super = g_new0 (struct vfs_s_super, 1);
  49. vfs_test_super->me = &vfs_test_ops1;
  50. vfs_root_inode = vfs_s_new_inode (&vfs_test_ops1, vfs_test_super, &initstat);
  51. vfs_root_entry = vfs_s_new_entry (&vfs_test_ops1, "/", vfs_root_inode);
  52. }
  53. static void
  54. teardown (void)
  55. {
  56. vfs_s_free_entry (&vfs_test_ops1, vfs_root_entry);
  57. vfs_shut ();
  58. str_uninit_strings ();
  59. }
  60. void
  61. message (int flags, const char *title, const char *text, ...)
  62. {
  63. char *p;
  64. va_list ap;
  65. (void) flags;
  66. (void) title;
  67. va_start (ap, text);
  68. p = g_strdup_vprintf (text, ap);
  69. va_end (ap);
  70. printf("message(): %s\n", p);
  71. g_free(p);
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. #define check_one_stat_field(etalon_stat, test_stat, field, format, input_str)\
  75. {\
  76. fail_unless(etalon_stat.field == test_stat.field,\
  77. "\ninput string: %s\netalon."#field" = " format "\nactual."#field" = " format "\n",\
  78. input_str, etalon_stat.field, test_stat.field);\
  79. }
  80. #define check_stat_struct(etalon_stat, test_stat, input_str)\
  81. {\
  82. check_one_stat_field(etalon_stat, test_stat, st_dev, "%zu", input_str);\
  83. check_one_stat_field(etalon_stat, test_stat, st_ino, "%zu", input_str);\
  84. check_one_stat_field(etalon_stat, test_stat, st_ino, "%zu", input_str);\
  85. check_one_stat_field(etalon_stat, test_stat, st_mode, "%04x", input_str);\
  86. check_one_stat_field(etalon_stat, test_stat, st_uid, "%u", input_str);\
  87. check_one_stat_field(etalon_stat, test_stat, st_gid, "%u", input_str);\
  88. check_one_stat_field(etalon_stat, test_stat, st_rdev, "%zu", input_str);\
  89. check_one_stat_field(etalon_stat, test_stat, st_size, "%zd", input_str);\
  90. check_one_stat_field(etalon_stat, test_stat, st_blksize, "%zu", input_str);\
  91. check_one_stat_field(etalon_stat, test_stat, st_blocks, "%zd", input_str);\
  92. \
  93. /* FIXME: these commented checks are related to time zone! \
  94. check_one_stat_field(etalon_stat, test_stat, st_atime, "%zd", input_str);\
  95. check_one_stat_field(etalon_stat, test_stat, st_mtime, "%zd", input_str);\
  96. check_one_stat_field(etalon_stat, test_stat, st_ctime, "%zd", input_str);\
  97. */\
  98. }
  99. static void check_vfs_parse_ls_lga_call(const char *input_data, int etalon_result,
  100. const char *etalon_filename, const char *etalon_linkname, struct stat etalon_stat, size_t *filepos)
  101. {
  102. static struct stat test_stat;
  103. char *filename = NULL;
  104. char *linkname = NULL;
  105. gboolean result;
  106. result = vfs_parse_ls_lga (input_data, &test_stat, &filename, &linkname, filepos);
  107. fail_if (result != etalon_result,
  108. "\nactual result: %d\netalon result: %d\n", result, etalon_result);
  109. fail_unless((filename != NULL && etalon_filename != NULL && strcmp(filename, etalon_filename) == 0)
  110. || (filename == NULL && etalon_filename == filename),
  111. "\nactual filename '%s'\netalon filename '%s'", filename, etalon_filename);
  112. fail_unless((linkname != NULL && etalon_linkname != NULL && strcmp(linkname, etalon_linkname) == 0)
  113. || (linkname == NULL && etalon_linkname == linkname),
  114. "\nactual linkname '%s'\netalon linkname '%s'", linkname, etalon_linkname);
  115. check_stat_struct(etalon_stat, test_stat, input_data);
  116. }
  117. START_TEST (test_vfs_parse_ls_lga)
  118. {
  119. size_t filepos = 0;
  120. struct stat etalon_stat;
  121. etalon_stat.st_dev = 0;
  122. etalon_stat.st_ino = 0;
  123. etalon_stat.st_mode = 0x41fd;
  124. etalon_stat.st_nlink = 10;
  125. etalon_stat.st_uid = 500;
  126. etalon_stat.st_gid = 500;
  127. etalon_stat.st_rdev = 0;
  128. etalon_stat.st_size = 4096;
  129. etalon_stat.st_blksize = 512;
  130. etalon_stat.st_blocks = 8;
  131. etalon_stat.st_atime = 1308838140;
  132. etalon_stat.st_mtime = 1308838140;
  133. etalon_stat.st_ctime = 1308838140;
  134. vfs_parse_ls_lga_init();
  135. check_vfs_parse_ls_lga_call(
  136. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  137. 1, "build_root", NULL, etalon_stat,
  138. NULL
  139. );
  140. etalon_stat.st_dev = 0;
  141. etalon_stat.st_ino = 0;
  142. etalon_stat.st_mode = 0xa1ff;
  143. etalon_stat.st_nlink = 10;
  144. etalon_stat.st_uid = 500;
  145. etalon_stat.st_gid = 500;
  146. etalon_stat.st_rdev = 0;
  147. etalon_stat.st_size = 11;
  148. etalon_stat.st_blksize = 512;
  149. etalon_stat.st_blocks = 1;
  150. etalon_stat.st_atime = 1268431200;
  151. etalon_stat.st_mtime = 1268431200;
  152. etalon_stat.st_ctime = 1268431200;
  153. check_vfs_parse_ls_lga_call(
  154. "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
  155. 1, "COPYING", "doc/COPYING", etalon_stat,
  156. NULL
  157. );
  158. etalon_stat.st_dev = 0;
  159. etalon_stat.st_ino = 0;
  160. etalon_stat.st_mode = 0x41fd;
  161. etalon_stat.st_nlink = 10;
  162. etalon_stat.st_uid = 500;
  163. etalon_stat.st_gid = 500;
  164. etalon_stat.st_rdev = 0;
  165. etalon_stat.st_size = 4096;
  166. etalon_stat.st_blksize = 512;
  167. etalon_stat.st_blocks = 8;
  168. etalon_stat.st_atime = 1308838140;
  169. etalon_stat.st_mtime = 1308838140;
  170. etalon_stat.st_ctime = 1308838140;
  171. check_vfs_parse_ls_lga_call(
  172. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  173. 1, "..", NULL, etalon_stat,
  174. &filepos
  175. );
  176. etalon_stat.st_dev = 0;
  177. etalon_stat.st_ino = 0;
  178. etalon_stat.st_mode = 0x41fd;
  179. etalon_stat.st_nlink = 10;
  180. etalon_stat.st_uid = 500;
  181. etalon_stat.st_gid = 500;
  182. etalon_stat.st_rdev = 0;
  183. etalon_stat.st_size = 4096;
  184. etalon_stat.st_blksize = 512;
  185. etalon_stat.st_blocks = 8;
  186. etalon_stat.st_atime = 1308838140;
  187. etalon_stat.st_mtime = 1308838140;
  188. etalon_stat.st_ctime = 1308838140;
  189. check_vfs_parse_ls_lga_call(
  190. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  191. 1, "build_root", NULL, etalon_stat,
  192. &filepos
  193. );
  194. }
  195. END_TEST
  196. /* --------------------------------------------------------------------------------------------- */
  197. START_TEST (test_vfs_parse_ls_lga_reorder)
  198. {
  199. size_t filepos = 0;
  200. struct vfs_s_entry *ent1, *ent2, *ent3;
  201. int i;
  202. vfs_parse_ls_lga_init();
  203. ent1 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  204. i = ent1->ino->st.st_nlink;
  205. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1",
  206. &ent1->ino->st, &ent1->name, &ent1->ino->linkname, &filepos))
  207. {
  208. fail ("An error occured while parse ls output");
  209. return;
  210. }
  211. vfs_s_store_filename_leading_spaces (ent1, filepos);
  212. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent1);
  213. ent2 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  214. i = ent2->ino->st.st_nlink;
  215. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
  216. &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos))
  217. {
  218. fail ("An error occured while parse ls output");
  219. return;
  220. }
  221. vfs_s_store_filename_leading_spaces (ent2, filepos);
  222. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent2);
  223. ent3 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  224. i = ent3->ino->st.st_nlink;
  225. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  226. &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos))
  227. {
  228. fail ("An error occured while parse ls output");
  229. return;
  230. }
  231. vfs_s_store_filename_leading_spaces (ent3, filepos);
  232. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent3);
  233. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  234. fail_unless(strcmp(ent1->name, " build_root1") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent1->name, " build_root1");
  235. fail_unless(strcmp(ent2->name, " build_root2") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent2->name, " build_root2");
  236. }
  237. END_TEST
  238. /* --------------------------------------------------------------------------------------------- */
  239. #define parce_one_line(ent_index, ls_output) {\
  240. ent[ent_index] = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);\
  241. if (! vfs_parse_ls_lga (ls_output,\
  242. &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
  243. {\
  244. fail ("An error occured while parse ls output");\
  245. return;\
  246. }\
  247. vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
  248. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
  249. \
  250. }
  251. #define fail_unless_ent(ent_index, etalon_str){\
  252. fail_unless(\
  253. strcmp(ent[ent_index]->name, etalon_str) == 0,\
  254. "\nactual '%s'\nnot equal to '%s'\n", ent[ent_index]->name, etalon_str\
  255. );\
  256. }
  257. START_TEST (test_vfs_parse_ls_lga_unaligned)
  258. {
  259. size_t filepos = 0;
  260. struct vfs_s_entry *ent[4];
  261. vfs_parse_ls_lga_init();
  262. parce_one_line(0, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1");
  263. parce_one_line(1, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2");
  264. parce_one_line(2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..");
  265. parce_one_line(3, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root 0");
  266. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  267. fail_unless_ent(0, "build_root1");
  268. fail_unless_ent(1, " build_root2");
  269. fail_unless_ent(3, " build_root 0");
  270. }
  271. END_TEST
  272. /* --------------------------------------------------------------------------------------------- */
  273. int
  274. main (void)
  275. {
  276. int number_failed;
  277. Suite *s = suite_create (TEST_SUITE_NAME);
  278. TCase *tc_core = tcase_create ("Core");
  279. SRunner *sr;
  280. tcase_add_checked_fixture (tc_core, setup, teardown);
  281. /* Add new tests here: *************** */
  282. tcase_add_test (tc_core, test_vfs_parse_ls_lga);
  283. tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
  284. tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
  285. /* *********************************** */
  286. suite_add_tcase (s, tc_core);
  287. sr = srunner_create (s);
  288. srunner_set_log (sr, "vfs_parse_ls_lga.log");
  289. srunner_run_all (sr, CK_NORMAL);
  290. number_failed = srunner_ntests_failed (sr);
  291. srunner_free (sr);
  292. return (number_failed == 0) ? 0 : 1;
  293. }
  294. /* --------------------------------------------------------------------------------------------- */