2019-05-14 00:54:49 +02:00
|
|
|
|
using System;
|
2019-06-18 05:16:45 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-10-14 18:58:43 +02:00
|
|
|
|
using LibHac.Common;
|
2021-12-19 09:35:45 +01:00
|
|
|
|
using LibHac.Tools.FsSystem;
|
2020-10-11 05:17:07 +02:00
|
|
|
|
using LibHac.Util;
|
2018-12-23 22:49:28 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
namespace LibHac.Tests;
|
|
|
|
|
|
|
|
|
|
public class PathToolsTests
|
2018-12-23 22:49:28 +01:00
|
|
|
|
{
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] NormalizedPathTestItems =
|
2023-12-10 02:48:56 +01:00
|
|
|
|
[
|
|
|
|
|
["", "/"],
|
|
|
|
|
["/", "/"],
|
|
|
|
|
["/.", "/"],
|
|
|
|
|
["/a/b/c", "/a/b/c"],
|
|
|
|
|
["/a/b/../c", "/a/c"],
|
|
|
|
|
["/a/b/c/..", "/a/b"],
|
|
|
|
|
["/a/b/c/.", "/a/b/c"],
|
|
|
|
|
["/a/../../..", "/"],
|
|
|
|
|
["/a/../../../a/b/c", "/a/b/c"],
|
|
|
|
|
["//a/b//.//c", "/a/b/c"],
|
|
|
|
|
["/../a/b/c/.", "/a/b/c"],
|
|
|
|
|
["/./aaa/bbb/ccc/.", "/aaa/bbb/ccc"],
|
|
|
|
|
|
|
|
|
|
["/a/b/c/", "/a/b/c/"],
|
|
|
|
|
["a/b/c/", "/a/b/c/"],
|
|
|
|
|
["/aa/./bb/../cc/", "/aa/cc/"],
|
|
|
|
|
["/./b/../c/", "/c/"],
|
|
|
|
|
["/a/../../../", "/"],
|
|
|
|
|
["//a/b//.//c/", "/a/b/c/"],
|
|
|
|
|
["/tmp/../", "/"],
|
|
|
|
|
|
|
|
|
|
["a", "/a"],
|
|
|
|
|
["a/../../../a/b/c", "/a/b/c"],
|
|
|
|
|
["./b/../c/", "/c/"],
|
|
|
|
|
[".", "/"],
|
|
|
|
|
["..", "/"],
|
|
|
|
|
["../a/b/c/.", "/a/b/c"],
|
|
|
|
|
["./a/b/c/.", "/a/b/c"],
|
|
|
|
|
|
|
|
|
|
["a:/a/b/c", "a:/a/b/c"],
|
|
|
|
|
["mount:/a/b/../c", "mount:/a/c"],
|
|
|
|
|
["mount:", "mount:/"],
|
|
|
|
|
["abc:/a/../../../a/b/c", "abc:/a/b/c"],
|
|
|
|
|
["abc:/./b/../c/", "abc:/c/"],
|
|
|
|
|
["abc:/.", "abc:/"],
|
|
|
|
|
["abc:/..", "abc:/"],
|
|
|
|
|
["abc:/", "abc:/"],
|
|
|
|
|
["abc://a/b//.//c", "abc:/a/b/c"],
|
|
|
|
|
["abc:/././/././a/b//.//c", "abc:/a/b/c"],
|
|
|
|
|
["mount:/d./aa", "mount:/d./aa"],
|
|
|
|
|
];
|
2018-12-23 22:49:28 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] SubPathTestItems =
|
2023-12-10 02:48:56 +01:00
|
|
|
|
[
|
|
|
|
|
["/", "/", false],
|
|
|
|
|
["/", "/a", true],
|
|
|
|
|
["/", "/a/", true],
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["/a/b/c", "/a/b/c/d", true],
|
|
|
|
|
["/a/b/c/", "/a/b/c/d", true],
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["/a/b/c", "/a/b/c", false],
|
|
|
|
|
["/a/b/c/", "/a/b/c/", false],
|
|
|
|
|
["/a/b/c/", "/a/b/c", false],
|
|
|
|
|
["/a/b/c", "/a/b/c/", false],
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["/a/b/c/", "/a/b/cdef", false],
|
|
|
|
|
["/a/b/c", "/a/b/cdef", false],
|
|
|
|
|
["/a/b/c/", "/a/b/cd", false],
|
2019-07-07 22:14:39 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["mount:/", "mount:/", false],
|
|
|
|
|
["mount:/", "mount:/a", true],
|
|
|
|
|
["mount:/", "mount:/a/", true],
|
2019-10-14 18:58:43 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["mount:/a/b/c", "mount:/a/b/c/d", true],
|
|
|
|
|
["mount:/a/b/c/", "mount:/a/b/c/d", true],
|
2019-10-14 18:58:43 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["mount:/a/b/c", "mount:/a/b/c", false],
|
|
|
|
|
["mount:/a/b/c/", "mount:/a/b/c/", false],
|
|
|
|
|
["mount:/a/b/c/", "mount:/a/b/c", false],
|
|
|
|
|
["mount:/a/b/c", "mount:/a/b/c/", false],
|
2019-10-14 18:58:43 +02:00
|
|
|
|
|
2023-12-10 02:48:56 +01:00
|
|
|
|
["mount:/a/b/c/", "mount:/a/b/cdef", false],
|
|
|
|
|
["mount:/a/b/c", "mount:/a/b/cdef", false],
|
|
|
|
|
["mount:/a/b/c/", "mount:/a/b/cd", false],
|
|
|
|
|
];
|
2019-07-07 22:14:39 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] ParentDirectoryTestItems =
|
2023-12-10 02:48:56 +01:00
|
|
|
|
[
|
|
|
|
|
["/", ""],
|
|
|
|
|
["/a", "/"],
|
|
|
|
|
["/aa/aabc/f", "/aa/aabc"],
|
|
|
|
|
["mount:/", ""],
|
|
|
|
|
["mount:/a", "mount:/"],
|
|
|
|
|
["mount:/aa/aabc/f", "mount:/aa/aabc"]
|
|
|
|
|
];
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] IsNormalizedTestItems = GetNormalizedPaths(true);
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] IsNotNormalizedTestItems = GetNormalizedPaths(false);
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(NormalizedPathTestItems))]
|
|
|
|
|
public static void NormalizePath(string path, string expected)
|
|
|
|
|
{
|
|
|
|
|
string actual = PathTools.Normalize(path);
|
2018-12-23 22:49:28 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(IsNormalizedTestItems))]
|
|
|
|
|
public static void IsNormalized(string path)
|
|
|
|
|
{
|
|
|
|
|
Assert.True(PathTools.IsNormalized(path.AsSpan()));
|
|
|
|
|
}
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(IsNotNormalizedTestItems))]
|
|
|
|
|
public static void IsNotNormalized(string path)
|
|
|
|
|
{
|
|
|
|
|
Assert.False(PathTools.IsNormalized(path.AsSpan()));
|
|
|
|
|
}
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(SubPathTestItems))]
|
|
|
|
|
public static void TestSubPath(string rootPath, string path, bool expected)
|
|
|
|
|
{
|
|
|
|
|
bool actual = PathTools.IsSubPath(rootPath.AsSpan(), path.AsSpan());
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2019-06-18 05:16:45 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(SubPathTestItems))]
|
|
|
|
|
public static void TestSubPathReverse(string rootPath, string path, bool expected)
|
|
|
|
|
{
|
|
|
|
|
bool actual = PathTools.IsSubPath(path.AsSpan(), rootPath.AsSpan());
|
2019-07-07 22:14:39 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2019-07-07 22:14:39 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(ParentDirectoryTestItems))]
|
|
|
|
|
public static void TestParentDirectory(string path, string expected)
|
|
|
|
|
{
|
|
|
|
|
string actual = PathTools.GetParentDirectory(path);
|
2019-07-07 22:14:39 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2019-07-07 22:14:39 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
private static object[][] GetNormalizedPaths(bool getNormalized)
|
|
|
|
|
{
|
|
|
|
|
var normalizedPaths = new HashSet<string>();
|
|
|
|
|
var notNormalizedPaths = new HashSet<string>();
|
2019-06-18 05:16:45 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
foreach (object[] pair in NormalizedPathTestItems)
|
|
|
|
|
{
|
|
|
|
|
string pathNotNorm = (string)pair[0];
|
|
|
|
|
string pathNorm = (string)pair[1];
|
2019-06-18 05:16:45 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
if (pathNorm != pathNotNorm) notNormalizedPaths.Add(pathNotNorm);
|
|
|
|
|
normalizedPaths.Add(pathNorm);
|
|
|
|
|
}
|
2019-06-18 05:16:45 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
HashSet<string> paths = getNormalized ? normalizedPaths : notNormalizedPaths;
|
2019-06-18 05:16:45 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
return paths.Select(x => new object[] { x }).ToArray();
|
|
|
|
|
}
|
2019-10-14 18:58:43 +02:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] GetFileNameTestItems =
|
2023-12-10 02:48:56 +01:00
|
|
|
|
[
|
|
|
|
|
["/a/bb/ccc", "ccc"],
|
|
|
|
|
["/a/bb/ccc/", ""],
|
|
|
|
|
["/a/bb", "bb"],
|
|
|
|
|
["/a/bb/", ""],
|
|
|
|
|
["/a", "a"],
|
|
|
|
|
["/a/", ""],
|
|
|
|
|
["/", ""],
|
|
|
|
|
];
|
2020-01-08 06:51:59 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(GetFileNameTestItems))]
|
|
|
|
|
public static void GetFileNameTest(string path, string expected)
|
|
|
|
|
{
|
2022-12-01 07:28:02 +01:00
|
|
|
|
var u8Path = path.ToU8Span();
|
2020-01-08 06:51:59 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
ReadOnlySpan<byte> fileName = PathTools.GetFileName(u8Path);
|
2020-01-08 06:51:59 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
string actual = StringUtils.Utf8ZToString(fileName);
|
2020-01-08 06:51:59 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2020-01-19 09:40:29 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
public static object[][] GetLastSegmentTestItems =
|
2023-12-10 02:48:56 +01:00
|
|
|
|
[
|
|
|
|
|
["/a/bb/ccc", "ccc"],
|
|
|
|
|
["/a/bb/ccc/", "ccc"],
|
|
|
|
|
["/a/bb", "bb"],
|
|
|
|
|
["/a/bb/", "bb"],
|
|
|
|
|
["/a", "a"],
|
|
|
|
|
["/a/", "a"],
|
|
|
|
|
["/", ""],
|
|
|
|
|
];
|
2020-01-19 09:40:29 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(GetLastSegmentTestItems))]
|
|
|
|
|
public static void GetLastSegmentTest(string path, string expected)
|
|
|
|
|
{
|
2022-12-01 07:28:02 +01:00
|
|
|
|
var u8Path = path.ToU8Span();
|
2020-01-19 09:40:29 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
ReadOnlySpan<byte> fileName = PathTools.GetLastSegment(u8Path);
|
2020-01-19 09:40:29 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
string actual = StringUtils.Utf8ZToString(fileName);
|
2020-01-19 09:40:29 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
Assert.Equal(expected, actual);
|
2018-12-23 22:49:28 +01:00
|
|
|
|
}
|
2021-12-19 09:35:45 +01:00
|
|
|
|
}
|