Skip to content

Improve code coverage for System.Security.Cryptography.Xml #107235

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

Closed
wants to merge 13 commits into from

Conversation

hootanht
Copy link
Contributor

@hootanht hootanht commented Sep 1, 2024

Fixes #20508

Add tests for KeyInfoClause and CanonicalXmlEntityReference to improve code coverage.

  • KeyInfoClauseTests.cs

    • Add a new test class KeyInfoClauseTests.
    • Add a test method LoadXml_ValidXml_Success to test loading valid XML.
    • Add a test method LoadXml_InvalidXml_ThrowsException to test loading invalid XML.
    • Add a test method GetXml_ReturnsExpectedXml to test getting XML from KeyInfoClause.
  • CanonicalXmlEntityReferenceTests.cs

    • Add a new test class CanonicalXmlEntityReferenceTests.
    • Add a test method Write_WritesExpectedOutput to test writing canonical XML.
    • Add a test method WriteHash_WritesExpectedHash to test writing hash of canonical XML.
    • Add a test method IsInNodeSet_GetSet_ReturnsExpected to test getting and setting IsInNodeSet.

Fixes dotnet#20508

Add tests for `KeyInfoClause` and `CanonicalXmlEntityReference` to improve code coverage.

* **KeyInfoClauseTests.cs**
  - Add a new test class `KeyInfoClauseTests`.
  - Add a test method `LoadXml_ValidXml_Success` to test loading valid XML.
  - Add a test method `LoadXml_InvalidXml_ThrowsException` to test loading invalid XML.
  - Add a test method `GetXml_ReturnsExpectedXml` to test getting XML from `KeyInfoClause`.

* **CanonicalXmlEntityReferenceTests.cs**
  - Add a new test class `CanonicalXmlEntityReferenceTests`.
  - Add a test method `Write_WritesExpectedOutput` to test writing canonical XML.
  - Add a test method `WriteHash_WritesExpectedHash` to test writing hash of canonical XML.
  - Add a test method `IsInNodeSet_GetSet_ReturnsExpected` to test getting and setting `IsInNodeSet`.
@ghost ghost added the area-System.Security label Sep 1, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 1, 2024
Copy link
Member

@vcsjones vcsjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 thanks for the contribution!

The dotnet/runtime repository does not automatically include .cs files in projects, they must be explicitly added to the project file.

As a result, these tests are not actually running. Can you please add them to https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj?

{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<KeyInfo><KeyName>Test</KeyName></KeyInfo>");
var keyInfoClause = new TestKeyInfoClause();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very clear to me what we are testing here. All of these tests are testing a test class, so the tests are just testing TestKeyInfoClause.

The only thing on KeyInfoClause that actually has functionality to be tested is GetXml(XmlDocument xmlDocument) when it isn't overridden.

… bool verifySignatureOnly)`, `System.Security.Cryptography.Xml.KeyInfoClause`, and `System.Security.Cryptography.Xml.CanonicalXmlEntityReference` in `EncryptedXmlTest.cs`.

* **CheckSignature**: Add test for `CheckSignature` method with `X509Certificate2` and `verifySignatureOnly` parameter.
* **KeyInfoClause**: Add tests for `GetXml` and `LoadXml` methods.
* **CanonicalXmlEntityReference**: Add tests for `Write`, `WriteHash`, and `IsInNodeSet` methods.

Add `SymmetricKeyWrapTest.cs` to test `System.Security.Cryptography.Xml.SymmetricKeyWrap`.

Add `EncryptionMethodTest.cs` to test `System.Security.Cryptography.Xml.EncryptionMethod`.
Add tests for `SignedXml.CheckSignature` base on code review
Two new test files, `CanonicalXmlEntityReferenceTests.cs` and
`KeyInfoClauseTests.cs`, have been added to the project file
`System.Security.Cryptography.Xml.Tests.csproj`. These files are
included in the `<ItemGroup>` section, which lists the source files
to be compiled.
Modified access modifiers of three test methods in `CanonicalXmlEntityReferenceTests.cs`:
- `Write_WritesExpectedOutput`
- `WriteHash_WritesExpectedHash`
- `IsInNodeSet_GetSet_ReturnsExpected`

Changed from instance methods (`public void`) to static methods (`public static void`).
Refactored `CanonicalXmlEntityReferenceTests.cs` and `KeyInfoClauseTests.cs` to replace explicit type declarations with `var` where the type is evident from the context. This change improves code readability and conciseness without altering functionality. Specifically, updated `XmlDocument`, `CanonicalXmlEntityReference`, `KeyInfoName`, and `XmlElement` variables.
Copy link
Member

@vcsjones vcsjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the tests are now showing, these tests do not compile because you are attempting to test and use internal types and members.

The unit test project here does not have access to internal members, and we very rarely make use of InternalsVisibleTo. internal things are typically tested by way of them being used from public things (behavioral tests).

I would also consider running tests locally before you submit them, or at least ensure they compile. The documentation on how to get started with local developer is in the workflow instructions.

At this point I would recommend identifying public types and functionality that are lacking test coverage.

public static void Write_WritesExpectedOutput()
{
var xmlDocument = new XmlDocument();
var entityReference = new CanonicalXmlEntityReference("entity", xmlDocument, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are failing to compile now because CanonicalXmlEntityReference is internal. We don't typically unit test internal classes, only the public API surface.

var xmlDocument = new XmlDocument();
var entityReference = new CanonicalXmlEntityReference("entity", xmlDocument, true);
var stringBuilder = new StringBuilder();
var anc = new AncestralNamespaceContextManager();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, this type is internal.

@vcsjones vcsjones added the needs-author-action An issue or pull request that requires more info or actions from the author. label Sep 3, 2024
@hootanht hootanht closed this Sep 18, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Security community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Code Coverage for System.Security.Cryptography.Xml (66.3%)
3 participants