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

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