test(data): cover sessions, SQLite binding and PostgreSQL wire client
Shared session tests run on SQLite and PostgreSQL (types, nulls, placeholders, transactions, constraint errors, COPY/bulk escaping, cancellation). PostgreSQL tests cover SCRAM/MD5/cleartext/trust auth, TLS, result decoding, bytea, LISTEN/NOTIFY, error mapping, pooling and broken-connection recovery, and the statement cache. In-memory SQLite now pools shared-cache connections and retries SQLITE_LOCKED. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
8 files changed
+1015
-22
No files matched your search
@@ -0,0 +1,106 @@
|
||||
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));
|
||||
scram.VerifyServerFinal("v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4="u8);
|
||||
Assert.Throws<System.Security.Authentication.AuthenticationException>(() => scram.VerifyServerFinal("v=AAAA"u8));
|
||||
}
|
||||
|
||||
[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 DecodesByteaInHexAndEscapeFormats()
|
||||
{
|
||||
Assert.BytesEqual(new byte[] { 0xDE, 0xAD }, (byte[])PgTypes.Decode(PgTypes.Bytea, "\\xdead"u8));
|
||||
Assert.BytesEqual(new byte[] { (byte)'a', 0, (byte)'\\', 0xFF }, (byte[])PgTypes.Decode(PgTypes.Bytea, "a\\000\\\\\\377"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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user