-
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.
* IPv4 checker is added. Fixes #23 * IPv6 checker is added. Fixes #24 * IP checker is added. Fixes #22 * IPv4 and IPv6 tests.
- Loading branch information
Showing
11 changed files
with
389 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,28 @@ | ||
# IP Checker | ||
|
||
The ```ip``` checker checks if the value is an IP address. If the value is not an IP address, the checker will return the ```NOT_IP``` result. Here is an example: | ||
|
||
```golang | ||
type Request struct { | ||
RemoteIp string `checkers:"ip"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "192.168.1.1", | ||
} | ||
|
||
_, valid := Check(request) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```ip``` checker function ```IsIp``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsIp("2001:db8::68") | ||
|
||
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 @@ | ||
# IPv4 Checker | ||
|
||
The ```ipv4``` checker checks if the value is an IPv4 address. If the value is not an IPv4 address, the checker will return the ```NOT_IP_V4``` result. Here is an example: | ||
|
||
```golang | ||
type Request struct { | ||
RemoteIp string `checkers:"ipv4"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "192.168.1.1", | ||
} | ||
|
||
_, valid := Check(request) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```ipv4``` checker function ```IsIpV4``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsIpV4("192.168.1.1") | ||
|
||
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 @@ | ||
# IPv6 Checker | ||
|
||
The ```ipv6``` checker checks if the value is an IPv6 address. If the value is not an IPv6 address, the checker will return the ```NOT_IP_V6``` result. Here is an example: | ||
|
||
```golang | ||
type Request struct { | ||
RemoteIp string `checkers:"ipv6"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "2001:db8::68", | ||
} | ||
|
||
_, valid := Check(request) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```ipv6``` checker function ```IsIpV6``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsIpV6("2001:db8::68") | ||
|
||
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" | ||
) | ||
|
||
// CheckerIp is the name of the checker. | ||
const CheckerIp = "ip" | ||
|
||
// ResultNotIp indicates that the given value is not an IP address. | ||
const ResultNotIp = "NOT_IP" | ||
|
||
// IsIp checks if the given value is an IP address. | ||
func IsIp(value string) Result { | ||
ip := net.ParseIP(value) | ||
if ip == nil { | ||
return ResultNotIp | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeIp makes a checker function for the ip checker. | ||
func makeIp(_ string) CheckFunc { | ||
return checkIp | ||
} | ||
|
||
// checkIp checks if the given value is an IP address. | ||
func checkIp(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsIp(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 TestIsIpInvalid(t *testing.T) { | ||
if IsIp("900.800.200.100") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsIpValid(t *testing.T) { | ||
if IsIp("2001:db8::68") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckIpNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type Request struct { | ||
RemoteIp int `checkers:"ip"` | ||
} | ||
|
||
request := &Request{} | ||
|
||
Check(request) | ||
} | ||
|
||
func TestCheckIpInvalid(t *testing.T) { | ||
type Request struct { | ||
RemoteIp string `checkers:"ip"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "900.800.200.100", | ||
} | ||
|
||
_, valid := Check(request) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckIpValid(t *testing.T) { | ||
type Request struct { | ||
RemoteIp string `checkers:"ip"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "192.168.1.1", | ||
} | ||
|
||
_, valid := Check(request) | ||
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,40 @@ | ||
package checker | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// CheckerIpV4 is the name of the checker. | ||
const CheckerIpV4 = "ipv4" | ||
|
||
// ResultNotIpV4 indicates that the given value is not an IPv4 address. | ||
const ResultNotIpV4 = "NOT_IP_V4" | ||
|
||
// IsIpV4 checks if the given value is an IPv4 address. | ||
func IsIpV4(value string) Result { | ||
ip := net.ParseIP(value) | ||
if ip == nil { | ||
return ResultNotIpV4 | ||
} | ||
|
||
if ip.To4() == nil { | ||
return ResultNotIpV4 | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeIpV4 makes a checker function for the ipV4 checker. | ||
func makeIpV4(_ string) CheckFunc { | ||
return checkIpV4 | ||
} | ||
|
||
// checkIpV4 checks if the given value is an IPv4 address. | ||
func checkIpV4(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsIpV4(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,63 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestIsIpV4Invalid(t *testing.T) { | ||
if IsIpV4("900.800.200.100") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsIpV4InvalidV6(t *testing.T) { | ||
if IsIpV4("2001:db8::68") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsIpV4Valid(t *testing.T) { | ||
if IsIpV4("192.168.1.1") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckIpV4NonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type Request struct { | ||
RemoteIp int `checkers:"ipv4"` | ||
} | ||
|
||
request := &Request{} | ||
|
||
Check(request) | ||
} | ||
|
||
func TestCheckIpV4Invalid(t *testing.T) { | ||
type Request struct { | ||
RemoteIp string `checkers:"ipv4"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "900.800.200.100", | ||
} | ||
|
||
_, valid := Check(request) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckIpV4Valid(t *testing.T) { | ||
type Request struct { | ||
RemoteIp string `checkers:"ipv4"` | ||
} | ||
|
||
request := &Request{ | ||
RemoteIp: "192.168.1.1", | ||
} | ||
|
||
_, valid := Check(request) | ||
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,40 @@ | ||
package checker | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// CheckerIpV6 is the name of the checker. | ||
const CheckerIpV6 = "ipv6" | ||
|
||
// ResultNotIpV6 indicates that the given value is not an IPv6 address. | ||
const ResultNotIpV6 = "NOT_IP_V6" | ||
|
||
// IsIpV6 checks if the given value is an IPv6 address. | ||
func IsIpV6(value string) Result { | ||
ip := net.ParseIP(value) | ||
if ip == nil { | ||
return ResultNotIpV6 | ||
} | ||
|
||
if ip.To4() != nil { | ||
return ResultNotIpV6 | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeIpV6 makes a checker function for the ipV6 checker. | ||
func makeIpV6(_ string) CheckFunc { | ||
return checkIpV6 | ||
} | ||
|
||
// checkIpV6 checks if the given value is an IPv6 address. | ||
func checkIpV6(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsIpV6(value.String()) | ||
} |
Oops, something went wrong.