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

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