Manual pages: mcmcdiffmceditmcview

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

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