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-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 #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     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", VFS_NOLINKS | VFS_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     str_uninit_strings ();
  75 }
  76 
  77 /* --------------------------------------------------------------------------------------------- */
  78 
  79 /* @DataSource("test_canonicalize_path_ds") */
  80 /* *INDENT-OFF* */
  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     { /* 1. join slashes */
  92         "///some_server/////////ww",
  93         "/some_server/ww"
  94     },
  95     { /* 2. Collapse "/./" -> "/" */
  96         "//some_server//.///////ww/./././.",
  97         "//some_server/ww"
  98     },
  99     {/* 3. Remove leading "./" */
 100         "./some_server/ww",
 101         "some_server/ww"
 102     },
 103     { /* 4. some/.. -> . */
 104         "some_server/..",
 105         "."
 106     },
 107     { /* 5. Collapse "/.." with the previous part of path */
 108         "/some_server/ww/some_server/../ww/../some_server/..//ww/some_server/ww",
 109         "/some_server/ww/ww/some_server/ww"
 110     },
 111     { /* 6. URI style */
 112         "/some_server/ww/ftp://user:pass@host.net/path/",
 113         "/some_server/ww/ftp://user:pass@host.net/path"
 114     },
 115     { /* 7. */
 116         "/some_server/ww/ftp://user:pass@host.net/path/../../",
 117         "/some_server/ww"
 118     },
 119     { /* 8. */
 120         "ftp://user:pass@host.net/path/../../",
 121         "."
 122     },
 123     { /* 9. */
 124         "ftp://user/../../",
 125         ".."
 126     },
 127 #ifdef HAVE_CHARSET
 128     { /* 10. Supported encoding */
 129         "/b/#enc:utf-8/../c",
 130         "/c"
 131     },
 132     { /* 11. Unsupported encoding */
 133         "/b/#enc:aaaa/../c",
 134         "/b/c"
 135     },
 136     { /* 12. Supported encoding */
 137         "/b/../#enc:utf-8/c",
 138         "/#enc:utf-8/c"
 139     },
 140     { /* 13. Unsupported encoding */
 141         "/b/../#enc:aaaa/c",
 142         "/#enc:aaaa/c"
 143     },
 144     { /* 14.  Supported encoding */
 145         "/b/c/#enc:utf-8/..",
 146         "/b"
 147     },
 148     { /* 15.  Unsupported encoding */
 149         "/b/c/#enc:aaaa/..",
 150         "/b/c"
 151     },
 152     { /* 16.  Supported encoding */
 153         "/b/c/../#enc:utf-8",
 154         "/b/#enc:utf-8"
 155     },
 156     { /* 17.  Unsupported encoding */
 157         "/b/c/../#enc:aaaa",
 158         "/b/#enc:aaaa"
 159     },
 160 #endif  /* HAVE_CHARSET */
 161 };
 162 /* *INDENT-ON* */
 163 
 164 /* @Test(dataSource = "test_canonicalize_path_ds") */
 165 /* *INDENT-OFF* */
 166 START_PARAMETRIZED_TEST (test_canonicalize_path, test_canonicalize_path_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 167 /* *INDENT-ON* */
 168 {
 169     /* given */
 170     char *actual_path;
 171 
 172     actual_path = g_strdup (data->input_path);
 173 
 174     /* when */
 175     canonicalize_pathname (actual_path);
 176 
 177     /* then */
 178     mctest_assert_str_eq (actual_path, data->expected_path) g_free (actual_path);
 179 }
 180 /* *INDENT-OFF* */
 181 END_PARAMETRIZED_TEST
 182 /* *INDENT-ON* */
 183 
 184 /* --------------------------------------------------------------------------------------------- */
 185 
 186 int
 187 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 {
 189     int number_failed;
 190 
 191     Suite *s = suite_create (TEST_SUITE_NAME);
 192     TCase *tc_core = tcase_create ("Core");
 193     SRunner *sr;
 194 
 195     tcase_add_checked_fixture (tc_core, setup, teardown);
 196 
 197     /* Add new tests here: *************** */
 198     mctest_add_parameterized_test (tc_core, test_canonicalize_path, test_canonicalize_path_ds);
 199     /* *********************************** */
 200 
 201     suite_add_tcase (s, tc_core);
 202     sr = srunner_create (s);
 203     srunner_set_log (sr, "canonicalize_pathname.log");
 204     srunner_run_all (sr, CK_ENV);
 205     number_failed = srunner_ntests_failed (sr);
 206     srunner_free (sr);
 207     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 208 }
 209 
 210 /* --------------------------------------------------------------------------------------------- */

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