- SQLite restore holds every pool slot instead of the write lock first, removing a lock-order deadlock with sessions that begin to write. - PostgreSQL sessions always return their connection on dispose, a rollback while disposing never masks the original exception, and a busy session keeps its transaction until it is discarded. - The pool disposes a connection whose health check the caller cancels. - Listener commands keep their lock until the server answers and never tear a write with the caller's token; closing and pending no longer race. - Tag replacement locks the sequence and the tag row; unused-tag cleanup locks candidates and re-checks them with a fresh snapshot; render counts and stats refreshes lock the sequence row on PostgreSQL. - Sequence upserts keep rendered_count, which renders maintain. - AuthenticationOk after an unfinished SCRAM exchange is rejected. - Oversized read buffers are released; failed backups leave no .tmp; a zero idle lifetime is rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
113 lines
5.0 KiB
C#
113 lines
5.0 KiB
C#
using System.Buffers;
|
|
using System.Text;
|
|
using Titano.Data.Postgres;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Data.Tests.Postgres;
|
|
|
|
/// <summary>Protocol pieces that need no server.</summary>
|
|
public sealed class PgProtocolUnitTests
|
|
{
|
|
[Test]
|
|
public void ScramMatchesTheRfc7677Example()
|
|
{
|
|
var scram = new ScramSha256("pencil", "user", "rOprNGfwEbeRWgbNEkqO");
|
|
Assert.Equal("n,,n=user,r=rOprNGfwEbeRWgbNEkqO", Encoding.UTF8.GetString(scram.ClientFirstMessage()));
|
|
var final = scram.ClientFinalMessage(Encoding.UTF8.GetBytes(
|
|
"r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096"));
|
|
Assert.Equal(
|
|
"c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=",
|
|
Encoding.UTF8.GetString(final));
|
|
Assert.False(scram.IsServerVerified);
|
|
Assert.Throws<System.Security.Authentication.AuthenticationException>(() => scram.VerifyServerFinal("v=AAAA"u8));
|
|
Assert.False(scram.IsServerVerified);
|
|
scram.VerifyServerFinal("v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4="u8);
|
|
Assert.True(scram.IsServerVerified);
|
|
}
|
|
|
|
[Test]
|
|
public void ScramRejectsANonceTheClientDidNotStart()
|
|
{
|
|
var scram = new ScramSha256("pencil", "user", "abc");
|
|
scram.ClientFirstMessage();
|
|
Assert.Throws<System.Security.Authentication.AuthenticationException>(
|
|
() => scram.ClientFinalMessage("r=xyz123,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096"u8));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("plain-ascii", "plain-ascii")]
|
|
[Case("I\u00ADX", "IX")]
|
|
[Case("a\u00A0b", "a b")]
|
|
[Case("\u2168", "IX")]
|
|
[Case("\u00AA", "a")]
|
|
[Case("bell\u0007", "bell\u0007")]
|
|
[Case("x\u00ADy\u0007", "x\u00ADy\u0007")]
|
|
public void SaslPrepMapsNormalisesAndFallsBack(string input, string expected) =>
|
|
Assert.Equal(expected, ScramSha256.SaslPrep(input));
|
|
|
|
[Test]
|
|
public void Md5ResponseMatchesPostgresFormula() =>
|
|
Assert.Equal("md534d7feefdad153ddeacb423a9893b99e", PgMd5.Response("titano_md5", "md5-pw", [1, 2, 3, 4]));
|
|
|
|
[Test]
|
|
public void CopyTextEscapesSpecialsAndEncodesValues()
|
|
{
|
|
var buffer = new ArrayBufferWriter<byte>();
|
|
foreach (var value in new object?[] { "a\tb\nc\rd\\e ✓", null, new byte[] { 0, 0xAB }, true, 42L, Guid.Empty, new DateTime(2020, 1, 2, 3, 4, 5), 2.5f })
|
|
{
|
|
PgTypes.WriteCopyValue(value, buffer);
|
|
buffer.Write("|"u8);
|
|
}
|
|
Assert.Equal(
|
|
"a\\tb\\nc\\rd\\\\e ✓|\\N|\\\\x00ab|t|42|00000000-0000-0000-0000-000000000000|2020-01-02T03:04:05.000000|2.5|",
|
|
Encoding.UTF8.GetString(buffer.WrittenSpan));
|
|
}
|
|
|
|
[Test]
|
|
public void DecodesHexByteaAndRejectsOtherForms()
|
|
{
|
|
Assert.BytesEqual(new byte[] { 0xDE, 0xAD, 0x01 }, (byte[])PgTypes.Decode(PgTypes.Bytea, "\\xdeAD01"u8));
|
|
Assert.Equal(0, ((byte[])PgTypes.Decode(PgTypes.Bytea, "\\x"u8)).Length);
|
|
Assert.Throws<FormatException>(() => PgTypes.Decode(PgTypes.Bytea, "a\\000"u8));
|
|
Assert.Throws<FormatException>(() => PgTypes.Decode(PgTypes.Bytea, "\\xabc"u8));
|
|
Assert.Throws<FormatException>(() => PgTypes.Decode(PgTypes.Bytea, "\\xzz"u8));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("SELECT 1", true)]
|
|
[Case(" -- comment\n /* block */ select * from t", true)]
|
|
[Case("VALUES (1)", true)]
|
|
[Case("SHOW TimeZone", true)]
|
|
[Case("SELECTED", false)]
|
|
[Case("UPDATE t SET a = 1", false)]
|
|
[Case("WITH x AS (DELETE FROM t RETURNING *) SELECT * FROM x", false)]
|
|
public void RecognisesRepeatableReads(string sql, bool expected) =>
|
|
Assert.Equal(expected, PostgresSession.IsReadOnlyQuery(sql));
|
|
|
|
[Test]
|
|
public void ParsesConnectionStrings()
|
|
{
|
|
var s = PostgresSettings.Parse("Host=db.local; Port=6543; Username=me; Password='p;w''d'; SslMode=verify-full; Maximum Pool Size=7; Search Path=s1");
|
|
Assert.Equal("db.local", s.Host);
|
|
Assert.Equal(6543, s.Port);
|
|
Assert.Equal("me", s.Database);
|
|
Assert.Equal("p;w'd", s.Password);
|
|
Assert.Equal(SslMode.VerifyFull, s.SslMode);
|
|
Assert.Equal(7, s.MaxPoolSize);
|
|
Assert.Equal("s1", s.SearchPath);
|
|
Assert.Equal("titano-server", s.ApplicationName);
|
|
Assert.Throws<FormatException>(() => PostgresSettings.Parse("Host=x;Username=u;SslMode=allowish"));
|
|
Assert.Throws<FormatException>(() => PostgresSettings.Parse("Host=x;Username=u;Pasword=typo"));
|
|
Assert.Throws<FormatException>(() => PostgresSettings.Parse("Host=x;Username=u;Minimum Pool Size=5;Maximum Pool Size=2"));
|
|
}
|
|
|
|
[Test]
|
|
public void InfersTheDialectFromTheConnectionString()
|
|
{
|
|
Assert.Equal(Dialect.Postgres, DatabaseOptions.Parse("Server=x;User Id=u").Dialect);
|
|
Assert.Equal(Dialect.Sqlite, DatabaseOptions.Parse("Data Source=/tmp/a.db").Dialect);
|
|
Assert.Equal(Dialect.Sqlite, DatabaseOptions.Parse(@"C:\Users\me\titano.db").Dialect);
|
|
Assert.Throws<FormatException>(() => DatabaseOptions.Parse("Foo=bar"));
|
|
}
|
|
}
|