github.com/xraypb/xray-core@v1.6.6/common/net/destination_test.go (about) 1 package net_test 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 . "github.com/xraypb/xray-core/common/net" 8 ) 9 10 func TestDestinationProperty(t *testing.T) { 11 testCases := []struct { 12 Input Destination 13 Network Network 14 String string 15 NetString string 16 }{ 17 { 18 Input: TCPDestination(IPAddress([]byte{1, 2, 3, 4}), 80), 19 Network: Network_TCP, 20 String: "tcp:1.2.3.4:80", 21 NetString: "1.2.3.4:80", 22 }, 23 { 24 Input: UDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53), 25 Network: Network_UDP, 26 String: "udp:[2001:4860:4860::8888]:53", 27 NetString: "[2001:4860:4860::8888]:53", 28 }, 29 { 30 Input: UnixDestination(DomainAddress("/tmp/test.sock")), 31 Network: Network_UNIX, 32 String: "unix:/tmp/test.sock", 33 NetString: "/tmp/test.sock", 34 }, 35 } 36 37 for _, testCase := range testCases { 38 dest := testCase.Input 39 if r := cmp.Diff(dest.Network, testCase.Network); r != "" { 40 t.Error("unexpected Network in ", dest.String(), ": ", r) 41 } 42 if r := cmp.Diff(dest.String(), testCase.String); r != "" { 43 t.Error(r) 44 } 45 if r := cmp.Diff(dest.NetAddr(), testCase.NetString); r != "" { 46 t.Error(r) 47 } 48 } 49 } 50 51 func TestDestinationParse(t *testing.T) { 52 cases := []struct { 53 Input string 54 Output Destination 55 Error bool 56 }{ 57 { 58 Input: "tcp:127.0.0.1:80", 59 Output: TCPDestination(LocalHostIP, Port(80)), 60 }, 61 { 62 Input: "udp:8.8.8.8:53", 63 Output: UDPDestination(IPAddress([]byte{8, 8, 8, 8}), Port(53)), 64 }, 65 { 66 Input: "unix:/tmp/test.sock", 67 Output: UnixDestination(DomainAddress("/tmp/test.sock")), 68 }, 69 { 70 Input: "8.8.8.8:53", 71 Output: Destination{ 72 Address: IPAddress([]byte{8, 8, 8, 8}), 73 Port: Port(53), 74 }, 75 }, 76 { 77 Input: ":53", 78 Output: Destination{ 79 Address: AnyIP, 80 Port: Port(53), 81 }, 82 }, 83 { 84 Input: "8.8.8.8", 85 Error: true, 86 }, 87 { 88 Input: "8.8.8.8:http", 89 Error: true, 90 }, 91 { 92 Input: "/tmp/test.sock", 93 Error: true, 94 }, 95 } 96 97 for _, testcase := range cases { 98 d, err := ParseDestination(testcase.Input) 99 if !testcase.Error { 100 if err != nil { 101 t.Error("for test case: ", testcase.Input, " expected no error, but got ", err) 102 } 103 if d != testcase.Output { 104 t.Error("for test case: ", testcase.Input, " expected output: ", testcase.Output.String(), " but got ", d.String()) 105 } 106 } else if err == nil { 107 t.Error("for test case: ", testcase.Input, " expected error, but got nil") 108 } 109 } 110 }