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-2020
   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 #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     mc_global.timer = mc_timer_new ();
  49     str_init_strings (NULL);
  50 
  51     vfs_init ();
  52     vfs_init_localfs ();
  53     vfs_setup_work_dir ();
  54 
  55 #ifdef HAVE_CHARSET
  56     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  57     load_codepages_list ();
  58 #endif
  59 
  60     vfs_init_class (&vfs_test_ops, "testfs", VFSF_NOLINKS | VFSF_REMOTE, "ftp");
  61     vfs_register_class (&vfs_test_ops);
  62 }
  63 
  64 /* --------------------------------------------------------------------------------------------- */
  65 
  66 /* @After */
  67 static void
  68 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70 #ifdef HAVE_CHARSET
  71     free_codepages_list ();
  72 #endif
  73 
  74     vfs_shut ();
  75 
  76     str_uninit_strings ();
  77     mc_timer_destroy (mc_global.timer);
  78 }
  79 
  80 /* --------------------------------------------------------------------------------------------- */
  81 
  82 /* @DataSource("test_canonicalize_path_ds") */
  83 /* *INDENT-OFF* */
  84 static const struct test_canonicalize_path_ds
  85 {
  86     const char *input_path;
  87     const char *expected_path;
  88 } test_canonicalize_path_ds[] =
  89 {
  90     { /* 0. UNC path */
  91         "//some_server/ww",
  92         "//some_server/ww"
  93     },
  94     { /* 1. join slashes */
  95         "///some_server/////////ww",
  96         "/some_server/ww"
  97     },
  98     { /* 2. Collapse "/./" -> "/" */
  99         "//some_server//.///////ww/./././.",
 100         "//some_server/ww"
 101     },
 102     {/* 3. Remove leading "./" */
 103         "./some_server/ww",
 104         "some_server/ww"
 105     },
 106     { /* 4. some/.. -> . */
 107         "some_server/..",
 108         "."
 109     },
 110     { /* 5. Collapse "/.." with the previous part of path */
 111         "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
 112         "/some_server/ww/ww/some_server/ww"
 113     },
 114     { /* 6. URI style */
 115         "/some_server/ww/ftp://user:pass@host.net/path/",
 116         "/some_server/ww/ftp://user:pass@host.net/path"
 117     },
 118     { /* 7. */
 119         "/some_server/ww/ftp://user:pass@host.net/path/../../",
 120         "/some_server/ww"
 121     },
 122     { /* 8. */
 123         "ftp://user:pass@host.net/path/../../",
 124         "."
 125     },
 126     { /* 9. */
 127         "ftp://user/../../",
 128         ".."
 129     },
 130 #ifdef HAVE_CHARSET
 131     { /* 10. Supported encoding */
 132         "/b/#enc:utf-8/../c",
 133         "/c"
 134     },
 135     { /* 11. Unsupported encoding */
 136         "/b/#enc:aaaa/../c",
 137         "/b/c"
 138     },
 139     { /* 12. Supported encoding */
 140         "/b/../#enc:utf-8/c",
 141         "/#enc:utf-8/c"
 142     },
 143     { /* 13. Unsupported encoding */
 144         "/b/../#enc:aaaa/c",
 145         "/#enc:aaaa/c"
 146     },
 147     { /* 14.  Supported encoding */
 148         "/b/c/#enc:utf-8/..",
 149         "/b"
 150     },
 151     { /* 15.  Unsupported encoding */
 152         "/b/c/#enc:aaaa/..",
 153         "/b/c"
 154     },
 155     { /* 16.  Supported encoding */
 156         "/b/c/../#enc:utf-8",
 157         "/b/#enc:utf-8"
 158     },
 159     { /* 17.  Unsupported encoding */
 160         "/b/c/../#enc:aaaa",
 161         "/b/#enc:aaaa"
 162     },
 163 #endif  /* HAVE_CHARSET */
 164 };
 165 /* *INDENT-ON* */
 166 
 167 /* @Test(dataSource = "test_canonicalize_path_ds") */
 168 /* *INDENT-OFF* */
 169 START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 170 /* *INDENT-ON* */
 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 /* *INDENT-OFF* */
 184 END_PARAMETRIZED_TEST
 185 /* *INDENT-ON* */
 186 
 187 /* --------------------------------------------------------------------------------------------- */
 188 
 189 int
 190 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 191 {
 192     int number_failed;
 193 
 194     Suite *s = suite_create (TEST_SUITE_NAME);
 195     TCase *tc_core = tcase_create ("Core");
 196     SRunner *sr;
 197 
 198     tcase_add_checked_fixture (tc_core, setup, teardown);
 199 
 200     /* Add new tests here: *************** */
 201     mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
 202     /* *********************************** */
 203 
 204     suite_add_tcase (s, tc_core);
 205     sr = srunner_create (s);
 206     srunner_set_log (sr, "canonicalize_pathname.log");
 207     srunner_run_all (sr, CK_ENV);
 208     number_failed = srunner_ntests_failed (sr);
 209     srunner_free (sr);
 210     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 211 }
 212 
 213 /* --------------------------------------------------------------------------------------------- */

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