Skip to content

Commit

Permalink
Add unit test for TestFilterSingleIPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkhauck committed Sep 6, 2023
1 parent 58dd47e commit 3cca8cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
22 changes: 18 additions & 4 deletions parser/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,21 @@ func TestFilterSingleIP(t *testing.T) {
}
}

// In alwaysIncluded, just leave it as an empty slice, replace
// util.ParseSubnets with []*net.IPNet
// In neverIncluded, include the IPv6 address without CIDR.
// Test case will be modeled after the "never" test case
func TestFilterSingleIPv6(t *testing.T) {
fsTest := filter{
// purposely omitting internal subnet definition
alwaysIncluded: []*net.IPNet{},
neverIncluded: util.ParseSubnets([]string{"::1"}),
}

never:= "::1"

testCases := []testCaseSingleIP{
{never, true, "NeverInclude IP should be filtered"},
}

for _, test := range testCases {
output := fsTest.filterSingleIP(net.ParseIP(test.ip))
assert.Equal(t, test.out, output, test.msg)
}
}
6 changes: 3 additions & 3 deletions util/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ParseSubnets(subnets []string) []*net.IPNet {
// Try to parse out CIDR range
_, block, err := net.ParseCIDR(entry)

// If there was an error, check if entry was an IP, not a range
// If there was an error, check if entry was an IP
if err != nil {
ipAddr := net.ParseIP(entry)
if ipAddr == nil {
Expand All @@ -45,9 +45,9 @@ func ParseSubnets(subnets []string) []*net.IPNet {
// Check if it's an IPv4 or IPv6 address and append the appropriate subnet mask
var subnetMask string
if ipAddr.To4() != nil {
subnetMask = "/32" // IPv4
subnetMask = "/32"
} else {
subnetMask = "/128" // IPv6
subnetMask = "/128"
}

// Append the subnet mask and parse as a CIDR range
Expand Down

0 comments on commit 3cca8cc

Please sign in to comment.