Add some unit test
Add unit tests for parsing IP addresses.
@@ -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)
+ }
+ }
+}
