-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthenticationErrorHandlingTest.cs
More file actions
107 lines (88 loc) · 3.71 KB
/
AuthenticationErrorHandlingTest.cs
File metadata and controls
107 lines (88 loc) · 3.71 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
using System;
using System.Threading.Tasks;
using Xunit;
using SuperSocket.MySQL;
namespace SuperSocket.MySQL.Test
{
/// <summary>
/// Tests specifically for authentication error handling scenarios
/// mentioned in the problem statement
/// </summary>
public class AuthenticationErrorHandlingTest
{
[Fact]
public async Task ConnectAsync_WhenServerUnreachable_ShouldContainAuthenticationFailed()
{
// Arrange
var connection = new MySQLConnection("unreachable-host-that-does-not-exist", 3306, "user", "password");
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.ConnectAsync()
);
Assert.Contains("authentication failed", exception.Message.ToLower());
}
[Fact]
public async Task ConnectAsync_WhenPortClosed_ShouldContainAuthenticationFailed()
{
// Arrange - Use a port that's unlikely to be open
var connection = new MySQLConnection("localhost", 9999, "user", "password");
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.ConnectAsync()
);
Assert.Contains("authentication failed", exception.Message.ToLower());
}
[Fact]
public async Task ConnectAsync_WhenNetworkError_ShouldContainAuthenticationFailed()
{
// Arrange - Use an invalid hostname that should cause DNS resolution failure
var connection = new MySQLConnection("invalid.invalid.invalid", 3306, "user", "password");
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.ConnectAsync()
);
Assert.Contains("authentication failed", exception.Message.ToLower());
}
[Fact]
public async Task DisconnectAsync_WhenConnectionNeverEstablished_ShouldNotThrowNullReference()
{
// Arrange
var connection = new MySQLConnection("localhost", 3306, "user", "password");
// Act & Assert - This should not throw NullReferenceException
await connection.DisconnectAsync();
// Verify authentication state is reset
Assert.False(connection.IsAuthenticated);
}
[Fact]
public async Task DisconnectAsync_AfterFailedConnection_ShouldNotThrowNullReference()
{
// Arrange
var connection = new MySQLConnection("unreachable-host", 3306, "user", "password");
try
{
// Try to connect (this will fail)
await connection.ConnectAsync();
}
catch (InvalidOperationException)
{
// Expected failure
}
// Act & Assert - This should not throw NullReferenceException
await connection.DisconnectAsync();
// Verify authentication state is reset
Assert.False(connection.IsAuthenticated);
}
[Fact]
public async Task DisconnectAsync_CalledMultipleTimes_ShouldNotThrow()
{
// Arrange
var connection = new MySQLConnection("localhost", 3306, "user", "password");
// Act & Assert - Multiple calls should not throw
await connection.DisconnectAsync();
await connection.DisconnectAsync();
await connection.DisconnectAsync();
// Verify authentication state is reset
Assert.False(connection.IsAuthenticated);
}
}
}