From baa135edd7e10a19a37ff569ef8ffe0257315dc1 Mon Sep 17 00:00:00 2001 From: Olivia Golden Date: Thu, 16 May 2024 15:29:26 -0500 Subject: [PATCH] add testing --- rules/lock/lock.go | 2 +- rules/lock/lock_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/rules/lock/lock.go b/rules/lock/lock.go index 43361f1..36bb547 100644 --- a/rules/lock/lock.go +++ b/rules/lock/lock.go @@ -56,7 +56,7 @@ type v3Locker struct { } func (v3l *v3Locker) Lock(key string, options ...Option) (RuleLock, error) { - validPath := regexp.MustCompile(`^[[:alnum:] \"\'\_\.\,\*\=\-]+$`) + validPath := regexp.MustCompile(`^[[:alnum:] \/\"\'\_\.\,\*\=\-]+$`) if !validPath.MatchString(key) { return nil, fmt.Errorf("Path variable contains an invalid character") } diff --git a/rules/lock/lock_test.go b/rules/lock/lock_test.go index 1e5962e..3a41922 100644 --- a/rules/lock/lock_test.go +++ b/rules/lock/lock_test.go @@ -1,6 +1,7 @@ package lock import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -58,3 +59,39 @@ func Test_V3Locker(t *testing.T) { }) } } + +func Test_V3LockerRegex(t *testing.T) { + cfg, cl := teststore.InitV3Etcd(t) + _, err := v3.New(cfg) + require.NoError(t, err) + newSession := func(_ context.Context) (*v3c.Session, error) { + return v3c.NewSession(cl, v3c.WithTTL(30)) + } + + testcases := []struct { + name string + lockKey string + err error + }{ + { + name: "bad regex", + lockKey: "/test?/", + err: fmt.Errorf("Path variable contains an invalid character"), + }, + { + name: "good regex", + lockKey: "/test/", + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + rlckr := v3Locker{ + newSession: newSession, + lockTimeout: 5, + } + _, err := rlckr.Lock(tc.lockKey) + assert.Equal(t, err, tc.err) + }) + } +}