root/tests/lib/vfs/path_serialize.c

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

DEFINITIONS

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

   1 /*
   2    lib/vfs - vfs_path_t serialize/deserialize functions
   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/vfs/xdirentry.h"
  36 #include "lib/vfs/path.h"
  37 
  38 #include "src/vfs/local/local.c"
  39 
  40 static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  41 
  42 /* --------------------------------------------------------------------------------------------- */
  43 
  44 /* @Before */
  45 static void
  46 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  47 {
  48     str_init_strings ("UTF-8");
  49 
  50     vfs_init ();
  51     vfs_init_localfs ();
  52     vfs_setup_work_dir ();
  53 
  54     vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  55     vfs_register_class (&vfs_test_ops1);
  56 
  57     vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_UNKNOWN, "test2");
  58     vfs_register_class (&vfs_test_ops2);
  59 
  60     vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  61     vfs_register_class (&vfs_test_ops3);
  62 
  63 #ifdef HAVE_CHARSET
  64     mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  65     load_codepages_list ();
  66 #endif
  67 }
  68 
  69 /* --------------------------------------------------------------------------------------------- */
  70 
  71 /* @After */
  72 static void
  73 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  74 {
  75 #ifdef HAVE_CHARSET
  76     free_codepages_list ();
  77 #endif
  78 
  79     vfs_shut ();
  80     str_uninit_strings ();
  81 }
  82 
  83 /* --------------------------------------------------------------------------------------------- */
  84 
  85 #ifdef HAVE_CHARSET
  86 #    define ETALON_PATH_STR                                                                        \
  87         "/local/path/#test1:user:pass@some.host:12345/bla-bla/some/path/#test2/#enc:KOI8-R/"       \
  88         "bla-bla/some/path#test3/111/22/33"
  89 #    define ETALON_PATH_URL_STR                                                                    \
  90         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/"     \
  91         "bla-bla/some/path/test3://111/22/33"
  92 #    define ETALON_SERIALIZED_PATH                                                                 \
  93         "g14:path-element-0"                                                                       \
  94         "p4:pathv12:/local/path/"                                                                  \
  95         "p10:class-namev7:localfs"                                                                 \
  96         "g14:path-element-1"                                                                       \
  97         "p4:pathv18:bla-bla/some/path/"                                                            \
  98         "p10:class-namev7:testfs1"                                                                 \
  99         "p10:vfs_prefixv5:test1"                                                                   \
 100         "p4:userv4:user"                                                                           \
 101         "p8:passwordv4:pass"                                                                       \
 102         "p4:hostv9:some.host"                                                                      \
 103         "p4:portv5:12345"                                                                          \
 104         "g14:path-element-2"                                                                       \
 105         "p4:pathv17:bla-bla/some/path"                                                             \
 106         "p10:class-namev7:testfs2"                                                                 \
 107         "p8:encodingv6:KOI8-R"                                                                     \
 108         "p10:vfs_prefixv5:test2"                                                                   \
 109         "g14:path-element-3"                                                                       \
 110         "p4:pathv9:111/22/33"                                                                      \
 111         "p10:class-namev7:testfs3"                                                                 \
 112         "p10:vfs_prefixv5:test3"
 113 #else
 114 #    define ETALON_PATH_STR                                                                        \
 115         "/local/path/#test1:user:pass@some.host:12345/bla-bla/some/path/#test2/bla-bla/some/"      \
 116         "path#test3/111/22/33"
 117 #    define ETALON_PATH_URL_STR                                                                    \
 118         "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/"    \
 119         "path/test3://111/22/33"
 120 #    define ETALON_SERIALIZED_PATH                                                                 \
 121         "g14:path-element-0"                                                                       \
 122         "p4:pathv12:/local/path/"                                                                  \
 123         "p10:class-namev7:localfs"                                                                 \
 124         "g14:path-element-1"                                                                       \
 125         "p4:pathv18:bla-bla/some/path/"                                                            \
 126         "p10:class-namev7:testfs1"                                                                 \
 127         "p10:vfs_prefixv5:test1"                                                                   \
 128         "p4:userv4:user"                                                                           \
 129         "p8:passwordv4:pass"                                                                       \
 130         "p4:hostv9:some.host"                                                                      \
 131         "p4:portv5:12345"                                                                          \
 132         "g14:path-element-2"                                                                       \
 133         "p4:pathv17:bla-bla/some/path"                                                             \
 134         "p10:class-namev7:testfs2"                                                                 \
 135         "p10:vfs_prefixv5:test2"                                                                   \
 136         "g14:path-element-3"                                                                       \
 137         "p4:pathv9:111/22/33"                                                                      \
 138         "p10:class-namev7:testfs3"                                                                 \
 139         "p10:vfs_prefixv5:test3"
 140 #endif
 141 
 142 START_TEST (test_path_serialize)
     /* [previous][next][first][last][top][bottom][index][help]  */
 143 {
 144     // given
 145     vfs_path_t *vpath;
 146     char *serialized_vpath;
 147     GError *error = NULL;
 148 
 149     // when
 150     vpath = vfs_path_from_str_flags (ETALON_PATH_STR, VPF_USE_DEPRECATED_PARSER);
 151     serialized_vpath = vfs_path_serialize (vpath, &error);
 152 
 153     vfs_path_free (vpath, TRUE);
 154 
 155     if (error != NULL)
 156         g_error_free (error);
 157 
 158     // then
 159     mctest_assert_ptr_ne (serialized_vpath, NULL);
 160     mctest_assert_str_eq (serialized_vpath, ETALON_SERIALIZED_PATH);
 161 }
 162 END_TEST
 163 
 164 /* --------------------------------------------------------------------------------------------- */
 165 
 166 START_TEST (test_path_deserialize)
     /* [previous][next][first][last][top][bottom][index][help]  */
 167 {
 168     // given
 169     vfs_path_t *vpath;
 170     GError *error = NULL;
 171 
 172     // when
 173     vpath = vfs_path_deserialize (ETALON_SERIALIZED_PATH, &error);
 174 
 175     // then
 176     mctest_assert_ptr_ne (vpath, NULL);
 177     mctest_assert_str_eq (vfs_path_as_str (vpath), ETALON_PATH_URL_STR);
 178 
 179     vfs_path_free (vpath, TRUE);
 180 }
 181 END_TEST
 182 
 183 /* --------------------------------------------------------------------------------------------- */
 184 
 185 int
 186 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 187 {
 188     TCase *tc_core;
 189 
 190     tc_core = tcase_create ("Core");
 191 
 192     tcase_add_checked_fixture (tc_core, setup, teardown);
 193 
 194     // Add new tests here: ***************
 195     tcase_add_test (tc_core, test_path_serialize);
 196     tcase_add_test (tc_core, test_path_deserialize);
 197     // ***********************************
 198 
 199     return mctest_run_all (tc_core);
 200 }
 201 
 202 /* --------------------------------------------------------------------------------------------- */

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