Skip to content

Commit 87f3817

Browse files
authored
add test for SslClientAuthenticationOptions.ShallowClone (#88557)
* add test for SslClientAuthenticationOptions.ShallowClone * update comments * comment
1 parent f2c46b6 commit 87f3817

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/libraries/Common/src/System/Net/Security/SslClientAuthenticationOptionsExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal static class SslClientAuthenticationOptionsExtensions
1414
{
1515
public static SslClientAuthenticationOptions ShallowClone(this SslClientAuthenticationOptions options)
1616
{
17+
// Use non-default values to verify the clone works fine.
1718
var clone = new SslClientAuthenticationOptions()
1819
{
1920
AllowRenegotiation = options.AllowRenegotiation,
@@ -33,7 +34,10 @@ public static SslClientAuthenticationOptions ShallowClone(this SslClientAuthenti
3334

3435
#if DEBUG
3536
// Try to detect if a property gets added that we're not copying correctly.
36-
foreach (PropertyInfo pi in typeof(SslClientAuthenticationOptions).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
37+
// The property count is guard for new properties that also needs to be added above.
38+
PropertyInfo[] properties = typeof(SslClientAuthenticationOptions).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)!;
39+
Debug.Assert(properties.Length == 13);
40+
foreach (PropertyInfo pi in properties)
3741
{
3842
object? origValue = pi.GetValue(options);
3943
object? cloneValue = pi.GetValue(clone);

src/libraries/System.Net.Security/tests/FunctionalTests/SslAuthenticationOptionsTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
144144
Assert.Equal(string.Empty, server.TargetHostName);
145145
}
146146
}
147+
148+
[Fact]
149+
public void ClientOptions_ShallowCopy_OK()
150+
{
151+
using X509Certificate2 clientCert = Configuration.Certificates.GetClientCertificate();
152+
153+
// needs to non-default values so we can verify it was copied correctly.
154+
var clientOptions = new SslClientAuthenticationOptions
155+
{
156+
AllowRenegotiation = false,
157+
AllowTlsResume = false,
158+
ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http11, SslApplicationProtocol.Http2 },
159+
CertificateRevocationCheckMode = X509RevocationMode.Online,
160+
ClientCertificates = new X509CertificateCollection() { clientCert },
161+
EnabledSslProtocols = SslProtocols.Tls12,
162+
EncryptionPolicy = EncryptionPolicy.RequireEncryption,
163+
TargetHost = "foo",
164+
CertificateChainPolicy = new X509ChainPolicy(),
165+
RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }),
166+
LocalCertificateSelectionCallback = new LocalCertificateSelectionCallback(delegate { return null; }),
167+
ClientCertificateContext = SslStreamCertificateContext.Create(clientCert, null, false),
168+
};
169+
170+
// There is consistency check inside of the ShallowClone
171+
_ = clientOptions.ShallowClone();
172+
}
173+
147174
}
148175

149176
public sealed class SslClientAuthenticationOptionsTestBase_Sync : SslClientAuthenticationOptionsTestBase

src/libraries/System.Net.Security/tests/FunctionalTests/System.Net.Security.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
101101
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs"
102102
Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
103+
<Compile Include="$(CommonPath)System\Net\Security\SslClientAuthenticationOptionsExtensions.cs"
104+
Link="Common\System\Net\Security\SslClientAuthenticationOptionsExtensions.cs" />
103105
<Compile Include="..\..\src\System\Net\Security\TlsFrameHelper.cs"
104106
Link="src\TlsFrameHelper.cs" />
105107
<Compile Include="TlsFrameHelperTests.cs" />

0 commit comments

Comments
 (0)