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-2025
   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 <https://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 static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  40 
  41 /* --------------------------------------------------------------------------------------------- */
  42 
  43 static void
  44 init_test_classes (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  45 {
  46     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  47     vfs_register_class (&vfs_test_ops1);
  48 
  49     vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_UNKNOWN, "test2");
  50     vfs_register_class (&vfs_test_ops2);
  51 
  52     vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_LOCAL, "test3");
  53     vfs_register_class (&vfs_test_ops3);
  54 }
  55 
  56 /* --------------------------------------------------------------------------------------------- */
  57 
  58 /* @Before */
  59 static void
  60 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  61 {
  62     str_init_strings ("UTF-8");
  63 
  64     vfs_init ();
  65     vfs_init_localfs ();
  66     vfs_setup_work_dir ();
  67 
  68     init_test_classes ();
  69 
  70 #ifdef HAVE_CHARSET
  71     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  72     load_codepages_list ();
  73 #endif
  74 }
  75 
  76 /* --------------------------------------------------------------------------------------------- */
  77 
  78 /* @After */
  79 static void
  80 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  81 {
  82 #ifdef HAVE_CHARSET
  83     free_codepages_list ();
  84 #endif
  85 
  86     vfs_shut ();
  87     str_uninit_strings ();
  88 }
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 
  92 /* @DataSource("test_vfs_path_tokens_count_ds") */
  93 static const struct test_vfs_path_tokens_count_ds
  94 {
  95     const char *input_path;
  96     const vfs_path_flag_t input_flags;
  97     const size_t expected_token_count;
  98 } test_vfs_path_tokens_count_ds[] = {
  99     {
 100         // 0.
 101         "/",
 102         VPF_NONE,
 103         0,
 104     },
 105     {
 106         // 1.
 107         "/path",
 108         VPF_NONE,
 109         1,
 110     },
 111     {
 112         // 2.
 113         "/path1/path2/path3",
 114         VPF_NONE,
 115         3,
 116     },
 117     {
 118         // 3.
 119         "test3://path1/path2/path3/path4",
 120         VPF_NO_CANON,
 121         4,
 122     },
 123     {
 124         // 4.
 125         "path1/path2/path3",
 126         VPF_NO_CANON,
 127         3,
 128     },
 129     {
 130         // 5.
 131         "/path1/path2/path3/",
 132         VPF_NONE,
 133         3,
 134     },
 135     {
 136         // 6.
 137         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 138         VPF_NONE,
 139         5,
 140     },
 141 #ifdef HAVE_CHARSET
 142     {
 143         // 7.
 144         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/"
 145         "test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33",
 146         VPF_NONE,
 147         11,
 148     },
 149 #endif
 150 };
 151 
 152 /* @Test(dataSource = "test_vfs_path_tokens_count_ds") */
 153 START_PARAMETRIZED_TEST (test_vfs_path_tokens_count, test_vfs_path_tokens_count_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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     ck_assert_int_eq (tokens_count, data->expected_token_count);
 166 
 167     vfs_path_free (vpath, TRUE);
 168 }
 169 END_PARAMETRIZED_TEST
 170 
 171 /* --------------------------------------------------------------------------------------------- */
 172 
 173 /* @DataSource("test_vfs_path_tokens_get_ds") */
 174 static const struct test_vfs_path_tokens_get_ds
 175 {
 176     const char *input_path;
 177     const ssize_t input_start_position;
 178     const ssize_t input_length;
 179 
 180     const char *expected_path;
 181 } test_vfs_path_tokens_get_ds[] = {
 182     {
 183         // 0. Invalid start position
 184         "/",
 185         2,
 186         1,
 187         NULL,
 188     },
 189     {
 190         // 1. Invalid negative position
 191         "/path",
 192         -3,
 193         1,
 194         NULL,
 195     },
 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     {
 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     {
 212         "test3://path1/path2/path3/path4",
 213         1,
 214         2,
 215         "path2/path3",
 216     },
 217     // 5. get 'path2/path3' by 1,2  from non-LOCAL VFS
 218     {
 219         "test2://path1/path2/path3/path4",
 220         1,
 221         2,
 222         "test2://path2/path3",
 223     },
 224     // 6. get 'path2/path3' by 1,2  through non-LOCAL VFS
 225     {
 226         "/path1/path2/test1://user:pass@some.host:12345/path3/path4",
 227         1,
 228         2,
 229         "path2/test1://user:pass@some.host:12345/path3",
 230     },
 231     // 7. get 'path2/path3' by 1,2  where path2 it's LOCAL VFS
 232     {
 233         "test3://path1/path2/test2://path3/path4",
 234         1,
 235         2,
 236         "path2/test2://path3",
 237     },
 238     // 8. get 'path2/path3' by 1,2  where path3 it's LOCAL VFS
 239     {
 240         "test2://path1/path2/test3://path3/path4",
 241         1,
 242         2,
 243         "test2://path2/test3://path3",
 244     },
 245     // 9. get 'path4' by -1,1
 246     {
 247         "/path1/path2/path3/path4",
 248         -1,
 249         1,
 250         "path4",
 251     },
 252     // 10. get 'path2/path3/path4' by -3,0
 253     {
 254         "/path1/path2/path3/path4",
 255         -3,
 256         0,
 257         "path2/path3/path4",
 258     },
 259 #ifdef HAVE_CHARSET
 260     // 11. get 'path2/path3' by 1,2  from LOCAL VFS with encoding
 261     {
 262         "test3://path1/path2/test3://#enc:KOI8-R/path3/path4",
 263         1,
 264         2,
 265         "path2/test3://#enc:KOI8-R/path3",
 266     },
 267     // 12. get 'path2/path3' by 1,2  with encoding
 268     {
 269         "#enc:KOI8-R/path1/path2/path3/path4",
 270         1,
 271         2,
 272         "#enc:KOI8-R/path2/path3",
 273     },
 274 #endif
 275     /* TODO: currently this test don't passed. Probably broken string URI parser
 276      * // 13. get 'path2/path3' by 1,2  from LOCAL VFS
 277      {
 278 
 279             "test3://path1/path2/test2://test3://path3/path4",
 280             1,
 281             2,
 282             "path2/path3"
 283         },
 284     */
 285 };
 286 
 287 /* @Test(dataSource = "test_vfs_path_tokens_get_ds") */
 288 START_PARAMETRIZED_TEST (test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     // given
 291     vfs_path_t *vpath;
 292     char *actual_path;
 293 
 294     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 295 
 296     // when
 297     actual_path = vfs_path_tokens_get (vpath, data->input_start_position, data->input_length);
 298 
 299     // then
 300     mctest_assert_str_eq (actual_path, data->expected_path);
 301 
 302     g_free (actual_path);
 303     vfs_path_free (vpath, TRUE);
 304 }
 305 END_PARAMETRIZED_TEST
 306 
 307 /* --------------------------------------------------------------------------------------------- */
 308 
 309 /* @DataSource("test_vfs_path_append_vpath_ds") */
 310 static const struct test_vfs_path_append_vpath_ds
 311 {
 312     const char *input_path1;
 313     const char *input_path2;
 314     const int expected_element_count;
 315     const char *expected_path;
 316 } test_vfs_path_append_vpath_ds[] = {
 317     {
 318         // 0.
 319         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/"
 320         "test3://111/22/33",
 321         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 322         6,
 323         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/"
 324         "test3://111/22/33"
 325         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
 326     },
 327 #ifdef HAVE_CHARSET
 328     {
 329         // 1.
 330         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/"
 331         "bla-bla/some/path/test3://111/22/33",
 332         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 333         6,
 334         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/"
 335         "bla-bla/some/path/test3://111/22/33"
 336         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path",
 337     },
 338 #endif
 339 };
 340 
 341 /* @Test(dataSource = "test_vfs_path_append_vpath_ds") */
 342 START_PARAMETRIZED_TEST (test_vfs_path_append_vpath, test_vfs_path_append_vpath_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 343 {
 344     // given
 345     vfs_path_t *vpath1, *vpath2, *vpath3;
 346 
 347     vpath1 = vfs_path_from_str (data->input_path1);
 348     vpath2 = vfs_path_from_str (data->input_path2);
 349 
 350     // when
 351     vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
 352 
 353     // then
 354     ck_assert_int_eq (vfs_path_elements_count (vpath3), data->expected_element_count);
 355     mctest_assert_str_eq (vfs_path_as_str (vpath3), data->expected_path);
 356 
 357     vfs_path_free (vpath1, TRUE);
 358     vfs_path_free (vpath2, TRUE);
 359     vfs_path_free (vpath3, TRUE);
 360 }
 361 END_PARAMETRIZED_TEST
 362 
 363 /* --------------------------------------------------------------------------------------------- */
 364 
 365 /* @DataSource("test_vfs_path_relative_ds") */
 366 static const struct test_vfs_path_relative_ds
 367 {
 368     const char *input_path;
 369     const char *expected_path;
 370     const char *expected_last_path_in_element;
 371 } test_vfs_path_relative_ds[] = {
 372     {
 373         // 0.
 374         "../bla-bla",
 375         "../bla-bla",
 376         "../bla-bla",
 377     },
 378     {
 379         // 1.
 380         "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 381         "../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
 382         "bla-bla/some/path/",
 383     },
 384 };
 385 
 386 /* @Test(dataSource = "test_vfs_path_relative_ds") */
 387 START_PARAMETRIZED_TEST (test_vfs_path_relative, test_vfs_path_relative_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 388 {
 389     // given
 390     vfs_path_t *vpath;
 391 
 392     // when
 393 
 394     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 395 
 396     // then
 397     mctest_assert_true (vpath->relative);
 398     mctest_assert_str_eq (vfs_path_get_last_path_str (vpath), data->expected_last_path_in_element);
 399     mctest_assert_str_eq (vfs_path_as_str (vpath), data->expected_path);
 400 
 401     vfs_path_free (vpath, TRUE);
 402 }
 403 END_PARAMETRIZED_TEST
 404 
 405 /* --------------------------------------------------------------------------------------------- */
 406 
 407 /* @Test(dataSource = "test_vfs_path_relative_ds") */
 408 START_PARAMETRIZED_TEST (test_vfs_path_relative_clone, test_vfs_path_relative_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 409 {
 410     // given
 411     vfs_path_t *vpath, *cloned_vpath;
 412 
 413     vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON);
 414 
 415     // when
 416 
 417     cloned_vpath = vfs_path_clone (vpath);
 418 
 419     // then
 420     mctest_assert_true (cloned_vpath->relative);
 421     mctest_assert_str_eq (vfs_path_get_last_path_str (cloned_vpath),
 422                           data->expected_last_path_in_element);
 423     mctest_assert_str_eq (vfs_path_as_str (cloned_vpath), data->expected_path);
 424 
 425     vfs_path_free (vpath, TRUE);
 426     vfs_path_free (cloned_vpath, TRUE);
 427 }
 428 END_PARAMETRIZED_TEST
 429 
 430 /* --------------------------------------------------------------------------------------------- */
 431 
 432 int
 433 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 434 {
 435     TCase *tc_core;
 436 
 437     tc_core = tcase_create ("Core");
 438 
 439     tcase_add_checked_fixture (tc_core, setup, teardown);
 440 
 441     // Add new tests here: ***************
 442     mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_count,
 443                                    test_vfs_path_tokens_count_ds);
 444     mctest_add_parameterized_test (tc_core, test_vfs_path_tokens_get, test_vfs_path_tokens_get_ds);
 445     mctest_add_parameterized_test (tc_core, test_vfs_path_append_vpath,
 446                                    test_vfs_path_append_vpath_ds);
 447     mctest_add_parameterized_test (tc_core, test_vfs_path_relative, test_vfs_path_relative_ds);
 448     mctest_add_parameterized_test (tc_core, test_vfs_path_relative_clone,
 449                                    test_vfs_path_relative_ds);
 450     // ***********************************
 451 
 452     return mctest_run_all (tc_core);
 453 }
 454 
 455 /* --------------------------------------------------------------------------------------------- */

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