@@ -11,44 +11,72 @@ import (
1111)
1212
1313// Test zero value address.
14+
1415var (
1516 addrZero = IPv4UnspecifiedAddr
1617 addrZeroConnAddr conn.Addr
1718)
1819
1920// Test IPv4 address.
21+
22+ const addr4port uint16 = 1080
23+
2024var (
21- addr4 = [IPv4AddrLen ]byte {AtypIPv4 , 127 , 0 , 0 , 1 , 4 , 56 }
22- addr4addr = netip .AddrFrom4 ([4 ]byte {127 , 0 , 0 , 1 })
23- addr4port uint16 = 1080
24- addr4addrport = netip .AddrPortFrom (addr4addr , addr4port )
25- addr4connaddr = conn .AddrFromIPPort (addr4addrport )
25+ addr4 = [IPv4AddrLen ]byte {
26+ AtypIPv4 ,
27+ 127 , 0 , 0 , 1 ,
28+ byte (addr4port >> 8 ), byte (addr4port & 0xff ),
29+ }
30+ addr4addr = netip .AddrFrom4 ([4 ]byte {127 , 0 , 0 , 1 })
31+ addr4addrport = netip .AddrPortFrom (addr4addr , addr4port )
32+ addr4connaddr = conn .AddrFromIPPort (addr4addrport )
2633)
2734
2835// Test IPv4-mapped IPv6 address.
36+
37+ const addr4in6port uint16 = 1080
38+
2939var (
30- addr4in6 = [IPv4AddrLen ]byte {AtypIPv4 , 127 , 0 , 0 , 1 , 4 , 56 }
31- addr4in6addr = netip .AddrFrom16 ([16 ]byte {10 : 0xff , 11 : 0xff , 127 , 0 , 0 , 1 })
32- addr4in6port uint16 = 1080
33- addr4in6addrport = netip .AddrPortFrom (addr4in6addr , addr4in6port )
34- addr4in6connaddr = conn .AddrFromIPPort (addr4in6addrport )
40+ addr4in6 = [IPv4AddrLen ]byte {
41+ AtypIPv4 ,
42+ 127 , 0 , 0 , 1 ,
43+ byte (addr4in6port >> 8 ), byte (addr4in6port & 0xff ),
44+ }
45+ addr4in6addr = netip .AddrFrom16 ([16 ]byte {10 : 0xff , 11 : 0xff , 127 , 0 , 0 , 1 })
46+ addr4in6addrport = netip .AddrPortFrom (addr4in6addr , addr4in6port )
47+ addr4in6connaddr = conn .AddrFromIPPort (addr4in6addrport )
3548)
3649
3750// Test IPv6 address.
51+
52+ const addr6port uint16 = 1080
53+
3854var (
39- addr6 = [IPv6AddrLen ]byte {AtypIPv6 , 0x20 , 0x01 , 0x0d , 0xb8 , 0xfa , 0xd6 , 0x05 , 0x72 , 0xac , 0xbe , 0x71 , 0x43 , 0x14 , 0xe5 , 0x7a , 0x6e , 4 , 56 }
40- addr6addr = netip .AddrFrom16 ([16 ]byte {0x20 , 0x01 , 0x0d , 0xb8 , 0xfa , 0xd6 , 0x05 , 0x72 , 0xac , 0xbe , 0x71 , 0x43 , 0x14 , 0xe5 , 0x7a , 0x6e })
41- addr6port uint16 = 1080
42- addr6addrport = netip .AddrPortFrom (addr6addr , addr6port )
43- addr6connaddr = conn .AddrFromIPPort (addr6addrport )
55+ addr6 = [IPv6AddrLen ]byte {
56+ AtypIPv6 ,
57+ 0x20 , 0x01 , 0x0d , 0xb8 , 0xfa , 0xd6 , 0x05 , 0x72 , 0xac , 0xbe , 0x71 , 0x43 , 0x14 , 0xe5 , 0x7a , 0x6e ,
58+ byte (addr6port >> 8 ), byte (addr6port & 0xff ),
59+ }
60+ addr6addr = netip .AddrFrom16 ([16 ]byte {0x20 , 0x01 , 0x0d , 0xb8 , 0xfa , 0xd6 , 0x05 , 0x72 , 0xac , 0xbe , 0x71 , 0x43 , 0x14 , 0xe5 , 0x7a , 0x6e })
61+ addr6addrport = netip .AddrPortFrom (addr6addr , addr6port )
62+ addr6connaddr = conn .AddrFromIPPort (addr6addrport )
4463)
4564
4665// Test domain name.
66+
67+ const (
68+ addrDomainHost = "example.com"
69+ addrDomainPort uint16 = 443
70+ )
71+
4772var (
48- addrDomain = [1 + 1 + 11 + 2 ]byte {AtypDomainName , 11 , 'e' , 'x' , 'a' , 'm' , 'p' , 'l' , 'e' , '.' , 'c' , 'o' , 'm' , 1 , 187 }
49- addrDomainHost = "example.com"
50- addrDomainPort uint16 = 443
51- addrDomainConnAddr = conn .MustAddrFromDomainPort (addrDomainHost , addrDomainPort )
73+ addrDomain = [1 + 1 + len (addrDomainHost ) + 2 ]byte {
74+ AtypDomainName ,
75+ byte (len (addrDomainHost )),
76+ 'e' , 'x' , 'a' , 'm' , 'p' , 'l' , 'e' , '.' , 'c' , 'o' , 'm' ,
77+ byte (addrDomainPort >> 8 ), byte (addrDomainPort & 0xff ),
78+ }
79+ addrDomainConnAddr = conn .MustAddrFromDomainPort (addrDomainHost , addrDomainPort )
5280)
5381
5482func testAddrFromReader (t * testing.T , addr []byte ) {
@@ -156,51 +184,54 @@ func TestConnAddrFromSliceAndReader(t *testing.T) {
156184 testConnAddrFromSliceAndReader (t , addrDomain [:], addrDomainConnAddr )
157185}
158186
159- func testConnAddrFromSliceWithDomainCache (t * testing.T , sa []byte , cachedDomain string , expectedAddr conn.Addr ) string {
160- b := make ([]byte , 512 )
187+ func testConnAddrFromSliceWithDomainCache (t * testing.T , b , sa []byte , dc * DomainCache , expectedAddr conn.Addr ) {
161188 n := copy (b , sa )
162- rand .Read (b [n :])
163- expectedTail := make ([]byte , 512 - n )
164- copy (expectedTail , b [n :])
189+ tail := b [n :]
190+ rand .Read (tail )
191+ expectedTail := make ([]byte , 0 , 512 )
192+ expectedTail = append (expectedTail , tail ... )
165193
166- addr , n , cachedDomain , err := ConnAddrFromSliceWithDomainCache ( b , cachedDomain )
194+ addr , n , err := dc . ConnAddrFromSlice ( b )
167195 if err != nil {
168196 t .Fatal (err )
169197 }
170198 if n != len (sa ) {
171- t .Errorf ("ConnAddrFromSlice(b ) returned n=%d, expected n=%d." , n , len (sa ))
199+ t .Errorf ("dc. ConnAddrFromSlice(%x ) returned n=%d, expected n=%d" , b , n , len (sa ))
172200 }
173201 if ! addr .Equals (expectedAddr ) {
174- t .Errorf ("ConnAddrFromSlice(b ) returned %s, expected %s." , addr , expectedAddr )
202+ t .Errorf ("dc. ConnAddrFromSlice(%x ) returned %s, expected %s" , b , addr , expectedAddr )
175203 }
176204 if ! bytes .Equal (b [n :], expectedTail ) {
177- t .Error ( " ConnAddrFromSlice(b ) modified non-address bytes." )
205+ t .Errorf ( "dc. ConnAddrFromSlice(%x ) modified non-address bytes" , b )
178206 }
179- return cachedDomain
180207}
181208
182209func TestConnAddrFromSliceWithDomainCache (t * testing.T ) {
183- const s = "🌐"
184- cachedDomain := s
185-
186- cachedDomain = testConnAddrFromSliceWithDomainCache (t , addr4 [:], cachedDomain , addr4connaddr )
187- if cachedDomain != s {
188- t .Errorf ("ConnAddrFromSliceWithDomainCache(addr4) modified cachedDomain to %s." , cachedDomain )
189- }
210+ var dc DomainCache
211+ b := make ([]byte , 512 )
190212
191- cachedDomain = testConnAddrFromSliceWithDomainCache (t , addr4in6 [:], cachedDomain , addr4connaddr )
192- if cachedDomain != s {
193- t .Errorf ("ConnAddrFromSliceWithDomainCache(addr4in6) modified cachedDomain to %s." , cachedDomain )
213+ if n := testing .AllocsPerRun (10 , func () {
214+ testConnAddrFromSliceWithDomainCache (t , b , addr4 [:], & dc , addr4connaddr )
215+ testConnAddrFromSliceWithDomainCache (t , b , addr4in6 [:], & dc , addr4connaddr )
216+ testConnAddrFromSliceWithDomainCache (t , b , addr6 [:], & dc , addr6connaddr )
217+ testConnAddrFromSliceWithDomainCache (t , b , addrDomain [:], & dc , addrDomainConnAddr )
218+ }); n > 0 {
219+ t .Errorf ("AllocsPerRun(10, ...) = %f, want 0" , n )
194220 }
195221
196- cachedDomain = testConnAddrFromSliceWithDomainCache (t , addr6 [:], cachedDomain , addr6connaddr )
197- if cachedDomain != s {
198- t .Errorf ("ConnAddrFromSliceWithDomainCache(addr6) modified cachedDomain to %s." , cachedDomain )
222+ const addrDomain2Host = "www.google.com"
223+ addrDomain2 := [1 + 1 + len (addrDomain2Host ) + 2 ]byte {
224+ AtypDomainName ,
225+ byte (len (addrDomain2Host )),
226+ 'w' , 'w' , 'w' , '.' , 'g' , 'o' , 'o' , 'g' , 'l' , 'e' , '.' , 'c' , 'o' , 'm' ,
227+ byte (addrDomainPort >> 8 ), byte (addrDomainPort & 0xff ),
199228 }
229+ addrDomain2ConnAddr := conn .MustAddrFromDomainPort (addrDomain2Host , addrDomainPort )
200230
201- cachedDomain = testConnAddrFromSliceWithDomainCache (t , addrDomain [:], cachedDomain , addrDomainConnAddr )
202- if cachedDomain != addrDomainHost {
203- t .Errorf ("ConnAddrFromSliceWithDomainCache(addrDomain) modified cachedDomain to %s, expected %s." , cachedDomain , addrDomainHost )
231+ if n := testing .AllocsPerRun (10 , func () {
232+ testConnAddrFromSliceWithDomainCache (t , b , addrDomain2 [:], & dc , addrDomain2ConnAddr )
233+ }); n > 0 {
234+ t .Errorf ("AllocsPerRun(10, ...) = %f, want 0" , n )
204235 }
205236}
206237
0 commit comments