diff options
author | Thomas Rosenstein <thomas.rosenstein@creamfinance.com> | 2020-10-18 21:34:36 +0200 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2020-10-20 09:01:40 +0900 |
commit | 0aff30a74216f499b8abfabc50016b041b319749 (patch) | |
tree | b885cc8f8f9be0b9e11736ed9a74272944d59b90 /internal | |
parent | 4e4113124f8d4a43b638d63d10af8698b4370d93 (diff) |
fix #2289: improve regex to give expected results
Diffstat (limited to 'internal')
-rw-r--r-- | internal/pkg/table/policy.go | 2 | ||||
-rw-r--r-- | internal/pkg/table/policy_test.go | 29 |
2 files changed, 30 insertions, 1 deletions
diff --git a/internal/pkg/table/policy.go b/internal/pkg/table/policy.go index 08636991..d2e4f5ef 100644 --- a/internal/pkg/table/policy.go +++ b/internal/pkg/table/policy.go @@ -1135,7 +1135,7 @@ func ParseExtCommunity(arg string) (bgp.ExtendedCommunityInterface, error) { return bgp.ParseExtendedCommunity(subtype, value) } -var _regexpCommunity2 = regexp.MustCompile(`(\d+.)*\d+:\d+`) +var _regexpCommunity2 = regexp.MustCompile(`^(\d+.)*\d+:\d+$`) func ParseCommunityRegexp(arg string) (*regexp.Regexp, error) { i, err := strconv.ParseUint(arg, 10, 32) diff --git a/internal/pkg/table/policy_test.go b/internal/pkg/table/policy_test.go index 7c2d7a64..9f74f32c 100644 --- a/internal/pkg/table/policy_test.go +++ b/internal/pkg/table/policy_test.go @@ -2771,6 +2771,35 @@ func TestParseCommunityRegexp(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, true, exp.MatchString("65000:1")) assert.Equal(t, false, exp.MatchString("65000:100")) + + // test if the parseCommunityRegexp function behaves as expected + + l1 := "6830:24370$" + r1, _ := ParseCommunityRegexp("6830:24370$") + + l2 := "^6830:24370$" + r2, _ := ParseCommunityRegexp("^6830:24370$") + + l3 := "^65001:100$" + r3, _ := ParseCommunityRegexp("65001:100") + + l4 := "^65001:400$" + r4, _ := ParseCommunityRegexp("4259905936") + + l5 := "^[0-9]*:300$" + r5, _ := ParseCommunityRegexp("^[0-9]*:300$") + + l6 := "^" + strconv.Itoa(int(bgp.COMMUNITY_INTERNET)) + ":" + strconv.Itoa(int(bgp.COMMUNITY_INTERNET)) + "$" + r6, _ := ParseCommunityRegexp("INTERNET") + + fmt.Printf("%v %v", l2, r2) + + assert.Equal(t, l1, r1.String()) + assert.Equal(t, l2, r2.String()) + assert.Equal(t, l3, r3.String()) + assert.Equal(t, l4, r4.String()) + assert.Equal(t, l5, r5.String()) + assert.Equal(t, l6, r6.String()) } func TestLocalPrefAction(t *testing.T) { |