2015-12-02 11:18:12 -05:00
|
|
|
package net_test
|
2015-11-04 16:48:54 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-01-07 17:27:59 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v5/common/net"
|
2015-11-04 16:48:54 -05:00
|
|
|
)
|
|
|
|
|
2019-01-07 17:27:59 -05:00
|
|
|
func TestDestinationProperty(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Input Destination
|
|
|
|
Network Network
|
|
|
|
String string
|
|
|
|
NetString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: TCPDestination(IPAddress([]byte{1, 2, 3, 4}), 80),
|
|
|
|
Network: Network_TCP,
|
|
|
|
String: "tcp:1.2.3.4:80",
|
|
|
|
NetString: "1.2.3.4:80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: UDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53),
|
|
|
|
Network: Network_UDP,
|
|
|
|
String: "udp:[2001:4860:4860::8888]:53",
|
|
|
|
NetString: "[2001:4860:4860::8888]:53",
|
|
|
|
},
|
2020-10-29 03:30:38 -04:00
|
|
|
{
|
|
|
|
Input: UnixDestination(DomainAddress("/tmp/test.sock")),
|
|
|
|
Network: Network_UNIX,
|
|
|
|
String: "unix:/tmp/test.sock",
|
|
|
|
NetString: "/tmp/test.sock",
|
|
|
|
},
|
2019-01-07 17:27:59 -05:00
|
|
|
}
|
2015-11-04 16:48:54 -05:00
|
|
|
|
2019-01-07 17:27:59 -05:00
|
|
|
for _, testCase := range testCases {
|
|
|
|
dest := testCase.Input
|
|
|
|
if r := cmp.Diff(dest.Network, testCase.Network); r != "" {
|
|
|
|
t.Error("unexpected Network in ", dest.String(), ": ", r)
|
|
|
|
}
|
|
|
|
if r := cmp.Diff(dest.String(), testCase.String); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
if r := cmp.Diff(dest.NetAddr(), testCase.NetString); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 16:48:54 -05:00
|
|
|
}
|
2018-04-21 05:00:40 -04:00
|
|
|
|
|
|
|
func TestDestinationParse(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output Destination
|
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: "tcp:127.0.0.1:80",
|
|
|
|
Output: TCPDestination(LocalHostIP, Port(80)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "udp:8.8.8.8:53",
|
|
|
|
Output: UDPDestination(IPAddress([]byte{8, 8, 8, 8}), Port(53)),
|
|
|
|
},
|
2020-10-29 03:30:38 -04:00
|
|
|
{
|
|
|
|
Input: "unix:/tmp/test.sock",
|
|
|
|
Output: UnixDestination(DomainAddress("/tmp/test.sock")),
|
|
|
|
},
|
2018-04-21 05:00:40 -04:00
|
|
|
{
|
|
|
|
Input: "8.8.8.8:53",
|
|
|
|
Output: Destination{
|
|
|
|
Address: IPAddress([]byte{8, 8, 8, 8}),
|
|
|
|
Port: Port(53),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: ":53",
|
|
|
|
Output: Destination{
|
|
|
|
Address: AnyIP,
|
|
|
|
Port: Port(53),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "8.8.8.8",
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "8.8.8.8:http",
|
|
|
|
Error: true,
|
|
|
|
},
|
2020-10-29 03:30:38 -04:00
|
|
|
{
|
|
|
|
Input: "/tmp/test.sock",
|
|
|
|
Error: true,
|
|
|
|
},
|
2018-04-21 05:00:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range cases {
|
|
|
|
d, err := ParseDestination(testcase.Input)
|
|
|
|
if !testcase.Error {
|
2019-01-07 17:27:59 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error("for test case: ", testcase.Input, " expected no error, but got ", err)
|
|
|
|
}
|
|
|
|
if d != testcase.Output {
|
|
|
|
t.Error("for test case: ", testcase.Input, " expected output: ", testcase.Output.String(), " but got ", d.String())
|
|
|
|
}
|
2020-10-11 07:22:46 -04:00
|
|
|
} else if err == nil {
|
|
|
|
t.Error("for test case: ", testcase.Input, " expected error, but got nil")
|
2018-04-21 05:00:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|