root/tests/lib/vfs/vfs_path_string_convert.c

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

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. START_PARAMETRIZED_TEST
  5. START_TEST
  6. main

   1 /*
   2    lib/vfs - get vfs_path_t from string
   3 
   4    Copyright (C) 2011-2020
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/lib/vfs"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #ifdef HAVE_CHARSET
  31 #include "lib/charsets.h"
  32 #endif
  33 
  34 #include "lib/strutil.h"
  35 #include "lib/vfs/xdirentry.h"
  36 #include "lib/vfs/path.c"       /* for testing static methods  */
  37 
  38 #include "src/vfs/local/local.c"
  39 
  40 static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  41 
  42 #define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
  43 #define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
  44 
  45 /* --------------------------------------------------------------------------------------------- */
  46 
  47 /* @Before */
  48 static void
  49 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  50 {
  51     mc_global.timer = mc_timer_new ();
  52     str_init_strings (NULL);
  53 
  54     vfs_init ();
  55     vfs_init_localfs ();
  56     vfs_setup_work_dir ();
  57 
  58     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS, "test1");
  59     vfs_register_class (&vfs_test_ops1);
  60 
  61     vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_REMOTE, "test2");
  62     vfs_register_class (&vfs_test_ops2);
  63 
  64     vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  65     vfs_register_class (&vfs_test_ops3);
  66 
  67 #ifdef HAVE_CHARSET
  68     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  69     load_codepages_list ();
  70 #endif /* HAVE_CHARSET */
  71 }
  72 
  73 /* --------------------------------------------------------------------------------------------- */
  74 
  75 /* @After */
  76 static void
  77 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79 #ifdef HAVE_CHARSET
  80     free_codepages_list ();
  81 #endif /* HAVE_CHARSET */
  82 
  83     vfs_shut ();
  84     str_uninit_strings ();
  85     mc_timer_destroy (mc_global.timer);
  86 }
  87 
  88 /* --------------------------------------------------------------------------------------------- */
  89 /* @DataSource("test_from_to_string_ds") */
  90 /* *INDENT-OFF* */
  91 static const struct test_from_to_string_ds
  92 {
  93     const char *input_string;
  94     const char *expected_result;
  95     const char *expected_element_path;
  96     const size_t expected_elements_count;
  97     struct vfs_class *expected_vfs_class;
  98 } test_from_to_string_ds[] =
  99 {
 100     { /* 0. */
 101         ETALON_PATH_STR,
 102         ETALON_PATH_URL_STR,
 103         "111/22/33",
 104         4,
 105         &vfs_test_ops3
 106     },
 107     { /* 1. */
 108         "/",
 109         "/",
 110         "/",
 111         1,
 112         VFS_CLASS (&local_subclass)
 113     },
 114     { /* 2. */
 115         "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
 116         "/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
 117         "111/22/33",
 118         4,
 119         &vfs_test_ops3
 120     },
 121 #ifdef HAVE_CHARSET
 122     { /* 3. */
 123         "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
 124         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 125         "111/22/33",
 126         4,
 127         &vfs_test_ops3
 128     },
 129     { /* 4. */
 130         "/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
 131         "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 132         "111/22/33",
 133         4,
 134         &vfs_test_ops3
 135     },
 136     {  /* 5. */
 137         "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
 138         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 139         "111/22/33",
 140         4,
 141         &vfs_test_ops3
 142     },
 143     { /* 6. */
 144         "/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
 145         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 146         "111/22/33",
 147         4,
 148         &vfs_test_ops3
 149     },
 150     { /* 7. */
 151         "/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
 152         "/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 153         "111/22/33",
 154         4,
 155         &vfs_test_ops3
 156     },
 157     { /* 8. */
 158         "/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
 159         "/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
 160         "111/22/33",
 161         4,
 162         &vfs_test_ops3
 163     },
 164 #endif /* HAVE_CHARSET */
 165 };
 166 /* *INDENT-ON* */
 167 
 168 /* @Test */
 169 /* *INDENT-OFF* */
 170 START_PARAMETRIZED_TEST (test_from_to_string, test_from_to_string_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 171 /* *INDENT-ON* */
 172 {
 173     /* given */
 174     vfs_path_t *vpath;
 175     size_t vpath_len;
 176     const vfs_path_element_t *path_element;
 177     const char *actual_result;
 178 
 179     vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
 180 
 181     /* when */
 182     vpath_len = vfs_path_elements_count (vpath);
 183     actual_result = vfs_path_as_str (vpath);
 184     path_element = vfs_path_get_by_index (vpath, -1);
 185 
 186     /* then */
 187     mctest_assert_int_eq (vpath_len, data->expected_elements_count);
 188     mctest_assert_str_eq (actual_result, data->expected_result);
 189     mctest_assert_ptr_eq (path_element->class, data->expected_vfs_class);
 190     mctest_assert_str_eq (path_element->path, data->expected_element_path);
 191 
 192     vfs_path_free (vpath);
 193 }
 194 /* *INDENT-OFF* */
 195 END_PARAMETRIZED_TEST
 196 /* *INDENT-ON* */
 197 
 198 /* --------------------------------------------------------------------------------------------- */
 199 
 200 /* @DataSource("test_partial_string_by_index_ds") */
 201 /* *INDENT-OFF* */
 202 static const struct test_partial_string_by_index_ds
 203 {
 204     const char *input_string;
 205     const off_t element_index;
 206     const char *expected_result;
 207 } test_partial_string_by_index_ds[] =
 208 {
 209     { /* 0. */
 210         ETALON_PATH_STR,
 211         -1,
 212         "/test1://bla-bla/some/path/test2://bla-bla/some/path"
 213     },
 214     { /* 1. */
 215         ETALON_PATH_STR,
 216         -2,
 217         "/test1://bla-bla/some/path/"
 218     },
 219     { /* 2. */
 220         ETALON_PATH_STR,
 221         -3,
 222         "/"
 223     },
 224     { /* 3. Index out of bound */
 225         ETALON_PATH_STR,
 226         -4,
 227         ""
 228     },
 229     { /* 4. */
 230         ETALON_PATH_STR,
 231         1,
 232         "/"
 233     },
 234     { /* 5. */
 235         ETALON_PATH_STR,
 236         2,
 237         "/test1://bla-bla/some/path/"
 238     },
 239     { /* 6. */
 240         ETALON_PATH_STR,
 241         3,
 242         "/test1://bla-bla/some/path/test2://bla-bla/some/path"
 243     },
 244     { /* 6. */
 245         ETALON_PATH_STR,
 246         4,
 247         ETALON_PATH_URL_STR
 248     },
 249     { /* 7. Index out of bound */
 250         ETALON_PATH_STR,
 251         5,
 252         ETALON_PATH_URL_STR
 253     },
 254 };
 255 /* *INDENT-ON* */
 256 
 257 /* @Test(dataSource = "test_partial_string_by_index_ds") */
 258 /* *INDENT-OFF* */
 259 START_PARAMETRIZED_TEST (test_partial_string_by_index, test_partial_string_by_index_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 260 /* *INDENT-ON* */
 261 {
 262     /* given */
 263     vfs_path_t *vpath;
 264     char *actual_result;
 265     vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
 266 
 267     /* when */
 268     actual_result = vfs_path_to_str_elements_count (vpath, data->element_index);
 269 
 270     /* then */
 271     mctest_assert_str_eq (actual_result, data->expected_result);
 272     g_free (actual_result);
 273 
 274     vfs_path_free (vpath);
 275 }
 276 /* *INDENT-OFF* */
 277 END_PARAMETRIZED_TEST
 278 /* *INDENT-ON* */
 279 
 280 /* --------------------------------------------------------------------------------------------- */
 281 #ifdef HAVE_CHARSET
 282 /* --------------------------------------------------------------------------------------------- */
 283 
 284 #define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
 285 /* @Test */
 286 /* *INDENT-OFF* */
 287 START_TEST (test_vfs_path_encoding_at_end)
     /* [previous][next][first][last][top][bottom][index][help]  */
 288 /* *INDENT-ON* */
 289 {
 290     /* given */
 291     vfs_path_t *vpath;
 292     const char *result;
 293     const vfs_path_element_t *element;
 294 
 295     vpath =
 296         vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER);
 297 
 298     /* when */
 299     result = vfs_path_as_str (vpath);
 300     element = vfs_path_get_by_index (vpath, -1);
 301 
 302     /* then */
 303     mctest_assert_str_eq (element->path, "");
 304     mctest_assert_not_null (element->encoding);
 305     mctest_assert_str_eq (result, ETALON_STR);
 306 
 307     vfs_path_free (vpath);
 308 }
 309 
 310 /* *INDENT-OFF* */
 311 END_TEST
 312 /* *INDENT-ON* */
 313 #endif /* HAVE_CHARSET */
 314 /* --------------------------------------------------------------------------------------------- */
 315 
 316 int
 317 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 318 {
 319     int number_failed;
 320 
 321     Suite *s = suite_create (TEST_SUITE_NAME);
 322     TCase *tc_core = tcase_create ("Core");
 323     SRunner *sr;
 324 
 325     tcase_add_checked_fixture (tc_core, setup, teardown);
 326 
 327     /* Add new tests here: *************** */
 328     mctest_add_parameterized_test (tc_core, test_from_to_string, test_from_to_string_ds);
 329     mctest_add_parameterized_test (tc_core, test_partial_string_by_index,
 330                                    test_partial_string_by_index_ds);
 331 #ifdef HAVE_CHARSET
 332     tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
 333 #endif
 334     /* *********************************** */
 335 
 336     suite_add_tcase (s, tc_core);
 337     sr = srunner_create (s);
 338     srunner_set_log (sr, "vfs_path_string_convert.log");
 339     srunner_run_all (sr, CK_ENV);
 340     number_failed = srunner_ntests_failed (sr);
 341     srunner_free (sr);
 342     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 343 }
 344 
 345 /* --------------------------------------------------------------------------------------------- */

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