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-05-14 23:39:20 +02:00
|
|
|
|
using LibHac.Fs;
|
2018-12-23 22:49:28 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests
|
|
|
|
|
{
|
|
|
|
|
public class PathToolsTests
|
|
|
|
|
{
|
|
|
|
|
public static object[][] NormalizedPathTestItems =
|
|
|
|
|
{
|
|
|
|
|
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"},
|
2019-01-03 02:16:19 +01:00
|
|
|
|
new object[] {"/../a/b/c/.", "/a/b/c"},
|
2019-06-17 07:09:01 +02:00
|
|
|
|
new object[] {"/./aaa/bbb/ccc/.", "/aaa/bbb/ccc"},
|
2018-12-23 22:49:28 +01:00
|
|
|
|
|
|
|
|
|
new object[] {"/a/b/c/", "/a/b/c/"},
|
2019-06-17 07:09:01 +02:00
|
|
|
|
new object[] {"/aa/./bb/../cc/", "/aa/cc/"},
|
2019-01-03 02:16:19 +01:00
|
|
|
|
new object[] {"/./b/../c/", "/c/"},
|
2018-12-23 22:49:28 +01:00
|
|
|
|
new object[] {"/a/../../../", "/"},
|
|
|
|
|
new object[] {"//a/b//.//c/", "/a/b/c/"},
|
2019-01-03 02:16:19 +01:00
|
|
|
|
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"},
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
|
|
|
|
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"},
|
2019-06-17 07:09:01 +02:00
|
|
|
|
new object[] {"mount:/d./aa", "mount:/d./aa"},
|
2018-12-23 22:49:28 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-05-14 00:54:49 +02: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},
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-18 05:16:45 +02:00
|
|
|
|
public static object[][] IsNormalizedTestItems = GetNormalizedPaths(true);
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2019-06-18 05:16:45 +02:00
|
|
|
|
public static object[][] IsNotNormalizedTestItems = GetNormalizedPaths(false);
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2018-12-23 22:49:28 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(NormalizedPathTestItems))]
|
|
|
|
|
public static void NormalizePath(string path, string expected)
|
|
|
|
|
{
|
|
|
|
|
string actual = PathTools.Normalize(path);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2019-05-14 00:54:49 +02:00
|
|
|
|
|
2019-06-11 23:41:54 +02:00
|
|
|
|
[Theory]
|
2019-06-18 05:16:45 +02:00
|
|
|
|
[MemberData(nameof(IsNormalizedTestItems))]
|
|
|
|
|
public static void IsNormalized(string path)
|
2019-06-11 23:41:54 +02:00
|
|
|
|
{
|
2019-06-18 05:16:45 +02:00
|
|
|
|
Assert.True(PathTools.IsNormalized(path.AsSpan()));
|
|
|
|
|
}
|
2019-06-11 23:41:54 +02:00
|
|
|
|
|
2019-06-18 05:16:45 +02: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
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 00:54:49 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData(nameof(SubPathTestItems))]
|
|
|
|
|
public static void TestSubPath(string rootPath, string path, bool expected)
|
|
|
|
|
{
|
|
|
|
|
bool actual = PathTools.IsSubPath(rootPath.AsSpan(), path.AsSpan());
|
|
|
|
|
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
2019-06-18 05:16:45 +02:00
|
|
|
|
|
|
|
|
|
private static object[][] GetNormalizedPaths(bool getNormalized)
|
|
|
|
|
{
|
|
|
|
|
var normalizedPaths = new HashSet<string>();
|
|
|
|
|
var notNormalizedPaths = new HashSet<string>();
|
|
|
|
|
|
|
|
|
|
foreach (object[] pair in NormalizedPathTestItems)
|
|
|
|
|
{
|
|
|
|
|
string pathNotNorm = (string)pair[0];
|
|
|
|
|
string pathNorm = (string)pair[1];
|
|
|
|
|
|
|
|
|
|
if (pathNorm != pathNotNorm) notNormalizedPaths.Add(pathNotNorm);
|
|
|
|
|
normalizedPaths.Add(pathNorm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HashSet<string> paths = getNormalized ? normalizedPaths : notNormalizedPaths;
|
|
|
|
|
|
|
|
|
|
return paths.Select(x => new object[] { x }).ToArray();
|
|
|
|
|
}
|
2018-12-23 22:49:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|