diff options
author | Paulo Janotti <pauloja@microsoft.com> | 2018-03-27 02:25:59 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-27 02:25:59 +0300 |
commit | 94079a75105c3ac2a9010fcb3e030eda6790ce86 (patch) | |
tree | a8793481c979dc9779c8a550dac334256302cfb5 /src/System.Private.Xml | |
parent | e17fe3561ec50f6881b4725b401bec59e12d9580 (diff) |
Add a test to prevent regression when XmlUriResolver uses Uri.LocalPath (#28475)
Close #25820
Diffstat (limited to 'src/System.Private.Xml')
-rw-r--r-- | src/System.Private.Xml/tests/Misc/System.Xml.Misc.Tests.csproj | 1 | ||||
-rw-r--r-- | src/System.Private.Xml/tests/Misc/XmlUrlResolverTests.cs | 77 |
2 files changed, 78 insertions, 0 deletions
diff --git a/src/System.Private.Xml/tests/Misc/System.Xml.Misc.Tests.csproj b/src/System.Private.Xml/tests/Misc/System.Xml.Misc.Tests.csproj index 275891669f..102aa3cc79 100644 --- a/src/System.Private.Xml/tests/Misc/System.Xml.Misc.Tests.csproj +++ b/src/System.Private.Xml/tests/Misc/System.Xml.Misc.Tests.csproj @@ -10,6 +10,7 @@ <ItemGroup> <Compile Include="RandomizedHashing.cs" /> <Compile Include="..\..\src\System\Xml\Core\SecureStringHasher.cs" /> + <Compile Include="XmlUrlResolverTests.cs" /> </ItemGroup> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> </Project>
\ No newline at end of file diff --git a/src/System.Private.Xml/tests/Misc/XmlUrlResolverTests.cs b/src/System.Private.Xml/tests/Misc/XmlUrlResolverTests.cs new file mode 100644 index 0000000000..ae7f394cee --- /dev/null +++ b/src/System.Private.Xml/tests/Misc/XmlUrlResolverTests.cs @@ -0,0 +1,77 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Xml.Tests +{ + public class XmlUriResolverTests + { + [Fact] + public void Resolving_RelativeBase_Throws() + { + var resolver = new XmlUrlResolver(); + Assert.Throws<NotSupportedException>(() => resolver.ResolveUri( + new Uri(Environment.CurrentDirectory + Path.DirectorySeparatorChar, UriKind.Relative), "test.xml")); + } + + [Theory] + [MemberData(nameof(GetBaseUriAndPath))] + public void Resolving_LocalPath_Ok(Uri baseUri, string path) + { + var resolver = new XmlUrlResolver(); + Uri resolvedUri = resolver.ResolveUri(baseUri, path); + + Assert.Equal(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, path)), resolvedUri.LocalPath); + Assert.True(resolvedUri.LocalPath.EndsWith(path.Replace('/', Path.DirectorySeparatorChar))); + } + + [Theory] + [MemberData(nameof(XmlFileTargets))] + public void Resolving_OnlyWithBaseUri_Ok(string basePath) + { + var baseUri = new Uri(Path.GetFullPath(basePath)); + var resolver = new XmlUrlResolver(); + Uri resolvedUri = resolver.ResolveUri(baseUri, string.Empty); + + Assert.Equal(Path.GetFullPath(basePath), resolvedUri.LocalPath); + } + + public static IEnumerable<object[]> GetBaseUriAndPath() + { + // Base URI as null is the default for internal Xml operation. + var baseUris = new List<Uri> { null }; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // The case below does not work on Unix, the '#' ends up treated as a fragment and the path is cut there. + var currDirWithDirSeparator = Environment.CurrentDirectory + Path.DirectorySeparatorChar; + baseUris.Add(new Uri(currDirWithDirSeparator, UriKind.Absolute)); + baseUris.Add(new Uri(string.Empty, UriKind.RelativeOrAbsolute)); + } + + foreach (Uri baseUri in baseUris) + { + foreach (object[] targetFile in XmlFileTargets) + yield return new object[] { baseUri, targetFile[0] }; + } + } + + public static IEnumerable<object[]> XmlFileTargets => new object[][] + { + new object[] { "f#/t/\u00eb/test.xml" }, + new object[] { "/f#/t/\u00eb/t#st.xml" }, + new object[] { "/f#/\u00e3/\u00eb/t\u00ebst.xml" }, + new object[] { "u/t/c/test.xml" }, + new object[] { "u/t/c/t#st.xml" }, + new object[] { "/u/t/c/t\u00ebst.xml" }, + new object[] { "test.xml" }, + new object[] { "t#st.xml" }, + new object[] { "t\u00ebst.xml" } + }; + } +} |