Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net_test
 
import (
    "testing"
 
    "github.com/google/go-cmp/cmp"
 
    . "github.com/v2fly/v2ray-core/v5/common/net"
)
 
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",
        },
        {
            Input:     UnixDestination(DomainAddress("/tmp/test.sock")),
            Network:   Network_UNIX,
            String:    "unix:/tmp/test.sock",
            NetString: "/tmp/test.sock",
        },
    }
 
    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)
        }
    }
}
 
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)),
        },
        {
            Input:  "unix:/tmp/test.sock",
            Output: UnixDestination(DomainAddress("/tmp/test.sock")),
        },
        {
            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,
        },
        {
            Input: "/tmp/test.sock",
            Error: true,
        },
    }
 
    for _, testcase := range cases {
        d, err := ParseDestination(testcase.Input)
        if !testcase.Error {
            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())
            }
        } else if err == nil {
            t.Error("for test case: ", testcase.Input, " expected error, but got nil")
        }
    }
}