You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
1.9 KiB
92 lines
1.9 KiB
package rings |
|
|
|
import ( |
|
"fmt" |
|
"net/netip" |
|
"testing" |
|
) |
|
|
|
func TestAddrFrom4(t *testing.T) { |
|
cases := []struct { |
|
v [4]uint |
|
s string |
|
}{ |
|
{[4]uint{0, 0, 0, 0}, "0.0.0.0"}, |
|
{[4]uint{127, 0, 0, 1}, "127.0.0.1"}, |
|
{[4]uint{4096 + 127, 0, 0, 1}, "127.0.0.1"}, |
|
{[4]uint{257, 258, 259, 260}, "1.2.3.4"}, |
|
{[4]uint{255, 255, 255, 255}, "255.255.255.255"}, |
|
} |
|
|
|
for i, tc := range cases { |
|
fn := fmt.Sprintf("%v.%v.%v.%v", tc.v[0], tc.v[1], tc.v[2], tc.v[3]) |
|
addr := AddrFrom4(tc.v[0], tc.v[1], tc.v[2], tc.v[3]) |
|
s := addr.String() |
|
|
|
if s == tc.s { |
|
t.Logf("[%v/%v]: %s → %s", i, len(cases), fn, s) |
|
} else { |
|
t.Errorf("ERROR: [%v/%v]: %s → %s (expected %s)", i, len(cases), fn, s, tc.s) |
|
} |
|
} |
|
} |
|
|
|
func TestAddrU32Invalid(t *testing.T) { |
|
cases := []netip.Addr{ |
|
{}, |
|
netip.IPv6Unspecified(), |
|
netip.IPv6Loopback(), |
|
} |
|
|
|
for i, tc := range cases { |
|
v, ok := AddrToU32(tc) |
|
switch { |
|
case !ok && v == 0: |
|
t.Logf("[%v/%v]: %s → %v %v", i, len(cases), tc, 0, false) |
|
default: |
|
t.Errorf("ERROR: [%v/%v]: %s → %v %v (expected %v %v)", i, len(cases), |
|
tc, v, ok, 0, false) |
|
} |
|
} |
|
} |
|
|
|
func TestAddrU32Valid(t *testing.T) { |
|
cases := []netip.Addr{ |
|
netip.IPv4Unspecified(), |
|
AddrFrom4(0, 0, 0, 0), |
|
AddrFrom4(1, 2, 3, 4), |
|
AddrFrom4(10, 20, 30, 40), |
|
AddrFrom4(127, 0, 0, 1), |
|
AddrFrom4(255, 255, 255, 255), |
|
MustParseAddr("::ffff:1.2.3.4"), |
|
} |
|
|
|
for i, tc := range cases { |
|
u32, ok := AddrToU32(tc) |
|
if !ok { |
|
t.Errorf("ERROR: [%v/%v]: %s → %v %v", i, len(cases), tc, u32, ok) |
|
continue |
|
} |
|
|
|
addr := AddrFromU32(u32) |
|
if tc.Is4In6() { |
|
ok = addr.Compare(tc.Unmap()) == 0 |
|
} else { |
|
ok = addr.Compare(tc) == 0 |
|
} |
|
|
|
if ok { |
|
t.Logf("[%v/%v]: %s → %v → %s", i, len(cases), tc, u32, addr) |
|
} else { |
|
t.Errorf("ERROR: [%v/%v]: %s → %v → %s", i, len(cases), tc, u32, addr) |
|
} |
|
} |
|
} |
|
|
|
func MustParseAddr(s string) netip.Addr { |
|
addr, err := netip.ParseAddr(s) |
|
if err != nil { |
|
panic(err) |
|
} |
|
return addr |
|
}
|
|
|