Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package net_test
 
import (
    "net"
    "testing"
 
    "github.com/google/go-cmp/cmp"
 
    . "github.com/v2fly/v2ray-core/v5/common/net"
)
 
func TestAddressProperty(t *testing.T) {
    type addrProprty struct {
        IP     []byte
        Domain string
        Family AddressFamily
        String string
    }
 
    testCases := []struct {
        Input  Address
        Output addrProprty
    }{
        {
            Input: IPAddress([]byte{byte(1), byte(2), byte(3), byte(4)}),
            Output: addrProprty{
                IP:     []byte{byte(1), byte(2), byte(3), byte(4)},
                Family: AddressFamilyIPv4,
                String: "1.2.3.4",
            },
        },
        {
            Input: IPAddress([]byte{
                byte(1), byte(2), byte(3), byte(4),
                byte(1), byte(2), byte(3), byte(4),
                byte(1), byte(2), byte(3), byte(4),
                byte(1), byte(2), byte(3), byte(4),
            }),
            Output: addrProprty{
                IP: []byte{
                    byte(1), byte(2), byte(3), byte(4),
                    byte(1), byte(2), byte(3), byte(4),
                    byte(1), byte(2), byte(3), byte(4),
                    byte(1), byte(2), byte(3), byte(4),
                },
                Family: AddressFamilyIPv6,
                String: "[102:304:102:304:102:304:102:304]",
            },
        },
        {
            Input: IPAddress([]byte{
                byte(0), byte(0), byte(0), byte(0),
                byte(0), byte(0), byte(0), byte(0),
                byte(0), byte(0), byte(255), byte(255),
                byte(1), byte(2), byte(3), byte(4),
            }),
            Output: addrProprty{
                IP:     []byte{byte(1), byte(2), byte(3), byte(4)},
                Family: AddressFamilyIPv4,
                String: "1.2.3.4",
            },
        },
        {
            Input: DomainAddress("v2fly.org"),
            Output: addrProprty{
                Domain: "v2fly.org",
                Family: AddressFamilyDomain,
                String: "v2fly.org",
            },
        },
        {
            Input: IPAddress(net.IPv4(1, 2, 3, 4)),
            Output: addrProprty{
                IP:     []byte{byte(1), byte(2), byte(3), byte(4)},
                Family: AddressFamilyIPv4,
                String: "1.2.3.4",
            },
        },
        {
            Input: ParseAddress("[2001:4860:0:2001::68]"),
            Output: addrProprty{
                IP:     []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
                Family: AddressFamilyIPv6,
                String: "[2001:4860:0:2001::68]",
            },
        },
        {
            Input: ParseAddress("::0"),
            Output: addrProprty{
                IP:     AnyIPv6.IP(),
                Family: AddressFamilyIPv6,
                String: "[::]",
            },
        },
        {
            Input: ParseAddress("[::ffff:123.151.71.143]"),
            Output: addrProprty{
                IP:     []byte{123, 151, 71, 143},
                Family: AddressFamilyIPv4,
                String: "123.151.71.143",
            },
        },
        {
            Input: NewIPOrDomain(ParseAddress("v2fly.org")).AsAddress(),
            Output: addrProprty{
                Domain: "v2fly.org",
                Family: AddressFamilyDomain,
                String: "v2fly.org",
            },
        },
        {
            Input: NewIPOrDomain(ParseAddress("8.8.8.8")).AsAddress(),
            Output: addrProprty{
                IP:     []byte{8, 8, 8, 8},
                Family: AddressFamilyIPv4,
                String: "8.8.8.8",
            },
        },
        {
            Input: NewIPOrDomain(ParseAddress("[2001:4860:0:2001::68]")).AsAddress(),
            Output: addrProprty{
                IP:     []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
                Family: AddressFamilyIPv6,
                String: "[2001:4860:0:2001::68]",
            },
        },
    }
 
    for _, testCase := range testCases {
        actual := addrProprty{
            Family: testCase.Input.Family(),
            String: testCase.Input.String(),
        }
        if testCase.Input.Family().IsIP() {
            actual.IP = testCase.Input.IP()
        } else {
            actual.Domain = testCase.Input.Domain()
        }
 
        if r := cmp.Diff(actual, testCase.Output); r != "" {
            t.Error("for input: ", testCase.Input, ":", r)
        }
    }
}
 
func TestInvalidAddressConvertion(t *testing.T) {
    panics := func(f func()) (ret bool) {
        defer func() {
            if r := recover(); r != nil {
                ret = true
            }
        }()
        f()
        return false
    }
 
    testCases := []func(){
        func() { ParseAddress("8.8.8.8").Domain() },
        func() { ParseAddress("2001:4860:0:2001::68").Domain() },
        func() { ParseAddress("v2fly.org").IP() },
    }
    for idx, testCase := range testCases {
        if !panics(testCase) {
            t.Error("case ", idx, " failed")
        }
    }
}
 
func BenchmarkParseAddressIPv4(b *testing.B) {
    for i := 0; i < b.N; i++ {
        addr := ParseAddress("8.8.8.8")
        if addr.Family() != AddressFamilyIPv4 {
            panic("not ipv4")
        }
    }
}
 
func BenchmarkParseAddressIPv6(b *testing.B) {
    for i := 0; i < b.N; i++ {
        addr := ParseAddress("2001:4860:0:2001::68")
        if addr.Family() != AddressFamilyIPv6 {
            panic("not ipv6")
        }
    }
}
 
func BenchmarkParseAddressDomain(b *testing.B) {
    for i := 0; i < b.N; i++ {
        addr := ParseAddress("v2fly.org")
        if addr.Family() != AddressFamilyDomain {
            panic("not domain")
        }
    }
}