vfs_path_to_str() now return URL string instead of old representation
[midnight-commander.git] / lib / tests / vfs / path_serialize.c
blobf86ddb9857c0a4e5e61ea4767b25c2253eef51ae
1 /* lib/vfs - vfs_path_t serialize/deserialize functions
3 Copyright (C) 2011 Free Software Foundation, Inc.
5 Written by:
6 Slava Zanko <slavazanko@gmail.com>, 2011
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define TEST_SUITE_NAME "/lib/vfs"
25 #include <check.h>
27 #include "lib/global.c"
29 #ifndef HAVE_CHARSET
30 #define HAVE_CHARSET 1
31 #endif
33 #include "lib/charsets.h"
35 #include "lib/strutil.h"
36 #include "lib/vfs/xdirentry.h"
37 #include "lib/vfs/path.h"
39 #include "src/vfs/local/local.c"
42 struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
43 struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
45 static void
46 setup (void)
49 str_init_strings (NULL);
51 vfs_init ();
52 init_localfs ();
53 vfs_setup_work_dir ();
56 test_subclass1.flags = VFS_S_REMOTE;
57 vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
59 vfs_test_ops1.name = "testfs1";
60 vfs_test_ops1.flags = VFSF_NOLINKS;
61 vfs_test_ops1.prefix = "test1";
62 vfs_register_class (&vfs_test_ops1);
64 vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
65 vfs_test_ops2.name = "testfs2";
66 vfs_test_ops2.prefix = "test2";
67 vfs_register_class (&vfs_test_ops2);
69 vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
70 vfs_test_ops3.name = "testfs3";
71 vfs_test_ops3.prefix = "test3";
72 vfs_register_class (&vfs_test_ops3);
74 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
75 load_codepages_list ();
78 static void
79 teardown (void)
81 free_codepages_list ();
83 vfs_shut ();
84 str_uninit_strings ();
87 /* --------------------------------------------------------------------------------------------- */
88 #define ETALON_PATH_STR "/local/path/#test1:user:pass@some.host:12345/bla-bla/some/path/#test2/#enc:KOI8-R/bla-bla/some/path#test3/111/22/33"
89 #define ETALON_PATH_URL_STR "/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
90 #define ETALON_SERIALIZED_PATH \
91 "g14:path-element-0" \
92 "p4:pathv12:/local/path/" \
93 "p10:class-namev7:localfs" \
94 "g14:path-element-1" \
95 "p4:pathv18:bla-bla/some/path/" \
96 "p10:class-namev7:testfs1" \
97 "p10:vfs_prefixv5:test1" \
98 "p4:userv4:user" \
99 "p8:passwordv4:pass" \
100 "p4:hostv9:some.host" \
101 "p4:portv5:12345" \
102 "g14:path-element-2" \
103 "p4:pathv17:bla-bla/some/path" \
104 "p10:class-namev7:testfs2" \
105 "p8:encodingv6:KOI8-R" \
106 "p10:vfs_prefixv5:test2" \
107 "g14:path-element-3" \
108 "p4:pathv9:111/22/33" \
109 "p10:class-namev7:testfs3" \
110 "p10:vfs_prefixv5:test3"
112 START_TEST (test_path_serialize_deserialize)
114 vfs_path_t *vpath;
115 char *serialized_vpath;
116 GError *error = NULL;
118 vpath = vfs_path_from_str (ETALON_PATH_STR);
119 serialized_vpath = vfs_path_serialize (vpath, &error);
120 vfs_path_free (vpath);
122 if (serialized_vpath == NULL)
124 fail ("serialized_vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
125 g_clear_error (&error);
126 return;
129 fail_unless (
130 strcmp (serialized_vpath, ETALON_SERIALIZED_PATH ) == 0,
131 "\nserialized_vpath (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_SERIALIZED_PATH
134 vpath = vfs_path_deserialize (serialized_vpath, &error);
135 g_free (serialized_vpath);
137 if (vpath == NULL)
139 fail ("vpath is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
140 g_clear_error (&error);
141 return;
144 serialized_vpath = vfs_path_to_str (vpath);
145 fail_unless (
146 strcmp (serialized_vpath, ETALON_PATH_URL_STR) == 0,
147 "\ndeserialized path (%s)\nnot equal to etalon (%s)", serialized_vpath, ETALON_PATH_URL_STR
149 vfs_path_free(vpath);
150 g_free(serialized_vpath);
153 END_TEST
155 /* --------------------------------------------------------------------------------------------- */
158 main (void)
160 int number_failed;
162 Suite *s = suite_create (TEST_SUITE_NAME);
163 TCase *tc_core = tcase_create ("Core");
164 SRunner *sr;
166 tcase_add_checked_fixture (tc_core, setup, teardown);
168 /* Add new tests here: *************** */
169 tcase_add_test (tc_core, test_path_serialize_deserialize);
170 /* *********************************** */
172 suite_add_tcase (s, tc_core);
173 sr = srunner_create (s);
174 srunner_set_log (sr, "path_serialize.log");
175 srunner_run_all (sr, CK_NORMAL);
176 number_failed = srunner_ntests_failed (sr);
177 srunner_free (sr);
178 return (number_failed == 0) ? 0 : 1;
181 /* --------------------------------------------------------------------------------------------- */