2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
lib/vfs - get vfs_path_t from string
|
2011-04-26 15:24:17 +04:00
|
|
|
|
2016-01-01 09:53:15 +03:00
|
|
|
Copyright (C) 2011-2016
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2011-04-26 15:24:17 +04:00
|
|
|
|
|
|
|
Written by:
|
2013-01-19 17:42:26 +04:00
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2011, 2013
|
2011-04-26 15:24:17 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2011-04-26 15:24:17 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2011-04-26 15:24:17 +04:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-10-15 14:56:47 +04:00
|
|
|
GNU General Public License for more details.
|
2011-04-26 15:24:17 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-01-19 16:05:19 +04:00
|
|
|
*/
|
2011-04-26 15:24:17 +04:00
|
|
|
|
|
|
|
#define TEST_SUITE_NAME "/lib/vfs"
|
|
|
|
|
2013-01-19 17:42:26 +04:00
|
|
|
#include "tests/mctest.h"
|
2011-05-30 17:54:45 +04:00
|
|
|
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2011-05-30 17:54:45 +04:00
|
|
|
#include "lib/charsets.h"
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-04-26 15:24:17 +04:00
|
|
|
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
#include "lib/vfs/xdirentry.h"
|
2013-01-19 16:05:19 +04:00
|
|
|
#include "lib/vfs/path.c" /* for testing static methods */
|
2011-04-26 15:24:17 +04:00
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
#define ETALON_PATH_STR "/#test1/bla-bla/some/path/#test2/bla-bla/some/path#test3/111/22/33"
|
|
|
|
#define ETALON_PATH_URL_STR "/test1://bla-bla/some/path/test2://bla-bla/some/path/test3://111/22/33"
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* @Before */
|
2011-04-26 15:24:17 +04:00
|
|
|
static void
|
|
|
|
setup (void)
|
|
|
|
{
|
|
|
|
str_init_strings (NULL);
|
|
|
|
|
|
|
|
vfs_init ();
|
|
|
|
init_localfs ();
|
|
|
|
vfs_setup_work_dir ();
|
|
|
|
|
|
|
|
vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
|
|
|
|
|
|
|
|
vfs_test_ops1.name = "testfs1";
|
|
|
|
vfs_test_ops1.flags = VFSF_NOLINKS;
|
2011-06-14 12:28:51 +04:00
|
|
|
vfs_test_ops1.prefix = "test1";
|
2011-04-26 15:24:17 +04:00
|
|
|
vfs_register_class (&vfs_test_ops1);
|
|
|
|
|
2011-06-14 12:55:27 +04:00
|
|
|
test_subclass2.flags = VFS_S_REMOTE;
|
2011-04-26 15:24:17 +04:00
|
|
|
vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
|
|
|
|
vfs_test_ops2.name = "testfs2";
|
2011-06-14 12:28:51 +04:00
|
|
|
vfs_test_ops2.prefix = "test2";
|
2011-04-26 15:24:17 +04:00
|
|
|
vfs_register_class (&vfs_test_ops2);
|
|
|
|
|
|
|
|
vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
|
|
|
|
vfs_test_ops3.name = "testfs3";
|
2011-06-14 12:28:51 +04:00
|
|
|
vfs_test_ops3.prefix = "test3";
|
2011-04-26 15:24:17 +04:00
|
|
|
vfs_register_class (&vfs_test_ops3);
|
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
|
|
|
|
load_codepages_list ();
|
|
|
|
#endif /* HAVE_CHARSET */
|
2011-04-26 15:24:17 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* @After */
|
2011-04-26 15:24:17 +04:00
|
|
|
static void
|
|
|
|
teardown (void)
|
|
|
|
{
|
2013-01-19 19:56:40 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
free_codepages_list ();
|
|
|
|
#endif /* HAVE_CHARSET */
|
|
|
|
|
2011-04-26 15:24:17 +04:00
|
|
|
vfs_shut ();
|
2011-06-04 16:12:24 +04:00
|
|
|
str_uninit_strings ();
|
2011-04-26 15:24:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2013-01-19 19:56:40 +04:00
|
|
|
/* @DataSource("test_from_to_string_ds") */
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2013-01-19 19:56:40 +04:00
|
|
|
static const struct test_from_to_string_ds
|
2011-04-26 15:24:17 +04:00
|
|
|
{
|
2013-01-19 19:56:40 +04:00
|
|
|
const char *input_string;
|
|
|
|
const char *expected_result;
|
|
|
|
const char *expected_element_path;
|
|
|
|
const size_t expected_elements_count;
|
|
|
|
struct vfs_class *expected_vfs_class;
|
|
|
|
} test_from_to_string_ds[] =
|
|
|
|
{
|
|
|
|
{ /* 0. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
ETALON_PATH_URL_STR,
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
{ /* 1. */
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
1,
|
|
|
|
&vfs_local_ops
|
|
|
|
},
|
|
|
|
{ /* 2. */
|
|
|
|
"/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
|
|
|
|
"/test1://bla-bla/some/path/test2://user:passwd@some.host:1234/bla-bla/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
{ /* 3. */
|
|
|
|
"/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
|
|
|
|
"/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
{ /* 4. */
|
|
|
|
"/#test1/bla-bla1/#enc:IBM866/some/path/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
|
|
|
|
"/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
{ /* 5. */
|
|
|
|
"/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/#enc:KOI8-R/some/path#test3/111/22/33",
|
|
|
|
"/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
{ /* 6. */
|
|
|
|
"/#test1/bla-bla1/some/path/#test2/bla-bla2/#enc:IBM866/some/#enc:KOI8-R/path#test3/111/22/33",
|
|
|
|
"/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
{ /* 7. */
|
|
|
|
"/#test1/bla-bla1/some/path/#test2/#enc:IBM866/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
|
|
|
|
"/test1://bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
{ /* 8. */
|
|
|
|
"/#test1/bla-bla1/some/path/#enc:IBM866/#test2/bla-bla2/#enc:KOI8-R/some/path#test3/111/22/33",
|
|
|
|
"/test1://#enc:IBM866/bla-bla1/some/path/test2://#enc:KOI8-R/bla-bla2/some/path/test3://111/22/33",
|
|
|
|
"111/22/33",
|
|
|
|
4,
|
|
|
|
&vfs_test_ops3
|
|
|
|
},
|
|
|
|
#endif /* HAVE_CHARSET */
|
|
|
|
};
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-04-26 15:24:17 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* @Test */
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2013-01-19 19:56:40 +04:00
|
|
|
START_PARAMETRIZED_TEST (test_from_to_string, test_from_to_string_ds)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-04-29 15:13:29 +04:00
|
|
|
{
|
2013-01-19 19:56:40 +04:00
|
|
|
/* given */
|
2011-04-29 15:13:29 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
size_t vpath_len;
|
2012-01-03 13:35:57 +04:00
|
|
|
const vfs_path_element_t *path_element;
|
2013-06-20 15:24:34 +04:00
|
|
|
const char *actual_result;
|
2011-04-29 15:13:29 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
|
2011-04-29 15:13:29 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* when */
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath_len = vfs_path_elements_count (vpath);
|
2013-04-14 17:38:37 +04:00
|
|
|
actual_result = vfs_path_as_str (vpath);
|
2011-04-29 15:13:29 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* then */
|
|
|
|
mctest_assert_int_eq (vpath_len, data->expected_elements_count);
|
|
|
|
mctest_assert_str_eq (actual_result, data->expected_result);
|
|
|
|
mctest_assert_ptr_eq (path_element->class, data->expected_vfs_class);
|
|
|
|
mctest_assert_str_eq (path_element->path, data->expected_element_path);
|
2011-04-29 15:13:29 +04:00
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-04-29 15:13:29 +04:00
|
|
|
}
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2013-01-19 19:56:40 +04:00
|
|
|
END_PARAMETRIZED_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-04-29 15:13:29 +04:00
|
|
|
|
2011-04-26 15:24:17 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 18:27:05 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* @DataSource("test_partial_string_by_index_ds") */
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2013-01-19 19:56:40 +04:00
|
|
|
static const struct test_partial_string_by_index_ds
|
|
|
|
{
|
|
|
|
const char *input_string;
|
|
|
|
const off_t element_index;
|
|
|
|
const char *expected_result;
|
|
|
|
} test_partial_string_by_index_ds[] =
|
|
|
|
{
|
|
|
|
{ /* 0. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
-1,
|
|
|
|
"/test1://bla-bla/some/path/test2://bla-bla/some/path"
|
|
|
|
},
|
|
|
|
{ /* 1. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
-2,
|
|
|
|
"/test1://bla-bla/some/path/"
|
|
|
|
},
|
|
|
|
{ /* 2. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
-3,
|
|
|
|
"/"
|
|
|
|
},
|
|
|
|
{ /* 3. Index out of bound */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
-4,
|
|
|
|
""
|
|
|
|
},
|
|
|
|
{ /* 4. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
1,
|
|
|
|
"/"
|
|
|
|
},
|
|
|
|
{ /* 5. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
2,
|
|
|
|
"/test1://bla-bla/some/path/"
|
|
|
|
},
|
|
|
|
{ /* 6. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
3,
|
|
|
|
"/test1://bla-bla/some/path/test2://bla-bla/some/path"
|
|
|
|
},
|
|
|
|
{ /* 6. */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
4,
|
|
|
|
ETALON_PATH_URL_STR
|
|
|
|
},
|
|
|
|
{ /* 7. Index out of bound */
|
|
|
|
ETALON_PATH_STR,
|
|
|
|
5,
|
|
|
|
ETALON_PATH_URL_STR
|
|
|
|
},
|
|
|
|
};
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* @Test(dataSource = "test_partial_string_by_index_ds") */
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2013-01-19 19:56:40 +04:00
|
|
|
START_PARAMETRIZED_TEST (test_partial_string_by_index, test_partial_string_by_index_ds)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-05-30 17:54:45 +04:00
|
|
|
{
|
2013-01-19 19:56:40 +04:00
|
|
|
/* given */
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
char *actual_result;
|
|
|
|
vpath = vfs_path_from_str_flags (data->input_string, VPF_USE_DEPRECATED_PARSER);
|
2011-05-30 17:54:45 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* when */
|
|
|
|
actual_result = vfs_path_to_str_elements_count (vpath, data->element_index);
|
2011-05-30 17:54:45 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* then */
|
|
|
|
mctest_assert_str_eq (actual_result, data->expected_result);
|
|
|
|
g_free (actual_result);
|
2011-05-30 17:54:45 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-05-30 17:54:45 +04:00
|
|
|
}
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2013-01-19 19:56:40 +04:00
|
|
|
END_PARAMETRIZED_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
2011-06-02 14:20:31 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2013-01-19 19:56:40 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2011-06-14 16:09:59 +04:00
|
|
|
#define ETALON_STR "/path/to/file.ext/test1://#enc:KOI8-R"
|
2013-01-19 19:56:40 +04:00
|
|
|
/* @Test */
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-06-02 14:20:31 +04:00
|
|
|
START_TEST (test_vfs_path_encoding_at_end)
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
2011-06-02 14:20:31 +04:00
|
|
|
{
|
2013-01-19 19:56:40 +04:00
|
|
|
/* given */
|
2011-06-02 14:20:31 +04:00
|
|
|
vfs_path_t *vpath;
|
2013-04-14 17:38:37 +04:00
|
|
|
const char *result;
|
2012-01-03 13:35:57 +04:00
|
|
|
const vfs_path_element_t *element;
|
2011-06-02 14:20:31 +04:00
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
vpath =
|
|
|
|
vfs_path_from_str_flags ("/path/to/file.ext#test1:/#enc:KOI8-R", VPF_USE_DEPRECATED_PARSER);
|
2011-06-02 14:20:31 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* when */
|
2013-04-14 17:38:37 +04:00
|
|
|
result = vfs_path_as_str (vpath);
|
2013-01-19 16:05:19 +04:00
|
|
|
element = vfs_path_get_by_index (vpath, -1);
|
2011-06-02 14:20:31 +04:00
|
|
|
|
2013-01-19 19:56:40 +04:00
|
|
|
/* then */
|
|
|
|
mctest_assert_str_eq (element->path, "");
|
|
|
|
mctest_assert_not_null (element->encoding);
|
|
|
|
mctest_assert_str_eq (result, ETALON_STR);
|
2011-06-02 14:20:31 +04:00
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-06-02 14:20:31 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-OFF* */
|
2011-05-30 17:54:45 +04:00
|
|
|
END_TEST
|
2013-01-19 16:05:19 +04:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
#endif /* HAVE_CHARSET */
|
2011-05-30 17:54:45 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-26 15:24:17 +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: *************** */
|
2013-01-19 19:56:40 +04:00
|
|
|
mctest_add_parameterized_test (tc_core, test_from_to_string, test_from_to_string_ds);
|
|
|
|
mctest_add_parameterized_test (tc_core, test_partial_string_by_index,
|
|
|
|
test_partial_string_by_index_ds);
|
2012-06-07 15:17:18 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2011-06-02 14:20:31 +04:00
|
|
|
tcase_add_test (tc_core, test_vfs_path_encoding_at_end);
|
2012-06-07 15:17:18 +04:00
|
|
|
#endif
|
2011-04-26 15:24:17 +04:00
|
|
|
/* *********************************** */
|
|
|
|
|
|
|
|
suite_add_tcase (s, tc_core);
|
|
|
|
sr = srunner_create (s);
|
2011-06-02 14:20:31 +04:00
|
|
|
srunner_set_log (sr, "vfs_path_string_convert.log");
|
2015-04-07 11:52:58 +03:00
|
|
|
srunner_run_all (sr, CK_ENV);
|
2011-04-26 15:24:17 +04:00
|
|
|
number_failed = srunner_ntests_failed (sr);
|
|
|
|
srunner_free (sr);
|
2015-04-07 11:52:58 +03:00
|
|
|
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
2011-04-26 15:24:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|