Skip to content

Commit b869f12

Browse files
authored
fix: excption (#6)
* feature: SHA256验证 待测试 * fix: 移除gauss demo * fix: 移除 * feature: Example of adding and querying data. * Update Program.cs * fix: excption (1) Modify the branch enumeration for MD5SHA256. (2) Remove unsupported syntax keywords in test cases for GaussDB. * Update NpgsqlConnectionStringBuilder.cs
1 parent b77322a commit b869f12

File tree

9 files changed

+52
-36
lines changed

9 files changed

+52
-36
lines changed

src/Npgsql/BackendMessages/AuthenticationMessages.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,9 @@ internal static AuthenticationRequestMessage Load(NpgsqlReadBuffer buf)
6969
var passwordStoreTypeValue = buf.ReadInt32();
7070
var passwordStoreType = (PasswordStoreType)passwordStoreTypeValue;
7171

72-
return passwordStoreType switch
73-
{
74-
PasswordStoreType.PlainText => AuthenticationCleartextPasswordMessage.Instance,
75-
PasswordStoreType.MD5 => AuthenticationMD5PasswordMessage.Load(buf),
76-
PasswordStoreType.SHA256 => new AuthenticationSHA256PasswordMessage(
77-
passwordStoreType, buf
78-
),
79-
_ => throw new InvalidOperationException($"Not supported password store type({passwordStoreTypeValue})")
80-
};
72+
return new AuthenticationSHA256PasswordMessage(
73+
passwordStoreType, buf
74+
);
8175
}
8276
}
8377

@@ -87,6 +81,7 @@ sealed class AuthenticationMD5SHA256PasswordMessage : AuthenticationRequestMessa
8781

