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

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