vfs_parse_ls_lga.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. vfs_parse_ls_lga_init();
  121. check_vfs_parse_ls_lga_call(
  122. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  123. 1, "build_root", NULL, (struct stat)
  124. {
  125. .st_dev = 0,
  126. .st_ino = 0,
  127. .st_mode = 0x41fd,
  128. .st_nlink = 10,
  129. .st_uid = 500,
  130. .st_gid = 500,
  131. .st_rdev = 0,
  132. .st_size = 4096,
  133. .st_blksize = 512,
  134. .st_blocks = 8,
  135. .st_atime = 1308838140,
  136. .st_mtime = 1308838140,
  137. .st_ctime = 1308838140
  138. },
  139. NULL
  140. );
  141. check_vfs_parse_ls_lga_call(
  142. "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
  143. 1, "COPYING", "doc/COPYING",
  144. (struct stat)
  145. {
  146. .st_dev = 0,
  147. .st_ino = 0,
  148. .st_mode = 0xa1ff,
  149. .st_nlink = 10,
  150. .st_uid = 500,
  151. .st_gid = 500,
  152. .st_rdev = 0,
  153. .st_size = 11,
  154. .st_blksize = 512,
  155. .st_blocks = 1,
  156. .st_atime = 1268431200,
  157. .st_mtime = 1268431200,
  158. .st_ctime = 1268431200
  159. },
  160. NULL
  161. );
  162. check_vfs_parse_ls_lga_call(
  163. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  164. 1, "..", NULL, (struct stat)
  165. {
  166. .st_dev = 0,
  167. .st_ino = 0,
  168. .st_mode = 0x41fd,
  169. .st_nlink = 10,
  170. .st_uid = 500,
  171. .st_gid = 500,
  172. .st_rdev = 0,
  173. .st_size = 4096,
  174. .st_blksize = 512,
  175. .st_blocks = 8,
  176. .st_atime = 1308838140,
  177. .st_mtime = 1308838140,
  178. .st_ctime = 1308838140
  179. },
  180. &filepos
  181. );
  182. check_vfs_parse_ls_lga_call(
  183. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  184. 1, "build_root", NULL, (struct stat)
  185. {
  186. .st_dev = 0,
  187. .st_ino = 0,
  188. .st_mode = 0x41fd,
  189. .st_nlink = 10,
  190. .st_uid = 500,
  191. .st_gid = 500,
  192. .st_rdev = 0,
  193. .st_size = 4096,
  194. .st_blksize = 512,
  195. .st_blocks = 8,
  196. .st_atime = 1308838140,
  197. .st_mtime = 1308838140,
  198. .st_ctime = 1308838140
  199. },
  200. &filepos
  201. );
  202. }
  203. END_TEST
  204. /* --------------------------------------------------------------------------------------------- */
  205. START_TEST (test_vfs_parse_ls_lga_reorder)
  206. {
  207. size_t filepos = 0;
  208. struct vfs_s_entry *ent1, *ent2, *ent3;
  209. int i;
  210. vfs_parse_ls_lga_init();
  211. ent1 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  212. i = ent1->ino->st.st_nlink;
  213. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1",
  214. &ent1->ino->st, &ent1->name, &ent1->ino->linkname, &filepos))
  215. {
  216. fail ("An error occured while parse ls output");
  217. return;
  218. }
  219. vfs_s_store_filename_leading_spaces (ent1, filepos);
  220. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent1);
  221. ent2 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  222. i = ent2->ino->st.st_nlink;
  223. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
  224. &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos))
  225. {
  226. fail ("An error occured while parse ls output");
  227. return;
  228. }
  229. vfs_s_store_filename_leading_spaces (ent2, filepos);
  230. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent2);
  231. ent3 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  232. i = ent3->ino->st.st_nlink;
  233. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  234. &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos))
  235. {
  236. fail ("An error occured while parse ls output");
  237. return;
  238. }
  239. vfs_s_store_filename_leading_spaces (ent3, filepos);
  240. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent3);
  241. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  242. fail_unless(strcmp(ent1->name, " build_root1") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent1->name, " build_root1");
  243. fail_unless(strcmp(ent2->name, " build_root2") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent2->name, " build_root2");
  244. }
  245. END_TEST
  246. /* --------------------------------------------------------------------------------------------- */
  247. #define parce_one_line(ent_index, ls_output) {\
  248. ent[ent_index] = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);\
  249. if (! vfs_parse_ls_lga (ls_output,\
  250. &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
  251. {\
  252. fail ("An error occured while parse ls output");\
  253. return;\
  254. }\
  255. vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
  256. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
  257. \
  258. }
  259. #define fail_unless_ent(ent_index, etalon_str){\
  260. fail_unless(\
  261. strcmp(ent[ent_index]->name, etalon_str) == 0,\
  262. "\nactual '%s'\nnot equal to '%s'\n", ent[ent_index]->name, etalon_str\
  263. );\
  264. }
  265. START_TEST (test_vfs_parse_ls_lga_unaligned)
  266. {
  267. size_t filepos = 0;
  268. struct vfs_s_entry *ent[4];
  269. vfs_parse_ls_lga_init();
  270. parce_one_line(0, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1");
  271. parce_one_line(1, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2");
  272. parce_one_line(2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..");
  273. parce_one_line(3, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root 0");
  274. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  275. fail_unless_ent(0, "build_root1");
  276. fail_unless_ent(1, " build_root2");
  277. fail_unless_ent(3, " build_root 0");
  278. }
  279. END_TEST
  280. /* --------------------------------------------------------------------------------------------- */
  281. int
  282. main (void)
  283. {
  284. int number_failed;
  285. Suite *s = suite_create (TEST_SUITE_NAME);
  286. TCase *tc_core = tcase_create ("Core");
  287. SRunner *sr;
  288. tcase_add_checked_fixture (tc_core, setup, teardown);
  289. /* Add new tests here: *************** */
  290. tcase_add_test (tc_core, test_vfs_parse_ls_lga);
  291. tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
  292. tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
  293. /* *********************************** */
  294. suite_add_tcase (s, tc_core);
  295. sr = srunner_create (s);
  296. srunner_set_log (sr, "vfs_parse_ls_lga.log");
  297. srunner_run_all (sr, CK_NORMAL);
  298. number_failed = srunner_ntests_failed (sr);
  299. srunner_free (sr);
  300. return (number_failed == 0) ? 0 : 1;
  301. }
  302. /* --------------------------------------------------------------------------------------------- */