8882
internal ReadOnlyMemory<byte> Salt { get; }
8983
internal string RandomCode { get; }
84+
9085
public AuthenticationMD5SHA256PasswordMessage(NpgsqlReadBuffer buf)
9186
{
9287
RandomCode = buf.ReadString(64);
@@ -96,7 +91,6 @@ public AuthenticationMD5SHA256PasswordMessage(NpgsqlReadBuffer buf)
9691

9792
#endregion SHA256Password
9893

99-
10094
sealed class AuthenticationGSSMessage : AuthenticationRequestMessage
10195
{
10296
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.GSS;
@@ -147,5 +141,6 @@ enum PasswordStoreType
147141
{
148142
PlainText = 0,
149143
MD5 = 1,
150-
SHA256 = 2
144+
SHA256 = 2,
145+
MD5SHA256 = 3
151146
}

src/Npgsql/Internal/NpgsqlConnector.Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async Task Authenticate(string username, NpgsqlTimeout timeout, bool async, Canc
5252
break;
5353

5454
case AuthenticationRequestType.MD5SHA256Password:
55-
ThrowIfNotAllowed(requiredAuthModes, RequireAuthMode.ScramSHA256);
55+
ThrowIfNotAllowed(requiredAuthModes, RequireAuthMode.MD5SHA256);
5656
await AuthenticateMD5SHA256(username, (AuthenticationMD5SHA256PasswordMessage)msg, async, cancellationToken)
5757
.ConfigureAwait(false);
5858
break;

src/Npgsql/Internal/NpgsqlConnector.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,7 @@ internal ValueTask<IBackendMessage> ReadMessage(
15441544
AuthenticationRequestType.SSPI => AuthenticationSSPIMessage.Instance,
15451545
AuthenticationRequestType.GSSContinue => AuthenticationGSSContinueMessage.Load(buf, len),
15461546
AuthenticationRequestType.SHA256Password => AuthenticationSHA256PasswordMessage.Load(buf),
1547+
AuthenticationRequestType.MD5SHA256Password => new AuthenticationMD5SHA256PasswordMessage(buf),
15471548
_ => throw new NotSupportedException($"Authentication method not supported (Received: {authType})")
15481549
};
15491550

@@ -2315,13 +2316,13 @@ void GenerateResetMessage()
23152316
}
23162317
if (DatabaseInfo.SupportsDiscardSequences)
23172318
{
2318-
sb.Append("DISCARD SEQUENCES;");
2319-
_resetWithoutDeallocateResponseCount++;
2319+
//sb.Append("DISCARD SEQUENCES;");
2320+
//_resetWithoutDeallocateResponseCount++;
23202321
}
23212322
if (DatabaseInfo.SupportsDiscardTemp)
23222323
{
2323-
sb.Append("DISCARD TEMP");
2324-
_resetWithoutDeallocateResponseCount++;
2324+
//sb.Append("DISCARD TEMP");
2325+
//_resetWithoutDeallocateResponseCount++;
23252326
}
23262327

23272328
_resetWithoutDeallocateResponseCount++; // One ReadyForQuery at the end

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,9 +1830,13 @@ enum RequireAuthMode
18301830
/// </summary>
18311831
None = 32,
18321832
/// <summary>
1833+
/// MD5 SHA256.
1834+
/// </summary>
1835+
MD5SHA256 = 64,
1836+
/// <summary>
18331837
/// All authentication methods. For internal use.
18341838
/// </summary>
1835-
All = Password | MD5 | GSS | SSPI | ScramSHA256 | None
1839+
All = Password | MD5 | GSS | SSPI | ScramSHA256 | None | MD5SHA256
18361840
}
18371841

18381842
#endregion

test/Npgsql.Tests/AsyncTests.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,28 @@ public class AsyncTests : TestBase
1111
public async Task NonQuery()
1212
{
1313
await using var conn = await OpenConnectionAsync();
14+
15+
// 创建临时表(使用正确的临时表语法)
1416
var tableName = await CreateTempTable(conn, "int INTEGER");
15-
await using var cmd = conn.CreateCommand();
16-
cmd.CommandText = $"INSERT INTO {tableName} (int) VALUES (4)";
17-
await cmd.ExecuteNonQueryAsync();
18-
Assert.That(await conn.ExecuteScalarAsync($"SELECT int FROM {tableName}"), Is.EqualTo(4));
17+
await using (var createTableCmd = conn.CreateCommand())
18+
{
19+
createTableCmd.CommandText = $"CREATE TEMP TABLE {tableName} (int INTEGER)";
20+
await createTableCmd.ExecuteNonQueryAsync();
21+
}
22+
23+
// 插入数据
24+
await using (var insertCmd = conn.CreateCommand())
25+
{
26+
insertCmd.CommandText = $"INSERT INTO {tableName} (int) VALUES (4)";
27+
await insertCmd.ExecuteNonQueryAsync();
28+
}
29+
30+
// 验证结果
31+
await using (var selectCmd = conn.CreateCommand())
32+
{
33+
selectCmd.CommandText = $"SELECT int FROM {tableName}";
34+
Assert.That(await selectCmd.ExecuteScalarAsync(), Is.EqualTo(4));
35+
}
1936
}
2037

2138
[Test]

test/Npgsql.Tests/BugTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public async Task Bug2296()
396396
{
397397
await using var conn = await OpenConnectionAsync();
398398
// Note that the type has to be named boolean
399-
await conn.ExecuteNonQueryAsync("DROP TYPE IF EXISTS \"boolean\" CASCADE");
399+
await conn.ExecuteNonQueryAsync("DROP TYPE IF EXISTS \"boolean\" ");//CASCADE
400400
await conn.ExecuteNonQueryAsync("CREATE DOMAIN pg_temp.\"boolean\" AS bool");
401401
conn.ReloadTypes();
402402
var tableName = await CreateTempTable(conn, $"mybool \"boolean\"");

test/Npgsql.Tests/CommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ public async Task Not_cancel_prepended_query([Values] bool failPrependedQuery)
14961496
var queryTask = conn.ExecuteNonQueryAsync("SELECT 1", cancellationToken: cts.Token);
14971497

14981498
var server = await postmasterMock.WaitForServerConnection();
1499-
await server.ExpectSimpleQuery("DISCARD ALL");
1499+
//await server.ExpectSimpleQuery("DISCARD ALL");
15001500
await server.ExpectExtendedQuery();
15011501

15021502
var cancelTask = Task.Run(cts.Cancel);

test/Npgsql.Tests/SecurityTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public void No_ssl_renegotiation()
6363
});
6464
using var conn = dataSource.OpenConnection();
6565
Assert.That(conn.ExecuteScalar("SHOW ssl_renegotiation_limit"), Is.EqualTo("0"));
66-
conn.ExecuteNonQuery("DISCARD ALL");
67-
Assert.That(conn.ExecuteScalar("SHOW ssl_renegotiation_limit"), Is.EqualTo("0"));
66+
//conn.ExecuteNonQuery("DISCARD ALL");
67+
//Assert.That(conn.ExecuteScalar("SHOW ssl_renegotiation_limit"), Is.EqualTo("0"));
6868
}
6969

7070
[Test, Description("Makes sure that when SSL is disabled IsSecure returns false")]

