root/tests/lib/vfs/vfs_parse_ls_lga.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. message
  2. teardown
  3. message
  4. fill_stat_struct
  5. START_PARAMETRIZED_TEST
  6. START_TEST
  7. START_TEST
  8. main

   1 /*
   2    lib/vfs - test vfs_parse_ls_lga() functionality
   3 
   4    Copyright (C) 2011-2020
   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);
     /* [previous][next][first][last][top][bottom][index][help]  */
  48 /* *INDENT-ON* */
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 /* @Before */
  53 static void
  54 setup (void)
  55 {
  56     static struct stat initstat;
  57 
  58     mc_global.timer = mc_timer_new ();
  59     str_init_strings (NULL);
  60 
  61     vfs_init ();
  62     vfs_init_localfs ();
  63     vfs_setup_work_dir ();
  64 
  65     vfs_init_subclass (&test_subclass1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  66     vfs_register_class (vfs_test_ops1);
  67 
  68     vfs_test_super = g_new0 (struct vfs_s_super, 1);
  69     vfs_test_super->me = vfs_test_ops1;
  70 
  71     vfs_root_inode = vfs_s_new_inode (vfs_test_ops1, vfs_test_super, &initstat);
  72     vfs_root_entry = vfs_s_new_entry (vfs_test_ops1, "/", vfs_root_inode);
  73 }
  74 
  75 /* --------------------------------------------------------------------------------------------- */
  76 
  77 /* @After */
  78 static void
  79 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  80 {
  81     vfs_s_free_entry (vfs_test_ops1, vfs_root_entry);
  82     vfs_shut ();
  83     str_uninit_strings ();
  84     mc_timer_destroy (mc_global.timer);
  85 }
  86 
  87 /* --------------------------------------------------------------------------------------------- */
  88 
  89 /* @Mock */
  90 void
  91 message (int flags, const char *title, const char *text, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     char *p;
  94     va_list ap;
  95 
  96     (void) flags;
  97     (void) title;
  98 
  99     va_start (ap, text);
 100     p = g_strdup_vprintf (text, ap);
 101     va_end (ap);
 102     printf ("message(): %s\n", p);
 103     g_free (p);
 104 }
 105 
 106 /* --------------------------------------------------------------------------------------------- */
 107 
 108 static void
 109 fill_stat_struct (struct stat *etalon_stat, int iterator)
     /* [previous][next][first][last][top][bottom][index][help]  */
 110 {
 111 
 112 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 113     etalon_stat->st_atim.tv_nsec = etalon_stat->st_mtim.tv_nsec = etalon_stat->st_ctim.tv_nsec = 0;
 114 #endif
 115 
 116     switch (iterator)
 117     {
 118     case 0:
 119         etalon_stat->st_dev = 0;
 120         etalon_stat->st_ino = 0;
 121         etalon_stat->st_mode = 0x41fd;
 122         etalon_stat->st_nlink = 10;
 123         etalon_stat->st_uid = 500;
 124         etalon_stat->st_gid = 500;
 125 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 126         etalon_stat->st_rdev = 0;
 127 #endif
 128         etalon_stat->st_size = 4096;
 129 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 130         etalon_stat->st_blksize = 512;
 131 #endif
 132 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 133         etalon_stat->st_blocks = 8;
 134 #endif
 135         etalon_stat->st_atime = 1308838140;
 136         etalon_stat->st_mtime = 1308838140;
 137         etalon_stat->st_ctime = 1308838140;
 138         break;
 139     case 1:
 140         etalon_stat->st_dev = 0;
 141         etalon_stat->st_ino = 0;
 142         etalon_stat->st_mode = 0xa1ff;
 143         etalon_stat->st_nlink = 10;
 144         etalon_stat->st_uid = 500;
 145         etalon_stat->st_gid = 500;
 146 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 147         etalon_stat->st_rdev = 0;
 148 #endif
 149         etalon_stat->st_size = 11;
 150 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 151         etalon_stat->st_blksize = 512;
 152 #endif
 153 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 154         etalon_stat->st_blocks = 1;
 155 #endif
 156         etalon_stat->st_atime = 1268431200;
 157         etalon_stat->st_mtime = 1268431200;
 158         etalon_stat->st_ctime = 1268431200;
 159         break;
 160     case 2:
 161         etalon_stat->st_dev = 0;
 162         etalon_stat->st_ino = 0;
 163         etalon_stat->st_mode = 0x41fd;
 164         etalon_stat->st_nlink = 10;
 165         etalon_stat->st_uid = 500;
 166         etalon_stat->st_gid = 500;
 167 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 168         etalon_stat->st_rdev = 0;
 169 #endif
 170         etalon_stat->st_size = 4096;
 171 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 172         etalon_stat->st_blksize = 512;
 173 #endif
 174 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 175         etalon_stat->st_blocks = 8;
 176 #endif
 177         etalon_stat->st_atime = 1308838140;
 178         etalon_stat->st_mtime = 1308838140;
 179         etalon_stat->st_ctime = 1308838140;
 180         break;
 181     case 3:
 182         etalon_stat->st_dev = 0;
 183         etalon_stat->st_ino = 0;
 184         etalon_stat->st_mode = 0x41fd;
 185         etalon_stat->st_nlink = 10;
 186         etalon_stat->st_uid = 500;
 187         etalon_stat->st_gid = 500;
 188 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 189         etalon_stat->st_rdev = 0;
 190 #endif
 191         etalon_stat->st_size = 4096;
 192 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 193         etalon_stat->st_blksize = 512;
 194 #endif
 195 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 196         etalon_stat->st_blocks = 8;
 197 #endif
 198         etalon_stat->st_atime = 1308838140;
 199         etalon_stat->st_mtime = 1308838140;
 200         etalon_stat->st_ctime = 1308838140;
 201         break;
 202     default:
 203         break;
 204     }
 205 }
 206 
 207 /* --------------------------------------------------------------------------------------------- */
 208 
 209 /* @DataSource("test_vfs_parse_ls_lga_ds") */
 210 /* *INDENT-OFF* */
 211 static const struct test_vfs_parse_ls_lga_ds
 212 {
 213     const char *input_string;
 214     int expected_result;
 215     const char *expected_filename;
 216     const char *expected_linkname;
 217     const size_t expected_filepos;
 218 } test_vfs_parse_ls_lga_ds[] =
 219 {
 220     { /* 0. */
 221         "drwxrwxr-x   10 500      500          4096 Jun 23 17:09 build_root",
 222         1,
 223         "build_root",
 224         NULL,
 225         0
 226     },
 227     { /* 1. */
 228         "lrwxrwxrwx    1 500      500            11 Mar 13  2010 COPYING -> doc/COPYING",
 229         1,
 230         "COPYING",
 231         "doc/COPYING",
 232         0
 233     },
 234     { /* 2. */
 235         "drwxrwxr-x   10 500      500          4096 Jun 23 17:09 ..",
 236         1,
 237         "..",
 238         NULL,
 239         0
 240     },
 241     { /* 3. */
 242         "drwxrwxr-x   10 500      500          4096 Jun 23 17:09   build_root",
 243         1,
 244         "build_root",
 245         NULL,
 246         0
 247     },
 248 };
 249 /* *INDENT-ON* */
 250 
 251 /* @Test(dataSource = "test_vfs_parse_ls_lga_ds") */
 252 /* *INDENT-OFF* */
 253 START_PARAMETRIZED_TEST (test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 254 /* *INDENT-ON* */
 255 
 256 {
 257     /* given */
 258     size_t filepos = 0;
 259     struct stat etalon_stat;
 260     static struct stat test_stat;
 261     char *filename = NULL;
 262     char *linkname = NULL;
 263     gboolean actual_result;
 264 
 265     vfs_parse_ls_lga_init ();
 266 
 267 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 268     etalon_stat.st_blocks = 0;
 269 #endif
 270     etalon_stat.st_size = 0;
 271     etalon_stat.st_mode = 0;
 272     fill_stat_struct (&etalon_stat, _i);
 273 
 274     /* when */
 275     actual_result =
 276         vfs_parse_ls_lga (data->input_string, &test_stat, &filename, &linkname, &filepos);
 277 
 278     /* then */
 279     mctest_assert_int_eq (actual_result, data->expected_result);
 280 
 281     mctest_assert_str_eq (filename, data->expected_filename);
 282     mctest_assert_str_eq (linkname, data->expected_linkname);
 283 
 284     mctest_assert_int_eq (etalon_stat.st_dev, test_stat.st_dev);
 285     mctest_assert_int_eq (etalon_stat.st_ino, test_stat.st_ino);
 286     mctest_assert_int_eq (etalon_stat.st_mode, test_stat.st_mode);
 287     mctest_assert_int_eq (etalon_stat.st_uid, test_stat.st_uid);
 288     mctest_assert_int_eq (etalon_stat.st_gid, test_stat.st_gid);
 289 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 290     mctest_assert_int_eq (etalon_stat.st_rdev, test_stat.st_rdev);
 291 #endif
 292     mctest_assert_int_eq (etalon_stat.st_size, test_stat.st_size);
 293 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 294     mctest_assert_int_eq (etalon_stat.st_blksize, test_stat.st_blksize);
 295 #endif
 296 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 297     mctest_assert_int_eq (etalon_stat.st_blocks, test_stat.st_blocks);
 298 #endif
 299 
 300     /* FIXME: these commented checks are related to time zone!
 301        mctest_assert_int_eq (etalon_stat.st_atime, test_stat.st_atime);
 302        mctest_assert_int_eq (etalon_stat.st_mtime, test_stat.st_mtime);
 303        mctest_assert_int_eq (etalon_stat.st_ctime, test_stat.st_ctime);
 304      */
 305 
 306 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 307     mctest_assert_int_eq (0, test_stat.st_atim.tv_nsec);
 308     mctest_assert_int_eq (0, test_stat.st_mtim.tv_nsec);
 309     mctest_assert_int_eq (0, test_stat.st_ctim.tv_nsec);
 310 #endif
 311 
 312 }
 313 /* *INDENT-OFF* */
 314 END_PARAMETRIZED_TEST
 315 /* *INDENT-ON* */
 316 
 317 /* --------------------------------------------------------------------------------------------- */
 318 
 319 /* @Test */
 320 /* *INDENT-OFF* */
 321 START_TEST (test_vfs_parse_ls_lga_reorder)
     /* [previous][next][first][last][top][bottom][index][help]  */
 322 /* *INDENT-ON* */
 323 {
 324     /* given */
 325     size_t filepos = 0;
 326     struct vfs_s_entry *ent1, *ent2, *ent3;
 327 
 328     vfs_parse_ls_lga_init ();
 329 
 330     /* init ent1 */
 331     ent1 = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);
 332     vfs_parse_ls_lga
 333         ("drwxrwxr-x   10 500      500          4096 Jun 23 17:09      build_root1", &ent1->ino->st,
 334          &ent1->name, &ent1->ino->linkname, &filepos);
 335     vfs_s_store_filename_leading_spaces (ent1, filepos);
 336     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent1);
 337 
 338 
 339     /* init ent2 */
 340     ent2 = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);
 341     vfs_parse_ls_lga ("drwxrwxr-x   10 500      500          4096 Jun 23 17:09    build_root2",
 342                       &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos);
 343     vfs_s_store_filename_leading_spaces (ent2, filepos);
 344     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent2);
 345 
 346     /* init ent3 */
 347     ent3 = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);
 348     vfs_parse_ls_lga ("drwxrwxr-x   10 500      500          4096 Jun 23 17:09 ..",
 349                       &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos);
 350     vfs_s_store_filename_leading_spaces (ent3, filepos);
 351     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent3);
 352 
 353     /* when */
 354     vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
 355 
 356     /* then */
 357     mctest_assert_str_eq (ent1->name, "     build_root1");
 358     mctest_assert_str_eq (ent2->name, "   build_root2");
 359 }
 360 /* *INDENT-OFF* */
 361 END_TEST
 362 /* *INDENT-ON* */
 363 
 364 /* --------------------------------------------------------------------------------------------- */
 365 #define parce_one_line(ent_index, ls_output) {\
 366     ent[ent_index] = vfs_s_generate_entry (vfs_test_ops1, NULL, vfs_root_inode, 0);\
 367     if (! vfs_parse_ls_lga (ls_output,\
 368     &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
 369     {\
 370         fail ("An error occurred while parse ls output");\
 371         return;\
 372     }\
 373     vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
 374     vfs_s_insert_entry (vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
 375     \
 376 }
 377 
 378 /* @Test */
 379 /* *INDENT-OFF* */
 380 START_TEST (test_vfs_parse_ls_lga_unaligned)
     /* [previous][next][first][last][top][bottom][index][help]  */
 381 /* *INDENT-ON* */
 382 {
 383     /* given */
 384     size_t filepos = 0;
 385     struct vfs_s_entry *ent[4];
 386 
 387     vfs_parse_ls_lga_init ();
 388 
 389     parce_one_line (0, "drwxrwxr-x   10 500      500          4096 Jun 23 17:09  build_root1");
 390     parce_one_line (1, "drwxrwxr-x   10 500     500         4096 Jun 23 17:09     build_root2");
 391     parce_one_line (2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09  ..");
 392     parce_one_line (3,
 393                     "drwxrwxr-x      10   500        500             4096   Jun   23   17:09   build_root 0");
 394 
 395     /* when */
 396     vfs_s_normalize_filename_leading_spaces (vfs_root_inode, vfs_parse_ls_lga_get_final_spaces ());
 397 
 398     /* then */
 399     mctest_assert_str_eq (ent[0]->name, "build_root1");
 400     mctest_assert_str_eq (ent[0]->name, "build_root1");
 401     mctest_assert_str_eq (ent[1]->name, "   build_root2");
 402     mctest_assert_str_eq (ent[3]->name, " build_root 0");
 403 }
 404 /* *INDENT-OFF* */
 405 END_TEST
 406 /* *INDENT-ON* */
 407 
 408 /* --------------------------------------------------------------------------------------------- */
 409 
 410 int
 411 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 412 {
 413     int number_failed;
 414 
 415     Suite *s = suite_create (TEST_SUITE_NAME);
 416     TCase *tc_core = tcase_create ("Core");
 417     SRunner *sr;
 418 
 419     tcase_add_checked_fixture (tc_core, setup, teardown);
 420 
 421     /* Add new tests here: *************** */
 422     mctest_add_parameterized_test (tc_core, test_vfs_parse_ls_lga, test_vfs_parse_ls_lga_ds);
 423     tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
 424     tcase_add_test (tc_core, test_vfs_parse_ls_lga_unaligned);
 425     /* *********************************** */
 426 
 427     suite_add_tcase (s, tc_core);
 428     sr = srunner_create (s);
 429     srunner_set_log (sr, "vfs_parse_ls_lga.log");
 430     srunner_run_all (sr, CK_ENV);
 431     number_failed = srunner_ntests_failed (sr);
 432     srunner_free (sr);
 433     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 434 }
 435 
 436 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */