Switch to new test harness

This commit is contained in:
2025-10-09 15:37:17 +02:00
parent 170a038612
commit fd55412020
8 changed files with 567 additions and 304 deletions

View File

@@ -21,17 +21,31 @@ func TestDetectPostgreSQLClient(t *testing.T) {
'd', 'a', 't', 'a', 'b', 'a', 's', 'e', 0x00, 't', 'e', 's', 't', 0x00, 0x00,
}
t.Run("Protocol 3.0", func(t *testing.T) {
p, c, err := Detect(Client, pgClientStartup, 0, 5432)
if err != nil {
t.Fatal(err)
return
}
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, 100*c)
if p.Name != ProtocolPostgreSQL {
t.Fatalf("expected postgres protocol, got %s", p.Name)
return
}
mysqlBanner := []byte{
0x0a, 0x38, 0x2e, 0x30, 0x2e, 0x33, 0x32, 0x00, 0x0d, 0x00, 0x00, 0x00,
0x04, 0x5a, 0x56, 0x5f, 0x3e, 0x6e, 0x76, 0x27, 0x00, 0xff, 0xff, 0xff,
0x02, 0x00, 0xff, 0xc7, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x63, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x5f, 0x73,
0x68, 0x61, 0x32, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64,
0x00,
}
testRunner(t, []*testCase{
{
Name: "PostgeSQL protocol 3.0",
Direction: Client,
Data: pgClientStartup,
DstPort: 5432,
WantProto: ProtocolPostgreSQL,
WantConfidence: .85,
},
{
Name: "Invalid MySQL server",
Direction: Server,
Data: mysqlBanner,
SrcPort: 3306,
WantError: ErrUnknown,
},
})
}
@@ -57,6 +71,24 @@ func TestDetectPostgreSQLServer(t *testing.T) {
// Invalid data (HTTP GET request)
httpBanner := []byte("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
testRunner(t, []*testCase{
{
Name: "PostgeSQL Sever AuthentcationOk",
Direction: Server,
Data: pgServerAuthOK,
DstPort: 5432,
WantProto: ProtocolPostgreSQL,
WantConfidence: .65,
},
{
Name: "Invalid HTTP request",
Direction: Server,
Data: httpBanner,
SrcPort: 3306,
WantError: ErrUnknown,
},
})
t.Run("AuthenticationOk", func(t *testing.T) {
p, c, err := Detect(Server, pgServerAuthOK, 5432, 0)
if err != nil {