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-2024
   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     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 /* *INDENT-OFF* */
  92 static const struct test_path_recode_ds
  93 {
  94     const char *input_codepage;
  95     const char *input_path;
  96     const char *expected_element_path;
  97     const char *expected_recoded_path;
  98 } test_path_recode_ds[] =
  99 {
 100     { /* 0. */
 101         "UTF-8",
 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         "/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
 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     { /* 2. */
 113         "KOI8-R",
 114         "/тестовый/путь",
 115         "/тестовый/путь",
 116         "/тестовый/путь"
 117     },
 118     { /* 3. */
 119         "KOI8-R",
 120         "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8",
 121         "/тестовый/путь",
 122         "/#enc:UTF-8/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
 123     },
 124     { /* 4. Test encode info at start */
 125         "UTF-8",
 126         "#enc:KOI8-R/bla-bla/some/path",
 127         "/bla-bla/some/path",
 128         "/#enc:KOI8-R/bla-bla/some/path"
 129     },
 130 };
 131 /* *INDENT-ON* */
 132 
 133 /* @Test(dataSource = "test_path_recode_ds") */
 134 /* *INDENT-OFF* */
 135 START_PARAMETRIZED_TEST (test_path_recode, test_path_recode_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 136 /* *INDENT-ON* */
 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 /* *INDENT-OFF* */
 159 END_PARAMETRIZED_TEST
 160 /* *INDENT-ON* */
 161 
 162 
 163 /* --------------------------------------------------------------------------------------------- */
 164 
 165 static struct vfs_class vfs_test_ops1;
 166 
 167 /* @DataSource("test_path_to_str_flags_ds") */
 168 /* *INDENT-OFF* */
 169 static const struct test_path_to_str_flags_ds
 170 {
 171     const char *input_path;
 172     const vfs_path_flag_t input_from_str_flags;
 173     const vfs_path_flag_t input_to_str_flags;
 174     const char *expected_path;
 175 } test_path_to_str_flags_ds[] =
 176 {
 177     { /* 0. */
 178         "test1://user:passwd@127.0.0.1",
 179         VPF_NO_CANON,
 180         VPF_STRIP_PASSWORD,
 181         "test1://user@127.0.0.1/"
 182     },
 183     { /* 1. */
 184         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 185         VPF_NONE,
 186         VPF_STRIP_PASSWORD,
 187         "/test1://user@host.name/#enc:KOI8-R/тестовый/путь"
 188     },
 189     { /* 2. */
 190         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 191         VPF_NONE,
 192         VPF_RECODE,
 193         "/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
 194     },
 195     { /* 3. */
 196         "/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 197         VPF_NONE,
 198         VPF_RECODE | VPF_STRIP_PASSWORD,
 199         "/test1://user@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
 200     },
 201     { /* 4. */
 202         "/mock/home/test/dir",
 203         VPF_NONE,
 204         VPF_STRIP_HOME,
 205         "~/test/dir"
 206     },
 207     { /* 5. */
 208         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 209         VPF_NONE,
 210         VPF_STRIP_HOME | VPF_STRIP_PASSWORD,
 211         "~/test1://user@host.name/#enc:KOI8-R/тестовый/путь"
 212     },
 213     { /* 6. */
 214         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 215         VPF_NONE,
 216         VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET,
 217         "~/test1://user@host.name/тестовый/путь"
 218     },
 219     { /* 7. */
 220         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 221         VPF_NONE,
 222         VPF_STRIP_HOME | VPF_RECODE,
 223         "~/test1://user:passwd@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
 224     },
 225     { /* 8. */
 226         "/mock/home/test1://user:passwd@host.name/#enc:KOI8-R/тестовый/путь",
 227         VPF_NONE,
 228         VPF_STRIP_HOME | VPF_RECODE | VPF_STRIP_PASSWORD,
 229         "~/test1://user@host.name/\xD4\xC5\xD3\xD4\xCF\xD7\xD9\xCA/\xD0\xD5\xD4\xD8"
 230     },
 231 };
 232 /* *INDENT-ON* */
 233 
 234 /* @Test(dataSource = "test_path_to_str_flags_ds") */
 235 /* *INDENT-OFF* */
 236 START_PARAMETRIZED_TEST (test_path_to_str_flags, test_path_to_str_flags_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 237 /* *INDENT-ON* */
 238 {
 239     /* given */
 240     vfs_path_t *vpath;
 241     char *str_path;
 242 
 243     test_init_vfs ("UTF-8");
 244 
 245     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
 246     vfs_register_class (&vfs_test_ops1);
 247 
 248     /* when */
 249 
 250     vpath = vfs_path_from_str_flags (data->input_path, data->input_from_str_flags);
 251     str_path = vfs_path_to_str_flags (vpath, 0, data->input_to_str_flags);
 252 
 253     /* then */
 254     mctest_assert_str_eq (str_path, data->expected_path);
 255 
 256     g_free (str_path);
 257     vfs_path_free (vpath, TRUE);
 258     test_deinit_vfs ();
 259 }
 260 /* *INDENT-OFF* */
 261 END_PARAMETRIZED_TEST
 262 /* *INDENT-ON* */
 263 
 264 /* --------------------------------------------------------------------------------------------- */
 265 
 266 int
 267 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 268 {
 269     TCase *tc_core;
 270 
 271     tc_core = tcase_create ("Core");
 272 
 273     tcase_add_checked_fixture (tc_core, setup, teardown);
 274 
 275     /* Add new tests here: *************** */
 276     mctest_add_parameterized_test (tc_core, test_path_recode, test_path_recode_ds);
 277     mctest_add_parameterized_test (tc_core, test_path_to_str_flags, test_path_to_str_flags_ds);
 278     /* *********************************** */
 279 
 280     return mctest_run_all (tc_core);
 281 }
 282 
 283 /* --------------------------------------------------------------------------------------------- */

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