-
Notifications
You must be signed in to change notification settings - Fork 343
Verify server ID before KILL QUERY to prevent cross-server cancellation #1575
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
a6b86d7
3e141d3
09e91c5
780bc2b
b0ee0f8
d2619e9
ae54398
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Diagnostics; | ||
|
||
namespace IntegrationTests; | ||
|
||
public class ServerIdentificationTests : IClassFixture<DatabaseFixture>, IDisposable | ||
{ | ||
public ServerIdentificationTests(DatabaseFixture database) | ||
{ | ||
m_database = database; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
[SkippableFact(ServerFeatures.Timeout)] | ||
public void CancelCommand_WithServerVerification() | ||
{ | ||
// This test verifies that cancellation still works with server verification | ||
using var connection = new MySqlConnection(AppConfig.ConnectionString); | ||
connection.Open(); | ||
|
||
Check failure on line 22 in tests/IntegrationTests/ServerIdentificationTests.cs
|
||
using var cmd = new MySqlCommand("SELECT SLEEP(5)", connection); | ||
var task = Task.Run(async () => | ||
{ | ||
await Task.Delay(TimeSpan.FromSeconds(0.5)); | ||
cmd.Cancel(); | ||
}); | ||
|
||
var stopwatch = Stopwatch.StartNew(); | ||
TestUtilities.AssertExecuteScalarReturnsOneOrIsCanceled(cmd); | ||
Assert.InRange(stopwatch.ElapsedMilliseconds, 250, 2500); | ||
|
||
#pragma warning disable xUnit1031 // Do not use blocking task operations in test method | ||
task.Wait(); // shouldn't throw | ||
#pragma warning restore xUnit1031 // Do not use blocking task operations in test method | ||
|
||
TestUtilities.LogInfo("Cancellation with server verification completed successfully"); | ||
Check failure on line 38 in tests/IntegrationTests/ServerIdentificationTests.cs
|
||
} | ||
|
||
[SkippableFact(ServerFeatures.KnownCertificateAuthority)] | ||
Check failure on line 41 in tests/IntegrationTests/ServerIdentificationTests.cs
|
||
public void ServerHasServerHostname() | ||
{ | ||
using var connection = new MySqlConnection(AppConfig.ConnectionString); | ||
connection.Open(); | ||
|
||
// Test that we can query server hostname | ||
using var cmd = new MySqlCommand("SELECT @@hostname", connection); | ||
var hostname = cmd.ExecuteScalar(); | ||
|
||
Check failure on line 50 in tests/IntegrationTests/ServerIdentificationTests.cs
|
||
// Hostname might be null on some server configurations, but the query should succeed | ||
TestUtilities.LogInfo($"Server hostname: {hostname ?? "null"}"); | ||
Check failure on line 52 in tests/IntegrationTests/ServerIdentificationTests.cs
|
||
} | ||
|
||
private readonly DatabaseFixture m_database; | ||
} | ||
Check failure on line 56 in tests/IntegrationTests/ServerIdentificationTests.cs
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using MySqlConnector.Core; | ||
using MySqlConnector.Logging; | ||
|
||
namespace MySqlConnector.Tests; | ||
|
||
public class ServerHostnameVerificationTests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole class is unnecessary; delete it. We can trust that C# properties can be set and then read. Only write useful tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted the entire ServerIdentificationTests class as requested - commit ae54398 |
||
{ | ||
[Fact] | ||
public void HostnameVerification_WithMatchingHostnames_AllowsCancellation() | ||
{ | ||
// Arrange | ||
var session1 = CreateServerSession(); | ||
var session2 = CreateServerSession(); | ||
session1.ServerHostname = "mysql-server-1"; | ||
session2.ServerHostname = "mysql-server-1"; | ||
|
||
// Act & Assert - this should not throw and should proceed with cancellation | ||
// In a real scenario, this would be tested through the DoCancel method | ||
Assert.Equal("mysql-server-1", session1.ServerHostname); | ||
Assert.Equal("mysql-server-1", session2.ServerHostname); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is even the point of this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted the entire test class as it was unnecessary - commit ae54398 |
||
} | ||
|
||
[Fact] | ||
public void HostnameVerification_WithDifferentHostnames_PreventsCancellation() | ||
{ | ||
// Arrange | ||
var session1 = CreateServerSession(); | ||
var session2 = CreateServerSession(); | ||
session1.ServerHostname = "mysql-server-1"; | ||
session2.ServerHostname = "mysql-server-2"; | ||
|
||
// Act & Assert - this should prevent cancellation | ||
Assert.NotEqual(session1.ServerHostname, session2.ServerHostname); | ||
} | ||
|
||
[Fact] | ||
public void HostnameVerification_WithNullHostnames_AllowsCancellationForBackwardCompatibility() | ||
{ | ||
// Arrange | ||
var session1 = CreateServerSession(); | ||
var session2 = CreateServerSession(); | ||
session1.ServerHostname = null; | ||
session2.ServerHostname = null; | ||
|
||
// Act & Assert - should allow cancellation for backward compatibility | ||
Assert.Null(session1.ServerHostname); | ||
Assert.Null(session2.ServerHostname); | ||
} | ||
|
||
[Fact] | ||
public void HostnameVerification_WithOneNullHostname_PreventsCancellation() | ||
{ | ||
// Arrange | ||
var session1 = CreateServerSession(); | ||
var session2 = CreateServerSession(); | ||
session1.ServerHostname = "mysql-server-1"; | ||
session2.ServerHostname = null; | ||
|
||
// Act & Assert - one has hostname, other doesn't - should prevent cancellation | ||
Assert.NotNull(session1.ServerHostname); | ||
Assert.Null(session2.ServerHostname); | ||
} | ||
|
||
private static ServerSession CreateServerSession() | ||
{ | ||
var pool = new TestConnectionPool(); | ||
return new ServerSession(NullLogger.Instance, pool); | ||
} | ||
|
||
private class TestConnectionPool : IConnectionPoolMetadata | ||
{ | ||
public int Id => 1; | ||
public int Generation => 1; | ||
public ConnectionPool? ConnectionPool => null; | ||
Check failure on line 75 in tests/MySqlConnector.Tests/ServerIdentificationTests.cs
|
||
public int GetNewSessionId() => 1; | ||
} | ||
} | ||
Check failure on line 78 in tests/MySqlConnector.Tests/ServerIdentificationTests.cs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no TestUtilities.LogInfo method. Delete all logging from tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed all TestUtilities.LogInfo calls from the integration tests in commit d4ba8b8.