-
Notifications
You must be signed in to change notification settings - Fork 0
Fix concurrency issues by removing file access #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bde5d64
c742452
8f8cb74
088b6d5
9c2e262
c66b24f
710775a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,28 @@ | |
| // 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; | ||
|
|
||
| namespace System.Security.Cryptography.Xml.Tests | ||
| { | ||
| public class TempFile : IDisposable | ||
| { | ||
| public TempFile(string fileName) | ||
| { | ||
| Path = fileName; | ||
| } | ||
|
|
||
| public string Path { get; } | ||
|
|
||
| public void Dispose() | ||
| { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| internal static class TestHelpers | ||
| { | ||
| public static TempFile CreateTestDtdFile(string testName) | ||
|
|
@@ -43,5 +61,96 @@ public static string EscapePath(string path) | |
| { | ||
| return path.Replace("-", "D;"); | ||
| } | ||
|
|
||
| /// <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 (stream == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(stream)); | ||
| } | ||
| if (encoding == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(encoding)); | ||
| } | ||
|
|
||
| using (StreamReader streamReader = new StreamReader(stream, encoding)) | ||
| { | ||
| return streamReader.ReadToEnd(); | ||
| } | ||
| } | ||
|
|
||
| /// <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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow, this xml doc is long There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's test code, but it's shared test code ;-)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this code is probably going to be shared across multiple test classes, I wanted to document it properly, including exceptions, so Intellisense will give the correct values. |
||
| { | ||
| if (string.IsNullOrEmpty(inputXml)) | ||
| { | ||
| throw new ArgumentException("Cannot be null, empty or whitespace", nameof(inputXml)); | ||
| } | ||
| if (transform == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(Transform)); | ||
| } | ||
|
|
||
| XmlDocument doc = new XmlDocument(); | ||
| doc.XmlResolver = resolver; | ||
| doc.PreserveWhitespace = true; | ||
| doc.LoadXml(inputXml); | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This already exists in the common test code:
https://github.com/tintoy/corefx/blob/feature/xml-crypto/test-files-base-class/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj#L49