Add some unit test (#4853)

Add unit tests for parsing IP addresses.

Signed-off-by: Zhou Hao <zhouhao@cn.fujitsu.com>
This commit is contained in:
Zhou Hao 2020-08-19 07:10:13 +08:00 committed by GitHub
parent f797ab1e65
commit 803511d77b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 111 additions and 0 deletions

View File

@ -198,3 +198,114 @@ func TestNetToPublic(t *testing.T) {
})
}
}
func TestParseProxyIPV4(t *testing.T) {
var testCases = []struct {
ip string
expAddr *pb.IPAddress
expErr bool
}{
{
ip: "10.0",
expAddr: nil,
expErr: true,
},
{
ip: "x.x.x.x",
expAddr: nil,
expErr: true,
},
{
ip: "10.10.10.10",
expAddr: &pb.IPAddress{
Ip: &pb.IPAddress_Ipv4{Ipv4: 168430090},
},
expErr: false,
},
}
for _, testCase := range testCases {
res, err := ParseProxyIPV4(testCase.ip)
if testCase.expErr && err == nil {
t.Fatalf("expected get err, but get nil")
}
if !testCase.expErr {
if err != nil {
t.Fatalf("Unexpected err %v", err)
}
if !proto.Equal(res, testCase.expAddr) {
t.Fatalf("Unexpected TCP Address: [%+v] expected: [%+v]", res, testCase.expAddr)
}
}
}
}
func TestParsePublicIPV4(t *testing.T) {
var testCases = []struct {
ip string
expAddr *public.IPAddress
expErr bool
}{
{
ip: "10.0",
expAddr: nil,
expErr: true,
},
{
ip: "x.x.x.x",
expAddr: nil,
expErr: true,
},
{
ip: "10.10.10.11",
expAddr: &public.IPAddress{
Ip: &public.IPAddress_Ipv4{Ipv4: 168430091},
},
expErr: false,
},
}
for _, testCase := range testCases {
res, err := ParsePublicIPV4(testCase.ip)
if testCase.expErr && err == nil {
t.Fatalf("expected get err, but get nil")
}
if !testCase.expErr {
if err != nil {
t.Fatalf("Unexpected err %v", err)
}
if !proto.Equal(res, testCase.expAddr) {
t.Fatalf("Unexpected TCP Address: [%+v] expected: [%+v]", res, testCase.expAddr)
}
}
}
}
func TestProxyAddressToString(t *testing.T) {
var testCases = []struct {
addr *pb.TcpAddress
expStr string
}{
{
addr: &pb.TcpAddress{
Ip: &pb.IPAddress{Ip: &pb.IPAddress_Ipv4{Ipv4: 1}},
Port: 1234,
},
expStr: "0.0.0.1:1234",
},
{
addr: &pb.TcpAddress{
Ip: &pb.IPAddress{Ip: &pb.IPAddress_Ipv4{Ipv4: 65535}},
Port: 5678,
},
expStr: "0.0.255.255:5678",
},
}
for _, testCase := range testCases {
res := ProxyAddressToString(testCase.addr)
if !(res == testCase.expStr) {
t.Fatalf("Unexpected string: %s expected: %s", res, testCase.expStr)
}
}
}