|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#if NET |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Net; |
| 9 | +using System.Net.Sockets; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.Data.ProviderBase; |
| 13 | +using Microsoft.Data.SqlClient.ManagedSni; |
| 14 | +using Microsoft.Data.SqlClient.Parser; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Microsoft.Data.SqlClient.UnitTests.ManagedSni |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Tests for <see cref="SniTcpHandle"/>. |
| 21 | + /// </summary> |
| 22 | + public sealed class SniTcpHandleTests |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// Upper bound for EnableSsl to return. Generous, so it only separates "honors the token" |
| 26 | + /// from "blocks until the operating system gives up on the socket". |
| 27 | + /// </summary> |
| 28 | + private static readonly TimeSpan HandshakeBudget = TimeSpan.FromSeconds(30); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// When the peer accepts the TLS ClientHello and never answers, EnableSsl must stop the |
| 32 | + /// handshake once its token is cancelled and report an SNI timeout error, for both TLS |
| 33 | + /// inside TDS and TLS-first (strict encryption). This is what bounds the handshake by |
| 34 | + /// Connect Timeout (GitHub issues #4834 and #5045). |
| 35 | + /// </summary> |
| 36 | + /// <param name="tlsFirst">Whether the handle negotiates TLS before any TDS traffic.</param> |
| 37 | + [Theory] |
| 38 | + [InlineData(false)] |
| 39 | + [InlineData(true)] |
| 40 | + public async Task EnableSsl_PeerNeverAnswersHandshake_ReturnsTimeoutErrorWhenTokenIsCancelled(bool tlsFirst) |
| 41 | + { |
| 42 | + // Arrange |
| 43 | + // Connect the handle to a local peer that will stay silent. |
| 44 | + using TcpListener listener = new(IPAddress.Loopback, 0); |
| 45 | + listener.Start(); |
| 46 | + SQLDNSInfo? pendingDnsInfo = null; |
| 47 | + SniTcpHandle handle = new( |
| 48 | + IPAddress.Loopback.ToString(), |
| 49 | + ((IPEndPoint)listener.LocalEndpoint).Port, |
| 50 | + TimeoutTimer.StartNew(TimeSpan.FromSeconds(5)), |
| 51 | + parallel: false, |
| 52 | + SqlConnectionIPAddressPreference.IPv4First, |
| 53 | + cachedFQDN: nameof(SniTcpHandleTests), |
| 54 | + ref pendingDnsInfo, |
| 55 | + tlsFirst, |
| 56 | + hostNameInCertificate: null, |
| 57 | + serverCertificateFilename: null); |
| 58 | + using CancellationTokenSource acceptTimeout = new(TimeSpan.FromSeconds(5)); |
| 59 | + using Socket peer = await listener.AcceptSocketAsync(acceptTimeout.Token); |
| 60 | + Assert.Equal(TdsEnums.SNI_SUCCESS, handle.Status); |
| 61 | + using CancellationTokenSource handshakeTimeout = new(TimeSpan.FromMilliseconds(500)); |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + // Act |
| 66 | + // SNI errors are thread-static, so read the error on the thread that ran EnableSsl. |
| 67 | + Task<(uint Result, SniError Error)> enableSsl = Task.Run( |
| 68 | + () => (handle.EnableSsl(0, handshakeTimeout.Token), SniLoadHandle.LastError)); |
| 69 | + Task settled = await Task.WhenAny(enableSsl, Task.Delay(HandshakeBudget)); |
| 70 | + |
| 71 | + // Assert |
| 72 | + Assert.True( |
| 73 | + ReferenceEquals(enableSsl, settled), |
| 74 | + $"EnableSsl did not return within {HandshakeBudget.TotalSeconds:N0}s after its token was cancelled."); |
| 75 | + (uint result, SniError error) = await enableSsl; |
| 76 | + Assert.Equal(TdsEnums.SNI_ERROR, result); |
| 77 | + Assert.Equal(SniProviders.TCP_PROV, error.provider); |
| 78 | + Assert.Equal((uint)SniCommon.ConnTimeoutError, error.sniError); |
| 79 | + Assert.Equal((int)TdsEnums.SNI_WAIT_TIMEOUT, error.nativeError); |
| 80 | + } |
| 81 | + finally |
| 82 | + { |
| 83 | + // Cleanup |
| 84 | + // Disposing the handle also releases a handshake that ignored the token. |
| 85 | + handle.Dispose(); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#endif |
0 commit comments