2020-02-06 12:44:16 +00:00
|
|
|
#include "common/common.h"
|
2023-02-26 03:50:08 +00:00
|
|
|
#include "common/msg.h"
|
2020-02-06 12:44:16 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "options/path.h"
|
2023-02-26 03:50:08 +00:00
|
|
|
#include "test_utils.h"
|
2020-02-06 12:44:16 +00:00
|
|
|
|
|
|
|
static void test_join(char *file, int line, char *a, char *b, char *c)
|
|
|
|
{
|
|
|
|
char *res = mp_path_join(NULL, a, b);
|
2024-06-26 16:01:23 +00:00
|
|
|
assert_string_equal_impl(file, line, res, c);
|
2020-02-06 12:44:16 +00:00
|
|
|
talloc_free(res);
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:14:35 +00:00
|
|
|
|
2024-05-05 16:11:00 +00:00
|
|
|
static void test_normalize(char *file, int line, char *expected, char *path)
|
|
|
|
{
|
|
|
|
void *ctx = talloc_new(NULL);
|
|
|
|
char *normalized = mp_normalize_path(ctx, path);
|
2024-06-26 16:01:23 +00:00
|
|
|
assert_string_equal_impl(file, line, normalized, expected);
|
2024-05-05 16:11:00 +00:00
|
|
|
talloc_free(ctx);
|
|
|
|
}
|
|
|
|
|
2024-06-26 16:02:37 +00:00
|
|
|
static void test_dirname(char *file, int line, char *path, char *expected)
|
|
|
|
{
|
|
|
|
char *res = bstrto0(NULL, mp_dirname(path));
|
|
|
|
assert_string_equal_impl(file, line, res, expected);
|
|
|
|
talloc_free(res);
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:44:16 +00:00
|
|
|
#define TEST_JOIN(a, b, c) \
|
|
|
|
test_join(__FILE__, __LINE__, a, b, c);
|
|
|
|
|
2020-02-06 13:14:35 +00:00
|
|
|
#define TEST_ABS(abs, a) \
|
2024-06-26 16:01:23 +00:00
|
|
|
assert_int_equal_impl(__FILE__, __LINE__, abs, mp_path_is_absolute(bstr0(a)))
|
2020-02-06 13:14:35 +00:00
|
|
|
|
2024-05-05 16:11:00 +00:00
|
|
|
#define TEST_NORMALIZE(expected, path) \
|
|
|
|
test_normalize(__FILE__, __LINE__, expected, path)
|
|
|
|
|
2024-06-26 16:02:37 +00:00
|
|
|
#define TEST_BASENAME(path, expected) \
|
|
|
|
assert_string_equal_impl(__FILE__, __LINE__, mp_basename(path), expected)
|
|
|
|
|
|
|
|
#define TEST_DIRNAME(path, expected) \
|
|
|
|
test_dirname(__FILE__, __LINE__, path, expected)
|
|
|
|
|
2023-02-26 03:50:08 +00:00
|
|
|
int main(void)
|
2020-02-06 12:44:16 +00:00
|
|
|
{
|
2020-02-06 13:14:35 +00:00
|
|
|
TEST_ABS(true, "/ab");
|
|
|
|
TEST_ABS(false, "ab");
|
2020-02-06 12:44:16 +00:00
|
|
|
TEST_JOIN("", "", "");
|
|
|
|
TEST_JOIN("a", "", "a");
|
|
|
|
TEST_JOIN("/a", "", "/a");
|
|
|
|
TEST_JOIN("", "b", "b");
|
|
|
|
TEST_JOIN("", "/b", "/b");
|
|
|
|
TEST_JOIN("ab", "cd", "ab/cd");
|
|
|
|
TEST_JOIN("ab/", "cd", "ab/cd");
|
|
|
|
TEST_JOIN("ab/", "/cd", "/cd");
|
|
|
|
// Note: we prefer "/" on win32, but tolerate "\".
|
|
|
|
#if HAVE_DOS_PATHS
|
2020-02-06 13:14:35 +00:00
|
|
|
TEST_ABS(true, "\\ab");
|
|
|
|
TEST_ABS(true, "c:\\");
|
|
|
|
TEST_ABS(true, "c:/");
|
|
|
|
TEST_ABS(false, "c:");
|
|
|
|
TEST_ABS(false, "c:a");
|
|
|
|
TEST_ABS(false, "c:a\\");
|
2020-02-06 12:44:16 +00:00
|
|
|
TEST_JOIN("ab\\", "cd", "ab\\cd");
|
|
|
|
TEST_JOIN("ab\\", "\\cd", "\\cd");
|
|
|
|
TEST_JOIN("c:/", "de", "c:/de");
|
|
|
|
TEST_JOIN("c:/a", "de", "c:/a/de");
|
|
|
|
TEST_JOIN("c:\\a", "c:\\b", "c:\\b");
|
|
|
|
TEST_JOIN("c:/a", "c:/b", "c:/b");
|
|
|
|
// Note: drive-relative paths are not always supported "properly"
|
2020-02-06 13:06:53 +00:00
|
|
|
TEST_JOIN("c:/a", "d:b", "c:/a/d:b");
|
2020-02-06 12:44:16 +00:00
|
|
|
TEST_JOIN("c:a", "b", "c:a/b");
|
|
|
|
TEST_JOIN("c:", "b", "c:b");
|
|
|
|
#endif
|
2024-05-05 16:11:00 +00:00
|
|
|
|
|
|
|
TEST_NORMALIZE("https://foo", "https://foo");
|
2024-05-05 16:50:38 +00:00
|
|
|
#if !HAVE_DOS_PATHS
|
2024-05-05 16:11:00 +00:00
|
|
|
TEST_NORMALIZE("/foo", "/foo");
|
2024-05-05 16:50:38 +00:00
|
|
|
#endif
|
2024-05-05 16:11:00 +00:00
|
|
|
|
2024-05-21 23:32:08 +00:00
|
|
|
#if defined(_WIN32) && (!defined(HAVE_PATHCCH) || !HAVE_PATHCCH)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2024-05-05 16:11:00 +00:00
|
|
|
void *ctx = talloc_new(NULL);
|
|
|
|
bstr dst = bstr0(mp_getcwd(ctx));
|
|
|
|
bstr_xappend(ctx, &dst, bstr0("/foo"));
|
2024-05-05 16:50:38 +00:00
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
char *p = dst.start;
|
|
|
|
while (*p) {
|
|
|
|
*p = *p == '/' ? '\\' : *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
#endif
|
2024-05-05 16:11:00 +00:00
|
|
|
TEST_NORMALIZE(dst.start, "foo");
|
|
|
|
talloc_free(ctx);
|
|
|
|
|
2024-05-05 16:50:38 +00:00
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
TEST_NORMALIZE("C:\\foo\\baz", "C:/foo/bar/../baz");
|
|
|
|
TEST_NORMALIZE("C:\\", "C:/foo/../..");
|
|
|
|
TEST_NORMALIZE("C:\\foo\\baz", "C:/foo/bar/./../baz");
|
|
|
|
TEST_NORMALIZE("C:\\foo\\bar\\baz", "C:/foo//bar/./baz");
|
|
|
|
TEST_NORMALIZE("C:\\foo\\bar\\baz", "C:/foo\\./bar\\/baz");
|
|
|
|
TEST_NORMALIZE("C:\\file.mkv", "\\\\?\\C:\\folder\\..\\file.mkv");
|
|
|
|
TEST_NORMALIZE("C:\\dir", "\\\\?\\C:\\dir\\subdir\\..\\.");
|
|
|
|
TEST_NORMALIZE("D:\\newfile.txt", "\\\\?\\D:\\\\new\\subdir\\..\\..\\newfile.txt");
|
|
|
|
TEST_NORMALIZE("\\\\server\\share\\path", "\\\\?\\UNC/server/share/path/.");
|
|
|
|
TEST_NORMALIZE("C:\\", "C:/.");
|
|
|
|
TEST_NORMALIZE("C:\\", "C:/../");
|
|
|
|
#else
|
2024-05-05 16:11:00 +00:00
|
|
|
TEST_NORMALIZE("/foo/bar", "/foo//bar");
|
|
|
|
TEST_NORMALIZE("/foo/bar", "/foo///bar");
|
|
|
|
TEST_NORMALIZE("/foo/bar", "/foo/bar/");
|
|
|
|
TEST_NORMALIZE("/foo/bar", "/foo/./bar");
|
|
|
|
TEST_NORMALIZE("/usr", "/usr/bin/..");
|
|
|
|
#endif
|
|
|
|
|
2024-06-26 16:02:37 +00:00
|
|
|
TEST_BASENAME("/usr/local/bin", "bin");
|
|
|
|
TEST_BASENAME("/usr/local/", "");
|
|
|
|
TEST_BASENAME("/usr/", "");
|
|
|
|
TEST_BASENAME("/", "");
|
|
|
|
TEST_BASENAME("usr/local/bin", "bin");
|
|
|
|
TEST_BASENAME("usr/local/", "");
|
|
|
|
TEST_BASENAME("usr/", "");
|
|
|
|
TEST_BASENAME("usr", "usr");
|
|
|
|
TEST_BASENAME("", "");
|
|
|
|
TEST_BASENAME(".", ".");
|
|
|
|
TEST_BASENAME("..", "..");
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
TEST_BASENAME("C:\\Windows\\System32", "System32");
|
|
|
|
TEST_BASENAME("C:\\Windows\\", "");
|
|
|
|
TEST_BASENAME("C:\\", "");
|
|
|
|
TEST_BASENAME("C:", "");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_DIRNAME("/usr/local/bin", "/usr/local/");
|
|
|
|
TEST_DIRNAME("/usr/local/", "/usr/local/");
|
|
|
|
TEST_DIRNAME("/usr/", "/usr/");
|
|
|
|
TEST_DIRNAME("/", "/");
|
|
|
|
TEST_DIRNAME("usr/local/bin", "usr/local/");
|
|
|
|
TEST_DIRNAME("usr/local/", "usr/local/");
|
|
|
|
TEST_DIRNAME("usr/", "usr/");
|
|
|
|
TEST_DIRNAME("usr", ".");
|
|
|
|
TEST_DIRNAME("", ".");
|
|
|
|
TEST_DIRNAME(".", ".");
|
|
|
|
TEST_DIRNAME("..", ".");
|
|
|
|
#if HAVE_DOS_PATHS
|
|
|
|
TEST_DIRNAME("C:\\Windows\\System32", "C:\\Windows\\");
|
|
|
|
TEST_DIRNAME("C:\\Windows\\", "C:\\Windows\\");
|
|
|
|
TEST_DIRNAME("C:\\", "C:\\");
|
|
|
|
TEST_DIRNAME("C:", "C:");
|
|
|
|
#endif
|
|
|
|
|
2023-02-26 03:50:08 +00:00
|
|
|
return 0;
|
2020-02-06 12:44:16 +00:00
|
|
|
}
|