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

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