Repository navigation
[7.0.4] Validate TDS parser prerequisites and invalidate malformed responses (#4941) #4982
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Microsoft.Data.SqlClient/tests/Common/ConnectionPoolVersionScope.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.Tests.Common; | ||
|
|
||
| /// <summary> | ||
| /// Selects the connection pool implementation (<c>WaitHandleDbConnectionPool</c> or | ||
| /// <c>ChannelDbConnectionPool</c>) for the duration of a test. | ||
| /// | ||
| /// A pool is bound to an implementation when it is created, so simply flipping the | ||
| /// <c>UseConnectionPoolV2</c> switch is not enough: pools created before the switch was flipped | ||
| /// keep their original implementation, and pools created inside the scope would otherwise outlive | ||
| /// it and leak the chosen implementation into unrelated tests. This scope therefore clears all | ||
| /// pools both on entry and on exit. | ||
| /// | ||
| /// This follows the RAII pattern; construct it at the start of a test and dispose it at the end. | ||
| /// Like <see cref="LocalAppContextSwitchesHelper"/>, it manipulates global state and enforces a | ||
| /// single-instance policy, so it must not be held for longer than necessary. | ||
| /// </summary> | ||
| public sealed class ConnectionPoolVersionScope : IDisposable | ||
| { | ||
| private readonly LocalAppContextSwitchesHelper _switches; | ||
|
|
||
| /// <summary> | ||
| /// Clears all existing pools and selects the requested pool implementation. | ||
| /// </summary> | ||
| /// <param name="usePoolV2"> | ||
| /// True to use <c>ChannelDbConnectionPool</c>; false to use <c>WaitHandleDbConnectionPool</c>. | ||
| /// </param> | ||
| public ConnectionPoolVersionScope(bool usePoolV2) | ||
| { | ||
| _switches = new LocalAppContextSwitchesHelper(); | ||
|
|
||
| try | ||
| { | ||
| SqlConnection.ClearAllPools(); | ||
| _switches.UseConnectionPoolV2 = usePoolV2; | ||
| } | ||
| catch | ||
| { | ||
| _switches.Dispose(); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Clears all pools created under the selected implementation and restores the original | ||
| /// switch values. | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| try | ||
| { | ||
| SqlConnection.ClearAllPools(); | ||
| } | ||
| finally | ||
| { | ||
| _switches.Dispose(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...soft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ParserResultProcessingTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Data; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Verifies that parser prerequisite validation preserves valid login, nullable rows, | ||
| /// result skipping, and subsequent commands against SQL Server. | ||
| /// </summary> | ||
| [Trait("Set", "2")] | ||
| public class ParserResultProcessingTest | ||
| { | ||
| /// <summary> | ||
| /// Consumes null and non-null values, skips unread rows, and reuses the connection with | ||
| /// synchronous/asynchronous APIs and both buffered and sequential access. | ||
| /// </summary> | ||
| [ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
| [InlineData(false, false)] | ||
| [InlineData(false, true)] | ||
| [InlineData(true, false)] | ||
| [InlineData(true, true)] | ||
| public async Task ValidResults_PreserveConnection(bool useAsync, bool sequential) | ||
| { | ||
| using SqlConnection connection = new(DataTestUtility.TCPConnectionString); | ||
| if (useAsync) | ||
| { | ||
| await connection.OpenAsync(); | ||
| } | ||
| else | ||
| { | ||
| connection.Open(); | ||
| } | ||
|
|
||
| // Sorting puts NULL first and leaves a deterministic unread row before the second result. | ||
| using SqlCommand command = new( | ||
| "SELECT CAST(NULL AS int) AS value UNION ALL SELECT 1 UNION ALL SELECT 2 " + | ||
| "ORDER BY value; " + | ||
| "SELECT 42 AS value;", connection); | ||
| CommandBehavior behavior = sequential | ||
| ? CommandBehavior.SequentialAccess | ||
| : CommandBehavior.Default; | ||
| using (SqlDataReader reader = useAsync | ||
| ? await command.ExecuteReaderAsync(behavior) | ||
| : command.ExecuteReader(behavior)) | ||
| { | ||
| Assert.True(useAsync ? await reader.ReadAsync() : reader.Read()); | ||
| Assert.True(reader.IsDBNull(0)); | ||
| Assert.True(useAsync ? await reader.ReadAsync() : reader.Read()); | ||
| Assert.Equal(1, reader.GetInt32(0)); | ||
| // Skip the unread value 2 and verify that the next result uses its own metadata. | ||
| Assert.True(useAsync ? await reader.NextResultAsync() : reader.NextResult()); | ||
| Assert.True(useAsync ? await reader.ReadAsync() : reader.Read()); | ||
| Assert.Equal(42, reader.GetInt32(0)); | ||
| Assert.False(useAsync ? await reader.ReadAsync() : reader.Read()); | ||
| Assert.False(useAsync ? await reader.NextResultAsync() : reader.NextResult()); | ||
| } | ||
|
|
||
| // A subsequent command proves valid result processing did not invalidate the connection. | ||
| command.CommandText = "SELECT 7"; | ||
| object result = useAsync | ||
| ? await command.ExecuteScalarAsync() | ||
| : command.ExecuteScalar(); | ||
| Assert.Equal(7, result); | ||
| Assert.Equal(ConnectionState.Open, connection.State); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
....Data.SqlClient/tests/UnitTests/SimulatedServerTests/Fixtures/SimulatedServerPoolScope.cs
|
cheenamalhotra marked this conversation as resolved.
Resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.UnitTests.SimulatedServerTests; | ||
|
|
||
| /// <summary> | ||
| /// Gives a scenario its own pool and clears it before its ephemeral servers are disposed. | ||
| /// Declare before the scenario's connections so they are returned before the pool is cleared. | ||
| /// </summary> | ||
| internal sealed class SimulatedServerPoolScope : IDisposable | ||
| { | ||
| private readonly SqlConnection _poolKey; | ||
|
|
||
| /// <summary> | ||
| /// Isolates the builder's pool from other test cases and previous uses of the same TCP port. | ||
| /// </summary> | ||
| public SimulatedServerPoolScope(SqlConnectionStringBuilder builder) | ||
| { | ||
| builder.ApplicationName = Guid.NewGuid().ToString(); | ||
| _poolKey = new SqlConnection(builder.ConnectionString); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Removes only this scenario's pool, without affecting other simulator clients. | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| SqlConnection.ClearPool(_poolKey); | ||
| _poolKey.Dispose(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.