Skip to content

Commit 138a5a5

Browse files
rzikmCopilot
andcommitted
Harden RequestClientCertificate staging and shutdown timeout in tests
Two test robustness fixes: - ServerSession_RequestClientCertificate_ReadWriteDuringSecondHandshake_Throws staged the post-handshake CertificateRequest with a single RequestClientCertificate call and asserted Complete. The API contract allows DestinationTooSmall when the flight does not fit the destination, so drain in a do/while (matching DriveClientCertNegotiationAsync) until the status is terminal before asserting. - SocketBoundServerSession_Shutdown_DeliversCloseNotifyToSslStreamClient computed its drain-loop deadline from DateTime.UtcNow, which is not monotonic and can drift with clock adjustments. Use Environment.TickCount64 instead. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 89084543-7d70-4562-8776-d4ca1baa3c7c
1 parent c03a62f commit 138a5a5

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

‎src/libraries/System.Net.Security/tests/FunctionalTests/TlsSessionTests.cs‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,16 +2224,24 @@ public async Task ServerSession_RequestClientCertificate_ReadWriteDuringSecondHa
22242224
Task<int> clientRead = clientSsl.ReadAsync(pingBuf).AsTask();
22252225

22262226
// Stage the post-handshake CertificateRequest and flush it. This re-arms the
2227-
// handshake state machine (IsHandshakeComplete flips back to false).
2227+
// handshake state machine (IsHandshakeComplete flips back to false). The staged
2228+
// CertificateRequest flight may not fit the destination in a single call, so drain
2229+
// until the status is no longer DestinationTooSmall before asserting completion.
22282230
byte[] netOut = new byte[CipherBufSize];
2229-
TlsOperationStatus reqStatus = session.RequestClientCertificate(netOut, out int produced);
2230-
Assert.Equal(TlsOperationStatus.Complete, reqStatus);
2231-
Assert.False(session.IsHandshakeComplete);
2232-
if (produced > 0)
2231+
TlsOperationStatus reqStatus;
2232+
do
22332233
{
2234-
await serverStream.WriteAsync(netOut.AsMemory(0, produced)).AsTask().WaitAsync(TimeSpan.FromSeconds(30));
2235-
await serverStream.FlushAsync().WaitAsync(TimeSpan.FromSeconds(30));
2234+
reqStatus = session.RequestClientCertificate(netOut, out int produced);
2235+
Assert.NotEqual(TlsOperationStatus.Closed, reqStatus);
2236+
if (produced > 0)
2237+
{
2238+
await serverStream.WriteAsync(netOut.AsMemory(0, produced)).AsTask().WaitAsync(TimeSpan.FromSeconds(30));
2239+
await serverStream.FlushAsync().WaitAsync(TimeSpan.FromSeconds(30));
2240+
}
22362241
}
2242+
while (reqStatus == TlsOperationStatus.DestinationTooSmall);
2243+
Assert.Equal(TlsOperationStatus.Complete, reqStatus);
2244+
Assert.False(session.IsHandshakeComplete);
22372245

22382246
// While the second handshake is in progress, application Read/Write are rejected.
22392247
byte[] scratch = new byte[CipherBufSize];
@@ -2689,13 +2697,13 @@ public async Task SocketBoundServerSession_Shutdown_DeliversCloseNotifyToSslStre
26892697
Assert.True(session.IsHandshakeComplete);
26902698

26912699
TlsOperationStatus status;
2692-
DateTime shutdownDeadline = DateTime.UtcNow.AddSeconds(30);
2700+
long shutdownDeadline = Environment.TickCount64 + 30_000;
26932701
while (true)
26942702
{
26952703
status = session.Shutdown();
26962704
if (status == TlsOperationStatus.DestinationTooSmall)
26972705
{
2698-
Assert.True(DateTime.UtcNow < shutdownDeadline, "Shutdown() kept returning DestinationTooSmall and did not complete within the timeout.");
2706+
Assert.True(Environment.TickCount64 < shutdownDeadline, "Shutdown() kept returning DestinationTooSmall and did not complete within the timeout.");
26992707
await Task.Delay(5);
27002708
continue;
27012709
}

0 commit comments

Comments
 (0)