add ut for PortPart()

This commit is contained in:
Weibin Lin 2018-08-16 11:32:17 +08:00
parent 76e9fdaa72
commit 2f30751db3

View File

@ -50,6 +50,58 @@ func TestIPPart(t *testing.T) {
}
}
func TestPortPart(t *testing.T) {
tests := []struct {
name string
endpoint string
want int
wantErr bool
}{
{
"no error parsing from ipv4-ip:port",
"1.2.3.4:1024",
1024,
false,
},
{
"no error parsing from ipv6-ip:port",
"[2001:db8::2:2]:9999",
9999,
false,
},
{
"error: missing port",
"1.2.3.4",
-1,
true,
},
{
"error: invalid port '1-2'",
"1.2.3.4:1-2",
-1,
true,
},
{
"error: invalid port 'port'",
"100.200.3.4:port",
-1,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PortPart(tt.endpoint)
if (err != nil) != tt.wantErr {
t.Errorf("PortPart() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("PortPart() = %v, want %v", got, tt.want)
}
})
}
}
func TestToCIDR(t *testing.T) {
testCases := []struct {
ip string