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

@@ -1,7 +1,6 @@
package protocol
import (
"errors"
"testing"
)
@@ -14,7 +13,8 @@ func TestDetectMySQL(t *testing.T) {
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,
0x68, 0x61, 0x32, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64,
0x00,
}
// 2. A valid MariaDB banner (protocol-compatible)
@@ -37,46 +37,43 @@ func TestDetectMySQL(t *testing.T) {
// 5. A slice that starts correctly but is malformed (no null terminator)
malformedSlice := []byte{0x0a, 0x38, 0x2e, 0x30, 0x2e, 0x30, 0x01, 0x02, 0x03, 0x04, 0x05}
t.Run("MySQL 8", func(t *testing.T) {
p, c, _ := Detect(Server, mysql8Banner, 3306, 0)
if p == nil {
t.Fatal("expected MySQL protocol, got nil")
}
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
})
t.Run("MariaDB", func(t *testing.T) {
p, c, _ := Detect(Server, mariaDBBanner, 3306, 0)
if p == nil {
t.Fatal("expected MySQL protocol, got nil")
}
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
})
t.Run("Invalid HTTP", func(t *testing.T) {
_, _, err := Detect(Server, httpBanner, 1234, 80)
if !errors.Is(err, ErrUnknown) {
t.Fatalf("expected unknown format, got error %T: %q", err, err)
} else {
t.Logf("error %q, as expected", err)
}
})
t.Run("Too short", func(t *testing.T) {
_, _, err := Detect(Server, shortSlice, 3306, 1234)
if !errors.Is(err, ErrUnknown) {
t.Fatalf("expected unknown format, got error %T: %q", err, err)
} else {
t.Logf("error %q, as expected", err)
}
})
t.Run("Malformed", func(t *testing.T) {
_, _, err := Detect(Server, malformedSlice, 3306, 1234)
if !errors.Is(err, ErrUnknown) {
t.Fatalf("expected unknown format, got error %T: %q", err, err)
} else {
t.Logf("error %q, as expected", err)
}
testRunner(t, []*testCase{
{
Name: "MySQL server",
Direction: Server,
Data: mysql8Banner,
SrcPort: 3306,
WantProto: ProtocolMySQL,
WantConfidence: .85,
},
{
Name: "MariaDB server",
Direction: Server,
Data: mariaDBBanner,
SrcPort: 3306,
WantProto: ProtocolMySQL,
WantConfidence: .85,
},
{
Name: "Invalid HTTP",
Direction: Server,
Data: httpBanner,
SrcPort: 80,
WantError: ErrUnknown,
},
{
Name: "Invalid too short",
Direction: Server,
Data: shortSlice,
SrcPort: 3306,
WantError: ErrUnknown,
},
{
Name: "Invalid malformed",
Direction: Server,
Data: malformedSlice,
SrcPort: 3306,
WantError: ErrUnknown,
},
})
}