test/Npgsql.Tests/TestUtil.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,9 @@ internal static async Task<string> CreateTempTable(NpgsqlConnection conn, string
202202
await conn.ExecuteNonQueryAsync(@$"
203203
START TRANSACTION;
204204
SELECT pg_advisory_xact_lock(0);
205-
DROP TABLE IF EXISTS {tableName} CASCADE;
205+
DROP TABLE IF EXISTS {tableName};
206206
COMMIT;
207207
CREATE TABLE {tableName} ({columns});");
208-
209208
return tableName;
210209
}
211210

@@ -219,7 +218,7 @@ internal static async Task<string> GetTempTableName(NpgsqlConnection conn)
219218
await conn.ExecuteNonQueryAsync(@$"
220219
START TRANSACTION;
221220
SELECT pg_advisory_xact_lock(0);
222-
DROP TABLE IF EXISTS {tableName} CASCADE;
221+
DROP TABLE IF EXISTS {tableName} ;
223222
COMMIT");
224223
return tableName;
225224
}
@@ -234,7 +233,7 @@ internal static async Task<string> CreateTempTable(NpgsqlDataSource dataSource,
234233
await dataSource.ExecuteNonQueryAsync(@$"
235234
START TRANSACTION;
236235
SELECT pg_advisory_xact_lock(0);
237-
DROP TABLE IF EXISTS {tableName} CASCADE;
236+
DROP TABLE IF EXISTS {tableName};
238237
COMMIT;
239238
CREATE TABLE {tableName} ({columns});");
240239
return tableName;
@@ -246,7 +245,7 @@ await dataSource.ExecuteNonQueryAsync(@$"
246245
internal static async Task<string> CreateTempSchema(NpgsqlConnection conn)
247246
{
248247
var schemaName = "temp_schema" + Interlocked.Increment(ref _tempSchemaCounter);
249-
await conn.ExecuteNonQueryAsync($"DROP SCHEMA IF EXISTS {schemaName} CASCADE; CREATE SCHEMA {schemaName}");
248+
await conn.ExecuteNonQueryAsync($"DROP SCHEMA IF EXISTS {schemaName} ; CREATE SCHEMA {schemaName}");
250249
return schemaName;
251250
}
252251

@@ -257,7 +256,7 @@ internal static async Task<string> CreateTempSchema(NpgsqlConnection conn)
257256
internal static async Task<string> GetTempViewName(NpgsqlConnection conn)
258257
{
259258
var viewName = "temp_view" + Interlocked.Increment(ref _tempViewCounter);
260-
await conn.ExecuteNonQueryAsync($"DROP VIEW IF EXISTS {viewName} CASCADE");
259+
await conn.ExecuteNonQueryAsync($"DROP VIEW IF EXISTS {viewName} ");
261260
return viewName;
262261
}
263262

@@ -268,7 +267,7 @@ internal static async Task<string> GetTempViewName(NpgsqlConnection conn)
268267
internal static async Task<string> GetTempMaterializedViewName(NpgsqlConnection conn)
269268
{
270269
var viewName = "temp_materialized_view" + Interlocked.Increment(ref _tempViewCounter);
271-
await conn.ExecuteNonQueryAsync($"DROP MATERIALIZED VIEW IF EXISTS {viewName} CASCADE");
270+
await conn.ExecuteNonQueryAsync($"DROP MATERIALIZED VIEW IF EXISTS {viewName} ");
272271
return viewName;
273272
}
274273

@@ -279,7 +278,7 @@ internal static async Task<string> GetTempMaterializedViewName(NpgsqlConnection
279278
internal static async Task<string> GetTempFunctionName(NpgsqlConnection conn)
280279
{
281280
var functionName = "temp_func" + Interlocked.Increment(ref _tempFunctionCounter);
282-
await conn.ExecuteNonQueryAsync($"DROP FUNCTION IF EXISTS {functionName} CASCADE");
281+
await conn.ExecuteNonQueryAsync($"DROP FUNCTION IF EXISTS {functionName}");
283282
return functionName;
284283
}
285284

@@ -293,7 +292,7 @@ internal static async Task<string> GetTempFunctionName(NpgsqlConnection conn)
293292
internal static async Task<string> GetTempProcedureName(NpgsqlDataSource dataSource)
294293
{
295294
var procedureName = "temp_procedure" + Interlocked.Increment(ref _tempProcedureCounter);
296-
await dataSource.ExecuteNonQueryAsync($"DROP PROCEDURE IF EXISTS {procedureName} CASCADE");
295+
await dataSource.ExecuteNonQueryAsync($"DROP PROCEDURE IF EXISTS {procedureName} ");
297296
return procedureName;
298297
}
299298

@@ -307,7 +306,7 @@ internal static async Task<string> GetTempProcedureName(NpgsqlDataSource dataSou
307306
internal static async Task<string> GetTempProcedureName(NpgsqlConnection connection)
308307
{
309308
var procedureName = "temp_procedure" + Interlocked.Increment(ref _tempProcedureCounter);
310-
await connection.ExecuteNonQueryAsync($"DROP PROCEDURE IF EXISTS {procedureName} CASCADE");
309+
await connection.ExecuteNonQueryAsync($"DROP PROCEDURE IF EXISTS {procedureName} ");
311310
return procedureName;
312311
}
313312

@@ -318,7 +317,7 @@ internal static async Task<string> GetTempProcedureName(NpgsqlConnection connect
318317
internal static async Task<string> GetTempTypeName(NpgsqlConnection conn)
319318
{
320319
var typeName = "temp_type" + Interlocked.Increment(ref _tempTypeCounter);
321-
await conn.ExecuteNonQueryAsync($"DROP TYPE IF EXISTS {typeName} CASCADE");
320+
await conn.ExecuteNonQueryAsync($"DROP TYPE IF EXISTS {typeName}");
322321
return typeName;
323322
}
324323

0 commit comments

Comments
 (0)