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.
kratos/internal/host/host_test.go

89 lines
1.9 KiB

4 years ago
package host
import (
"net"
"testing"
)
4 years ago
func TestValidIP(t *testing.T) {
4 years ago
tests := []struct {
addr string
expect bool
}{
4 years ago
{"127.0.0.1", false},
{"255.255.255.255", false},
{"0.0.0.0", false},
{"localhost", false},
4 years ago
{"10.1.0.1", true},
{"172.16.0.1", true},
{"192.168.1.1", true},
4 years ago
{"8.8.8.8", true},
{"1.1.1.1", true},
{"9.255.255.255", true},
{"10.0.0.0", true},
{"10.255.255.255", true},
4 years ago
{"11.0.0.0", true},
{"172.15.255.255", true},
{"172.16.0.0", true},
{"172.16.255.255", true},
{"172.23.18.255", true},
{"172.31.255.255", true},
{"172.31.0.0", true},
4 years ago
{"172.32.0.0", true},
{"192.167.255.255", true},
{"192.168.0.0", true},
{"192.168.255.255", true},
4 years ago
{"192.169.0.0", true},
{"fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true},
{"fc00::", true},
{"fcff:1200:0:44::", true},
{"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true},
4 years ago
{"fe00::", true},
4 years ago
}
for _, test := range tests {
t.Run(test.addr, func(t *testing.T) {
res := isValidIP(test.addr)
4 years ago
if res != test.expect {
t.Fatalf("expected %t got %t", test.expect, res)
}
})
}
}
func TestExtract(t *testing.T) {
tests := []struct {
addr string
expect string
}{
{"127.0.0.1:80", "127.0.0.1:80"},
{"10.0.0.1:80", "10.0.0.1:80"},
{"172.16.0.1:80", "172.16.0.1:80"},
{"192.168.1.1:80", "192.168.1.1:80"},
{"0.0.0.0:80", ""},
{"[::]:80", ""},
{":80", ""},
}
for _, test := range tests {
t.Run(test.addr, func(t *testing.T) {
res, err := Extract(test.addr, nil)
if err != nil {
t.Fatal(err)
}
if res != test.expect && (test.expect == "" && test.addr == test.expect) {
t.Fatalf("expected %s got %s", test.expect, res)
}
})
}
}
func TestPort(t *testing.T) {
lis, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
port, ok := Port(lis)
if !ok || port == 0 {
t.Fatalf("expected: %s got %d", lis.Addr().String(), port)
}
}