From c6482becb1d894bdb8421126a172aa4d4abc7dae Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Fri, 18 Oct 2024 16:35:19 -0400 Subject: [PATCH] patch: ParseLocator, sanitize locator components - currently, parsing the following string "event: /xmidt-test/2/config" yields an authority with the value ` ` - sanitize each locator component, removing trailing and leading whitespaces #210 --- id.go | 8 ++++---- id_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/id.go b/id.go index 8f0b3e4..69d9c52 100644 --- a/id.go +++ b/id.go @@ -141,13 +141,13 @@ func ParseLocator(locator string) (Locator, error) { var l Locator - l.Scheme = strings.ToLower(match[1]) - l.Authority = match[2] + l.Scheme = strings.TrimSpace(strings.ToLower(match[1])) + l.Authority = strings.TrimSpace(match[2]) if len(match) > 3 { - l.Service = strings.TrimPrefix(match[3], "/") + l.Service = strings.TrimSpace(strings.TrimPrefix(match[3], "/")) } if len(match) > 4 { - l.Ignored = match[4] + l.Ignored = strings.TrimSpace(match[4]) } // If the locator is a device identifier, then we need to parse it. diff --git a/id_test.go b/id_test.go index d52e43a..8cb38f8 100644 --- a/id_test.go +++ b/id_test.go @@ -190,6 +190,14 @@ func TestParseLocator(t *testing.T) { Scheme: SchemeDNS, Authority: "foo.bar.com", }, + }, { + description: "event scheme (with spaces)", + locator: "event: targetedEvent ", + str: "event:targetedEvent", + want: Locator{ + Scheme: SchemeEvent, + Authority: "targetedEvent", + }, }, { description: "event scheme", locator: "event:targetedEvent", @@ -251,6 +259,14 @@ func TestParseLocator(t *testing.T) { description: "invalid self scheme", locator: "self:anything", expectedErr: ErrorInvalidDeviceName, + }, { + description: "invalid event scheme (no authority)", + locator: "event:/anything", + expectedErr: ErrorInvalidLocator, + }, { + description: "invalid event scheme (no authority and with spaces)", + locator: "event: /anything", + expectedErr: ErrorInvalidLocator, }, } for _, tc := range tests {