Skip to content

Commit

Permalink
Merge pull request #1232 from vmware/more-validators
Browse files Browse the repository at this point in the history
Add validators and improve validator testing
  • Loading branch information
annakhm authored Jun 14, 2024
2 parents e43e85b + 066ecd0 commit b1d6b2c
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 2 deletions.
162 changes: 162 additions & 0 deletions nsxt/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/* Copyright © 2024 Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"testing"
)

func TestValidateCidrOrIPOrRange(t *testing.T) {

cases := map[string]struct {
value interface{}
result bool
}{
"NotString": {
value: 777,
result: false,
},
"Empty": {
value: "",
result: false,
},
"IpRangeCidrMix": {
value: "1.1.1.1,3.3.3.0/25, 2.2.2.3-2.2.2.21",
result: false,
},
"Range": {
value: "2.2.2.3 - 2.2.2.21",
result: true,
},
"NotIP": {
value: "1.2.3.no",
result: false,
},
"Text": {
value: "frog",
result: false,
},
"ipv4": {
value: "192.218.3.1",
result: true,
},
"ipv6": {
value: "1231:3213:abcd::15",
result: true,
},
"withSpaces": {
value: "20.2.5.5 ",
result: true,
},
}

validator := validateCidrOrIPOrRange()
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
_, errors := validator(tc.value, tn)

if len(errors) > 0 && tc.result {
t.Errorf("validateCidrOrIPOrRange (%s) produced an unexpected error %s", tc.value, errors)
} else if len(errors) == 0 && !tc.result {
t.Errorf("validateCidrOrIPOrRange (%s) did not error", tc.value)
}
})
}
}

func TestValidateCidrOrIPOrRangeList(t *testing.T) {

cases := map[string]struct {
value interface{}
result bool
}{
"NotString": {
value: 777,
result: false,
},
"Empty": {
value: "",
result: false,
},
"IpRangeCidrMix": {
value: "1.1.1.1,3.3.3.0/25, 2.2.2.3-2.2.2.21",
result: true,
},
"NotIP": {
value: "1.2.3.no",
result: false,
},
"Text": {
value: "frog",
result: false,
},
"ipv6": {
value: "9870::2-9870::5, 1231:3213:abcd::15",
result: true,
},
"ipVersionMix": {
value: "1.1.1.1,3.3.3.0/25, 2.2.2.3-2.2.2.21",
result: true,
},
}

validator := validateCidrOrIPOrRangeList()
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
_, errors := validator(tc.value, tn)

if len(errors) > 0 && tc.result {
t.Errorf("validateCidrOrIPOrRangeList (%s) produced an unexpected error %s", tc.value, errors)
} else if len(errors) == 0 && !tc.result {
t.Errorf("validateCidrOrIPOrRangeList (%s) did not error", tc.value)
}
})
}
}

func TestValidateSingleIP(t *testing.T) {

cases := map[string]struct {
value interface{}
result bool
}{
"NotString": {
value: 777,
result: false,
},
"Empty": {
value: "",
result: false,
},
"ipv4": {
value: "192.218.3.1",
result: true,
},
"ipv6": {
value: "1231:3213:abcd::15",
result: true,
},
"withSpaces": {
value: "20.2.5.5 ",
result: true,
},
"badIP": {
value: "192.278.3.1",
result: true,
},
}

validator := validateSingleIP()
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
_, errors := validator(tc.value, tn)

if len(errors) > 0 && tc.result {
t.Errorf("validateSingleIP (%s) produced an unexpected error %s", tc.value, errors)
} else if len(errors) == 0 && !tc.result {
t.Errorf("validateSingleIP (%s) did not error", tc.value)
}
})
}
}
26 changes: 24 additions & 2 deletions nsxt/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,22 @@ func isIPRange(v string) bool {
if len(s) != 2 {
return false
}
ip1 := net.ParseIP(s[0])
ip2 := net.ParseIP(s[1])
ip1 := net.ParseIP(strings.TrimSpace(s[0]))
ip2 := net.ParseIP(strings.TrimSpace(s[1]))
if ip1 == nil || ip2 == nil {
return false
}
return true
}

func isSingleIP(v string) bool {
v = strings.TrimSpace(v)
ip := net.ParseIP(v)
return ip != nil
}

func isCidr(v string, allowMaxPrefix bool, isIP bool) bool {
v = strings.TrimSpace(v)
_, ipnet, err := net.ParseCIDR(v)
if err != nil {
return false
Expand Down Expand Up @@ -142,6 +144,26 @@ func validateCidrOrIPOrRange() schema.SchemaValidateFunc {
}
}

func validateCidrOrIPOrRangeList() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

tokens := strings.Split(v, ",")
for _, t := range tokens {
if !isCidr(t, true, false) && !isSingleIP(t) && !isIPRange(t) {
es = append(es, fmt.Errorf(
"expected %s to contain a list of valid CIDRs or IPs or Ranges, got: %s", k, t))
return
}
}
return
}
}

func validateIPOrRange() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
Expand Down

0 comments on commit b1d6b2c

Please sign in to comment.