Skip to content
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

add test for SslClientAuthenticationOptions.ShallowClone #88557

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static class SslClientAuthenticationOptionsExtensions
{
public static SslClientAuthenticationOptions ShallowClone(this SslClientAuthenticationOptions options)
{
// Use non-default values to verify the clone works fine.
var clone = new SslClientAuthenticationOptions()
{
AllowRenegotiation = options.AllowRenegotiation,
Expand All @@ -33,7 +34,10 @@ public static SslClientAuthenticationOptions ShallowClone(this SslClientAuthenti

#if DEBUG
// Try to detect if a property gets added that we're not copying correctly.
foreach (PropertyInfo pi in typeof(SslClientAuthenticationOptions).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
// The property count is guard for new properties that also needs to be added above.
PropertyInfo[] properties = typeof(SslClientAuthenticationOptions).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)!;
Debug.Assert(properties.Length == 13);
rzikm marked this conversation as resolved.
Show resolved Hide resolved
foreach (PropertyInfo pi in properties)
{
object? origValue = pi.GetValue(options);
object? cloneValue = pi.GetValue(clone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
Assert.Equal(string.Empty, server.TargetHostName);
}
}

[Fact]
public void ClientOptions_ShallowCopy_OK()
{
using X509Certificate2 clientCert = Configuration.Certificates.GetClientCertificate();

// needs to non-default values so we can verify it was copied correctly.
var clientOptions = new SslClientAuthenticationOptions
{
AllowRenegotiation = false,
AllowTlsResume = false,
ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 },
CertificateRevocationCheckMode = X509RevocationMode.Online,
ClientCertificates = new X509CertificateCollection() { clientCert },
EnabledSslProtocols = SslProtocols.Tls12,
EncryptionPolicy = EncryptionPolicy.RequireEncryption,
TargetHost = "foo",
CertificateChainPolicy = new X509ChainPolicy(),
RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }),
LocalCertificateSelectionCallback = new LocalCertificateSelectionCallback(delegate { return null; }),
ClientCertificateContext = SslStreamCertificateContext.Create(clientCert, null, false),
Comment on lines +156 to +167
Copy link
Member

Choose a reason for hiding this comment

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

I assume all of these are non-default values? Might be worth a comment mentioning that.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. With default value they work out of the box e.g. the test would not see if we fail to copy some value.

};

// There is consistency check inside of the ShallowClone
_ = clientOptions.ShallowClone();
}

}

public sealed class SslClientAuthenticationOptionsTestBase_Sync : SslClientAuthenticationOptionsTestBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs"
Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SslClientAuthenticationOptionsExtensions.cs"
Link="Common\System\Net\Security\SslClientAuthenticationOptionsExtensions.cs" />
<Compile Include="..\..\src\System\Net\Security\TlsFrameHelper.cs"
Link="src\TlsFrameHelper.cs" />
<Compile Include="TlsFrameHelperTests.cs" />
Expand Down
Loading