diff options
author | Ritu Sood <Ritu.Sood@intel.com> | 2020-04-10 20:33:16 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-04-10 20:33:16 +0000 |
commit | 107142787e331ca565c166c72ae5605621d2d2ee (patch) | |
tree | e8b368eb77647acf40a8a87e157f4c1b59763be3 /src | |
parent | 1e240a189cfc4a02d9072241fdf30adbc8c1539b (diff) | |
parent | eafe59ee894f6bb42d090a2aad42323e67243b4f (diff) |
Merge "Add validation routines"
Diffstat (limited to 'src')
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation.go | 24 | ||||
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation_test.go | 37 |
2 files changed, 57 insertions, 4 deletions
diff --git a/src/orchestrator/pkg/infra/validation/validation.go b/src/orchestrator/pkg/infra/validation/validation.go index d744dc3d..03b37fa1 100644 --- a/src/orchestrator/pkg/infra/validation/validation.go +++ b/src/orchestrator/pkg/infra/validation/validation.go @@ -70,21 +70,37 @@ func IsTarGz(r io.Reader) error { } func IsIpv4Cidr(cidr string) error { - _, _, err := net.ParseCIDR(cidr) - if err != nil { - return pkgerrors.Wrapf(err, "could not parse subnet %v", cidr) + ip, _, err := net.ParseCIDR(cidr) + if err != nil || ip.To4() == nil { + return pkgerrors.Wrapf(err, "could not parse ipv4 cidr %v", cidr) } return nil } -func IsIpv4(ip string) error { +func IsIp(ip string) error { addr := net.ParseIP(ip) if addr == nil { + return pkgerrors.Errorf("invalid ip address %v", ip) + } + return nil +} + +func IsIpv4(ip string) error { + addr := net.ParseIP(ip) + if addr == nil || addr.To4() == nil { return pkgerrors.Errorf("invalid ipv4 address %v", ip) } return nil } +func IsMac(mac string) error { + _, err := net.ParseMAC(mac) + if err != nil { + return pkgerrors.Errorf("invalid MAC address %v", mac) + } + return nil +} + // default name check - matches valid label value with addtion that length > 0 func IsValidName(name string) []string { var errs []string diff --git a/src/orchestrator/pkg/infra/validation/validation_test.go b/src/orchestrator/pkg/infra/validation/validation_test.go index 5109b6c7..6a7f504b 100644 --- a/src/orchestrator/pkg/infra/validation/validation_test.go +++ b/src/orchestrator/pkg/infra/validation/validation_test.go @@ -185,6 +185,43 @@ func TestIsIpv4(t *testing.T) { }) } +func TestIsMac(t *testing.T) { + t.Run("Valid MAC", func(t *testing.T) { + validmacs := []string{ + "11:22:33:44:55:66", + "ab-cd-ef-12-34-56", + "AB-CD-EF-12-34-56", + } + for _, mac := range validmacs { + err := IsMac(mac) + if err != nil { + t.Errorf("Valid MAC string failed to pass: %v", mac) + } + } + }) + + t.Run("Invalid MAC", func(t *testing.T) { + invalidmacs := []string{ + "", + "1.2.3.4.5", + "1.2.3.45/32", + "ab:cd:ef:gh:12:34", + "11:22-33-44:55:66", + "11,22,33,44,55,66", + "11|22|33|44|55|66", + "11:22:33:44:55:66:77", + "11-22-33-44-55", + "11-22-33-44-55-66-77", + } + for _, mac := range invalidmacs { + err := IsMac(mac) + if err == nil { + t.Errorf("Invalid MAC passed: %v", mac) + } + } + }) +} + func TestIsValidString(t *testing.T) { t.Run("Valid Strings", func(t *testing.T) { validStrings := []struct { |