vfs_parse_ls_lga.c 13 KB

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