From b68624648fd2b26e03bb5ca689db9c966595175e Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Fri, 27 Dec 2024 03:08:13 +0000 Subject: [PATCH] Add MAC validation checker and tests to v2. --- v2/mac.go | 41 +++++++++++++++++++++++++++ v2/mac_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ v2/maker.go | 1 + 3 files changed, 118 insertions(+) create mode 100644 v2/mac.go create mode 100644 v2/mac_test.go diff --git a/v2/mac.go b/v2/mac.go new file mode 100644 index 0000000..33312a3 --- /dev/null +++ b/v2/mac.go @@ -0,0 +1,41 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2 + +import ( + "net" + "reflect" +) + +const ( + // nameMAC is the name of the MAC check. + nameMAC = "mac" +) + +var ( + // ErrNotMAC indicates that the given value is not a valid MAC address. + ErrNotMAC = NewCheckError("MAC") +) + +// IsMAC checks if the value is a valid MAC address. +func IsMAC(value string) (string, error) { + _, err := net.ParseMAC(value) + if err != nil { + return value, ErrNotMAC + } + return value, nil +} + +// checkMAC checks if the value is a valid MAC address. +func checkMAC(value reflect.Value) (reflect.Value, error) { + _, err := IsMAC(value.Interface().(string)) + return value, err +} + +// makeMAC makes a checker function for the MAC checker. +func makeMAC(_ string) CheckFunc[reflect.Value] { + return checkMAC +} diff --git a/v2/mac_test.go b/v2/mac_test.go new file mode 100644 index 0000000..23ec403 --- /dev/null +++ b/v2/mac_test.go @@ -0,0 +1,76 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2_test + +import ( + "fmt" + "testing" + + v2 "github.com/cinar/checker/v2" +) + +func ExampleIsMAC() { + _, err := v2.IsMAC("00:1A:2B:3C:4D:5E") + if err != nil { + fmt.Println(err) + } +} + +func TestIsMACInvalid(t *testing.T) { + _, err := v2.IsMAC("invalid-mac") + if err == nil { + t.Fatal("expected error") + } +} + +func TestIsMACValid(t *testing.T) { + _, err := v2.IsMAC("00:1A:2B:3C:4D:5E") + if err != nil { + t.Fatal(err) + } +} + +func TestCheckMACNonString(t *testing.T) { + defer FailIfNoPanic(t, "expected panic") + + type Device struct { + MAC int `checkers:"mac"` + } + + device := &Device{} + + v2.CheckStruct(device) +} + +func TestCheckMACInvalid(t *testing.T) { + type Device struct { + MAC string `checkers:"mac"` + } + + device := &Device{ + MAC: "invalid-mac", + } + + _, ok := v2.CheckStruct(device) + if ok { + t.Fatal("expected error") + } +} + +func TestCheckMACValid(t *testing.T) { + type Device struct { + MAC string `checkers:"mac"` + } + + device := &Device{ + MAC: "00:1A:2B:3C:4D:5E", + } + + _, ok := v2.CheckStruct(device) + if !ok { + t.Fatal("expected valid") + } +} diff --git a/v2/maker.go b/v2/maker.go index 1a49c6e..b20feb6 100644 --- a/v2/maker.go +++ b/v2/maker.go @@ -27,6 +27,7 @@ var makers = map[string]MakeCheckFunc{ nameIPv6: makeIPv6, nameISBN: makeISBN, nameLUHN: makeLUHN, + nameMAC: makeMAC, nameMaxLen: makeMaxLen, nameMinLen: makeMinLen, nameRequired: makeRequired,