LibHac/tests/LibHac.Tests/PathToolsTests.cs

222 lines
7.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using LibHac.Common;
using LibHac.Tools.FsSystem;
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 =
2018-12-23 22:49:28 +01:00
{
new object[] { "", "/" },
new object[] { "/", "/" },
new object[] { "/.", "/" },
new object[] { "/a/b/c", "/a/b/c" },
new object[] { "/a/b/../c", "/a/c" },
new object[] { "/a/b/c/..", "/a/b" },
new object[] { "/a/b/c/.", "/a/b/c" },
new object[] { "/a/../../..", "/" },
new object[] { "/a/../../../a/b/c", "/a/b/c" },
new object[] { "//a/b//.//c", "/a/b/c" },
new object[] { "/../a/b/c/.", "/a/b/c" },
new object[] { "/./aaa/bbb/ccc/.", "/aaa/bbb/ccc" },
new object[] { "/a/b/c/", "/a/b/c/" },
new object[] { "a/b/c/", "/a/b/c/" },
new object[] { "/aa/./bb/../cc/", "/aa/cc/" },
new object[] { "/./b/../c/", "/c/" },
new object[] { "/a/../../../", "/" },
new object[] { "//a/b//.//c/", "/a/b/c/" },
new object[] { "/tmp/../", "/" },
new object[] { "a", "/a" },
new object[] { "a/../../../a/b/c", "/a/b/c" },
new object[] { "./b/../c/", "/c/" },
new object[] { ".", "/" },
new object[] { "..", "/" },
new object[] { "../a/b/c/.", "/a/b/c" },
new object[] { "./a/b/c/.", "/a/b/c" },
new object[] { "a:/a/b/c", "a:/a/b/c" },
new object[] { "mount:/a/b/../c", "mount:/a/c" },
new object[] { "mount:", "mount:/" },
new object[] { "abc:/a/../../../a/b/c", "abc:/a/b/c" },
new object[] { "abc:/./b/../c/", "abc:/c/" },
new object[] { "abc:/.", "abc:/" },
new object[] { "abc:/..", "abc:/" },
new object[] { "abc:/", "abc:/" },
new object[] { "abc://a/b//.//c", "abc:/a/b/c" },
new object[] { "abc:/././/././a/b//.//c", "abc:/a/b/c" },
new object[] { "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 =
{
new object[] { "/", "/", false },
new object[] { "/", "/a", true },
new object[] { "/", "/a/", true },
new object[] { "/a/b/c", "/a/b/c/d", true },
new object[] { "/a/b/c/", "/a/b/c/d", true },
new object[] { "/a/b/c", "/a/b/c", false },
new object[] { "/a/b/c/", "/a/b/c/", false },
new object[] { "/a/b/c/", "/a/b/c", false },
new object[] { "/a/b/c", "/a/b/c/", false },
new object[] { "/a/b/c/", "/a/b/cdef", false },
new object[] { "/a/b/c", "/a/b/cdef", false },
new object[] { "/a/b/c/", "/a/b/cd", false },
new object[] { "mount:/", "mount:/", false },
new object[] { "mount:/", "mount:/a", true },
new object[] { "mount:/", "mount:/a/", true },
new object[] { "mount:/a/b/c", "mount:/a/b/c/d", true },
new object[] { "mount:/a/b/c/", "mount:/a/b/c/d", true },
new object[] { "mount:/a/b/c", "mount:/a/b/c", false },
new object[] { "mount:/a/b/c/", "mount:/a/b/c/", false },
new object[] { "mount:/a/b/c/", "mount:/a/b/c", false },
new object[] { "mount:/a/b/c", "mount:/a/b/c/", false },
new object[] { "mount:/a/b/c/", "mount:/a/b/cdef", false },
new object[] { "mount:/a/b/c", "mount:/a/b/cdef", false },
new object[] { "mount:/a/b/c/", "mount:/a/b/cd", false },
};
2021-11-14 20:08:57 +01:00
public static object[][] ParentDirectoryTestItems =
{
new object[] { "/", "" },
new object[] { "/a", "/" },
new object[] { "/aa/aabc/f", "/aa/aabc" },
new object[] { "mount:/", "" },
new object[] { "mount:/a", "mount:/" },
new object[] { "mount:/aa/aabc/f", "mount:/aa/aabc" }
};
2021-11-14 20:08:57 +01:00
public static object[][] IsNormalizedTestItems = GetNormalizedPaths(true);
2021-11-14 20:08:57 +01:00
public static object[][] IsNotNormalizedTestItems = GetNormalizedPaths(false);
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);
}
2021-11-14 20:08:57 +01:00
[Theory]
[MemberData(nameof(IsNormalizedTestItems))]
public static void IsNormalized(string path)
{
Assert.True(PathTools.IsNormalized(path.AsSpan()));
}
2021-11-14 20:08:57 +01:00
[Theory]
[MemberData(nameof(IsNotNormalizedTestItems))]
public static void IsNotNormalized(string path)
{
Assert.False(PathTools.IsNormalized(path.AsSpan()));
}
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());
2021-11-14 20:08:57 +01:00
Assert.Equal(expected, actual);
}
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());
2021-11-14 20:08:57 +01:00
Assert.Equal(expected, actual);
}
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);
2021-11-14 20:08:57 +01:00
Assert.Equal(expected, actual);
}
2021-11-14 20:08:57 +01:00
private static object[][] GetNormalizedPaths(bool getNormalized)
{
var normalizedPaths = new HashSet<string>();
var notNormalizedPaths = new HashSet<string>();
2021-11-14 20:08:57 +01:00
foreach (object[] pair in NormalizedPathTestItems)
{
string pathNotNorm = (string)pair[0];
string pathNorm = (string)pair[1];
2021-11-14 20:08:57 +01:00
if (pathNorm != pathNotNorm) notNormalizedPaths.Add(pathNotNorm);
normalizedPaths.Add(pathNorm);
}
2021-11-14 20:08:57 +01:00
HashSet<string> paths = getNormalized ? normalizedPaths : notNormalizedPaths;
2021-11-14 20:08:57 +01:00
return paths.Select(x => new object[] { x }).ToArray();
}
2021-11-14 20:08:57 +01:00
public static object[][] GetFileNameTestItems =
{
new object[] { "/a/bb/ccc", "ccc" },
new object[] { "/a/bb/ccc/", "" },
new object[] { "/a/bb", "bb" },
new object[] { "/a/bb/", "" },
new object[] { "/a", "a" },
new object[] { "/a/", "" },
new object[] { "/", "" },
};
2021-11-14 20:08:57 +01:00
[Theory]
[MemberData(nameof(GetFileNameTestItems))]
public static void GetFileNameTest(string path, string expected)
{
var u8Path = path.ToU8String();
2021-11-14 20:08:57 +01:00
ReadOnlySpan<byte> fileName = PathTools.GetFileName(u8Path);
2021-11-14 20:08:57 +01:00
string actual = StringUtils.Utf8ZToString(fileName);
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 =
{
new object[] { "/a/bb/ccc", "ccc" },
new object[] { "/a/bb/ccc/", "ccc" },
new object[] { "/a/bb", "bb" },
new object[] { "/a/bb/", "bb" },
new object[] { "/a", "a" },
new object[] { "/a/", "a" },
new object[] { "/", "" },
};
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)
{
var u8Path = path.ToU8String();
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
}
}