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-2020
   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", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  48     vfs_register_class (&vfs_test_ops1);
  49 
  50     vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_UNKNOWN, "test2");
  51     vfs_register_class (&vfs_test_ops2);
  52 
  53     vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_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     mc_global.timer = mc_timer_new ();
  64     str_init_strings (NULL);
  65 
  66     vfs_init ();
  67     vfs_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     mc_timer_destroy (mc_global.timer);
  91 }
  92 
  93 /* --------------------------------------------------------------------------------------------- */
  94 
  95 /* @DataSource("test_vfs_path_tokens_count_ds") */
  96 /* *INDENT-OFF* */
  97 static const struct test_vfs_path_tokens_count_ds
  98 {
  99     const char *input_path;
 100     const vfs_path_flag_t input_flags;
 101     const size_t expected_token_count;
 102 } test_vfs_path_tokens_count_ds[] =
 103 {
 104     { /* 0. */
 105         "/",
 106         VPF_NONE,
 107         0
 108     },
 109     { /* 1. */
 110         "/path",
 111         VPF_NONE,
 112         1
 113     },
 114     { /* 2. */
 115         "/path1/path2/path3",
 116         VPF_NONE,
 117         3
 118     },
 119     { /* 3. */
 120         "test3://path1/path2/path3/path4",
 121         VPF_NO_CANON,
 122         4
 123     },
 124     { /* 4. */
 125         "path1/path2/path3",
 126         VPF_NO_CANON,
 127         3
 128     },
 129     { /* 5. */
 130         "/path1/path2/path3/",
 131         VPF_NONE,
 132         3
 133     },
 134     { /* 6. */
 135         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 136         VPF_NONE,
 137         5
 138     },
 139 #ifdef HAVE_CHARSET
 140     { /* 7. */
 141         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/"
 142         "test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
 143         VPF_NONE,
 144         11
 145     },
 146 #endif
 147 };
 148 /* *INDENT-ON* */
 149 
 150 /* @Test(dataSource = "test_vfs_path_tokens_count_ds") */
 151 /* *INDENT-OFF* */
 152 START_PARAMETRIZED_TEST (test_vfs_path_tokens_count, test_vfs_path_tokens_count_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 153 /* *INDENT-ON* */
 154 {
 155     /* given */
 156     size_t tokens_count;
 157     vfs_path_t *vpath;
 158 
 159     vpath = vfs_path_from_str_flags (data->input_path, data->input_flags);
 160 
 161     /* when */
 162     tokens_count = vfs_path_tokens_count (vpath);
 163 
 164     /* then */
 165     mctest_assert_int_eq (tokens_count, data->expected_token_count);
 166 
 167     vfs_path_free (vpath);
 168 }
 169 /* *INDENT-OFF* */
 170 END_PARAMETRIZED_TEST
 171 /* *INDENT-ON* */
 172 
 173 /* --------------------------------------------------------------------------------------------- */
 174 
 175 /* @DataSource("test_vfs_path_tokens_get_ds") */
 176 /* *INDENT-OFF* */
 177 static const struct test_vfs_path_tokens_get_ds
 178 {
 179     const char *input_path;
 180     const ssize_t input_start_position;
 181     const ssize_t input_length;
 182 
 183     const char *expected_path;
 184 } test_vfs_path_tokens_get_ds[] =
 185 {
 186     { /* 0. Invalid start position  */
 187         "/",
 188         2,
 189         1,
 190         NULL
 191     },
 192     { /* 1. Invalid negative position */
 193         "/path",
 194         -3,
 195         1,
 196         NULL
 197     },
 198     { /* 2. Count of tokens is zero. Count should be autocorrected */
 199         "/path",
 200         0,
 201         0,
 202         "path"
 203     },
 204     { /* 3. get 'path2/path3' by 1,2  */
 205         "/path1/path2/path3/path4",
 206         1,
 207         2,
 208         "path2/path3"
 209     },
 210     { /* 4. get 'path2/path3' by 1,2  from LOCAL VFS */
 211         "test3://path1/path2/path3/path4",
 212         1,
 213         2,
 214         "path2/path3"
 215     },
 216     { /* 5. get 'path2/path3' by 1,2  from non-LOCAL VFS */
 217         "test2://path1/path2/path3/path4",
 218         1,
 219         2,
 220         "test2://path2/path3"
 221     },
 222     { /* 6. get 'path2/path3' by 1,2  throught non-LOCAL VFS */
 223         "/path1/path2/test1://user:pass@some.host:12345/path3/path4",
 224         1,
 225         2,
 226         "path2/test1://user:pass@some.host:12345/path3"
 227     },
 228     { /* 7. get 'path2/path3' by 1,2  where path2 it's LOCAL VFS */
 229         "test3://path1/path2/test2://path3/path4",
 230         1,
 231         2,
 232         "path2/test2://path3"
 233     },
 234     { /* 8. get 'path2/path3' by 1,2  where path3 it's LOCAL VFS */
 235         "test2://path1/path2/test3://path3/path4",
 236         1,
 237         2,
 238         "test2://path2/test3://path3"
 239     },
 240     { /* 9. get 'path4' by -1,1  */
 241         "/path1/path2/path3/path4",
 242         -1,
 243         1,
 244         "path4"
 245     },
 246     { /* 10. get 'path2/path3/path4' by -3,0  */
 247         "/path1/path2/path3/path4",
 248         -3,
 249         0,
 250         "path2/path3/path4"
 251     },
 252 #ifdef HAVE_CHARSET
 253     { /* 11. get 'path2/path3' by 1,2  from LOCAL VFS with encoding */
 254         "test3://path1/path2/test3://#enc:KOI8-R/path3/path4",
 255         1,
 256         2,
 257         "path2/test3://#enc:KOI8-R/path3"
 258     },
 259     { /* 12. get 'path2/path3' by 1,2  with encoding */
 260         "#enc:KOI8-R/path1/path2/path3/path4",
 261         1,
 262         2,
 263         "#enc:KOI8-R/path2/path3"
 264     },
 265 #endif
 266 /* TODO: currently this test don't passed. Probably broken string URI parser
 267  { *//* 13. get 'path2/path3' by 1,2  from LOCAL VFS *//*
 268 
 269         "test3://path1/path2/test2://test3://path3/path4",
 270         1,
 271         2,
 272         "path2/path3"
 273     },
 274 */
 275 };
 276 /* *INDENT-ON* */
 277 
 278 /* @Test(dataSource = "test_vfs_path_tokens_get_ds") */
 279 /* *INDENT-OFF* */
 280 START_PARAMETRIZED_TEST (test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 281 /* *INDENT-ON* */
 282 {
 283     /* given */
 284     vfs_path_t *vpath;
 285     char *actual_path;
 286 
 287     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 288 
 289     /* when */
 290     actual_path = vfs_path_tokens_get (vpath, data->input_start_position, data->input_length);
 291 
 292     /* then */
 293     mctest_assert_str_eq (actual_path, data->expected_path);
 294 
 295     g_free (actual_path);
 296     vfs_path_free (vpath);
 297 }
 298 /* *INDENT-OFF* */
 299 END_PARAMETRIZED_TEST
 300 /* *INDENT-ON* */
 301 
 302 /* --------------------------------------------------------------------------------------------- */
 303 
 304 /* @DataSource("test_vfs_path_append_vpath_ds") */
 305 /* *INDENT-OFF* */
 306 static const struct test_vfs_path_append_vpath_ds
 307 {
 308     const char *input_path1;
 309     const char *input_path2;
 310     const int expected_element_count;
 311     const char *expected_path;
 312 } test_vfs_path_append_vpath_ds[] =
 313 {
 314     { /* 0. */
 315         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33",
 316         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 317         6,
 318         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
 319         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
 320     },
 321 #ifdef HAVE_CHARSET
 322     { /* 1. */
 323         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
 324         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 325         6,
 326         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
 327         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
 328     },
 329 #endif /* HAVE_CHARSET */
 330 };
 331 /* *INDENT-ON* */
 332 
 333 /* @Test(dataSource = "test_vfs_path_append_vpath_ds") */
 334 /* *INDENT-OFF* */
 335 START_PARAMETRIZED_TEST (test_vfs_path_append_vpath, test_vfs_path_append_vpath_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 336 /* *INDENT-ON* */
 337 {
 338     /* given */
 339     vfs_path_t *vpath1, *vpath2, *vpath3;
 340 
 341     vpath1 = vfs_path_from_str (data->input_path1);
 342     vpath2 = vfs_path_from_str (data->input_path2);
 343 
 344     /* when */
 345     vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
 346 
 347     /* then */
 348     mctest_assert_int_eq (vfs_path_elements_count (vpath3), data->expected_element_count);
 349     mctest_assert_str_eq (vfs_path_as_str (vpath3), data->expected_path);
 350 
 351     vfs_path_free (vpath1);
 352     vfs_path_free (vpath2);
 353     vfs_path_free (vpath3);
 354 }
 355 /* *INDENT-OFF* */
 356 END_PARAMETRIZED_TEST
 357 /* *INDENT-ON* */
 358 
 359 /* --------------------------------------------------------------------------------------------- */
 360 
 361 /* @DataSource("test_vfs_path_relative_ds") */
 362 /* *INDENT-OFF* */
 363 static const struct test_vfs_path_relative_ds
 364 {
 365     const char *input_path;
 366     const char *expected_path;
 367     const char *expected_last_path_in_element;
 368 } test_vfs_path_relative_ds[] =
 369 {
 370     { /* 0. */
 371         "../bla-bla",
 372         "../bla-bla",
 373         "../bla-bla"
 374     },
 375     { /* 1. */
 376         "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 377         "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 378         "bla-bla/some/path/"
 379     },
 380 };
 381 /* *INDENT-ON* */
 382 
 383 /* @Test(dataSource = "test_vfs_path_relative_ds") */
 384 /* *INDENT-OFF* */
 385 START_PARAMETRIZED_TEST (test_vfs_path_relative, test_vfs_path_relative_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 386 /* *INDENT-ON* */
 387 {
 388     /* given */
 389     vfs_path_t *vpath;
 390 
 391     /* when */
 392 
 393     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 394 
 395     /* then */
 396     mctest_assert_int_eq (vpath->relative, TRUE);
 397     mctest_assert_str_eq (vfs_path_get_last_path_str (vpath), data->expected_last_path_in_element);
 398     mctest_assert_str_eq (vfs_path_as_str (vpath), data->expected_path);
 399 
 400     vfs_path_free (vpath);
 401 }
 402 /* *INDENT-OFF* */
 403 END_PARAMETRIZED_TEST
 404 /* *INDENT-ON* */
 405 
 406 /* --------------------------------------------------------------------------------------------- */
 407 
 408 /* @Test(dataSource = "test_vfs_path_relative_ds") */
 409 /* *INDENT-OFF* */
 410 START_PARAMETRIZED_TEST (test_vfs_path_relative_clone, test_vfs_path_relative_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 411 /* *INDENT-ON* */
 412 {
 413     /* given */
 414     vfs_path_t *vpath, *cloned_vpath;
 415 
 416     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 417 
 418     /* when */
 419 
 420     cloned_vpath = vfs_path_clone (vpath);
 421 
 422     /* then */
 423     mctest_assert_int_eq (cloned_vpath->relative, TRUE);
 424     mctest_assert_str_eq (vfs_path_get_last_path_str (cloned_vpath),
 425                           data->expected_last_path_in_element);
 426     mctest_assert_str_eq (vfs_path_as_str (cloned_vpath), data->expected_path);
 427 
 428     vfs_path_free (vpath);
 429     vfs_path_free (cloned_vpath);
 430 }
 431 /* *INDENT-OFF* */
 432 END_PARAMETRIZED_TEST
 433 /* *INDENT-ON* */
 434 
 435 /* --------------------------------------------------------------------------------------------- */
 436 
 437 int
 438 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 439 {
 440     int number_failed;
 441 
 442     Suite *s = suite_create (TEST_SUITE_NAME);
 443     TCase *tc_core = tcase_create ("Core");
 444     SRunner *sr;
 445 
 446     tcase_add_checked_fixture (tc_core, setup, teardown);
 447 
 448     /* Add new tests here: *************** */
 449     mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_count,
 450                                    test_vfs_path_tokens_count_ds);
 451     mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds);
 452     mctest_add_parameterized_test (tc_core, test_vfs_path_append_vpath,
 453                                    test_vfs_path_append_vpath_ds);
 454     mctest_add_parameterized_test (tc_core, test_vfs_path_relative, test_vfs_path_relative_ds);
 455     mctest_add_parameterized_test (tc_core, test_vfs_path_relative_clone,
 456                                    test_vfs_path_relative_ds);
 457     /* *********************************** */
 458 
 459     suite_add_tcase (s, tc_core);
 460     sr = srunner_create (s);
 461     srunner_set_log (sr, "path_manipulations.log");
 462     srunner_run_all (sr, CK_ENV);
 463     number_failed = srunner_ntests_failed (sr);
 464     srunner_free (sr);
 465     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 466 }
 467 
 468 /* --------------------------------------------------------------------------------------------- */

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