Skip to content

Commit

Permalink
Merge pull request dotnet/corefx#1 from anthonylangsworth/signed-xml-…
Browse files Browse the repository at this point in the history
…fixing-tests-1

Fix concurrency issues by removing file access

Commit migrated from dotnet/corefx@e5b43ed
  • Loading branch information
krwq authored Mar 4, 2017
2 parents 621733c + ea635c6 commit 0975213
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
<Compile Include="XmlDsigXsltTransformTest.cs" />
<Compile Include="XmlLicenseTransformTest.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Common\tests\System\IO\TempFile.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="EncryptedXmlSample3.xml" />
<EmbeddedResource Include="EncryptedXmlSample2.xml" />
Expand All @@ -57,4 +54,4 @@
<Compile Include="Samples\Samples.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
122 changes: 98 additions & 24 deletions src/libraries/System.Security.Cryptography.Xml/tests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,120 @@
// 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;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Resolvers;

namespace System.Security.Cryptography.Xml.Tests
{
internal static class TestHelpers
{
public static TempFile CreateTestDtdFile(string testName)
/// <summary>
/// Convert a <see cref="Stream"/> to a <see cref="string"/> using the given <see cref="Encoding"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="Stream"/> to read from. This cannot be null.
/// </param>
/// <param name="encoding">
/// The <see cref="Encoding"/> to use. This cannot be null.
/// </param>
/// <returns>
/// The stream as a string.
/// </returns>
/// <exception cref="ArgumentNullException">
/// No argument can be null.
/// </exception>
public static string StreamToString(Stream stream, Encoding encoding)
{
if (testName == null)
throw new ArgumentNullException(nameof(testName));
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}

var file = new TempFile(
Path.Combine(Directory.GetCurrentDirectory(), testName + ".dtd")
);

File.WriteAllText(file.Path, "<!-- presence, not content, required -->");

return file;
using (StreamReader streamReader = new StreamReader(stream, encoding))
{
return streamReader.ReadToEnd();
}
}

public static TempFile CreateTestTextFile(string testName, string content)
/// <summary>
/// Perform
/// </summary>
/// <param name="inputXml">
/// The XML to transform. This cannot be null, empty or whitespace.
/// </param>
/// <param name="transform">
/// The <see cref="Transform"/> to perform on
/// <paramref name="inputXml"/>. This cannot be null.
/// </param>
/// <param name="encoding">
/// An optional <see cref="Encoding"/> to use when serializing or
/// deserializing <paramref name="inputXml"/>. This should match the
/// encoding specified in <paramref name="inputXml"/>. If omitted or
/// null, <see cref="UTF8Encoding"/> is used.
/// </param>
/// <param name="resolver">
/// An optional <see cref="XmlResolver"/> to use. If omitted or null,
/// no resolver is used.
/// </param>
/// <returns>
/// The transformed <paramref name="inputXml"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="transform"/> cannot be null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="inputXml"/> cannot be null, empty or whitespace.
/// </exception>
/// <exception cref="XmlException">
/// <paramref name="inputXml"/> is not valid XML.
/// </exception>
public static string ExecuteTransform(string inputXml, Transform transform, Encoding encoding = null, XmlResolver resolver = null)
{
if (testName == null)
throw new ArgumentNullException(nameof(testName));

if (content == null)
throw new ArgumentNullException(nameof(content));

var file = new TempFile(
Path.Combine(Directory.GetCurrentDirectory(), testName + ".txt")
);
if (string.IsNullOrEmpty(inputXml))
{
throw new ArgumentException("Cannot be null, empty or whitespace", nameof(inputXml));
}
if (transform == null)
{
throw new ArgumentNullException(nameof(Transform));
}

File.WriteAllText(file.Path, content);
XmlDocument doc = new XmlDocument();
doc.XmlResolver = resolver;
doc.PreserveWhitespace = true;
doc.LoadXml(inputXml);

return file;
Encoding actualEncoding = encoding ?? Encoding.UTF8;
byte[] data = actualEncoding.GetBytes(inputXml);
using (Stream stream = new MemoryStream(data))
using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings { ValidationType = ValidationType.None, DtdProcessing = DtdProcessing.Parse, XmlResolver = resolver }))
{
doc.Load(reader);
transform.LoadInput(doc);
return StreamToString((Stream)transform.GetOutput(), actualEncoding);
}
}

public static string EscapePath(string path)
/// <summary>
/// Convert <paramref name="fileName"/> to a full URI for referencing
/// in an <see cref="XmlPreloadedResolver"/>.
/// </summary>
/// <param name="fileName">
/// The file name.
/// </param>
/// <returns>
/// The created <see cref="Uri"/>.
/// </returns>
public static Uri ToUri(string fileName)
{
return path.Replace("-", "&#2D;");
return new Uri("file:///" + Path.Combine(Directory.GetCurrentDirectory(), fileName));
}
}
}
Loading

0 comments on commit 0975213

Please sign in to comment.