Manual pages: mcmcdiffmceditmcview

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

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