Manual pages: mcmcdiffmceditmcview

root/tests/lib/vfs/canonicalize_pathname.c

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

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. main

   1 /*
   2    lib - canonicalize path
   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 #include "lib/strutil.h"
  32 #include "lib/util.h"
  33 #include "lib/vfs/xdirentry.h"
  34 
  35 #include "src/vfs/local/local.c"
  36 
  37 static struct vfs_class vfs_test_ops;
  38 
  39 /* --------------------------------------------------------------------------------------------- */
  40 
  41 /* @Before */
  42 static void
  43 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  44 {
  45     str_init_strings (NULL);
  46 
  47     vfs_init ();
  48     vfs_init_localfs ();
  49     vfs_setup_work_dir ();
  50 
  51     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  52     load_codepages_list ();
  53 
  54     vfs_init_class (&vfs_test_ops, "testfs", VFSF_NOLINKS | VFSF_REMOTE, "ftp");
  55     vfs_register_class (&vfs_test_ops);
  56 }
  57 
  58 /* --------------------------------------------------------------------------------------------- */
  59 
  60 /* @After */
  61 static void
  62 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     free_codepages_list ();
  65     vfs_shut ();
  66     str_uninit_strings ();
  67 }
  68 
  69 /* --------------------------------------------------------------------------------------------- */
  70 
  71 /* @DataSource("test_canonicalize_path_ds") */
  72 static const struct test_canonicalize_path_ds
  73 {
  74     const char *input_path;
  75     const char *expected_path;
  76 } test_canonicalize_path_ds[] = {
  77     {
  78         // 0. UNC path
  79         "//some_server/ww",
  80         "//some_server/ww",
  81     },
  82     {
  83         // 1. join slashes
  84         "///some_server/////////ww",
  85         "/some_server/ww",
  86     },
  87     {
  88         // 2. Collapse "/./" -> "/"
  89         "//some_server//.///////ww/./././.",
  90         "//some_server/ww",
  91     },
  92     {
  93         // 3. Remove leading "./"
  94         "./some_server/ww",
  95         "some_server/ww",
  96     },
  97     {
  98         // 4. some/.. -> .
  99         "some_server/..",
 100         ".",
 101     },
 102     {
 103         // 5. Collapse "/.." with the previous part of path
 104         "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
 105         "/some_server/ww/ww/some_server/ww",
 106     },
 107     {
 108         // 6. URI style
 109         "/some_server/ww/ftp://user:pass@host.net/path/",
 110         "/some_server/ww/ftp://user:pass@host.net/path",
 111     },
 112     {
 113         // 7.
 114         "/some_server/ww/ftp://user:pass@host.net/path/../../",
 115         "/some_server/ww",
 116     },
 117     {
 118         // 8.
 119         "ftp://user:pass@host.net/path/../../",
 120         ".",
 121     },
 122     {
 123         // 9.
 124         "ftp://user/../../",
 125         "..",
 126     },
 127     {
 128         // 10. Supported encoding
 129         "/b/#enc:utf-8/../c",
 130         "/c",
 131     },
 132     {
 133         // 11. Unsupported encoding
 134         "/b/#enc:aaaa/../c",
 135         "/b/c",
 136     },
 137     {
 138         // 12. Supported encoding
 139         "/b/../#enc:utf-8/c",
 140         "/#enc:utf-8/c",
 141     },
 142     {
 143         // 13. Unsupported encoding
 144         "/b/../#enc:aaaa/c",
 145         "/#enc:aaaa/c",
 146     },
 147     {
 148         // 14.  Supported encoding
 149         "/b/c/#enc:utf-8/..",
 150         "/b",
 151     },
 152     {
 153         // 15.  Unsupported encoding
 154         "/b/c/#enc:aaaa/..",
 155         "/b/c",
 156     },
 157     {
 158         // 16.  Supported encoding
 159         "/b/c/../#enc:utf-8",
 160         "/b/#enc:utf-8",
 161     },
 162     {
 163         // 17.  Unsupported encoding
 164         "/b/c/../#enc:aaaa",
 165         "/b/#enc:aaaa",
 166     },
 167 };
 168 
 169 /* @Test(dataSource = "test_canonicalize_path_ds") */
 170 START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 171 {
 172     // given
 173     char *actual_path;
 174 
 175     actual_path = g_strdup (data->input_path);
 176 
 177     // when
 178     canonicalize_pathname (actual_path);
 179 
 180     // then
 181     mctest_assert_str_eq (actual_path, data->expected_path) g_free (actual_path);
 182 }
 183 END_PARAMETRIZED_TEST
 184 
 185 /* --------------------------------------------------------------------------------------------- */
 186 
 187 int
 188 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 189 {
 190     TCase *tc_core;
 191 
 192     tc_core = tcase_create ("Core");
 193 
 194     tcase_add_checked_fixture (tc_core, setup, teardown);
 195 
 196     // Add new tests here: ***************
 197     mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
 198     // ***********************************
 199 
 200     return mctest_run_all (tc_core);
 201 }
 202 
 203 /* --------------------------------------------------------------------------------------------- */

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