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-2025
   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 <https://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     str_init_strings (encoding);
  69 
  70     vfs_init ();
  71     vfs_init_localfs ();
  72     vfs_setup_work_dir ();
  73 
  74     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  75     load_codepages_list ();
  76 }
  77 
  78 /* --------------------------------------------------------------------------------------------- */
  79 
  80 static void
  81 test_deinit_vfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  82 {
  83     free_codepages_list ();
  84     str_uninit_strings ();
  85     vfs_shut ();
  86 }
  87 
  88 /* --------------------------------------------------------------------------------------------- */
  89 
  90 /* @DataSource("test_path_recode_ds") */
  91 static const struct test_path_recode_ds
  92 {
  93     const char *input_codepage;
  94     const char *input_path;
  95     const char *expected_element_path;
  96     const char *expected_recoded_path;
  97 } test_path_recode_ds[] = {
  98     {
  99         // 0.
 100         "UTF-8",
 101         "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 102         "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 103         "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 104     },
 105     {
 106         // 1.
 107         "UTF-8",
 108         "/#enc:KOI8-R/тестовый/путь",
 109         "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 110         "/#enc:KOI8-R/тестовый/путь",
 111     },
 112     {
 113         // 2.
 114         "KOI8-R",
 115         "/тестовый/путь",
 116         "/тестовый/путь",
 117         "/тестовый/путь",
 118     },
 119     {
 120         // 3.
 121         "KOI8-R",
 122         "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 123         "/тестовый/путь",
 124         "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 125     },
 126     {
 127         // 4. Test encode info at start
 128         "UTF-8",
 129         "#enc:KOI8-R/bla-bla/some/path",
 130         "/bla-bla/some/path",
 131         "/#enc:KOI8-R/bla-bla/some/path",
 132     },
 133 };
 134 
 135 /* @Test(dataSource = "test_path_recode_ds") */
 136 START_PARAMETRIZED_TEST (test_path_recode, test_path_recode_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 137 {
 138     // given
 139     vfs_path_t *vpath;
 140     const char *element_path;
 141     const char *vpath_str;
 142 
 143     test_init_vfs (data->input_codepage);
 144 
 145     // when
 146     vpath = vfs_path_from_str (data->input_path);
 147     element_path = vfs_path_get_last_path_str (vpath);
 148 
 149     // then
 150     vpath_str = vfs_path_as_str (vpath);
 151     mctest_assert_ptr_ne (vpath, NULL);
 152     mctest_assert_str_eq (element_path, data->expected_element_path);
 153     mctest_assert_str_eq (vpath_str, data->expected_recoded_path);
 154 
 155     vfs_path_free (vpath, TRUE);
 156     test_deinit_vfs ();
 157 }
 158 END_PARAMETRIZED_TEST
 159 
 160 /* --------------------------------------------------------------------------------------------- */
 161 
 162 static struct vfs_class vfs_test_ops1;
 163 
 164 /* @DataSource("test_path_to_str_flags_ds") */
 165 static const struct test_path_to_str_flags_ds
 166 {
 167     const char *input_path;
 168     const vfs_path_flag_t input_from_str_flags;
 169     const vfs_path_flag_t input_to_str_flags;
 170     const char *expected_path;
 171 } test_path_to_str_flags_ds[] = {
 172     {
 173         // 0.
 174         "test1://user:passwd@127.0.0.1",
 175         VPF_NO_CANON,
 176         VPF_STRIP_PASSWORD,
 177         "test1://user@127.0.0.1/",
 178     },
 179     {
 180         // 1.
 181         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 182         VPF_NONE,
 183         VPF_STRIP_PASSWORD,
 184         "/test1://user@host.name/#enc:KOI8-R/тестовый/путь",
 185     },
 186     {
 187         // 2.
 188         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 189         VPF_NONE,
 190         VPF_RECODE,
 191         "/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 192     },
 193     {
 194         // 3.
 195         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 196         VPF_NONE,
 197         VPF_RECODE | VPF_STRIP_PASSWORD,
 198         "/test1://user@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 199     },
 200     {
 201         // 4.
 202         "/mock/home/test/dir",
 203         VPF_NONE,
 204         VPF_STRIP_HOME,
 205         "~/test/dir",
 206     },
 207     {
 208         // 5.
 209         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 210         VPF_NONE,
 211         VPF_STRIP_HOME | VPF_STRIP_PASSWORD,
 212         "~/test1://user@host.name/#enc:KOI8-R/тестовый/путь",
 213     },
 214     {
 215         // 6.
 216         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 217         VPF_NONE,
 218         VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
 219         "~/test1://user@host.name/тестовый/путь",
 220     },
 221     {
 222         // 7.
 223         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 224         VPF_NONE,
 225         VPF_STRIP_HOME | VPF_RECODE,
 226         "~/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 227     },
 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/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 234     },
 235 };
 236 
 237 /* @Test(dataSource = "test_path_to_str_flags_ds") */
 238 START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 239 {
 240     // given
 241     vfs_path_t *vpath;
 242     char *str_path;
 243 
 244     test_init_vfs ("UTF-8");
 245 
 246     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
 247     vfs_register_class (&vfs_test_ops1);
 248 
 249     // when
 250 
 251     vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
 252     str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
 253 
 254     // then
 255     mctest_assert_str_eq (str_path, data->expected_path);
 256 
 257     g_free (str_path);
 258     vfs_path_free (vpath, TRUE);
 259     test_deinit_vfs ();
 260 }
 261 END_PARAMETRIZED_TEST
 262 
 263 /* --------------------------------------------------------------------------------------------- */
 264 
 265 int
 266 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 267 {
 268     TCase *tc_core;
 269 
 270     tc_core = tcase_create ("Core");
 271 
 272     tcase_add_checked_fixture (tc_core, setup, teardown);
 273 
 274     // Add new tests here: ***************
 275     mctest_add_parameterized_test (tc_core, test_path_recode, test_path_recode_ds);
 276     mctest_add_parameterized_test (tc_core, test_path_to_str_flags, test_path_to_str_flags_ds);
 277     // ***********************************
 278 
 279     return mctest_run_all (tc_core);
 280 }
 281 
 282 /* --------------------------------------------------------------------------------------------- */

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