-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAC and CIDR checkers are added. (#53)
* CIDR checker is added. Fixes #42 * Update README.md. * MAC checker is added. Fixes #25 * Remove file.
- Loading branch information
Showing
8 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package checker | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// CheckerCidr is the name of the checker. | ||
const CheckerCidr = "cidr" | ||
|
||
// ResultNotCidr indicates that the given value is not a valid CIDR. | ||
const ResultNotCidr = "NOT_CIDR" | ||
|
||
// IsCidr checker checks if the value is a valid CIDR notation IP address and prefix length. | ||
func IsCidr(value string) Result { | ||
_, _, err := net.ParseCIDR(value) | ||
if err != nil { | ||
return ResultNotCidr | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeCidr makes a checker function for the ip checker. | ||
func makeCidr(_ string) CheckFunc { | ||
return checkCidr | ||
} | ||
|
||
// checkCidr checker checks if the value is a valid CIDR notation IP address and prefix length. | ||
func checkCidr(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsCidr(value.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestIsCidrInvalid(t *testing.T) { | ||
if IsCidr("900.800.200.100//24") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsCidrValid(t *testing.T) { | ||
if IsCidr("2001:db8::/32") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckCidrNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type Network struct { | ||
Subnet int `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{} | ||
|
||
Check(network) | ||
} | ||
|
||
func TestCheckCidrInvalid(t *testing.T) { | ||
type Network struct { | ||
Subnet string `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{ | ||
Subnet: "900.800.200.100//24", | ||
} | ||
|
||
_, valid := Check(network) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckCidrValid(t *testing.T) { | ||
type Network struct { | ||
Subnet string `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{ | ||
Subnet: "192.0.2.0/24", | ||
} | ||
|
||
_, valid := Check(network) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# CIDR Checker | ||
|
||
The ```cidr``` checker checks if the value is a valid CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in [RFC 4632](https://rfc-editor.org/rfc/rfc4632.html) and [RFC 4291](https://rfc-editor.org/rfc/rfc4291.html). If the value is not a valid CIDR notation, the checker will return the ```NOT_CIDR``` result. Here is an example: | ||
|
||
```golang | ||
type Network struct { | ||
Subnet string `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{ | ||
Subnet: "192.0.2.0/24", | ||
} | ||
|
||
_, valid := Check(network) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```cidr``` checker function ```IsCidr``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsCidr("2001:db8::/32") | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# MAC Checker | ||
|
||
The ```mac``` checker checks if the value is a valid an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address. If the value is not a valid MAC address, the checker will return the ```NOT_MAC``` result. Here is an example: | ||
|
||
```golang | ||
type Network struct { | ||
HardwareAddress string `checkers:"mac"` | ||
} | ||
|
||
network := &Network{ | ||
HardwareAddress: "00:00:5e:00:53:01", | ||
} | ||
|
||
_, valid := Check(network) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```mac``` checker function ```IsMAc``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsMac("00:00:5e:00:53:01") | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package checker | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// CheckerMac is the name of the checker. | ||
const CheckerMac = "mac" | ||
|
||
// ResultNotMac indicates that the given value is not an MAC address. | ||
const ResultNotMac = "NOT_MAC" | ||
|
||
// IsMac checks if the given value is a valid an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address. | ||
func IsMac(value string) Result { | ||
_, err := net.ParseMAC(value) | ||
if err != nil { | ||
return ResultNotMac | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeMac makes a checker function for the ip checker. | ||
func makeMac(_ string) CheckFunc { | ||
return checkMac | ||
} | ||
|
||
// checkMac checks if the given value is a valid an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address. | ||
func checkMac(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsMac(value.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestIsMacInvalid(t *testing.T) { | ||
if IsMac("1234") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsMacValid(t *testing.T) { | ||
if IsMac("00:00:5e:00:53:01") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMacNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type Network struct { | ||
HardwareAddress int `checkers:"mac"` | ||
} | ||
|
||
network := &Network{} | ||
|
||
Check(network) | ||
} | ||
|
||
func TestCheckMacInvalid(t *testing.T) { | ||
type Network struct { | ||
HardwareAddress string `checkers:"mac"` | ||
} | ||
|
||
network := &Network{ | ||
HardwareAddress: "1234", | ||
} | ||
|
||
_, valid := Check(network) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMacValid(t *testing.T) { | ||
type Network struct { | ||
HardwareAddress string `checkers:"mac"` | ||
} | ||
|
||
network := &Network{ | ||
HardwareAddress: "00:00:5e:00:53:01", | ||
} | ||
|
||
_, valid := Check(network) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} |