vfs_parse_ls_lga.c 13 KB

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