-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Fix incorrect "illegal XML comment chars" check & add regression tests #74787
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/libraries/System.Private.Xml/tests/XmlWriter/XmlTextWriterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace System.Xml.Tests | ||
GrabYourPitchforks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public class XmlTextWriterTests | ||
{ | ||
public static IEnumerable<object[]> PositiveTestCases | ||
{ | ||
get | ||
{ | ||
yield return new string[] { null }; // will be normalized to empty string | ||
yield return new string[] { "" }; | ||
yield return new string[] { "This is some data." }; | ||
yield return new string[] { " << brackets and whitespace >> " }; // brackets & surrounding whitespace are ok | ||
yield return new string[] { "&" }; // entities are ok (treated opaquely) | ||
yield return new string[] { "Hello\r\nthere." }; // newlines are ok | ||
yield return new string[] { "\U0001F643 Upside-down smiley \U0001F643" }; // correctly paired surrogates are ok | ||
yield return new string[] { "\uFFFD\uFFFE\uFFFF" }; // replacement char & private use are ok | ||
} | ||
} | ||
|
||
public static IEnumerable<object[]> BadSurrogateTestCases | ||
{ | ||
get | ||
{ | ||
yield return new string[] { "\uD800 Unpaired high surrogate." }; | ||
yield return new string[] { "\uDFFF Unpaired low surrogate." }; | ||
yield return new string[] { "Unpaired high surrogate at end. \uD800" }; | ||
yield return new string[] { "Unpaired low surrogate at end. \uDFFF" }; | ||
yield return new string[] { "Unpaired surrogates \uDFFF\uD800 in middle." }; | ||
} | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(PositiveTestCases))] | ||
[InlineData("]]")] // ]] without trailing > is ok | ||
[InlineData("-->")] // end of comment marker ok (meaningless to cdata tag) | ||
public void WriteCData_SuccessCases(string cdataText) | ||
{ | ||
GrabYourPitchforks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
StringWriter sw = new StringWriter(); | ||
XmlTextWriter xw = new XmlTextWriter(sw); | ||
|
||
xw.WriteCData(cdataText); | ||
|
||
Assert.Equal($"<![CDATA[{cdataText}]]>", sw.ToString()); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(BadSurrogateTestCases), DisableDiscoveryEnumeration = true)] // disable enumeration to avoid test harness misinterpreting unpaired surrogates | ||
[InlineData("]]>")] // end of cdata marker forbidden (ambiguous close tag) | ||
public void WriteCData_FailureCases(string cdataText) | ||
{ | ||
StringWriter sw = new StringWriter(); | ||
XmlTextWriter xw = new XmlTextWriter(sw); | ||
|
||
Assert.Throws<ArgumentException>(() => xw.WriteCData(cdataText)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(PositiveTestCases))] | ||
[InlineData("-12345")] // hyphen at beginning is ok | ||
[InlineData("123- -45")] // single hyphens are ok in middle | ||
[InlineData("]]>")] // end of cdata marker ok (meaningless to comment tag) | ||
public void WriteComment_SuccessCases(string commentText) | ||
{ | ||
StringWriter sw = new StringWriter(); | ||
XmlTextWriter xw = new XmlTextWriter(sw); | ||
|
||
xw.WriteComment(commentText); | ||
|
||
Assert.Equal($"<!--{commentText}-->", sw.ToString()); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(BadSurrogateTestCases), DisableDiscoveryEnumeration = true)] // disable enumeration to avoid test harness misinterpreting unpaired surrogates | ||
[InlineData("123--45")] // double-hyphen in middle is forbidden (ambiguous comment close tag) | ||
[InlineData("12345-")] // hyphen at end is forbidden (ambiguous comment close tag) | ||
[InlineData("-->")] // end of comment marker forbidden (ambiguous close tag) | ||
public void WriteComment_FailureCases(string commentText) | ||
{ | ||
StringWriter sw = new StringWriter(); | ||
XmlTextWriter xw = new XmlTextWriter(sw); | ||
|
||
Assert.Throws<ArgumentException>(() => xw.WriteComment(commentText)); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 caught my eye :/ .. as far as I can see our test coverage of XmlTextWriter is minimal. Our tests test various other
*Xml*Writer
s. XmlWriter.Create() doesn't seem to create one either. Not an urgent problem, but I suppose it led to this bug.