-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve code coverage for CanonicalXmlEntityReference class (0% → 92.8%) #120640
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
Open
Copilot
wants to merge
2
commits into
main
Choose a base branch
from
copilot/improve-code-coverage-xml
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−0
Open
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
170 changes: 170 additions & 0 deletions
170
src/libraries/System.Security.Cryptography.Xml/tests/CanonicalXmlEntityReferenceTest.cs
This file contains hidden or 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,170 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
using System.Text; | ||
using System.Xml; | ||
using Xunit; | ||
|
||
namespace System.Security.Cryptography.Xml.Tests | ||
{ | ||
public class CanonicalXmlEntityReferenceTest | ||
{ | ||
[Fact] | ||
public void EntityReferenceInCanonicalization() | ||
{ | ||
// This test exercises CanonicalXmlEntityReference by using a transform | ||
// that internally creates a CanonicalXmlDocument and loads XML into it. | ||
// When an XmlNodeReader reads from a document containing entity references, | ||
// the target document's CreateEntityReference method is called. | ||
|
||
string xml = @"<!DOCTYPE doc [ | ||
<!ENTITY test ""TestValue""> | ||
]> | ||
<root>&test;</root>"; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
XmlDocument doc = new XmlDocument(); | ||
doc.PreserveWhitespace = true; | ||
doc.LoadXml(xml); | ||
|
||
// Use C14N transform which internally uses CanonicalXmlDocument | ||
XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); | ||
transform.LoadInput(doc); | ||
|
||
Stream output = (Stream)transform.GetOutput(); | ||
string result = new StreamReader(output, Encoding.UTF8).ReadToEnd(); | ||
|
||
// Entity should be expanded in canonical form | ||
Assert.Contains("TestValue", result); | ||
Assert.Contains("<root>", result); | ||
} | ||
|
||
[Fact] | ||
public void EntityReferenceWithXmlNodeList() | ||
{ | ||
// Test with XmlNodeList input which triggers different code path in CanonicalXml | ||
// When using XmlNodeList, nodes not in the list are not included, which may affect | ||
// entity reference expansion in the canonical output | ||
string xml = @"<!DOCTYPE doc [ | ||
<!ENTITY test ""Hello""> | ||
]> | ||
<root><child>&test;</child></root>"; | ||
|
||
XmlDocument doc = new XmlDocument(); | ||
doc.PreserveWhitespace = true; | ||
doc.LoadXml(xml); | ||
|
||
XmlNodeList nodeList = doc.SelectNodes("//child")!; | ||
|
||
XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); | ||
transform.LoadInput(nodeList); | ||
|
||
Stream output = (Stream)transform.GetOutput(); | ||
string result = new StreamReader(output, Encoding.UTF8).ReadToEnd(); | ||
|
||
// The node should be present in canonical form | ||
Assert.Contains("<child>", result); | ||
Assert.Contains("</child>", result); | ||
} | ||
|
||
[Fact] | ||
public void EntityReferenceWithCommentsIncluded() | ||
{ | ||
// Test with includeComments = true | ||
string xml = @"<!DOCTYPE doc [ | ||
<!ENTITY ent ""EntityContent""> | ||
]> | ||
<root><!--comment-->&ent;</root>"; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
XmlDocument doc = new XmlDocument(); | ||
doc.PreserveWhitespace = true; | ||
doc.LoadXml(xml); | ||
|
||
XmlDsigC14NWithCommentsTransform transform = new XmlDsigC14NWithCommentsTransform(); | ||
transform.LoadInput(doc); | ||
|
||
Stream output = (Stream)transform.GetOutput(); | ||
string result = new StreamReader(output, Encoding.UTF8).ReadToEnd(); | ||
|
||
// Both comment and expanded entity should be in output | ||
Assert.Contains("<!--comment-->", result); | ||
Assert.Contains("EntityContent", result); | ||
} | ||
|
||
[Fact] | ||
public void EntityReferenceInExclusiveCanonicalization() | ||
{ | ||
// Test with Exclusive C14N transform | ||
string xml = @"<!DOCTYPE doc [ | ||
<!ENTITY test ""ExclusiveTest""> | ||
]> | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<root xmlns=""http://example.com"">&test;</root>"; | ||
|
||
XmlDocument doc = new XmlDocument(); | ||
doc.PreserveWhitespace = true; | ||
doc.LoadXml(xml); | ||
|
||
XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform(); | ||
transform.LoadInput(doc); | ||
|
||
Stream output = (Stream)transform.GetOutput(); | ||
string result = new StreamReader(output, Encoding.UTF8).ReadToEnd(); | ||
|
||
// Entity should be expanded in canonical form | ||
Assert.Contains("ExclusiveTest", result); | ||
Assert.Contains("http://example.com", result); | ||
} | ||
|
||
[Fact] | ||
public void EntityReferenceWithHash() | ||
{ | ||
// Test the WriteHash code path by using GetDigestedOutput | ||
string xml = @"<!DOCTYPE doc [ | ||
<!ENTITY test ""HashTest""> | ||
]> | ||
<root>&test;</root>"; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
XmlDocument doc = new XmlDocument(); | ||
doc.PreserveWhitespace = true; | ||
doc.LoadXml(xml); | ||
|
||
XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); | ||
transform.LoadInput(doc); | ||
|
||
using (SHA256 hash = SHA256.Create()) | ||
{ | ||
byte[] digest = transform.GetDigestedOutput(hash); | ||
|
||
// Should produce a valid hash | ||
Assert.NotNull(digest); | ||
Assert.Equal(32, digest.Length); // SHA256 produces 32 bytes | ||
} | ||
} | ||
|
||
[Fact] | ||
public void MultipleEntityReferences() | ||
{ | ||
// Test with multiple entity references | ||
string xml = @"<!DOCTYPE doc [ | ||
<!ENTITY ent1 ""First""> | ||
<!ENTITY ent2 ""Second""> | ||
]> | ||
<root>&ent1; and &ent2;</root>"; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
XmlDocument doc = new XmlDocument(); | ||
doc.PreserveWhitespace = true; | ||
doc.LoadXml(xml); | ||
|
||
XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); | ||
transform.LoadInput(doc); | ||
|
||
Stream output = (Stream)transform.GetOutput(); | ||
string result = new StreamReader(output, Encoding.UTF8).ReadToEnd(); | ||
|
||
// Both entities should be expanded | ||
Assert.Contains("First", result); | ||
Assert.Contains("Second", result); | ||
Assert.Contains("and", result); | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.