root/tests/lib/vfs/path_manipulations.c

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

DEFINITIONS

This source file includes following definitions.
  1. init_test_classes
  2. setup
  3. teardown
  4. START_PARAMETRIZED_TEST
  5. START_PARAMETRIZED_TEST
  6. START_PARAMETRIZED_TEST
  7. START_PARAMETRIZED_TEST
  8. START_PARAMETRIZED_TEST
  9. main

   1 /* lib/vfs - test vfs_path_t manipulation functions
   2 
   3    Copyright (C) 2011-2019
   4    Free Software Foundation, Inc.
   5 
   6    Written by:
   7    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   8 
   9    This file is part of the Midnight Commander.
  10 
  11    The Midnight Commander is free software: you can redistribute it
  12    and/or modify it under the terms of the GNU General Public License as
  13    published by the Free Software Foundation, either version 3 of the License,
  14    or (at your option) any later version.
  15 
  16    The Midnight Commander is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19    GNU General Public License for more details.
  20 
  21    You should have received a copy of the GNU General Public License
  22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23  */
  24 
  25 #define TEST_SUITE_NAME "/lib/vfs"
  26 
  27 #include "tests/mctest.h"
  28 
  29 #ifdef HAVE_CHARSET
  30 #include "lib/charsets.h"
  31 #endif
  32 
  33 #include "lib/strutil.h"
  34 #include "lib/vfs/xdirentry.h"
  35 #include "lib/vfs/path.h"
  36 
  37 #include "src/vfs/local/local.c"
  38 
  39 
  40 static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  41 
  42 /* --------------------------------------------------------------------------------------------- */
  43 
  44 static void
  45 init_test_classes (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  46 {
  47     vfs_init_class (&vfs_test_ops1, "testfs1", VFS_NOLINKS | VFS_REMOTE, "test1");
  48     vfs_register_class (&vfs_test_ops1);
  49 
  50     vfs_init_class (&vfs_test_ops2, "testfs2", VFS_UNKNOWN, "test2");
  51     vfs_register_class (&vfs_test_ops2);
  52 
  53     vfs_init_class (&vfs_test_ops3, "testfs3", VFS_LOCAL, "test3");
  54     vfs_register_class (&vfs_test_ops3);
  55 }
  56 
  57 /* --------------------------------------------------------------------------------------------- */
  58 
  59 /* @Before */
  60 static void
  61 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  62 {
  63 
  64     str_init_strings (NULL);
  65 
  66     vfs_init ();
  67     init_localfs ();
  68     vfs_setup_work_dir ();
  69 
  70     init_test_classes ();
  71 
  72     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  73 #ifdef HAVE_CHARSET
  74     load_codepages_list ();
  75 #endif
  76 }
  77 
  78 /* --------------------------------------------------------------------------------------------- */
  79 
  80 /* @After */
  81 static void
  82 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84 #ifdef HAVE_CHARSET
  85     free_codepages_list ();
  86 #endif
  87 
  88     vfs_shut ();
  89     str_uninit_strings ();
  90 }
  91 
  92 /* --------------------------------------------------------------------------------------------- */
  93 
  94 /* @DataSource("test_vfs_path_tokens_count_ds") */
  95 /* *INDENT-OFF* */
  96 static const struct test_vfs_path_tokens_count_ds
  97 {
  98     const char *input_path;
  99     const vfs_path_flag_t input_flags;
 100     const size_t expected_token_count;
 101 } test_vfs_path_tokens_count_ds[] =
 102 {
 103     { /* 0. */
 104         "/",
 105         VPF_NONE,
 106         0
 107     },
 108     { /* 1. */
 109         "/path",
 110         VPF_NONE,
 111         1
 112     },
 113     { /* 2. */
 114         "/path1/path2/path3",
 115         VPF_NONE,
 116         3
 117     },
 118     { /* 3. */
 119         "test3://path1/path2/path3/path4",
 120         VPF_NO_CANON,
 121         4
 122     },
 123     { /* 4. */
 124         "path1/path2/path3",
 125         VPF_NO_CANON,
 126         3
 127     },
 128     { /* 5. */
 129         "/path1/path2/path3/",
 130         VPF_NONE,
 131         3
 132     },
 133     { /* 6. */
 134         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 135         VPF_NONE,
 136         5
 137     },
 138 #ifdef HAVE_CHARSET
 139     { /* 7. */
 140         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/"
 141         "test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
 142         VPF_NONE,
 143         11
 144     },
 145 #endif
 146 };
 147 /* *INDENT-ON* */
 148 
 149 /* @Test(dataSource = "test_vfs_path_tokens_count_ds") */
 150 /* *INDENT-OFF* */
 151 START_PARAMETRIZED_TEST (test_vfs_path_tokens_count, test_vfs_path_tokens_count_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 152 /* *INDENT-ON* */
 153 {
 154     /* given */
 155     size_t tokens_count;
 156     vfs_path_t *vpath;
 157 
 158     vpath = vfs_path_from_str_flags (data->input_path, data->input_flags);
 159 
 160     /* when */
 161     tokens_count = vfs_path_tokens_count (vpath);
 162 
 163     /* then */
 164     mctest_assert_int_eq (tokens_count, data->expected_token_count);
 165 
 166     vfs_path_free (vpath);
 167 }
 168 /* *INDENT-OFF* */
 169 END_PARAMETRIZED_TEST
 170 /* *INDENT-ON* */
 171 
 172 /* --------------------------------------------------------------------------------------------- */
 173 
 174 /* @DataSource("test_vfs_path_tokens_get_ds") */
 175 /* *INDENT-OFF* */
 176 static const struct test_vfs_path_tokens_get_ds
 177 {
 178     const char *input_path;
 179     const ssize_t input_start_position;
 180     const ssize_t input_length;
 181 
 182     const char *expected_path;
 183 } test_vfs_path_tokens_get_ds[] =
 184 {
 185     { /* 0. Invalid start position  */
 186         "/",
 187         2,
 188         1,
 189         NULL
 190     },
 191     { /* 1. Invalid negative position */
 192         "/path",
 193         -3,
 194         1,
 195         NULL
 196     },
 197     { /* 2. Count of tokens is zero. Count should be autocorrected */
 198         "/path",
 199         0,
 200         0,
 201         "path"
 202     },
 203     { /* 3. get 'path2/path3' by 1,2  */
 204         "/path1/path2/path3/path4",
 205         1,
 206         2,
 207         "path2/path3"
 208     },
 209     { /* 4. get 'path2/path3' by 1,2  from LOCAL VFS */
 210         "test3://path1/path2/path3/path4",
 211         1,
 212         2,
 213         "path2/path3"
 214     },
 215     { /* 5. get 'path2/path3' by 1,2  from non-LOCAL VFS */
 216         "test2://path1/path2/path3/path4",
 217         1,
 218         2,
 219         "test2://path2/path3"
 220     },
 221     { /* 6. get 'path2/path3' by 1,2  throught non-LOCAL VFS */
 222         "/path1/path2/test1://user:pass@some.host:12345/path3/path4",
 223         1,
 224         2,
 225         "path2/test1://user:pass@some.host:12345/path3"
 226     },
 227     { /* 7. get 'path2/path3' by 1,2  where path2 it's LOCAL VFS */
 228         "test3://path1/path2/test2://path3/path4",
 229         1,
 230         2,
 231         "path2/test2://path3"
 232     },
 233     { /* 8. get 'path2/path3' by 1,2  where path3 it's LOCAL VFS */
 234         "test2://path1/path2/test3://path3/path4",
 235         1,
 236         2,
 237         "test2://path2/test3://path3"
 238     },
 239     { /* 9. get 'path4' by -1,1  */
 240         "/path1/path2/path3/path4",
 241         -1,
 242         1,
 243         "path4"
 244     },
 245     { /* 10. get 'path2/path3/path4' by -3,0  */
 246         "/path1/path2/path3/path4",
 247         -3,
 248         0,
 249         "path2/path3/path4"
 250     },
 251 #ifdef HAVE_CHARSET
 252     { /* 11. get 'path2/path3' by 1,2  from LOCAL VFS with encoding */
 253         "test3://path1/path2/test3://#enc:KOI8-R/path3/path4",
 254         1,
 255         2,
 256         "path2/test3://#enc:KOI8-R/path3"
 257     },
 258     { /* 12. get 'path2/path3' by 1,2  with encoding */
 259         "#enc:KOI8-R/path1/path2/path3/path4",
 260         1,
 261         2,
 262         "#enc:KOI8-R/path2/path3"
 263     },
 264 #endif
 265 /* TODO: currently this test don't passed. Probably broken string URI parser
 266  { *//* 13. get 'path2/path3' by 1,2  from LOCAL VFS *//*
 267 
 268         "test3://path1/path2/test2://test3://path3/path4",
 269         1,
 270         2,
 271         "path2/path3"
 272     },
 273 */
 274 };
 275 /* *INDENT-ON* */
 276 
 277 /* @Test(dataSource = "test_vfs_path_tokens_get_ds") */
 278 /* *INDENT-OFF* */
 279 START_PARAMETRIZED_TEST (test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 280 /* *INDENT-ON* */
 281 {
 282     /* given */
 283     vfs_path_t *vpath;
 284     char *actual_path;
 285 
 286     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 287 
 288     /* when */
 289     actual_path = vfs_path_tokens_get (vpath, data->input_start_position, data->input_length);
 290 
 291     /* then */
 292     mctest_assert_str_eq (actual_path, data->expected_path);
 293 
 294     g_free (actual_path);
 295     vfs_path_free (vpath);
 296 }
 297 /* *INDENT-OFF* */
 298 END_PARAMETRIZED_TEST
 299 /* *INDENT-ON* */
 300 
 301 /* --------------------------------------------------------------------------------------------- */
 302 
 303 /* @DataSource("test_vfs_path_append_vpath_ds") */
 304 /* *INDENT-OFF* */
 305 static const struct test_vfs_path_append_vpath_ds
 306 {
 307     const char *input_path1;
 308     const char *input_path2;
 309     const int expected_element_count;
 310     const char *expected_path;
 311 } test_vfs_path_append_vpath_ds[] =
 312 {
 313     { /* 0. */
 314         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33",
 315         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 316         6,
 317         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
 318         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
 319     },
 320 #ifdef HAVE_CHARSET
 321     { /* 1. */
 322         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
 323         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 324         6,
 325         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
 326         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
 327     },
 328 #endif /* HAVE_CHARSET */
 329 };
 330 /* *INDENT-ON* */
 331 
 332 /* @Test(dataSource = "test_vfs_path_append_vpath_ds") */
 333 /* *INDENT-OFF* */
 334 START_PARAMETRIZED_TEST (test_vfs_path_append_vpath, test_vfs_path_append_vpath_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 335 /* *INDENT-ON* */
 336 {
 337     /* given */
 338     vfs_path_t *vpath1, *vpath2, *vpath3;
 339 
 340     vpath1 = vfs_path_from_str (data->input_path1);
 341     vpath2 = vfs_path_from_str (data->input_path2);
 342 
 343     /* when */
 344     vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
 345 
 346     /* then */
 347     mctest_assert_int_eq (vfs_path_elements_count (vpath3), data->expected_element_count);
 348     mctest_assert_str_eq (vfs_path_as_str (vpath3), data->expected_path);
 349 
 350     vfs_path_free (vpath1);
 351     vfs_path_free (vpath2);
 352     vfs_path_free (vpath3);
 353 }
 354 /* *INDENT-OFF* */
 355 END_PARAMETRIZED_TEST
 356 /* *INDENT-ON* */
 357 
 358 /* --------------------------------------------------------------------------------------------- */
 359 
 360 /* @DataSource("test_vfs_path_relative_ds") */
 361 /* *INDENT-OFF* */
 362 static const struct test_vfs_path_relative_ds
 363 {
 364     const char *input_path;
 365     const char *expected_path;
 366     const char *expected_last_path_in_element;
 367 } test_vfs_path_relative_ds[] =
 368 {
 369     { /* 0. */
 370         "../bla-bla",
 371         "../bla-bla",
 372         "../bla-bla"
 373     },
 374     { /* 1. */
 375         "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 376         "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 377         "bla-bla/some/path/"
 378     },
 379 };
 380 /* *INDENT-ON* */
 381 
 382 /* @Test(dataSource = "test_vfs_path_relative_ds") */
 383 /* *INDENT-OFF* */
 384 START_PARAMETRIZED_TEST (test_vfs_path_relative, test_vfs_path_relative_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 385 /* *INDENT-ON* */
 386 {
 387     /* given */
 388     vfs_path_t *vpath;
 389 
 390     /* when */
 391 
 392     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 393 
 394     /* then */
 395     mctest_assert_int_eq (vpath->relative, TRUE);
 396     mctest_assert_str_eq (vfs_path_get_last_path_str (vpath), data->expected_last_path_in_element);
 397     mctest_assert_str_eq (vfs_path_as_str (vpath), data->expected_path);
 398 
 399     vfs_path_free (vpath);
 400 }
 401 /* *INDENT-OFF* */
 402 END_PARAMETRIZED_TEST
 403 /* *INDENT-ON* */
 404 
 405 /* --------------------------------------------------------------------------------------------- */
 406 
 407 /* @Test(dataSource = "test_vfs_path_relative_ds") */
 408 /* *INDENT-OFF* */
 409 START_PARAMETRIZED_TEST (test_vfs_path_relative_clone, test_vfs_path_relative_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 410 /* *INDENT-ON* */
 411 {
 412     /* given */
 413     vfs_path_t *vpath, *cloned_vpath;
 414 
 415     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 416 
 417     /* when */
 418 
 419     cloned_vpath = vfs_path_clone (vpath);
 420 
 421     /* then */
 422     mctest_assert_int_eq (cloned_vpath->relative, TRUE);
 423     mctest_assert_str_eq (vfs_path_get_last_path_str (cloned_vpath),
 424                           data->expected_last_path_in_element);
 425     mctest_assert_str_eq (vfs_path_as_str (cloned_vpath), data->expected_path);
 426 
 427     vfs_path_free (vpath);
 428     vfs_path_free (cloned_vpath);
 429 }
 430 /* *INDENT-OFF* */
 431 END_PARAMETRIZED_TEST
 432 /* *INDENT-ON* */
 433 
 434 /* --------------------------------------------------------------------------------------------- */
 435 
 436 int
 437 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 438 {
 439     int number_failed;
 440 
 441     Suite *s = suite_create (TEST_SUITE_NAME);
 442     TCase *tc_core = tcase_create ("Core");
 443     SRunner *sr;
 444 
 445     tcase_add_checked_fixture (tc_core, setup, teardown);
 446 
 447     /* Add new tests here: *************** */
 448     mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_count,
 449                                    test_vfs_path_tokens_count_ds);
 450     mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds);
 451     mctest_add_parameterized_test (tc_core, test_vfs_path_append_vpath,
 452                                    test_vfs_path_append_vpath_ds);
 453     mctest_add_parameterized_test (tc_core, test_vfs_path_relative, test_vfs_path_relative_ds);
 454     mctest_add_parameterized_test (tc_core, test_vfs_path_relative_clone,
 455                                    test_vfs_path_relative_ds);
 456     /* *********************************** */
 457 
 458     suite_add_tcase (s, tc_core);
 459     sr = srunner_create (s);
 460     srunner_set_log (sr, "path_manipulations.log");
 461     srunner_run_all (sr, CK_ENV);
 462     number_failed = srunner_ntests_failed (sr);
 463     srunner_free (sr);
 464     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 465 }
 466 
 467 /* --------------------------------------------------------------------------------------------- */

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