root/tests/lib/vfs/path_recode.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_config_get_home_dir
  2. setup
  3. teardown
  4. test_init_vfs
  5. test_deinit_vfs
  6. START_PARAMETRIZED_TEST
  7. START_PARAMETRIZED_TEST
  8. main

   1 /*
   2    lib/vfs - vfs_path_t charset recode functions
   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 #include "lib/charsets.h"
  31 
  32 #include "lib/strutil.h"
  33 #include "lib/vfs/xdirentry.h"
  34 #include "lib/vfs/path.h"
  35 
  36 #include "src/vfs/local/local.c"
  37 
  38 /* --------------------------------------------------------------------------------------------- */
  39 
  40 /* @Mock */
  41 const char *
  42 mc_config_get_home_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  43 {
  44     return "/mock/home";
  45 }
  46 
  47 /* --------------------------------------------------------------------------------------------- */
  48 
  49 /* @Before */
  50 static void
  51 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  52 {
  53 }
  54 
  55 /* --------------------------------------------------------------------------------------------- */
  56 
  57 /* @After */
  58 static void
  59 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61 }
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 
  65 static void
  66 test_init_vfs (const char *encoding)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     mc_global.timer = mc_timer_new ();
  69     str_init_strings (encoding);
  70 
  71     vfs_init ();
  72     vfs_init_localfs ();
  73     vfs_setup_work_dir ();
  74 
  75     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  76 
  77     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  78     load_codepages_list ();
  79 }
  80 
  81 /* --------------------------------------------------------------------------------------------- */
  82 
  83 static void
  84 test_deinit_vfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  85 {
  86     free_codepages_list ();
  87     str_uninit_strings ();
  88     vfs_shut ();
  89     mc_timer_destroy (mc_global.timer);
  90 }
  91 
  92 /* --------------------------------------------------------------------------------------------- */
  93 
  94 /* @DataSource("test_path_recode_ds") */
  95 /* *INDENT-OFF* */
  96 static const struct test_path_recode_ds
  97 {
  98     const char *input_codepage;
  99     const char *input_path;
 100     const char *expected_element_path;
 101     const char *expected_recoded_path;
 102 } test_path_recode_ds[] =
 103 {
 104     { /* 0. */
 105         "UTF-8",
 106         "//",
 107         "//",
 108         "//"
 109     },
 110     { /* 1. */
 111         "UTF-8",
 112         "/#enc:KOI8-R/тестовый/путь",
 113         "//",
 114         "/#enc:KOI8-R/тестовый/путь"
 115     },
 116     { /* 2. */
 117         "KOI8-R",
 118         "/тестовый/путь",
 119         "/тестовый/путь",
 120         "/тестовый/путь"
 121     },
 122     { /* 3. */
 123         "KOI8-R",
 124         "/#enc:UTF-8//",
 125         "/тестовый/путь",
 126         "/#enc:UTF-8//"
 127     },
 128     { /* 4. Test encode info at start */
 129         "UTF-8",
 130         "#enc:KOI8-R/bla-bla/some/path",
 131         "/bla-bla/some/path",
 132         "/#enc:KOI8-R/bla-bla/some/path"
 133     },
 134 };
 135 /* *INDENT-ON* */
 136 
 137 /* @Test(dataSource = "test_path_recode_ds") */
 138 /* *INDENT-OFF* */
 139 START_PARAMETRIZED_TEST (test_path_recode, test_path_recode_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 /* *INDENT-ON* */
 141 {
 142     /* given */
 143     vfs_path_t *vpath;
 144     const vfs_path_element_t *element;
 145     const char *vpath_str;
 146 
 147     test_init_vfs (data->input_codepage);
 148 
 149     /* when */
 150     vpath = vfs_path_from_str (data->input_path);
 151     element = vfs_path_get_by_index (vpath, -1);
 152 
 153     /* then */
 154     vpath_str = vfs_path_as_str (vpath);
 155     mctest_assert_ptr_ne (vpath, NULL);
 156     mctest_assert_str_eq (element->path, data->expected_element_path);
 157     mctest_assert_str_eq (vpath_str, data->expected_recoded_path);
 158 
 159     vfs_path_free (vpath);
 160     test_deinit_vfs ();
 161 }
 162 /* *INDENT-OFF* */
 163 END_PARAMETRIZED_TEST
 164 /* *INDENT-ON* */
 165 
 166 
 167 /* --------------------------------------------------------------------------------------------- */
 168 
 169 static struct vfs_class vfs_test_ops1;
 170 
 171 /* @DataSource("test_path_to_str_flags_ds") */
 172 /* *INDENT-OFF* */
 173 static const struct test_path_to_str_flags_ds
 174 {
 175     const char *input_path;
 176     const vfs_path_flag_t input_from_str_flags;
 177     const vfs_path_flag_t input_to_str_flags;
 178     const char *expected_path;
 179 } test_path_to_str_flags_ds[] =
 180 {
 181     { /* 0. */
 182         "test1://user:passwd@127.0.0.1",
 183         VPF_NO_CANON,
 184         VPF_STRIP_PASSWORD,
 185         "test1://user@127.0.0.1/"
 186     },
 187     { /* 1. */
 188         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 189         VPF_NONE,
 190         VPF_STRIP_PASSWORD,
 191         "/test1://user@host.name/#enc:KOI8-R/тестовый/путь"
 192     },
 193     { /* 2. */
 194         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 195         VPF_NONE,
 196         VPF_RECODE,
 197         "/test1://user:passwd@host.name//"
 198     },
 199     { /* 3. */
 200         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 201         VPF_NONE,
 202         VPF_RECODE | VPF_STRIP_PASSWORD,
 203         "/test1://user@host.name//"
 204     },
 205     { /* 4. */
 206         "/mock/home/test/dir",
 207         VPF_NONE,
 208         VPF_STRIP_HOME,
 209         "~/test/dir"
 210     },
 211     { /* 5. */
 212         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 213         VPF_NONE,
 214         VPF_STRIP_HOME | VPF_STRIP_PASSWORD,
 215         "~/test1://user@host.name/#enc:KOI8-R/тестовый/путь"
 216     },
 217     { /* 6. */
 218         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 219         VPF_NONE,
 220         VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
 221         "~/test1://user@host.name/тестовый/путь"
 222     },
 223     { /* 7. */
 224         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 225         VPF_NONE,
 226         VPF_STRIP_HOME | VPF_RECODE,
 227         "~/test1://user:passwd@host.name//"
 228     },
 229     { /* 8. */
 230         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 231         VPF_NONE,
 232         VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD,
 233         "~/test1://user@host.name//"
 234     },
 235 };
 236 /* *INDENT-ON* */
 237 
 238 /* @Test(dataSource = "test_path_to_str_flags_ds") */
 239 /* *INDENT-OFF* */
 240 START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 241 /* *INDENT-ON* */
 242 {
 243     /* given */
 244     vfs_path_t *vpath;
 245     char *str_path;
 246 
 247     test_init_vfs ("UTF-8");
 248 
 249     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
 250     vfs_register_class (&vfs_test_ops1);
 251 
 252     /* when */
 253 
 254     vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
 255     str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
 256 
 257     /* then */
 258     mctest_assert_str_eq (str_path, data->expected_path);
 259 
 260     g_free (str_path);
 261     vfs_path_free (vpath);
 262     test_deinit_vfs ();
 263 }
 264 /* *INDENT-OFF* */
 265 END_PARAMETRIZED_TEST
 266 /* *INDENT-ON* */
 267 
 268 /* --------------------------------------------------------------------------------------------- */
 269 
 270 int
 271 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 272 {
 273     int number_failed;
 274 
 275     Suite *s = suite_create (TEST_SUITE_NAME);
 276     TCase *tc_core = tcase_create ("Core");
 277     SRunner *sr;
 278 
 279     tcase_add_checked_fixture (tc_core, setup, teardown);
 280 
 281     /* Add new tests here: *************** */
 282     mctest_add_parameterized_test (tc_core, test_path_recode, test_path_recode_ds);
 283     mctest_add_parameterized_test (tc_core, test_path_to_str_flags, test_path_to_str_flags_ds);
 284     /* *********************************** */
 285 
 286     suite_add_tcase (s, tc_core);
 287     sr = srunner_create (s);
 288     srunner_set_log (sr, "path_recode.log");
 289     srunner_run_all (sr, CK_ENV);
 290     number_failed = srunner_ntests_failed (sr);
 291     srunner_free (sr);
 292     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 293 }
 294 
 295 /* --------------------------------------------------------------------------------------------- */

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