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

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