-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
376 additions
and
100 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,9 @@ | ||
package fnutil | ||
|
||
type Stringer interface { | ||
String() string | ||
} | ||
|
||
func AsString[T Stringer](v T) string { | ||
return v.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,15 @@ | ||
package internal | ||
|
||
import ( | ||
"net/netip" | ||
) | ||
|
||
func ListAddresses(prefixes ...netip.Prefix) []netip.Addr { | ||
var rv []netip.Addr | ||
for _, p := range prefixes { | ||
for addr := p.Addr(); p.Contains(addr); addr = addr.Next() { | ||
rv = append(rv, addr) | ||
} | ||
} | ||
return rv | ||
} |
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,70 @@ | ||
package internal | ||
|
||
import ( | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestListAddresses(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Prefixes []netip.Prefix | ||
Expected []netip.Addr | ||
}{ | ||
{ | ||
Name: "Empty", | ||
}, | ||
{ | ||
Name: "Single IPv4 Address", | ||
Prefixes: []netip.Prefix{netip.MustParsePrefix("192.168.1.1/32")}, | ||
Expected: []netip.Addr{netip.MustParseAddr("192.168.1.1")}, | ||
}, | ||
{ | ||
Name: "IPv4 Subnet", | ||
Prefixes: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/30")}, | ||
Expected: []netip.Addr{ | ||
netip.MustParseAddr("192.168.1.0"), | ||
netip.MustParseAddr("192.168.1.1"), | ||
netip.MustParseAddr("192.168.1.2"), | ||
netip.MustParseAddr("192.168.1.3"), | ||
}, | ||
}, | ||
{ | ||
Name: "Single IPv6 Address", | ||
Prefixes: []netip.Prefix{netip.MustParsePrefix("2001:db8::1/128")}, | ||
Expected: []netip.Addr{netip.MustParseAddr("2001:db8::1")}, | ||
}, | ||
{ | ||
Name: "IPv6 Subnet", | ||
Prefixes: []netip.Prefix{netip.MustParsePrefix("2001:db8::/126")}, | ||
Expected: []netip.Addr{ | ||
netip.MustParseAddr("2001:db8::"), | ||
netip.MustParseAddr("2001:db8::1"), | ||
netip.MustParseAddr("2001:db8::2"), | ||
netip.MustParseAddr("2001:db8::3"), | ||
}, | ||
}, | ||
{ | ||
Name: "Multiple Prefixes", | ||
Prefixes: []netip.Prefix{ | ||
netip.MustParsePrefix("192.168.1.0/31"), | ||
netip.MustParsePrefix("2001:db8::/127"), | ||
}, | ||
Expected: []netip.Addr{ | ||
netip.MustParseAddr("192.168.1.0"), | ||
netip.MustParseAddr("192.168.1.1"), | ||
netip.MustParseAddr("2001:db8::"), | ||
netip.MustParseAddr("2001:db8::1"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
actual := ListAddresses(tt.Prefixes...) | ||
assert.Equal(t, tt.Expected, actual) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.