diff --git a/fbconn/fbconn.go b/fbconn/fbconn.go index e55a22e..2a71998 100644 --- a/fbconn/fbconn.go +++ b/fbconn/fbconn.go @@ -1031,7 +1031,7 @@ func (fbConn *FbConn) parseDatabaseInfo(buf []byte) error { if err != nil { return err } - fbConn.serverVersion, err = parseServerVersion(v) + fbConn.serverVersion, err = parseDatabaseVersionInfo(v) if err != nil { return err } diff --git a/fbconn/server_info_test.go b/fbconn/server_info_test.go index ee3c6f8..dcc4738 100644 --- a/fbconn/server_info_test.go +++ b/fbconn/server_info_test.go @@ -24,7 +24,8 @@ func tagBufferValue(tag byte, value []byte) []byte { func TestParseDatabaseInfo(t *testing.T) { attachmentID := uint64(1<<63 + 42) - buf := tagBufferValue(fbproto.Isc_info_firebird_version, []byte("WI-V6.0.0")) + version := "WI-V6.0.0.2459 Firebird 6.0" + buf := tagBufferValue(fbproto.Isc_info_firebird_version, append([]byte{1, byte(len(version))}, version...)) buf = append(buf, tagBufferValue( fbproto.Isc_info_attachment_id, fbio.AppendInt64LE(nil, int64(attachmentID)), diff --git a/fbconn/server_version.go b/fbconn/server_version.go index 37fe123..acd1886 100644 --- a/fbconn/server_version.go +++ b/fbconn/server_version.go @@ -6,7 +6,7 @@ import ( "strconv" ) -var serverVersionRegexp = regexp.MustCompile(`-V(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?`) +var serverVersionRegexp = regexp.MustCompile(`^\w{2}-\w(\d+)\.(\d+)\.(\d+)\.(\d+)(?:-\S+)? .+$`) // ServerVersion identifies the Firebird or RedDatabase server version. // @@ -61,9 +61,6 @@ func parseServerVersion(raw string) (ServerVersion, error) { parts := [4]int{} for i, match := range matches[1:] { - if match == "" { - continue - } part, err := strconv.Atoi(match) if err != nil { return ServerVersion{}, fmt.Errorf("server version string parsing error: %q: %w", raw, err) @@ -78,3 +75,29 @@ func parseServerVersion(raw string) (ServerVersion, error) { Build: parts[3], }, nil } + +// parseDatabaseVersionInfo validates the length-prefixed entries and parses +// the first one, which contains the engine version. +func parseDatabaseVersionInfo(entries string) (ServerVersion, error) { + if len(entries) == 0 || entries[0] == 0 { + return ServerVersion{}, fmt.Errorf("database version info contains no entries") + } + + count := int(entries[0]) + entries = entries[1:] + var first string + for i := 0; i < count; i++ { + if len(entries) == 0 || int(entries[0]) > len(entries)-1 { + return ServerVersion{}, fmt.Errorf("database version info: truncated entry %d", i+1) + } + end := 1 + int(entries[0]) + if i == 0 { + first = entries[1:end] + } + entries = entries[end:] + } + if len(entries) != 0 { + return ServerVersion{}, fmt.Errorf("database version info contains trailing bytes") + } + return parseServerVersion(first) +} diff --git a/fbconn/server_version_test.go b/fbconn/server_version_test.go index b29703e..af5dcd8 100644 --- a/fbconn/server_version_test.go +++ b/fbconn/server_version_test.go @@ -3,10 +3,11 @@ package fbconn import ( "testing" + "github.com/firebirdsql/fbx/fbproto" "github.com/stretchr/testify/require" ) -func TestParseServerVersion(t *testing.T) { +func TestParseServiceServerVersion(t *testing.T) { tests := []struct { name string raw string @@ -33,30 +34,27 @@ func TestParseServerVersion(t *testing.T) { }, }, { - name: "database info with multiple version strings", - raw: "\x02\x1bLI-V4.0.6.3221 Firebird 4.04LI-V4.0.6.3221 Firebird 4.0/tcp/server/P17:C", - expected: ServerVersion{ - Major: 4, - Minor: 0, - Patch: 6, - Build: 3221, - }, + name: "snapshot", + raw: "LI-T6.0.0.2459 Firebird 6.0 2f725cf", + expected: ServerVersion{Major: 6, Build: 2459}, }, { - name: "missing optional components", - raw: "WI-V4 Firebird", - expected: ServerVersion{ - Major: 4, - }, + name: "experimental", + raw: "LI-X6.1.2.3456 Firebird 6.1", + expected: ServerVersion{Major: 6, Minor: 1, Patch: 2, Build: 3456}, + }, + { + name: "development suffix", + raw: "LI-T6.0.0.2459-dev Firebird 6.0", + expected: ServerVersion{Major: 6, Build: 2459}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := parseServerVersion(tt.raw) + actual, err := parseServiceServerVersion(serviceVersionReply(tt.raw)) require.NoError(t, err) require.Equal(t, tt.expected, actual) - require.Equal(t, tt.expected.String(), actual.String()) }) } } @@ -64,6 +62,42 @@ func TestParseServerVersion(t *testing.T) { func TestParseServerVersionRejectsInvalidValue(t *testing.T) { _, err := parseServerVersion("Firebird version unknown") require.EqualError(t, err, `server version string parsing error: "Firebird version unknown"`) + for _, raw := range []string{"WI-V4 Firebird", "LI-V4.0.0.0", "junk LI-V4.0.0.0 Firebird 4.0"} { + _, err := parseServerVersion(raw) + require.Error(t, err, "%q", raw) + } +} + +func TestParseDatabaseInfoVersionEntries(t *testing.T) { + first := "LI-T6.0.0.2459-dev Firebird 6.0" + second := "LI-V4.0.6.3221 Firebird 4.0/tcp/server/P17:C" + entries := append([]byte{2, byte(len(first))}, first...) + entries = append(entries, byte(len(second))) + entries = append(entries, second...) + info := append(tagBufferValue(fbproto.Isc_info_firebird_version, entries), fbproto.Isc_info_end) + + var conn FbConn + require.NoError(t, conn.parseDatabaseInfo(info)) + require.Equal(t, ServerVersion{Major: 6, Build: 2459}, conn.serverVersion) + require.Equal(t, 6, conn.serverMajorVersion) +} + +func TestParseDatabaseInfoRejectsMalformedVersionEntries(t *testing.T) { + const version = "LI-V4.0.0.0 Firebird 4.0" + for name, entries := range map[string][]byte{ + "missing count": nil, + "zero count": {0}, + "missing second entry": append([]byte{2, byte(len(version))}, version...), + "truncated entry": {1, 2, 'x'}, + "trailing bytes": append(append([]byte{1, byte(len(version))}, version...), 'y'), + "invalid first version": append([]byte{2, 1, 'x', byte(len(version))}, version...), + } { + t.Run(name, func(t *testing.T) { + info := append(tagBufferValue(fbproto.Isc_info_firebird_version, entries), fbproto.Isc_info_end) + var conn FbConn + require.Error(t, conn.parseDatabaseInfo(info)) + }) + } } func TestServerVersionCompare(t *testing.T) { diff --git a/fbconn/service_sweep_test.go b/fbconn/service_sweep_test.go index c479f81..c2302aa 100644 --- a/fbconn/service_sweep_test.go +++ b/fbconn/service_sweep_test.go @@ -20,7 +20,7 @@ func TestServiceConnSweepParameters(t *testing.T) { }{ "defaults need no version query": {}, "legacy RDB": {"LI-V3.0.8.0 RedDatabase", 2, 17, []byte{100, 2, 0, 0, 0}}, - "version 4 uses legacy tag": {"LI-V4.0.0.0", 2, 0, []byte{100, 2, 0, 0, 0}}, + "version 4 uses legacy tag": {"LI-V4.0.0.0 Firebird 4.0", 2, 0, []byte{100, 2, 0, 0, 0}}, "RDB 5": {"LI-V5.0.0.0 RedDatabase", 2, 17, []byte{52, 2, 0, 0, 0}}, "Firebird 5": {"WI-V5.0.3.1683 Firebird 5.0", 2, 0, []byte{52, 2, 0, 0, 0}}, "RDB 6 both parameters": {"LI-V6.0.0.0 RedDatabase", 2, 17, []byte{52, 2, 0, 0, 0, 101, 17, 0, 0, 0}},