From c8a6032cd5c35685a64890e337ccd078f7998465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Y=C4=B1ld=C4=B1r=C4=B1m?= Date: Fri, 10 Nov 2023 14:05:56 +0300 Subject: [PATCH] feat: validate dot in group name (#72) --- couchbase/metadata.go | 6 +++++- couchbase/metedata_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 couchbase/metedata_test.go diff --git a/couchbase/metadata.go b/couchbase/metadata.go index 1eeff37..c60b2a9 100644 --- a/couchbase/metadata.go +++ b/couchbase/metadata.go @@ -3,7 +3,9 @@ package couchbase import ( "context" "errors" + "github.com/couchbase/gocbcore/v10" "strconv" + "strings" "sync" "github.com/Trendyol/go-dcp/wrapper" @@ -19,7 +21,6 @@ import ( "github.com/json-iterator/go" - "github.com/couchbase/gocbcore/v10" "github.com/couchbase/gocbcore/v10/memd" ) @@ -147,5 +148,8 @@ func NewCBMetadata(client Client, config *config.Dcp) metadata.Metadata { func getCheckpointID(vbID uint16, groupName string) []byte { // _connector:cbgo:groupName:stdout-listener:checkpoint:vbId + if strings.Contains(groupName, ".") { + panic("can not get checkpoint id. unsupported group name includes dot") + } return []byte(helpers.Prefix + groupName + ":checkpoint:" + strconv.Itoa(int(vbID))) } diff --git a/couchbase/metedata_test.go b/couchbase/metedata_test.go new file mode 100644 index 0000000..963c8f1 --- /dev/null +++ b/couchbase/metedata_test.go @@ -0,0 +1,24 @@ +package couchbase + +import ( + "bytes" + "testing" +) + +func TestGetCheckpointID(t *testing.T) { + expected := []byte("_connector:cbgo:group1:checkpoint:1") + actual := getCheckpointID(uint16(1), "group1") + if !bytes.Equal(actual, expected) { + t.Errorf("Unexpected result. Expected: %s, Got: %s", expected, actual) + } +} + +func TestGetCheckpointIDWithInvalidGroupName(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("Expected panic but did not occur") + } + }() + + getCheckpointID(uint16(1), "group.with.dot") +}