vfs_parse_ls_lga.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. lib/vfs - test vfs_parse_ls_lga() functionality
  3. Copyright (C) 2011-2017
  4. 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. /* *INDENT-OFF* */
  32. void message (int flags, const char *title, const char *text, ...) G_GNUC_PRINTF (3, 4);
  33. /* *INDENT-ON* */
  34. /* --------------------------------------------------------------------------------------------- */
  35. /* @Before */
  36. static void
  37. setup (void)
  38. {
  39. static struct stat initstat;
  40. str_init_strings (NULL);
  41. vfs_init ();
  42. init_localfs ();
  43. vfs_setup_work_dir ();
  44. test_subclass1.flags = VFS_S_REMOTE;
  45. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  46. vfs_test_ops1.name = "testfs1";
  47. vfs_test_ops1.flags = VFSF_NOLINKS;
  48. vfs_test_ops1.prefix = "test1:";
  49. vfs_register_class (&vfs_test_ops1);
  50. vfs_test_super = g_new0 (struct vfs_s_super, 1);
  51. vfs_test_super->me = &vfs_test_ops1;
  52. vfs_root_inode = vfs_s_new_inode (&vfs_test_ops1, vfs_test_super, &initstat);
  53. vfs_root_entry = vfs_s_new_entry (&vfs_test_ops1, "/", vfs_root_inode);
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. /* @After */
  57. static void
  58. teardown (void)
  59. {
  60. vfs_s_free_entry (&vfs_test_ops1, vfs_root_entry);
  61. vfs_shut ();
  62. str_uninit_strings ();
  63. }
  64. /* --------------------------------------------------------------------------------------------- */
  65. /* @Mock */
  66. void
  67. message (int flags, const char *title, const char *text, ...)
  68. {
  69. char *p;
  70. va_list ap;
  71. (void) flags;
  72. (void) title;
  73. va_start (ap, text);
  74. p = g_strdup_vprintf (text, ap);
  75. va_end (ap);
  76. printf ("message(): %s\n", p);
  77. g_free (p);
  78. }
  79. /* --------------------------------------------------------------------------------------------- */
  80. static void
  81. fill_stat_struct (struct stat *etalon_stat, int iterator)
  82. {
  83. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  84. etalon_stat->st_atim.tv_nsec = etalon_stat->st_mtim.tv_nsec = etalon_stat->st_ctim.tv_nsec = 0;
  85. #endif
  86. switch (iterator)
  87. {
  88. case 0:
  89. etalon_stat->st_dev = 0;
  90. etalon_stat->st_ino = 0;
  91. etalon_stat->st_mode = 0x41fd;
  92. etalon_stat->st_nlink = 10;
  93. etalon_stat->st_uid = 500;
  94. etalon_stat->st_gid = 500;
  95. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  96. etalon_stat->st_rdev = 0;
  97. #endif
  98. etalon_stat->st_size = 4096;
  99. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  100. etalon_stat->st_blksize = 512;
  101. #endif
  102. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  103. etalon_stat->st_blocks = 8;
  104. #endif
  105. etalon_stat->st_atime = 1308838140;
  106. etalon_stat->st_mtime = 1308838140;
  107. etalon_stat->st_ctime = 1308838140;
  108. break;
  109. case 1:
  110. etalon_stat->st_dev = 0;
  111. etalon_stat->st_ino = 0;
  112. etalon_stat->st_mode = 0xa1ff;
  113. etalon_stat->st_nlink = 10;
  114. etalon_stat->st_uid = 500;
  115. etalon_stat->st_gid = 500;
  116. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  117. etalon_stat->st_rdev = 0;
  118. #endif
  119. etalon_stat->st_size = 11;
  120. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  121. etalon_stat->st_blksize = 512;
  122. #endif
  123. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  124. etalon_stat->st_blocks = 1;
  125. #endif
  126. etalon_stat->st_atime = 1268431200;
  127. etalon_stat->st_mtime = 1268431200;
  128. etalon_stat->st_ctime = 1268431200;
  129. break;
  130. case 2:
  131. etalon_stat->st_dev = 0;
  132. etalon_stat->st_ino = 0;
  133. etalon_stat->st_mode = 0x41fd;
  134. etalon_stat->st_nlink = 10;
  135. etalon_stat->st_uid = 500;
  136. etalon_stat->st_gid = 500;
  137. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  138. etalon_stat->st_rdev = 0;
  139. #endif
  140. etalon_stat->st_size = 4096;
  141. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  142. etalon_stat->st_blksize = 512;
  143. #endif
  144. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  145. etalon_stat->st_blocks = 8;
  146. #endif
  147. etalon_stat->st_atime = 1308838140;
  148. etalon_stat->st_mtime = 1308838140;
  149. etalon_stat->st_ctime = 1308838140;
  150. break;
  151. case 3:
  152. etalon_stat->st_dev = 0;
  153. etalon_stat->st_ino = 0;
  154. etalon_stat->st_mode = 0x41fd;
  155. etalon_stat->st_nlink = 10;
  156. etalon_stat->st_uid = 500;
  157. etalon_stat->st_gid = 500;
  158. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  159. etalon_stat->st_rdev = 0;
  160. #endif
  161. etalon_stat->st_size = 4096;
  162. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  163. etalon_stat->st_blksize = 512;
  164. #endif
  165. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  166. etalon_stat->st_blocks = 8;
  167. #endif
  168. etalon_stat->st_atime = 1308838140;
  169. etalon_stat->st_mtime = 1308838140;
  170. etalon_stat->st_ctime = 1308838140;
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. /* --------------------------------------------------------------------------------------------- */
  177. /* @DataSource("test_vfs_parse_ls_lga_ds") */
  178. /* *INDENT-OFF* */
  179. static const struct test_vfs_parse_ls_lga_ds
  180. {
  181. const char *input_string;
  182. int expected_result;
  183. const char *expected_filename;
  184. const char *expected_linkname;
  185. const size_t expected_filepos;
  186. } test_vfs_parse_ls_lga_ds[] =
  187. {
  188. { /* 0. */
  189. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  190. 1,
  191. "build_root",
  192. NULL,
  193. 0
  194. },
  195. { /* 1. */
  196. "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
  197. 1,
  198. "COPYING",
  199. "doc/COPYING",
  200. 0
  201. },
  202. { /* 2. */
  203. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  204. 1,
  205. "..",
  206. NULL,
  207. 0
  208. },
  209. { /* 3. */
  210. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  211. 1,
  212. "build_root",
  213. NULL,
  214. 0
  215. },
  216. };
  217. /* *INDENT-ON* */
  218. /* @Test(dataSource = "test_vfs_parse_ls_lga_ds") */
  219. /* *INDENT-OFF* */
  220. START_PARAMETRIZED_TEST (test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds)
  221. /* *INDENT-ON* */
  222. {
  223. /* given */
  224. size_t filepos = 0;
  225. struct stat etalon_stat;
  226. static struct stat test_stat;
  227. char *filename = NULL;
  228. char *linkname = NULL;
  229. gboolean actual_result;
  230. vfs_parse_ls_lga_init ();
  231. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  232. etalon_stat.st_blocks = 0;
  233. #endif
  234. etalon_stat.st_size = 0;
  235. etalon_stat.st_mode = 0;
  236. fill_stat_struct (&etalon_stat, _i);
  237. /* when */
  238. actual_result =
  239. vfs_parse_ls_lga (data->input_string, &test_stat, &filename, &linkname, &filepos);
  240. /* then */
  241. mctest_assert_int_eq (actual_result, data->expected_result);
  242. mctest_assert_str_eq (filename, data->expected_filename);
  243. mctest_assert_str_eq (linkname, data->expected_linkname);
  244. mctest_assert_int_eq (etalon_stat.st_dev, test_stat.st_dev);
  245. mctest_assert_int_eq (etalon_stat.st_ino, test_stat.st_ino);
  246. mctest_assert_int_eq (etalon_stat.st_mode, test_stat.st_mode);
  247. mctest_assert_int_eq (etalon_stat.st_uid, test_stat.st_uid);
  248. mctest_assert_int_eq (etalon_stat.st_gid, test_stat.st_gid);
  249. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  250. mctest_assert_int_eq (etalon_stat.st_rdev, test_stat.st_rdev);
  251. #endif
  252. mctest_assert_int_eq (etalon_stat.st_size, test_stat.st_size);
  253. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  254. mctest_assert_int_eq (etalon_stat.st_blksize, test_stat.st_blksize);
  255. #endif
  256. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  257. mctest_assert_int_eq (etalon_stat.st_blocks, test_stat.st_blocks);
  258. #endif
  259. /* FIXME: these commented checks are related to time zone!
  260. mctest_assert_int_eq (etalon_stat.st_atime, test_stat.st_atime);
  261. mctest_assert_int_eq (etalon_stat.st_mtime, test_stat.st_mtime);
  262. mctest_assert_int_eq (etalon_stat.st_ctime, test_stat.st_ctime);
  263. */
  264. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  265. mctest_assert_int_eq (0, test_stat.st_atim.tv_nsec);
  266. mctest_assert_int_eq (0, test_stat.st_mtim.tv_nsec);
  267. mctest_assert_int_eq (0, test_stat.st_ctim.tv_nsec);
  268. #endif
  269. }
  270. /* *INDENT-OFF* */
  271. END_PARAMETRIZED_TEST
  272. /* *INDENT-ON* */
  273. /* --------------------------------------------------------------------------------------------- */
  274. /* @Test */
  275. /* *INDENT-OFF* */
  276. START_TEST (test_vfs_parse_ls_lga_reorder)
  277. /* *INDENT-ON* */
  278. {
  279. /* given */
  280. size_t filepos = 0;
  281. struct vfs_s_entry *ent1, *ent2, *ent3;
  282. vfs_parse_ls_lga_init ();
  283. /* init ent1 */
  284. ent1 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  285. vfs_parse_ls_lga
  286. ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1", &ent1->ino->st,
  287. &ent1->name, &ent1->ino->linkname, &filepos);
  288. vfs_s_store_filename_leading_spaces (ent1, filepos);
  289. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent1);
  290. /* init ent2 */
  291. ent2 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  292. vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
  293. &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos);
  294. vfs_s_store_filename_leading_spaces (ent2, filepos);
  295. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent2);
  296. /* init ent3 */
  297. ent3 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  298. vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  299. &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos);
  300. vfs_s_store_filename_leading_spaces (ent3, filepos);
  301. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent3);
  302. /* when */
  303. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  304. /* then */
  305. mctest_assert_str_eq (ent1->name, " build_root1");
  306. mctest_assert_str_eq (ent2->name, " build_root2");
  307. }
  308. /* *INDENT-OFF* */
  309. END_TEST
  310. /* *INDENT-ON* */
  311. /* --------------------------------------------------------------------------------------------- */
  312. #define parce_one_line(ent_index, ls_output) {\
  313. ent[ent_index] = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);\
  314. if (! vfs_parse_ls_lga (ls_output,\
  315. &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
  316. {\
  317. fail ("An error occurred while parse ls output");\
  318. return;\
  319. }\
  320. vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
  321. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
  322. \
  323. }
  324. /* @Test */
  325. /* *INDENT-OFF* */
  326. START_TEST (test_vfs_parse_ls_lga_unaligned)
  327. /* *INDENT-ON* */
  328. {
  329. /* given */
  330. size_t filepos = 0;
  331. struct vfs_s_entry *ent[4];
  332. vfs_parse_ls_lga_init ();
  333. parce_one_line (0, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1");
  334. parce_one_line (1, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2");
  335. parce_one_line (2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..");
  336. parce_one_line (3,
  337. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root 0");
  338. /* when */
  339. vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
  340. /* then */
  341. mctest_assert_str_eq (ent[0]->name, "build_root1");
  342. mctest_assert_str_eq (ent[0]->name, "build_root1");
  343. mctest_assert_str_eq (ent[1]->name, " build_root2");
  344. mctest_assert_str_eq (ent[3]->name, " build_root 0");
  345. }
  346. /* *INDENT-OFF* */
  347. END_TEST
  348. /* *INDENT-ON* */
  349. /* --------------------------------------------------------------------------------------------- */
  350. int
  351. main (void)
  352. {
  353. int number_failed;
  354. Suite *s = suite_create (TEST_SUITE_NAME);
  355. TCase *tc_core = tcase_create ("Core");
  356. SRunner *sr;
  357. tcase_add_checked_fixture (tc_core, setup, teardown);
  358. /* Add new tests here: *************** */
  359. mctest_add_parameterized_test (tc_core, test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds);
  360. tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
  361. tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
  362. /* *********************************** */
  363. suite_add_tcase (s, tc_core);
  364. sr = srunner_create (s);
  365. srunner_set_log (sr, "vfs_parse_ls_lga.log");
  366. srunner_run_all (sr, CK_ENV);
  367. number_failed = srunner_ntests_failed (sr);
  368. srunner_free (sr);
  369. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  370. }
  371. /* --------------------------------------------------------------------------------------------- */