2011-08-01 00:44:15 +04:00
|
|
|
/* lib/vfs - test vfs_path_t manipulation functions
|
|
|
|
|
2013-01-19 17:42:26 +04:00
|
|
|
Copyright (C) 2011, 2013
|
|
|
|
The Free Software Foundation, Inc.
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
Written by:
|
2013-01-19 17:42:26 +04:00
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2011, 2013
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-01-19 16:05:19 +04:00
|
|
|
*/
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
#define TEST_SUITE_NAME "/lib/vfs"
|
|
|
|
|
2013-01-19 17:42:26 +04:00
|
|
|
#include "tests/mctest.h"
|
2011-08-01 00:44:15 +04:00
|
|
|
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2011-08-01 00:44:15 +04:00
|
|
|
#include "lib/charsets.h"
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
#include "lib/vfs/xdirentry.h"
|
|
|
|
#include "lib/vfs/path.h"
|
|
|
|
|
|
|
|
#include "src/vfs/local/local.c"
|
|
|
|
|
|
|
|
|
|
|
|
struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
|
|
|
|
struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup (void)
|
|
|
|
{
|
|
|
|
|
|
|
|
str_init_strings (NULL);
|
|
|
|
|
|
|
|
vfs_init ();
|
|
|
|
init_localfs ();
|
|
|
|
vfs_setup_work_dir ();
|
|
|
|
|
|
|
|
|
|
|
|
test_subclass1.flags = VFS_S_REMOTE;
|
|
|
|
vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
|
|
|
|
|
|
|
|
vfs_test_ops1.name = "testfs1";
|
|
|
|
vfs_test_ops1.flags = VFSF_NOLINKS;
|
|
|
|
vfs_test_ops1.prefix = "test1";
|
|
|
|
vfs_register_class (&vfs_test_ops1);
|
|
|
|
|
|
|
|
vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
|
|
|
|
vfs_test_ops2.name = "testfs2";
|
|
|
|
vfs_test_ops2.prefix = "test2";
|
|
|
|
vfs_register_class (&vfs_test_ops2);
|
|
|
|
|
|
|
|
vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
|
|
|
|
vfs_test_ops3.name = "testfs3";
|
|
|
|
vfs_test_ops3.prefix = "test3";
|
|
|
|
vfs_test_ops3.flags = VFSF_LOCAL;
|
|
|
|
vfs_register_class (&vfs_test_ops3);
|
|
|
|
|
|
|
|
mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2011-08-01 00:44:15 +04:00
|
|
|
load_codepages_list ();
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-08-01 00:44:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
teardown (void)
|
|
|
|
{
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2011-08-01 00:44:15 +04:00
|
|
|
free_codepages_list ();
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
vfs_shut ();
|
|
|
|
str_uninit_strings ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-08-01 00:44:15 +04:00
|
|
|
START_TEST (test_vfs_path_tokens_count)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-08-01 00:44:15 +04:00
|
|
|
{
|
|
|
|
size_t tokens_count;
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str ("/");
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 0, "actual: %zu; expected: 0\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str ("/path");
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 1, "actual: %zu; expected: 1\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str ("/path1/path2/path3");
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str_flags ("test3://path1/path2/path3/path4", VPF_NO_CANON);
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 4, "actual: %zu; expected: 4\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str_flags ("path1/path2/path3", VPF_NO_CANON);
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str ("/path1/path2/path3/");
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str ("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
|
2013-01-19 16:05:19 +04:00
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 5, "actual: %zu; expected: 5\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath =
|
|
|
|
vfs_path_from_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");
|
|
|
|
tokens_count = vfs_path_tokens_count (vpath);
|
2011-08-01 00:44:15 +04:00
|
|
|
fail_unless (tokens_count == 11, "actual: %zu; expected: 11\n", tokens_count);
|
|
|
|
vfs_path_free (vpath);
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-08-01 00:44:15 +04:00
|
|
|
}
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-08-01 00:44:15 +04:00
|
|
|
END_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define check_invalid_token_str(input, start, length) { \
|
|
|
|
vpath = vfs_path_from_str (input); \
|
|
|
|
path_tokens = vfs_path_tokens_get(vpath, start, length); \
|
|
|
|
fail_unless (path_tokens == NULL, "path_tokens should be NULL!\n"); \
|
|
|
|
g_free (path_tokens); \
|
|
|
|
vfs_path_free (vpath); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define check_token_str(input, start, length, etalon) { \
|
|
|
|
vpath = vfs_path_from_str_flags (input, VPF_NO_CANON); \
|
|
|
|
path_tokens = vfs_path_tokens_get(vpath, start, length); \
|
|
|
|
fail_unless (path_tokens != NULL, "path_tokens shouldn't equal to NULL!\n"); \
|
|
|
|
if (path_tokens != NULL) \
|
|
|
|
fail_unless (strcmp(path_tokens, etalon) == 0, "\nactual: '%s'\netalon: '%s'", path_tokens, etalon); \
|
|
|
|
g_free (path_tokens); \
|
|
|
|
vfs_path_free (vpath); \
|
|
|
|
}
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-08-01 00:44:15 +04:00
|
|
|
START_TEST (test_vfs_path_tokens_get)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-08-01 00:44:15 +04:00
|
|
|
{
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
char *path_tokens;
|
|
|
|
|
|
|
|
/* Invalid start position */
|
2013-01-19 16:05:19 +04:00
|
|
|
check_invalid_token_str ("/", 2, 1);
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* Invalid negative position */
|
2013-01-19 16:05:19 +04:00
|
|
|
check_invalid_token_str ("/path", -3, 1);
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* Count of tokens is zero. Count should be autocorrected */
|
|
|
|
check_token_str ("/path", 0, 0, "path");
|
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 */
|
|
|
|
check_token_str ("/path1/path2/path3/path4", 1, 2, "path2/path3");
|
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 from LOCAL VFS */
|
|
|
|
check_token_str ("test3://path1/path2/path3/path4", 1, 2, "path2/path3");
|
|
|
|
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
/* get 'path2/path3' by 1,2 from LOCAL VFS with encoding */
|
2013-01-19 16:05:19 +04:00
|
|
|
check_token_str ("test3://path1/path2/test3://#enc:KOI8-R/path3/path4", 1, 2,
|
|
|
|
"path2/test3://#enc:KOI8-R/path3");
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 with encoding */
|
|
|
|
check_token_str ("#enc:KOI8-R/path1/path2/path3/path4", 1, 2, "#enc:KOI8-R/path2/path3");
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 from non-LOCAL VFS */
|
|
|
|
check_token_str ("test2://path1/path2/path3/path4", 1, 2, "test2://path2/path3");
|
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 throught non-LOCAL VFS */
|
2013-01-19 16:05:19 +04:00
|
|
|
check_token_str ("/path1/path2/test1://user:pass@some.host:12345/path3/path4", 1, 2,
|
|
|
|
"path2/test1://user:pass@some.host:12345/path3");
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 from LOCAL VFS */
|
|
|
|
/* TODO: currently this test don't passed. Probably broken string URI parser */
|
2013-01-19 16:05:19 +04:00
|
|
|
/* check_token_str ("test3://path1/path2/test2://test3://path3/path4", 1, 2, "path2/path3"); */
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 where path2 it's LOCAL VFS */
|
|
|
|
check_token_str ("test3://path1/path2/test2://path3/path4", 1, 2, "path2/test2://path3");
|
|
|
|
|
|
|
|
/* get 'path2/path3' by 1,2 where path3 it's LOCAL VFS */
|
2013-01-19 16:05:19 +04:00
|
|
|
check_token_str ("test2://path1/path2/test3://path3/path4", 1, 2,
|
|
|
|
"test2://path2/test3://path3");
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* get 'path4' by -1,1 */
|
|
|
|
check_token_str ("/path1/path2/path3/path4", -1, 1, "path4");
|
|
|
|
|
|
|
|
/* get 'path2/path3/path4' by -3,0 */
|
|
|
|
check_token_str ("/path1/path2/path3/path4", -3, 0, "path2/path3/path4");
|
|
|
|
|
|
|
|
}
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-08-01 00:44:15 +04:00
|
|
|
END_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-08-01 00:44:15 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-09-25 15:36:44 +04:00
|
|
|
START_TEST (test_vfs_path_append_vpath)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-09-25 15:36:44 +04:00
|
|
|
{
|
|
|
|
vfs_path_t *vpath1, *vpath2, *vpath3;
|
|
|
|
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath1 =
|
|
|
|
vfs_path_from_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");
|
2012-06-07 15:17:18 +04:00
|
|
|
#else
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath1 =
|
|
|
|
vfs_path_from_str
|
|
|
|
("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33");
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath2 = vfs_path_from_str ("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
|
2011-09-25 15:36:44 +04:00
|
|
|
|
|
|
|
vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL);
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
fail_unless (vfs_path_elements_count (vpath3) == 6,
|
|
|
|
"\nvpath elements count should be %d, actial is %d\n",
|
|
|
|
6, vfs_path_elements_count (vpath3));
|
2011-09-25 15:36:44 +04:00
|
|
|
vfs_path_free (vpath1);
|
|
|
|
vfs_path_free (vpath2);
|
|
|
|
vfs_path_free (vpath3);
|
|
|
|
|
|
|
|
}
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-09-25 15:36:44 +04:00
|
|
|
END_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-09-25 15:36:44 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-08-01 00:44:15 +04:00
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2012-02-07 15:54:58 +04:00
|
|
|
START_TEST (test_vfs_path_relative)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2012-02-07 15:54:58 +04:00
|
|
|
{
|
|
|
|
vfs_path_t *vpath, *copy_vpath;
|
|
|
|
char *path_str;
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath = vfs_path_from_str_flags ("../bla-bla", VPF_NO_CANON);
|
2012-02-07 15:54:58 +04:00
|
|
|
fail_unless (vpath->relative, "relative flag fail!\n");
|
|
|
|
|
|
|
|
path_str = vfs_path_to_str (vpath);
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
fail_unless (strcmp (path_str, "../bla-bla") == 0, "relative fail!\nactual: [%s]\n", path_str);
|
2012-02-07 15:54:58 +04:00
|
|
|
g_free (path_str);
|
|
|
|
|
|
|
|
path_str = (char *) vfs_path_get_last_path_str (vpath);
|
2013-01-19 16:05:19 +04:00
|
|
|
fail_unless (strcmp (path_str, "../bla-bla") == 0,
|
|
|
|
"relative fail!\nactual: element->path=[%s]\n", path_str);
|
2012-02-07 15:54:58 +04:00
|
|
|
|
|
|
|
copy_vpath = vfs_path_clone (vpath);
|
|
|
|
|
|
|
|
path_str = vfs_path_to_str (copy_vpath);
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
fail_unless (strcmp (path_str, "../bla-bla") == 0, "relative fail!\nactual: [%s]\n", path_str);
|
2012-02-07 15:54:58 +04:00
|
|
|
g_free (path_str);
|
|
|
|
|
|
|
|
vfs_path_free (copy_vpath);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath =
|
|
|
|
vfs_path_from_str_flags ("../path/test1://user:pass@some.host:12345/bla-bla/some/path/",
|
|
|
|
VPF_NO_CANON);
|
2012-02-07 15:54:58 +04:00
|
|
|
path_str = vfs_path_to_str (vpath);
|
2013-01-19 16:05:19 +04:00
|
|
|
fail_unless (strcmp (path_str, "../path/test1://user:pass@some.host:12345/bla-bla/some/path/")
|
|
|
|
== 0, "relative fail!\nactual: [%s]\n", path_str);
|
2012-02-07 15:54:58 +04:00
|
|
|
g_free (path_str);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
|
|
|
|
}
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2012-02-07 15:54:58 +04:00
|
|
|
END_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2012-02-07 15:54:58 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2011-08-01 00:44:15 +04:00
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
|
|
|
int number_failed;
|
|
|
|
|
|
|
|
Suite *s = suite_create (TEST_SUITE_NAME);
|
|
|
|
TCase *tc_core = tcase_create ("Core");
|
|
|
|
SRunner *sr;
|
|
|
|
|
|
|
|
tcase_add_checked_fixture (tc_core, setup, teardown);
|
|
|
|
|
|
|
|
/* Add new tests here: *************** */
|
|
|
|
tcase_add_test (tc_core, test_vfs_path_tokens_count);
|
|
|
|
tcase_add_test (tc_core, test_vfs_path_tokens_get);
|
2011-09-25 15:36:44 +04:00
|
|
|
tcase_add_test (tc_core, test_vfs_path_append_vpath);
|
2012-02-07 15:54:58 +04:00
|
|
|
tcase_add_test (tc_core, test_vfs_path_relative);
|
2011-08-01 00:44:15 +04:00
|
|
|
/* *********************************** */
|
|
|
|
|
|
|
|
suite_add_tcase (s, tc_core);
|
|
|
|
sr = srunner_create (s);
|
|
|
|
srunner_set_log (sr, "path_manipulations.log");
|
|
|
|
srunner_run_all (sr, CK_NORMAL);
|
|
|
|
number_failed = srunner_ntests_failed (sr);
|
|
|
|
srunner_free (sr);
|
|
|
|
return (number_failed == 0) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|