-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CIDR validation checker and tests to v2. (#132)
# Describe Request Add CIDR validation checker and tests to v2. # Change Type New code.
- Loading branch information
Showing
3 changed files
with
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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 ( | ||
// nameCIDR is the name of the CIDR check. | ||
nameCIDR = "cidr" | ||
) | ||
|
||
var ( | ||
// ErrNotCIDR indicates that the given value is not a valid CIDR. | ||
ErrNotCIDR = NewCheckError("CIDR") | ||
) | ||
|
||
// IsCIDR checks if the value is a valid CIDR notation IP address and prefix length. | ||
func IsCIDR(value string) (string, error) { | ||
_, _, err := net.ParseCIDR(value) | ||
if err != nil { | ||
return value, ErrNotCIDR | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// checkCIDR checks if the value is a valid CIDR notation IP address and prefix length. | ||
func checkCIDR(value reflect.Value) (reflect.Value, error) { | ||
_, err := IsCIDR(value.Interface().(string)) | ||
return value, err | ||
} | ||
|
||
// makeCIDR makes a checker function for the CIDR checker. | ||
func makeCIDR(_ string) CheckFunc[reflect.Value] { | ||
return checkCIDR | ||
} |
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,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 ExampleIsCIDR() { | ||
_, err := v2.IsCIDR("2001:db8::/32") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func TestIsCIDRInvalid(t *testing.T) { | ||
_, err := v2.IsCIDR("900.800.200.100//24") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestIsCIDRValid(t *testing.T) { | ||
_, err := v2.IsCIDR("2001:db8::/32") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCheckCIDRNonString(t *testing.T) { | ||
defer FailIfNoPanic(t, "expected panic") | ||
|
||
type Network struct { | ||
Subnet int `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{} | ||
|
||
v2.CheckStruct(network) | ||
} | ||
|
||
func TestCheckCIDRInvalid(t *testing.T) { | ||
type Network struct { | ||
Subnet string `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{ | ||
Subnet: "900.800.200.100//24", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if ok { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestCheckCIDRValid(t *testing.T) { | ||
type Network struct { | ||
Subnet string `checkers:"cidr"` | ||
} | ||
|
||
network := &Network{ | ||
Subnet: "192.0.2.0/24", | ||
} | ||
|
||
_, ok := v2.CheckStruct(network) | ||
if !ok { | ||
t.Fatal("expected valid") | ||
} | ||
} |
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