-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMySQLIntegrationTest.cs
More file actions
202 lines (175 loc) · 7.3 KB
/
MySQLIntegrationTest.cs
File metadata and controls
202 lines (175 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using SuperSocket.MySQL;
namespace SuperSocket.MySQL.Test
{
/// <summary>
/// Integration tests for MySQL connection and handshake process.
/// These tests require a running MySQL server and should be configured via environment variables.
/// </summary>
public class MySQLIntegrationTest
{
[Fact]
[Trait("Category", "Integration")]
public async Task MySQLConnection_CompleteHandshakeFlow_ShouldAuthenticate()
{
// Arrange
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
try
{
// Act
await connection.ConnectAsync();
// Assert
Assert.True(connection.IsAuthenticated,
"Connection should be authenticated after successful handshake");
}
finally
{
// Cleanup
await connection.DisconnectAsync();
}
}
[Fact]
[Trait("Category", "Integration")]
public async Task MySQLConnection_InvalidCredentials_ShouldFailHandshake()
{
// Arrange
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, "nonexistent_user", "wrong_password");
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.ConnectAsync()
);
// The error could be "authentication failed" or "unsupported authentication plugin" depending on MySQL config
Assert.True(
exception.Message.ToLower().Contains("authentication failed") ||
exception.Message.ToLower().Contains("unsupported authentication plugin"),
$"Expected authentication failure message, got: {exception.Message}");
Assert.False(connection.IsAuthenticated,
"Connection should not be authenticated after failed handshake");
}
[Fact]
[Trait("Category", "Integration")]
public async Task MySQLConnection_CachingSha2PasswordUser_ShouldAuthenticate()
{
var userName = $"sha2_user_{Guid.NewGuid():N}";
var password = $"sha2_pass_{Guid.NewGuid():N}";
var host = TestConst.Host;
var adminConnection = new MySQLConnection(host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
MySQLConnection userConnection = null;
try
{
await adminConnection.ConnectAsync();
var pluginResult = await adminConnection.ExecuteQueryAsync(
"SELECT PLUGIN_NAME FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME='caching_sha2_password' AND PLUGIN_STATUS='ACTIVE'");
if (!pluginResult.IsSuccess || pluginResult.RowCount == 0)
return;
await adminConnection.ExecuteQueryAsync(
$"CREATE USER '{userName}'@'{host}' IDENTIFIED WITH caching_sha2_password BY '{password}'");
await adminConnection.ExecuteQueryAsync($"GRANT USAGE ON *.* TO '{userName}'@'{host}'");
userConnection = new MySQLConnection(host, TestConst.DefaultPort, userName, password);
await userConnection.ConnectAsync();
Assert.True(userConnection.IsAuthenticated,
"Connection should be authenticated using caching_sha2_password.");
}
finally
{
if (userConnection != null)
{
await userConnection.DisconnectAsync();
}
if (adminConnection.IsAuthenticated)
{
await adminConnection.ExecuteQueryAsync($"DROP USER IF EXISTS '{userName}'@'{host}'");
}
await adminConnection.DisconnectAsync();
}
}
[Fact]
[Trait("Category", "Integration")]
public async Task MySQLConnection_ConcurrentConnections_ShouldWork()
{
// Arrange
const int connectionCount = 5;
var connections = new MySQLConnection[connectionCount];
var tasks = new Task[connectionCount];
try
{
// Act - Create multiple concurrent connections
for (int i = 0; i < connectionCount; i++)
{
connections[i] = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
tasks[i] = connections[i].ConnectAsync();
}
await Task.WhenAll(tasks);
// Assert
for (int i = 0; i < connectionCount; i++)
{
Assert.True(connections[i].IsAuthenticated,
$"Connection {i} should be authenticated");
}
}
finally
{
// Cleanup
var disconnectTasks = new Task[connectionCount];
for (int i = 0; i < connectionCount; i++)
{
if (connections[i] != null)
{
disconnectTasks[i] = connections[i].DisconnectAsync();
}
}
await Task.WhenAll(disconnectTasks.Where(t => t != null));
}
}
/*
[Fact]
[Trait("Category", "Integration")]
public async Task MySQLConnection_ReconnectAfterDisconnect_ShouldWork()
{
// Arrange
var connection = new MySQLConnection(_host, _port, _username, _password);
try
{
// Act - First connection
await connection.ConnectAsync();
Assert.True(connection.IsAuthenticated, "First connection should be authenticated");
// Disconnect
await connection.DisconnectAsync();
Assert.False(connection.IsAuthenticated, "Should not be authenticated after disconnect");
// Reconnect
await connection.ConnectAsync();
// Assert
Assert.True(connection.IsAuthenticated, "Reconnection should be authenticated");
}
finally
{
// Cleanup
await connection.DisconnectAsync();
}
}
*/
[Fact]
[Trait("Category", "Integration")]
public async Task MySQLConnection_HandshakeTimeout_ShouldBeHandled()
{
// Arrange
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
using var cts = new System.Threading.CancellationTokenSource(TimeSpan.FromSeconds(10));
try
{
// Act
await connection.ConnectAsync(cts.Token);
// Assert
Assert.True(connection.IsAuthenticated, "Connection should complete within timeout");
}
finally
{
// Cleanup
await connection.DisconnectAsync();
}
}
}
}