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