validate ext. community in search query

This commit is contained in:
Annika Hannig
2024-01-31 16:26:24 +01:00
parent bb457e1ba9
commit f427012e6f
2 changed files with 34 additions and 3 deletions
+12 -3
View File
@@ -1,11 +1,16 @@
package api
import (
"fmt"
"errors"
"strconv"
"strings"
)
// Errors
var (
ErrExtCommunityIncomplete = errors.New("incomplete extended community")
)
// FilterQueryParser parses a filter value into a search filter
type FilterQueryParser func(value string) (*SearchFilter, error)
@@ -64,10 +69,14 @@ func parseExtCommunityValue(value string) (*SearchFilter, error) {
community := make(ExtCommunity, len(components))
if len(community) != 3 {
return nil, fmt.Errorf("malformed ext. community: %s", value)
return nil, ErrExtCommunityIncomplete
}
// Communities are not stringly typed, but a mix of string and int
// Check if the community is incomplete
if components[0] == "" || components[1] == "" || components[2] == "" {
return nil, ErrExtCommunityIncomplete
}
// TODO: Mixing strings and integers is not a good idea
community[0] = components[0]
community[1], _ = strconv.Atoi(components[1])
community[2], _ = strconv.Atoi(components[2])
+22
View File
@@ -59,3 +59,25 @@ func TestParseExtCommunityValue(t *testing.T) {
}
}
func TestPartialParseExtCommunityValue(t *testing.T) {
filter, err := parseExtCommunityValue("rt:23")
if err == nil {
t.Error("Expected error, result:", filter)
}
filter, err = parseExtCommunityValue("rt:23:")
if err == nil {
t.Error("Expected error, result:", filter)
}
filter, err = parseExtCommunityValue("rt::")
if err == nil {
t.Error("Expected error, result:", filter)
}
filter, err = parseExtCommunityValue("::")
if err == nil {
t.Error("Expected error, result:", filter)
}
}