vfs_parse_ls_lga.c 13 KB

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