vfs_parse_ls_lga.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. lib/vfs - test vfs_parse_ls_lga() functionality
  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 <stdio.h>
  22. #include "lib/vfs/utilvfs.h"
  23. #include "lib/vfs/xdirentry.h"
  24. #include "lib/strutil.h"
  25. #include "src/vfs/local/local.c"
  26. struct vfs_s_subclass test_subclass1;
  27. struct vfs_class vfs_test_ops1;
  28. struct vfs_s_entry *vfs_root_entry;
  29. static struct vfs_s_inode *vfs_root_inode;
  30. static struct vfs_s_super *vfs_test_super;
  31. void message (int flags, const char *title, const char *text, ...);
  32. /* --------------------------------------------------------------------------------------------- */
  33. /* @Before */
  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. /* --------------------------------------------------------------------------------------------- */
  54. /* @After */
  55. static void
  56. teardown (void)
  57. {
  58. vfs_s_free_entry (&vfs_test_ops1, vfs_root_entry);
  59. vfs_shut ();
  60. str_uninit_strings ();
  61. }
  62. /* --------------------------------------------------------------------------------------------- */
  63. /* @Mock */
  64. void
  65. message (int flags, const char *title, const char *text, ...)
  66. {
  67. char *p;
  68. va_list ap;
  69. (void) flags;
  70. (void) title;
  71. va_start (ap, text);
  72. p = g_strdup_vprintf (text, ap);
  73. va_end (ap);
  74. printf ("message(): %s\n", p);
  75. g_free (p);
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. static void
  79. fill_stat_struct (struct stat *etalon_stat, int iterator)
  80. {
  81. switch (iterator)
  82. {
  83. case 0:
  84. etalon_stat->st_dev = 0;
  85. etalon_stat->st_ino = 0;
  86. etalon_stat->st_mode = 0x41fd;
  87. etalon_stat->st_nlink = 10;
  88. etalon_stat->st_uid = 500;
  89. etalon_stat->st_gid = 500;
  90. etalon_stat->st_rdev = 0;
  91. etalon_stat->st_size = 4096;
  92. etalon_stat->st_blksize = 512;
  93. etalon_stat->st_blocks = 8;
  94. etalon_stat->st_atime = 1308838140;
  95. etalon_stat->st_mtime = 1308838140;
  96. etalon_stat->st_ctime = 1308838140;
  97. break;
  98. case 1:
  99. etalon_stat->st_dev = 0;
  100. etalon_stat->st_ino = 0;
  101. etalon_stat->st_mode = 0xa1ff;
  102. etalon_stat->st_nlink = 10;
  103. etalon_stat->st_uid = 500;
  104. etalon_stat->st_gid = 500;
  105. etalon_stat->st_rdev = 0;
  106. etalon_stat->st_size = 11;
  107. etalon_stat->st_blksize = 512;
  108. etalon_stat->st_blocks = 1;
  109. etalon_stat->st_atime = 1268431200;
  110. etalon_stat->st_mtime = 1268431200;
  111. etalon_stat->st_ctime = 1268431200;
  112. break;
  113. case 2:
  114. etalon_stat->st_dev = 0;
  115. etalon_stat->st_ino = 0;
  116. etalon_stat->st_mode = 0x41fd;
  117. etalon_stat->st_nlink = 10;
  118. etalon_stat->st_uid = 500;
  119. etalon_stat->st_gid = 500;
  120. etalon_stat->st_rdev = 0;
  121. etalon_stat->st_size = 4096;
  122. etalon_stat->st_blksize = 512;
  123. etalon_stat->st_blocks = 8;
  124. etalon_stat->st_atime = 1308838140;
  125. etalon_stat->st_mtime = 1308838140;
  126. etalon_stat->st_ctime = 1308838140;
  127. break;
  128. case 3:
  129. etalon_stat->st_dev = 0;
  130. etalon_stat->st_ino = 0;
  131. etalon_stat->st_mode = 0x41fd;
  132. etalon_stat->st_nlink = 10;
  133. etalon_stat->st_uid = 500;
  134. etalon_stat->st_gid = 500;
  135. etalon_stat->st_rdev = 0;
  136. etalon_stat->st_size = 4096;
  137. etalon_stat->st_blksize = 512;
  138. etalon_stat->st_blocks = 8;
  139. etalon_stat->st_atime = 1308838140;
  140. etalon_stat->st_mtime = 1308838140;
  141. etalon_stat->st_ctime = 1308838140;
  142. break;
  143. }
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. /* @DataSource("test_vfs_parse_ls_lga_ds") */
  147. /* *INDENT-OFF* */
  148. static const struct test_vfs_parse_ls_lga_ds
  149. {
  150. const char *input_string;
  151. int expected_result;
  152. const char *expected_filename;
  153. const char *expected_linkname;
  154. const size_t expected_filepos;
  155. } test_vfs_parse_ls_lga_ds[] =
  156. {
  157. { /* 0. */
  158. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  159. 1,
  160. "build_root",
  161. NULL,
  162. 0
  163. },
  164. { /* 1. */
  165. "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
  166. 1,
  167. "COPYING",
  168. "doc/COPYING",
  169. 0
  170. },
  171. { /* 2. */
  172. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  173. 1,
  174. "..",
  175. NULL,
  176. 0
  177. },
  178. { /* 3. */
  179. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  180. 1,
  181. "build_root",
  182. NULL,
  183. 0
  184. },
  185. };
  186. /* *INDENT-ON* */
  187. /* @Test(dataSource = "test_vfs_parse_ls_lga_ds") */
  188. /* *INDENT-OFF* */
  189. START_PARAMETRIZED_TEST (test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds)
  190. /* *INDENT-ON* */
  191. {
  192. /* given */
  193. size_t filepos = 0;
  194. struct stat etalon_stat;
  195. static struct stat test_stat;
  196. char *filename = NULL;
  197. char *linkname = NULL;
  198. gboolean actual_result;
  199. vfs_parse_ls_lga_init ();
  200. etalon_stat.st_blocks = 0;
  201. etalon_stat.st_size = 0;
  202. etalon_stat.st_mode = 0;
  203. fill_stat_struct (&etalon_stat, _i);
  204. /* when */
  205. actual_result =
  206. vfs_parse_ls_lga (data->input_string, &test_stat, &filename, &linkname, &filepos);
  207. /* then */
  208. mctest_assert_int_eq (actual_result, data->expected_result);
  209. mctest_assert_str_eq (filename, data->expected_filename);
  210. mctest_assert_str_eq (linkname, data->expected_linkname);
  211. mctest_assert_int_eq (etalon_stat.st_dev, test_stat.st_dev);
  212. mctest_assert_int_eq (etalon_stat.st_ino, test_stat.st_ino);
  213. mctest_assert_int_eq (etalon_stat.st_mode, test_stat.st_mode);
  214. mctest_assert_int_eq (etalon_stat.st_uid, test_stat.st_uid);
  215. mctest_assert_int_eq (etalon_stat.st_gid, test_stat.st_gid);
  216. mctest_assert_int_eq (etalon_stat.st_rdev, test_stat.st_rdev);
  217. mctest_assert_int_eq (etalon_stat.st_size, test_stat.st_size);
  218. mctest_assert_int_eq (etalon_stat.st_blksize, test_stat.st_blksize);
  219. mctest_assert_int_eq (etalon_stat.st_blocks, test_stat.st_blocks);
  220. /* FIXME: these commented checks are related to time zone!
  221. mctest_assert_int_eq (etalon_stat.st_atime, test_stat.st_atime);
  222. mctest_assert_int_eq (etalon_stat.st_mtime, test_stat.st_mtime);
  223. mctest_assert_int_eq (etalon_stat.st_ctime, test_stat.st_ctime);
  224. */
  225. }
  226. /* *INDENT-OFF* */
  227. END_PARAMETRIZED_TEST
  228. /* *INDENT-ON* */
  229. /* --------------------------------------------------------------------------------------------- */
  230. /* @Test */
  231. /* *INDENT-OFF* */
  232. START_TEST (test_vfs_parse_ls_lga_reorder)
  233. /* *INDENT-ON* */
  234. {
  235. /* given */
  236. size_t filepos = 0;
  237. struct vfs_s_entry *ent1, *ent2, *ent3;
  238. vfs_parse_ls_lga_init ();
  239. /* init ent1 */
  240. ent1 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  241. vfs_parse_ls_lga
  242. ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1", &ent1->ino->st,
  243. &ent1->name, &ent1->ino->linkname, &filepos);
  244. vfs_s_store_filename_leading_spaces (ent1, filepos);
  245. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent1);
  246. /* init ent2 */
  247. ent2 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  248. vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
  249. &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos);
  250. vfs_s_store_filename_leading_spaces (ent2, filepos);
  251. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent2);
  252. /* init ent3 */
  253. ent3 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  254. vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  255. &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos);
  256. vfs_s_store_filename_leading_spaces (ent3, filepos);
  257. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent3);
  258. /* when */
  259. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  260. /* then */
  261. mctest_assert_str_eq (ent1->name, " build_root1");
  262. mctest_assert_str_eq (ent2->name, " build_root2");
  263. }
  264. /* *INDENT-OFF* */
  265. END_TEST
  266. /* *INDENT-ON* */
  267. /* --------------------------------------------------------------------------------------------- */
  268. #define parce_one_line(ent_index, ls_output) {\
  269. ent[ent_index] = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);\
  270. if (! vfs_parse_ls_lga (ls_output,\
  271. &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
  272. {\
  273. fail ("An error occurred while parse ls output");\
  274. return;\
  275. }\
  276. vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
  277. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
  278. \
  279. }
  280. /* @Test */
  281. /* *INDENT-OFF* */
  282. START_TEST (test_vfs_parse_ls_lga_unaligned)
  283. /* *INDENT-ON* */
  284. {
  285. /* given */
  286. size_t filepos = 0;
  287. struct vfs_s_entry *ent[4];
  288. vfs_parse_ls_lga_init ();
  289. parce_one_line (0, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1");
  290. parce_one_line (1, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2");
  291. parce_one_line (2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..");
  292. parce_one_line (3,
  293. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root 0");
  294. /* when */
  295. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  296. /* then */
  297. mctest_assert_str_eq (ent[0]->name, "build_root1");
  298. mctest_assert_str_eq (ent[0]->name, "build_root1");
  299. mctest_assert_str_eq (ent[1]->name, " build_root2");
  300. mctest_assert_str_eq (ent[3]->name, " build_root 0");
  301. }
  302. /* *INDENT-OFF* */
  303. END_TEST
  304. /* *INDENT-ON* */
  305. /* --------------------------------------------------------------------------------------------- */
  306. int
  307. main (void)
  308. {
  309. int number_failed;
  310. Suite *s = suite_create (TEST_SUITE_NAME);
  311. TCase *tc_core = tcase_create ("Core");
  312. SRunner *sr;
  313. tcase_add_checked_fixture (tc_core, setup, teardown);
  314. /* Add new tests here: *************** */
  315. mctest_add_parameterized_test (tc_core, test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds);
  316. tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
  317. tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
  318. /* *********************************** */
  319. suite_add_tcase (s, tc_core);
  320. sr = srunner_create (s);
  321. srunner_set_log (sr, "vfs_parse_ls_lga.log");
  322. srunner_run_all (sr, CK_NORMAL);
  323. number_failed = srunner_ntests_failed (sr);
  324. srunner_free (sr);
  325. return (number_failed == 0) ? 0 : 1;
  326. }
  327. /* --------------------------------------------------------------------------------------------- */