Clarify usage of st_rdev. Use it if HAVE_STRUCT_STAT_ST_RDEV is defined.
[midnight-commander.git] / tests / lib / vfs / vfs_parse_ls_lga.c
blob77d2d2b443a729ef89db65aa12bde956881bd016
1 /*
2 lib/vfs - test vfs_parse_ls_lga() functionality
4 Copyright (C) 2011-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
10 This file is part of the Midnight Commander.
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.
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.
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/>.
26 #define TEST_SUITE_NAME "/lib/vfs"
28 #include "tests/mctest.h"
30 #include <stdio.h>
32 #include "lib/vfs/utilvfs.h"
33 #include "lib/vfs/xdirentry.h"
34 #include "lib/strutil.h"
36 #include "src/vfs/local/local.c"
39 struct vfs_s_subclass test_subclass1;
40 struct vfs_class vfs_test_ops1;
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;
46 /* *INDENT-OFF* */
47 void message (int flags, const char *title, const char *text, ...) G_GNUC_PRINTF (3, 4);
48 /* *INDENT-ON* */
50 /* --------------------------------------------------------------------------------------------- */
52 /* @Before */
53 static void
54 setup (void)
56 static struct stat initstat;
58 str_init_strings (NULL);
60 vfs_init ();
61 init_localfs ();
62 vfs_setup_work_dir ();
64 test_subclass1.flags = VFS_S_REMOTE;
65 vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
66 vfs_test_ops1.name = "testfs1";
67 vfs_test_ops1.flags = VFSF_NOLINKS;
68 vfs_test_ops1.prefix = "test1:";
69 vfs_register_class (&vfs_test_ops1);
71 vfs_test_super = g_new0 (struct vfs_s_super, 1);
72 vfs_test_super->me = &vfs_test_ops1;
74 vfs_root_inode = vfs_s_new_inode (&vfs_test_ops1, vfs_test_super, &initstat);
75 vfs_root_entry = vfs_s_new_entry (&vfs_test_ops1, "/", vfs_root_inode);
78 /* --------------------------------------------------------------------------------------------- */
80 /* @After */
81 static void
82 teardown (void)
84 vfs_s_free_entry (&vfs_test_ops1, vfs_root_entry);
85 vfs_shut ();
86 str_uninit_strings ();
89 /* --------------------------------------------------------------------------------------------- */
91 /* @Mock */
92 void
93 message (int flags, const char *title, const char *text, ...)
95 char *p;
96 va_list ap;
98 (void) flags;
99 (void) title;
101 va_start (ap, text);
102 p = g_strdup_vprintf (text, ap);
103 va_end (ap);
104 printf ("message(): %s\n", p);
105 g_free (p);
108 /* --------------------------------------------------------------------------------------------- */
110 static void
111 fill_stat_struct (struct stat *etalon_stat, int iterator)
113 switch (iterator)
115 case 0:
116 etalon_stat->st_dev = 0;
117 etalon_stat->st_ino = 0;
118 etalon_stat->st_mode = 0x41fd;
119 etalon_stat->st_nlink = 10;
120 etalon_stat->st_uid = 500;
121 etalon_stat->st_gid = 500;
122 #ifdef HAVE_STRUCT_STAT_ST_RDEV
123 etalon_stat->st_rdev = 0;
124 #endif
125 etalon_stat->st_size = 4096;
126 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
127 etalon_stat->st_blksize = 512;
128 #endif
129 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
130 etalon_stat->st_blocks = 8;
131 #endif
132 etalon_stat->st_atime = 1308838140;
133 etalon_stat->st_mtime = 1308838140;
134 etalon_stat->st_ctime = 1308838140;
135 break;
136 case 1:
137 etalon_stat->st_dev = 0;
138 etalon_stat->st_ino = 0;
139 etalon_stat->st_mode = 0xa1ff;
140 etalon_stat->st_nlink = 10;
141 etalon_stat->st_uid = 500;
142 etalon_stat->st_gid = 500;
143 #ifdef HAVE_STRUCT_STAT_ST_RDEV
144 etalon_stat->st_rdev = 0;
145 #endif
146 etalon_stat->st_size = 11;
147 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
148 etalon_stat->st_blksize = 512;
149 #endif
150 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
151 etalon_stat->st_blocks = 1;
152 #endif
153 etalon_stat->st_atime = 1268431200;
154 etalon_stat->st_mtime = 1268431200;
155 etalon_stat->st_ctime = 1268431200;
156 break;
157 case 2:
158 etalon_stat->st_dev = 0;
159 etalon_stat->st_ino = 0;
160 etalon_stat->st_mode = 0x41fd;
161 etalon_stat->st_nlink = 10;
162 etalon_stat->st_uid = 500;
163 etalon_stat->st_gid = 500;
164 #ifdef HAVE_STRUCT_STAT_ST_RDEV
165 etalon_stat->st_rdev = 0;
166 #endif
167 etalon_stat->st_size = 4096;
168 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
169 etalon_stat->st_blksize = 512;
170 #endif
171 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
172 etalon_stat->st_blocks = 8;
173 #endif
174 etalon_stat->st_atime = 1308838140;
175 etalon_stat->st_mtime = 1308838140;
176 etalon_stat->st_ctime = 1308838140;
177 break;
178 case 3:
179 etalon_stat->st_dev = 0;
180 etalon_stat->st_ino = 0;
181 etalon_stat->st_mode = 0x41fd;
182 etalon_stat->st_nlink = 10;
183 etalon_stat->st_uid = 500;
184 etalon_stat->st_gid = 500;
185 #ifdef HAVE_STRUCT_STAT_ST_RDEV
186 etalon_stat->st_rdev = 0;
187 #endif
188 etalon_stat->st_size = 4096;
189 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
190 etalon_stat->st_blksize = 512;
191 #endif
192 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
193 etalon_stat->st_blocks = 8;
194 #endif
195 etalon_stat->st_atime = 1308838140;
196 etalon_stat->st_mtime = 1308838140;
197 etalon_stat->st_ctime = 1308838140;
198 break;
199 default:
200 break;
204 /* --------------------------------------------------------------------------------------------- */
206 /* @DataSource("test_vfs_parse_ls_lga_ds") */
207 /* *INDENT-OFF* */
208 static const struct test_vfs_parse_ls_lga_ds
210 const char *input_string;
211 int expected_result;
212 const char *expected_filename;
213 const char *expected_linkname;
214 const size_t expected_filepos;
215 } test_vfs_parse_ls_lga_ds[] =
217 { /* 0. */
218 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
220 "build_root",
221 NULL,
224 { /* 1. */
225 "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
227 "COPYING",
228 "doc/COPYING",
231 { /* 2. */
232 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
234 "..",
235 NULL,
238 { /* 3. */
239 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
241 "build_root",
242 NULL,
246 /* *INDENT-ON* */
248 /* @Test(dataSource = "test_vfs_parse_ls_lga_ds") */
249 /* *INDENT-OFF* */
250 START_PARAMETRIZED_TEST (test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds)
251 /* *INDENT-ON* */
254 /* given */
255 size_t filepos = 0;
256 struct stat etalon_stat;
257 static struct stat test_stat;
258 char *filename = NULL;
259 char *linkname = NULL;
260 gboolean actual_result;
262 vfs_parse_ls_lga_init ();
264 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
265 etalon_stat.st_blocks = 0;
266 #endif
267 etalon_stat.st_size = 0;
268 etalon_stat.st_mode = 0;
269 fill_stat_struct (&etalon_stat, _i);
271 /* when */
272 actual_result =
273 vfs_parse_ls_lga (data->input_string, &test_stat, &filename, &linkname, &filepos);
275 /* then */
276 mctest_assert_int_eq (actual_result, data->expected_result);
278 mctest_assert_str_eq (filename, data->expected_filename);
279 mctest_assert_str_eq (linkname, data->expected_linkname);
281 mctest_assert_int_eq (etalon_stat.st_dev, test_stat.st_dev);
282 mctest_assert_int_eq (etalon_stat.st_ino, test_stat.st_ino);
283 mctest_assert_int_eq (etalon_stat.st_mode, test_stat.st_mode);
284 mctest_assert_int_eq (etalon_stat.st_uid, test_stat.st_uid);
285 mctest_assert_int_eq (etalon_stat.st_gid, test_stat.st_gid);
286 #ifdef HAVE_STRUCT_STAT_ST_RDEV
287 mctest_assert_int_eq (etalon_stat.st_rdev, test_stat.st_rdev);
288 #endif
289 mctest_assert_int_eq (etalon_stat.st_size, test_stat.st_size);
290 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
291 mctest_assert_int_eq (etalon_stat.st_blksize, test_stat.st_blksize);
292 #endif
293 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
294 mctest_assert_int_eq (etalon_stat.st_blocks, test_stat.st_blocks);
295 #endif
297 /* FIXME: these commented checks are related to time zone!
298 mctest_assert_int_eq (etalon_stat.st_atime, test_stat.st_atime);
299 mctest_assert_int_eq (etalon_stat.st_mtime, test_stat.st_mtime);
300 mctest_assert_int_eq (etalon_stat.st_ctime, test_stat.st_ctime);
303 /* *INDENT-OFF* */
304 END_PARAMETRIZED_TEST
305 /* *INDENT-ON* */
307 /* --------------------------------------------------------------------------------------------- */
309 /* @Test */
310 /* *INDENT-OFF* */
311 START_TEST (test_vfs_parse_ls_lga_reorder)
312 /* *INDENT-ON* */
314 /* given */
315 size_t filepos = 0;
316 struct vfs_s_entry *ent1, *ent2, *ent3;
318 vfs_parse_ls_lga_init ();
320 /* init ent1 */
321 ent1 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
322 vfs_parse_ls_lga
323 ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1", &ent1->ino->st,
324 &ent1->name, &ent1->ino->linkname, &filepos);
325 vfs_s_store_filename_leading_spaces (ent1, filepos);
326 vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent1);
329 /* init ent2 */
330 ent2 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
331 vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
332 &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos);
333 vfs_s_store_filename_leading_spaces (ent2, filepos);
334 vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent2);
336 /* init ent3 */
337 ent3 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
338 vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
339 &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos);
340 vfs_s_store_filename_leading_spaces (ent3, filepos);
341 vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent3);
343 /* when */
344 vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
346 /* then */
347 mctest_assert_str_eq (ent1->name, " build_root1");
348 mctest_assert_str_eq (ent2->name, " build_root2");
350 /* *INDENT-OFF* */
351 END_TEST
352 /* *INDENT-ON* */
354 /* --------------------------------------------------------------------------------------------- */
355 #define parce_one_line(ent_index, ls_output) {\
356 ent[ent_index] = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);\
357 if (! vfs_parse_ls_lga (ls_output,\
358 &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
360 fail ("An error occurred while parse ls output");\
361 return;\
363 vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
364 vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
368 /* @Test */
369 /* *INDENT-OFF* */
370 START_TEST (test_vfs_parse_ls_lga_unaligned)
371 /* *INDENT-ON* */
373 /* given */
374 size_t filepos = 0;
375 struct vfs_s_entry *ent[4];
377 vfs_parse_ls_lga_init ();
379 parce_one_line (0, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1");
380 parce_one_line (1, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2");
381 parce_one_line (2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..");
382 parce_one_line (3,
383 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root 0");
385 /* when */
386 vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
388 /* then */
389 mctest_assert_str_eq (ent[0]->name, "build_root1");
390 mctest_assert_str_eq (ent[0]->name, "build_root1");
391 mctest_assert_str_eq (ent[1]->name, " build_root2");
392 mctest_assert_str_eq (ent[3]->name, " build_root 0");
394 /* *INDENT-OFF* */
395 END_TEST
396 /* *INDENT-ON* */
398 /* --------------------------------------------------------------------------------------------- */
401 main (void)
403 int number_failed;
405 Suite *s = suite_create (TEST_SUITE_NAME);
406 TCase *tc_core = tcase_create ("Core");
407 SRunner *sr;
409 tcase_add_checked_fixture (tc_core, setup, teardown);
411 /* Add new tests here: *************** */
412 mctest_add_parameterized_test (tc_core, test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds);
413 tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
414 tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
415 /* *********************************** */
417 suite_add_tcase (s, tc_core);
418 sr = srunner_create (s);
419 srunner_set_log (sr, "vfs_parse_ls_lga.log");
420 srunner_run_all (sr, CK_ENV);
421 number_failed = srunner_ntests_failed (sr);
422 srunner_free (sr);
423 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
426 /* --------------------------------------------------------------------------------------------- */