1 /*
2    lib/vfs - test vfs_parse_ls_lga() functionality
3 
4    Copyright (C) 2011-2021
5    Free Software Foundation, Inc.
6 
7    Written by:
8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
9 
10    This file is part of the Midnight Commander.
11 
12    The Midnight Commander is free software: you can redistribute it
13    and/or modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation, either version 3 of the License,
15    or (at your option) any later version.
16 
17    The Midnight Commander is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #define TEST_SUITE_NAME "/lib/vfs"
27 
28 #include "tests/mctest.h"
29 
30 #include <stdio.h>
31 
32 #include "lib/vfs/utilvfs.h"
33 #include "lib/vfs/xdirentry.h"
34 #include "lib/strutil.h"
35 
36 #include "src/vfs/local/local.c"
37 
38 
39 struct vfs_s_subclass test_subclass1;
40 static struct vfs_class *vfs_test_ops1 = VFS_CLASS (&test_subclass1);
41 
42 struct vfs_s_entry *vfs_root_entry;
43 static struct vfs_s_inode *vfs_root_inode;
44 static struct vfs_s_super *vfs_test_super;
45 
46 /* *INDENT-OFF* */
47 void message (int flags, const char *title, const char *text, ...) G_GNUC_PRINTF (3, 4);
48 /* *INDENT-ON* */
49 
50 /* --------------------------------------------------------------------------------------------- */
51 
52 /* @Before */
53 static void
setup(void)54 setup (void)
55 {
56     static struct stat initstat;
57 
58     str_init_strings (NULL);
59 
60     vfs_init ();
61     vfs_init_localfs ();
62     vfs_setup_work_dir ();
63 
64     vfs_init_subclass (&test_subclass1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
65     vfs_register_class (vfs_test_ops1);
66 
67     vfs_test_super = g_new0 (struct vfs_s_super, 1);
68     vfs_test_super->me = vfs_test_ops1;
69 
70     vfs_root_inode = vfs_s_new_inode (vfs_test_ops1, vfs_test_super, &initstat);
71     vfs_root_entry = vfs_s_new_entry (vfs_test_ops1, "/", vfs_root_inode);
72 }
73 
74 /* --------------------------------------------------------------------------------------------- */
75 
76 /* @After */
77 static void
teardown(void)78 teardown (void)
79 {
80     vfs_s_free_entry (vfs_test_ops1, vfs_root_entry);
81     vfs_shut ();
82     str_uninit_strings ();
83 }
84 
85 /* --------------------------------------------------------------------------------------------- */
86 
87 /* @Mock */
88 void
message(int flags,const char * title,const char * text,...)89 message (int flags, const char *title, const char *text, ...)
90 {
91     char *p;
92     va_list ap;
93 
94     (void) flags;
95     (void) title;
96 
97     va_start (ap, text);
98     p = g_strdup_vprintf (text, ap);
99     va_end (ap);
100     printf ("message(): %s\n", p);
101     g_free (p);
102 }
103 
104 /* --------------------------------------------------------------------------------------------- */
105 
106 static void
fill_stat_struct(struct stat * etalon_stat,int iterator)107 fill_stat_struct (struct stat *etalon_stat, int iterator)
108 {
109 
110 #ifdef HAVE_STRUCT_STAT_ST_MTIM
111     etalon_stat->st_atim.tv_nsec = etalon_stat->st_mtim.tv_nsec = etalon_stat->st_ctim.tv_nsec = 0;
112 #endif
113 
114     switch (iterator)
115     {
116     case 0:
117         etalon_stat->st_dev = 0;
118         etalon_stat->st_ino = 0;
119         etalon_stat->st_mode = 0x41fd;
120         etalon_stat->st_nlink = 10;
121         etalon_stat->st_uid = 500;
122         etalon_stat->st_gid = 500;
123 #ifdef HAVE_STRUCT_STAT_ST_RDEV
124         etalon_stat->st_rdev = 0;
125 #endif
126         etalon_stat->st_size = 4096;
127 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
128         etalon_stat->st_blksize = 512;
129 #endif
130 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
131         etalon_stat->st_blocks = 8;
132 #endif
133         etalon_stat->st_atime = 1308838140;
134         etalon_stat->st_mtime = 1308838140;
135         etalon_stat->st_ctime = 1308838140;
136         break;
137     case 1:
138         etalon_stat->st_dev = 0;
139         etalon_stat->st_ino = 0;
140         etalon_stat->st_mode = 0xa1ff;
141         etalon_stat->st_nlink = 10;
142         etalon_stat->st_uid = 500;
143         etalon_stat->st_gid = 500;
144 #ifdef HAVE_STRUCT_STAT_ST_RDEV
145         etalon_stat->st_rdev = 0;
146 #endif
147         etalon_stat->st_size = 11;
148 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
149         etalon_stat->st_blksize = 512;
150 #endif
151 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
152         etalon_stat->st_blocks = 1;
153 #endif
154         etalon_stat->st_atime = 1268431200;
155         etalon_stat->st_mtime = 1268431200;
156         etalon_stat->st_ctime = 1268431200;
157         break;
158     case 2:
159         etalon_stat->st_dev = 0;
160         etalon_stat->st_ino = 0;
161         etalon_stat->st_mode = 0x41fd;
162         etalon_stat->st_nlink = 10;
163         etalon_stat->st_uid = 500;
164         etalon_stat->st_gid = 500;
165 #ifdef HAVE_STRUCT_STAT_ST_RDEV
166         etalon_stat->st_rdev = 0;
167 #endif
168         etalon_stat->st_size = 4096;
169 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
170         etalon_stat->st_blksize = 512;
171 #endif
172 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
173         etalon_stat->st_blocks = 8;
174 #endif
175         etalon_stat->st_atime = 1308838140;
176         etalon_stat->st_mtime = 1308838140;
177         etalon_stat->st_ctime = 1308838140;
178         break;
179     case 3:
180         etalon_stat->st_dev = 0;
181         etalon_stat->st_ino = 0;
182         etalon_stat->st_mode = 0x41fd;
183         etalon_stat->st_nlink = 10;
184         etalon_stat->st_uid = 500;
185         etalon_stat->st_gid = 500;
186 #ifdef HAVE_STRUCT_STAT_ST_RDEV
187         etalon_stat->st_rdev = 0;
188 #endif
189         etalon_stat->st_size = 4096;
190 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
191         etalon_stat->st_blksize = 512;
192 #endif
193 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
194         etalon_stat->st_blocks = 8;
195 #endif
196         etalon_stat->st_atime = 1308838140;
197         etalon_stat->st_mtime = 1308838140;
198         etalon_stat->st_ctime = 1308838140;
199         break;
200     default:
201         break;
202     }
203 }
204 
205 /* --------------------------------------------------------------------------------------------- */
206 
207 /* @DataSource("test_vfs_parse_ls_lga_ds") */
208 /* *INDENT-OFF* */
209 static const struct test_vfs_parse_ls_lga_ds
210 {
211     const char *input_string;
212     int expected_result;
213     const char *expected_filename;
214     const char *expected_linkname;
215     const size_t expected_filepos;
216 } test_vfs_parse_ls_lga_ds[] =
217 {
218     { /* 0. */
219         "drwxrwxr-x   10 500      500          4096 Jun 23 17:09 build_root",
220         1,
221         "build_root",
222         NULL,
223         0
224     },
225     { /* 1. */
226         "lrwxrwxrwx    1 500      500            11 Mar 13  2010 COPYING -> doc/COPYING",
227         1,
228         "COPYING",
229         "doc/COPYING",
230         0
231     },
232     { /* 2. */
233         "drwxrwxr-x   10 500      500          4096 Jun 23 17:09 ..",
234         1,
235         "..",
236         NULL,
237         0
238     },
239     { /* 3. */
240         "drwxrwxr-x   10 500      500          4096 Jun 23 17:09   build_root",
241         1,
242         "build_root",
243         NULL,
244         0
245     },
246 };
247 /* *INDENT-ON* */
248 
249 /* @Test(dataSource = "test_vfs_parse_ls_lga_ds") */
250 /* *INDENT-OFF* */
START_PARAMETRIZED_TEST(test_vfs_parse_ls_lga,test_vfs_parse_ls_lga_ds)251 START_PARAMETRIZED_TEST (test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds)
252 /* *INDENT-ON* */
253 
254 {
255     /* given */
256     size_t filepos = 0;
257     struct stat etalon_stat;
258     static struct stat test_stat;
259     char *filename = NULL;
260     char *linkname = NULL;
261     gboolean actual_result;
262 
263     vfs_parse_ls_lga_init ();
264 
265 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
266     etalon_stat.st_blocks = 0;
267 #endif
268     etalon_stat.st_size = 0;
269     etalon_stat.st_mode = 0;
270     fill_stat_struct (&etalon_stat, _i);
271 
272     /* when */
273     actual_result =
274         vfs_parse_ls_lga (data->input_string, &test_stat, &filename, &linkname, &filepos);
275 
276     /* then */
277     mctest_assert_int_eq (actual_result, data->expected_result);
278 
279     mctest_assert_str_eq (filename, data->expected_filename);
280     mctest_assert_str_eq (linkname, data->expected_linkname);
281 
282     mctest_assert_int_eq (etalon_stat.st_dev, test_stat.st_dev);
283     mctest_assert_int_eq (etalon_stat.st_ino, test_stat.st_ino);
284     mctest_assert_int_eq (etalon_stat.st_mode, test_stat.st_mode);
285     mctest_assert_int_eq (etalon_stat.st_uid, test_stat.st_uid);
286     mctest_assert_int_eq (etalon_stat.st_gid, test_stat.st_gid);
287 #ifdef HAVE_STRUCT_STAT_ST_RDEV
288     mctest_assert_int_eq (etalon_stat.st_rdev, test_stat.st_rdev);
289 #endif
290     mctest_assert_int_eq (etalon_stat.st_size, test_stat.st_size);
291 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
292     mctest_assert_int_eq (etalon_stat.st_blksize, test_stat.st_blksize);
293 #endif
294 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
295     mctest_assert_int_eq (etalon_stat.st_blocks, test_stat.st_blocks);
296 #endif
297 
298     /* FIXME: these commented checks are related to time zone!
299        mctest_assert_int_eq (etalon_stat.st_atime, test_stat.st_atime);
300        mctest_assert_int_eq (etalon_stat.st_mtime, test_stat.st_mtime);
301        mctest_assert_int_eq (etalon_stat.st_ctime, test_stat.st_ctime);
302      */
303 
304 #ifdef HAVE_STRUCT_STAT_ST_MTIM
305     mctest_assert_int_eq (0, test_stat.st_atim.tv_nsec);
306     mctest_assert_int_eq (0, test_stat.st_mtim.tv_nsec);
307     mctest_assert_int_eq (0, test_stat.st_ctim.tv_nsec);
308 #endif
309 
310 }
311 /* *INDENT-OFF* */
312 END_PARAMETRIZED_TEST
313 /* *INDENT-ON* */
314 
315 /* --------------------------------------------------------------------------------------------- */
316 
317 /* @Test */
318 /* *INDENT-OFF* */
START_TEST(test_vfs_parse_ls_lga_reorder)319 START_TEST (test_vfs_parse_ls_lga_reorder)
320 /* *INDENT-ON* */
321 {
322     /* given */
323     size_t filepos = 0;
324     struct vfs_s_entry *ent1, *ent2, *ent3;
325 
326     vfs_parse_ls_lga_init ();
327 
328     /* init ent1 */
329     ent1 = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);
330     vfs_parse_ls_lga
331         ("drwxrwxr-x   10 500      500          4096 Jun 23 17:09      build_root1", &ent1->ino->st,
332          &ent1->name, &ent1->ino->linkname, &filepos);
333     vfs_s_store_filename_leading_spaces (ent1, filepos);
334     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent1);
335 
336 
337     /* init ent2 */
338     ent2 = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);
339     vfs_parse_ls_lga ("drwxrwxr-x   10 500      500          4096 Jun 23 17:09    build_root2",
340                       &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos);
341     vfs_s_store_filename_leading_spaces (ent2, filepos);
342     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent2);
343 
344     /* init ent3 */
345     ent3 = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);
346     vfs_parse_ls_lga ("drwxrwxr-x   10 500      500          4096 Jun 23 17:09 ..",
347                       &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos);
348     vfs_s_store_filename_leading_spaces (ent3, filepos);
349     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent3);
350 
351     /* when */
352     vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
353 
354     /* then */
355     mctest_assert_str_eq (ent1->name, "     build_root1");
356     mctest_assert_str_eq (ent2->name, "   build_root2");
357 }
358 /* *INDENT-OFF* */
359 END_TEST
360 /* *INDENT-ON* */
361 
362 /* --------------------------------------------------------------------------------------------- */
363 #define parce_one_line(ent_index, ls_output) {\
364     ent[ent_index] = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);\
365     if (! vfs_parse_ls_lga (ls_output,\
366     &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
367     {\
368         ck_abort_msg ("An error occurred while parse ls output");\
369         return;\
370     }\
371     vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
372     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
373     \
374 }
375 
376 /* @Test */
377 /* *INDENT-OFF* */
START_TEST(test_vfs_parse_ls_lga_unaligned)378 START_TEST (test_vfs_parse_ls_lga_unaligned)
379 /* *INDENT-ON* */
380 {
381     /* given */
382     size_t filepos = 0;
383     struct vfs_s_entry *ent[4];
384 
385     vfs_parse_ls_lga_init ();
386 
387     parce_one_line (0, "drwxrwxr-x   10 500      500          4096 Jun 23 17:09  build_root1");
388     parce_one_line (1, "drwxrwxr-x   10 500     500         4096 Jun 23 17:09     build_root2");
389     parce_one_line (2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09  ..");
390     parce_one_line (3,
391                     "drwxrwxr-x      10   500        500             4096   Jun   23   17:09   build_root 0");
392 
393     /* when */
394     vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
395 
396     /* then */
397     mctest_assert_str_eq (ent[0]->name, "build_root1");
398     mctest_assert_str_eq (ent[0]->name, "build_root1");
399     mctest_assert_str_eq (ent[1]->name, "   build_root2");
400     mctest_assert_str_eq (ent[3]->name, " build_root 0");
401 }
402 /* *INDENT-OFF* */
403 END_TEST
404 /* *INDENT-ON* */
405 
406 /* --------------------------------------------------------------------------------------------- */
407 
408 int
main(void)409 main (void)
410 {
411     TCase *tc_core;
412 
413     tc_core = tcase_create ("Core");
414 
415     tcase_add_checked_fixture (tc_core, setup, teardown);
416 
417     /* Add new tests here: *************** */
418     mctest_add_parameterized_test (tc_core, test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds);
419     tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
420     tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
421     /* *********************************** */
422 
423     return mctest_run_all (tc_core);
424 }
425 
426 /* --------------------------------------------------------------------------------------------- */
427